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; |
|
benwells
2012/09/21 08:19:50
Sorry, but what is the void here for?
Hongbo Min
2012/09/21 09:00:23
It is the callback signature, right? All SamplingC
benwells
2012/09/25 17:32:13
Yes, you're right.
| |
| 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. It gets called on UI thread, the |callback| gets called on the |
| 29 // returned. | 32 // FILE thread. |
| 30 bool StartSampling(const QueryCpuTimeCallback& callback); | 33 void StartSampling(const SamplingCallback& callback); |
| 31 | 34 |
| 32 // Stop the sampling cycle. Return true if it succeeds to stop. | 35 // Stop the sampling cycle. Called on the FILE thread. |
| 33 bool StopSampling(); | 36 void StopSampling(); |
| 34 | 37 |
| 35 // Return the single shared instance of CpuInfoProvider. | 38 // Return the single shared instance of CpuInfoProvider. |
| 36 static CpuInfoProvider* Get(); | 39 static CpuInfoProvider* Get(); |
| 37 | 40 |
| 38 private: | 41 private: |
| 42 friend class SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo>; | |
| 43 friend class MockCpuInfoProviderImpl; | |
| 44 | |
| 39 // The amount of time that CPU spent on performing different kinds of work. | 45 // The amount of time that CPU spent on performing different kinds of work. |
| 40 // It is used to calculate the usage percent for processors. | 46 // It is used to calculate the usage percent for processors. |
| 41 struct CpuTime { | 47 class CpuTime { |
| 48 public: | |
| 49 CpuTime() : user(0), kernel(0), idle(0) {} | |
| 42 int64 user; // user mode. | 50 int64 user; // user mode. |
| 43 int64 kernel; // kernel mode. | 51 int64 kernel; // kernel mode. |
| 44 int64 idle; // twiddling thumbs. | 52 int64 idle; // twiddling thumbs. |
| 45 }; | 53 }; |
| 46 | 54 |
| 55 CpuInfoProvider(); | |
| 56 | |
| 47 // Platform specific implementation for querying the CPU time information | 57 // Platform specific implementation for querying the CPU time information |
| 48 // for each processor. | 58 // for each processor. |
| 49 bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); | 59 virtual bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); |
| 60 | |
| 61 // Start and stop sampling on the FILE thread. | |
| 62 void StartSamplingOnFileThread(const SamplingCallback& callback); | |
| 63 void StopSamplingOnFileThread(); | |
| 64 | |
| 65 // Called when the sampling timer is triggered. | |
| 66 void DoSample(); | |
| 67 | |
| 68 // Indicates whether the CPU sampling is started. | |
| 69 bool is_sampling_started_; | |
| 70 | |
| 71 // The sampling value returned from the most recent QueryCpuTimePerProcessor | |
| 72 // call. | |
| 73 std::vector<CpuTime> baseline_cpu_time_; | |
| 74 | |
| 75 // The callback which will be called when one sampling cycle is completed. | |
| 76 SamplingCallback callback_; | |
| 77 | |
| 78 // The timer used for polling CPU time periodically. Live on FILE thread. | |
|
benwells
2012/09/21 08:19:50
Nit: s/Live/Lives/
Hongbo Min
2012/09/21 09:00:23
Done.
| |
| 79 base::RepeatingTimer<CpuInfoProvider>* sampling_timer_; | |
| 80 | |
| 81 // The time interval for sampling, in milliseconds. | |
| 82 int sampling_interval_; | |
| 50 }; | 83 }; |
| 51 | 84 |
| 52 } // namespace extensions | 85 } // namespace extensions |
| 53 | 86 |
| 54 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 87 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
| 55 | 88 |
| OLD | NEW |