| 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( |
| 71 int type, |
| 72 const content::NotificationSource& source, |
| 73 const content::NotificationDetails& details) { |
| 74 switch (type) { |
| 75 case chrome::NOTIFICATION_APP_TERMINATING: |
| 76 StopSampling(); |
| 77 break; |
| 78 default: |
| 79 NOTREACHED(); |
| 80 break; |
| 81 } |
| 82 } |
| 83 |
| 84 void CpuInfoProvider::StartSamplingOnFileThread( |
| 85 const SamplingCallback& callback) { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 87 if (is_sampling_started_) |
| 88 return; |
| 89 |
| 90 if (!QueryCpuTimePerProcessor(&baseline_cpu_time_)) |
| 91 return; |
| 92 |
| 93 is_sampling_started_ = true; |
| 94 callback_ = callback; |
| 95 DCHECK(!sampling_timer_) << "The sampling timer has leaked. Did you forgot " |
| 96 "to call StopSampling?"; |
| 97 // Should be deleted on FILE thread. |
| 98 sampling_timer_ = new base::RepeatingTimer<CpuInfoProvider>(); |
| 99 sampling_timer_->Start(FROM_HERE, |
| 100 base::TimeDelta::FromMilliseconds(sampling_interval_), |
| 101 this, &CpuInfoProvider::DoSample); |
| 102 } |
| 103 |
| 104 void CpuInfoProvider::StopSamplingOnFileThread() { |
| 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 106 if (!is_sampling_started_) return; |
| 107 delete sampling_timer_; |
| 108 sampling_timer_ = NULL; |
| 109 baseline_cpu_time_.clear(); |
| 110 is_sampling_started_ = false; |
| 111 } |
| 112 |
| 113 void CpuInfoProvider::DoSample() { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 115 |
| 116 std::vector<CpuTime> next_cpu_time; |
| 117 if (!QueryCpuTimePerProcessor(&next_cpu_time)) |
| 118 return; |
| 119 |
| 120 // Calculate the CPU usage information from the next_cpu_time and |
| 121 // |baseline_cpu_time_|. |
| 122 double total_usage = 0; |
| 123 scoped_ptr<CpuUpdateInfo> info(new CpuUpdateInfo()); |
| 124 for (size_t i = 0; i < next_cpu_time.size(); ++i) { |
| 125 double total_time = |
| 126 (next_cpu_time[i].user - baseline_cpu_time_[i].user) + |
| 127 (next_cpu_time[i].kernel - baseline_cpu_time_[i].kernel) + |
| 128 (next_cpu_time[i].idle - baseline_cpu_time_[i].idle); |
| 129 double idle_time = next_cpu_time[i].idle - baseline_cpu_time_[i].idle; |
| 130 |
| 131 double usage = 0; |
| 132 if (total_time != 0) |
| 133 usage = (100 - idle_time * 100 / total_time); |
| 134 info->usage_per_processor.push_back(usage); |
| 135 total_usage += usage; |
| 136 } |
| 137 |
| 138 info->average_usage = total_usage / next_cpu_time.size(); |
| 139 if (!callback_.is_null()) |
| 140 callback_.Run(info.Pass()); |
| 141 // Use next_cpu_time as baseline_cpu_time_ for the next sampling cycle. |
| 142 baseline_cpu_time_.swap(next_cpu_time); |
| 143 } |
| 144 |
| 38 } // namespace extensions | 145 } // namespace extensions |
| OLD | NEW |