| 1 |
/** |
| 2 |
BlkInfo thread-local cache. Used for array appending in the conservative GC to avoid the lock when possible. |
| 3 |
|
| 4 |
Note: this used to be in rt.lifetime, but was moved here to allow GCs to take over array operations. |
| 5 |
*/ |
| 6 |
module core.internal.gc.blkcache; |
| 7 |
|
| 8 |
import core.memory; |
| 9 |
import core.attribute; |
| 10 |
|
| 11 |
debug (PRINTF) import core.stdc.stdio : printf; |
| 12 |
|
| 13 |
alias BlkInfo = GC.BlkInfo; |
| 14 |
alias BlkAttr = GC.BlkAttr; |
| 15 |
|
| 16 |
/** |
| 17 |
cache for the lookup of the block info |
| 18 |
*/ |
| 19 |
private enum N_CACHE_BLOCKS = 8; |
| 20 |
|
| 21 |
// note this is TLS, so no need to sync. |
| 22 |
BlkInfo *__blkcache_storage; |
| 23 |
|
| 24 |
static if (N_CACHE_BLOCKS == 1) |
| 25 |
{ |
| 26 |
version=single_cache; |
| 27 |
} |
| 28 |
else |
| 29 |
{ |
| 30 |
//version=simple_cache; // uncomment to test simple cache strategy |
| 31 |
//version=random_cache; // uncomment to test random cache strategy |
| 32 |
|
| 33 |
// ensure N_CACHE_BLOCKS is power of 2. |
| 34 |
static assert(!((N_CACHE_BLOCKS - 1) & N_CACHE_BLOCKS)); |
| 35 |
|
| 36 |
version (random_cache) |
| 37 |
{ |
| 38 |
int __nextRndNum = 0; |
| 39 |
} |
| 40 |
int __nextBlkIdx; |
| 41 |
} |
| 42 |
|
| 43 |
@property BlkInfo *__blkcache() nothrow @nogc |
| 44 |
{ |
| 45 |
if (!__blkcache_storage) |
| 46 |
{ |
| 47 |
import core.stdc.stdlib : calloc; |
| 48 |
import core.thread.threadbase; |
| 49 |
auto tBase = ThreadBase.getThis(); |
| 50 |
if (tBase is null) |
| 51 |
// if we don't have a thread object, this is a detached thread, and |
| 52 |
// this won't be properly maintained by the GC. |
| 53 |
return null; |
| 54 |
|
| 55 |
// allocate the block cache for the first time |
| 56 |
immutable size = BlkInfo.sizeof * N_CACHE_BLOCKS; |
| 57 |
// use C alloc, because this may become a detached thread, and the GC |
| 58 |
// would then clean up the cache without zeroing this pointer. |
| 59 |
__blkcache_storage = cast(BlkInfo*) calloc(size, 1); |
| 60 |
tBase.tlsGCData = __blkcache_storage; |
| 61 |
} |
| 62 |
return __blkcache_storage; |
| 63 |
} |
| 64 |
|
| 65 |
// free the allocation on thread exit. |
| 66 |
@standalone static ~this() |
| 67 |
{ |
| 68 |
if (__blkcache_storage) |
| 69 |
{ |
| 70 |
import core.stdc.stdlib : free; |
| 71 |
import core.thread.threadbase; |
| 72 |
auto tBase = ThreadBase.getThis(); |
| 73 |
if (tBase !is null) |
| 74 |
tBase.tlsGCData = null; |
| 75 |
free(__blkcache_storage); |
| 76 |
__blkcache_storage = null; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Indicates whether an address has been marked by the GC. |
| 82 |
*/ |
| 83 |
enum IsMarked : int |
| 84 |
{ |
| 85 |
no, /// Address is not marked. |
| 86 |
yes, /// Address is marked. |
| 87 |
unknown, /// Address is not managed by the GC. |
| 88 |
} |
| 89 |
|
| 90 |
alias IsMarkedDg = IsMarked delegate(void* addr) nothrow; /// The isMarked callback function. |
| 91 |
|
| 92 |
// we expect this to be called with the lock in place |
| 93 |
void processGCMarks(void* data, scope IsMarkedDg isMarked) nothrow |
| 94 |
{ |
| 95 |
if (!data) |
| 96 |
return; |
| 97 |
|
| 98 |
auto cache = cast(BlkInfo*) data; |
| 99 |
// called after the mark routine to eliminate block cache data when it |
| 100 |
// might be ready to sweep |
| 101 |
|
| 102 |
debug(PRINTF) printf("processing GC Marks, %p\n", cache); |
| 103 |
debug(PRINTF) foreach (i; 0 .. N_CACHE_BLOCKS) |
| 104 |
{ |
| 105 |
printf("cache entry %d has base ptr %p\tsize %zd\tflags %x\n", i, cache[i].base, cache[i].size, cache[i].attr); |
| 106 |
} |
| 107 |
auto cache_end = cache + N_CACHE_BLOCKS; |
| 108 |
for (;cache < cache_end; ++cache) |
| 109 |
{ |
| 110 |
if (cache.base != null && isMarked(cache.base) == IsMarked.no) |
| 111 |
{ |
| 112 |
debug(PRINTF) printf("clearing cache entry at %p\n", cache.base); |
| 113 |
cache.base = null; // clear that data. |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
unittest |
| 119 |
{ |
| 120 |
// Bugzilla 10701 - segfault in GC |
| 121 |
ubyte[] result; result.length = 4096; |
| 122 |
GC.free(result.ptr); |
| 123 |
GC.collect(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
Get the cached block info of an interior pointer. Returns null if the |
| 128 |
interior pointer's block is not cached. |
| 129 |
|
| 130 |
NOTE: The following note was not valid, but is retained for historical |
| 131 |
purposes. The data cannot be cleared because the stack contains a |
| 132 |
reference to the affected block (e.g. through `interior`). Therefore, |
| 133 |
the element will not be collected, and the data will remain valid. |
| 134 |
|
| 135 |
ORIGINAL: The base ptr in this struct can be cleared asynchronously by the GC, |
| 136 |
so any use of the returned BlkInfo should copy it and then check the |
| 137 |
base ptr of the copy before actually using it. |
| 138 |
*/ |
| 139 |
BlkInfo *__getBlkInfo(void *interior) nothrow @nogc |
| 140 |
{ |
| 141 |
BlkInfo *ptr = __blkcache; |
| 142 |
if (ptr is null) |
| 143 |
// if for some reason we don't have a cache, return null. |
| 144 |
return null; |
| 145 |
version (single_cache) |
| 146 |
{ |
| 147 |
if (ptr.base && ptr.base <= interior && (interior - ptr.base) < ptr.size) |
| 148 |
return ptr; |
| 149 |
return null; // not in cache. |
| 150 |
} |
| 151 |
else version (simple_cache) |
| 152 |
{ |
| 153 |
foreach (i; 0..N_CACHE_BLOCKS) |
| 154 |
{ |
| 155 |
if (ptr.base && ptr.base <= interior && (interior - ptr.base) < ptr.size) |
| 156 |
return ptr; |
| 157 |
ptr++; |
| 158 |
} |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
// try to do a smart lookup, using __nextBlkIdx as the "head" |
| 163 |
auto curi = ptr + __nextBlkIdx; |
| 164 |
for (auto i = curi; i >= ptr; --i) |
| 165 |
{ |
| 166 |
if (i.base && i.base <= interior && cast(size_t)(interior - i.base) < i.size) |
| 167 |
return i; |
| 168 |
} |
| 169 |
|
| 170 |
for (auto i = ptr + N_CACHE_BLOCKS - 1; i > curi; --i) |
| 171 |
{ |
| 172 |
if (i.base && i.base <= interior && cast(size_t)(interior - i.base) < i.size) |
| 173 |
return i; |
| 174 |
} |
| 175 |
} |
| 176 |
return null; // not in cache. |
| 177 |
} |
| 178 |
|
| 179 |
void __insertBlkInfoCache(BlkInfo bi, BlkInfo *curpos) nothrow @nogc |
| 180 |
{ |
| 181 |
auto cache = __blkcache; |
| 182 |
if (cache is null) |
| 183 |
// no cache to use. |
| 184 |
return; |
| 185 |
|
| 186 |
version (single_cache) |
| 187 |
{ |
| 188 |
*cache = bi; |
| 189 |
return; |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
version (simple_cache) |
| 194 |
{ |
| 195 |
if (curpos) |
| 196 |
*curpos = bi; |
| 197 |
else |
| 198 |
{ |
| 199 |
// note, this is a super-simple algorithm that does not care about |
| 200 |
// most recently used. It simply uses a round-robin technique to |
| 201 |
// cache block info. This means that the ordering of the cache |
| 202 |
// doesn't mean anything. Certain patterns of allocation may |
| 203 |
// render the cache near-useless. |
| 204 |
cache[__nextBlkIdx] = bi; |
| 205 |
__nextBlkIdx = (__nextBlkIdx+1) & (N_CACHE_BLOCKS - 1); |
| 206 |
} |
| 207 |
} |
| 208 |
else version (random_cache) |
| 209 |
{ |
| 210 |
// strategy: if the block currently is in the cache, move the |
| 211 |
// current block index to the a random element and evict that |
| 212 |
// element. |
| 213 |
if (!curpos) |
| 214 |
{ |
| 215 |
__nextBlkIdx = (__nextRndNum = 1664525 * __nextRndNum + 1013904223) & (N_CACHE_BLOCKS - 1); |
| 216 |
curpos = cache + __nextBlkIdx; |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
__nextBlkIdx = curpos - cache; |
| 221 |
} |
| 222 |
*curpos = bi; |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
// |
| 227 |
// strategy: If the block currently is in the cache, swap it with |
| 228 |
// the head element. Otherwise, move the head element up by one, |
| 229 |
// and insert it there. |
| 230 |
// |
| 231 |
if (!curpos) |
| 232 |
{ |
| 233 |
__nextBlkIdx = (__nextBlkIdx+1) & (N_CACHE_BLOCKS - 1); |
| 234 |
curpos = cache + __nextBlkIdx; |
| 235 |
} |
| 236 |
else if (curpos !is cache + __nextBlkIdx) |
| 237 |
{ |
| 238 |
*curpos = cache[__nextBlkIdx]; |
| 239 |
curpos = cache + __nextBlkIdx; |
| 240 |
} |
| 241 |
*curpos = bi; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
debug(PRINTF) |
| 247 |
{ |
| 248 |
extern(C) void printArrayCache() |
| 249 |
{ |
| 250 |
auto ptr = __blkcache; |
| 251 |
printf("CACHE: \n"); |
| 252 |
foreach (i; 0 .. N_CACHE_BLOCKS) |
| 253 |
{ |
| 254 |
printf(" %d\taddr:% .8p\tsize:% .10zd\tflags:% .8x\n", i, ptr[i].base, ptr[i].size, ptr[i].attr); |
| 255 |
} |
| 256 |
} |
| 257 |
} |