1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2008
5 * MegaBlock Allocator interface.
7 * See wiki commentary at
8 * http://hackage.haskell.org/trac/ghc/wiki/Commentary/HeapAlloced
10 * ---------------------------------------------------------------------------*/
12 #ifndef RTS_STORAGE_MBLOCK_H
13 #define RTS_STORAGE_MBLOCK_H
15 extern lnat mblocks_allocated;
17 extern void initMBlocks(void);
18 extern void * getMBlock(void);
19 extern void * getMBlocks(nat n);
20 extern void freeAllMBlocks(void);
23 extern void *getFirstMBlock(void);
24 extern void *getNextMBlock(void *mblock);
27 /* -----------------------------------------------------------------------------
28 The HEAP_ALLOCED() test.
30 HEAP_ALLOCED is called FOR EVERY SINGLE CLOSURE during GC.
33 See wiki commentary at
34 http://hackage.haskell.org/trac/ghc/wiki/Commentary/HeapAlloced
36 Implementation of HEAP_ALLOCED
37 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39 Since heap is allocated in chunks of megablocks (MBLOCK_SIZE), we
40 can just use a table to record which megablocks in the address
41 space belong to the heap. On a 32-bit machine, with 1Mb
42 megablocks, using 8 bits for each entry in the table, the table
43 requires 4k. Lookups during GC will be fast, because the table
44 will be quickly cached (indeed, performance measurements showed no
45 measurable difference between doing the table lookup and using a
48 On 64-bit machines, we cache one 12-bit block map that describes
49 4096 megablocks or 4GB of memory. If HEAP_ALLOCED is called for
50 an address that is not in the cache, it calls slowIsHeapAlloced
51 (see MBlock.c) which will find the block map for the 4GB block in
53 -------------------------------------------------------------------------- */
55 #if SIZEOF_VOID_P == 4
56 extern StgWord8 mblock_map[];
58 /* On a 32-bit machine a 4KB table is always sufficient */
59 # define MBLOCK_MAP_SIZE 4096
60 # define MBLOCK_MAP_ENTRY(p) ((StgWord)(p) >> MBLOCK_SHIFT)
61 # define HEAP_ALLOCED(p) mblock_map[MBLOCK_MAP_ENTRY(p)]
62 # define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
64 /* -----------------------------------------------------------------------------
65 HEAP_ALLOCED for 64-bit machines.
67 Here are some cache layout options:
70 16KB cache of 16-bit entries, 1MB lines (capacity 8GB)
72 entries = 8192 13 bits
73 line size = 0 bits (1 bit of value)
78 32KB cache of 16-bit entries, 4MB lines (capacity 32GB)
80 entries = 16384 14 bits
81 line size = 2 bits (4 bits of value)
86 16KB cache of 16-bit entries, 2MB lines (capacity 16GB)
88 entries = 8192 13 bits
89 line size = 1 bits (2 bits of value)
94 4KB cache of 32-bit entries, 16MB lines (capacity 16GB)
96 entries = 1024 10 bits
97 line size = 4 bits (16 bits of value)
102 4KB cache of 64-bit entries, 32MB lines (capacity 16GB)
103 mblock size = 20 bits
105 line size = 5 bits (32 bits of value)
109 We actually use none of the above. After much experimentation it was
110 found that optimising the lookup is the most important factor,
111 followed by reducing the number of misses. To that end, we use a
112 variant of [1] in which each cache entry is ((mblock << 1) + value)
113 where value is 0 for non-heap and 1 for heap. The cache entries can
114 be 32 bits, since the mblock number is 48-20 = 28 bits, and we need
115 1 bit for the value. The cache can be as big as we like, but
116 currently we use 8k entries, giving us 8GB capacity.
118 ---------------------------------------------------------------------------- */
120 #elif SIZEOF_VOID_P == 8
122 #define MBC_LINE_BITS 0
123 #define MBC_TAG_BITS 15
124 typedef StgWord32 MbcCacheLine; // could use 16, but 32 was faster
125 typedef StgWord8 MBlockMapLine;
127 #define MBLOCK_MAP_LINE(p) (((StgWord)p & 0xffffffff) >> (MBLOCK_SHIFT + MBC_LINE_BITS))
129 #define MBC_LINE_SIZE (1<<MBC_LINE_BITS)
130 #define MBC_SHIFT (48 - MBLOCK_SHIFT - MBC_LINE_BITS - MBC_TAG_BITS)
131 #define MBC_ENTRIES (1<<MBC_SHIFT)
133 extern MbcCacheLine mblock_cache[];
135 #define MBC_LINE(p) ((StgWord)p >> (MBLOCK_SHIFT + MBC_LINE_BITS))
137 #define MBLOCK_MAP_ENTRIES (1 << (32 - MBLOCK_SHIFT - MBC_LINE_BITS))
140 StgWord32 addrHigh32;
141 MBlockMapLine lines[MBLOCK_MAP_ENTRIES];
144 extern lnat mpc_misses;
147 extern SpinLock gc_alloc_block_sync;
150 StgBool HEAP_ALLOCED_miss(StgWord mblock, void *p);
153 StgBool HEAP_ALLOCED(void *p)
157 MbcCacheLine entry, value;
159 mblock = (StgWord)p >> MBLOCK_SHIFT;
160 entry_no = mblock & (MBC_ENTRIES-1);
161 entry = mblock_cache[entry_no];
162 value = entry ^ (mblock << 1);
163 // this formulation coaxes gcc into prioritising the value==1
164 // case, which we expect to be the most common.
165 // __builtin_expect() didn't have any useful effect (gcc-4.3.0).
168 } else if (value == 0) {
171 // putting the rest out of line turned out to be a slight
172 // performance improvement:
173 return HEAP_ALLOCED_miss(mblock,p);
177 // In the parallel GC, the cache itself is safe to *read*, and can be
178 // updated atomically, but we need to place a lock around operations
179 // that touch the MBlock map.
181 StgBool HEAP_ALLOCED_GC(void *p)
185 MbcCacheLine entry, value;
188 mblock = (StgWord)p >> MBLOCK_SHIFT;
189 entry_no = mblock & (MBC_ENTRIES-1);
190 entry = mblock_cache[entry_no];
191 value = entry ^ (mblock << 1);
194 } else if (value == 0) {
197 // putting the rest out of line turned out to be a slight
198 // performance improvement:
199 ACQUIRE_SPIN_LOCK(&gc_alloc_block_sync);
200 b = HEAP_ALLOCED_miss(mblock,p);
201 RELEASE_SPIN_LOCK(&gc_alloc_block_sync);
207 # error HEAP_ALLOCED not defined
210 #endif /* RTS_STORAGE_MBLOCK_H */