OLD | NEW |
---|---|
(Empty) | |
1 #include "platform/scheduler/base/thread_load_tracker.h" | |
2 | |
3 namespace blink { | |
4 namespace scheduler { | |
5 | |
6 namespace { | |
7 | |
8 const int kLoadReportingIntervalInMilliseconds = 1000; | |
alex clarke (OOO till 29th)
2016/08/22 16:13:13
nit: any particular reason for using different uni
altimin
2016/08/22 17:02:22
Initially it was 100, but I thought that it's a ba
| |
9 const int kWaitingPeriodBeforeReportingInSeconds = 10; | |
10 | |
11 } // namespace | |
12 | |
13 ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now, | |
14 const Callback& callback) | |
alex clarke (OOO till 29th)
2016/08/22 16:13:13
nit: indent looks wrong in several places.
altimin
2016/08/22 17:02:22
I've run "git cl format", but it seems to work str
| |
15 : current_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_(base::TimeDelta::FromMilliseconds( | |
22 kLoadReportingIntervalInMilliseconds)), | |
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, | |
53 TaskState task_state) { | |
54 if (current_time_ > now) { | |
55 return; | |
56 } | |
57 | |
58 if (thread_state_ == ThreadState::PAUSED) { | |
59 current_time_ = now; | |
60 next_reporting_time_ = now + reporting_interval_; | |
61 return; | |
62 } | |
63 | |
64 while (true) { | |
65 base::TimeTicks next_current_time = std::min(next_reporting_time_, now); | |
66 | |
67 base::TimeDelta delta = next_current_time - current_time_; | |
68 | |
69 if (thread_state_ == ThreadState::ACTIVE) { | |
70 total_time_ += delta; | |
71 if (task_state == TaskState::TASK_RUNNING) { | |
72 total_runtime_ += delta; | |
73 } | |
74 } | |
75 current_time_ = next_current_time; | |
76 | |
77 if (current_time_ == next_reporting_time_) { | |
78 if (thread_state_ == ThreadState::ACTIVE && total_time_ >= waiting_period_ ) { | |
79 callback_.Run(current_time_, Load()); | |
80 } | |
81 next_reporting_time_ += reporting_interval_; | |
82 } else { | |
83 break; | |
84 } | |
85 } | |
86 } | |
87 | |
88 double ThreadLoadTracker::Load() { | |
89 if (total_time_.is_zero()) { | |
90 return 0; | |
91 } | |
92 return total_runtime_.InSecondsF() / total_time_.InSecondsF(); | |
93 } | |
94 | |
95 } // namespace scheduler | |
96 } // namespace blink | |
OLD | NEW |