windows kernel internals user-mode heap manager heap stats 0:006> !heap -s the process has the...

55
Windows Kernel Internals User-mode Heap Manager David B. Probert, Ph.D. Windows Kernel Development Microsoft Corporation

Upload: duongcong

Post on 23-May-2018

241 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Windows Kernel InternalsUser-mode Heap Manager

David B. Probert, Ph.D.Windows Kernel Development

Microsoft Corporation

Page 2: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Topics

• Common problems with the NT heap• LFH design• Benchmarks data• Heap analysis

Page 3: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Default NT Heap• Unbounded fragmentation for the worst

scenario:– External fragmentation– Virtual address fragmentation

• Poor performance for:– Large heaps– SMP– Large blocks– Fast growing scenarios– Fragmented heaps

Page 4: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Goals For LFH

• Bounded low fragmentation• Low risk (minimal impact)• Stable and high performance for:

– Large heaps– Large blocks– SMP– Long running applications

Page 5: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

LFH Design

• Bucket-oriented heap• Better balance between internal and

external fragmentation• Improved data locality• No locking for most common paths

Page 6: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Tradeoffs

• Performance / footprint• Internal / external fragmentation• Thread / processor data locality• Using prefetch techniques

Page 7: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

LFH

NT Heap

NT Memory Manager

Block Size16 K0 1K 512 K

Page 8: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

8 16128

Buckets 16 K

NT Heap

Page 9: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Allocation Granularity

165121638416256819616128409616642048163210241616512328256

BucketsGranularityBlock Size

Page 10: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

8 16128

Buckets 16 K

NT Heap

Page 11: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Active segmentSegmentqueue

Descriptor

User data area

Unmanaged segments

Page 12: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Active segmentSegment

queue

Descriptor

User data area

Unmanaged segments

Alloc

Page 13: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Free

Segmentqueue

Active segment

Unmanaged segments

Page 14: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

8 16Buckets 16 K

Descriptorscache

Large segments cache

NT Heap

Page 15: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Free

Segmentqueue

Active segment

Unmanaged segments

Page 16: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

8 16Buckets 16 K

Descriptorscache

Large segments cache

NT Heap

Page 17: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Improving the SMP Scalability

• Thread locality• Processor locality

Page 18: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Thread Data Locality• Advantages

– Easy to implement (TLS)– Can reduce the number of interlocked instructions

• Disadvantages– Significantly larger footprint for high number of threads– Common source of leaks (the cleanup is not guaranteed)– Larger footprint for scenarios involving cross thread

operations– Performance issues at low memory (larger footprint can

cause paging)– Increases the CPU cost per thread creation / deletion

Page 19: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Processor Locality• Advantages

– The memory footprint is bounded to the number of CPUs regardless of the number of threads

– Expands the structures only if needed– No cleanup issues

• Disadvantages– The current CPU is not available in user mode– Not efficient for a large number of processors and

few threads

Page 20: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

MP Scalability

16

DescriptorscacheDescriptors

cacheDescriptorscache

168 16 16 K

Affinity manager Large segments cache

NT Heap

Page 21: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Better Than Lookaside

• Better data locality (likely in same page)• Almost perfect SMP scalability (no false sharing)• Covers a larger size range (up to 16k blocks)• Works well regardless of the number of blocks• Non-blocking operations even during growing

and shrinking phases

Page 22: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Benchmarks

• Fragmentation• Speed• Scalability• Memory efficiency

Page 23: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Fragmentationtest for 266 MB limit

14%88%Fragmentation

224 MB26 MBBusy

7 MB4 MBFree

39 MB235 MBUncommited

LFHDefault

Page 24: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Default NT Heap

88%

2%10%

Uncommited

Free

Busy

Page 25: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Low Fragmentation Heap

14%

3%

83%

Uncommited

Free

Busy

Page 26: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

External FragmentationTest (70 MB)

14% + 12%46% + 36%Fragmentation

46 MB12 MBBusy

8 MB32 MBFree

7 MB25 MBUncommited

LFHDefault

Page 27: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

NT Heap at 70 M usage( 8478 UCR, 10828 free blocks )

36%

46%

18%

Uncommited

Free

Busy

