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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc
diff --git a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc
index 7a9e086cb03bbfcc4786c00908242fefc30f12ca..8b1d962122a3ef8db60873dbae296e39c2dcdb96 100644
--- a/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc
+++ b/chrome/browser/extensions/api/system_info_cpu/cpu_info_provider_win.cc
@@ -4,11 +4,83 @@
#include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
+#include <windows.h>
+
+#include "base/sys_info.h"
+
namespace extensions {
+namespace {
+
+// SYSTEM_INFORMATION_CLASS enum value for processor information.
+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.
+
+// The SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION data structure is originated
+// from MSDN.
+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.
+ LARGE_INTEGER IdleTime;
+ LARGE_INTEGER KernelTime;
+ LARGE_INTEGER UserTime;
+ LARGE_INTEGER Reserved1[2];
+ ULONG Reserved2;
+} SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION;
+
+// 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.
+// |system_info_class|: The kind of the system information to be retrieved.
+// |system_info|: The pointer to a buffer that receives the requested info.
+// |system_info_length|: The size of the buffer pointed to by the |system_info|
+// parameter, in bytes.
+// |return_length|: An optional pointer to a location where the function
+// writes the actual size of the information requested.
+typedef DWORD (WINAPI *NtQuerySystemInformationPF)(DWORD system_info_class,
+ PVOID system_info,
+ ULONG system_info_length,
+ PULONG return_length);
+} // namespace
+
bool CpuInfoProvider::QueryCpuTimePerProcessor(std::vector<CpuTime>* times) {
- // TODO(hongbo): use NtQuerySystemInformation to query the cpu time.
- return false;
+ 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.
+
+ 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.
+
+ 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.
+ NtQuerySystemInformationPF NtQuerySystemInformation =
+ reinterpret_cast<NtQuerySystemInformationPF>(
+ ::GetProcAddress(ntmodule, "NtQuerySystemInformation"));
+
+ 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
+ return false;
+
+ 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
+
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.
+ scoped_array<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> processor_info(
+ new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]);
+
+ 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.
+ bytes = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors;
+ returned_bytes = 0;
+
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.
+ if (NtQuerySystemInformation(kSystemProcessorPerformanceInformation,
+ processor_info.get(),
+ bytes,
+ &returned_bytes) != 0)
+ return false;
+
+ int returned_num_of_processors =
+ returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
+
+ 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.
+
+ for (int i = 0; i < returned_num_of_processors; ++i) {
+ CpuTime time;
+
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.
+ time.kernel = processor_info[i].KernelTime.QuadPart;
+ time.user = processor_info[i].UserTime.QuadPart;
+ time.idle = processor_info[i].IdleTime.QuadPart;
+
+ times->push_back(time);
+ }
+ return true;
}
} // namespace extensions
« 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