HeapInfo.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "CommonMemoryPch.h"
  6. #include "Memory/PageHeapBlockTypeFilter.h"
  7. #if defined(TARGET_32)
  8. #include "ValidPointersMap/vpm.32b.h"
  9. #elif defined(TARGET_64)
  10. #include "ValidPointersMap/vpm.64b.h"
  11. #else
  12. #error "Platform is not handled"
  13. #endif
  14. template _ALWAYSINLINE char* HeapInfo::RealAlloc<NoBit, false>(Recycler * recycler, size_t sizeCat, size_t size);
  15. // The VS2013 linker treats this as a redefinition of an already
  16. // defined constant and complains. So skip the declaration if we're compiling
  17. // with VS2013 or below.
  18. #if !defined(_MSC_VER) || _MSC_VER >= 1900
  19. const uint SmallAllocationBlockAttributes::MaxSmallObjectCount;
  20. const uint MediumAllocationBlockAttributes::MaxSmallObjectCount;
  21. #endif
  22. HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes> HeapInfo::smallAllocValidPointersMap;
  23. HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes> HeapInfo::mediumAllocValidPointersMap;
  24. template <class TBlockAttributes>
  25. ValidPointers<TBlockAttributes>::ValidPointers(ushort const * validPointers, uint bucketIndex)
  26. #if USE_VPM_TABLE
  27. : validPointers(validPointers)
  28. #endif
  29. {
  30. #if !USE_VPM_TABLE
  31. Assert(validPointers == nullptr);
  32. maxObjectIndex = CalculateBucketInfo(bucketIndex, &indexPerObject);
  33. #endif
  34. #if DBG && USE_VPM_TABLE
  35. uint localIndexPerObject;
  36. uint localMaxObjectIndex = CalculateBucketInfo(bucketIndex, &localIndexPerObject);
  37. for (uint index = 0; index < TBlockAttributes::MaxSmallObjectCount; index++)
  38. {
  39. Assert(CalculateAddressIndex(index, localIndexPerObject, localMaxObjectIndex) == validPointers[index]);
  40. Assert(CalculateInteriorAddressIndex(index, localIndexPerObject, localMaxObjectIndex) == validPointers[index + TBlockAttributes::MaxSmallObjectCount]);
  41. }
  42. #endif
  43. }
  44. template <class TBlockAttributes>
  45. ushort ValidPointers<TBlockAttributes>::GetAddressIndex(uint index) const
  46. {
  47. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  48. #if USE_VPM_TABLE
  49. return validPointers[index];
  50. #else
  51. return CalculateAddressIndex(index, indexPerObject, maxObjectIndex);
  52. #endif
  53. }
  54. template <class TBlockAttributes>
  55. ushort ValidPointers<TBlockAttributes>::GetInteriorAddressIndex(uint index) const
  56. {
  57. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  58. #if USE_VPM_TABLE
  59. return validPointers[index + TBlockAttributes::MaxSmallObjectCount];
  60. #else
  61. return CalculateInteriorAddressIndex(index, indexPerObject, maxObjectIndex);
  62. #endif
  63. }
  64. #if !USE_VPM_TABLE || DBG
  65. template <class TBlockAttributes>
  66. uint ValidPointers<TBlockAttributes>::CalculateBucketInfo(uint bucketIndex, uint * indexPerObject)
  67. {
  68. uint bucketSize;
  69. if (TBlockAttributes::IsSmallBlock)
  70. {
  71. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::ObjectGranularity * bucketIndex;
  72. }
  73. else
  74. {
  75. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::MediumObjectGranularity * (bucketIndex + 1);
  76. }
  77. *indexPerObject = bucketSize / HeapConstants::ObjectGranularity;
  78. return ((TBlockAttributes::PageCount * AutoSystemInfo::PageSize) / bucketSize) * bucketSize / HeapConstants::ObjectGranularity;
  79. }
  80. template <class TBlockAttributes>
  81. ushort ValidPointers<TBlockAttributes>::CalculateAddressIndex(uint index, uint indexPerObject, uint maxObjectIndex)
  82. {
  83. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  84. if (index >= maxObjectIndex)
  85. {
  86. return (ushort)-1;
  87. }
  88. uint addressIndex = index / indexPerObject;
  89. Assert(addressIndex < USHRT_MAX);
  90. if (addressIndex * indexPerObject != index)
  91. {
  92. return (ushort)-1;
  93. }
  94. return (ushort)addressIndex;
  95. }
  96. template <class TBlockAttributes>
  97. ushort ValidPointers<TBlockAttributes>::CalculateInteriorAddressIndex(uint index, uint indexPerObject, uint maxObjectIndex)
  98. {
  99. Assert(index < TBlockAttributes::MaxSmallObjectCount);
  100. if (index >= maxObjectIndex)
  101. {
  102. return (ushort)-1;
  103. }
  104. uint addressIndex = index / indexPerObject;
  105. Assert(addressIndex < USHRT_MAX);
  106. return (ushort)addressIndex;
  107. }
  108. #endif
  109. #if defined(ENABLE_TEST_HOOKS) || !USE_STATIC_VPM
  110. template <class TBlockAttributes>
  111. void HeapInfo::ValidPointersMap<TBlockAttributes>::GenerateValidPointersMap(ValidPointersMapTable * validTable, InvalidBitsTable& invalidTable, BlockInfoMapTable& blockInfoTable)
  112. {
  113. // Create the valid pointer map to be shared by the buckets.
  114. // Also create the invalid objects bit vector.
  115. ushort * buffer = &((*validTable)[0][0]);
  116. if (buffer)
  117. {
  118. memset(buffer, -1, sizeof(ushort) * 2 * TBlockAttributes::MaxSmallObjectCount * TBlockAttributes::BucketCount);
  119. }
  120. for (uint i = 0; i < TBlockAttributes::BucketCount; i++)
  121. {
  122. // Non-interior first
  123. ushort * validPointers = buffer;
  124. if (buffer)
  125. {
  126. buffer += TBlockAttributes::MaxSmallObjectCount;
  127. }
  128. typename SmallHeapBlockT<TBlockAttributes>::SmallHeapBlockBitVector * invalidBitVector = &invalidTable[i];
  129. invalidBitVector->SetAll();
  130. uint bucketSize;
  131. if (TBlockAttributes::IsSmallBlock)
  132. {
  133. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::ObjectGranularity * i;
  134. }
  135. else
  136. {
  137. bucketSize = TBlockAttributes::MinObjectSize + HeapConstants::MediumObjectGranularity * (i + 1);
  138. }
  139. uint stride = bucketSize / HeapConstants::ObjectGranularity;
  140. uint maxObjectCountForBucket = ((TBlockAttributes::PageCount * AutoSystemInfo::PageSize) / bucketSize);
  141. BlockInfoMapRow* blockInfoRow = &blockInfoTable[i];
  142. memset(blockInfoRow, 0, sizeof(BlockInfoMapRow));
  143. for (ushort j = 0; j < maxObjectCountForBucket; j++)
  144. {
  145. if (validPointers)
  146. {
  147. validPointers[j * stride] = j;
  148. }
  149. uintptr_t objectAddress = j * bucketSize;
  150. Assert(objectAddress / AutoSystemInfo::PageSize < USHRT_MAX);
  151. ushort pageIndex = (ushort)(objectAddress / AutoSystemInfo::PageSize);
  152. (*blockInfoRow)[pageIndex].pageObjectCount++;
  153. (*blockInfoRow)[pageIndex].lastObjectIndexOnPage = max(j, (*blockInfoRow)[pageIndex].lastObjectIndexOnPage);
  154. invalidBitVector->Clear(j * stride);
  155. }
  156. if (buffer)
  157. {
  158. // interior pointer
  159. ushort * validInteriorPointers = buffer;
  160. buffer += TBlockAttributes::MaxSmallObjectCount;
  161. for (ushort j = 0; j < maxObjectCountForBucket; j++)
  162. {
  163. uint start = j * stride;
  164. uint end = min(start + stride, TBlockAttributes::MaxSmallObjectCount);
  165. for (uint k = start; k < end; k++)
  166. {
  167. validInteriorPointers[k] = j;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. #endif
  174. #ifdef ENABLE_TEST_HOOKS
  175. template <>
  176. HRESULT HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(FILE* file)
  177. {
  178. #define IfErrorGotoCleanup(result) if ((result) < 0) { hr = E_FAIL; goto cleanup; }
  179. Assert(file != nullptr);
  180. HRESULT hr = S_OK;
  181. // Use heap to allocate the table so we don't bloat the stack (~64k). We only use this function
  182. // to generate headers as part of testing.
  183. ValidPointersMapTable *valid = (ValidPointersMapTable *)malloc(sizeof(ValidPointersMapTable));
  184. InvalidBitsTable *invalid = (InvalidBitsTable *)malloc(sizeof(InvalidBitsTable));
  185. BlockInfoMapTable *blockMap = (BlockInfoMapTable*)malloc(sizeof(BlockInfoMapTable));
  186. if (valid == nullptr || invalid == nullptr || blockMap == nullptr)
  187. {
  188. hr = E_FAIL;
  189. goto cleanup;
  190. }
  191. GenerateValidPointersMap(valid, *invalid, *blockMap);
  192. IfErrorGotoCleanup(fwprintf(file, _u("#if USE_VPM_TABLE\n")))
  193. IfErrorGotoCleanup(fwprintf(file, _u("const ushort HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::validPointersBuffer[HeapConstants::BucketCount][HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::rowSize] = \n{\n")));
  194. // Generate the full buffer.
  195. for (unsigned i = 0; i < HeapConstants::BucketCount; ++i)
  196. {
  197. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  198. for (unsigned j = 0; j < rowSize; ++j)
  199. {
  200. IfErrorGotoCleanup(fwprintf(
  201. file,
  202. (j < rowSize - 1) ? _u("0x%04hX, ") : _u("0x%04hX"),
  203. (*valid)[i][j]));
  204. }
  205. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::BucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  206. }
  207. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  208. IfErrorGotoCleanup(fwprintf(file, _u("#endif // USE_VPM_TABLE\n\n")))
  209. // Generate the invalid bitvectors.
  210. IfErrorGotoCleanup(fwprintf(
  211. file,
  212. _u("const BVUnit HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::invalidBitsData[HeapConstants::BucketCount][SmallHeapBlockT<SmallAllocationBlockAttributes>::SmallHeapBlockBitVector::wordCount] = {\n")));
  213. for (unsigned i = 0; i < HeapConstants::BucketCount; ++i)
  214. {
  215. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  216. for (unsigned j = 0; j < (*invalid)[i].wordCount; ++j)
  217. {
  218. const char16 *format = (j < (*invalid)[i].wordCount - 1) ?
  219. #if defined(TARGET_32)
  220. _u("0x%08X, ") : _u("0x%08X")
  221. #elif defined(TARGET_64)
  222. _u("0x%016I64X, ") : _u("0x%016I64X")
  223. #else
  224. #error "Platform is not handled"
  225. #endif
  226. ;
  227. IfErrorGotoCleanup(fwprintf(file, format, (*invalid)[i].GetRawData()[j]));
  228. }
  229. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::BucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  230. }
  231. IfErrorGotoCleanup(fwprintf(
  232. file,
  233. _u("};\n")
  234. _u("// The following is used to construct the InvalidBitsTable statically without forcing BVStatic to be an aggregate\n")
  235. _u("const HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::InvalidBitsTable * const HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::invalidBitsBuffers =\n")
  236. _u(" reinterpret_cast<const HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::InvalidBitsTable *>(&HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::invalidBitsData);\n")));
  237. // Generate the block map table
  238. IfErrorGotoCleanup(fwprintf(
  239. file,
  240. _u("const SmallHeapBlockT<SmallAllocationBlockAttributes>::BlockInfo HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>::blockInfoBuffer[SmallAllocationBlockAttributes::BucketCount][SmallAllocationBlockAttributes::PageCount] = {\n")));
  241. for (unsigned i = 0; i < HeapConstants::BucketCount; ++i)
  242. {
  243. IfErrorGotoCleanup(fwprintf(file, _u(" // Bucket: %u, Size: %d\n"), i, (int) (HeapConstants::ObjectGranularity + (i * SmallAllocationBlockAttributes::BucketGranularity))));
  244. IfErrorGotoCleanup(fwprintf(file, _u(" {\n")));
  245. for (unsigned j = 0; j < SmallAllocationBlockAttributes::PageCount; ++j)
  246. {
  247. IfErrorGotoCleanup(fwprintf(file, _u(" { ")));
  248. const char16 *format = _u("0x%04hX, 0x%04hX");
  249. IfErrorGotoCleanup(fwprintf(file, format, (*blockMap)[i][j].lastObjectIndexOnPage, (*blockMap)[i][j].pageObjectCount));
  250. IfErrorGotoCleanup(fwprintf(file, (j < SmallAllocationBlockAttributes::PageCount - 1 ? _u(" },\n") : _u(" }\n"))));
  251. }
  252. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::BucketCount - 1 ? _u(" },\n") : _u(" }\n"))));
  253. }
  254. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  255. cleanup:
  256. #undef IfErrorGotoCleanup
  257. free(valid);
  258. free(invalid);
  259. return hr;
  260. }
  261. template <>
  262. HRESULT HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(FILE* file)
  263. {
  264. #define IfErrorGotoCleanup(result) if ((result) < 0) { hr = E_FAIL; goto cleanup; }
  265. Assert(file != nullptr);
  266. HRESULT hr = S_OK;
  267. // Use heap to allocate the table so we don't bloat the stack (~64k). We only use this function
  268. // to generate headers as part of testing.
  269. ValidPointersMapTable *valid = (ValidPointersMapTable *)malloc(sizeof(ValidPointersMapTable));
  270. InvalidBitsTable *invalid = (InvalidBitsTable *)malloc(sizeof(InvalidBitsTable));
  271. BlockInfoMapTable *blockMap = (BlockInfoMapTable *)malloc(sizeof(BlockInfoMapTable));
  272. if (valid == nullptr || invalid == nullptr || blockMap == nullptr)
  273. {
  274. hr = E_FAIL;
  275. goto cleanup;
  276. }
  277. GenerateValidPointersMap(valid, *invalid, *blockMap);
  278. IfErrorGotoCleanup(fwprintf(file, _u("#if USE_VPM_TABLE\n")))
  279. IfErrorGotoCleanup(fwprintf(file, _u("const ushort HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::validPointersBuffer[MediumAllocationBlockAttributes::BucketCount][HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::rowSize] = \n{\n")));
  280. // Generate the full buffer.
  281. for (unsigned i = 0; i < HeapConstants::MediumBucketCount; ++i)
  282. {
  283. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  284. for (unsigned j = 0; j < rowSize; ++j)
  285. {
  286. IfErrorGotoCleanup(fwprintf(
  287. file,
  288. (j < rowSize - 1) ? _u("0x%04hX, ") : _u("0x%04hX"),
  289. (*valid)[i][j]));
  290. }
  291. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::MediumBucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  292. }
  293. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  294. IfErrorGotoCleanup(fwprintf(file, _u("#endif // USE_VPM_TABLE\n\n")))
  295. // Generate the invalid bitvectors.
  296. IfErrorGotoCleanup(fwprintf(
  297. file,
  298. _u("const BVUnit HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::invalidBitsData[MediumAllocationBlockAttributes::BucketCount][SmallHeapBlockT<MediumAllocationBlockAttributes>::SmallHeapBlockBitVector::wordCount] = {\n")));
  299. for (unsigned i = 0; i < HeapConstants::MediumBucketCount; ++i)
  300. {
  301. IfErrorGotoCleanup(fwprintf(file, _u(" {\n ")));
  302. for (unsigned j = 0; j < (*invalid)[i].wordCount; ++j)
  303. {
  304. const char16 *format = (j < (*invalid)[i].wordCount - 1) ?
  305. #if defined(TARGET_32)
  306. _u("0x%08X, ") : _u("0x%08X")
  307. #elif defined(TARGET_64)
  308. _u("0x%016I64X, ") : _u("0x%016I64X")
  309. #else
  310. #error "Platform is not handled"
  311. #endif
  312. ;
  313. IfErrorGotoCleanup(fwprintf(file, format, (*invalid)[i].GetRawData()[j]));
  314. }
  315. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::MediumBucketCount - 1 ? _u("\n },\n") : _u("\n }\n"))));
  316. }
  317. IfErrorGotoCleanup(fwprintf(
  318. file,
  319. _u("};\n")
  320. _u("// The following is used to construct the InvalidBitsTable statically without forcing BVStatic to be an aggregate\n")
  321. _u("const HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::InvalidBitsTable * const HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::invalidBitsBuffers =\n")
  322. _u(" reinterpret_cast<const HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::InvalidBitsTable *>(&HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::invalidBitsData);\n")));
  323. // Generate the block map table
  324. IfErrorGotoCleanup(fwprintf(
  325. file,
  326. _u("const SmallHeapBlockT<MediumAllocationBlockAttributes>::BlockInfo HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>::blockInfoBuffer[MediumAllocationBlockAttributes::BucketCount][MediumAllocationBlockAttributes::PageCount] = {\n")));
  327. for (unsigned i = 0; i < HeapConstants::MediumBucketCount; ++i)
  328. {
  329. IfErrorGotoCleanup(fwprintf(file, _u(" // Bucket: %u, Size: %d\n"), i, (int)(HeapConstants::MaxSmallObjectSize + ((i + 1) * MediumAllocationBlockAttributes::BucketGranularity))));
  330. IfErrorGotoCleanup(fwprintf(file, _u(" {\n")));
  331. for (unsigned j = 0; j < MediumAllocationBlockAttributes::PageCount; ++j)
  332. {
  333. IfErrorGotoCleanup(fwprintf(file, _u(" { ")));
  334. const char16 *format = _u("0x%04hX, 0x%04hX");
  335. IfErrorGotoCleanup(fwprintf(file, format, (*blockMap)[i][j].lastObjectIndexOnPage, (*blockMap)[i][j].pageObjectCount));
  336. IfErrorGotoCleanup(fwprintf(file, (j < MediumAllocationBlockAttributes::PageCount - 1 ? _u(" },\n") : _u(" }\n"))));
  337. }
  338. IfErrorGotoCleanup(fwprintf(file, (i < HeapConstants::MediumBucketCount - 1 ? _u(" },\n") : _u(" }\n"))));
  339. }
  340. IfErrorGotoCleanup(fwprintf(file, _u("};\n")));
  341. cleanup:
  342. #undef IfErrorGotoCleanup
  343. free(valid);
  344. free(invalid);
  345. return hr;
  346. }
  347. template <class TBlockAttributes>
  348. HRESULT HeapInfo::ValidPointersMap<TBlockAttributes>::GenerateValidPointersMapHeader(LPCWSTR vpmFullPath)
  349. {
  350. Assert(vpmFullPath != nullptr);
  351. HRESULT hr = E_FAIL;
  352. FILE * file = nullptr;
  353. if (_wfopen_s(&file, vpmFullPath, _u("w")) == 0 && file != nullptr)
  354. {
  355. const char16 * header =
  356. _u("//-------------------------------------------------------------------------------------------------------\n")
  357. _u("// Copyright (C) Microsoft. All rights reserved.\n")
  358. _u("// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.\n")
  359. _u("//-------------------------------------------------------------------------------------------------------\n")
  360. _u("// Generated via jshost -GenerateValidPointersMapHeader\n")
  361. #if defined(TARGET_32)
  362. _u("// Target platforms: 32bit - x86 & arm\n")
  363. #elif defined(TARGET_64)
  364. _u("// Target platform: 64bit - amd64 & arm64\n")
  365. #else
  366. #error "Platform is not handled"
  367. #endif
  368. _u("#if USE_STATIC_VPM\n")
  369. _u("\n");
  370. if (fwprintf(file, header) >= 0)
  371. {
  372. hr = ValidPointersMap<SmallAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(file);
  373. if (SUCCEEDED(hr))
  374. {
  375. hr = ValidPointersMap<MediumAllocationBlockAttributes>::GenerateValidPointersMapForBlockType(file);
  376. }
  377. fwprintf(file, _u("#endif // USE_STATIC_VPM\n"));
  378. }
  379. fclose(file);
  380. }
  381. return hr;
  382. }
  383. #endif
  384. HeapInfo::HeapInfo() :
  385. recycler(nullptr),
  386. #if ENABLE_CONCURRENT_GC
  387. newLeafHeapBlockList(nullptr),
  388. newNormalHeapBlockList(nullptr),
  389. #ifdef RECYCLER_WRITE_BARRIER
  390. newNormalWithBarrierHeapBlockList(nullptr),
  391. newFinalizableWithBarrierHeapBlockList(nullptr),
  392. #endif
  393. newFinalizableHeapBlockList(nullptr),
  394. #ifdef RECYCLER_VISITED_HOST
  395. newRecyclerVisitedHostHeapBlockList(nullptr),
  396. #endif
  397. newMediumLeafHeapBlockList(nullptr),
  398. newMediumNormalHeapBlockList(nullptr),
  399. #ifdef RECYCLER_WRITE_BARRIER
  400. newMediumNormalWithBarrierHeapBlockList(nullptr),
  401. newMediumFinalizableWithBarrierHeapBlockList(nullptr),
  402. #endif
  403. #ifdef RECYCLER_VISITED_HOST
  404. newMediumRecyclerVisitedHostHeapBlockList(nullptr),
  405. #endif
  406. newMediumFinalizableHeapBlockList(nullptr),
  407. #endif
  408. #ifdef RECYCLER_FINALIZE_CHECK
  409. liveFinalizableObjectCount(0),
  410. pendingDisposableObjectCount(0),
  411. newFinalizableObjectCount(0),
  412. #endif
  413. #if ENABLE_PARTIAL_GC
  414. uncollectedNewPageCount(0),
  415. unusedPartialCollectFreeBytes(0),
  416. #endif
  417. uncollectedAllocBytes(0),
  418. lastUncollectedAllocBytes(0),
  419. pendingZeroPageCount(0)
  420. #ifdef RECYCLER_PAGE_HEAP
  421. , pageHeapMode(PageHeapMode::PageHeapModeOff)
  422. , isPageHeapEnabled(false)
  423. , pageHeapBlockType(PageHeapBlockTypeFilter::PageHeapBlockTypeFilterAll)
  424. , captureAllocCallStack(false)
  425. , captureFreeCallStack(false)
  426. #endif
  427. {
  428. }
  429. HeapInfo::~HeapInfo()
  430. {
  431. RECYCLER_SLOW_CHECK(this->VerifySmallHeapBlockCount());
  432. // Finalize all finalizable object first
  433. for (uint i=0; i < HeapConstants::BucketCount; i++)
  434. {
  435. heapBuckets[i].FinalizeAllObjects();
  436. }
  437. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  438. for (uint i=0; i < HeapConstants::MediumBucketCount; i++)
  439. {
  440. mediumHeapBuckets[i].FinalizeAllObjects();
  441. }
  442. #endif
  443. largeObjectBucket.FinalizeAllObjects();
  444. #if ENABLE_CONCURRENT_GC
  445. SmallFinalizableHeapBucket::FinalizeHeapBlockList(this->newFinalizableHeapBlockList);
  446. MediumFinalizableHeapBucket::FinalizeHeapBlockList(this->newMediumFinalizableHeapBlockList);
  447. #ifdef RECYCLER_VISITED_HOST
  448. SmallRecyclerVisitedHostHeapBucket::FinalizeHeapBlockList(this->newRecyclerVisitedHostHeapBlockList);
  449. MediumRecyclerVisitedHostHeapBucket::FinalizeHeapBlockList(this->newMediumRecyclerVisitedHostHeapBlockList);
  450. #endif
  451. #ifdef RECYCLER_WRITE_BARRIER
  452. SmallFinalizableWithBarrierHeapBucket::FinalizeHeapBlockList(this->newFinalizableWithBarrierHeapBlockList);
  453. MediumFinalizableWithBarrierHeapBucket::FinalizeHeapBlockList(this->newMediumFinalizableWithBarrierHeapBlockList);
  454. #endif
  455. #endif
  456. #ifdef RECYCLER_FINALIZE_CHECK
  457. Assert(liveFinalizableObjectCount == 0);
  458. Assert(pendingDisposableObjectCount == 0);
  459. #endif
  460. // Delete the heap blocks
  461. Recycler * recycler = this->recycler;
  462. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  463. size_t largeBlockCount = this->largeObjectBucket.GetLargeHeapBlockCount(false);
  464. uint mediumBlockCount = 0;
  465. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  466. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  467. {
  468. mediumBlockCount += mediumHeapBuckets[i].GetLargeHeapBlockCount(false);
  469. }
  470. #endif
  471. #endif
  472. RECYCLER_SLOW_CHECK(Assert(this->heapBlockCount[HeapBlock::HeapBlockType::LargeBlockType] - largeBlockCount - mediumBlockCount == 0));
  473. #if ENABLE_CONCURRENT_GC
  474. SmallLeafHeapBucket::DeleteHeapBlockList(this->newLeafHeapBlockList, recycler);
  475. SmallNormalHeapBucket::DeleteHeapBlockList(this->newNormalHeapBlockList, recycler);
  476. #ifdef RECYCLER_WRITE_BARRIER
  477. SmallNormalWithBarrierHeapBucket::DeleteHeapBlockList(this->newNormalWithBarrierHeapBlockList, recycler);
  478. SmallFinalizableWithBarrierHeapBucket::DeleteHeapBlockList(this->newFinalizableWithBarrierHeapBlockList, recycler);
  479. #endif
  480. SmallFinalizableHeapBucket::DeleteHeapBlockList(this->newFinalizableHeapBlockList, recycler);
  481. MediumLeafHeapBucket::DeleteHeapBlockList(this->newMediumLeafHeapBlockList, recycler);
  482. MediumNormalHeapBucket::DeleteHeapBlockList(this->newMediumNormalHeapBlockList, recycler);
  483. #ifdef RECYCLER_WRITE_BARRIER
  484. MediumNormalWithBarrierHeapBucket::DeleteHeapBlockList(this->newMediumNormalWithBarrierHeapBlockList, recycler);
  485. MediumFinalizableWithBarrierHeapBucket::DeleteHeapBlockList(this->newMediumFinalizableWithBarrierHeapBlockList, recycler);
  486. #endif
  487. MediumFinalizableHeapBucket::DeleteHeapBlockList(this->newMediumFinalizableHeapBlockList, recycler);
  488. #ifdef RECYCLER_VISITED_HOST
  489. SmallFinalizableHeapBucket::DeleteHeapBlockList(this->newRecyclerVisitedHostHeapBlockList, recycler);
  490. MediumFinalizableHeapBucket::DeleteHeapBlockList(this->newMediumRecyclerVisitedHostHeapBlockList, recycler);
  491. #endif
  492. #endif
  493. // We do this here, instead of in the Recycler destructor, because the above stuff may
  494. // generate additional tracking events, particularly ReportUnallocated.
  495. // Arguably we shouldn't report these things as ReportUnallocated...
  496. RecyclerMemoryTracking::ReportRecyclerDestroy(recycler);
  497. }
  498. void
  499. HeapInfo::Initialize(Recycler * recycler
  500. #ifdef RECYCLER_PAGE_HEAP
  501. , PageHeapMode pageheapmode
  502. , bool captureAllocCallStack
  503. , bool captureFreeCallStack
  504. #endif
  505. )
  506. {
  507. this->recycler = recycler;
  508. #ifdef DUMP_FRAGMENTATION_STATS
  509. if (recycler->GetRecyclerFlagsTable().DumpFragmentationStats)
  510. {
  511. Output::Print(_u("[FRAG %d] Start"), ::GetTickCount());
  512. }
  513. #endif
  514. #ifdef RECYCLER_PAGE_HEAP
  515. isPageHeapEnabled = false;
  516. PageHeapBlockTypeFilter blockTypeFilter = PageHeapBlockTypeFilter::PageHeapBlockTypeFilterAll;
  517. Js::NumberRange bucketNumberRange;
  518. Js::NumberRange* pBucketNumberRange = &bucketNumberRange;
  519. if (pageheapmode == PageHeapMode::PageHeapModeOff)
  520. {
  521. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  522. isPageHeapEnabled = recycler->GetRecyclerFlagsTable().PageHeap != PageHeapMode::PageHeapModeOff;
  523. pageheapmode = (PageHeapMode)recycler->GetRecyclerFlagsTable().PageHeap;
  524. blockTypeFilter = (PageHeapBlockTypeFilter)recycler->GetRecyclerFlagsTable().PageHeapBlockType;
  525. pBucketNumberRange = &recycler->GetRecyclerFlagsTable().PageHeapBucketNumber;
  526. #else
  527. // @TODO in free build, use environment var or other way to enable page heap
  528. // currently page heap build is enable in free build but has not implemented a way to input the page heap flags.
  529. // if we only need page heap in free test build, just move RECYCLER_PAGE_HEAP definition into ENABLE_DEBUG_CONFIG_OPTIONS
  530. // in CommonDefines.h it should work
  531. #endif
  532. }
  533. else
  534. {
  535. isPageHeapEnabled = true;
  536. }
  537. #ifdef RECYCLER_PAGE_HEAP
  538. if (isPageHeapEnabled)
  539. {
  540. this->captureAllocCallStack = captureAllocCallStack;
  541. this->captureFreeCallStack = captureFreeCallStack;
  542. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  543. this->captureAllocCallStack = captureAllocCallStack || recycler->GetRecyclerFlagsTable().PageHeapAllocStack;
  544. this->captureFreeCallStack = captureFreeCallStack || recycler->GetRecyclerFlagsTable().PageHeapFreeStack;
  545. #endif
  546. }
  547. #endif
  548. if (IsPageHeapEnabled())
  549. {
  550. this->pageHeapMode = pageheapmode;
  551. // Use one of the two modes with -PageHeap flag
  552. Assert(this->pageHeapMode == PageHeapMode::PageHeapModeBlockStart || this->pageHeapMode == PageHeapMode::PageHeapModeBlockEnd);
  553. this->pageHeapBlockType = blockTypeFilter;
  554. for (int i = 0; i < HeapConstants::BucketCount + HeapConstants::MediumBucketCount; i++)
  555. {
  556. if (pBucketNumberRange->InRange(i))
  557. {
  558. if (i < HeapConstants::BucketCount)
  559. {
  560. this->smallBlockPageHeapBucketFilter.Set(i);
  561. }
  562. else
  563. {
  564. this->mediumBlockPageHeapBucketFilter.Set(i - HeapConstants::BucketCount);
  565. }
  566. }
  567. }
  568. }
  569. else
  570. {
  571. // These should not be set if we're not in page heap mode
  572. Assert(!(captureAllocCallStack || captureFreeCallStack));
  573. }
  574. #endif
  575. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  576. {
  577. heapBuckets[i].Initialize(this, (i + 1) << HeapConstants::ObjectAllocationShift);
  578. }
  579. RECYCLER_SLOW_CHECK(memset(this->heapBlockCount, 0, sizeof(this->heapBlockCount)));
  580. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  581. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  582. {
  583. #if SMALLBLOCK_MEDIUM_ALLOC
  584. mediumHeapBuckets[i].Initialize(this, HeapConstants::MaxSmallObjectSize + ((i + 1) * HeapConstants::MediumObjectGranularity));
  585. #else
  586. mediumHeapBuckets[i].Initialize(this, HeapConstants::MaxSmallObjectSize + ((i + 1) * HeapConstants::MediumObjectGranularity), true);
  587. #endif
  588. }
  589. #endif
  590. largeObjectBucket.Initialize(this, HeapConstants::MaxMediumObjectSize);
  591. }
  592. #if defined(PROFILE_RECYCLER_ALLOC) || defined(RECYCLER_MEMORY_VERIFY) || defined(MEMSPECT_TRACKING) || defined(ETW_MEMORY_TRACKING)
  593. void
  594. HeapInfo::Initialize(Recycler * recycler, void(*trackNativeAllocCallBack)(Recycler *, void *, size_t)
  595. #ifdef RECYCLER_PAGE_HEAP
  596. , PageHeapMode pageheapmode
  597. , bool captureAllocCallStack
  598. , bool captureFreeCallStack
  599. #endif
  600. )
  601. {
  602. Initialize(recycler
  603. #ifdef RECYCLER_PAGE_HEAP
  604. , pageheapmode
  605. , captureAllocCallStack
  606. , captureFreeCallStack
  607. #endif
  608. );
  609. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  610. {
  611. heapBuckets[i].GetBucket<NoBit>().GetAllocator()->SetTrackNativeAllocatedObjectCallBack(trackNativeAllocCallBack);
  612. }
  613. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  614. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  615. {
  616. mediumHeapBuckets[i].GetBucket<NoBit>().GetAllocator()->SetTrackNativeAllocatedObjectCallBack(trackNativeAllocCallBack);
  617. }
  618. #endif
  619. }
  620. #endif
  621. #ifdef RECYCLER_PAGE_HEAP
  622. template bool HeapInfo::IsPageHeapEnabledForBlock<MediumAllocationBlockAttributes>(const size_t objectSize);
  623. template bool HeapInfo::IsPageHeapEnabledForBlock<SmallAllocationBlockAttributes>(const size_t objectSize);
  624. template bool HeapInfo::IsPageHeapEnabledForBlock<LargeAllocationBlockAttributes>(const size_t objectSize);
  625. template <typename TBlockAttributes>
  626. bool HeapInfo::IsPageHeapEnabledForBlock(const size_t objectSize)
  627. {
  628. if (IsPageHeapEnabled())
  629. {
  630. if (TBlockAttributes::IsSmallBlock)
  631. {
  632. return smallBlockPageHeapBucketFilter.Test(GetBucketIndex(objectSize)) != 0;
  633. }
  634. else if (TBlockAttributes::IsMediumBlock)
  635. {
  636. return mediumBlockPageHeapBucketFilter.Test(GetMediumBucketIndex(objectSize)) != 0;
  637. }
  638. else
  639. {
  640. return ((byte)this->pageHeapBlockType & (byte)PageHeapBlockTypeFilter::PageHeapBlockTypeFilterLarge) != 0;
  641. }
  642. }
  643. return false;
  644. }
  645. #endif
  646. void
  647. HeapInfo::ResetMarks(ResetMarkFlags flags)
  648. {
  649. for (uint i=0; i < HeapConstants::BucketCount; i++)
  650. {
  651. heapBuckets[i].ResetMarks(flags);
  652. }
  653. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  654. for (uint i=0; i < HeapConstants::MediumBucketCount; i++)
  655. {
  656. mediumHeapBuckets[i].ResetMarks(flags);
  657. }
  658. #endif
  659. largeObjectBucket.ResetMarks(flags);
  660. #if ENABLE_CONCURRENT_GC
  661. if ((flags & ResetMarkFlags_ScanImplicitRoot) != 0)
  662. {
  663. HeapBlockList::ForEach(newLeafHeapBlockList, [flags](SmallLeafHeapBlock * heapBlock)
  664. {
  665. heapBlock->MarkImplicitRoots();
  666. });
  667. HeapBlockList::ForEach(newNormalHeapBlockList, [flags](SmallNormalHeapBlock * heapBlock)
  668. {
  669. heapBlock->MarkImplicitRoots();
  670. });
  671. #ifdef RECYCLER_WRITE_BARRIER
  672. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [flags](SmallNormalWithBarrierHeapBlock * heapBlock)
  673. {
  674. heapBlock->MarkImplicitRoots();
  675. });
  676. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [flags](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  677. {
  678. heapBlock->MarkImplicitRoots();
  679. });
  680. #endif
  681. HeapBlockList::ForEach(newFinalizableHeapBlockList, [flags](SmallNormalHeapBlock * heapBlock)
  682. {
  683. heapBlock->MarkImplicitRoots();
  684. });
  685. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [flags](MediumLeafHeapBlock * heapBlock)
  686. {
  687. heapBlock->MarkImplicitRoots();
  688. });
  689. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [flags](MediumNormalHeapBlock * heapBlock)
  690. {
  691. heapBlock->MarkImplicitRoots();
  692. });
  693. #ifdef RECYCLER_WRITE_BARRIER
  694. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [flags](MediumNormalWithBarrierHeapBlock * heapBlock)
  695. {
  696. heapBlock->MarkImplicitRoots();
  697. });
  698. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [flags](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  699. {
  700. heapBlock->MarkImplicitRoots();
  701. });
  702. #endif
  703. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [flags](MediumNormalHeapBlock * heapBlock)
  704. {
  705. heapBlock->MarkImplicitRoots();
  706. });
  707. }
  708. #endif
  709. }
  710. void
  711. HeapInfo::ScanInitialImplicitRoots()
  712. {
  713. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  714. {
  715. heapBuckets[i].ScanInitialImplicitRoots(recycler);
  716. }
  717. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  718. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  719. {
  720. mediumHeapBuckets[i].ScanInitialImplicitRoots(recycler);
  721. }
  722. #endif
  723. largeObjectBucket.ScanInitialImplicitRoots(recycler);
  724. #if ENABLE_CONCURRENT_GC
  725. // NOTE: Don't need to do newLeafHeapBlockList
  726. HeapBlockList::ForEach(newNormalHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  727. {
  728. heapBlock->ScanInitialImplicitRoots(recycler);
  729. });
  730. #ifdef RECYCLER_WRITE_BARRIER
  731. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [this](SmallNormalWithBarrierHeapBlock * heapBlock)
  732. {
  733. heapBlock->ScanInitialImplicitRoots(recycler);
  734. });
  735. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [this](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  736. {
  737. heapBlock->ScanInitialImplicitRoots(recycler);
  738. });
  739. #endif
  740. HeapBlockList::ForEach(newFinalizableHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  741. {
  742. heapBlock->ScanInitialImplicitRoots(recycler);
  743. });
  744. #endif
  745. #if ENABLE_CONCURRENT_GC
  746. // NOTE: Don't need to do newLeafHeapBlockList
  747. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  748. {
  749. heapBlock->ScanInitialImplicitRoots(recycler);
  750. });
  751. #ifdef RECYCLER_WRITE_BARRIER
  752. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [this](MediumNormalWithBarrierHeapBlock * heapBlock)
  753. {
  754. heapBlock->ScanInitialImplicitRoots(recycler);
  755. });
  756. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [this](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  757. {
  758. heapBlock->ScanInitialImplicitRoots(recycler);
  759. });
  760. #endif
  761. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  762. {
  763. heapBlock->ScanInitialImplicitRoots(recycler);
  764. });
  765. #endif
  766. }
  767. void
  768. HeapInfo::ScanNewImplicitRoots()
  769. {
  770. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  771. {
  772. heapBuckets[i].ScanNewImplicitRoots(recycler);
  773. }
  774. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  775. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  776. {
  777. mediumHeapBuckets[i].ScanNewImplicitRoots(recycler);
  778. }
  779. #endif
  780. largeObjectBucket.ScanNewImplicitRoots(recycler);
  781. #if ENABLE_CONCURRENT_GC
  782. // NOTE: need to do newLeafHeapBlockList to find new memory
  783. HeapBlockList::ForEach(newLeafHeapBlockList, [this](SmallLeafHeapBlock * heapBlock)
  784. {
  785. heapBlock->ScanNewImplicitRoots(recycler);
  786. });
  787. HeapBlockList::ForEach(newNormalHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  788. {
  789. heapBlock->ScanNewImplicitRoots(recycler);
  790. });
  791. #ifdef RECYCLER_WRITE_BARRIER
  792. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [this](SmallNormalWithBarrierHeapBlock * heapBlock)
  793. {
  794. heapBlock->ScanNewImplicitRoots(recycler);
  795. });
  796. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [this](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  797. {
  798. heapBlock->ScanNewImplicitRoots(recycler);
  799. });
  800. #endif
  801. HeapBlockList::ForEach(newFinalizableHeapBlockList, [this](SmallNormalHeapBlock * heapBlock)
  802. {
  803. heapBlock->ScanNewImplicitRoots(recycler);
  804. });
  805. // NOTE: need to do newLeafHeapBlockList to find new memory
  806. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [this](MediumLeafHeapBlock * heapBlock)
  807. {
  808. heapBlock->ScanNewImplicitRoots(recycler);
  809. });
  810. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  811. {
  812. heapBlock->ScanNewImplicitRoots(recycler);
  813. });
  814. #ifdef RECYCLER_WRITE_BARRIER
  815. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [this](MediumNormalWithBarrierHeapBlock * heapBlock)
  816. {
  817. heapBlock->ScanNewImplicitRoots(recycler);
  818. });
  819. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [this](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  820. {
  821. heapBlock->ScanNewImplicitRoots(recycler);
  822. });
  823. #endif
  824. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [this](MediumNormalHeapBlock * heapBlock)
  825. {
  826. heapBlock->ScanNewImplicitRoots(recycler);
  827. });
  828. #endif
  829. }
  830. LargeHeapBlock *
  831. HeapInfo::AddLargeHeapBlock(size_t size)
  832. {
  833. // Do a no-throwing allocation here
  834. return largeObjectBucket.AddLargeHeapBlock(size, /* nothrow = */ true);
  835. }
  836. void HeapInfo::SweepBuckets(RecyclerSweep& recyclerSweep, bool concurrent)
  837. {
  838. Recycler * recycler = recyclerSweep.GetRecycler();
  839. // TODO: Remove below workaround for unreferenced local after enabled -profile for GC
  840. static_cast<Recycler*>(recycler);
  841. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  842. {
  843. heapBuckets[i].SweepFinalizableObjects(recyclerSweep);
  844. }
  845. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  846. // CONCURRENT-TODO: Allow this in the background as well
  847. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  848. {
  849. mediumHeapBuckets[i].SweepFinalizableObjects(recyclerSweep);
  850. }
  851. #endif
  852. #if ENABLE_CONCURRENT_GC
  853. if (concurrent)
  854. {
  855. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  856. RECYCLER_SLOW_CHECK(VerifyLargeHeapBlockCount());
  857. }
  858. if (concurrent)
  859. {
  860. this->SetupBackgroundSweep(recyclerSweep);
  861. }
  862. else
  863. #endif
  864. {
  865. this->SweepSmallNonFinalizable(recyclerSweep);
  866. }
  867. RECYCLER_PROFILE_EXEC_CHANGE(recycler, Js::SweepSmallPhase, Js::SweepLargePhase);
  868. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !(SMALLBLOCK_MEDIUM_ALLOC)
  869. // CONCURRENT-TODO: Allow this in the background as well
  870. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  871. {
  872. mediumHeapBuckets[i].Sweep(recyclerSweep);
  873. }
  874. #endif
  875. largeObjectBucket.Sweep(recyclerSweep);
  876. }
  877. void
  878. HeapInfo::Sweep(RecyclerSweep& recyclerSweep, bool concurrent)
  879. {
  880. #ifdef RECYCLER_FINALIZE_CHECK
  881. this->newFinalizableObjectCount = 0;
  882. #endif
  883. // Initialize this to false. Individual heap buckets can set it to true
  884. Recycler * recycler = recyclerSweep.GetRecycler();
  885. RECYCLER_PROFILE_EXEC_BEGIN(recycler, Js::SweepSmallPhase);
  886. #ifdef RECYCLER_STATS
  887. memset(&recycler->collectionStats.numEmptySmallBlocks, 0, sizeof(recycler->collectionStats.numEmptySmallBlocks));
  888. recycler->collectionStats.numZeroedOutSmallBlocks = 0;
  889. #endif
  890. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  891. // Call finalize before sweeping so that the finalizer can still access object it referenced
  892. largeObjectBucket.Finalize();
  893. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !(SMALLBLOCK_MEDIUM_ALLOC)
  894. for (uint i=0; i < HeapConstants::MediumBucketCount; i++)
  895. {
  896. mediumHeapBuckets[i].Finalize();
  897. }
  898. #endif
  899. #if ENABLE_CONCURRENT_GC
  900. // Merge the new blocks before we sweep the finalizable object in thread
  901. recyclerSweep.MergePendingNewHeapBlockList<SmallFinalizableHeapBlock>();
  902. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumFinalizableHeapBlock>();
  903. #ifdef RECYCLER_WRITE_BARRIER
  904. recyclerSweep.MergePendingNewHeapBlockList<SmallFinalizableWithBarrierHeapBlock>();
  905. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumFinalizableWithBarrierHeapBlock>();
  906. #endif
  907. #ifdef RECYCLER_VISITED_HOST
  908. recyclerSweep.MergePendingNewHeapBlockList<SmallRecyclerVisitedHostHeapBlock>();
  909. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumRecyclerVisitedHostHeapBlock>();
  910. #endif
  911. #endif
  912. SweepBuckets(recyclerSweep, concurrent);
  913. RECYCLER_PROFILE_EXEC_END(recycler, Js::SweepLargePhase);
  914. RECYCLER_SLOW_CHECK(VerifyLargeHeapBlockCount());
  915. RECYCLER_SLOW_CHECK(Assert(this->newFinalizableObjectCount == 0));
  916. }
  917. #if ENABLE_CONCURRENT_GC
  918. void
  919. HeapInfo::SetupBackgroundSweep(RecyclerSweep& recyclerSweep)
  920. {
  921. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  922. {
  923. this->heapBuckets[i].SetupBackgroundSweep(recyclerSweep);
  924. }
  925. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  926. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  927. {
  928. this->mediumHeapBuckets[i].SetupBackgroundSweep(recyclerSweep);
  929. }
  930. #endif
  931. }
  932. #endif
  933. void
  934. HeapInfo::SweepSmallNonFinalizable(RecyclerSweep& recyclerSweep)
  935. {
  936. #if ENABLE_CONCURRENT_GC
  937. recyclerSweep.MergePendingNewHeapBlockList<SmallLeafHeapBlock>();
  938. recyclerSweep.MergePendingNewHeapBlockList<SmallNormalHeapBlock>();
  939. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumLeafHeapBlock>();
  940. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumNormalHeapBlock>();
  941. #ifdef RECYCLER_WRITE_BARRIER
  942. recyclerSweep.MergePendingNewHeapBlockList<SmallNormalWithBarrierHeapBlock>();
  943. recyclerSweep.MergePendingNewMediumHeapBlockList<MediumNormalWithBarrierHeapBlock>();
  944. #endif
  945. // Finalizable are already merge before in SweepHeap
  946. Assert(!recyclerSweep.HasPendingNewHeapBlocks());
  947. #endif
  948. if (!recyclerSweep.IsBackground())
  949. {
  950. // finalizer may trigger arena allocations, do don't suspend the leaf (thread) page allocator
  951. // until we are going to sweep leaf pages.
  952. recycler->GetRecyclerLeafPageAllocator()->SuspendIdleDecommit();
  953. }
  954. for (uint i=0; i<HeapConstants::BucketCount; i++)
  955. {
  956. heapBuckets[i].Sweep(recyclerSweep);
  957. }
  958. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  959. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  960. {
  961. mediumHeapBuckets[i].Sweep(recyclerSweep);
  962. }
  963. #endif
  964. if (!recyclerSweep.IsBackground())
  965. {
  966. // large block don't use the leaf page allocator, we can resume idle decommit now
  967. recycler->GetRecyclerLeafPageAllocator()->ResumeIdleDecommit();
  968. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  969. RECYCLER_SLOW_CHECK(VerifyLargeHeapBlockCount());
  970. }
  971. }
  972. size_t
  973. HeapInfo::Rescan(RescanFlags flags)
  974. {
  975. size_t scannedPageCount = 0;
  976. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  977. {
  978. scannedPageCount += heapBuckets[i].Rescan(recycler, flags);
  979. }
  980. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  981. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  982. {
  983. #if SMALLBLOCK_MEDIUM_ALLOC
  984. scannedPageCount += mediumHeapBuckets[i].Rescan(recycler, flags);
  985. #else
  986. scannedPageCount += mediumHeapBuckets[i].Rescan(flags);
  987. #endif
  988. }
  989. #endif
  990. scannedPageCount += largeObjectBucket.Rescan(flags);
  991. return scannedPageCount;
  992. }
  993. #if ENABLE_MEM_STATS
  994. #ifdef DUMP_FRAGMENTATION_STATS
  995. template <ObjectInfoBits TBucketType>
  996. struct DumpBucketTypeName { static char16 name[]; };
  997. template<> char16 DumpBucketTypeName<NoBit>::name[] = _u("Normal ");
  998. template<> char16 DumpBucketTypeName<LeafBit>::name[] = _u("Leaf ");
  999. template<> char16 DumpBucketTypeName<FinalizeBit>::name[] = _u("Fin ");
  1000. #ifdef RECYCLER_WRITE_BARRIER
  1001. template<> char16 DumpBucketTypeName<WithBarrierBit>::name[] = _u("NormWB ");
  1002. template<> char16 DumpBucketTypeName<FinalizableWithBarrierBit>::name[] = _u("FinWB ");
  1003. #endif
  1004. #ifdef RECYCLER_VISITED_HOST
  1005. template<> char16 DumpBucketTypeName<RecyclerVisitedHostBit>::name[] = _u("Visited");
  1006. #endif
  1007. template <typename TBlockType>
  1008. struct DumpBlockTypeName { static char16 name[]; };
  1009. template<> char16 DumpBlockTypeName<SmallAllocationBlockAttributes>::name[] = _u("(S)");
  1010. template<> char16 DumpBlockTypeName<MediumAllocationBlockAttributes>::name[] = _u("(M)");
  1011. #endif // DUMP_FRAGMENTATION_STATS
  1012. template <ObjectInfoBits TBucketType>
  1013. struct EtwBucketTypeEnum { static uint16 code; };
  1014. template<> uint16 EtwBucketTypeEnum<NoBit>::code = 0;
  1015. template<> uint16 EtwBucketTypeEnum<LeafBit>::code = 1;
  1016. template<> uint16 EtwBucketTypeEnum<FinalizeBit>::code = 2;
  1017. #ifdef RECYCLER_WRITE_BARRIER
  1018. template<> uint16 EtwBucketTypeEnum<WithBarrierBit>::code = 3;
  1019. template<> uint16 EtwBucketTypeEnum<FinalizableWithBarrierBit>::code = 4;
  1020. #endif
  1021. #ifdef RECYCLER_VISITED_HOST
  1022. template<> uint16 EtwBucketTypeEnum<RecyclerVisitedHostBit>::code = 5;
  1023. #endif
  1024. template <typename TBlockType>
  1025. struct EtwBlockTypeEnum { static uint16 code; };
  1026. template<> uint16 EtwBlockTypeEnum<SmallAllocationBlockAttributes>::code = 0;
  1027. template<> uint16 EtwBlockTypeEnum<MediumAllocationBlockAttributes>::code = 1;
  1028. class BucketStatsReporter
  1029. {
  1030. private:
  1031. static const uint16 LargeBucketNameCode = 2 << 8;
  1032. static const uint16 TotalBucketNameCode = 3 << 8;
  1033. Recycler* recycler;
  1034. HeapBucketStats total;
  1035. template <class TBlockAttributes, ObjectInfoBits TBucketType>
  1036. uint16 BucketNameCode() const
  1037. {
  1038. return EtwBucketTypeEnum<TBucketType>::code + (EtwBlockTypeEnum<TBlockAttributes>::code << 8);
  1039. }
  1040. bool IsMemProtectMode() const
  1041. {
  1042. return recycler->IsMemProtectMode();
  1043. }
  1044. public:
  1045. BucketStatsReporter(Recycler* recycler)
  1046. : recycler(recycler)
  1047. {
  1048. DUMP_FRAGMENTATION_STATS_ONLY(DumpHeader());
  1049. }
  1050. bool IsEtwEnabled() const
  1051. {
  1052. return IS_GCETW_Enabled(GC_BUCKET_STATS);
  1053. }
  1054. bool IsDumpEnabled() const
  1055. {
  1056. return DUMP_FRAGMENTATION_STATS_IS(!!recycler->GetRecyclerFlagsTable().DumpFragmentationStats);
  1057. }
  1058. bool IsEnabled() const
  1059. {
  1060. return IsEtwEnabled() || IsDumpEnabled();
  1061. }
  1062. template <class TBlockType>
  1063. void PreAggregateBucketStats(TBlockType* list)
  1064. {
  1065. HeapBlockList::ForEach(list, [](TBlockType* heapBlock)
  1066. {
  1067. // Process blocks not in allocator in pre-pass. They are not put into buckets yet.
  1068. if (!heapBlock->IsInAllocator())
  1069. {
  1070. heapBlock->heapBucket->PreAggregateBucketStats(heapBlock);
  1071. }
  1072. });
  1073. }
  1074. template <class TBlockAttributes, ObjectInfoBits TBucketType>
  1075. void GetBucketStats(HeapBucketGroup<TBlockAttributes>& group)
  1076. {
  1077. auto& bucket = group.template GetBucket<TBucketType>();
  1078. bucket.AggregateBucketStats();
  1079. const auto& stats = bucket.GetMemStats();
  1080. total.Aggregate(stats);
  1081. if (stats.totalByteCount > 0)
  1082. {
  1083. const uint16 bucketNameCode = BucketNameCode<TBlockAttributes, TBucketType>();
  1084. const uint16 sizeCat = static_cast<uint16>(bucket.GetSizeCat());
  1085. GCETW(GC_BUCKET_STATS, (recycler, bucketNameCode, sizeCat, stats.objectByteCount, stats.totalByteCount));
  1086. #ifdef DUMP_FRAGMENTATION_STATS
  1087. DumpStats<TBlockAttributes, TBucketType>(sizeCat, stats);
  1088. #endif
  1089. }
  1090. }
  1091. void GetBucketStats(LargeHeapBucket& bucket)
  1092. {
  1093. bucket.AggregateBucketStats();
  1094. const auto& stats = bucket.GetMemStats();
  1095. total.Aggregate(stats);
  1096. if (stats.totalByteCount > 0)
  1097. {
  1098. const uint16 sizeCat = static_cast<uint16>(bucket.GetSizeCat());
  1099. GCETW(GC_BUCKET_STATS, (recycler, LargeBucketNameCode, sizeCat, stats.objectByteCount, stats.totalByteCount));
  1100. DUMP_FRAGMENTATION_STATS_ONLY(DumpLarge(stats));
  1101. }
  1102. }
  1103. void Report()
  1104. {
  1105. GCETW(GC_BUCKET_STATS, (recycler, TotalBucketNameCode, 0, total.objectByteCount, total.totalByteCount));
  1106. DUMP_FRAGMENTATION_STATS_ONLY(DumpFooter());
  1107. }
  1108. #ifdef DUMP_FRAGMENTATION_STATS
  1109. void DumpHeader()
  1110. {
  1111. if (IsDumpEnabled())
  1112. {
  1113. Output::Print(_u("[FRAG %d] Post-Collection State\n"), ::GetTickCount());
  1114. Output::Print(_u("---------------------------------------------------------------------------------------\n"));
  1115. Output::Print(_u(" #Blk #Objs #Fin ObjBytes FreeBytes TotalBytes UsedPercent\n"));
  1116. Output::Print(_u("---------------------------------------------------------------------------------------\n"));
  1117. }
  1118. }
  1119. template <class TBlockAttributes, ObjectInfoBits TBucketType>
  1120. void DumpStats(uint sizeCat, const HeapBucketStats& stats)
  1121. {
  1122. if (IsDumpEnabled())
  1123. {
  1124. Output::Print(_u("%-7s%s %4d : "),
  1125. DumpBucketTypeName<TBucketType>::name, DumpBlockTypeName<TBlockAttributes>::name, sizeCat);
  1126. stats.Dump();
  1127. }
  1128. }
  1129. void DumpLarge(const HeapBucketStats& stats)
  1130. {
  1131. if (IsDumpEnabled())
  1132. {
  1133. Output::Print(_u("Large : "));
  1134. stats.Dump();
  1135. }
  1136. }
  1137. void DumpFooter()
  1138. {
  1139. if (IsDumpEnabled())
  1140. {
  1141. Output::Print(_u("---------------------------------------------------------------------------------------\n"));
  1142. Output::Print(_u("Total : "));
  1143. total.Dump();
  1144. }
  1145. }
  1146. #endif
  1147. };
  1148. void
  1149. HeapInfo::ReportMemStats()
  1150. {
  1151. BucketStatsReporter report(recycler);
  1152. if (!report.IsEnabled())
  1153. {
  1154. return;
  1155. }
  1156. #if ENABLE_CONCURRENT_GC
  1157. // Pre aggregate pass on all the heap blocks that are not merged into bucket's lists yet
  1158. report.PreAggregateBucketStats(this->newNormalHeapBlockList);
  1159. report.PreAggregateBucketStats(this->newLeafHeapBlockList);
  1160. report.PreAggregateBucketStats(this->newFinalizableHeapBlockList);
  1161. #ifdef RECYCLER_WRITE_BARRIER
  1162. report.PreAggregateBucketStats(this->newNormalWithBarrierHeapBlockList);
  1163. report.PreAggregateBucketStats(this->newFinalizableWithBarrierHeapBlockList);
  1164. #endif
  1165. #ifdef RECYCLER_VISITED_HOST
  1166. report.PreAggregateBucketStats(this->newRecyclerVisitedHostHeapBlockList);
  1167. #endif
  1168. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1169. report.PreAggregateBucketStats(this->newMediumNormalHeapBlockList);
  1170. report.PreAggregateBucketStats(this->newMediumLeafHeapBlockList);
  1171. report.PreAggregateBucketStats(this->newMediumFinalizableHeapBlockList);
  1172. #ifdef RECYCLER_WRITE_BARRIER
  1173. report.PreAggregateBucketStats(this->newMediumNormalWithBarrierHeapBlockList);
  1174. report.PreAggregateBucketStats(this->newMediumFinalizableWithBarrierHeapBlockList);
  1175. #endif
  1176. #ifdef RECYCLER_VISITED_HOST
  1177. report.PreAggregateBucketStats(this->newMediumRecyclerVisitedHostHeapBlockList);
  1178. #endif
  1179. #endif
  1180. #endif // ENABLE_CONCURRENT_GC
  1181. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1182. {
  1183. report.GetBucketStats<SmallAllocationBlockAttributes, NoBit>(heapBuckets[i]);
  1184. report.GetBucketStats<SmallAllocationBlockAttributes, LeafBit>(heapBuckets[i]);
  1185. report.GetBucketStats<SmallAllocationBlockAttributes, FinalizeBit>(heapBuckets[i]);
  1186. #ifdef RECYCLER_WRITE_BARRIER
  1187. report.GetBucketStats<SmallAllocationBlockAttributes, WithBarrierBit>(heapBuckets[i]);
  1188. report.GetBucketStats<SmallAllocationBlockAttributes, FinalizableWithBarrierBit>(heapBuckets[i]);
  1189. #endif
  1190. #ifdef RECYCLER_VISITED_HOST
  1191. report.GetBucketStats<SmallAllocationBlockAttributes, RecyclerVisitedHostBit>(heapBuckets[i]);
  1192. #endif
  1193. }
  1194. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1195. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1196. {
  1197. report.GetBucketStats<MediumAllocationBlockAttributes, NoBit>(mediumHeapBuckets[i]);
  1198. report.GetBucketStats<MediumAllocationBlockAttributes, LeafBit>(mediumHeapBuckets[i]);
  1199. report.GetBucketStats<MediumAllocationBlockAttributes, FinalizeBit>(mediumHeapBuckets[i]);
  1200. #ifdef RECYCLER_WRITE_BARRIER
  1201. report.GetBucketStats<MediumAllocationBlockAttributes, WithBarrierBit>(mediumHeapBuckets[i]);
  1202. report.GetBucketStats<MediumAllocationBlockAttributes, FinalizableWithBarrierBit>(mediumHeapBuckets[i]);
  1203. #endif
  1204. #ifdef RECYCLER_VISITED_HOST
  1205. report.GetBucketStats<MediumAllocationBlockAttributes, RecyclerVisitedHostBit>(mediumHeapBuckets[i]);
  1206. #endif
  1207. }
  1208. #endif
  1209. report.GetBucketStats(largeObjectBucket);
  1210. report.Report();
  1211. }
  1212. #endif // ENABLE_MEM_STATS
  1213. #if ENABLE_PARTIAL_GC
  1214. void
  1215. HeapInfo::SweepPartialReusePages(RecyclerSweep& recyclerSweep)
  1216. {
  1217. RECYCLER_PROFILE_EXEC_THREAD_BEGIN(recyclerSweep.IsBackground(), recyclerSweep.GetRecycler(), Js::SweepPartialReusePhase);
  1218. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1219. {
  1220. heapBuckets[i].SweepPartialReusePages(recyclerSweep);
  1221. }
  1222. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1223. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1224. {
  1225. mediumHeapBuckets[i].SweepPartialReusePages(recyclerSweep);
  1226. }
  1227. #endif
  1228. RECYCLER_PROFILE_EXEC_THREAD_END(recyclerSweep.IsBackground(), recyclerSweep.GetRecycler(), Js::SweepPartialReusePhase);
  1229. // GC-TODO: LargeHeapBlock don't reuse object, so we don't need to keep
  1230. // pages with low free space from being reused.
  1231. // Only count the byte that we would have freed but we are not reusing it if we are doing a partial GC
  1232. // This will increase the GC pressure and make partial less and less likely.
  1233. if (recyclerSweep.InPartialCollect())
  1234. {
  1235. this->unusedPartialCollectFreeBytes += recyclerSweep.GetPartialUnusedFreeByteCount();
  1236. }
  1237. }
  1238. void HeapInfo::FinishPartialCollect(RecyclerSweep * recyclerSweep)
  1239. {
  1240. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1241. {
  1242. heapBuckets[i].FinishPartialCollect(recyclerSweep);
  1243. }
  1244. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1245. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1246. {
  1247. mediumHeapBuckets[i].FinishPartialCollect(recyclerSweep);
  1248. }
  1249. #endif
  1250. largeObjectBucket.FinishPartialCollect(recyclerSweep);
  1251. }
  1252. #endif
  1253. #if ENABLE_CONCURRENT_GC
  1254. void
  1255. HeapInfo::PrepareSweep()
  1256. {
  1257. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1258. {
  1259. heapBuckets[i].PrepareSweep();
  1260. }
  1261. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1262. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1263. {
  1264. mediumHeapBuckets[i].PrepareSweep();
  1265. }
  1266. #endif
  1267. }
  1268. #endif
  1269. #if ENABLE_CONCURRENT_GC
  1270. void
  1271. HeapInfo::SweepPendingObjects(RecyclerSweep& recyclerSweep)
  1272. {
  1273. if (recyclerSweep.HasPendingSweepSmallHeapBlocks())
  1274. {
  1275. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1276. {
  1277. heapBuckets[i].SweepPendingObjects(recyclerSweep);
  1278. }
  1279. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1280. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1281. {
  1282. mediumHeapBuckets[i].SweepPendingObjects(recyclerSweep);
  1283. }
  1284. #endif
  1285. }
  1286. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1287. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1288. {
  1289. mediumHeapBuckets[i].SweepPendingObjects(recyclerSweep);
  1290. }
  1291. #endif
  1292. largeObjectBucket.SweepPendingObjects(recyclerSweep);
  1293. }
  1294. #endif
  1295. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  1296. void HeapInfo::StartAllocationsDuringConcurrentSweep()
  1297. {
  1298. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1299. {
  1300. heapBuckets[i].StartAllocationDuringConcurrentSweep();
  1301. }
  1302. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1303. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1304. {
  1305. mediumHeapBuckets[i].StartAllocationDuringConcurrentSweep();
  1306. }
  1307. #endif
  1308. }
  1309. bool
  1310. HeapInfo::DoTwoPassConcurrentSweepPreCheck()
  1311. {
  1312. bool enableTwoPassSweep = false;
  1313. // We will continue to do the check for all the buckets so we can enable/disable the feature
  1314. // per bucket.
  1315. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1316. {
  1317. if (heapBuckets[i].DoTwoPassConcurrentSweepPreCheck())
  1318. {
  1319. enableTwoPassSweep = true;
  1320. }
  1321. }
  1322. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1323. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1324. {
  1325. if (mediumHeapBuckets[i].DoTwoPassConcurrentSweepPreCheck())
  1326. {
  1327. enableTwoPassSweep = true;
  1328. }
  1329. }
  1330. #endif
  1331. return enableTwoPassSweep;
  1332. }
  1333. void
  1334. HeapInfo::FinishConcurrentSweepPass1(RecyclerSweep& recyclerSweep)
  1335. {
  1336. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1337. {
  1338. heapBuckets[i].FinishConcurrentSweepPass1(recyclerSweep);
  1339. }
  1340. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1341. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1342. {
  1343. mediumHeapBuckets[i].FinishConcurrentSweepPass1(recyclerSweep);
  1344. }
  1345. #endif
  1346. }
  1347. void
  1348. HeapInfo::FinishSweepPrep(RecyclerSweep& recyclerSweep)
  1349. {
  1350. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1351. {
  1352. heapBuckets[i].FinishSweepPrep(recyclerSweep);
  1353. }
  1354. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1355. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1356. {
  1357. mediumHeapBuckets[i].FinishSweepPrep(recyclerSweep);
  1358. }
  1359. #endif
  1360. }
  1361. void
  1362. HeapInfo::FinishConcurrentSweep()
  1363. {
  1364. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1365. {
  1366. heapBuckets[i].FinishConcurrentSweep();
  1367. }
  1368. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1369. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1370. {
  1371. mediumHeapBuckets[i].FinishConcurrentSweep();
  1372. }
  1373. #endif
  1374. }
  1375. #endif
  1376. #if ENABLE_CONCURRENT_GC
  1377. void
  1378. HeapInfo::TransferPendingHeapBlocks(RecyclerSweep& recyclerSweep)
  1379. {
  1380. Assert(!recyclerSweep.IsBackground());
  1381. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  1382. if (recyclerSweep.HasPendingEmptyBlocks())
  1383. {
  1384. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1385. {
  1386. heapBuckets[i].TransferPendingEmptyHeapBlocks(recyclerSweep);
  1387. }
  1388. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1389. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1390. {
  1391. mediumHeapBuckets[i].TransferPendingEmptyHeapBlocks(recyclerSweep);
  1392. }
  1393. #endif
  1394. RECYCLER_SLOW_CHECK(VerifySmallHeapBlockCount());
  1395. }
  1396. // We might still have block that has been disposed but not made allocable
  1397. // which happens if we finish disposing object during concurrent sweep
  1398. // and can't modify the block lists
  1399. recyclerSweep.FlushPendingTransferDisposedObjects();
  1400. }
  1401. void
  1402. HeapInfo::ConcurrentTransferSweptObjects(RecyclerSweep& recyclerSweep)
  1403. {
  1404. #if ENABLE_PARTIAL_GC
  1405. Assert(!recyclerSweep.InPartialCollectMode());
  1406. #endif
  1407. Assert(!recyclerSweep.IsBackground());
  1408. TransferPendingHeapBlocks(recyclerSweep);
  1409. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1410. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1411. {
  1412. mediumHeapBuckets[i].ConcurrentTransferSweptObjects(recyclerSweep);
  1413. }
  1414. #endif
  1415. largeObjectBucket.ConcurrentTransferSweptObjects(recyclerSweep);
  1416. }
  1417. #if ENABLE_PARTIAL_GC
  1418. void
  1419. HeapInfo::ConcurrentPartialTransferSweptObjects(RecyclerSweep& recyclerSweep)
  1420. {
  1421. Assert(recyclerSweep.InPartialCollectMode());
  1422. Assert(!recyclerSweep.IsBackground());
  1423. TransferPendingHeapBlocks(recyclerSweep);
  1424. RECYCLER_SLOW_CHECK(this->VerifyLargeHeapBlockCount());
  1425. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1426. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1427. {
  1428. mediumHeapBuckets[i].ConcurrentPartialTransferSweptObjects(recyclerSweep);
  1429. }
  1430. #endif
  1431. largeObjectBucket.ConcurrentPartialTransferSweptObjects(recyclerSweep);
  1432. RECYCLER_SLOW_CHECK(this->VerifyLargeHeapBlockCount());
  1433. }
  1434. #endif
  1435. #endif
  1436. void
  1437. HeapInfo::DisposeObjects()
  1438. {
  1439. Recycler * recycler = this->recycler;
  1440. do
  1441. {
  1442. recycler->hasDisposableObject = false;
  1443. // finalizing the objects
  1444. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1445. {
  1446. heapBuckets[i].DisposeObjects();
  1447. }
  1448. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1449. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1450. {
  1451. mediumHeapBuckets[i].DisposeObjects();
  1452. }
  1453. #endif
  1454. largeObjectBucket.DisposeObjects();
  1455. }
  1456. // Calling dispose may enter the GC again and dispose more objects, loop until we don't have any more
  1457. while (recycler->hasDisposableObject);
  1458. recycler->hasPendingTransferDisposedObjects = true;
  1459. #if ENABLE_CONCURRENT_GC
  1460. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  1461. // As during concurrent sweep we start/stop allocations it is safer to prevent transferring disposed objects altogether.
  1462. if (!recycler->IsConcurrentExecutingState() && !recycler->IsConcurrentSweepState())
  1463. #else
  1464. if (!recycler->IsConcurrentExecutingState())
  1465. #endif
  1466. #endif
  1467. {
  1468. // Can't transfer disposed object when the background thread is walking the heap block list
  1469. // That includes reset mark, background rescan and concurrent sweep. Delay the transfer later.
  1470. // NOTE1: During concurrent sweep, we can't do this only if the bucket has "stopped" allocation
  1471. // After it resume allocation, we don't walk the list in the background thread any more
  1472. // (except for checking heap block count). But this is easier to detect via the collection state
  1473. // without walking all buckets.
  1474. // NOTE2: During transitive closure mark, we don't walk the heap block list, but we can continue
  1475. // to do background rescan. Since we don't have synchronization for that, we can't really enable
  1476. // able this just for the transitive closure, so just do all the background executing state.
  1477. TransferDisposedObjects();
  1478. }
  1479. }
  1480. void
  1481. HeapInfo::TransferDisposedObjects()
  1482. {
  1483. Recycler * recycler = this->recycler;
  1484. Assert(recycler->hasPendingTransferDisposedObjects);
  1485. #if ENABLE_CONCURRENT_GC
  1486. #if ENABLE_ALLOCATIONS_DURING_CONCURRENT_SWEEP
  1487. Assert(!recycler->IsConcurrentExecutingState() && !recycler->IsConcurrentSweepState());
  1488. #else
  1489. Assert(!recycler->IsConcurrentExecutingState());
  1490. #endif
  1491. #endif
  1492. recycler->hasPendingTransferDisposedObjects = false;
  1493. // move the disposed object back to the free lists
  1494. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1495. {
  1496. heapBuckets[i].TransferDisposedObjects();
  1497. }
  1498. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1499. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1500. {
  1501. mediumHeapBuckets[i].TransferDisposedObjects();
  1502. }
  1503. #endif
  1504. largeObjectBucket.TransferDisposedObjects();
  1505. }
  1506. void
  1507. HeapInfo::EnumerateObjects(ObjectInfoBits infoBits, void (*CallBackFunction)(void * address, size_t size))
  1508. {
  1509. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1510. {
  1511. heapBuckets[i].EnumerateObjects(infoBits, CallBackFunction);
  1512. }
  1513. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1514. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1515. {
  1516. mediumHeapBuckets[i].EnumerateObjects(infoBits, CallBackFunction);
  1517. }
  1518. #endif
  1519. largeObjectBucket.EnumerateObjects(infoBits, CallBackFunction);
  1520. #if ENABLE_CONCURRENT_GC
  1521. HeapBucket::EnumerateObjects(newLeafHeapBlockList, infoBits, CallBackFunction);
  1522. HeapBucket::EnumerateObjects(newNormalHeapBlockList, infoBits, CallBackFunction);
  1523. #ifdef RECYCLER_WRITE_BARRIER
  1524. HeapBucket::EnumerateObjects(newNormalWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1525. HeapBucket::EnumerateObjects(newFinalizableWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1526. #endif
  1527. #ifdef RECYCLER_VISITED_HOST
  1528. HeapBucket::EnumerateObjects(newRecyclerVisitedHostHeapBlockList, infoBits, CallBackFunction);
  1529. #endif
  1530. HeapBucket::EnumerateObjects(newFinalizableHeapBlockList, infoBits, CallBackFunction);
  1531. HeapBucket::EnumerateObjects(newMediumLeafHeapBlockList, infoBits, CallBackFunction);
  1532. HeapBucket::EnumerateObjects(newMediumNormalHeapBlockList, infoBits, CallBackFunction);
  1533. #ifdef RECYCLER_WRITE_BARRIER
  1534. HeapBucket::EnumerateObjects(newMediumNormalWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1535. HeapBucket::EnumerateObjects(newMediumFinalizableWithBarrierHeapBlockList, infoBits, CallBackFunction);
  1536. #endif
  1537. #ifdef RECYCLER_VISITED_HOST
  1538. HeapBucket::EnumerateObjects(newMediumRecyclerVisitedHostHeapBlockList, infoBits, CallBackFunction);
  1539. #endif
  1540. HeapBucket::EnumerateObjects(newMediumFinalizableHeapBlockList, infoBits, CallBackFunction);
  1541. #endif
  1542. }
  1543. #if DBG || defined(RECYCLER_SLOW_CHECK_ENABLED)
  1544. size_t
  1545. HeapInfo::GetSmallHeapBlockCount(bool checkCount) const
  1546. {
  1547. size_t currentSmallHeapBlockCount = 0;
  1548. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1549. {
  1550. currentSmallHeapBlockCount += heapBuckets[i].GetNonEmptyHeapBlockCount(checkCount);
  1551. currentSmallHeapBlockCount += heapBuckets[i].GetEmptyHeapBlockCount();
  1552. }
  1553. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1554. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1555. {
  1556. currentSmallHeapBlockCount += mediumHeapBuckets[i].GetNonEmptyHeapBlockCount(checkCount);
  1557. currentSmallHeapBlockCount += mediumHeapBuckets[i].GetEmptyHeapBlockCount();
  1558. }
  1559. #endif
  1560. #if ENABLE_CONCURRENT_GC
  1561. currentSmallHeapBlockCount += HeapBlockList::Count(this->newLeafHeapBlockList);
  1562. currentSmallHeapBlockCount += HeapBlockList::Count(this->newNormalHeapBlockList);
  1563. currentSmallHeapBlockCount += HeapBlockList::Count(this->newFinalizableHeapBlockList);
  1564. #ifdef RECYCLER_VISITED_HOST
  1565. currentSmallHeapBlockCount += HeapBlockList::Count(this->newRecyclerVisitedHostHeapBlockList);
  1566. #endif
  1567. #ifdef RECYCLER_WRITE_BARRIER
  1568. currentSmallHeapBlockCount += HeapBlockList::Count(this->newNormalWithBarrierHeapBlockList);
  1569. currentSmallHeapBlockCount += HeapBlockList::Count(this->newFinalizableWithBarrierHeapBlockList);
  1570. #endif
  1571. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumLeafHeapBlockList);
  1572. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumNormalHeapBlockList);
  1573. #ifdef RECYCLER_VISITED_HOST
  1574. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumRecyclerVisitedHostHeapBlockList);
  1575. #endif
  1576. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumFinalizableHeapBlockList);
  1577. #ifdef RECYCLER_WRITE_BARRIER
  1578. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumNormalWithBarrierHeapBlockList);
  1579. currentSmallHeapBlockCount += HeapBlockList::Count(this->newMediumFinalizableWithBarrierHeapBlockList);
  1580. #endif
  1581. // TODO: Update recycler sweep
  1582. // Recycler can be null if we have OOM in the ctor
  1583. if (this->recycler && this->recycler->recyclerSweep != nullptr)
  1584. {
  1585. // This function can't be called in the background
  1586. Assert(!this->recycler->recyclerSweep->IsBackground());
  1587. currentSmallHeapBlockCount += this->recycler->recyclerSweep->SetPendingMergeNewHeapBlockCount();
  1588. }
  1589. #endif
  1590. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  1591. size_t expectedHeapBlockCount =
  1592. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockType]
  1593. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallLeafBlockType]
  1594. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockType]
  1595. #ifdef RECYCLER_VISITED_HOST
  1596. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallRecyclerVisitedHostBlockType]
  1597. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumRecyclerVisitedHostBlockType]
  1598. #endif
  1599. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockType]
  1600. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumLeafBlockType]
  1601. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockType];
  1602. #ifdef RECYCLER_WRITE_BARRIER
  1603. expectedHeapBlockCount +=
  1604. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockWithBarrierType]
  1605. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockWithBarrierType]
  1606. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockWithBarrierType]
  1607. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockWithBarrierType];
  1608. #endif
  1609. Assert(!checkCount || currentSmallHeapBlockCount == expectedHeapBlockCount);
  1610. #endif
  1611. return currentSmallHeapBlockCount;
  1612. }
  1613. size_t
  1614. HeapInfo::GetLargeHeapBlockCount(bool checkCount) const
  1615. {
  1616. size_t currentLargeHeapBlockCount = 0;
  1617. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && !SMALLBLOCK_MEDIUM_ALLOC
  1618. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1619. {
  1620. currentLargeHeapBlockCount += mediumHeapBuckets[i].GetLargeHeapBlockCount(checkCount);
  1621. }
  1622. #endif
  1623. currentLargeHeapBlockCount += largeObjectBucket.GetLargeHeapBlockCount(checkCount);
  1624. RECYCLER_SLOW_CHECK(Assert(!checkCount || currentLargeHeapBlockCount == this->heapBlockCount[HeapBlock::HeapBlockType::LargeBlockType]));
  1625. return currentLargeHeapBlockCount;
  1626. }
  1627. #endif
  1628. #ifdef RECYCLER_SLOW_CHECK_ENABLED
  1629. void
  1630. HeapInfo::Check()
  1631. {
  1632. Assert(!this->recycler->CollectionInProgress());
  1633. size_t currentSmallHeapBlockCount = 0;
  1634. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1635. {
  1636. currentSmallHeapBlockCount += heapBuckets[i].Check();
  1637. currentSmallHeapBlockCount += heapBuckets[i].GetEmptyHeapBlockCount();
  1638. }
  1639. size_t currentLargeHeapBlockCount = 0;
  1640. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1641. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1642. {
  1643. #if SMALLBLOCK_MEDIUM_ALLOC
  1644. currentSmallHeapBlockCount += mediumHeapBuckets[i].Check();
  1645. currentSmallHeapBlockCount += mediumHeapBuckets[i].GetEmptyHeapBlockCount();
  1646. #else
  1647. currentLargeHeapBlockCount += mediumHeapBuckets[i].Check();
  1648. #endif
  1649. }
  1650. #endif
  1651. #if ENABLE_CONCURRENT_GC
  1652. currentSmallHeapBlockCount += Check(true, false, this->newLeafHeapBlockList);
  1653. currentSmallHeapBlockCount += Check(true, false, this->newNormalHeapBlockList);
  1654. #ifdef RECYCLER_WRITE_BARRIER
  1655. currentSmallHeapBlockCount += Check(true, false, this->newNormalWithBarrierHeapBlockList);
  1656. currentSmallHeapBlockCount += Check(true, false, this->newFinalizableWithBarrierHeapBlockList);
  1657. #endif
  1658. currentSmallHeapBlockCount += Check(true, false, this->newFinalizableHeapBlockList);
  1659. #ifdef RECYCLER_VISITED_HOST
  1660. currentSmallHeapBlockCount += Check(true, false, this->newRecyclerVisitedHostHeapBlockList);
  1661. currentSmallHeapBlockCount += Check(true, false, this->newMediumRecyclerVisitedHostHeapBlockList);
  1662. #endif
  1663. #endif
  1664. #if ENABLE_CONCURRENT_GC
  1665. currentSmallHeapBlockCount += Check(true, false, this->newMediumLeafHeapBlockList);
  1666. currentSmallHeapBlockCount += Check(true, false, this->newMediumNormalHeapBlockList);
  1667. #ifdef RECYCLER_WRITE_BARRIER
  1668. currentSmallHeapBlockCount += Check(true, false, this->newMediumNormalWithBarrierHeapBlockList);
  1669. currentSmallHeapBlockCount += Check(true, false, this->newMediumFinalizableWithBarrierHeapBlockList);
  1670. #endif
  1671. currentSmallHeapBlockCount += Check(true, false, this->newMediumFinalizableHeapBlockList);
  1672. #endif
  1673. size_t expectedHeapBlockCount =
  1674. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockType]
  1675. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallLeafBlockType]
  1676. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockType]
  1677. #ifdef RECYCLER_VISITED_HOST
  1678. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallRecyclerVisitedHostBlockType]
  1679. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumRecyclerVisitedHostBlockType]
  1680. #endif
  1681. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockType]
  1682. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumLeafBlockType]
  1683. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockType];
  1684. #ifdef RECYCLER_WRITE_BARRIER
  1685. expectedHeapBlockCount +=
  1686. this->heapBlockCount[HeapBlock::HeapBlockType::SmallNormalBlockWithBarrierType]
  1687. + this->heapBlockCount[HeapBlock::HeapBlockType::SmallFinalizableBlockWithBarrierType]
  1688. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumNormalBlockWithBarrierType]
  1689. + this->heapBlockCount[HeapBlock::HeapBlockType::MediumFinalizableBlockWithBarrierType];
  1690. #endif
  1691. Assert(currentSmallHeapBlockCount == expectedHeapBlockCount);
  1692. currentLargeHeapBlockCount += largeObjectBucket.Check();
  1693. Assert(currentLargeHeapBlockCount == this->heapBlockCount[HeapBlock::HeapBlockType::LargeBlockType]);
  1694. }
  1695. template <typename TBlockType>
  1696. size_t
  1697. HeapInfo::Check(bool expectFull, bool expectPending, TBlockType * list, TBlockType * tail)
  1698. {
  1699. size_t heapBlockCount = 0;
  1700. HeapBlockList::ForEach(list, tail, [&heapBlockCount, expectFull, expectPending](TBlockType * heapBlock)
  1701. {
  1702. heapBlock->Check(expectFull, expectPending);
  1703. heapBlockCount++;
  1704. });
  1705. return heapBlockCount;
  1706. }
  1707. template size_t HeapInfo::Check<SmallNormalHeapBlock>(bool expectFull, bool expectPending, SmallNormalHeapBlock * list, SmallNormalHeapBlock * tail);
  1708. template size_t HeapInfo::Check<SmallLeafHeapBlock>(bool expectFull, bool expectPending, SmallLeafHeapBlock * list, SmallLeafHeapBlock * tail);
  1709. template size_t HeapInfo::Check<SmallFinalizableHeapBlock>(bool expectFull, bool expectPending, SmallFinalizableHeapBlock * list, SmallFinalizableHeapBlock * tail);
  1710. #ifdef RECYCLER_VISITED_HOST
  1711. template size_t HeapInfo::Check<SmallRecyclerVisitedHostHeapBlock>(bool expectFull, bool expectPending, SmallRecyclerVisitedHostHeapBlock * list, SmallRecyclerVisitedHostHeapBlock * tail);
  1712. template size_t HeapInfo::Check<MediumRecyclerVisitedHostHeapBlock>(bool expectFull, bool expectPending, MediumRecyclerVisitedHostHeapBlock * list, MediumRecyclerVisitedHostHeapBlock * tail);
  1713. #endif
  1714. template size_t HeapInfo::Check<LargeHeapBlock>(bool expectFull, bool expectPending, LargeHeapBlock * list, LargeHeapBlock * tail);
  1715. #ifdef RECYCLER_WRITE_BARRIER
  1716. template size_t HeapInfo::Check<SmallNormalWithBarrierHeapBlock>(bool expectFull, bool expectPending, SmallNormalWithBarrierHeapBlock * list, SmallNormalWithBarrierHeapBlock * tail);
  1717. template size_t HeapInfo::Check<SmallFinalizableWithBarrierHeapBlock>(bool expectFull, bool expectPending, SmallFinalizableWithBarrierHeapBlock * list, SmallFinalizableWithBarrierHeapBlock * tail);
  1718. #endif
  1719. template size_t HeapInfo::Check<MediumNormalHeapBlock>(bool expectFull, bool expectPending, MediumNormalHeapBlock * list, MediumNormalHeapBlock * tail);
  1720. template size_t HeapInfo::Check<MediumLeafHeapBlock>(bool expectFull, bool expectPending, MediumLeafHeapBlock * list, MediumLeafHeapBlock * tail);
  1721. template size_t HeapInfo::Check<MediumFinalizableHeapBlock>(bool expectFull, bool expectPending, MediumFinalizableHeapBlock * list, MediumFinalizableHeapBlock * tail);
  1722. template size_t HeapInfo::Check<LargeHeapBlock>(bool expectFull, bool expectPending, LargeHeapBlock * list, LargeHeapBlock * tail);
  1723. #ifdef RECYCLER_WRITE_BARRIER
  1724. template size_t HeapInfo::Check<MediumNormalWithBarrierHeapBlock>(bool expectFull, bool expectPending, MediumNormalWithBarrierHeapBlock * list, MediumNormalWithBarrierHeapBlock * tail);
  1725. template size_t HeapInfo::Check<MediumFinalizableWithBarrierHeapBlock>(bool expectFull, bool expectPending, MediumFinalizableWithBarrierHeapBlock * list, MediumFinalizableWithBarrierHeapBlock * tail);
  1726. #endif
  1727. void
  1728. HeapInfo::VerifySmallHeapBlockCount()
  1729. {
  1730. GetSmallHeapBlockCount(true);
  1731. }
  1732. void
  1733. HeapInfo::VerifyLargeHeapBlockCount()
  1734. {
  1735. GetLargeHeapBlockCount(true);
  1736. }
  1737. #endif
  1738. #ifdef RECYCLER_MEMORY_VERIFY
  1739. void
  1740. HeapInfo::Verify()
  1741. {
  1742. Assert(!this->recycler->CollectionInProgress());
  1743. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1744. {
  1745. heapBuckets[i].Verify();
  1746. }
  1747. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1748. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1749. {
  1750. mediumHeapBuckets[i].Verify();
  1751. }
  1752. #endif
  1753. largeObjectBucket.Verify();
  1754. #if ENABLE_CONCURRENT_GC
  1755. HeapBlockList::ForEach(newLeafHeapBlockList, [](SmallLeafHeapBlock * heapBlock)
  1756. {
  1757. heapBlock->Verify();
  1758. });
  1759. HeapBlockList::ForEach(newNormalHeapBlockList, [](SmallNormalHeapBlock * heapBlock)
  1760. {
  1761. heapBlock->Verify();
  1762. });
  1763. #ifdef RECYCLER_WRITE_BARRIER
  1764. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [](SmallNormalWithBarrierHeapBlock * heapBlock)
  1765. {
  1766. heapBlock->Verify();
  1767. });
  1768. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  1769. {
  1770. heapBlock->Verify();
  1771. });
  1772. #endif
  1773. #ifdef RECYCLER_VISITED_HOST
  1774. HeapBlockList::ForEach(newRecyclerVisitedHostHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1775. {
  1776. heapBlock->Verify();
  1777. });
  1778. #endif
  1779. HeapBlockList::ForEach(newFinalizableHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1780. {
  1781. heapBlock->Verify();
  1782. });
  1783. #endif
  1784. #if ENABLE_CONCURRENT_GC
  1785. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [](MediumLeafHeapBlock * heapBlock)
  1786. {
  1787. heapBlock->Verify();
  1788. });
  1789. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [](MediumNormalHeapBlock * heapBlock)
  1790. {
  1791. heapBlock->Verify();
  1792. });
  1793. #ifdef RECYCLER_WRITE_BARRIER
  1794. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [](MediumNormalWithBarrierHeapBlock * heapBlock)
  1795. {
  1796. heapBlock->Verify();
  1797. });
  1798. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  1799. {
  1800. heapBlock->Verify();
  1801. });
  1802. #endif
  1803. #ifdef RECYCLER_VISITED_HOST
  1804. HeapBlockList::ForEach(newMediumRecyclerVisitedHostHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1805. {
  1806. heapBlock->Verify();
  1807. });
  1808. #endif
  1809. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1810. {
  1811. heapBlock->Verify();
  1812. });
  1813. #endif
  1814. }
  1815. #endif
  1816. #ifdef RECYCLER_VERIFY_MARK
  1817. void
  1818. HeapInfo::VerifyMark()
  1819. {
  1820. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1821. {
  1822. heapBuckets[i].VerifyMark();
  1823. }
  1824. #ifdef BUCKETIZE_MEDIUM_ALLOCATIONS
  1825. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1826. {
  1827. mediumHeapBuckets[i].VerifyMark();
  1828. }
  1829. #endif
  1830. largeObjectBucket.VerifyMark();
  1831. #if ENABLE_CONCURRENT_GC
  1832. HeapBlockList::ForEach(newLeafHeapBlockList, [](SmallLeafHeapBlock * heapBlock)
  1833. {
  1834. heapBlock->VerifyMark();
  1835. });
  1836. HeapBlockList::ForEach(newNormalHeapBlockList, [](SmallNormalHeapBlock * heapBlock)
  1837. {
  1838. heapBlock->VerifyMark();
  1839. });
  1840. #ifdef RECYCLER_WRITE_BARRIER
  1841. HeapBlockList::ForEach(newNormalWithBarrierHeapBlockList, [](SmallNormalWithBarrierHeapBlock * heapBlock)
  1842. {
  1843. heapBlock->VerifyMark();
  1844. });
  1845. HeapBlockList::ForEach(newFinalizableWithBarrierHeapBlockList, [](SmallFinalizableWithBarrierHeapBlock * heapBlock)
  1846. {
  1847. heapBlock->VerifyMark();
  1848. });
  1849. #endif
  1850. #ifdef RECYCLER_VISITED_HOST
  1851. HeapBlockList::ForEach(newRecyclerVisitedHostHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1852. {
  1853. heapBlock->VerifyMark();
  1854. });
  1855. #endif
  1856. HeapBlockList::ForEach(newFinalizableHeapBlockList, [](SmallFinalizableHeapBlock * heapBlock)
  1857. {
  1858. heapBlock->VerifyMark();
  1859. });
  1860. #endif
  1861. #if ENABLE_CONCURRENT_GC
  1862. HeapBlockList::ForEach(newMediumLeafHeapBlockList, [](MediumLeafHeapBlock * heapBlock)
  1863. {
  1864. heapBlock->VerifyMark();
  1865. });
  1866. HeapBlockList::ForEach(newMediumNormalHeapBlockList, [](MediumNormalHeapBlock * heapBlock)
  1867. {
  1868. heapBlock->VerifyMark();
  1869. });
  1870. #ifdef RECYCLER_WRITE_BARRIER
  1871. HeapBlockList::ForEach(newMediumNormalWithBarrierHeapBlockList, [](MediumNormalWithBarrierHeapBlock * heapBlock)
  1872. {
  1873. heapBlock->VerifyMark();
  1874. });
  1875. HeapBlockList::ForEach(newMediumFinalizableWithBarrierHeapBlockList, [](MediumFinalizableWithBarrierHeapBlock * heapBlock)
  1876. {
  1877. heapBlock->VerifyMark();
  1878. });
  1879. #endif
  1880. #ifdef RECYCLER_VISITED_HOST
  1881. HeapBlockList::ForEach(newMediumRecyclerVisitedHostHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1882. {
  1883. heapBlock->VerifyMark();
  1884. });
  1885. #endif
  1886. HeapBlockList::ForEach(newMediumFinalizableHeapBlockList, [](MediumFinalizableHeapBlock * heapBlock)
  1887. {
  1888. heapBlock->VerifyMark();
  1889. });
  1890. #endif
  1891. }
  1892. #endif
  1893. #ifdef RECYCLER_FINALIZE_CHECK
  1894. void
  1895. HeapInfo::VerifyFinalize()
  1896. {
  1897. // We can't check this if we are marking
  1898. Assert(!this->recycler->IsMarkState());
  1899. size_t currentFinalizableObjectCount = this->liveFinalizableObjectCount - this->newFinalizableObjectCount - this->pendingDisposableObjectCount;
  1900. #if DBG
  1901. Assert(currentFinalizableObjectCount == this->recycler->collectionStats.finalizeCount);
  1902. #else
  1903. if (currentFinalizableObjectCount != this->recycler->collectionStats.finalizeCount)
  1904. {
  1905. Output::Print(_u("ERROR: Recycler dropped some finalizable objects"));
  1906. DebugBreak();
  1907. }
  1908. #endif
  1909. }
  1910. #endif
  1911. #if DBG
  1912. bool
  1913. HeapInfo::AllocatorsAreEmpty()
  1914. {
  1915. for (uint i = 0; i < HeapConstants::BucketCount; i++)
  1916. {
  1917. if (!heapBuckets[i].AllocatorsAreEmpty())
  1918. {
  1919. return false;
  1920. }
  1921. }
  1922. #if defined(BUCKETIZE_MEDIUM_ALLOCATIONS) && SMALLBLOCK_MEDIUM_ALLOC
  1923. for (uint i = 0; i < HeapConstants::MediumBucketCount; i++)
  1924. {
  1925. if (!mediumHeapBuckets[i].AllocatorsAreEmpty())
  1926. {
  1927. return false;
  1928. }
  1929. }
  1930. #endif
  1931. return true;
  1932. }
  1933. #endif
  1934. // Block attribute functions
  1935. /* static */
  1936. BOOL SmallAllocationBlockAttributes::IsAlignedObjectSize(size_t sizeCat)
  1937. {
  1938. return HeapInfo::IsAlignedSmallObjectSize(sizeCat);
  1939. }
  1940. /* static */
  1941. BOOL MediumAllocationBlockAttributes::IsAlignedObjectSize(size_t sizeCat)
  1942. {
  1943. return HeapInfo::IsAlignedMediumObjectSize(sizeCat);
  1944. }
  1945. namespace Memory
  1946. {
  1947. template class HeapInfo::ValidPointersMap<SmallAllocationBlockAttributes>;
  1948. template class ValidPointers<SmallAllocationBlockAttributes>;
  1949. template class HeapInfo::ValidPointersMap<MediumAllocationBlockAttributes>;
  1950. template class ValidPointers<MediumAllocationBlockAttributes>;
  1951. };