3 * Copyright 2010 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_CPUMONITOR_H_
29 #define TALK_BASE_CPUMONITOR_H_
31 #include "talk/base/basictypes.h"
32 #include "talk/base/messagehandler.h"
33 #include "talk/base/scoped_ptr.h"
34 #include "talk/base/sigslot.h"
35 #if defined(LINUX) || defined(ANDROID)
36 #include "talk/base/stream.h"
37 #endif // defined(LINUX) || defined(ANDROID)
45 : prev_total_times_(0),
51 uint64 prev_total_times_;
52 uint64 prev_cpu_times_;
53 float prev_load_; // Previous load value.
54 uint32 prev_load_time_; // Time previous load value was taken.
57 // CpuSampler samples the process and system load.
63 // Initialize CpuSampler. Returns true if successful.
66 // Set minimum interval in ms between computing new load values.
67 // Default 950 ms. Set to 0 to disable interval.
68 void set_load_interval(int min_load_interval);
70 // Return CPU load of current process as a float from 0 to 1.
71 float GetProcessLoad();
73 // Return CPU load of current process as a float from 0 to 1.
74 float GetSystemLoad();
76 // Return number of cpus. Includes hyperthreads.
77 int GetMaxCpus() const;
79 // Return current number of cpus available to this process.
82 // For testing. Allows forcing of fallback to using NTDLL functions.
83 void set_force_fallback(bool fallback) {
85 force_fallback_ = fallback;
90 float UpdateCpuLoad(uint64 current_total_times,
91 uint64 current_cpu_times,
92 uint64 *prev_total_times,
93 uint64 *prev_cpu_times);
97 int min_load_interval_; // Minimum time between computing new load.
98 scoped_ptr<SystemInfo> sysinfo_;
100 void* get_system_times_;
101 void* nt_query_system_information_;
102 bool force_fallback_;
104 #if defined(LINUX) || defined(ANDROID)
105 // File for reading /proc/stat
106 scoped_ptr<FileStream> sfile_;
107 #endif // defined(LINUX) || defined(ANDROID)
110 // CpuMonitor samples and signals the CPU load periodically.
112 : public talk_base::MessageHandler, public sigslot::has_slots<> {
114 explicit CpuMonitor(Thread* thread);
115 virtual ~CpuMonitor();
116 void set_thread(Thread* thread);
118 bool Start(int period_ms);
120 // Signal parameters are current cpus, max cpus, process load and system load.
121 sigslot::signal4<int, int, float, float> SignalUpdate;
124 // Override virtual method of parent MessageHandler.
125 virtual void OnMessage(talk_base::Message* msg);
126 // Clear the monitor thread and stop sending it messages if the thread goes
127 // away before our lifetime.
128 void OnMessageQueueDestroyed() { monitor_thread_ = NULL; }
131 Thread* monitor_thread_;
135 DISALLOW_COPY_AND_ASSIGN(CpuMonitor);
138 } // namespace talk_base
140 #endif // TALK_BASE_CPUMONITOR_H_