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

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: updated patch 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..b886c648979d13fbf4ab720b0b2d17fb8d4c5ff7 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,71 @@
#include "chrome/browser/extensions/api/system_info_cpu/cpu_info_provider.h"
+#include <windows.h>
+#include <winternl.h>
+
+#include "base/sys_info.h"
+
namespace extensions {
+namespace {
+
+const wchar_t kNtdll[] = L"ntdll.dll";
+const char kNtQuerySystemInformationName[] = "NtQuerySystemInformation";
+
+// See MSDN about NtQuerySystemInformation definition.
+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;
+
+ std::vector<CpuTime> results;
+
+ HMODULE ntdll = GetModuleHandle(kNtdll);
+ CHECK(ntdll != NULL);
+ NtQuerySystemInformationPF NtQuerySystemInformation =
+ reinterpret_cast<NtQuerySystemInformationPF>(
+ ::GetProcAddress(ntdll, kNtQuerySystemInformationName));
+
+ CHECK(NtQuerySystemInformation != NULL);
+ 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.
+ return false;
+
+ int num_of_processors = base::SysInfo::NumberOfProcessors();
+ scoped_array<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> processor_info(
+ new SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION[num_of_processors]);
+ DCHECK_GT(num_of_processors, 0);
+
+ 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(
+ bytes = sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * num_of_processors;
+ if (!NT_SUCCESS(NtQuerySystemInformation(
+ SystemProcessorPerformanceInformation,
+ processor_info.get(), bytes, &returned_bytes)))
+ return false;
+
+ 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
+ returned_bytes / sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION);
+
+ if (returned_num_of_processors != num_of_processors)
+ return false;
+
+ results.reserve(returned_num_of_processors);
+ for (int i = 0; i < returned_num_of_processors; ++i) {
+ CpuTime time;
+ time.kernel = processor_info[i].KernelTime.QuadPart;
+ time.user = processor_info[i].UserTime.QuadPart;
+ time.idle = processor_info[i].IdleTime.QuadPart;
+ results.push_back(time);
+ }
+
+ 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.
+ 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