GCStress.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "stdafx.h"
  6. #include "GCStress.h"
  7. // For converting from ANSI to UTF16
  8. #ifndef _WIN32
  9. #include <src/include/pal/utils.h>
  10. #endif
  11. void DoVerify(bool value, const char * expr, const char * file, int line)
  12. {
  13. if (!value)
  14. {
  15. wprintf(_u("==== FAILURE: '%S' evaluated to false. %S(%d)\n"), expr, file, line);
  16. DebugBreak();
  17. }
  18. }
  19. // Some constants for the stress test
  20. static const unsigned int stackRootCount = 50;
  21. static const unsigned int globalRootCount = 50;
  22. static const unsigned int implicitRootCount = 50;
  23. #ifdef _WIN32
  24. static const unsigned int initializeCount = 1000000;
  25. static const unsigned int operationsPerHeapWalk = 1000000;
  26. #else
  27. // xplat-todo: Increase this number to match the windows numbers
  28. // Currently, the windows numbers seem to be really slow on linux
  29. // Need to investigate what operation is so much slower on linux
  30. static const unsigned int initializeCount = 10000;
  31. static const unsigned int operationsPerHeapWalk = 100000;
  32. #endif
  33. // Some global variables
  34. // Recycler instance
  35. Recycler * recyclerInstance = nullptr;
  36. // TODO, make this configurable on the command line
  37. bool implicitRootsMode = false;
  38. //bool implicitRootsMode = true;
  39. // List of root locations. These may be on the stack or elsewhere.
  40. WeightedTable<Location> roots;
  41. // Global root locations. These are pinned (if not null).
  42. RecyclerTestObject * globalRoots[globalRootCount];
  43. // Implicit root locations. These are allocated using ImplicitRootBit.
  44. // Only enabled in MemProtect mode.
  45. RecyclerTestObject * implicitRoots[implicitRootCount];
  46. // Object creation function table. Used to randomly create new objects.
  47. typedef RecyclerTestObject * (*ObjectCreationFunc)(void);
  48. WeightedTable<ObjectCreationFunc> objectCreationTable;
  49. // Operation table. Used to randomly perform heap operations.
  50. typedef void (*Operation)(void);
  51. WeightedTable<Operation> operationTable;
  52. // Not used currently, but keep for now
  53. bool verbose = false;
  54. RecyclerTestObject * CreateNewObject()
  55. {
  56. // Get a random creation routine from the objectCreationTable
  57. ObjectCreationFunc creationFunc = objectCreationTable.GetRandomEntry();
  58. // Invoke it to create the new object
  59. return creationFunc();
  60. }
  61. Location GetRandomLocation()
  62. {
  63. Location location = roots.GetRandomEntry();
  64. while (true)
  65. {
  66. // If the current location contains nullptr, we can't walk it.
  67. // Just return this location.
  68. RecyclerTestObject * object = location.Get();
  69. if (object == nullptr)
  70. {
  71. return location;
  72. }
  73. // Once in a while, just stop walking and return the current location, even though it's not nullptr.
  74. // We don't want to do this too often, because if we update the location, we'll prune the entire tree
  75. // underneath it. So make this relatively rare.
  76. // (Note, different object mixes may require this to be tuned up/down as appropriate.)
  77. if (GetRandomInteger(10000) == 0)
  78. {
  79. return location;
  80. }
  81. // Otherwise, try to walk to a new location on the specified object
  82. if (!object->TryGetRandomLocation(&location))
  83. {
  84. // TryGetRandomLocation failed, e.g. because the object is a leaf object and has no internal locations.
  85. // Thus we can't walk any further. Return the location we have.
  86. return location;
  87. }
  88. }
  89. }
  90. void InsertObject()
  91. {
  92. // Create a new object
  93. RecyclerTestObject * object = CreateNewObject();
  94. // Walk to a random location in the current object graph
  95. Location location = GetRandomLocation();
  96. // If the location is currently null, set the object there
  97. // If it's not null, do nothing and let the new object be collected.
  98. if (location.Get() == nullptr)
  99. {
  100. location.Set(object);
  101. }
  102. }
  103. void ReplaceObject()
  104. {
  105. // Create a new object
  106. RecyclerTestObject * object = CreateNewObject();
  107. // Walk to a random location in the current object graph
  108. Location location = GetRandomLocation();
  109. // Set the new object there unconditionally
  110. location.Set(object);
  111. }
  112. void DeleteObject()
  113. {
  114. // Walk to a random location in the current object graph
  115. Location location = GetRandomLocation();
  116. // Set it to nullptr
  117. location.Set(nullptr);
  118. }
  119. void MoveObject()
  120. {
  121. // Walk to two random locations in the current object graph
  122. Location location1 = GetRandomLocation();
  123. Location location2 = GetRandomLocation();
  124. // Move the reference and delete the old reference.
  125. RecyclerTestObject * object = location1.Get();
  126. location1.Set(nullptr);
  127. location2.Set(object);
  128. }
  129. void CopyObject()
  130. {
  131. // Walk to two random locations in the current object graph
  132. Location location1 = GetRandomLocation();
  133. Location location2 = GetRandomLocation();
  134. // Copy from the first reference to second.
  135. RecyclerTestObject * object = location1.Get();
  136. location2.Set(object);
  137. }
  138. void SwapObjects()
  139. {
  140. // Walk to two random locations in the current object graph
  141. Location location1 = GetRandomLocation();
  142. Location location2 = GetRandomLocation();
  143. // Swap their references.
  144. RecyclerTestObject * object = location1.Get();
  145. location1.Set(location2.Get());
  146. location2.Set(object);
  147. }
  148. void DoHeapOperation()
  149. {
  150. // Get a random heap operation routine from the operation table
  151. Operation operationFunc = operationTable.GetRandomEntry();
  152. // Invoke it to perform the heap walk
  153. return operationFunc();
  154. }
  155. void WalkHeap()
  156. {
  157. RecyclerTestObject::BeginWalk();
  158. // The roots table always has weight 1 for every entry, so we can walk it directly without hitting duplicates.
  159. for (unsigned int i = 0; i < roots.GetSize(); i++)
  160. {
  161. RecyclerTestObject::WalkReference(roots.GetEntry(i).Get());
  162. }
  163. RecyclerTestObject::EndWalk();
  164. }
  165. void BuildObjectCreationTable()
  166. {
  167. // Populate the object creation func table
  168. // This defines the set of objects we create and their relative weights
  169. objectCreationTable.AddWeightedEntry(&LeafObject<1, 50>::New, 1000);
  170. objectCreationTable.AddWeightedEntry(&ScannedObject<1, 50>::New, 10000);
  171. objectCreationTable.AddWeightedEntry(&BarrierObject<1, 50>::New, 2000);
  172. objectCreationTable.AddWeightedEntry(&TrackedObject<1, 50>::New, 2000);
  173. #ifdef RECYCLER_VISITED_HOST
  174. objectCreationTable.AddWeightedEntry(&RecyclerVisitedObject<1, 50>::New, 2000);
  175. #endif
  176. objectCreationTable.AddWeightedEntry(&LeafObject<51, 1000>::New, 10);
  177. objectCreationTable.AddWeightedEntry(&ScannedObject<51, 1000>::New, 100);
  178. objectCreationTable.AddWeightedEntry(&BarrierObject<51, 1000>::New, 20);
  179. objectCreationTable.AddWeightedEntry(&TrackedObject<51, 1000>::New, 20);
  180. #ifdef RECYCLER_VISITED_HOST
  181. objectCreationTable.AddWeightedEntry(&RecyclerVisitedObject<51, 1000>::New, 40);
  182. #endif
  183. objectCreationTable.AddWeightedEntry(&LeafObject<1001, 50000>::New, 1);
  184. objectCreationTable.AddWeightedEntry(&ScannedObject<1001, 50000>::New, 10);
  185. objectCreationTable.AddWeightedEntry(&BarrierObject<1001, 50000>::New, 2);
  186. objectCreationTable.AddWeightedEntry(&FinalizedObject<1001, 50000>::New, 2);
  187. // objectCreationTable.AddWeightedEntry(&TrackedObject<1001, 50000>::New, 2); // Large tracked objects are not supported
  188. // objectCreationTable.AddWeightedEntry(&RecyclerVisitedObject<1001, 50000>::New, 2); // Large recycler visited objects are not supported
  189. }
  190. void BuildOperationTable()
  191. {
  192. operationTable.AddWeightedEntry(&InsertObject, 5);
  193. operationTable.AddWeightedEntry(&ReplaceObject, 2);
  194. operationTable.AddWeightedEntry(&DeleteObject, 2);
  195. operationTable.AddWeightedEntry(&MoveObject, 5);
  196. operationTable.AddWeightedEntry(&CopyObject, 5);
  197. operationTable.AddWeightedEntry(&SwapObjects, 5);
  198. }
  199. void SimpleRecyclerTest()
  200. {
  201. // Initialize the probability tables for object creation and heap operations.
  202. BuildObjectCreationTable();
  203. BuildOperationTable();
  204. // Construct Recycler instance and use it
  205. #if ENABLE_BACKGROUND_PAGE_FREEING
  206. PageAllocator::BackgroundPageQueue backgroundPageQueue;
  207. #endif
  208. IdleDecommitPageAllocator pageAllocator(nullptr,
  209. PageAllocatorType::PageAllocatorType_Thread,
  210. Js::Configuration::Global.flags,
  211. 0 /* maxFreePageCount */, PageAllocator::DefaultMaxFreePageCount /* maxIdleFreePageCount */,
  212. false /* zero pages */
  213. #if ENABLE_BACKGROUND_PAGE_FREEING
  214. , &backgroundPageQueue
  215. #endif
  216. );
  217. try
  218. {
  219. #ifdef EXCEPTION_CHECK
  220. // REVIEW: Do we need a stack probe here? We don't care about OOM's since we deal with that below
  221. AUTO_NESTED_HANDLED_EXCEPTION_TYPE(ExceptionType_DisableCheck);
  222. #endif
  223. recyclerInstance = HeapNewZ(Recycler, nullptr, &pageAllocator, Js::Throw::OutOfMemory, Js::Configuration::Global.flags);
  224. recyclerInstance->Initialize(false /* forceInThread */, nullptr /* threadService */);
  225. #if FALSE
  226. // TODO: Support EnableImplicitRoots call on Recycler (or similar, e.g. constructor param)
  227. // Until then, implicitRootsMode support doesn't actually work.
  228. if (implicitRootsMode)
  229. {
  230. recycler->EnableImplicitRoots();
  231. }
  232. #endif
  233. wprintf(_u("Recycler created, initializing heap...\n"));
  234. // Initialize stack roots and add to our roots table
  235. RecyclerTestObject * stackRoots[stackRootCount];
  236. for (unsigned int i = 0; i < stackRootCount; i++)
  237. {
  238. stackRoots[i] = nullptr;
  239. roots.AddWeightedEntry(Location::Scanned(&stackRoots[i]), 1);
  240. }
  241. // Initialize global roots and add to our roots table
  242. for (unsigned int i = 0; i < globalRootCount; i++)
  243. {
  244. globalRoots[i] = nullptr;
  245. roots.AddWeightedEntry(Location::Rooted(&globalRoots[i]), 1);
  246. }
  247. // MemProtect only:
  248. // Initialize implicit roots and add to our roots table
  249. if (implicitRootsMode)
  250. {
  251. for (unsigned int i = 0; i < implicitRootCount; i++)
  252. {
  253. implicitRoots[i] = nullptr;
  254. roots.AddWeightedEntry(Location::ImplicitRoot(&implicitRoots[i]), 1);
  255. }
  256. }
  257. // Initialize GC heap randomly
  258. for (unsigned int i = 0; i < initializeCount; i++)
  259. {
  260. InsertObject();
  261. }
  262. wprintf(_u("Initialization complete\n"));
  263. // Do an initial walk
  264. WalkHeap();
  265. // Loop, continually doing heap operations, and periodically doing a full heap walk
  266. while (true)
  267. {
  268. for (unsigned int i = 0; i < operationsPerHeapWalk; i++)
  269. {
  270. DoHeapOperation();
  271. }
  272. WalkHeap();
  273. // Dispose now
  274. recyclerInstance->FinishDisposeObjectsNow<FinishDispose>();
  275. }
  276. }
  277. catch (Js::OutOfMemoryException)
  278. {
  279. printf("Error: OOM\n");
  280. }
  281. wprintf(_u("==== Test completed.\n"));
  282. }
  283. //////////////////// End test implementations ////////////////////
  284. //////////////////// Begin test stubs ////////////////////
  285. // This is consumed by AutoSystemInfo. AutoSystemInfo is in Chakra.Common.Core.lib, which is linked
  286. // into multiple DLLs. The hosting DLL provides the implementation of this function.
  287. _Success_(return)
  288. bool GetDeviceFamilyInfo(
  289. _Out_opt_ ULONGLONG* /*pullUAPInfo*/,
  290. _Out_opt_ ULONG* /*pulDeviceFamily*/,
  291. _Out_opt_ ULONG* /*pulDeviceForm*/)
  292. {
  293. return false;
  294. }
  295. //////////////////// End test stubs ////////////////////
  296. //////////////////// Begin program entrypoint ////////////////////
  297. void usage(const WCHAR* self)
  298. {
  299. wprintf(
  300. _u("usage: %s [-?|-v] [-js <jscript options from here on>]\n")
  301. _u(" -v\n\tverbose logging\n"),
  302. self);
  303. }
  304. int __cdecl wmain(int argc, __in_ecount(argc) WCHAR* argv[])
  305. {
  306. int jscriptOptions = 0;
  307. for (int i = 1; i < argc; ++i)
  308. {
  309. if (argv[i][0] == '-')
  310. {
  311. if (wcscmp(argv[i], _u("-?")) == 0)
  312. {
  313. usage(argv[0]);
  314. exit(1);
  315. }
  316. else if (wcscmp(argv[i], _u("-v")) == 0)
  317. {
  318. verbose = true;
  319. }
  320. else if (wcscmp(argv[i], _u("-js")) == 0 || wcscmp(argv[i], _u("-JS")) == 0)
  321. {
  322. jscriptOptions = i;
  323. break;
  324. }
  325. else
  326. {
  327. wprintf(_u("unknown argument '%s'\n"), argv[i]);
  328. usage(argv[0]);
  329. exit(1);
  330. }
  331. }
  332. else
  333. {
  334. wprintf(_u("unknown argument '%s'\n"), argv[i]);
  335. usage(argv[0]);
  336. exit(1);
  337. }
  338. }
  339. // Parse the rest of the command line as js options
  340. if (jscriptOptions)
  341. {
  342. CmdLineArgsParser parser(nullptr);
  343. parser.Parse(argc - jscriptOptions, argv + jscriptOptions);
  344. }
  345. // Run the actual test
  346. SimpleRecyclerTest();
  347. return 0;
  348. }
  349. #ifndef _WIN32
  350. int main(int argc, char** argv)
  351. {
  352. // Ignoring mem-alloc failures here as this is
  353. // simply a test tool. We can add more error checking
  354. // here later if desired.
  355. char16** args = new char16*[argc];
  356. for (int i = 0; i < argc; i++)
  357. {
  358. args[i] = UTIL_MBToWC_Alloc(argv[i], -1);
  359. }
  360. int ret = wmain(argc, args);
  361. for (int i = 0; i < argc; i++)
  362. {
  363. free(args[i]);
  364. }
  365. delete[] args;
  366. PAL_Shutdown();
  367. return ret;
  368. }
  369. #endif
  370. //////////////////// End program entrypoint ////////////////////