• 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 #ifndef BASE_PROFILER_PERIODIC_SAMPLING_SCHEDULER_H_
6 #define BASE_PROFILER_PERIODIC_SAMPLING_SCHEDULER_H_
7 
8 #include "base/time/time.h"
9 
10 namespace base {
11 
12 // The scheduler works by splitting execution time into repeated periods such
13 // that the time to take one collection represents
14 // |fraction_of_execution_time_to_sample| of the period, and the time not spent
15 // sampling represents 1 - |fraction_of_execution_time_to_sample| of the period.
16 // The collection start time is chosen randomly within each period such that the
17 // entire collection is contained within the period.
18 // It repeatedly schedules periodic sampling of the
19 // thread through calls to GetTimeToNextCollection().
20 class BASE_EXPORT PeriodicSamplingScheduler {
21  public:
22   PeriodicSamplingScheduler(TimeDelta sampling_duration,
23                             double fraction_of_execution_time_to_sample,
24                             TimeTicks start_time);
25 
26   PeriodicSamplingScheduler(const PeriodicSamplingScheduler&) = delete;
27   PeriodicSamplingScheduler& operator=(const PeriodicSamplingScheduler&) =
28       delete;
29 
30   virtual ~PeriodicSamplingScheduler();
31 
32   // Returns the amount of time between now and the next collection.
33   TimeDelta GetTimeToNextCollection();
34 
35  protected:
36   // Virtual to provide seams for test use.
37   virtual double RandDouble() const;
38   virtual TimeTicks Now() const;
39 
40  private:
41   const TimeDelta period_duration_;
42   const TimeDelta sampling_duration_;
43   TimeTicks period_start_time_;
44 };
45 
46 }  // namespace base
47 
48 #endif  // BASE_PROFILER_PERIODIC_SAMPLING_SCHEDULER_H_
49