You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.4 KiB

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