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

Side by Side Diff: tools/win/IdleWakeups/idle_wakeups.cpp

Issue 2356753004: IdleWakeups tool (Closed)
Patch Set: Removed TODO from stdafx.cpp and added myself to OWNERS file. Created 4 years, 2 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
« no previous file with comments | « tools/win/IdleWakeups/ReadMe.txt ('k') | tools/win/IdleWakeups/power_sampler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
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 "stdafx.h"
6
7 #include <algorithm>
8 #include <map>
9 #include <vector>
10
11 #include "power_sampler.h"
12 #include "system_information_sampler.h"
13
14 // Result data structure contains a final set of values calculated based on
15 // comparison of two snapshots. These are the values that the tool prints
16 // in the output.
17 struct Result {
18 ULONG idle_wakeups_per_sec;
19 double cpu_usage;
20 ULONGLONG working_set;
21 double power;
22 };
23
24 typedef std::vector<Result> ResultVector;
25
26 // The following 4 functions are used for sorting of ResultVector.
27 ULONG GetIdleWakeupsPerSec(const Result& r) {
28 return r.idle_wakeups_per_sec;
29 }
30 double GetCpuUsage(const Result& r) {
31 return r.cpu_usage;
32 }
33 ULONGLONG GetWorkingSet(const Result& r) {
34 return r.working_set;
35 }
36 double GetPower(const Result& r) {
37 return r.power;
38 }
39
40 template <typename T>
41 T GetMedian(ResultVector* results, T (*getter)(const Result&)) {
42 std::sort(results->begin(), results->end(),
43 [&](const Result& lhs, const Result& rhs) {
44 return getter(lhs) < getter(rhs);
45 });
46
47 size_t median_index = results->size() / 2;
48 if (results->size() % 2 != 0) {
49 return getter((*results)[median_index]);
50 } else {
51 return (getter((*results)[median_index - 1]) +
52 getter((*results)[median_index])) /
53 2;
54 }
55 }
56
57 // This class holds the app state and constains a number of utilities for
58 // collecting and diffing snapshots of data, handling processes, etc.
59 class IdleWakeups {
60 public:
61 IdleWakeups();
62 ~IdleWakeups();
63
64 Result DiffSnapshots(const ProcessDataSnapshot& prev_snapshot,
65 const ProcessDataSnapshot& snapshot);
66
67 void OpenProcesses(const ProcessDataSnapshot& snapshot);
68 void CloseProcesses();
69
70 private:
71 HANDLE GetProcessHandle(ProcessId process_id);
72 void OpenProcess(ProcessId process_id);
73 void CloseProcess(ProcessId process_id);
74 bool GetFinishedProcessCpuTime(ProcessId process_id, ULONGLONG* cpu_usage);
75
76 static ULONG CountContextSwitches(const ProcessData& process_data);
77 static ULONG DiffContextSwitches(const ProcessData& prev_process_data,
78 const ProcessData& process_data);
79
80 std::map<ProcessId, HANDLE> process_id_to_hanle_map;
81
82 IdleWakeups& operator=(const IdleWakeups&) = delete;
83 IdleWakeups(const IdleWakeups&) = delete;
84 };
85
86 IdleWakeups::IdleWakeups() {}
87
88 IdleWakeups::~IdleWakeups() {
89 CloseProcesses();
90 }
91
92 void IdleWakeups::OpenProcesses(const ProcessDataSnapshot& snapshot) {
93 for (auto& pair : snapshot.processes) {
94 OpenProcess(pair.first);
95 }
96 }
97
98 void IdleWakeups::CloseProcesses() {
99 for (auto& pair : process_id_to_hanle_map) {
100 CloseHandle(pair.second);
101 }
102 process_id_to_hanle_map.clear();
103 }
104
105 HANDLE IdleWakeups::GetProcessHandle(ProcessId process_id) {
106 return process_id_to_hanle_map[process_id];
107 }
108
109 void IdleWakeups::OpenProcess(ProcessId process_id) {
110 process_id_to_hanle_map[process_id] = ::OpenProcess(
111 PROCESS_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)(ULONGLONG)process_id);
112 }
113
114 void IdleWakeups::CloseProcess(ProcessId process_id) {
115 HANDLE handle = GetProcessHandle(process_id);
116 CloseHandle(handle);
117 process_id_to_hanle_map.erase(process_id);
118 }
119
120 ULONG IdleWakeups::CountContextSwitches(const ProcessData& process_data) {
121 ULONG context_switches = 0;
122
123 for (const auto& thread_data : process_data.threads) {
124 context_switches += thread_data.context_switches;
125 }
126
127 return context_switches;
128 }
129
130 ULONG IdleWakeups::DiffContextSwitches(const ProcessData& prev_process_data,
131 const ProcessData& process_data) {
132 ULONG context_switches = 0;
133 size_t prev_index = 0;
134
135 for (const auto& thread_data : process_data.threads) {
136 ULONG prev_context_switches = 0;
137
138 for (; prev_index < prev_process_data.threads.size(); ++prev_index) {
139 const auto& prev_thread_data = prev_process_data.threads[prev_index];
140 if (prev_thread_data.thread_id == thread_data.thread_id) {
141 prev_context_switches = prev_thread_data.context_switches;
142 ++prev_index;
143 break;
144 }
145
146 if (prev_thread_data.thread_id > thread_data.thread_id)
147 break;
148 }
149
150 context_switches += thread_data.context_switches - prev_context_switches;
151 }
152
153 return context_switches;
154 }
155
156 bool IdleWakeups::GetFinishedProcessCpuTime(ProcessId process_id,
157 ULONGLONG* cpu_time) {
158 HANDLE process_handle = GetProcessHandle(process_id);
159
160 FILETIME creation_time, exit_time, kernel_time, user_time;
161 if (GetProcessTimes(process_handle, &creation_time, &exit_time, &kernel_time,
162 &user_time)) {
163 ULARGE_INTEGER ul_kernel_time, ul_user_time;
164 ul_kernel_time.LowPart = kernel_time.dwLowDateTime;
165 ul_kernel_time.HighPart = kernel_time.dwHighDateTime;
166 ul_user_time.LowPart = user_time.dwLowDateTime;
167 ul_user_time.HighPart = user_time.dwHighDateTime;
168 *cpu_time = ul_kernel_time.QuadPart + ul_user_time.QuadPart;
169 return true;
170 }
171
172 *cpu_time = 0;
173 return false;
174 }
175
176 Result IdleWakeups::DiffSnapshots(const ProcessDataSnapshot& prev_snapshot,
177 const ProcessDataSnapshot& snapshot) {
178 ULONG idle_wakeups_delta = 0;
179 ULONGLONG cpu_usage_delta = 0;
180 ULONGLONG total_working_set = 0;
181
182 ProcessDataMap::const_iterator prev_it = prev_snapshot.processes.begin();
183
184 for (const auto& it : snapshot.processes) {
185 ProcessId process_id = it.first;
186 const ProcessData& process_data = it.second;
187 const ProcessData* prev_process_data_to_diff = nullptr;
188 ULONGLONG prev_process_cpu_time = 0;
189
190 for (; prev_it != prev_snapshot.processes.end(); ++prev_it) {
191 ProcessId prev_process_id = prev_it->first;
192 const ProcessData& prev_process_data = prev_it->second;
193
194 if (prev_process_id == process_id) {
195 prev_process_data_to_diff = &prev_process_data;
196 prev_process_cpu_time = prev_process_data.cpu_time;
197 ++prev_it;
198 break;
199 }
200
201 if (prev_process_id > process_id)
202 break;
203
204 // Prev process disappeared.
205 ULONGLONG last_known_cpu_time;
206 if (GetFinishedProcessCpuTime(prev_process_id, &last_known_cpu_time)) {
207 cpu_usage_delta += last_known_cpu_time - prev_process_data.cpu_time;
208 }
209 CloseProcess(prev_process_id);
210 }
211
212 if (prev_process_data_to_diff) {
213 idle_wakeups_delta +=
214 DiffContextSwitches(*prev_process_data_to_diff, process_data);
215 } else {
216 // New process that we haven't seen before.
217 OpenProcess(process_id);
218 idle_wakeups_delta += CountContextSwitches(process_data);
219 }
220
221 cpu_usage_delta += process_data.cpu_time - prev_process_cpu_time;
222 total_working_set += process_data.working_set / 1024;
223 }
224
225 double time_delta = snapshot.timestamp - prev_snapshot.timestamp;
226 Result result;
227 result.idle_wakeups_per_sec =
228 static_cast<ULONG>(idle_wakeups_delta / time_delta);
229 // brucedawson: Don't divide by number of processors so that all numbers are
230 // percentage of a core
231 // result.cpu_usage = (double)cpu_usage_delta * 100 / (time_delta * 10000000 *
232 // NumberOfprocessors());
233 result.cpu_usage = (double)cpu_usage_delta * 100 / (time_delta * 10000000);
234 result.working_set = total_working_set;
235
236 return result;
237 }
238
239 HANDLE ctrl_c_pressed = NULL;
240
241 BOOL WINAPI HandlerFunction(DWORD ctrl_type) {
242 if (ctrl_type == CTRL_C_EVENT) {
243 printf("Ctrl+C pressed...\n");
244 SetEvent(ctrl_c_pressed);
245 return TRUE;
246 }
247
248 return FALSE;
249 }
250
251 const DWORD sleep_time_sec = 2;
252
253 void PrintHeader() {
254 printf(
255 "------------------------------------------------------------------------"
256 "----------\n");
257 printf(
258 " Context switches/sec CPU usage Working set "
259 " Power\n");
260 printf(
261 "------------------------------------------------------------------------"
262 "----------\n");
263 }
264
265 #define RESULT_FORMAT_STRING " %20lu %8.2f%c %6.2f MiB %4.2f W\n"
266
267 int wmain(int argc, wchar_t* argv[]) {
268 ctrl_c_pressed = CreateEvent(NULL, FALSE, FALSE, NULL);
269 SetConsoleCtrlHandler(HandlerFunction, TRUE);
270
271 PowerSampler power_sampler;
272 SystemInformationSampler system_information_sampler(argc > 1 ? argv[1]
273 : L"chrome.exe");
274 IdleWakeups the_app;
275
276 // Take the initial snapshot.
277 std::unique_ptr<ProcessDataSnapshot> previous_snapshot =
278 system_information_sampler.TakeSnapshot();
279
280 the_app.OpenProcesses(*previous_snapshot);
281
282 ULONG cumulative_idle_wakeups_per_sec = 0;
283 double cumulative_cpu_usage = 0.0;
284 ULONGLONG cumulative_working_set = 0;
285 double cumulative_energy = 0.0;
286
287 ResultVector results;
288
289 printf("Capturing perf data for all processes matching %ls\n",
290 system_information_sampler.target_process_name_filter());
291
292 PrintHeader();
293
294 for (;;) {
295 if (WaitForSingleObject(ctrl_c_pressed, sleep_time_sec * 1000) ==
296 WAIT_OBJECT_0)
297 break;
298
299 std::unique_ptr<ProcessDataSnapshot> snapshot =
300 system_information_sampler.TakeSnapshot();
301 size_t number_of_processes = snapshot->processes.size();
302
303 Result result = the_app.DiffSnapshots(*previous_snapshot, *snapshot);
304 previous_snapshot = std::move(snapshot);
305
306 power_sampler.SampleCPUPowerState();
307 result.power = power_sampler.get_power(L"Processor");
308
309 printf("%9u processes" RESULT_FORMAT_STRING, (DWORD)number_of_processes,
310 result.idle_wakeups_per_sec, result.cpu_usage, '%',
311 result.working_set / 1024.0, result.power);
312
313 cumulative_idle_wakeups_per_sec += result.idle_wakeups_per_sec;
314 cumulative_cpu_usage += result.cpu_usage;
315 cumulative_working_set += result.working_set;
316 cumulative_energy += result.power;
317
318 results.push_back(result);
319 }
320
321 CloseHandle(ctrl_c_pressed);
322
323 ULONG sample_count = (ULONG)results.size();
324 if (sample_count == 0)
325 return 0;
326
327 PrintHeader();
328
329 printf(" Average" RESULT_FORMAT_STRING,
330 cumulative_idle_wakeups_per_sec / sample_count,
331 cumulative_cpu_usage / sample_count, '%',
332 (cumulative_working_set / 1024.0) / sample_count,
333 cumulative_energy / sample_count);
334
335 Result median_result;
336
337 median_result.idle_wakeups_per_sec =
338 GetMedian<ULONG>(&results, GetIdleWakeupsPerSec);
339 median_result.cpu_usage = GetMedian<double>(&results, GetCpuUsage);
340 median_result.working_set = GetMedian<ULONGLONG>(&results, GetWorkingSet);
341 median_result.power = GetMedian<double>(&results, GetPower);
342
343 printf(" Median" RESULT_FORMAT_STRING,
344 median_result.idle_wakeups_per_sec, median_result.cpu_usage, '%',
345 median_result.working_set / 1024.0, median_result.power);
346
347 return 0;
348 }
OLDNEW
« no previous file with comments | « tools/win/IdleWakeups/ReadMe.txt ('k') | tools/win/IdleWakeups/power_sampler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698