Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / system_cpu / cpu_info_provider_win.cc
blobf47dcad24e947b62ef344670529787edfcdcae90
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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/system_cpu/cpu_info_provider.h"
7 #include <windows.h>
8 #include <winternl.h>
10 #include "base/sys_info.h"
12 namespace extensions {
14 namespace {
16 const wchar_t kNtdll[] = L"ntdll.dll";
17 const char kNtQuerySystemInformationName[] = "NtQuerySystemInformation";
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);
25 } // namespace
27 bool CpuInfoProvider::QueryCpuTimePerProcessor(
28 std::vector<linked_ptr<core_api::system_cpu::ProcessorInfo> >* infos) {
29 DCHECK(infos);
31 HMODULE ntdll = GetModuleHandle(kNtdll);
32 CHECK(ntdll != NULL);
33 NtQuerySystemInformationPF NtQuerySystemInformation =
34 reinterpret_cast<NtQuerySystemInformationPF>(
35 ::GetProcAddress(ntdll, kNtQuerySystemInformationName));
37 CHECK(NtQuerySystemInformation != NULL);
39 int num_of_processors = base::SysInfo::NumberOfProcessors();
40 scoped_ptr<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[]> processor_info(
41 new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]);
43 ULONG returned_bytes = 0,
44 bytes = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) *
45 num_of_processors;
46 if (!NT_SUCCESS(
47 NtQuerySystemInformation(SystemProcessorPerformanceInformation,
48 processor_info.get(),
49 bytes,
50 &returned_bytes)))
51 return false;
53 int returned_num_of_processors =
54 returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
56 if (returned_num_of_processors != num_of_processors)
57 return false;
59 DCHECK_EQ(num_of_processors, static_cast<int>(infos->size()));
60 for (int i = 0; i < returned_num_of_processors; ++i) {
61 double kernel = static_cast<double>(processor_info[i].KernelTime.QuadPart),
62 user = static_cast<double>(processor_info[i].UserTime.QuadPart),
63 idle = static_cast<double>(processor_info[i].IdleTime.QuadPart);
65 // KernelTime needs to be fixed-up, because it includes both idle time and
66 // real kernel time.
67 infos->at(i)->usage.kernel = kernel - idle;
68 infos->at(i)->usage.user = user;
69 infos->at(i)->usage.idle = idle;
70 infos->at(i)->usage.total = kernel + user;
73 return true;
76 } // namespace extensions