periodic_update.cc
Go to the documentation of this file.
1 // Copyright 2022 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 
16 
18 
19 #include <atomic>
20 
22 
23 namespace grpc_core {
24 
26  // updates_remaining_ just reached 0 and the thread calling this function was
27  // the decrementer that got us there.
28  // We can now safely mutate any non-atomic mutable variables (we've got a
29  // guarantee that no other thread will), and by the time this function returns
30  // we must store a postive number into updates_remaining_.
31  auto now = ExecCtx::Get()->Now();
32  Duration time_so_far = now - period_start_;
33  if (time_so_far < period_) {
34  // At most double the number of updates remaining until the next period.
35  // At least try to estimate when we'll reach it.
36  int64_t better_guess;
37  if (time_so_far.millis() == 0) {
38  better_guess = expected_updates_per_period_ * 2;
39  } else {
40  // Determine a scaling factor that would have gotten us to the next
41  // period, but clamp between 1.01 (at least 1% increase in guesses)
42  // and 2.0 (at most doubling) - to avoid running completely out of
43  // control.
44  const double scale =
45  Clamp(period_.seconds() / time_so_far.seconds(), 1.01, 2.0);
46  better_guess = expected_updates_per_period_ * scale;
47  if (better_guess <= expected_updates_per_period_) {
48  better_guess = expected_updates_per_period_ + 1;
49  }
50  }
51  // Store the remainder left. Note that updates_remaining_ may have been
52  // decremented by another thread whilst we performed the above calculations:
53  // we simply discard those decrements.
55  std::memory_order_release);
56  // Not quite done, return false, try for longer.
57  return false;
58  }
59  // Finished period, start a new one and return true.
60  // We try to predict how many update periods we'd need to cover the full time
61  // span, and we increase that by 1% to attempt to tend to not enter the above
62  // stanza.
68  std::memory_order_release);
69  return true;
70 }
71 
72 } // namespace grpc_core
grpc_core::Duration::seconds
double seconds() const
Definition: src/core/lib/gprpp/time.h:209
now
static double now(void)
Definition: test/core/fling/client.cc:130
grpc_core
Definition: call_metric_recorder.h:31
useful.h
periodic_update.h
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc_core::PeriodicUpdate::updates_remaining_
std::atomic< int64_t > updates_remaining_
Definition: periodic_update.h:65
grpc_core::PeriodicUpdate::period_
const Duration period_
Definition: periodic_update.h:62
grpc_core::PeriodicUpdate::MaybeEndPeriod
GRPC_MUST_USE_RESULT bool MaybeEndPeriod()
Definition: periodic_update.cc:25
bm_speedup.scale
def scale(a, mul)
Definition: bm_speedup.py:24
grpc_core::PeriodicUpdate::expected_updates_per_period_
int64_t expected_updates_per_period_
Definition: periodic_update.h:64
grpc_core::Duration::millis
constexpr int64_t millis() const
Definition: src/core/lib/gprpp/time.h:208
grpc_core::Clamp
T Clamp(T val, T min, T max)
Definition: useful.h:31
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_core::Duration
Definition: src/core/lib/gprpp/time.h:122
grpc_core::PeriodicUpdate::period_start_
Timestamp period_start_
Definition: periodic_update.h:63
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:42