ThreadPoolDataStructures.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #pragma once
  2. #include <vector>
  3. #include "Baselib.h"
  4. #include "Cpp/Atomic.h"
  5. #include "Cpp/ReentrantLock.h"
  6. #include "Cpp/ConditionVariable.h"
  7. #include "os/Mutex.h"
  8. struct Il2CppDomain;
  9. struct Il2CppInternalThread;
  10. union ThreadPoolCounter
  11. {
  12. struct
  13. {
  14. int16_t max_working; /* determined by heuristic */
  15. int16_t active; /* executing worker_thread */
  16. int16_t working; /* actively executing worker_thread, not parked */
  17. int16_t parked; /* parked */
  18. } _;
  19. int64_t as_int64_t;
  20. };
  21. struct ThreadPoolDomain
  22. {
  23. Il2CppDomain* domain;
  24. int32_t outstanding_request;
  25. };
  26. struct ThreadPoolHillClimbing
  27. {
  28. int32_t wave_period;
  29. int32_t samples_to_measure;
  30. double target_throughput_ratio;
  31. double target_signal_to_noise_ratio;
  32. double max_change_per_second;
  33. double max_change_per_sample;
  34. int32_t max_thread_wave_magnitude;
  35. int32_t sample_interval_low;
  36. double thread_magnitude_multiplier;
  37. int32_t sample_interval_high;
  38. double throughput_error_smoothing_factor;
  39. double gain_exponent;
  40. double max_sample_error;
  41. double current_control_setting;
  42. int64_t total_samples;
  43. int16_t last_thread_count;
  44. double elapsed_since_last_change;
  45. double completions_since_last_change;
  46. double average_throughput_noise;
  47. double *samples;
  48. double *thread_counts;
  49. uint32_t current_sample_interval;
  50. void* random_interval_generator;
  51. int32_t accumulated_completion_count;
  52. double accumulated_sample_duration;
  53. };
  54. struct ThreadPool
  55. {
  56. ThreadPool();
  57. ThreadPoolCounter counters;
  58. std::vector<ThreadPoolDomain*> domains;
  59. baselib::ReentrantLock domains_lock;
  60. std::vector<Il2CppInternalThread*> working_threads;
  61. int32_t parked_threads_count;
  62. baselib::ConditionVariable parked_threads_cond;
  63. baselib::Lock active_threads_lock; /* protect access to working_threads and parked_threads */
  64. uint32_t worker_creation_current_second;
  65. uint32_t worker_creation_current_count;
  66. baselib::ReentrantLock worker_creation_lock;
  67. baselib::atomic<int32_t> heuristic_completions;
  68. int64_t heuristic_sample_start;
  69. int64_t heuristic_last_dequeue; // ms
  70. int64_t heuristic_last_adjustment; // ms
  71. int64_t heuristic_adjustment_interval; // ms
  72. ThreadPoolHillClimbing heuristic_hill_climbing;
  73. baselib::ReentrantLock heuristic_lock;
  74. int32_t limit_worker_min;
  75. int32_t limit_worker_max;
  76. int32_t limit_io_min;
  77. int32_t limit_io_max;
  78. void* cpu_usage_state;
  79. int32_t cpu_usage;
  80. /* suspended by the debugger */
  81. bool suspended;
  82. };
  83. enum ThreadPoolHeuristicStateTransition
  84. {
  85. TRANSITION_WARMUP,
  86. TRANSITION_INITIALIZING,
  87. TRANSITION_RANDOM_MOVE,
  88. TRANSITION_CLIMBING_MOVE,
  89. TRANSITION_CHANGE_POINT,
  90. TRANSITION_STABILIZING,
  91. TRANSITION_STARVATION,
  92. TRANSITION_THREAD_TIMED_OUT,
  93. TRANSITION_UNDEFINED,
  94. };