summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/SConscript1
-rw-r--r--base/build/base.vcproj8
-rw-r--r--base/cpu.cc52
-rw-r--r--base/cpu.h42
4 files changed, 103 insertions, 0 deletions
diff --git a/base/SConscript b/base/SConscript
index 8e5cf21..ad77557 100644
--- a/base/SConscript
+++ b/base/SConscript
@@ -100,6 +100,7 @@ if env['PLATFORM'] == 'win32':
'base_paths_win.cc',
'clipboard_win.cc',
'condition_variable_win.cc',
+ 'cpu.cc',
'debug_on_start.cc',
'debug_util_win.cc',
'file_util_win.cc',
diff --git a/base/build/base.vcproj b/base/build/base.vcproj
index 4eed57ea..5f69555 100644
--- a/base/build/base.vcproj
+++ b/base/build/base.vcproj
@@ -242,6 +242,14 @@
>
</File>
<File
+ RelativePath="..\cpu.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\cpu.h"
+ >
+ </File>
+ <File
RelativePath="..\debug_on_start.cc"
>
</File>
diff --git a/base/cpu.cc b/base/cpu.cc
new file mode 100644
index 0000000..d227d9a
--- /dev/null
+++ b/base/cpu.cc
@@ -0,0 +1,52 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/cpu.h"
+#include <intrin.h>
+#include <string>
+
+namespace base {
+
+CPU::CPU()
+ : type_(0),
+ family_(0),
+ model_(0),
+ stepping_(0),
+ ext_model_(0),
+ ext_family_(0),
+ cpu_vendor_("unknown") {
+ Initialize();
+}
+
+void CPU::Initialize() {
+ int cpu_info[4] = {-1};
+ char cpu_string[0x20];
+
+ // __cpuid with an InfoType argument of 0 returns the number of
+ // valid Ids in CPUInfo[0] and the CPU identification string in
+ // the other three array elements. The CPU identification string is
+ // not in linear order. The code below arranges the information
+ // in a human readable form.
+ //
+ // More info can be found here:
+ // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
+ __cpuid(cpu_info, 0);
+ int num_ids = cpu_info[0];
+ memset(cpu_string, 0, sizeof(cpu_string));
+ *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1];
+ *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3];
+ *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2];
+
+ // Interpret CPU feature information.
+ __cpuid(cpu_info, 1);
+ stepping_ = cpu_info[0] & 0xf;
+ model_ = (cpu_info[0] >> 4) & 0xf;
+ family_ = (cpu_info[0] >> 8) & 0xf;
+ type_ = (cpu_info[0] >> 12) & 0x3;
+ ext_model_ = (cpu_info[0] >> 16) & 0xf;
+ ext_family_ = (cpu_info[0] >> 20) & 0xff;
+ cpu_vendor_ = cpu_string;
+}
+
+} // namespace base
diff --git a/base/cpu.h b/base/cpu.h
new file mode 100644
index 0000000..e437a19
--- /dev/null
+++ b/base/cpu.h
@@ -0,0 +1,42 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_CPU_H_
+#define BASE_CPU_H_
+
+#include <string>
+
+namespace base {
+
+// Query information about the processor.
+class CPU {
+ public:
+ // Constructor
+ CPU();
+
+ // Accessors for CPU information.
+ const std::string& vendor_name() const { return cpu_vendor_; }
+ int stepping() const { return stepping_; }
+ int model() const { return model_; }
+ int family() const { return family_; }
+ int type() const { return type_; }
+ int extended_model() const { return ext_model_; }
+ int extended_family() const { return ext_family_; }
+
+ private:
+ // Query the processor for CPUID information.
+ void Initialize();
+
+ int type_; // process type
+ int family_; // family of the processor
+ int model_; // model of processor
+ int stepping_; // processor revision number
+ int ext_model_;
+ int ext_family_;
+ std::string cpu_vendor_;
+};
+
+} // namespace base
+
+#endif // BASE_CPU_H_