OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_cpu/cpu_info_provider.h" | 5 #include "extensions/browser/api/system_cpu/cpu_info_provider.h" |
6 | 6 |
7 #include <mach/mach_host.h> | 7 #include <mach/mach_host.h> |
8 | 8 |
9 #include "base/mac/scoped_mach_port.h" | 9 #include "base/mac/scoped_mach_port.h" |
10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
11 | 11 |
12 namespace extensions { | 12 namespace extensions { |
13 | 13 |
14 bool CpuInfoProvider::QueryCpuTimePerProcessor( | 14 bool CpuInfoProvider::QueryCpuTimePerProcessor( |
15 std::vector<linked_ptr<api::system_cpu::ProcessorInfo> >* infos) { | 15 std::vector<linked_ptr<core_api::system_cpu::ProcessorInfo> >* infos) { |
16 DCHECK(infos); | 16 DCHECK(infos); |
17 | 17 |
18 natural_t num_of_processors; | 18 natural_t num_of_processors; |
19 base::mac::ScopedMachSendRight host(mach_host_self()); | 19 base::mac::ScopedMachSendRight host(mach_host_self()); |
20 mach_msg_type_number_t type; | 20 mach_msg_type_number_t type; |
21 processor_cpu_load_info_data_t* cpu_infos; | 21 processor_cpu_load_info_data_t* cpu_infos; |
22 | 22 |
23 if (host_processor_info(host.get(), | 23 if (host_processor_info(host.get(), |
24 PROCESSOR_CPU_LOAD_INFO, | 24 PROCESSOR_CPU_LOAD_INFO, |
25 &num_of_processors, | 25 &num_of_processors, |
26 reinterpret_cast<processor_info_array_t*>(&cpu_infos), | 26 reinterpret_cast<processor_info_array_t*>(&cpu_infos), |
27 &type) == KERN_SUCCESS) { | 27 &type) == KERN_SUCCESS) { |
28 DCHECK_EQ(num_of_processors, | 28 DCHECK_EQ(num_of_processors, |
29 static_cast<natural_t>(base::SysInfo::NumberOfProcessors())); | 29 static_cast<natural_t>(base::SysInfo::NumberOfProcessors())); |
30 DCHECK_EQ(num_of_processors, static_cast<natural_t>(infos->size())); | 30 DCHECK_EQ(num_of_processors, static_cast<natural_t>(infos->size())); |
31 | 31 |
32 for (natural_t i = 0; i < num_of_processors; ++i) { | 32 for (natural_t i = 0; i < num_of_processors; ++i) { |
33 double user = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_USER]), | 33 double user = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_USER]), |
34 sys = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_SYSTEM]), | 34 sys = |
35 nice = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_NICE]), | 35 static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_SYSTEM]), |
36 idle = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_IDLE]); | 36 nice = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_NICE]), |
| 37 idle = static_cast<double>(cpu_infos[i].cpu_ticks[CPU_STATE_IDLE]); |
37 | 38 |
38 infos->at(i)->usage.kernel = sys; | 39 infos->at(i)->usage.kernel = sys; |
39 infos->at(i)->usage.user = user + nice; | 40 infos->at(i)->usage.user = user + nice; |
40 infos->at(i)->usage.idle = idle; | 41 infos->at(i)->usage.idle = idle; |
41 infos->at(i)->usage.total = sys + user + nice + idle; | 42 infos->at(i)->usage.total = sys + user + nice + idle; |
42 } | 43 } |
43 | 44 |
44 vm_deallocate(mach_task_self(), | 45 vm_deallocate(mach_task_self(), |
45 reinterpret_cast<vm_address_t>(cpu_infos), | 46 reinterpret_cast<vm_address_t>(cpu_infos), |
46 num_of_processors * sizeof(processor_cpu_load_info)); | 47 num_of_processors * sizeof(processor_cpu_load_info)); |
47 | 48 |
48 return true; | 49 return true; |
49 } | 50 } |
50 | 51 |
51 return false; | 52 return false; |
52 } | 53 } |
53 | 54 |
54 } // namespace extensions | 55 } // namespace extensions |
OLD | NEW |