23-max-thread-limitation.patch 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. --- a/src/core/lib/event_engine/thread_pool.cc (e2ca673ad16f4cb0e0e81da057545ef9583bd947)
  2. +++ b/src/core/lib/event_engine/thread_pool.cc (working tree)
  3. @@ -34,9 +34,30 @@
  4. #include "src/core/lib/gprpp/thd.h"
  5. #include "src/core/lib/gprpp/time.h"
  6. +namespace {
  7. + size_t threads_limit_ = 0;
  8. +}
  9. +
  10. namespace grpc_event_engine {
  11. namespace experimental {
  12. +size_t ThreadPool::SetThreadsLimit(size_t count) {
  13. + size_t prev = threads_limit_;
  14. + threads_limit_ = count;
  15. + return prev;
  16. +}
  17. +
  18. +unsigned ThreadPool::GetMaxSystemThread() {
  19. + unsigned max_threads = grpc_core::Clamp(gpr_cpu_num_cores(), 2u, 32u);
  20. +
  21. + if (threads_limit_) {
  22. + unsigned new_max_threads = std::min(max_threads, static_cast<unsigned>(threads_limit_));
  23. + gpr_log(GPR_INFO, "Threads limit changed from %u to %u", max_threads, new_max_threads);
  24. + max_threads = new_max_threads;
  25. + }
  26. + return max_threads;
  27. +}
  28. +
  29. void ThreadPool::StartThread(StatePtr state, StartThreadReason reason) {
  30. state->thread_count.Add();
  31. const auto now = grpc_core::Timestamp::Now();
  32. --- a/src/core/lib/event_engine/thread_pool.h (e2ca673ad16f4cb0e0e81da057545ef9583bd947)
  33. +++ b/src/core/lib/event_engine/thread_pool.h (working tree)
  34. @@ -62,6 +62,9 @@ class ThreadPool final : public Forkable, public Executor {
  35. // Returns true if the current thread is a thread pool thread.
  36. static bool IsThreadPoolThread();
  37. + // Set the maximum numbers of treads for threadpool
  38. + static size_t SetThreadsLimit(size_t count);
  39. +
  40. private:
  41. class Queue {
  42. public:
  43. @@ -129,8 +132,9 @@ class ThreadPool final : public Forkable, public Executor {
  44. static void StartThread(StatePtr state, StartThreadReason reason);
  45. void Postfork();
  46. - const unsigned reserve_threads_ =
  47. - grpc_core::Clamp(gpr_cpu_num_cores(), 2u, 32u);
  48. + unsigned GetMaxSystemThread();
  49. +
  50. + const unsigned reserve_threads_ = GetMaxSystemThread();
  51. const StatePtr state_ = std::make_shared<State>(reserve_threads_);
  52. std::atomic<bool> quiesced_{false};
  53. };