jolt_job_system.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**************************************************************************/
  2. /* jolt_job_system.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "jolt_job_system.h"
  31. #include "../jolt_project_settings.h"
  32. #include "core/debugger/engine_debugger.h"
  33. #include "core/object/worker_thread_pool.h"
  34. #include "core/os/os.h"
  35. #include "core/os/time.h"
  36. #include "Jolt/Physics/PhysicsSettings.h"
  37. void JoltJobSystem::Job::_execute(void *p_user_data) {
  38. Job *job = static_cast<Job *>(p_user_data);
  39. #ifdef DEBUG_ENABLED
  40. const uint64_t time_start = Time::get_singleton()->get_ticks_usec();
  41. #endif
  42. job->Execute();
  43. #ifdef DEBUG_ENABLED
  44. const uint64_t time_end = Time::get_singleton()->get_ticks_usec();
  45. const uint64_t time_elapsed = time_end - time_start;
  46. timings_lock.lock();
  47. timings_by_job[job->name] += time_elapsed;
  48. timings_lock.unlock();
  49. #endif
  50. job->Release();
  51. }
  52. JoltJobSystem::Job::Job(const char *p_name, JPH::ColorArg p_color, JPH::JobSystem *p_job_system, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count) :
  53. JPH::JobSystem::Job(p_name, p_color, p_job_system, p_job_function, p_dependency_count)
  54. #ifdef DEBUG_ENABLED
  55. ,
  56. name(p_name)
  57. #endif
  58. {
  59. }
  60. JoltJobSystem::Job::~Job() {
  61. if (task_id != -1) {
  62. WorkerThreadPool::get_singleton()->wait_for_task_completion(task_id);
  63. }
  64. }
  65. void JoltJobSystem::Job::push_completed(Job *p_job) {
  66. Job *prev_head = nullptr;
  67. do {
  68. prev_head = completed_head.load(std::memory_order_relaxed);
  69. p_job->completed_next = prev_head;
  70. } while (!completed_head.compare_exchange_weak(prev_head, p_job, std::memory_order_release, std::memory_order_relaxed));
  71. }
  72. JoltJobSystem::Job *JoltJobSystem::Job::pop_completed() {
  73. Job *prev_head = nullptr;
  74. do {
  75. prev_head = completed_head.load(std::memory_order_relaxed);
  76. if (prev_head == nullptr) {
  77. return nullptr;
  78. }
  79. } while (!completed_head.compare_exchange_weak(prev_head, prev_head->completed_next, std::memory_order_acquire, std::memory_order_relaxed));
  80. return prev_head;
  81. }
  82. void JoltJobSystem::Job::queue() {
  83. AddRef();
  84. // Ideally we would use Jolt's actual job name here, but I'd rather not incur the overhead of a memory allocation or
  85. // thread-safe lookup every time we create/queue a task. So instead we use the same cached description for all of them.
  86. static const String task_name("Jolt Physics");
  87. task_id = WorkerThreadPool::get_singleton()->add_native_task(&_execute, this, true, task_name);
  88. }
  89. int JoltJobSystem::GetMaxConcurrency() const {
  90. return thread_count;
  91. }
  92. JPH::JobHandle JoltJobSystem::CreateJob(const char *p_name, JPH::ColorArg p_color, const JPH::JobSystem::JobFunction &p_job_function, JPH::uint32 p_dependency_count) {
  93. Job *job = nullptr;
  94. while (true) {
  95. JPH::uint32 job_index = jobs.ConstructObject(p_name, p_color, this, p_job_function, p_dependency_count);
  96. if (job_index != JPH::FixedSizeFreeList<Job>::cInvalidObjectIndex) {
  97. job = &jobs.Get(job_index);
  98. break;
  99. }
  100. WARN_PRINT_ONCE("Jolt Physics job system exceeded the maximum number of jobs. This should not happen. Please report this. Waiting for jobs to become available...");
  101. OS::get_singleton()->delay_usec(100);
  102. _reclaim_jobs();
  103. }
  104. // This will increment the job's reference count, so must happen before we queue the job
  105. JPH::JobHandle job_handle(job);
  106. if (p_dependency_count == 0) {
  107. QueueJob(job);
  108. }
  109. return job_handle;
  110. }
  111. void JoltJobSystem::QueueJob(JPH::JobSystem::Job *p_job) {
  112. static_cast<Job *>(p_job)->queue();
  113. }
  114. void JoltJobSystem::QueueJobs(JPH::JobSystem::Job **p_jobs, JPH::uint p_job_count) {
  115. for (JPH::uint i = 0; i < p_job_count; ++i) {
  116. QueueJob(p_jobs[i]);
  117. }
  118. }
  119. void JoltJobSystem::FreeJob(JPH::JobSystem::Job *p_job) {
  120. Job::push_completed(static_cast<Job *>(p_job));
  121. }
  122. void JoltJobSystem::_reclaim_jobs() {
  123. while (Job *job = Job::pop_completed()) {
  124. jobs.DestructObject(job);
  125. }
  126. }
  127. JoltJobSystem::JoltJobSystem() :
  128. JPH::JobSystemWithBarrier(JPH::cMaxPhysicsBarriers),
  129. thread_count(MAX(1, WorkerThreadPool::get_singleton()->get_thread_count())) {
  130. jobs.Init(JPH::cMaxPhysicsJobs, JPH::cMaxPhysicsJobs);
  131. }
  132. void JoltJobSystem::pre_step() {
  133. // Nothing to do.
  134. }
  135. void JoltJobSystem::post_step() {
  136. _reclaim_jobs();
  137. }
  138. #ifdef DEBUG_ENABLED
  139. void JoltJobSystem::flush_timings() {
  140. static const StringName profiler_name("servers");
  141. EngineDebugger *engine_debugger = EngineDebugger::get_singleton();
  142. if (engine_debugger->is_profiling(profiler_name)) {
  143. Array timings;
  144. for (const KeyValue<const void *, uint64_t> &E : timings_by_job) {
  145. timings.push_back(static_cast<const char *>(E.key));
  146. timings.push_back(USEC_TO_SEC(E.value));
  147. }
  148. timings.push_front("physics_3d");
  149. engine_debugger->profiler_add_frame_data(profiler_name, timings);
  150. }
  151. for (KeyValue<const void *, uint64_t> &E : timings_by_job) {
  152. E.value = 0;
  153. }
  154. }
  155. #endif