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 | 4 |
| 5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" | 5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h" |
| 6 | 6 |
| 7 #include "base/sys_info.h" | 7 #include "base/sys_info.h" |
| 8 #include "chrome/common/chrome_notification_types.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/browser/notification_details.h" | |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 #include "content/public/browser/notification_source.h" | |
| 13 #include "content/public/browser/notification_types.h" | |
| 8 | 14 |
| 9 namespace extensions { | 15 namespace extensions { |
| 10 | 16 |
| 11 using api::experimental_system_info_cpu::CpuInfo; | 17 using api::experimental_system_info_cpu::CpuInfo; |
| 18 using api::experimental_system_info_cpu::CpuUpdateInfo; | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 // Default sampling interval is 1000ms. | |
| 22 const unsigned int kDefaultSamplingIntervalMs = 1000; | |
| 23 | |
| 24 CpuInfoProvider::CpuInfoProvider() | |
| 25 : is_sampling_started_(false), | |
| 26 sampling_timer_(NULL), | |
| 27 sampling_interval_(kDefaultSamplingIntervalMs) { | |
| 28 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | |
| 29 content::NotificationService::AllSources()); | |
| 30 } | |
| 31 | |
| 32 CpuInfoProvider::~CpuInfoProvider() { | |
| 33 DCHECK(sampling_timer_ == NULL); | |
| 34 registrar_.RemoveAll(); | |
| 35 } | |
| 12 | 36 |
| 13 bool CpuInfoProvider::QueryInfo(CpuInfo* info) { | 37 bool CpuInfoProvider::QueryInfo(CpuInfo* info) { |
| 14 if (info == NULL) | 38 if (info == NULL) |
| 15 return false; | 39 return false; |
| 16 | 40 |
| 17 info->num_of_processors = base::SysInfo::NumberOfProcessors(); | 41 info->num_of_processors = base::SysInfo::NumberOfProcessors(); |
| 18 info->arch_name = base::SysInfo::CPUArchitecture(); | 42 info->arch_name = base::SysInfo::CPUArchitecture(); |
| 19 info->model_name = base::SysInfo::CPUModelName(); | 43 info->model_name = base::SysInfo::CPUModelName(); |
| 20 return true; | 44 return true; |
| 21 } | 45 } |
| 22 | 46 |
| 23 bool CpuInfoProvider::StartSampling(const QueryCpuTimeCallback& callback) { | 47 void CpuInfoProvider::StartSampling(const SamplingCallback& callback) { |
| 24 // TODO(hongbo): Implement sampling functionality for event router. | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 25 return false; | 49 BrowserThread::PostTask( |
| 50 BrowserThread::FILE, | |
| 51 FROM_HERE, | |
| 52 base::Bind(&CpuInfoProvider::StartSamplingOnFileThread, | |
| 53 base::Unretained(this), callback)); | |
| 26 } | 54 } |
| 27 | 55 |
| 28 bool CpuInfoProvider::StopSampling() { | 56 void CpuInfoProvider::StopSampling() { |
| 29 // TODO(hongbo): Implement sampling functionality for event router. | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 30 return false; | 58 BrowserThread::PostTask( |
| 59 BrowserThread::FILE, | |
| 60 FROM_HERE, | |
| 61 base::Bind(&CpuInfoProvider::StopSamplingOnFileThread, | |
| 62 base::Unretained(this))); | |
| 31 } | 63 } |
| 32 | 64 |
| 33 // static | 65 // static |
| 34 CpuInfoProvider* CpuInfoProvider::Get() { | 66 CpuInfoProvider* CpuInfoProvider::Get() { |
| 35 return CpuInfoProvider::GetInstance<CpuInfoProvider>(); | 67 return CpuInfoProvider::GetInstance<CpuInfoProvider>(); |
| 36 } | 68 } |
| 37 | 69 |
| 70 void CpuInfoProvider::Observe(int type, | |
| 71 const content::NotificationSource& source, | |
|
benwells
2012/10/04 06:58:59
Nit: fix indenting here. If you have room line up
Hongbo Min
2012/10/05 02:40:56
Done.
| |
| 72 const content::NotificationDetails& details) { | |
| 73 switch (type) { | |
| 74 case chrome::NOTIFICATION_APP_TERMINATING: | |
| 75 StopSampling(); | |
| 76 break; | |
| 77 default: | |
| 78 NOTREACHED(); | |
| 79 break; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void CpuInfoProvider::StartSamplingOnFileThread( | |
| 84 const SamplingCallback& callback) { | |
| 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 86 if (is_sampling_started_) | |
| 87 return; | |
| 88 | |
| 89 if (!QueryCpuTimePerProcessor(&baseline_cpu_time_)) | |
| 90 return; | |
| 91 | |
| 92 is_sampling_started_ = true; | |
| 93 callback_ = callback; | |
| 94 DCHECK(!sampling_timer_) << "The sampling timer is leaked. Did you forgot " | |
|
benwells
2012/10/04 06:58:59
Nit: The sampling timer has leaked. Did you forget
Hongbo Min
2012/10/05 02:40:56
Done.
| |
| 95 "calling StopSampling?"; | |
| 96 // Should be deleted on FILE thread. | |
| 97 sampling_timer_ = new base::RepeatingTimer<CpuInfoProvider>(); | |
| 98 sampling_timer_->Start(FROM_HERE, | |
| 99 base::TimeDelta::FromMilliseconds(sampling_interval_), | |
| 100 this, &CpuInfoProvider::DoSample); | |
| 101 } | |
| 102 | |
| 103 void CpuInfoProvider::StopSamplingOnFileThread() { | |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 105 if (!is_sampling_started_) return; | |
| 106 delete sampling_timer_; | |
| 107 sampling_timer_ = NULL; | |
| 108 baseline_cpu_time_.clear(); | |
| 109 is_sampling_started_ = false; | |
| 110 } | |
| 111 | |
| 112 void CpuInfoProvider::DoSample() { | |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 114 | |
| 115 std::vector<CpuTime> next_cpu_time; | |
| 116 if (!QueryCpuTimePerProcessor(&next_cpu_time)) | |
| 117 return; | |
| 118 | |
| 119 // Calculate the CPU usage information from the next_cpu_time and | |
| 120 // |baseline_cpu_time_|. | |
| 121 double total_usage = 0; | |
| 122 scoped_ptr<CpuUpdateInfo> info(new CpuUpdateInfo()); | |
| 123 for (size_t i = 0; i < next_cpu_time.size(); ++i) { | |
| 124 double total_time = | |
| 125 (next_cpu_time[i].user - baseline_cpu_time_[i].user) + | |
| 126 (next_cpu_time[i].kernel - baseline_cpu_time_[i].kernel) + | |
| 127 (next_cpu_time[i].idle - baseline_cpu_time_[i].idle); | |
| 128 double idle_time = next_cpu_time[i].idle - baseline_cpu_time_[i].idle; | |
| 129 | |
| 130 double usage = 0; | |
| 131 if (total_time != 0) | |
| 132 usage = (100 - idle_time * 100 / total_time); | |
| 133 info->usage_per_processor.push_back(usage); | |
| 134 total_usage += usage; | |
| 135 } | |
| 136 | |
| 137 info->average_usage = total_usage / next_cpu_time.size(); | |
| 138 if (!callback_.is_null()) | |
| 139 callback_.Run(info.Pass()); | |
| 140 // Use next_cpu_time as baseline_cpu_time_ for the next sampling cycle. | |
| 141 baseline_cpu_time_.swap(next_cpu_time); | |
| 142 } | |
| 143 | |
| 38 } // namespace extensions | 144 } // namespace extensions |
| OLD | NEW |