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