Page 28: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Low Fragmentation Heap at 70 M(417 UCR, 1666 free blocks)

12%

14%

74%

UncommitedFreeBusy

Page 29: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test0-1k, 10000 blocks (4P x 200MHz)

0

200000

400000

600000

800000

1000000

1200000

1 2 4 8 16 32 64 128Threads

Aloc

s/se

c

LFHNT

Page 30: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test0-1k, 10000 blocks

0

0.5

1

1.5

2

2.5

1 2 4 8 16 32 64 128Threads

Mem

eff.

LFHNT

Page 31: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test1-2k, 10000 blocks

0

200000

400000

600000

800000

1000000

1200000

1 2 4 8 16 32Threads

Alo

cs/s

ec

LFHNT

Page 32: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test1-2k, 10000 blocks

00.20.40.60.8

11.21.41.61.8

1 2 4 8 16 32

Threads

Mem

eff

.

LFHNT

Page 33: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test on a 32P machine0-1k, 100000 blocks

100000

1000000

10000000

100000000

1 2 4 8 16 32 64 128 256 512

Threads (log)

Ops

/sec

(log

) LFHNTIdeal

Page 34: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test on 32P machine0-1k, 100000 blocks

00.2

0.40.6

0.81

1.21.4

1.61.8

2

1 2 4 8 16 32 64 128 256 512

Threads (log)

Mem

. Eff. LFH

NT

Page 35: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test on 32P machine22 bytes, 100000 blocks

10000

100000

1000000

10000000

100000000

1 2 4 8 16 32 64 128 256 512

Threads (log)

Ops

/sec

(log

)

LFHNTIdeal

Page 36: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test on 32P machine1k-2k, 100000 blocks

1000

10000

100000

1000000

10000000

100000000

1 2 4 8 16 32 64 128 256 512

Threads (log)

Ops

/sec

(log

)

LFHNTIdeal

Page 37: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Replacement test on 32P machine1k-2k, 100000 blocks

0

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

1 2 4 8 16 32 64 128 256 512Threads (log)

Mem

. Eff. LFH

NT

Page 38: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Larson MT test on 32P machine0 - 1k, 3000 blocks/thread

0

5000000

10000000

15000000

20000000

25000000

30000000

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31

Threads

Ops

/sec

LFHNTIdeal

Page 39: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Larson MT test on 32P machine0 - 1k, 3000 blocks/thread

100000

1000000

10000000

100000000

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31

Threads

Ops

/sec

(log

) LFHNTIdeal

Page 40: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Larson MT test on 32P machine0 - 1k, 3000 blocks / thread

0

20

40

60

80

100

120

140

160

180

200

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31Threads

Mem

. Eff.

%

LFHNT

Page 41: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Larson MT test on 32P machine1k -2k, 100000 blocks

0

5000000

10000000

15000000

20000000

25000000

30000000

1 4 7 10 13 16 19 22 25 28 31

Threads

Ops

/sec LFH

NT

Ideal

Page 42: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Larson MT test on 32P machine1k -2k, 100000 blocks

1000

10000

100000

1000000

10000000

100000000

1 4 7 10 13 16 19 22 25 28 31

Threads

Ops

/sec

. (lo

g)

LFH

NT

Ideal

Page 43: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Aggressive alloc test on 32P machine50 Mbytes allocs in blocks of 32 bytes

100

1000

10000

100000

1000000

1 2 4 8 16 32 64

Threads (log)

Tim

e (m

sec)

- lo

g

LFHNT

Page 44: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

When is the Default Heap Preferred

• ~95% of applications• The heap operations are rare • Low memory usage

Page 45: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Where LFH is Recommended

• High memory usage and:– High external fragmentation (> 10-15%)– High virtual address fragmentation (>10-15%)

• Performance degradation on long run• High heap lock contention• Aggressive usage of large blocks (> 1K)

Page 46: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Activating LFH

• HeapSetInformation– Can be called any time after the heap creation– Restriction for some flags (HEAP_NO_SERIALIZE, debug flags)– Can be destroyed only with the entire heap

• HeapQueryInformation– Retrieve the current front end heap type

• 0 – none• 1 – lookaside• 2 – LFH

Page 47: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Heap Analysis

