Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(884)

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/base/thread_load_tracker.cc

Issue 2265873004: [scheduler] Monitor renderer load level. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added some comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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;
9 const int kWaitingPeriodBeforeReportingInSeconds = 10;
10
11 } // namespace
12
13 ThreadLoadTracker::ThreadLoadTracker(base::TimeTicks now,
14 const Callback& callback)
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 // This function advances |current_time_| to now and calls |callback_|
55 // when appropriate.
56 if (current_time_ > now) {
57 return;
58 }
59
60 if (thread_state_ == ThreadState::PAUSED) {
61 // If the load tracker is paused, bail out early.
62 current_time_ = now;
63 next_reporting_time_ = now + reporting_interval_;
64 return;
65 }
66
67 while (true) {
Sami 2016/08/22 16:35:40 nit: while (current_time < now) would make this lo
altimin 2016/08/22 17:02:23 Done.
68 // Forward current_time_ to the earliest of following:
69 // a) time to call |callback_|
70 // b) requested time to forward (|now|).
71 base::TimeTicks next_current_time = std::min(next_reporting_time_, now);
72
73 base::TimeDelta delta = next_current_time - current_time_;
74
75 // Forward time and recalculate |total_time_| and |total_runtime_|.
76 if (thread_state_ == ThreadState::ACTIVE) {
77 total_time_ += delta;
78 if (task_state == TaskState::TASK_RUNNING) {
79 total_runtime_ += delta;
80 }
81 }
82 current_time_ = next_current_time;
83
84 if (current_time_ == next_reporting_time_) {
85 // Call |callback_| if need and update next callback time.
86 if (thread_state_ == ThreadState::ACTIVE && total_time_ >= waiting_period_ ) {
87 callback_.Run(current_time_, Load());
88 }
89 next_reporting_time_ += reporting_interval_;
90 } else {
91 // current_time_ == now, and it's time to finish up.
92 break;
93 }
94 }
95 }
96
97 double ThreadLoadTracker::Load() {
98 if (total_time_.is_zero()) {
99 return 0;
100 }
101 return total_runtime_.InSecondsF() / total_time_.InSecondsF();
102 }
103
104 } // namespace scheduler
105 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698