PerformanceTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. // Jolt includes
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/ConfigurationString.h>
  6. #include <Jolt/RegisterTypes.h>
  7. #include <Jolt/Core/Factory.h>
  8. #include <Jolt/Core/TempAllocator.h>
  9. #include <Jolt/Core/JobSystemThreadPool.h>
  10. #include <Jolt/Physics/PhysicsSettings.h>
  11. #include <Jolt/Physics/PhysicsSystem.h>
  12. #include <Jolt/Physics/Collision/NarrowPhaseStats.h>
  13. #include <Jolt/Physics/StateRecorderImpl.h>
  14. #include <Jolt/Physics/DeterminismLog.h>
  15. #ifdef JPH_DEBUG_RENDERER
  16. #include <Jolt/Renderer/DebugRendererRecorder.h>
  17. #include <Jolt/Core/StreamWrapper.h>
  18. #endif // JPH_DEBUG_RENDERER
  19. #ifdef JPH_PLATFORM_ANDROID
  20. #include <android/log.h>
  21. #include <android_native_app_glue.h>
  22. #endif // JPH_PLATFORM_ANDROID
  23. // STL includes
  24. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  25. #include <iostream>
  26. #include <thread>
  27. #include <chrono>
  28. #include <memory>
  29. #include <cstdarg>
  30. JPH_SUPPRESS_WARNINGS_STD_END
  31. using namespace JPH;
  32. using namespace JPH::literals;
  33. using namespace std;
  34. // Disable common warnings triggered by Jolt
  35. JPH_SUPPRESS_WARNINGS
  36. // Local includes
  37. #include "RagdollScene.h"
  38. #include "ConvexVsMeshScene.h"
  39. // Time step for physics
  40. constexpr float cDeltaTime = 1.0f / 60.0f;
  41. static void TraceImpl(const char *inFMT, ...)
  42. {
  43. // Format the message
  44. va_list list;
  45. va_start(list, inFMT);
  46. char buffer[1024];
  47. vsnprintf(buffer, sizeof(buffer), inFMT, list);
  48. va_end(list);
  49. // Print to the TTY
  50. #ifndef JPH_PLATFORM_ANDROID
  51. cout << buffer << endl;
  52. #else
  53. __android_log_write(ANDROID_LOG_INFO, "Jolt", buffer);
  54. #endif
  55. }
  56. // Program entry point
  57. int main(int argc, char** argv)
  58. {
  59. // Install callbacks
  60. Trace = TraceImpl;
  61. // Register allocation hook
  62. RegisterDefaultAllocator();
  63. // Parse command line parameters
  64. int specified_quality = -1;
  65. int specified_threads = -1;
  66. uint max_iterations = 500;
  67. bool disable_sleep = false;
  68. bool enable_profiler = false;
  69. #ifdef JPH_DEBUG_RENDERER
  70. bool enable_debug_renderer = false;
  71. #endif // JPH_DEBUG_RENDERER
  72. bool enable_per_frame_recording = false;
  73. bool record_state = false;
  74. bool validate_state = false;
  75. unique_ptr<PerformanceTestScene> scene;
  76. const char *validate_hash = nullptr;
  77. int repeat = 1;
  78. for (int argidx = 1; argidx < argc; ++argidx)
  79. {
  80. const char *arg = argv[argidx];
  81. if (strncmp(arg, "-s=", 3) == 0)
  82. {
  83. // Parse scene
  84. if (strcmp(arg + 3, "Ragdoll") == 0)
  85. scene = unique_ptr<PerformanceTestScene>(new RagdollScene);
  86. else if (strcmp(arg + 3, "ConvexVsMesh") == 0)
  87. scene = unique_ptr<PerformanceTestScene>(new ConvexVsMeshScene);
  88. else
  89. {
  90. Trace("Invalid scene");
  91. return 1;
  92. }
  93. }
  94. else if (strncmp(arg, "-i=", 3) == 0)
  95. {
  96. // Parse max iterations
  97. max_iterations = (uint)atoi(arg + 3);
  98. }
  99. else if (strncmp(arg, "-q=", 3) == 0)
  100. {
  101. // Parse quality
  102. if (strcmp(arg + 3, "Discrete") == 0)
  103. specified_quality = 0;
  104. else if (strcmp(arg + 3, "LinearCast") == 0)
  105. specified_quality = 1;
  106. else
  107. {
  108. Trace("Invalid quality");
  109. return 1;
  110. }
  111. }
  112. else if (strncmp(arg, "-t=max", 6) == 0)
  113. {
  114. // Default to number of threads on the system
  115. specified_threads = thread::hardware_concurrency();
  116. }
  117. else if (strncmp(arg, "-t=", 3) == 0)
  118. {
  119. // Parse threads
  120. specified_threads = atoi(arg + 3);
  121. }
  122. else if (strcmp(arg, "-no_sleep") == 0)
  123. {
  124. disable_sleep = true;
  125. }
  126. else if (strcmp(arg, "-p") == 0)
  127. {
  128. enable_profiler = true;
  129. }
  130. #ifdef JPH_DEBUG_RENDERER
  131. else if (strcmp(arg, "-r") == 0)
  132. {
  133. enable_debug_renderer = true;
  134. }
  135. #endif // JPH_DEBUG_RENDERER
  136. else if (strcmp(arg, "-f") == 0)
  137. {
  138. enable_per_frame_recording = true;
  139. }
  140. else if (strcmp(arg, "-rs") == 0)
  141. {
  142. record_state = true;
  143. }
  144. else if (strcmp(arg, "-vs") == 0)
  145. {
  146. validate_state = true;
  147. }
  148. else if (strncmp(arg, "-validate_hash=", 15) == 0)
  149. {
  150. validate_hash = arg + 15;
  151. }
  152. else if (strncmp(arg, "-repeat=", 8) == 0)
  153. {
  154. // Parse repeat count
  155. repeat = atoi(arg + 8);
  156. }
  157. else if (strcmp(arg, "-h") == 0)
  158. {
  159. // Print usage
  160. Trace("Usage:\n"
  161. "-s=<scene>: Select scene (Ragdoll, ConvexVsMesh)\n"
  162. "-i=<num physics steps>: Number of physics steps to simulate (default 500)\n"
  163. "-q=<quality>: Test only with specified quality (Discrete, LinearCast)\n"
  164. "-t=<num threads>: Test only with N threads (default is to iterate over 1 .. num hardware threads)\n"
  165. "-t=max: Test with the number of threads available on the system\n"
  166. "-p: Write out profiles\n"
  167. "-r: Record debug renderer output for JoltViewer\n"
  168. "-f: Record per frame timings\n"
  169. "-no_sleep: Disable sleeping\n"
  170. "-rs: Record state\n"
  171. "-vs: Validate state\n"
  172. "-validate_hash=<hash>: Validate hash (return 0 if successful, 1 if failed)\n"
  173. "-repeat=<num>: Repeat all tests <num> times");
  174. return 0;
  175. }
  176. }
  177. // Create a factory
  178. Factory::sInstance = new Factory();
  179. // Register all Jolt physics types
  180. RegisterTypes();
  181. // Create temp allocator
  182. TempAllocatorImpl temp_allocator(10 * 1024 * 1024);
  183. // Load the scene
  184. if (scene == nullptr)
  185. scene = unique_ptr<PerformanceTestScene>(new RagdollScene);
  186. if (!scene->Load())
  187. return 1;
  188. // Show used instruction sets
  189. Trace(GetConfigurationString());
  190. // Output scene we're running
  191. Trace("Running scene: %s", scene->GetName());
  192. // Create mapping table from object layer to broadphase layer
  193. BPLayerInterfaceImpl broad_phase_layer_interface;
  194. // Create class that filters object vs broadphase layers
  195. ObjectVsBroadPhaseLayerFilterImpl object_vs_broadphase_layer_filter;
  196. // Create class that filters object vs object layers
  197. ObjectLayerPairFilterImpl object_vs_object_layer_filter;
  198. // Start profiling this program
  199. JPH_PROFILE_START("Main");
  200. // Trace header
  201. Trace("Motion Quality, Thread Count, Steps / Second, Hash");
  202. // Repeat test
  203. for (int r = 0; r < repeat; ++r)
  204. {
  205. // Iterate motion qualities
  206. for (uint mq = 0; mq < 2; ++mq)
  207. {
  208. // Skip quality if another was specified
  209. if (specified_quality != -1 && mq != (uint)specified_quality)
  210. continue;
  211. // Determine motion quality
  212. EMotionQuality motion_quality = mq == 0? EMotionQuality::Discrete : EMotionQuality::LinearCast;
  213. String motion_quality_str = mq == 0? "Discrete" : "LinearCast";
  214. // Determine which thread counts to test
  215. Array<uint> thread_permutations;
  216. if (specified_threads > 0)
  217. thread_permutations.push_back((uint)specified_threads - 1);
  218. else
  219. for (uint num_threads = 0; num_threads < thread::hardware_concurrency(); ++num_threads)
  220. thread_permutations.push_back(num_threads);
  221. // Test thread permutations
  222. for (uint num_threads : thread_permutations)
  223. {
  224. // Create job system with desired number of threads
  225. JobSystemThreadPool job_system(cMaxPhysicsJobs, cMaxPhysicsBarriers, num_threads);
  226. // Create physics system
  227. PhysicsSystem physics_system;
  228. physics_system.Init(10240, 0, 65536, 10240, broad_phase_layer_interface, object_vs_broadphase_layer_filter, object_vs_object_layer_filter);
  229. // Start test scene
  230. scene->StartTest(physics_system, motion_quality);
  231. // Disable sleeping if requested
  232. if (disable_sleep)
  233. {
  234. const BodyLockInterface &bli = physics_system.GetBodyLockInterfaceNoLock();
  235. BodyIDVector body_ids;
  236. physics_system.GetBodies(body_ids);
  237. for (BodyID id : body_ids)
  238. {
  239. BodyLockWrite lock(bli, id);
  240. if (lock.Succeeded())
  241. {
  242. Body &body = lock.GetBody();
  243. if (!body.IsStatic())
  244. body.SetAllowSleeping(false);
  245. }
  246. }
  247. }
  248. // Optimize the broadphase to prevent an expensive first frame
  249. physics_system.OptimizeBroadPhase();
  250. // A tag used to identify the test
  251. String tag = ToLower(motion_quality_str) + "_th" + ConvertToString(num_threads + 1);
  252. #ifdef JPH_DEBUG_RENDERER
  253. // Open renderer output
  254. ofstream renderer_file;
  255. if (enable_debug_renderer)
  256. renderer_file.open(("performance_test_" + tag + ".jor").c_str(), ofstream::out | ofstream::binary | ofstream::trunc);
  257. StreamOutWrapper renderer_stream(renderer_file);
  258. DebugRendererRecorder renderer(renderer_stream);
  259. #endif // JPH_DEBUG_RENDERER
  260. // Open per frame timing output
  261. ofstream per_frame_file;
  262. if (enable_per_frame_recording)
  263. {
  264. per_frame_file.open(("per_frame_" + tag + ".csv").c_str(), ofstream::out | ofstream::trunc);
  265. per_frame_file << "Frame, Time (ms)" << endl;
  266. }
  267. ofstream record_state_file;
  268. ifstream validate_state_file;
  269. if (record_state)
  270. record_state_file.open(("state_" + ToLower(motion_quality_str) + ".bin").c_str(), ofstream::out | ofstream::binary | ofstream::trunc);
  271. else if (validate_state)
  272. validate_state_file.open(("state_" + ToLower(motion_quality_str) + ".bin").c_str(), ifstream::in | ifstream::binary);
  273. chrono::nanoseconds total_duration(0);
  274. // Step the world for a fixed amount of iterations
  275. for (uint iterations = 0; iterations < max_iterations; ++iterations)
  276. {
  277. JPH_PROFILE_NEXTFRAME();
  278. JPH_DET_LOG("Iteration: " << iterations);
  279. // Start measuring
  280. chrono::high_resolution_clock::time_point clock_start = chrono::high_resolution_clock::now();
  281. // Do a physics step
  282. physics_system.Update(cDeltaTime, 1, 1, &temp_allocator, &job_system);
  283. // Stop measuring
  284. chrono::high_resolution_clock::time_point clock_end = chrono::high_resolution_clock::now();
  285. chrono::nanoseconds duration = chrono::duration_cast<chrono::nanoseconds>(clock_end - clock_start);
  286. total_duration += duration;
  287. #ifdef JPH_DEBUG_RENDERER
  288. if (enable_debug_renderer)
  289. {
  290. // Draw the state of the world
  291. BodyManager::DrawSettings settings;
  292. physics_system.DrawBodies(settings, &renderer);
  293. // Mark end of frame
  294. renderer.EndFrame();
  295. }
  296. #endif // JPH_DEBUG_RENDERER
  297. // Record time taken this iteration
  298. if (enable_per_frame_recording)
  299. per_frame_file << iterations << ", " << (1.0e-6 * duration.count()) << endl;
  300. // Dump profile information every 100 iterations
  301. if (enable_profiler && iterations % 100 == 0)
  302. {
  303. JPH_PROFILE_DUMP(tag + "_it" + ConvertToString(iterations));
  304. }
  305. if (record_state)
  306. {
  307. // Record state
  308. StateRecorderImpl recorder;
  309. physics_system.SaveState(recorder);
  310. // Write to file
  311. string data = recorder.GetData();
  312. size_t size = data.size();
  313. record_state_file.write((char *)&size, sizeof(size));
  314. record_state_file.write(data.data(), size);
  315. }
  316. else if (validate_state)
  317. {
  318. // Read state
  319. size_t size = 0;
  320. validate_state_file.read((char *)&size, sizeof(size));
  321. string data;
  322. data.resize(size);
  323. validate_state_file.read(data.data(), size);
  324. // Copy to validator
  325. StateRecorderImpl validator;
  326. validator.WriteBytes(data.data(), size);
  327. // Validate state
  328. validator.SetValidating(true);
  329. physics_system.RestoreState(validator);
  330. }
  331. #ifdef JPH_ENABLE_DETERMINISM_LOG
  332. const BodyLockInterface &bli = physics_system.GetBodyLockInterfaceNoLock();
  333. BodyIDVector body_ids;
  334. physics_system.GetBodies(body_ids);
  335. for (BodyID id : body_ids)
  336. {
  337. BodyLockRead lock(bli, id);
  338. const Body &body = lock.GetBody();
  339. if (!body.IsStatic())
  340. JPH_DET_LOG(id << ": p: " << body.GetPosition() << " r: " << body.GetRotation() << " v: " << body.GetLinearVelocity() << " w: " << body.GetAngularVelocity());
  341. }
  342. #endif // JPH_ENABLE_DETERMINISM_LOG
  343. }
  344. // Calculate hash of all positions and rotations of the bodies
  345. uint64 hash = HashBytes(nullptr, 0); // Ensure we start with the proper seed
  346. BodyInterface &bi = physics_system.GetBodyInterfaceNoLock();
  347. BodyIDVector body_ids;
  348. physics_system.GetBodies(body_ids);
  349. for (BodyID id : body_ids)
  350. {
  351. RVec3 pos = bi.GetPosition(id);
  352. hash = HashBytes(&pos, 3 * sizeof(Real), hash);
  353. Quat rot = bi.GetRotation(id);
  354. hash = HashBytes(&rot, sizeof(Quat), hash);
  355. }
  356. // Convert hash to string
  357. stringstream hash_stream;
  358. hash_stream << "0x" << hex << hash << dec;
  359. string hash_str = hash_stream.str();
  360. // Stop test scene
  361. scene->StopTest(physics_system);
  362. // Trace stat line
  363. Trace("%s, %d, %f, %s", motion_quality_str.c_str(), num_threads + 1, double(max_iterations) / (1.0e-9 * total_duration.count()), hash_str.c_str());
  364. // Check hash code
  365. if (validate_hash != nullptr && hash_str != validate_hash)
  366. {
  367. Trace("Fail hash validation. Was: %s, expected: %s", hash_str.c_str(), validate_hash);
  368. return 1;
  369. }
  370. }
  371. }
  372. }
  373. #ifdef JPH_TRACK_NARROWPHASE_STATS
  374. NarrowPhaseStat::sReportStats();
  375. #endif // JPH_TRACK_NARROWPHASE_STATS
  376. // Destroy the factory
  377. delete Factory::sInstance;
  378. Factory::sInstance = nullptr;
  379. // End profiling this program
  380. JPH_PROFILE_END();
  381. return 0;
  382. }
  383. #ifdef JPH_PLATFORM_ANDROID
  384. // Main entry point for android
  385. void android_main(struct android_app *ioApp)
  386. {
  387. // Run the regular main function
  388. const char *args[] = { "Unused", "-s=ConvexVsMesh", "-t=max" };
  389. main(size(args), (char **)args);
  390. }
  391. #endif // JPH_PLATFORM_ANDROID