• !heap to collect statistics and validate the heap– !heap –s– !heap –s heap_addr –b8– !heap –s heap_addr –d40

• Perfmon

Page 48: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Overall Heap Stats

0:001> !heap –s

Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast(k) (k) (k) (k) length blocks cont. heap

-----------------------------------------------------------------------------00080000 00000002 1024 28 28 14 1 1 0 0 L00180000 00008000 64 4 4 2 1 1 0 000250000 00001002 64 24 24 6 1 1 0 0 L00270000 00001002 130304 58244 96888 36722 10828 8478 0 0 L

External fragmentation 63 % (10828 free blocks)Virtual address fragmentation 39 % (8478 uncommited ranges)

-----------------------------------------------------------------------------

Page 49: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Overall Heap Stats

0:000> !heap –s

Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast(k) (k) (k) (k) length blocks cont. heap

-----------------------------------------------------------------------------00080000 00000002 1024 28 28 16 2 1 0 000180000 00008000 64 4 4 2 1 1 0 000250000 00001002 64 24 24 6 1 1 0 000270000 00001002 256 116 116 5 1 1 0 0002b0000 00001002 130304 122972 122972 1936 67 1 0 14d5b8

Lock contention 1365432-----------------------------------------------------------------------------

Page 50: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Overall Heap Stats0:006> !heap -s

The process has the following heap extended settings 00000008:- Low Fragmentation Heap activated for all heaps

Affinity manager status:- Virtual affinity limit 8- Current entries in use 4- Statistics: Swaps=18, Resets=0, Allocs=18

Heap Flags Reserv Commit Virt Free List UCR Virt Lock Fast(k) (k) (k) (k) length blocks cont. heap

-----------------------------------------------------------------------------00080000 00000002 1024 432 432 2 1 1 0 0 LFH00180000 00008000 64 4 4 2 1 1 0 000250000 00001002 1088 364 364 1 1 1 0 0 LFH00370000 00001002 256 212 212 3 1 1 0 0 LFH003b0000 00001002 7424 5720 6240 43 3 26 0 f LFH-----------------------------------------------------------------------------

Page 51: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Default NT Heap Side0:006> !heap -s 003b0000

Walking the heap 003b0000 ....0: Heap 003b0000Flags 00001002 - HEAP_GROWABLEReserved 7424 (k)Commited 5720 (k)Virtual bytes 6240 (k)Free space 43 (k)External fragmentation 0% (3 free blocks)Virtual address fragmentation 8% (26 uncommited ranges)Virtual blocks 0Lock contention 15Segments 42432 hash table for the free list

Commits 0Decommitts 0

... Page 1/3

Page 52: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

LFH Heap SideLow fragmentation heap 003b0688

Lock contention 4Metadata usage 76800Statistics:

Segments created 2236Segments deleted 733Segments reused 0Conversions 0ConvertedSpace 0

Block cache:Free blocks 0Sequence 0Cache blocks 0 0 14 37 70 74 19Available 0 0 79 252 517 795 74

... Page 2/3

Page 53: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Default NT Heap Side0:006> !heap -s 003b0000

Walking the heap 003b0000 ....0: Heap 003b0000Flags 00001002 - HEAP_GROWABLEReserved 7424 (k)Commited 5720 (k)Virtual bytes 6240 (k)Free space 43 (k)External fragmentation 0% (3 free blocks)Virtual address fragmentation 8% (26 uncommited ranges)Virtual blocks 0Lock contention 15Segments 42432 hash table for the free list

Commits 0Decommitts 0

... Page 1/3

Page 54: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Blocks Distribution

Default heap Front heapRange (bytes) Busy Free Busy Free

-----------------------------------------------0 - 1024 18 83 49997 9118

1024 - 2048 113 0 0 02048 - 3072 70 1 0 04096 - 5120 74 0 0 08192 - 9216 19 2 0 0

16384 - 17408 9 0 0 032768 - 33792 8 0 0 0

104448 - 105472 1 0 0 0-----------------------------------------------

Total 312 86 49997 9118

Page 3/3

Page 55: Windows Kernel Internals User-mode Heap Manager Heap Stats 0:006> !heap -s The process has the following heap extended settings 00000008: - Low Fragmentation Heap activated for all

Discussion