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 <windows.h> | |
| 8 #include <winternl.h> | |
| 9 | |
| 10 #include "base/sys_info.h" | |
| 11 | |
| 7 namespace extensions { | 12 namespace extensions { |
| 8 | 13 |
| 14 namespace { | |
| 15 | |
| 16 const wchar_t kNtdll[] = L"ntdll.dll"; | |
| 17 const char kNtQuerySystemInformationName[] = "NtQuerySystemInformation"; | |
| 18 | |
| 19 // See MSDN about NtQuerySystemInformation definition. | |
| 20 typedef DWORD (WINAPI *NtQuerySystemInformationPF)(DWORD system_info_class, | |
| 21 PVOID system_info, | |
| 22 ULONG system_info_length, | |
| 23 PULONG return_length); | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 9 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { | 27 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { |
| 10 // TODO(hongbo): use NtQuerySystemInformation to query the cpu time. | 28 if (!times) |
| 11 return false; | 29 return false; |
| 30 | |
| 31 std::vector<CpuTime> results; | |
| 32 | |
| 33 HMODULE ntdll = GetModuleHandle(kNtdll); | |
| 34 CHECK(ntdll != NULL); | |
| 35 NtQuerySystemInformationPF NtQuerySystemInformation = | |
| 36 reinterpret_cast<NtQuerySystemInformationPF>( | |
| 37 ::GetProcAddress(ntdll, kNtQuerySystemInformationName)); | |
| 38 | |
| 39 CHECK(NtQuerySystemInformation != NULL); | |
| 40 if (!NtQuerySystemInformation) | |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: remove the if statement. CHECK will crash the
Hongbo Min
2012/09/14 02:56:40
Done.
| |
| 41 return false; | |
| 42 | |
| 43 int num_of_processors = base::SysInfo::NumberOfProcessors(); | |
| 44 scoped_array<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> processor_info( | |
| 45 new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]); | |
| 46 DCHECK_GT(num_of_processors, 0); | |
| 47 | |
| 48 ULONG bytes = 0, returned_bytes = 0; | |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: Sorry, what I mean is:
ULONG bytes = sizeof(
| |
| 49 bytes = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors; | |
| 50 if (!NT_SUCCESS(NtQuerySystemInformation( | |
| 51 SystemProcessorPerformanceInformation, | |
| 52 processor_info.get(), bytes, &returned_bytes))) | |
| 53 return false; | |
| 54 | |
| 55 int returned_num_of_processors = | |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: The type should be ULONG since |returned_byte
Hongbo Min
2012/09/14 02:56:40
Yes, but because the num_of_processors is int type
| |
| 56 returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION); | |
| 57 | |
| 58 if (returned_num_of_processors != num_of_processors) | |
| 59 return false; | |
| 60 | |
| 61 results.reserve(returned_num_of_processors); | |
| 62 for (int i = 0; i < returned_num_of_processors; ++i) { | |
| 63 CpuTime time; | |
| 64 time.kernel = processor_info[i].KernelTime.QuadPart; | |
| 65 time.user = processor_info[i].UserTime.QuadPart; | |
| 66 time.idle = processor_info[i].IdleTime.QuadPart; | |
| 67 results.push_back(time); | |
| 68 } | |
| 69 | |
| 70 results.swap(*times); | |
|
alexeypa (please no reviews)
2012/09/13 16:45:48
nit: This probably is more readable if it says tim
Hongbo Min
2012/09/14 02:56:40
Done.
| |
| 71 return true; | |
| 12 } | 72 } |
| 13 | 73 |
| 14 } // namespace extensions | 74 } // namespace extensions |
| OLD | NEW |