| 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" |
| 13 #include "content/public/browser/notification_observer.h" |
| 14 #include "content/public/browser/notification_registrar.h" |
| 12 | 15 |
| 13 namespace extensions { | 16 namespace extensions { |
| 14 | 17 |
| 15 class CpuInfoProvider | 18 class CpuInfoProvider |
| 16 : public SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo> { | 19 : public content::NotificationObserver, |
| 20 public SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo> { |
| 17 public: | 21 public: |
| 18 typedef base::Callback<api::experimental_system_info_cpu::CpuUpdateInfo> | 22 typedef base::Callback< |
| 19 QueryCpuTimeCallback; | 23 void(scoped_ptr<api::experimental_system_info_cpu::CpuUpdateInfo>)> |
| 20 virtual ~CpuInfoProvider() {} | 24 SamplingCallback; |
| 25 |
| 26 virtual ~CpuInfoProvider(); |
| 21 | 27 |
| 22 // Overriden from SystemInfoProvider<CpuInfo>. | 28 // Overriden from SystemInfoProvider<CpuInfo>. |
| 23 virtual bool QueryInfo( | 29 virtual bool QueryInfo( |
| 24 api::experimental_system_info_cpu::CpuInfo* info) OVERRIDE; | 30 api::experimental_system_info_cpu::CpuInfo* info) OVERRIDE; |
| 25 | 31 |
| 26 // Start sampling the CPU usage. The callback gets called when one sampling | 32 // 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 | 33 // cycle is completed periodically with the CPU updated usage info for each |
| 28 // processors. Return true if it succeeds to start, otherwise, false is | 34 // processors. It gets called on UI thread, the |callback| gets called on the |
| 29 // returned. | 35 // FILE thread. |
| 30 bool StartSampling(const QueryCpuTimeCallback& callback); | 36 void StartSampling(const SamplingCallback& callback); |
| 31 | 37 |
| 32 // Stop the sampling cycle. Return true if it succeeds to stop. | 38 // Stop the sampling cycle. Called on the FILE thread. |
| 33 bool StopSampling(); | 39 void StopSampling(); |
| 34 | 40 |
| 35 // Return the single shared instance of CpuInfoProvider. | 41 // Return the single shared instance of CpuInfoProvider. |
| 36 static CpuInfoProvider* Get(); | 42 static CpuInfoProvider* Get(); |
| 37 | 43 |
| 38 private: | 44 private: |
| 45 friend class SystemInfoProvider<api::experimental_system_info_cpu::CpuInfo>; |
| 46 friend class MockCpuInfoProviderImpl; |
| 47 |
| 39 // The amount of time that CPU spent on performing different kinds of work. | 48 // The amount of time that CPU spent on performing different kinds of work. |
| 40 // It is used to calculate the usage percent for processors. | 49 // It is used to calculate the usage percent for processors. |
| 41 struct CpuTime { | 50 struct CpuTime { |
| 51 CpuTime() : user(0), kernel(0), idle(0) {} |
| 42 int64 user; // user mode. | 52 int64 user; // user mode. |
| 43 int64 kernel; // kernel mode. | 53 int64 kernel; // kernel mode. |
| 44 int64 idle; // twiddling thumbs. | 54 int64 idle; // twiddling thumbs. |
| 45 }; | 55 }; |
| 46 | 56 |
| 57 CpuInfoProvider(); |
| 58 |
| 59 // content::NotificationObserver implementation. |
| 60 virtual void Observe(int type, |
| 61 const content::NotificationSource& source, |
| 62 const content::NotificationDetails& details) OVERRIDE; |
| 63 |
| 47 // Platform specific implementation for querying the CPU time information | 64 // Platform specific implementation for querying the CPU time information |
| 48 // for each processor. | 65 // for each processor. |
| 49 bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); | 66 virtual bool QueryCpuTimePerProcessor(std::vector<CpuTime>* times); |
| 67 |
| 68 // Start and stop sampling on the FILE thread. |
| 69 void StartSamplingOnFileThread(const SamplingCallback& callback); |
| 70 void StopSamplingOnFileThread(); |
| 71 |
| 72 // Called when the sampling timer is triggered. |
| 73 void DoSample(); |
| 74 |
| 75 content::NotificationRegistrar registrar_; |
| 76 |
| 77 // Indicates whether the CPU sampling is started. |
| 78 bool is_sampling_started_; |
| 79 |
| 80 // The sampling value returned from the most recent QueryCpuTimePerProcessor |
| 81 // call. |
| 82 std::vector<CpuTime> baseline_cpu_time_; |
| 83 |
| 84 // The callback which will be called when one sampling cycle is completed. |
| 85 SamplingCallback callback_; |
| 86 |
| 87 // The timer used for polling CPU time periodically. Lives on FILE thread. |
| 88 base::RepeatingTimer<CpuInfoProvider>* sampling_timer_; |
| 89 |
| 90 // The time interval for sampling, in milliseconds. |
| 91 int sampling_interval_; |
| 50 }; | 92 }; |
| 51 | 93 |
| 52 } // namespace extensions | 94 } // namespace extensions |
| 53 | 95 |
| 54 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ | 96 #endif // CHROME_BROWSER_EXTENSIONS_API_SYSTEM_INFO_CPU_CPU_INFO_PROVIDER_H_ |
| 55 | 97 |
| OLD | NEW |