- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / cpumonitor.h
1 /*
2  * libjingle
3  * Copyright 2010 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27
28 #ifndef TALK_BASE_CPUMONITOR_H_
29 #define TALK_BASE_CPUMONITOR_H_
30
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)
38
39 namespace talk_base {
40 class Thread;
41 class SystemInfo;
42
43 struct CpuStats {
44   CpuStats()
45       : prev_total_times_(0),
46         prev_cpu_times_(0),
47         prev_load_(0.f),
48         prev_load_time_(0u) {
49   }
50
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.
55 };
56
57 // CpuSampler samples the process and system load.
58 class CpuSampler {
59  public:
60   CpuSampler();
61   ~CpuSampler();
62
63   // Initialize CpuSampler.  Returns true if successful.
64   bool Init();
65
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);
69
70   // Return CPU load of current process as a float from 0 to 1.
71   float GetProcessLoad();
72
73   // Return CPU load of current process as a float from 0 to 1.
74   float GetSystemLoad();
75
76   // Return number of cpus. Includes hyperthreads.
77   int GetMaxCpus() const;
78
79   // Return current number of cpus available to this process.
80   int GetCurrentCpus();
81
82   // For testing. Allows forcing of fallback to using NTDLL functions.
83   void set_force_fallback(bool fallback) {
84 #ifdef WIN32
85     force_fallback_ = fallback;
86 #endif
87   }
88
89  private:
90   float UpdateCpuLoad(uint64 current_total_times,
91                       uint64 current_cpu_times,
92                       uint64 *prev_total_times,
93                       uint64 *prev_cpu_times);
94   CpuStats process_;
95   CpuStats system_;
96   int cpus_;
97   int min_load_interval_;  // Minimum time between computing new load.
98   scoped_ptr<SystemInfo> sysinfo_;
99 #ifdef WIN32
100   void* get_system_times_;
101   void* nt_query_system_information_;
102   bool force_fallback_;
103 #endif
104 #if defined(LINUX) || defined(ANDROID)
105   // File for reading /proc/stat
106   scoped_ptr<FileStream> sfile_;
107 #endif // defined(LINUX) || defined(ANDROID)
108 };
109
110 // CpuMonitor samples and signals the CPU load periodically.
111 class CpuMonitor
112     : public talk_base::MessageHandler, public sigslot::has_slots<> {
113  public:
114   explicit CpuMonitor(Thread* thread);
115   virtual ~CpuMonitor();
116   void set_thread(Thread* thread);
117
118   bool Start(int period_ms);
119   void Stop();
120   // Signal parameters are current cpus, max cpus, process load and system load.
121   sigslot::signal4<int, int, float, float> SignalUpdate;
122
123  protected:
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; }
129
130  private:
131   Thread* monitor_thread_;
132   CpuSampler sampler_;
133   int period_ms_;
134
135   DISALLOW_COPY_AND_ASSIGN(CpuMonitor);
136 };
137
138 }  // namespace talk_base
139
140 #endif  // TALK_BASE_CPUMONITOR_H_