OLD | NEW |
---|---|
(Empty) | |
1 #include "platform/scheduler/base/thread_load_tracker.h" | |
haraken
2016/08/23 02:35:45
Add a copyright.
| |
2 | |
3 namespace blink { | |
4 namespace scheduler { | |
5 | |
6 namespace { | |
7 | |
8 const int kLoadReportingIntervalInSeconds = 1; | |
9 const int kWaitingPeriodBeforeReportingInSeconds = 10; | |
10 | |
11 } // namespace | |
12 | |
13 ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now, | |
14 const Callback& callback) | |
15 : time_(now), | |
16 next_reporting_time_(now), | |
17 thread_state_(ThreadState::ACTIVE), | |
18 last_state_change_time_(now), | |
19 waiting_period_( | |
20 base::TimeDelta::FromSeconds(kWaitingPeriodBeforeReportingInSeconds)), | |
21 reporting_interval_( | |
22 base::TimeDelta::FromSeconds(kLoadReportingIntervalInSeconds)), | |
23 callback_(callback) {} | |
24 | |
25 ThreadLoadTracker::~ThreadLoadTracker() {} | |
26 | |
27 void ThreadLoadTracker::Pause(base::TimeTicks now) { | |
28 Advance(now, TaskState::IDLE); | |
29 thread_state_ = ThreadState::PAUSED; | |
30 last_state_change_time_ = now; | |
31 } | |
32 | |
33 void ThreadLoadTracker::Resume(base::TimeTicks now) { | |
34 Advance(now, TaskState::IDLE); | |
35 thread_state_ = ThreadState::ACTIVE; | |
36 last_state_change_time_ = now; | |
37 } | |
38 | |
39 void ThreadLoadTracker::RecordTaskTime(base::TimeTicks start_time, | |
40 base::TimeTicks end_time) { | |
41 start_time = std::max(last_state_change_time_, start_time); | |
42 end_time = std::max(last_state_change_time_, end_time); | |
43 | |
44 Advance(start_time, TaskState::IDLE); | |
45 Advance(end_time, TaskState::TASK_RUNNING); | |
46 } | |
47 | |
48 void ThreadLoadTracker::RecordIdle(base::TimeTicks now) { | |
49 Advance(now, TaskState::IDLE); | |
50 } | |
51 | |
52 void ThreadLoadTracker::Advance(base::TimeTicks now, TaskState task_state) { | |
53 // This function advances |time_| to now and calls |callback_| | |
54 // when appropriate. | |
55 if (time_ > now) { | |
56 return; | |
57 } | |
58 | |
59 if (thread_state_ == ThreadState::PAUSED) { | |
60 // If the load tracker is paused, bail out early. | |
61 time_ = now; | |
62 next_reporting_time_ = now + reporting_interval_; | |
63 return; | |
64 } | |
65 | |
66 while (time_ < now) { | |
67 // Forward time_ to the earliest of following: | |
68 // a) time to call |callback_| | |
69 // b) requested time to forward (|now|). | |
70 base::TimeTicks next_current_time = std::min(next_reporting_time_, now); | |
71 | |
72 base::TimeDelta delta = next_current_time - time_; | |
73 | |
74 // Forward time and recalculate |total_time_| and |total_runtime_|. | |
75 if (thread_state_ == ThreadState::ACTIVE) { | |
76 total_time_ += delta; | |
77 if (task_state == TaskState::TASK_RUNNING) { | |
78 total_runtime_ += delta; | |
79 } | |
80 } | |
81 time_ = next_current_time; | |
82 | |
83 if (time_ == next_reporting_time_) { | |
84 // Call |callback_| if need and update next callback time. | |
85 if (thread_state_ == ThreadState::ACTIVE && | |
86 total_time_ >= waiting_period_) { | |
87 callback_.Run(time_, Load()); | |
88 } | |
89 next_reporting_time_ += reporting_interval_; | |
90 } | |
91 } | |
92 } | |
93 | |
94 double ThreadLoadTracker::Load() { | |
95 if (total_time_.is_zero()) { | |
96 return 0; | |
97 } | |
98 return total_runtime_.InSecondsF() / total_time_.InSecondsF(); | |
99 } | |
100 | |
101 } // namespace scheduler | |
102 } // namespace blink | |
OLD | NEW |