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