Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc

Issue 15817008: Move systemInfo.cpu API out out experimental namespace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
6
7 #include <windows.h>
8 #include <winternl.h>
9
10 #include "base/sys_info.h"
11
12 namespace extensions {
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
27 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) {
28 std::vector<CpuTime> results;
29
30 HMODULE ntdll = GetModuleHandle(kNtdll);
31 CHECK(ntdll != NULL);
32 NtQuerySystemInformationPF NtQuerySystemInformation =
33 reinterpret_cast<NtQuerySystemInformationPF>(
34 ::GetProcAddress(ntdll, kNtQuerySystemInformationName));
35
36 CHECK(NtQuerySystemInformation != NULL);
37
38 int num_of_processors = base::SysInfo::NumberOfProcessors();
39 scoped_ptr<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[]> processor_info(
40 new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]);
41
42 ULONG returned_bytes = 0, bytes =
43 sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors;
44 if (!NT_SUCCESS(NtQuerySystemInformation(
45 SystemProcessorPerformanceInformation,
46 processor_info.get(), bytes, &returned_bytes)))
47 return false;
48
49 int returned_num_of_processors =
50 returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
51
52 if (returned_num_of_processors != num_of_processors)
53 return false;
54
55 results.reserve(returned_num_of_processors);
56 for (int i = 0; i < returned_num_of_processors; ++i) {
57 CpuTime time;
58 // KernelTime needs to be fixed-up, it includes both idle time and real
59 // kernel time.
60 time.kernel = processor_info[i].KernelTime.QuadPart -
61 processor_info[i].IdleTime.QuadPart;
62 time.user = processor_info[i].UserTime.QuadPart;
63 time.idle = processor_info[i].IdleTime.QuadPart;
64 results.push_back(time);
65 }
66
67 times->swap(results);
68 return true;
69 }
70
71 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698