GCStress.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. objectCreationTable.AddWeightedEntry(&LeafObject<51, 1000>::New, 10);
  174. objectCreationTable.AddWeightedEntry(&ScannedObject<51, 1000>::New, 100);
  175. objectCreationTable.AddWeightedEntry(&BarrierObject<51, 1000>::New, 20);
  176. objectCreationTable.AddWeightedEntry(&TrackedObject<51, 1000>::New, 20);
  177. objectCreationTable.AddWeightedEntry(&LeafObject<1001, 50000>::New, 1);
  178. objectCreationTable.AddWeightedEntry(&ScannedObject<1001, 50000>::New, 10);
  179. objectCreationTable.AddWeightedEntry(&BarrierObject<1001, 50000>::New, 2);
  180. // objectCreationTable.AddWeightedEntry(&TrackedObject<1001, 50000>::New, 2); // Large tracked objects are not supported
  181. }
  182. void BuildOperationTable()
  183. {
  184. operationTable.AddWeightedEntry(&InsertObject, 5);
  185. operationTable.AddWeightedEntry(&ReplaceObject, 2);
  186. operationTable.AddWeightedEntry(&DeleteObject, 2);
  187. operationTable.AddWeightedEntry(&MoveObject, 5);
  188. operationTable.AddWeightedEntry(&CopyObject, 5);
  189. operationTable.AddWeightedEntry(&SwapObjects, 5);
  190. }
  191. void SimpleRecyclerTest()
  192. {
  193. // Initialize the probability tables for object creation and heap operations.
  194. BuildObjectCreationTable();
  195. BuildOperationTable();
  196. // Construct Recycler instance and use it
  197. #if ENABLE_BACKGROUND_PAGE_FREEING
  198. PageAllocator::BackgroundPageQueue backgroundPageQueue;
  199. #endif
  200. IdleDecommitPageAllocator pageAllocator(nullptr,
  201. PageAllocatorType::PageAllocatorType_Thread,
  202. Js::Configuration::Global.flags,
  203. 0 /* maxFreePageCount */, PageAllocator::DefaultMaxFreePageCount /* maxIdleFreePageCount */,
  204. false /* zero pages */
  205. #if ENABLE_BACKGROUND_PAGE_FREEING
  206. , &backgroundPageQueue
  207. #endif
  208. );
  209. try
  210. {
  211. #ifdef EXCEPTION_CHECK
  212. // REVIEW: Do we need a stack probe here? We don't care about OOM's since we deal with that below
  213. AUTO_NESTED_HANDLED_EXCEPTION_TYPE(ExceptionType_DisableCheck);
  214. #endif
  215. recyclerInstance = HeapNewZ(Recycler, nullptr, &pageAllocator, Js::Throw::OutOfMemory, Js::Configuration::Global.flags);
  216. recyclerInstance->Initialize(false /* forceInThread */, nullptr /* threadService */);
  217. #if FALSE
  218. // TODO: Support EnableImplicitRoots call on Recycler (or similar, e.g. constructor param)
  219. // Until then, implicitRootsMode support doesn't actually work.
  220. if (implicitRootsMode)
  221. {
  222. recycler->EnableImplicitRoots();
  223. }
  224. #endif
  225. wprintf(_u("Recycler created, initializing heap...\n"));
  226. // Initialize stack roots and add to our roots table
  227. RecyclerTestObject * stackRoots[stackRootCount];
  228. for (unsigned int i = 0; i < stackRootCount; i++)
  229. {
  230. stackRoots[i] = nullptr;
  231. roots.AddWeightedEntry(Location::Scanned(&stackRoots[i]), 1);
  232. }
  233. // Initialize global roots and add to our roots table
  234. for (unsigned int i = 0; i < globalRootCount; i++)
  235. {
  236. globalRoots[i] = nullptr;
  237. roots.AddWeightedEntry(Location::Rooted(&globalRoots[i]), 1);
  238. }
  239. // MemProtect only:
  240. // Initialize implicit roots and add to our roots table
  241. if (implicitRootsMode)
  242. {
  243. for (unsigned int i = 0; i < implicitRootCount; i++)
  244. {
  245. implicitRoots[i] = nullptr;
  246. roots.AddWeightedEntry(Location::ImplicitRoot(&implicitRoots[i]), 1);
  247. }
  248. }
  249. // Initialize GC heap randomly
  250. for (unsigned int i = 0; i < initializeCount; i++)
  251. {
  252. InsertObject();
  253. }
  254. wprintf(_u("Initialization complete\n"));
  255. // Do an initial walk
  256. WalkHeap();
  257. // Loop, continually doing heap operations, and periodically doing a full heap walk
  258. while (true)
  259. {
  260. for (unsigned int i = 0; i < operationsPerHeapWalk; i++)
  261. {
  262. DoHeapOperation();
  263. }
  264. WalkHeap();
  265. // Dispose now
  266. recyclerInstance->FinishDisposeObjectsNow<FinishDispose>();
  267. }
  268. }
  269. catch (Js::OutOfMemoryException)
  270. {
  271. printf("Error: OOM\n");
  272. }
  273. wprintf(_u("==== Test completed.\n"));
  274. }
  275. //////////////////// End test implementations ////////////////////
  276. //////////////////// Begin test stubs ////////////////////
  277. // This is consumed by AutoSystemInfo. AutoSystemInfo is in Chakra.Common.Core.lib, which is linked
  278. // into multiple DLLs. The hosting DLL provides the implementation of this function.
  279. _Success_(return)
  280. bool GetDeviceFamilyInfo(
  281. _Out_opt_ ULONGLONG* /*pullUAPInfo*/,
  282. _Out_opt_ ULONG* /*pulDeviceFamily*/,
  283. _Out_opt_ ULONG* /*pulDeviceForm*/)
  284. {
  285. return false;
  286. }
  287. //////////////////// End test stubs ////////////////////
  288. //////////////////// Begin program entrypoint ////////////////////
  289. void usage(const WCHAR* self)
  290. {
  291. wprintf(
  292. _u("usage: %s [-?|-v] [-js <jscript options from here on>]\n")
  293. _u(" -v\n\tverbose logging\n"),
  294. self);
  295. }
  296. int __cdecl wmain(int argc, __in_ecount(argc) WCHAR* argv[])
  297. {
  298. int jscriptOptions = 0;
  299. for (int i = 1; i < argc; ++i)
  300. {
  301. if (argv[i][0] == '-')
  302. {
  303. if (wcscmp(argv[i], _u("-?")) == 0)
  304. {
  305. usage(argv[0]);
  306. exit(1);
  307. }
  308. else if (wcscmp(argv[i], _u("-v")) == 0)
  309. {
  310. verbose = true;
  311. }
  312. else if (wcscmp(argv[i], _u("-js")) == 0 || wcscmp(argv[i], _u("-JS")) == 0)
  313. {
  314. jscriptOptions = i;
  315. break;
  316. }
  317. else
  318. {
  319. wprintf(_u("unknown argument '%s'\n"), argv[i]);
  320. usage(argv[0]);
  321. exit(1);
  322. }
  323. }
  324. else
  325. {
  326. wprintf(_u("unknown argument '%s'\n"), argv[i]);
  327. usage(argv[0]);
  328. exit(1);
  329. }
  330. }
  331. // Parse the rest of the command line as js options
  332. if (jscriptOptions)
  333. {
  334. CmdLineArgsParser parser(nullptr);
  335. parser.Parse(argc - jscriptOptions, argv + jscriptOptions);
  336. }
  337. // Run the actual test
  338. SimpleRecyclerTest();
  339. return 0;
  340. }
  341. #ifndef _WIN32
  342. int main(int argc, char** argv)
  343. {
  344. // Ignoring mem-alloc failures here as this is
  345. // simply a test tool. We can add more error checking
  346. // here later if desired.
  347. char16** args = new char16*[argc];
  348. for (int i = 0; i < argc; i++)
  349. {
  350. args[i] = UTIL_MBToWC_Alloc(argv[i], -1);
  351. }
  352. int ret = wmain(argc, args);
  353. for (int i = 0; i < argc; i++)
  354. {
  355. free(args[i]);
  356. }
  357. delete[] args;
  358. PAL_Shutdown();
  359. return ret;
  360. }
  361. #endif
  362. //////////////////// End program entrypoint ////////////////////