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 * ---------------------------------------------------------------------------*/
17 extern lnat RTS_VAR(mblocks_allocated);
19 extern void initMBlocks(void);
20 extern void * getMBlock(void);
21 extern void * getMBlocks(nat n);
22 extern void freeAllMBlocks(void);
25 extern void *getFirstMBlock(void);
26 extern void *getNextMBlock(void *mblock);
29 /* -----------------------------------------------------------------------------
30 The HEAP_ALLOCED() test.
32 HEAP_ALLOCED is called FOR EVERY SINGLE CLOSURE during GC.
35 See wiki commentary at
36 http://hackage.haskell.org/trac/ghc/wiki/Commentary/HeapAlloced
38 Implementation of HEAP_ALLOCED
39 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 Since heap is allocated in chunks of megablocks (MBLOCK_SIZE), we
42 can just use a table to record which megablocks in the address
43 space belong to the heap. On a 32-bit machine, with 1Mb
44 megablocks, using 8 bits for each entry in the table, the table
45 requires 4k. Lookups during GC will be fast, because the table
46 will be quickly cached (indeed, performance measurements showed no
47 measurable difference between doing the table lookup and using a
50 On 64-bit machines, we cache one 12-bit block map that describes
51 4096 megablocks or 4GB of memory. If HEAP_ALLOCED is called for
52 an address that is not in the cache, it calls slowIsHeapAlloced
53 (see MBlock.c) which will find the block map for the 4GB block in
55 -------------------------------------------------------------------------- */
57 #if SIZEOF_VOID_P == 4
58 extern StgWord8 mblock_map[];
60 /* On a 32-bit machine a 4KB table is always sufficient */
61 # define MBLOCK_MAP_SIZE 4096
62 # define MBLOCK_MAP_ENTRY(p) ((StgWord)(p) >> MBLOCK_SHIFT)
63 # define HEAP_ALLOCED(p) mblock_map[MBLOCK_MAP_ENTRY(p)]
64 # define HEAP_ALLOCED_GC(p) HEAP_ALLOCED(p)
66 /* -----------------------------------------------------------------------------
67 HEAP_ALLOCED for 64-bit machines.
69 Here are some cache layout options:
72 16KB cache of 16-bit entries, 1MB lines (capacity 8GB)
74 entries = 8192 13 bits
75 line size = 0 bits (1 bit of value)
80 32KB cache of 16-bit entries, 4MB lines (capacity 32GB)
82 entries = 16384 14 bits
83 line size = 2 bits (4 bits of value)
88 16KB cache of 16-bit entries, 2MB lines (capacity 16GB)
90 entries = 8192 13 bits
91 line size = 1 bits (2 bits of value)
96 4KB cache of 32-bit entries, 16MB lines (capacity 16GB)
98 entries = 1024 10 bits
99 line size = 4 bits (16 bits of value)
104 4KB cache of 64-bit entries, 32MB lines (capacity 16GB)
105 mblock size = 20 bits
107 line size = 5 bits (32 bits of value)
111 We actually use none of the above. After much experimentation it was
112 found that optimising the lookup is the most important factor,
113 followed by reducing the number of misses. To that end, we use a
114 variant of [1] in which each cache entry is ((mblock << 1) + value)
115 where value is 0 for non-heap and 1 for heap. The cache entries can
116 be 32 bits, since the mblock number is 48-20 = 28 bits, and we need
117 1 bit for the value. The cache can be as big as we like, but
118 currently we use 8k entries, giving us 8GB capacity.
120 ---------------------------------------------------------------------------- */
122 #elif SIZEOF_VOID_P == 8
124 #define MBC_LINE_BITS 0
125 #define MBC_TAG_BITS 15
126 typedef StgWord32 MbcCacheLine; // could use 16, but 32 was faster
127 typedef StgWord8 MBlockMapLine;
129 #define MBLOCK_MAP_LINE(p) (((StgWord)p & 0xffffffff) >> (MBLOCK_SHIFT + MBC_LINE_BITS))
131 #define MBC_LINE_SIZE (1<<MBC_LINE_BITS)
132 #define MBC_SHIFT (48 - MBLOCK_SHIFT - MBC_LINE_BITS - MBC_TAG_BITS)
133 #define MBC_ENTRIES (1<<MBC_SHIFT)
135 extern MbcCacheLine mblock_cache[];
137 #define MBC_LINE(p) ((StgWord)p >> (MBLOCK_SHIFT + MBC_LINE_BITS))
139 #define MBLOCK_MAP_ENTRIES (1 << (32 - MBLOCK_SHIFT - MBC_LINE_BITS))
142 StgWord32 addrHigh32;
143 MBlockMapLine lines[MBLOCK_MAP_ENTRIES];
146 extern lnat mpc_misses;
148 StgBool HEAP_ALLOCED_miss(StgWord mblock, void *p);
151 StgBool HEAP_ALLOCED(void *p)
155 MbcCacheLine entry, value;
157 mblock = (StgWord)p >> MBLOCK_SHIFT;
158 entry_no = mblock & (MBC_ENTRIES-1);
159 entry = mblock_cache[entry_no];
160 value = entry ^ (mblock << 1);
161 // this formulation coaxes gcc into prioritising the value==1
162 // case, which we expect to be the most common.
163 // __builtin_expect() didn't have any useful effect (gcc-4.3.0).
166 } else if (value == 0) {
169 // putting the rest out of line turned out to be a slight
170 // performance improvement:
171 return HEAP_ALLOCED_miss(mblock,p);
175 // In the parallel GC, the cache itself is safe to *read*, and can be
176 // updated atomically, but we need to place a lock around operations
177 // that touch the MBlock map.
179 StgBool HEAP_ALLOCED_GC(void *p)
183 MbcCacheLine entry, value;
186 mblock = (StgWord)p >> MBLOCK_SHIFT;
187 entry_no = mblock & (MBC_ENTRIES-1);
188 entry = mblock_cache[entry_no];
189 value = entry ^ (mblock << 1);
192 } else if (value == 0) {
195 // putting the rest out of line turned out to be a slight
196 // performance improvement:
197 ACQUIRE_SPIN_LOCK(&gc_alloc_block_sync);
198 b = HEAP_ALLOCED_miss(mblock,p);
199 RELEASE_SPIN_LOCK(&gc_alloc_block_sync);
205 # error HEAP_ALLOCED not defined
208 #endif /* MBLOCK_H */