• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef WEBRTC_BASE_CPUMONITOR_H_
12 #define WEBRTC_BASE_CPUMONITOR_H_
13 
14 #include "webrtc/base/basictypes.h"
15 #include "webrtc/base/messagehandler.h"
16 #include "webrtc/base/scoped_ptr.h"
17 #include "webrtc/base/sigslot.h"
18 #if defined(WEBRTC_LINUX)
19 #include "webrtc/base/stream.h"
20 #endif // defined(WEBRTC_LINUX)
21 
22 namespace rtc {
23 class Thread;
24 class SystemInfo;
25 
26 struct CpuStats {
CpuStatsCpuStats27   CpuStats()
28       : prev_total_times_(0),
29         prev_cpu_times_(0),
30         prev_load_(0.f),
31         prev_load_time_(0u) {
32   }
33 
34   uint64 prev_total_times_;
35   uint64 prev_cpu_times_;
36   float prev_load_;  // Previous load value.
37   uint32 prev_load_time_;  // Time previous load value was taken.
38 };
39 
40 // CpuSampler samples the process and system load.
41 class CpuSampler {
42  public:
43   CpuSampler();
44   ~CpuSampler();
45 
46   // Initialize CpuSampler.  Returns true if successful.
47   bool Init();
48 
49   // Set minimum interval in ms between computing new load values.
50   // Default 950 ms.  Set to 0 to disable interval.
51   void set_load_interval(int min_load_interval);
52 
53   // Return CPU load of current process as a float from 0 to 1.
54   float GetProcessLoad();
55 
56   // Return CPU load of current process as a float from 0 to 1.
57   float GetSystemLoad();
58 
59   // Return number of cpus. Includes hyperthreads.
60   int GetMaxCpus() const;
61 
62   // Return current number of cpus available to this process.
63   int GetCurrentCpus();
64 
65   // For testing. Allows forcing of fallback to using NTDLL functions.
set_force_fallback(bool fallback)66   void set_force_fallback(bool fallback) {
67 #if defined(WEBRTC_WIN)
68     force_fallback_ = fallback;
69 #endif
70   }
71 
72  private:
73   float UpdateCpuLoad(uint64 current_total_times,
74                       uint64 current_cpu_times,
75                       uint64 *prev_total_times,
76                       uint64 *prev_cpu_times);
77   CpuStats process_;
78   CpuStats system_;
79   int cpus_;
80   int min_load_interval_;  // Minimum time between computing new load.
81   scoped_ptr<SystemInfo> sysinfo_;
82 #if defined(WEBRTC_WIN)
83   void* get_system_times_;
84   void* nt_query_system_information_;
85   bool force_fallback_;
86 #endif
87 #if defined(WEBRTC_LINUX)
88   // File for reading /proc/stat
89   scoped_ptr<FileStream> sfile_;
90 #endif // defined(WEBRTC_LINUX)
91 };
92 
93 // CpuMonitor samples and signals the CPU load periodically.
94 class CpuMonitor
95     : public rtc::MessageHandler, public sigslot::has_slots<> {
96  public:
97   explicit CpuMonitor(Thread* thread);
98   virtual ~CpuMonitor();
99   void set_thread(Thread* thread);
100 
101   bool Start(int period_ms);
102   void Stop();
103   // Signal parameters are current cpus, max cpus, process load and system load.
104   sigslot::signal4<int, int, float, float> SignalUpdate;
105 
106  protected:
107   // Override virtual method of parent MessageHandler.
108   virtual void OnMessage(rtc::Message* msg);
109   // Clear the monitor thread and stop sending it messages if the thread goes
110   // away before our lifetime.
OnMessageQueueDestroyed()111   void OnMessageQueueDestroyed() { monitor_thread_ = NULL; }
112 
113  private:
114   Thread* monitor_thread_;
115   CpuSampler sampler_;
116   int period_ms_;
117 
118   DISALLOW_COPY_AND_ASSIGN(CpuMonitor);
119 };
120 
121 }  // namespace rtc
122 
123 #endif  // WEBRTC_BASE_CPUMONITOR_H_
124