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 <grpc/support/port_platform.h>
15
16 #include "src/core/lib/event_engine/thread_pool/thread_count.h"
17
18 #include <inttypes.h>
19
20 #include <cstddef>
21
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
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gprpp/time.h"
30
31 namespace grpc_event_engine {
32 namespace experimental {
33
34 // -------- LivingThreadCount --------
35
BlockUntilThreadCount(size_t desired_threads,const char * why,grpc_core::Duration stuck_timeout)36 absl::Status LivingThreadCount::BlockUntilThreadCount(
37 size_t desired_threads, const char* why,
38 grpc_core::Duration stuck_timeout) {
39 grpc_core::Timestamp timeout_baseline = grpc_core::Timestamp::Now();
40 constexpr grpc_core::Duration log_rate = grpc_core::Duration::Seconds(5);
41 size_t prev_thread_count = 0;
42 while (true) {
43 auto curr_threads = WaitForCountChange(desired_threads, log_rate / 2);
44 if (curr_threads == desired_threads) return absl::OkStatus();
45 auto elapsed = grpc_core::Timestamp::Now() - timeout_baseline;
46 if (curr_threads == prev_thread_count) {
47 if (elapsed > stuck_timeout) {
48 return absl::DeadlineExceededError(absl::StrFormat(
49 "Timed out after %f seconds", stuck_timeout.seconds()));
50 }
51 } else {
52 // the thread count has changed. Reset the timeout clock
53 prev_thread_count = curr_threads;
54 timeout_baseline = grpc_core::Timestamp::Now();
55 }
56 GRPC_LOG_EVERY_N_SEC_DELAYED(
57 log_rate.seconds(), GPR_DEBUG,
58 "Waiting for thread pool to idle before %s. (%" PRIdPTR " to %" PRIdPTR
59 "). Timing out in %0.f seconds.",
60 why, curr_threads, desired_threads,
61 (stuck_timeout - elapsed).seconds());
62 }
63 }
64
WaitForCountChange(size_t desired_threads,grpc_core::Duration timeout)65 size_t LivingThreadCount::WaitForCountChange(size_t desired_threads,
66 grpc_core::Duration timeout) {
67 size_t count;
68 auto deadline = absl::Now() + absl::Milliseconds(timeout.millis());
69 do {
70 grpc_core::MutexLock lock(&mu_);
71 count = CountLocked();
72 if (count == desired_threads) break;
73 cv_.WaitWithDeadline(&mu_, deadline);
74 } while (absl::Now() < deadline);
75 return count;
76 }
77
78 } // namespace experimental
79 } // namespace grpc_event_engine
80