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

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

Issue 10916197: Implement querying CPU time on Windows for systemInfo.cpu API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 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
« no previous file with comments | « chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
9 #include "base/sys_info.h"
10
7 namespace extensions { 11 namespace extensions {
8 12
13 namespace {
14
15 // SYSTEM_INFORMATION_CLASS enum value for processor information.
16 const DWORD kSystemProcessorPerformanceInformation = 0x8;
alexeypa (please no reviews) 2012/09/11 16:33:16 This information class is declared in winternl.h.
Hongbo Min 2012/09/12 12:31:37 Done.
17
18 // The SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION data structure is originated
19 // from MSDN.
20 typedef struct _SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION {
alexeypa (please no reviews) 2012/09/11 16:33:16 This structure is declared in <winternl.h>.
Hongbo Min 2012/09/12 12:31:37 Done.
21 LARGE_INTEGER IdleTime;
22 LARGE_INTEGER KernelTime;
23 LARGE_INTEGER UserTime;
24 LARGE_INTEGER Reserved1[2];
25 ULONG Reserved2;
26 } SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
27
28 // Undocumented NT API used for querying system information.
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: It is partially documented. There us an entry
Hongbo Min 2012/09/12 12:31:37 Done.
29 // |system_info_class|: The kind of the system information to be retrieved.
30 // |system_info|: The pointer to a buffer that receives the requested info.
31 // |system_info_length|: The size of the buffer pointed to by the |system_info|
32 // parameter, in bytes.
33 // |return_length|: An optional pointer to a location where the function
34 // writes the actual size of the information requested.
35 typedef DWORD (WINAPI *NtQuerySystemInformationPF)(DWORD system_info_class,
36 PVOID system_info,
37 ULONG system_info_length,
38 PULONG return_length);
39 } // namespace
40
9 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) { 41 bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) {
10 // TODO(hongbo): use NtQuerySystemInformation to query the cpu time. 42 if (!times) return false;
alexeypa (please no reviews) 2012/09/11 16:33:16 Move "return false" to the next line.
Hongbo Min 2012/09/12 12:31:37 Done.
11 return false; 43
44 times->clear();
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: you can avoid this by having a local std::vec
Hongbo Min 2012/09/12 12:31:37 Done.
45
46 HMODULE ntmodule = GetModuleHandle(L"ntdll.dll");
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: consider declaring constants for L"ntdll.dll"
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: Add CHECK(ntmodule != NULL) here.
Hongbo Min 2012/09/12 12:31:37 Done.
47 NtQuerySystemInformationPF NtQuerySystemInformation =
48 reinterpret_cast<NtQuerySystemInformationPF>(
49 ::GetProcAddress(ntmodule, "NtQuerySystemInformation"));
50
51 if (!NtQuerySystemInformation)
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: I believe this can be CHECK(NtQuerySystemInfo
Hongbo Min 2012/09/12 12:31:37 Directly use NtQuerySystemInformation API as defin
52 return false;
53
54 int num_of_processors = base::SysInfo::NumberOfProcessors();
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: Why is this number signed?
Hongbo Min 2012/09/12 12:31:37 The base::SysInfo::NumberOfProcessors returns sign
alexeypa (please no reviews) 2012/09/12 15:41:55 I understand but why does it return a signed numbe
55
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: no need for an empty line here.
Hongbo Min 2012/09/12 12:31:37 Done.
56 scoped_array<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> processor_info(
57 new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]);
58
59 ULONG bytes, returned_bytes;
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: Declare both |bytes| and |returned_bytes| on
Hongbo Min 2012/09/12 12:31:37 Done.
60 bytes = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors;
61 returned_bytes = 0;
62
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: no need for an empty line here.
Hongbo Min 2012/09/12 12:31:37 Done.
63 if (NtQuerySystemInformation(kSystemProcessorPerformanceInformation,
64 processor_info.get(),
65 bytes,
66 &returned_bytes) != 0)
67 return false;
68
69 int returned_num_of_processors =
70 returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
71
72 DCHECK(returned_num_of_processors == num_of_processors);
alexeypa (please no reviews) 2012/09/11 16:33:16 Does |returned_num_of_processors != num_of_process
Hongbo Min 2012/09/12 12:31:37 Return false in case of that error happens.
73
74 for (int i = 0; i < returned_num_of_processors; ++i) {
75 CpuTime time;
76
alexeypa (please no reviews) 2012/09/11 16:33:16 nit: no need for an empty line.
Hongbo Min 2012/09/12 12:31:37 Done.
77 time.kernel = processor_info[i].KernelTime.QuadPart;
78 time.user = processor_info[i].UserTime.QuadPart;
79 time.idle = processor_info[i].IdleTime.QuadPart;
80
81 times->push_back(time);
82 }
83 return true;
12 } 84 }
13 85
14 } // namespace extensions 86 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698