• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/profiler/periodic_sampling_scheduler.h"
6 
7 #include "base/rand_util.h"
8 
9 namespace base {
10 
PeriodicSamplingScheduler(TimeDelta sampling_duration,double fraction_of_execution_time_to_sample,TimeTicks start_time)11 PeriodicSamplingScheduler::PeriodicSamplingScheduler(
12     TimeDelta sampling_duration,
13     double fraction_of_execution_time_to_sample,
14     TimeTicks start_time)
15     : period_duration_(sampling_duration /
16                        fraction_of_execution_time_to_sample),
17       sampling_duration_(sampling_duration),
18       period_start_time_(start_time) {
19   DCHECK(sampling_duration_ <= period_duration_);
20 }
21 
22 PeriodicSamplingScheduler::~PeriodicSamplingScheduler() = default;
23 
GetTimeToNextCollection()24 TimeDelta PeriodicSamplingScheduler::GetTimeToNextCollection() {
25   const TimeTicks now = Now();
26   // Avoid scheduling in the past in the presence of discontinuous jumps in
27   // the current TimeTicks.
28   period_start_time_ = std::max(period_start_time_, now);
29 
30   const TimeDelta sampling_offset =
31       (period_duration_ - sampling_duration_) * RandDouble();
32   const TimeTicks next_collection_time = period_start_time_ + sampling_offset;
33   period_start_time_ += period_duration_;
34   return next_collection_time - now;
35 }
36 
RandDouble() const37 double PeriodicSamplingScheduler::RandDouble() const {
38   return base::RandDouble();
39 }
40 
Now() const41 TimeTicks PeriodicSamplingScheduler::Now() const {
42   return TimeTicks::Now();
43 }
44 
45 }  // namespace base
46