blob: f5c0d063a6a8bfc49ef689b435ed11214875324c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2010 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/base/cpumonitor.h"
12
13#include <string>
14
15#include "webrtc/base/common.h"
16#include "webrtc/base/logging.h"
17#include "webrtc/base/scoped_ptr.h"
18#include "webrtc/base/systeminfo.h"
19#include "webrtc/base/thread.h"
20#include "webrtc/base/timeutils.h"
21
22#if defined(WEBRTC_WIN)
23#include "webrtc/base/win32.h"
24#include <winternl.h>
25#endif
26
27#if defined(WEBRTC_POSIX)
28#include <sys/time.h>
29#endif
30
31#if defined(WEBRTC_MAC)
32#include <mach/mach_host.h>
33#include <mach/mach_init.h>
34#include <mach/host_info.h>
35#include <mach/task.h>
36#endif // defined(WEBRTC_MAC)
37
38#if defined(WEBRTC_LINUX)
39#include <sys/resource.h>
40#include <errno.h>
41#include <stdio.h>
42#include "webrtc/base/fileutils.h"
43#include "webrtc/base/pathutils.h"
44#endif // defined(WEBRTC_LINUX)
45
46#if defined(WEBRTC_MAC)
47static uint64 TimeValueTToInt64(const time_value_t &time_value) {
48 return rtc::kNumMicrosecsPerSec * time_value.seconds +
49 time_value.microseconds;
50}
51#endif // defined(WEBRTC_MAC)
52
53// How CpuSampler works
54// When threads switch, the time they spent is accumulated to system counters.
55// The time can be treated as user, kernel or idle.
56// user time is applications.
57// kernel time is the OS, including the thread switching code itself.
58// typically kernel time indicates IO.
59// idle time is a process that wastes time when nothing is ready to run.
60//
61// User time is broken down by process (application). One of the applications
62// is the current process. When you add up all application times, this is
63// system time. If only your application is running, system time should be the
64// same as process time.
65//
66// All cores contribute to these accumulators. A dual core process is able to
67// process twice as many cycles as a single core. The actual code efficiency
68// may be worse, due to contention, but the available cycles is exactly twice
69// as many, and the cpu load will reflect the efficiency. Hyperthreads behave
70// the same way. The load will reflect 200%, but the actual amount of work
71// completed will be much less than a true dual core.
72//
73// Total available performance is the sum of all accumulators.
74// If you tracked this for 1 second, it would essentially give you the clock
75// rate - number of cycles per second.
76// Speed step / Turbo Boost is not considered, so infact more processing time
77// may be available.
78
79namespace rtc {
80
81// Note Tests on Windows show 600 ms is minimum stable interval for Windows 7.
82static const int32 kDefaultInterval = 950; // Slightly under 1 second.
83
84CpuSampler::CpuSampler()
85 : min_load_interval_(kDefaultInterval)
86#if defined(WEBRTC_WIN)
87 , get_system_times_(NULL),
88 nt_query_system_information_(NULL),
89 force_fallback_(false)
90#endif
91 {
92}
93
94CpuSampler::~CpuSampler() {
95}
96
97// Set minimum interval in ms between computing new load values. Default 950.
98void CpuSampler::set_load_interval(int min_load_interval) {
99 min_load_interval_ = min_load_interval;
100}
101
102bool CpuSampler::Init() {
103 sysinfo_.reset(new SystemInfo);
104 cpus_ = sysinfo_->GetMaxCpus();
105 if (cpus_ == 0) {
106 return false;
107 }
108#if defined(WEBRTC_WIN)
109 // Note that GetSystemTimes is available in Windows XP SP1 or later.
110 // http://msdn.microsoft.com/en-us/library/ms724400.aspx
111 // NtQuerySystemInformation is used as a fallback.
112 if (!force_fallback_) {
113 get_system_times_ = GetProcAddress(GetModuleHandle(L"kernel32.dll"),
114 "GetSystemTimes");
115 }
116 nt_query_system_information_ = GetProcAddress(GetModuleHandle(L"ntdll.dll"),
117 "NtQuerySystemInformation");
118 if ((get_system_times_ == NULL) && (nt_query_system_information_ == NULL)) {
119 return false;
120 }
121#endif
122#if defined(WEBRTC_LINUX)
123 Pathname sname("/proc/stat");
124 sfile_.reset(Filesystem::OpenFile(sname, "rb"));
125 if (!sfile_) {
126 LOG_ERR(LS_ERROR) << "open proc/stat failed:";
127 return false;
128 }
129 if (!sfile_->DisableBuffering()) {
130 LOG_ERR(LS_ERROR) << "could not disable buffering for proc/stat";
131 return false;
132 }
133#endif // defined(WEBRTC_LINUX)
134 GetProcessLoad(); // Initialize values.
135 GetSystemLoad();
136 // Help next user call return valid data by recomputing load.
137 process_.prev_load_time_ = 0u;
138 system_.prev_load_time_ = 0u;
139 return true;
140}
141
142float CpuSampler::UpdateCpuLoad(uint64 current_total_times,
143 uint64 current_cpu_times,
144 uint64 *prev_total_times,
145 uint64 *prev_cpu_times) {
146 float result = 0.f;
147 if (current_total_times < *prev_total_times ||
148 current_cpu_times < *prev_cpu_times) {
149 LOG(LS_ERROR) << "Inconsistent time values are passed. ignored";
150 } else {
151 const uint64 cpu_diff = current_cpu_times - *prev_cpu_times;
152 const uint64 total_diff = current_total_times - *prev_total_times;
153 result = (total_diff == 0ULL ? 0.f :
154 static_cast<float>(1.0f * cpu_diff / total_diff));
155 if (result > static_cast<float>(cpus_)) {
156 result = static_cast<float>(cpus_);
157 }
158 *prev_total_times = current_total_times;
159 *prev_cpu_times = current_cpu_times;
160 }
161 return result;
162}
163
164float CpuSampler::GetSystemLoad() {
165 uint32 timenow = Time();
166 int elapsed = static_cast<int>(TimeDiff(timenow, system_.prev_load_time_));
167 if (min_load_interval_ != 0 && system_.prev_load_time_ != 0u &&
168 elapsed < min_load_interval_) {
169 return system_.prev_load_;
170 }
171#if defined(WEBRTC_WIN)
172 uint64 total_times, cpu_times;
173
174 typedef BOOL (_stdcall *GST_PROC)(LPFILETIME, LPFILETIME, LPFILETIME);
175 typedef NTSTATUS (WINAPI *QSI_PROC)(SYSTEM_INFORMATION_CLASS,
176 PVOID, ULONG, PULONG);
177
178 GST_PROC get_system_times = reinterpret_cast<GST_PROC>(get_system_times_);
179 QSI_PROC nt_query_system_information = reinterpret_cast<QSI_PROC>(
180 nt_query_system_information_);
181
182 if (get_system_times) {
183 FILETIME idle_time, kernel_time, user_time;
184 if (!get_system_times(&idle_time, &kernel_time, &user_time)) {
185 LOG(LS_ERROR) << "::GetSystemTimes() failed: " << ::GetLastError();
186 return 0.f;
187 }
188 // kernel_time includes Kernel idle time, so no need to
189 // include cpu_time as total_times
190 total_times = ToUInt64(kernel_time) + ToUInt64(user_time);
191 cpu_times = total_times - ToUInt64(idle_time);
192
193 } else {
194 if (nt_query_system_information) {
195 ULONG returned_length = 0;
196 scoped_ptr<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[]> processor_info(
197 new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[cpus_]);
198 nt_query_system_information(
199 ::SystemProcessorPerformanceInformation,
200 reinterpret_cast<void*>(processor_info.get()),
201 cpus_ * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION),
202 &returned_length);
203
204 if (returned_length !=
205 (cpus_ * sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION))) {
206 LOG(LS_ERROR) << "NtQuerySystemInformation has unexpected size";
207 return 0.f;
208 }
209
210 uint64 current_idle = 0;
211 uint64 current_kernel = 0;
212 uint64 current_user = 0;
213 for (int ix = 0; ix < cpus_; ++ix) {
214 current_idle += processor_info[ix].IdleTime.QuadPart;
215 current_kernel += processor_info[ix].UserTime.QuadPart;
216 current_user += processor_info[ix].KernelTime.QuadPart;
217 }
218 total_times = current_kernel + current_user;
219 cpu_times = total_times - current_idle;
220 } else {
221 return 0.f;
222 }
223 }
224#endif // WEBRTC_WIN
225
226#if defined(WEBRTC_MAC)
227 host_cpu_load_info_data_t cpu_info;
228 mach_msg_type_number_t info_count = HOST_CPU_LOAD_INFO_COUNT;
229 if (KERN_SUCCESS != host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO,
230 reinterpret_cast<host_info_t>(&cpu_info),
231 &info_count)) {
232 LOG(LS_ERROR) << "::host_statistics() failed";
233 return 0.f;
234 }
235
236 const uint64 cpu_times = cpu_info.cpu_ticks[CPU_STATE_NICE] +
237 cpu_info.cpu_ticks[CPU_STATE_SYSTEM] +
238 cpu_info.cpu_ticks[CPU_STATE_USER];
239 const uint64 total_times = cpu_times + cpu_info.cpu_ticks[CPU_STATE_IDLE];
240#endif // defined(WEBRTC_MAC)
241
242#if defined(WEBRTC_LINUX)
243 if (!sfile_) {
244 LOG(LS_ERROR) << "Invalid handle for proc/stat";
245 return 0.f;
246 }
247 std::string statbuf;
248 sfile_->SetPosition(0);
249 if (!sfile_->ReadLine(&statbuf)) {
250 LOG_ERR(LS_ERROR) << "Could not read proc/stat file";
251 return 0.f;
252 }
253
254 unsigned long long user;
255 unsigned long long nice;
256 unsigned long long system;
257 unsigned long long idle;
258 if (sscanf(statbuf.c_str(), "cpu %Lu %Lu %Lu %Lu",
259 &user, &nice,
260 &system, &idle) != 4) {
261 LOG_ERR(LS_ERROR) << "Could not parse cpu info";
262 return 0.f;
263 }
264 const uint64 cpu_times = nice + system + user;
265 const uint64 total_times = cpu_times + idle;
266#endif // defined(WEBRTC_LINUX)
267
268#if defined(__native_client__)
269 // TODO(ryanpetrie): Implement this via PPAPI when it's available.
270 const uint64 cpu_times = 0;
271 const uint64 total_times = 0;
272#endif // defined(__native_client__)
273
274 system_.prev_load_time_ = timenow;
275 system_.prev_load_ = UpdateCpuLoad(total_times,
276 cpu_times * cpus_,
277 &system_.prev_total_times_,
278 &system_.prev_cpu_times_);
279 return system_.prev_load_;
280}
281
282float CpuSampler::GetProcessLoad() {
283 uint32 timenow = Time();
284 int elapsed = static_cast<int>(TimeDiff(timenow, process_.prev_load_time_));
285 if (min_load_interval_ != 0 && process_.prev_load_time_ != 0u &&
286 elapsed < min_load_interval_) {
287 return process_.prev_load_;
288 }
289#if defined(WEBRTC_WIN)
290 FILETIME current_file_time;
291 ::GetSystemTimeAsFileTime(&current_file_time);
292
293 FILETIME create_time, exit_time, kernel_time, user_time;
294 if (!::GetProcessTimes(::GetCurrentProcess(),
295 &create_time, &exit_time, &kernel_time, &user_time)) {
296 LOG(LS_ERROR) << "::GetProcessTimes() failed: " << ::GetLastError();
297 return 0.f;
298 }
299
300 const uint64 total_times =
301 ToUInt64(current_file_time) - ToUInt64(create_time);
302 const uint64 cpu_times =
303 (ToUInt64(kernel_time) + ToUInt64(user_time));
304#endif // WEBRTC_WIN
305
306#if defined(WEBRTC_POSIX)
307 // Common to both OSX and Linux.
308 struct timeval tv;
309 gettimeofday(&tv, NULL);
310 const uint64 total_times = tv.tv_sec * kNumMicrosecsPerSec + tv.tv_usec;
311#endif
312
313#if defined(WEBRTC_MAC)
314 // Get live thread usage.
315 task_thread_times_info task_times_info;
316 mach_msg_type_number_t info_count = TASK_THREAD_TIMES_INFO_COUNT;
317
318 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_THREAD_TIMES_INFO,
319 reinterpret_cast<task_info_t>(&task_times_info),
320 &info_count)) {
321 LOG(LS_ERROR) << "::task_info(TASK_THREAD_TIMES_INFO) failed";
322 return 0.f;
323 }
324
325 // Get terminated thread usage.
326 task_basic_info task_term_info;
327 info_count = TASK_BASIC_INFO_COUNT;
328 if (KERN_SUCCESS != task_info(mach_task_self(), TASK_BASIC_INFO,
329 reinterpret_cast<task_info_t>(&task_term_info),
330 &info_count)) {
331 LOG(LS_ERROR) << "::task_info(TASK_BASIC_INFO) failed";
332 return 0.f;
333 }
334
335 const uint64 cpu_times = (TimeValueTToInt64(task_times_info.user_time) +
336 TimeValueTToInt64(task_times_info.system_time) +
337 TimeValueTToInt64(task_term_info.user_time) +
338 TimeValueTToInt64(task_term_info.system_time));
339#endif // defined(WEBRTC_MAC)
340
341#if defined(WEBRTC_LINUX)
342 rusage usage;
343 if (getrusage(RUSAGE_SELF, &usage) < 0) {
344 LOG_ERR(LS_ERROR) << "getrusage failed";
345 return 0.f;
346 }
347
348 const uint64 cpu_times =
349 (usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) * kNumMicrosecsPerSec +
350 usage.ru_utime.tv_usec + usage.ru_stime.tv_usec;
351#endif // defined(WEBRTC_LINUX)
352
353#if defined(__native_client__)
354 // TODO(ryanpetrie): Implement this via PPAPI when it's available.
355 const uint64 cpu_times = 0;
356#endif // defined(__native_client__)
357
358 process_.prev_load_time_ = timenow;
359 process_.prev_load_ = UpdateCpuLoad(total_times,
360 cpu_times,
361 &process_.prev_total_times_,
362 &process_.prev_cpu_times_);
363 return process_.prev_load_;
364}
365
366int CpuSampler::GetMaxCpus() const {
367 return cpus_;
368}
369
370int CpuSampler::GetCurrentCpus() {
371 return sysinfo_->GetCurCpus();
372}
373
374///////////////////////////////////////////////////////////////////
375// Implementation of class CpuMonitor.
376CpuMonitor::CpuMonitor(Thread* thread)
377 : monitor_thread_(thread) {
378}
379
380CpuMonitor::~CpuMonitor() {
381 Stop();
382}
383
384void CpuMonitor::set_thread(Thread* thread) {
385 ASSERT(monitor_thread_ == NULL || monitor_thread_ == thread);
386 monitor_thread_ = thread;
387}
388
389bool CpuMonitor::Start(int period_ms) {
390 if (!monitor_thread_ || !sampler_.Init()) return false;
391
392 monitor_thread_->SignalQueueDestroyed.connect(
393 this, &CpuMonitor::OnMessageQueueDestroyed);
394
395 period_ms_ = period_ms;
396 monitor_thread_->PostDelayed(period_ms_, this);
397
398 return true;
399}
400
401void CpuMonitor::Stop() {
402 if (monitor_thread_) {
403 monitor_thread_->Clear(this);
404 }
405}
406
407void CpuMonitor::OnMessage(Message* msg) {
408 int max_cpus = sampler_.GetMaxCpus();
409 int current_cpus = sampler_.GetCurrentCpus();
410 float process_load = sampler_.GetProcessLoad();
411 float system_load = sampler_.GetSystemLoad();
412 SignalUpdate(current_cpus, max_cpus, process_load, system_load);
413
414 if (monitor_thread_) {
415 monitor_thread_->PostDelayed(period_ms_, this);
416 }
417}
418
419} // namespace rtc