Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 #ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 4 #ifndef CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
| 5 #define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 5 #define CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/system_info_provider.h" | 7 #include "chrome/browser/extensions/system_info_provider.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/timer.h" | |
| 11 #include "chrome/common/extensions/api/experimental_system_info_cpu.h" | 12 #include "chrome/common/extensions/api/experimental_system_info_cpu.h" |
| 12 | 13 |
| 13 namespace extensions { | 14 namespace extensions { |
| 14 | 15 |
| 15 class CpuInfoProvider | 16 class CpuInfoProvider |
| 16 : public SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo> { | 17 : public SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo> { |
| 17 public: | 18 public: |
| 18 typedef base::Callback<api::experimental_system_info_cpu::CpuUpdateInfo> | 19 typedef base::Callback< |
| 19 QueryCpuTimeCallback; | 20 void(scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo>)> |
| 20 virtual ~CpuInfoProvider() {} | 21 SamplingCallback; |
| 22 | |
| 23 virtual ~CpuInfoProvider(); | |
| 21 | 24 |
| 22 // Overriden from SystemInfoProvider<CpuInfo>. | 25 // Overriden from SystemInfoProvider<CpuInfo>. |
| 23 virtual bool QueryInfo( | 26 virtual bool QueryInfo( |
| 24 api::experimental_system_info_cpu::CpuInfo* info) OVERRIDE; | 27 api::experimental_system_info_cpu::CpuInfo* info) OVERRIDE; |
| 25 | 28 |
| 26 // Start sampling the CPU usage. The callback gets called when one sampling | 29 // Start sampling the CPU usage. The callback gets called when one sampling |
| 27 // cycle is completed periodically with the CPU updated usage info for each | 30 // cycle is completed periodically with the CPU updated usage info for each |
| 28 // processors. Return true if it succeeds to start, otherwise, false is | 31 // processors. Return true if it succeeds to start, otherwise, false is |
| 29 // returned. | 32 // returned. Can be called from any thread. |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
Allowing this to be called from any thread may mas
Hongbo Min
2012/09/16 14:07:49
Done.
| |
| 30 bool StartSampling(const QueryCpuTimeCallback& callback); | 33 bool StartSampling(const SamplingCallback& callback); |
| 31 | 34 |
| 32 // Stop the sampling cycle. Return true if it succeeds to stop. | 35 // Stop the sampling cycle. Return true if it succeeds to stop. Can be called |
| 36 // from any thread. | |
| 33 bool StopSampling(); | 37 bool StopSampling(); |
| 34 | 38 |
| 35 // Return the single shared instance of CpuInfoProvider. | 39 // Return the single shared instance of CpuInfoProvider. |
| 36 static CpuInfoProvider* Get(); | 40 static CpuInfoProvider* Get(); |
| 37 | 41 |
| 38 private: | 42 private: |
| 43 friend class SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo>; | |
| 44 friend class MockCpuInfoProviderImpl; | |
| 45 | |
| 39 // The amount of time that CPU spent on performing different kinds of work. | 46 // The amount of time that CPU spent on performing different kinds of work. |
| 40 // It is used to calculate the usage percent for processors. | 47 // It is used to calculate the usage percent for processors. |
| 41 struct CpuTime { | 48 class CpuTime { |
| 49 public: | |
| 50 CpuTime() : user(0), kernel(0), idle(0) {} | |
| 42 int64 user; // user mode. | 51 int64 user; // user mode. |
| 43 int64 kernel; // kernel mode. | 52 int64 kernel; // kernel mode. |
| 44 int64 idle; // twiddling thumbs. | 53 int64 idle; // twiddling thumbs. |
| 45 }; | 54 }; |
| 46 | 55 |
| 56 CpuInfoProvider(); | |
| 57 | |
| 47 // Platform specific implementation for querying the CPU time information | 58 // Platform specific implementation for querying the CPU time information |
| 48 // for each processor. Note that the first element is the total aggregated | 59 // for each processor. Note that the first element is the total aggregated |
| 49 // numbers of all logic processors. | 60 // numbers of all logic processors. |
| 50 bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); | 61 virtual bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); |
| 62 | |
| 63 // Called when the sampling timer is expired. | |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
"expired" doesn't seem right. "fired" perhaps?
Hongbo Min
2012/09/16 14:07:49
Done.
| |
| 64 void DoStartSampling(); | |
| 65 | |
| 66 // Indicates whether the CPU sampling is started. | |
| 67 bool is_sampling_started_; | |
| 68 | |
| 69 // The sampling value returned from the most recent QueryCpuTimePerProcessor | |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
This doesn't seem to be true. You set the variable
Hongbo Min
2012/09/16 14:07:49
Use baseline_cpu_time_. Done
| |
| 70 // call. | |
| 71 std::vector<CpuTime> sampling_value_; | |
| 72 | |
| 73 // The callback which will be called when one sampling cycle is completed. | |
| 74 SamplingCallback callback_; | |
| 75 | |
| 76 // The timer used for polling CPU time periodically. Live on File thread. | |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
"file thread" or "FILE thread" are the more common
Hongbo Min
2012/09/16 14:07:49
Done.
| |
| 77 base::RepeatingTimer<CpuInfoProvider>* sampling_timer_; | |
| 78 | |
| 79 // The time interval for sampling. | |
|
Mihai Parparita -not on Chrome
2012/09/16 06:23:05
Mention the time units.
Hongbo Min
2012/09/16 14:07:49
Done.
| |
| 80 int sampling_interval_; | |
| 51 }; | 81 }; |
| 52 | 82 |
| 53 } // namespace extensions | 83 } // namespace extensions |
| 54 | 84 |
| 55 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 85 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
| 56 | 86 |
| OLD | NEW |