• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "src/core/lib/event_engine/thread_pool/thread_count.h"
15 
16 #include <grpc/support/port_platform.h>
17 #include <inttypes.h>
18 
19 #include <cstddef>
20 
21 #include "absl/log/log.h"
22 #include "absl/status/status.h"
23 #include "absl/strings/str_format.h"
24 #include "absl/time/clock.h"
25 #include "absl/time/time.h"
26 #include "src/core/util/time.h"
27 
28 namespace grpc_event_engine {
29 namespace experimental {
30 
31 // -------- LivingThreadCount --------
32 
BlockUntilThreadCount(size_t desired_threads,const char * why,grpc_core::Duration stuck_timeout)33 absl::Status LivingThreadCount::BlockUntilThreadCount(
34     size_t desired_threads, const char* why,
35     grpc_core::Duration stuck_timeout) {
36   grpc_core::Timestamp timeout_baseline = grpc_core::Timestamp::Now();
37   constexpr grpc_core::Duration log_rate = grpc_core::Duration::Seconds(5);
38   size_t prev_thread_count = 0;
39   while (true) {
40     auto curr_threads = WaitForCountChange(desired_threads, log_rate / 2);
41     if (curr_threads == desired_threads) return absl::OkStatus();
42     auto elapsed = grpc_core::Timestamp::Now() - timeout_baseline;
43     if (curr_threads == prev_thread_count) {
44       if (elapsed > stuck_timeout) {
45         return absl::DeadlineExceededError(absl::StrFormat(
46             "Timed out after %f seconds", stuck_timeout.seconds()));
47       }
48     } else {
49       // the thread count has changed. Reset the timeout clock
50       prev_thread_count = curr_threads;
51       timeout_baseline = grpc_core::Timestamp::Now();
52     }
53     GRPC_LOG_EVERY_N_SEC_DELAYED_DEBUG(
54         log_rate.seconds(),
55         "Waiting for thread pool to idle before %s. (%" PRIdPTR " to %" PRIdPTR
56         "). Timing out in %0.f seconds.",
57         why, curr_threads, desired_threads,
58         (stuck_timeout - elapsed).seconds());
59   }
60 }
61 
WaitForCountChange(size_t desired_threads,grpc_core::Duration timeout)62 size_t LivingThreadCount::WaitForCountChange(size_t desired_threads,
63                                              grpc_core::Duration timeout) {
64   size_t count;
65   auto deadline = absl::Now() + absl::Milliseconds(timeout.millis());
66   do {
67     grpc_core::MutexLock lock(&mu_);
68     count = CountLocked();
69     if (count == desired_threads) break;
70     cv_.WaitWithDeadline(&mu_, deadline);
71   } while (absl::Now() < deadline);
72   return count;
73 }
74 
75 }  // namespace experimental
76 }  // namespace grpc_event_engine
77