Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / SystemInformation.cxx
blob6cd63e6f9ea3a20209a16c44dca9d39f280f94db
1 /*=========================================================================
3 Program: BatchMake
4 Module: $RCSfile: SystemInformation.cxx,v $
5 Language: C++
6 Date: $Date: 2009-03-20 02:48:05 $
7 Version: $Revision: 1.39 $
8 Copyright (c) 2005 Insight Consortium. All rights reserved.
9 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
15 =========================================================================*/
16 #ifdef _WIN32
17 # include <winsock.h> // WSADATA, include before sys/types.h
18 #endif
20 #include "kwsysPrivate.h"
21 #include KWSYS_HEADER(FundamentalType.h)
22 #include KWSYS_HEADER(stl/string)
23 #include KWSYS_HEADER(stl/vector)
24 #include KWSYS_HEADER(ios/iosfwd)
25 #include KWSYS_HEADER(SystemInformation.hxx)
26 #include KWSYS_HEADER(Process.h)
27 #include KWSYS_HEADER(ios/iostream)
28 #include KWSYS_HEADER(ios/sstream)
29 // Work-around CMake dependency scanning limitation. This must
30 // duplicate the above list of headers.
31 #if 0
32 # include "FundamentalType.h.in"
33 # include "SystemInformation.hxx.in"
34 # include "Process.h.in"
35 # include "Configure.hxx.in"
36 # include "kwsys_stl.hxx.in"
37 # include "kwsys_stl_vector.in"
38 # include "kwsys_stl_iosfwd.in"
39 # include "kwsys_ios_sstream.h.in"
40 # include "kwsys_ios_iostream.h.in"
41 #endif
44 #ifndef WIN32
45 # include <sys/utsname.h> // int uname(struct utsname *buf);
46 #endif
48 #ifdef _WIN32
49 # include <windows.h>
50 #endif
52 #ifdef __linux
53 # include <sys/procfs.h>
54 # include <sys/types.h>
55 # include <unistd.h>
56 # include <fcntl.h>
57 # include <ctype.h> // int isdigit(int c);
58 # include <errno.h> // extern int errno;
59 # include <sys/time.h>
60 #elif __hpux
61 # include <sys/param.h>
62 # include <sys/pstat.h>
63 #endif
65 #ifdef __HAIKU__
66 #include <OS.h>
67 #endif
69 #include <memory.h>
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <string.h>
76 namespace KWSYS_NAMESPACE
79 // Create longlong
80 #if KWSYS_USE_LONG_LONG
81 typedef long long LongLong;
82 #elif KWSYS_USE___INT64
83 typedef __int64 LongLong;
84 #else
85 # error "No Long Long"
86 #endif
88 // Define SystemInformationImplementation class
89 typedef void (*DELAY_FUNC)(unsigned int uiMS);
91 class SystemInformationImplementation
94 public:
95 SystemInformationImplementation ();
96 ~SystemInformationImplementation ();
98 const char * GetVendorString();
99 const char * GetVendorID();
100 kwsys_stl::string GetTypeID();
101 kwsys_stl::string GetFamilyID();
102 kwsys_stl::string GetModelID();
103 kwsys_stl::string GetSteppingCode();
104 const char * GetExtendedProcessorName();
105 const char * GetProcessorSerialNumber();
106 int GetProcessorCacheSize();
107 unsigned int GetLogicalProcessorsPerPhysical();
108 float GetProcessorClockFrequency();
109 int GetProcessorAPICID();
110 int GetProcessorCacheXSize(long int);
111 bool DoesCPUSupportFeature(long int);
113 const char * GetOSName();
114 const char * GetHostname();
115 const char * GetOSRelease();
116 const char * GetOSVersion();
117 const char * GetOSPlatform();
119 bool Is64Bits();
121 unsigned int GetNumberOfLogicalCPU(); // per physical cpu
122 unsigned int GetNumberOfPhysicalCPU();
124 bool DoesCPUSupportCPUID();
126 // Retrieve memory information in megabyte.
127 unsigned long GetTotalVirtualMemory();
128 unsigned long GetAvailableVirtualMemory();
129 unsigned long GetTotalPhysicalMemory();
130 unsigned long GetAvailablePhysicalMemory();
132 /** Run the different checks */
133 void RunCPUCheck();
134 void RunOSCheck();
135 void RunMemoryCheck();
136 public:
137 #define VENDOR_STRING_LENGTH (12 + 1)
138 #define CHIPNAME_STRING_LENGTH (48 + 1)
139 #define SERIALNUMBER_STRING_LENGTH (29 + 1)
141 typedef struct tagID
143 int Type;
144 int Family;
145 int Model;
146 int Revision;
147 int ExtendedFamily;
148 int ExtendedModel;
149 char ProcessorName[CHIPNAME_STRING_LENGTH];
150 char Vendor[VENDOR_STRING_LENGTH];
151 char SerialNumber[SERIALNUMBER_STRING_LENGTH];
152 } ID;
154 typedef struct tagCPUPowerManagement
156 bool HasVoltageID;
157 bool HasFrequencyID;
158 bool HasTempSenseDiode;
159 } CPUPowerManagement;
161 typedef struct tagCPUExtendedFeatures
163 bool Has3DNow;
164 bool Has3DNowPlus;
165 bool SupportsMP;
166 bool HasMMXPlus;
167 bool HasSSEMMX;
168 bool SupportsHyperthreading;
169 unsigned int LogicalProcessorsPerPhysical;
170 int APIC_ID;
171 CPUPowerManagement PowerManagement;
172 } CPUExtendedFeatures;
174 typedef struct CPUtagFeatures
176 bool HasFPU;
177 bool HasTSC;
178 bool HasMMX;
179 bool HasSSE;
180 bool HasSSEFP;
181 bool HasSSE2;
182 bool HasIA64;
183 bool HasAPIC;
184 bool HasCMOV;
185 bool HasMTRR;
186 bool HasACPI;
187 bool HasSerial;
188 bool HasThermal;
189 int CPUSpeed;
190 int L1CacheSize;
191 int L2CacheSize;
192 int L3CacheSize;
193 CPUExtendedFeatures ExtendedFeatures;
194 } CPUFeatures;
196 enum Manufacturer
198 AMD, Intel, NSC, UMC, Cyrix, NexGen, IDT, Rise, Transmeta, Sun, UnknownManufacturer
200 protected:
202 // Functions.
203 bool RetrieveCPUFeatures();
204 bool RetrieveCPUIdentity();
205 bool RetrieveCPUCacheDetails();
206 bool RetrieveClassicalCPUCacheDetails();
207 bool RetrieveCPUClockSpeed();
208 bool RetrieveClassicalCPUClockSpeed();
209 bool RetrieveCPUExtendedLevelSupport(int);
210 bool RetrieveExtendedCPUFeatures();
211 bool RetrieveProcessorSerialNumber();
212 bool RetrieveCPUPowerManagement();
213 bool RetrieveClassicalCPUIdentity();
214 bool RetrieveExtendedCPUIdentity();
216 Manufacturer ChipManufacturer;
217 CPUFeatures Features;
218 ID ChipID;
219 float CPUSpeedInMHz;
220 unsigned int NumberOfLogicalCPU;
221 unsigned int NumberOfPhysicalCPU;
223 int CPUCount();
224 unsigned char LogicalCPUPerPhysicalCPU();
225 unsigned char GetAPICId();
226 unsigned int IsHyperThreadingSupported();
227 LongLong GetCyclesDifference(DELAY_FUNC, unsigned int);
229 // For Linux and Cygwin, /proc/cpuinfo formats are slightly different
230 int RetreiveInformationFromCpuInfoFile();
231 kwsys_stl::string ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,
232 const char* word, size_t init=0);
234 static void Delay (unsigned int);
235 static void DelayOverhead (unsigned int);
237 void FindManufacturer();
239 // For Mac
240 bool ParseSysCtl();
241 kwsys_stl::string ExtractValueFromSysCtl(const char* word);
242 kwsys_stl::string SysCtlBuffer;
244 // For Solaris
245 bool QuerySolarisInfo();
246 kwsys_stl::string ParseValueFromKStat(const char* arguments);
247 kwsys_stl::string RunProcess(kwsys_stl::vector<const char*> args);
249 //For Haiku OS
250 bool QueryHaikuInfo();
252 // Evaluate the memory information.
253 int QueryMemory();
254 unsigned long TotalVirtualMemory;
255 unsigned long AvailableVirtualMemory;
256 unsigned long TotalPhysicalMemory;
257 unsigned long AvailablePhysicalMemory;
259 size_t CurrentPositionInFile;
261 // Operating System information
262 bool QueryOSInformation();
263 kwsys_stl::string OSName;
264 kwsys_stl::string Hostname;
265 kwsys_stl::string OSRelease;
266 kwsys_stl::string OSVersion;
267 kwsys_stl::string OSPlatform;
274 SystemInformation::SystemInformation()
276 this->Implementation = new SystemInformationImplementation;
279 SystemInformation::~SystemInformation ()
281 delete this->Implementation;
284 const char * SystemInformation::GetVendorString()
286 return this->Implementation->GetVendorString();
288 const char * SystemInformation::GetVendorID()
290 return this->Implementation->GetVendorID();
292 kwsys_stl::string SystemInformation::GetTypeID()
294 return this->Implementation->GetTypeID();
296 kwsys_stl::string SystemInformation::GetFamilyID()
298 return this->Implementation->GetFamilyID();
300 kwsys_stl::string SystemInformation::GetModelID()
302 return this->Implementation->GetModelID();
304 kwsys_stl::string SystemInformation::GetSteppingCode()
306 return this->Implementation->GetSteppingCode();
308 const char * SystemInformation::GetExtendedProcessorName()
310 return this->Implementation->GetExtendedProcessorName();
312 const char * SystemInformation::GetProcessorSerialNumber()
314 return this->Implementation->GetProcessorSerialNumber();
316 int SystemInformation::GetProcessorCacheSize()
318 return this->Implementation->GetProcessorCacheSize();
320 unsigned int SystemInformation::GetLogicalProcessorsPerPhysical()
322 return this->Implementation->GetLogicalProcessorsPerPhysical();
324 float SystemInformation::GetProcessorClockFrequency()
326 return this->Implementation->GetProcessorClockFrequency();
328 int SystemInformation::GetProcessorAPICID()
330 return this->Implementation->GetProcessorAPICID();
332 int SystemInformation::GetProcessorCacheXSize(long int l)
334 return this->Implementation->GetProcessorCacheXSize(l);
336 bool SystemInformation::DoesCPUSupportFeature(long int i)
338 return this->Implementation->DoesCPUSupportFeature(i);
341 const char * SystemInformation::GetOSName()
343 return this->Implementation->GetOSName();
345 const char * SystemInformation::GetHostname()
347 return this->Implementation->GetHostname();
349 const char * SystemInformation::GetOSRelease()
351 return this->Implementation->GetOSRelease();
353 const char * SystemInformation::GetOSVersion()
355 return this->Implementation->GetOSVersion();
357 const char * SystemInformation::GetOSPlatform()
359 return this->Implementation->GetOSPlatform();
362 bool SystemInformation::Is64Bits()
364 return this->Implementation->Is64Bits();
367 unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
369 return this->Implementation->GetNumberOfLogicalCPU();
371 unsigned int SystemInformation::GetNumberOfPhysicalCPU()
373 return this->Implementation->GetNumberOfPhysicalCPU();
376 bool SystemInformation::DoesCPUSupportCPUID()
378 return this->Implementation->DoesCPUSupportCPUID();
381 // Retrieve memory information in megabyte.
382 unsigned long SystemInformation::GetTotalVirtualMemory()
384 return this->Implementation->GetTotalVirtualMemory();
386 unsigned long SystemInformation::GetAvailableVirtualMemory()
388 return this->Implementation->GetAvailableVirtualMemory();
390 unsigned long SystemInformation::GetTotalPhysicalMemory()
392 return this->Implementation->GetTotalPhysicalMemory();
395 unsigned long SystemInformation::GetAvailablePhysicalMemory()
397 return this->Implementation->GetAvailablePhysicalMemory();
400 /** Run the different checks */
401 void SystemInformation::RunCPUCheck()
403 this->Implementation->RunCPUCheck();
405 void SystemInformation::RunOSCheck()
407 this->Implementation->RunOSCheck();
409 void SystemInformation::RunMemoryCheck()
411 this->Implementation->RunMemoryCheck();
416 // --------------------------------------------------------------
417 // SystemInformationImplementation starts here
419 #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64)
420 #define USE_ASM_INSTRUCTIONS 1
421 #else
422 #define USE_ASM_INSTRUCTIONS 0
423 #endif
425 #define STORE_TLBCACHE_INFO(x,y) x = (x < y) ? y : x
426 #define TLBCACHE_INFO_UNITS (15)
427 #define CLASSICAL_CPU_FREQ_LOOP 10000000
428 #define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
430 #define CPUID_AWARE_COMPILER
431 #ifdef CPUID_AWARE_COMPILER
432 #define CPUID_INSTRUCTION cpuid
433 #else
434 #define CPUID_INSTRUCTION _asm _emit 0x0f _asm _emit 0xa2
435 #endif
437 #define MMX_FEATURE 0x00000001
438 #define MMX_PLUS_FEATURE 0x00000002
439 #define SSE_FEATURE 0x00000004
440 #define SSE2_FEATURE 0x00000008
441 #define AMD_3DNOW_FEATURE 0x00000010
442 #define AMD_3DNOW_PLUS_FEATURE 0x00000020
443 #define IA64_FEATURE 0x00000040
444 #define MP_CAPABLE 0x00000080
445 #define HYPERTHREAD_FEATURE 0x00000100
446 #define SERIALNUMBER_FEATURE 0x00000200
447 #define APIC_FEATURE 0x00000400
448 #define SSE_FP_FEATURE 0x00000800
449 #define SSE_MMX_FEATURE 0x00001000
450 #define CMOV_FEATURE 0x00002000
451 #define MTRR_FEATURE 0x00004000
452 #define L1CACHE_FEATURE 0x00008000
453 #define L2CACHE_FEATURE 0x00010000
454 #define L3CACHE_FEATURE 0x00020000
455 #define ACPI_FEATURE 0x00040000
456 #define THERMALMONITOR_FEATURE 0x00080000
457 #define TEMPSENSEDIODE_FEATURE 0x00100000
458 #define FREQUENCYID_FEATURE 0x00200000
459 #define VOLTAGEID_FREQUENCY 0x00400000
461 // Status Flag
462 #define HT_NOT_CAPABLE 0
463 #define HT_ENABLED 1
464 #define HT_DISABLED 2
465 #define HT_SUPPORTED_NOT_ENABLED 3
466 #define HT_CANNOT_DETECT 4
468 // EDX[28] Bit 28 is set if HT is supported
469 #define HT_BIT 0x10000000
471 // EAX[11:8] Bit 8-11 contains family processor ID.
472 #define FAMILY_ID 0x0F00
473 #define PENTIUM4_ID 0x0F00
474 // EAX[23:20] Bit 20-23 contains extended family processor ID
475 #define EXT_FAMILY_ID 0x0F00000
476 // EBX[23:16] Bit 16-23 in ebx contains the number of logical
477 #define NUM_LOGICAL_BITS 0x00FF0000
478 // processors per physical processor when execute cpuid with
479 // eax set to 1
480 // EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
481 #define INITIAL_APIC_ID_BITS 0xFF000000
482 // initial APIC ID for the processor this code is running on.
483 // Default value = 0xff if HT is not supported
487 SystemInformationImplementation::SystemInformationImplementation()
489 this->TotalVirtualMemory = 0;
490 this->AvailableVirtualMemory = 0;
491 this->TotalPhysicalMemory = 0;
492 this->AvailablePhysicalMemory = 0;
493 this->CurrentPositionInFile = 0;
494 this->ChipManufacturer = UnknownManufacturer;
495 memset(&this->Features, 0, sizeof(CPUFeatures));
496 memset(&this->ChipID, 0, sizeof(ID));
497 this->CPUSpeedInMHz = 0;
498 this->NumberOfLogicalCPU = 0;
499 this->NumberOfPhysicalCPU = 0;
500 this->OSName = "";
501 this->Hostname = "";
502 this->OSRelease = "";
503 this->OSVersion = "";
504 this->OSPlatform = "";
507 SystemInformationImplementation::~SystemInformationImplementation()
511 void SystemInformationImplementation::RunCPUCheck()
513 #ifdef WIN32
514 // Check to see if this processor supports CPUID.
515 if (DoesCPUSupportCPUID())
517 // Retrieve the CPU details.
518 RetrieveCPUIdentity();
519 RetrieveCPUFeatures();
520 if (!RetrieveCPUClockSpeed())
522 RetrieveClassicalCPUClockSpeed();
525 // Attempt to retrieve cache information.
526 if (!RetrieveCPUCacheDetails())
528 RetrieveClassicalCPUCacheDetails();
530 // Retrieve the extended CPU details.
531 if (!RetrieveExtendedCPUIdentity())
533 RetrieveClassicalCPUIdentity();
535 RetrieveExtendedCPUFeatures();
537 // Now attempt to retrieve the serial number (if possible).
538 RetrieveProcessorSerialNumber();
540 this->CPUCount();
541 #elif defined(__APPLE__)
542 this->ParseSysCtl();
543 #elif defined (__SVR4) && defined (__sun)
544 this->QuerySolarisInfo();
545 #elif defined(__HAIKU__)
546 this->QueryHaikuInfo();
547 #else
548 this->RetreiveInformationFromCpuInfoFile();
549 #endif
552 void SystemInformationImplementation::RunOSCheck()
554 this->QueryOSInformation();
557 void SystemInformationImplementation::RunMemoryCheck()
559 #if defined(__APPLE__)
560 this->ParseSysCtl();
561 #elif defined (__SVR4) && defined (__sun)
562 this->QuerySolarisInfo();
563 #elif defined(__HAIKU__)
564 this->QueryHaikuInfo();
565 #else
566 this->QueryMemory();
567 #endif
570 /** Get the vendor string */
571 const char * SystemInformationImplementation::GetVendorString()
573 return this->ChipID.Vendor;
576 /** Get the OS Name */
577 const char * SystemInformationImplementation::GetOSName()
579 return this->OSName.c_str();
582 /** Get the hostname */
583 const char* SystemInformationImplementation::GetHostname()
585 return this->Hostname.c_str();
588 /** Get the OS release */
589 const char* SystemInformationImplementation::GetOSRelease()
591 return this->OSRelease.c_str();
594 /** Get the OS version */
595 const char* SystemInformationImplementation::GetOSVersion()
597 return this->OSVersion.c_str();
600 /** Get the OS platform */
601 const char* SystemInformationImplementation::GetOSPlatform()
603 return this->OSPlatform.c_str();
606 /** Get the vendor ID */
607 const char * SystemInformationImplementation::GetVendorID()
609 // Return the vendor ID.
610 switch (this->ChipManufacturer)
612 case Intel:
613 return "Intel Corporation";
614 case AMD:
615 return "Advanced Micro Devices";
616 case NSC:
617 return "National Semiconductor";
618 case Cyrix:
619 return "Cyrix Corp., VIA Inc.";
620 case NexGen:
621 return "NexGen Inc., Advanced Micro Devices";
622 case IDT:
623 return "IDT\\Centaur, Via Inc.";
624 case UMC:
625 return "United Microelectronics Corp.";
626 case Rise:
627 return "Rise";
628 case Transmeta:
629 return "Transmeta";
630 case Sun:
631 return "Sun Microelectronics";
632 default:
633 return "Unknown Manufacturer";
637 /** Return the type ID of the CPU */
638 kwsys_stl::string SystemInformationImplementation::GetTypeID()
640 kwsys_ios::ostringstream str;
641 str << this->ChipID.Type;
642 return str.str();
645 /** Return the family of the CPU present */
646 kwsys_stl::string SystemInformationImplementation::GetFamilyID()
648 kwsys_ios::ostringstream str;
649 str << this->ChipID.Family;
650 return str.str();
653 // Return the model of CPU present */
654 kwsys_stl::string SystemInformationImplementation::GetModelID()
656 kwsys_ios::ostringstream str;
657 str << this->ChipID.Model;
658 return str.str();
661 /** Return the stepping code of the CPU present. */
662 kwsys_stl::string SystemInformationImplementation::GetSteppingCode()
664 kwsys_ios::ostringstream str;
665 str << this->ChipID.Revision;
666 return str.str();
669 /** Return the stepping code of the CPU present. */
670 const char * SystemInformationImplementation::GetExtendedProcessorName()
672 return this->ChipID.ProcessorName;
675 /** Return the serial number of the processor
676 * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
677 const char * SystemInformationImplementation::GetProcessorSerialNumber()
679 return this->ChipID.SerialNumber;
682 /** Return the logical processors per physical */
683 unsigned int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
685 return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
688 /** Return the processor clock frequency. */
689 float SystemInformationImplementation::GetProcessorClockFrequency()
691 return this->CPUSpeedInMHz;
694 /** Return the APIC ID. */
695 int SystemInformationImplementation::GetProcessorAPICID()
697 return this->Features.ExtendedFeatures.APIC_ID;
700 /** Return the L1 cache size. */
701 int SystemInformationImplementation::GetProcessorCacheSize()
703 return this->Features.L1CacheSize;
706 /** Return the chosen cache size. */
707 int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
709 switch (dwCacheID)
711 case L1CACHE_FEATURE:
712 return this->Features.L1CacheSize;
713 case L2CACHE_FEATURE:
714 return this->Features.L2CacheSize;
715 case L3CACHE_FEATURE:
716 return this->Features.L3CacheSize;
718 return -1;
721 bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
723 bool bHasFeature = false;
725 // Check for MMX instructions.
726 if (((dwFeature & MMX_FEATURE) != 0) && this->Features.HasMMX) bHasFeature = true;
728 // Check for MMX+ instructions.
729 if (((dwFeature & MMX_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.HasMMXPlus) bHasFeature = true;
731 // Check for SSE FP instructions.
732 if (((dwFeature & SSE_FEATURE) != 0) && this->Features.HasSSE) bHasFeature = true;
734 // Check for SSE FP instructions.
735 if (((dwFeature & SSE_FP_FEATURE) != 0) && this->Features.HasSSEFP) bHasFeature = true;
737 // Check for SSE MMX instructions.
738 if (((dwFeature & SSE_MMX_FEATURE) != 0) && this->Features.ExtendedFeatures.HasSSEMMX) bHasFeature = true;
740 // Check for SSE2 instructions.
741 if (((dwFeature & SSE2_FEATURE) != 0) && this->Features.HasSSE2) bHasFeature = true;
743 // Check for 3DNow! instructions.
744 if (((dwFeature & AMD_3DNOW_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNow) bHasFeature = true;
746 // Check for 3DNow+ instructions.
747 if (((dwFeature & AMD_3DNOW_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNowPlus) bHasFeature = true;
749 // Check for IA64 instructions.
750 if (((dwFeature & IA64_FEATURE) != 0) && this->Features.HasIA64) bHasFeature = true;
752 // Check for MP capable.
753 if (((dwFeature & MP_CAPABLE) != 0) && this->Features.ExtendedFeatures.SupportsMP) bHasFeature = true;
755 // Check for a serial number for the processor.
756 if (((dwFeature & SERIALNUMBER_FEATURE) != 0) && this->Features.HasSerial) bHasFeature = true;
758 // Check for a local APIC in the processor.
759 if (((dwFeature & APIC_FEATURE) != 0) && this->Features.HasAPIC) bHasFeature = true;
761 // Check for CMOV instructions.
762 if (((dwFeature & CMOV_FEATURE) != 0) && this->Features.HasCMOV) bHasFeature = true;
764 // Check for MTRR instructions.
765 if (((dwFeature & MTRR_FEATURE) != 0) && this->Features.HasMTRR) bHasFeature = true;
767 // Check for L1 cache size.
768 if (((dwFeature & L1CACHE_FEATURE) != 0) && (this->Features.L1CacheSize != -1)) bHasFeature = true;
770 // Check for L2 cache size.
771 if (((dwFeature & L2CACHE_FEATURE) != 0) && (this->Features.L2CacheSize != -1)) bHasFeature = true;
773 // Check for L3 cache size.
774 if (((dwFeature & L3CACHE_FEATURE) != 0) && (this->Features.L3CacheSize != -1)) bHasFeature = true;
776 // Check for ACPI capability.
777 if (((dwFeature & ACPI_FEATURE) != 0) && this->Features.HasACPI) bHasFeature = true;
779 // Check for thermal monitor support.
780 if (((dwFeature & THERMALMONITOR_FEATURE) != 0) && this->Features.HasThermal) bHasFeature = true;
782 // Check for temperature sensing diode support.
783 if (((dwFeature & TEMPSENSEDIODE_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode) bHasFeature = true;
785 // Check for frequency ID support.
786 if (((dwFeature & FREQUENCYID_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID) bHasFeature = true;
788 // Check for voltage ID support.
789 if (((dwFeature & VOLTAGEID_FREQUENCY) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasVoltageID) bHasFeature = true;
791 return bHasFeature;
794 void SystemInformationImplementation::Delay(unsigned int uiMS)
796 #ifdef WIN32
797 LARGE_INTEGER Frequency, StartCounter, EndCounter;
798 __int64 x;
800 // Get the frequency of the high performance counter.
801 if (!QueryPerformanceFrequency (&Frequency)) return;
802 x = Frequency.QuadPart / 1000 * uiMS;
804 // Get the starting position of the counter.
805 QueryPerformanceCounter (&StartCounter);
807 do {
808 // Get the ending position of the counter.
809 QueryPerformanceCounter (&EndCounter);
810 } while (EndCounter.QuadPart - StartCounter.QuadPart < x);
811 #endif
812 (void)uiMS;
815 bool SystemInformationImplementation::DoesCPUSupportCPUID()
817 #if USE_ASM_INSTRUCTIONS
818 // Use SEH to determine CPUID presence
819 __try {
820 _asm {
821 #ifdef CPUID_AWARE_COMPILER
822 ; we must push/pop the registers <<CPUID>> writes to, as the
823 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
824 ; these registers to change.
825 push eax
826 push ebx
827 push ecx
828 push edx
829 #endif
830 ; <<CPUID>>
831 mov eax, 0
832 CPUID_INSTRUCTION
834 #ifdef CPUID_AWARE_COMPILER
835 pop edx
836 pop ecx
837 pop ebx
838 pop eax
839 #endif
842 __except(1)
844 // Stop the class from trying to use CPUID again!
845 return false;
848 // The cpuid instruction succeeded.
849 return true;
850 #else
851 // Assume no cpuid instruction.
852 return false;
853 #endif
856 bool SystemInformationImplementation::RetrieveCPUFeatures()
858 #if USE_ASM_INSTRUCTIONS
859 int localCPUFeatures = 0;
860 int localCPUAdvanced = 0;
863 // Use assembly to detect CPUID information...
864 __try {
865 _asm {
866 #ifdef CPUID_AWARE_COMPILER
867 ; we must push/pop the registers <<CPUID>> writes to, as the
868 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
869 ; these registers to change.
870 push eax
871 push ebx
872 push ecx
873 push edx
874 #endif
875 ; <<CPUID>>
876 ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
877 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
878 ; edx: CPU feature flags
879 mov eax,1
880 CPUID_INSTRUCTION
881 mov localCPUFeatures, edx
882 mov localCPUAdvanced, ebx
884 #ifdef CPUID_AWARE_COMPILER
885 pop edx
886 pop ecx
887 pop ebx
888 pop eax
889 #endif
892 __except(1)
894 return false;
897 // Retrieve the features of CPU present.
898 this->Features.HasFPU = ((localCPUFeatures & 0x00000001) != 0); // FPU Present --> Bit 0
899 this->Features.HasTSC = ((localCPUFeatures & 0x00000010) != 0); // TSC Present --> Bit 4
900 this->Features.HasAPIC = ((localCPUFeatures & 0x00000200) != 0); // APIC Present --> Bit 9
901 this->Features.HasMTRR = ((localCPUFeatures & 0x00001000) != 0); // MTRR Present --> Bit 12
902 this->Features.HasCMOV = ((localCPUFeatures & 0x00008000) != 0); // CMOV Present --> Bit 15
903 this->Features.HasSerial = ((localCPUFeatures & 0x00040000) != 0); // Serial Present --> Bit 18
904 this->Features.HasACPI = ((localCPUFeatures & 0x00400000) != 0); // ACPI Capable --> Bit 22
905 this->Features.HasMMX = ((localCPUFeatures & 0x00800000) != 0); // MMX Present --> Bit 23
906 this->Features.HasSSE = ((localCPUFeatures & 0x02000000) != 0); // SSE Present --> Bit 25
907 this->Features.HasSSE2 = ((localCPUFeatures & 0x04000000) != 0); // SSE2 Present --> Bit 26
908 this->Features.HasThermal = ((localCPUFeatures & 0x20000000) != 0); // Thermal Monitor Present --> Bit 29
909 this->Features.HasIA64 = ((localCPUFeatures & 0x40000000) != 0); // IA64 Present --> Bit 30
911 // Retrieve extended SSE capabilities if SSE is available.
912 if (this->Features.HasSSE) {
914 // Attempt to __try some SSE FP instructions.
915 __try
917 // Perform: orps xmm0, xmm0
918 _asm
920 _emit 0x0f
921 _emit 0x56
922 _emit 0xc0
925 // SSE FP capable processor.
926 this->Features.HasSSEFP = true;
928 __except(1)
930 // bad instruction - processor or OS cannot handle SSE FP.
931 this->Features.HasSSEFP = false;
934 else
936 // Set the advanced SSE capabilities to not available.
937 this->Features.HasSSEFP = false;
940 // Retrieve Intel specific extended features.
941 if (this->ChipManufacturer == Intel)
943 this->Features.ExtendedFeatures.SupportsHyperthreading = ((localCPUFeatures & 0x10000000) != 0); // Intel specific: Hyperthreading --> Bit 28
944 this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = (this->Features.ExtendedFeatures.SupportsHyperthreading) ? ((localCPUAdvanced & 0x00FF0000) >> 16) : 1;
946 if ((this->Features.ExtendedFeatures.SupportsHyperthreading) && (this->Features.HasAPIC))
948 // Retrieve APIC information if there is one present.
949 this->Features.ExtendedFeatures.APIC_ID = ((localCPUAdvanced & 0xFF000000) >> 24);
952 #endif
953 return true;
957 /** Find the manufacturer given the vendor id */
958 void SystemInformationImplementation::FindManufacturer()
960 if (strcmp (this->ChipID.Vendor, "GenuineIntel") == 0) this->ChipManufacturer = Intel; // Intel Corp.
961 else if (strcmp (this->ChipID.Vendor, "UMC UMC UMC ") == 0) this->ChipManufacturer = UMC; // United Microelectronics Corp.
962 else if (strcmp (this->ChipID.Vendor, "AuthenticAMD") == 0) this->ChipManufacturer = AMD; // Advanced Micro Devices
963 else if (strcmp (this->ChipID.Vendor, "AMD ISBETTER") == 0) this->ChipManufacturer = AMD; // Advanced Micro Devices (1994)
964 else if (strcmp (this->ChipID.Vendor, "CyrixInstead") == 0) this->ChipManufacturer = Cyrix; // Cyrix Corp., VIA Inc.
965 else if (strcmp (this->ChipID.Vendor, "NexGenDriven") == 0) this->ChipManufacturer = NexGen; // NexGen Inc. (now AMD)
966 else if (strcmp (this->ChipID.Vendor, "CentaurHauls") == 0) this->ChipManufacturer = IDT; // IDT/Centaur (now VIA)
967 else if (strcmp (this->ChipID.Vendor, "RiseRiseRise") == 0) this->ChipManufacturer = Rise; // Rise
968 else if (strcmp (this->ChipID.Vendor, "GenuineTMx86") == 0) this->ChipManufacturer = Transmeta; // Transmeta
969 else if (strcmp (this->ChipID.Vendor, "TransmetaCPU") == 0) this->ChipManufacturer = Transmeta; // Transmeta
970 else if (strcmp (this->ChipID.Vendor, "Geode By NSC") == 0) this->ChipManufacturer = NSC; // National Semiconductor
971 else if (strcmp (this->ChipID.Vendor, "Sun") == 0) this->ChipManufacturer = Sun; // Sun Microelectronics
972 else this->ChipManufacturer = UnknownManufacturer; // Unknown manufacturer
975 /** */
976 bool SystemInformationImplementation::RetrieveCPUIdentity()
978 #if USE_ASM_INSTRUCTIONS
979 int localCPUVendor[3];
980 int localCPUSignature;
982 // Use assembly to detect CPUID information...
983 __try
985 _asm
987 #ifdef CPUID_AWARE_COMPILER
988 ; we must push/pop the registers <<CPUID>> writes to, as the
989 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
990 ; these registers to change.
991 push eax
992 push ebx
993 push ecx
994 push edx
995 #endif
996 ; <<CPUID>>
997 ; eax = 0 --> eax: maximum value of CPUID instruction.
998 ; ebx: part 1 of 3; CPU signature.
999 ; edx: part 2 of 3; CPU signature.
1000 ; ecx: part 3 of 3; CPU signature.
1001 mov eax, 0
1002 CPUID_INSTRUCTION
1003 mov localCPUVendor[0 * TYPE int], ebx
1004 mov localCPUVendor[1 * TYPE int], edx
1005 mov localCPUVendor[2 * TYPE int], ecx
1007 ; <<CPUID>>
1008 ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
1009 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
1010 ; edx: CPU feature flags
1011 mov eax,1
1012 CPUID_INSTRUCTION
1013 mov localCPUSignature, eax
1015 #ifdef CPUID_AWARE_COMPILER
1016 pop edx
1017 pop ecx
1018 pop ebx
1019 pop eax
1020 #endif
1023 __except(1)
1025 return false;
1028 // Process the returned information.
1029 memcpy (this->ChipID.Vendor, &(localCPUVendor[0]), sizeof (int));
1030 memcpy (&(this->ChipID.Vendor[4]), &(localCPUVendor[1]), sizeof (int));
1031 memcpy (&(this->ChipID.Vendor[8]), &(localCPUVendor[2]), sizeof (int));
1032 this->ChipID.Vendor[12] = '\0';
1034 this->FindManufacturer();
1036 // Retrieve the family of CPU present.
1037 this->ChipID.ExtendedFamily = ((localCPUSignature & 0x0FF00000) >> 20); // Bits 27..20 Used
1038 this->ChipID.ExtendedModel = ((localCPUSignature & 0x000F0000) >> 16); // Bits 19..16 Used
1039 this->ChipID.Type = ((localCPUSignature & 0x0000F000) >> 12); // Bits 15..12 Used
1040 this->ChipID.Family = ((localCPUSignature & 0x00000F00) >> 8); // Bits 11..8 Used
1041 this->ChipID.Model = ((localCPUSignature & 0x000000F0) >> 4); // Bits 7..4 Used
1042 this->ChipID.Revision = ((localCPUSignature & 0x0000000F) >> 0); // Bits 3..0 Used
1043 #endif
1045 return true;
1048 /** */
1049 bool SystemInformationImplementation::RetrieveCPUCacheDetails()
1051 #if USE_ASM_INSTRUCTIONS
1052 int L1Cache[4] = { 0, 0, 0, 0 };
1053 int L2Cache[4] = { 0, 0, 0, 0 };
1055 // Check to see if what we are about to do is supported...
1056 if (RetrieveCPUExtendedLevelSupport (0x80000005))
1058 // Use assembly to retrieve the L1 cache information ...
1059 __try
1061 _asm
1063 #ifdef CPUID_AWARE_COMPILER
1064 ; we must push/pop the registers <<CPUID>> writes to, as the
1065 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1066 ; these registers to change.
1067 push eax
1068 push ebx
1069 push ecx
1070 push edx
1071 #endif
1072 ; <<CPUID>>
1073 ; eax = 0x80000005 --> eax: L1 cache information - Part 1 of 4.
1074 ; ebx: L1 cache information - Part 2 of 4.
1075 ; edx: L1 cache information - Part 3 of 4.
1076 ; ecx: L1 cache information - Part 4 of 4.
1077 mov eax, 0x80000005
1078 CPUID_INSTRUCTION
1079 mov L1Cache[0 * TYPE int], eax
1080 mov L1Cache[1 * TYPE int], ebx
1081 mov L1Cache[2 * TYPE int], ecx
1082 mov L1Cache[3 * TYPE int], edx
1084 #ifdef CPUID_AWARE_COMPILER
1085 pop edx
1086 pop ecx
1087 pop ebx
1088 pop eax
1089 #endif
1092 __except(1)
1094 return false;
1096 // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as data cache size from edx: bits 31..24.
1097 this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24);
1098 this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24);
1100 else
1102 // Store -1 to indicate the cache could not be queried.
1103 this->Features.L1CacheSize = -1;
1106 // Check to see if what we are about to do is supported...
1107 if (RetrieveCPUExtendedLevelSupport (0x80000006))
1109 // Use assembly to retrieve the L2 cache information ...
1110 __try
1112 _asm
1114 #ifdef CPUID_AWARE_COMPILER
1115 ; we must push/pop the registers <<CPUID>> writes to, as the
1116 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1117 ; these registers to change.
1118 push eax
1119 push ebx
1120 push ecx
1121 push edx
1122 #endif
1123 ; <<CPUID>>
1124 ; eax = 0x80000006 --> eax: L2 cache information - Part 1 of 4.
1125 ; ebx: L2 cache information - Part 2 of 4.
1126 ; edx: L2 cache information - Part 3 of 4.
1127 ; ecx: L2 cache information - Part 4 of 4.
1128 mov eax, 0x80000006
1129 CPUID_INSTRUCTION
1130 mov L2Cache[0 * TYPE int], eax
1131 mov L2Cache[1 * TYPE int], ebx
1132 mov L2Cache[2 * TYPE int], ecx
1133 mov L2Cache[3 * TYPE int], edx
1135 #ifdef CPUID_AWARE_COMPILER
1136 pop edx
1137 pop ecx
1138 pop ebx
1139 pop eax
1140 #endif
1143 __except(1)
1145 return false;
1147 // Save the L2 unified cache size (in KB) from ecx: bits 31..16.
1148 this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16);
1150 else
1152 // Store -1 to indicate the cache could not be queried.
1153 this->Features.L2CacheSize = -1;
1156 // Define L3 as being not present as we cannot test for it.
1157 this->Features.L3CacheSize = -1;
1159 #endif
1161 // Return failure if we cannot detect either cache with this method.
1162 return ((this->Features.L1CacheSize == -1) && (this->Features.L2CacheSize == -1)) ? false : true;
1165 /** */
1166 bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails()
1168 #if USE_ASM_INSTRUCTIONS
1169 int TLBCode = -1, TLBData = -1, L1Code = -1, L1Data = -1, L1Trace = -1, L2Unified = -1, L3Unified = -1;
1170 int TLBCacheData[4] = { 0, 0, 0, 0 };
1171 int TLBPassCounter = 0;
1172 int TLBCacheUnit = 0;
1175 do {
1176 // Use assembly to retrieve the L2 cache information ...
1177 __try {
1178 _asm {
1179 #ifdef CPUID_AWARE_COMPILER
1180 ; we must push/pop the registers <<CPUID>> writes to, as the
1181 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1182 ; these registers to change.
1183 push eax
1184 push ebx
1185 push ecx
1186 push edx
1187 #endif
1188 ; <<CPUID>>
1189 ; eax = 2 --> eax: TLB and cache information - Part 1 of 4.
1190 ; ebx: TLB and cache information - Part 2 of 4.
1191 ; ecx: TLB and cache information - Part 3 of 4.
1192 ; edx: TLB and cache information - Part 4 of 4.
1193 mov eax, 2
1194 CPUID_INSTRUCTION
1195 mov TLBCacheData[0 * TYPE int], eax
1196 mov TLBCacheData[1 * TYPE int], ebx
1197 mov TLBCacheData[2 * TYPE int], ecx
1198 mov TLBCacheData[3 * TYPE int], edx
1200 #ifdef CPUID_AWARE_COMPILER
1201 pop edx
1202 pop ecx
1203 pop ebx
1204 pop eax
1205 #endif
1208 __except(1)
1210 return false;
1213 int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16);
1214 (void)bob;
1215 // Process the returned TLB and cache information.
1216 for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++)
1218 // First of all - decide which unit we are dealing with.
1219 switch (nCounter)
1221 // eax: bits 8..15 : bits 16..23 : bits 24..31
1222 case 0: TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8); break;
1223 case 1: TLBCacheUnit = ((TLBCacheData[0] & 0x00FF0000) >> 16); break;
1224 case 2: TLBCacheUnit = ((TLBCacheData[0] & 0xFF000000) >> 24); break;
1226 // ebx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1227 case 3: TLBCacheUnit = ((TLBCacheData[1] & 0x000000FF) >> 0); break;
1228 case 4: TLBCacheUnit = ((TLBCacheData[1] & 0x0000FF00) >> 8); break;
1229 case 5: TLBCacheUnit = ((TLBCacheData[1] & 0x00FF0000) >> 16); break;
1230 case 6: TLBCacheUnit = ((TLBCacheData[1] & 0xFF000000) >> 24); break;
1232 // ecx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1233 case 7: TLBCacheUnit = ((TLBCacheData[2] & 0x000000FF) >> 0); break;
1234 case 8: TLBCacheUnit = ((TLBCacheData[2] & 0x0000FF00) >> 8); break;
1235 case 9: TLBCacheUnit = ((TLBCacheData[2] & 0x00FF0000) >> 16); break;
1236 case 10: TLBCacheUnit = ((TLBCacheData[2] & 0xFF000000) >> 24); break;
1238 // edx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1239 case 11: TLBCacheUnit = ((TLBCacheData[3] & 0x000000FF) >> 0); break;
1240 case 12: TLBCacheUnit = ((TLBCacheData[3] & 0x0000FF00) >> 8); break;
1241 case 13: TLBCacheUnit = ((TLBCacheData[3] & 0x00FF0000) >> 16); break;
1242 case 14: TLBCacheUnit = ((TLBCacheData[3] & 0xFF000000) >> 24); break;
1244 // Default case - an error has occured.
1245 default: return false;
1248 // Now process the resulting unit to see what it means....
1249 switch (TLBCacheUnit)
1251 case 0x00: break;
1252 case 0x01: STORE_TLBCACHE_INFO (TLBCode, 4); break;
1253 case 0x02: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1254 case 0x03: STORE_TLBCACHE_INFO (TLBData, 4); break;
1255 case 0x04: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1256 case 0x06: STORE_TLBCACHE_INFO (L1Code, 8); break;
1257 case 0x08: STORE_TLBCACHE_INFO (L1Code, 16); break;
1258 case 0x0a: STORE_TLBCACHE_INFO (L1Data, 8); break;
1259 case 0x0c: STORE_TLBCACHE_INFO (L1Data, 16); break;
1260 case 0x10: STORE_TLBCACHE_INFO (L1Data, 16); break; // <-- FIXME: IA-64 Only
1261 case 0x15: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
1262 case 0x1a: STORE_TLBCACHE_INFO (L2Unified, 96); break; // <-- FIXME: IA-64 Only
1263 case 0x22: STORE_TLBCACHE_INFO (L3Unified, 512); break;
1264 case 0x23: STORE_TLBCACHE_INFO (L3Unified, 1024); break;
1265 case 0x25: STORE_TLBCACHE_INFO (L3Unified, 2048); break;
1266 case 0x29: STORE_TLBCACHE_INFO (L3Unified, 4096); break;
1267 case 0x39: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1268 case 0x3c: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1269 case 0x40: STORE_TLBCACHE_INFO (L2Unified, 0); break; // <-- FIXME: No integrated L2 cache (P6 core) or L3 cache (P4 core).
1270 case 0x41: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1271 case 0x42: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1272 case 0x43: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1273 case 0x44: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1274 case 0x45: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
1275 case 0x50: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1276 case 0x51: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1277 case 0x52: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1278 case 0x5b: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1279 case 0x5c: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1280 case 0x5d: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1281 case 0x66: STORE_TLBCACHE_INFO (L1Data, 8); break;
1282 case 0x67: STORE_TLBCACHE_INFO (L1Data, 16); break;
1283 case 0x68: STORE_TLBCACHE_INFO (L1Data, 32); break;
1284 case 0x70: STORE_TLBCACHE_INFO (L1Trace, 12); break;
1285 case 0x71: STORE_TLBCACHE_INFO (L1Trace, 16); break;
1286 case 0x72: STORE_TLBCACHE_INFO (L1Trace, 32); break;
1287 case 0x77: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
1288 case 0x79: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1289 case 0x7a: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1290 case 0x7b: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1291 case 0x7c: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1292 case 0x7e: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1293 case 0x81: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1294 case 0x82: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1295 case 0x83: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1296 case 0x84: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1297 case 0x85: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
1298 case 0x88: STORE_TLBCACHE_INFO (L3Unified, 2048); break; // <-- FIXME: IA-64 Only
1299 case 0x89: STORE_TLBCACHE_INFO (L3Unified, 4096); break; // <-- FIXME: IA-64 Only
1300 case 0x8a: STORE_TLBCACHE_INFO (L3Unified, 8192); break; // <-- FIXME: IA-64 Only
1301 case 0x8d: STORE_TLBCACHE_INFO (L3Unified, 3096); break; // <-- FIXME: IA-64 Only
1302 case 0x90: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1303 case 0x96: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1304 case 0x9b: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1306 // Default case - an error has occured.
1307 default: return false;
1311 // Increment the TLB pass counter.
1312 TLBPassCounter ++;
1313 } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter);
1315 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1316 if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1))
1318 this->Features.L1CacheSize = -1;
1320 else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1))
1322 this->Features.L1CacheSize = L1Trace;
1324 else if ((L1Code != -1) && (L1Data == -1))
1326 this->Features.L1CacheSize = L1Code;
1328 else if ((L1Code == -1) && (L1Data != -1))
1330 this->Features.L1CacheSize = L1Data;
1332 else if ((L1Code != -1) && (L1Data != -1))
1334 this->Features.L1CacheSize = L1Code + L1Data;
1336 else
1338 this->Features.L1CacheSize = -1;
1341 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1342 if (L2Unified == -1)
1344 this->Features.L2CacheSize = -1;
1346 else
1348 this->Features.L2CacheSize = L2Unified;
1351 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1352 if (L3Unified == -1)
1354 this->Features.L3CacheSize = -1;
1356 else
1358 this->Features.L3CacheSize = L3Unified;
1361 #endif
1362 return true;
1365 /** */
1366 bool SystemInformationImplementation::RetrieveCPUClockSpeed()
1368 #if _WIN32
1369 // First of all we check to see if the RDTSC (0x0F, 0x31) instruction is supported.
1370 if (!this->Features.HasTSC)
1372 return false;
1375 unsigned int uiRepetitions = 1;
1376 unsigned int uiMSecPerRepetition = 50;
1377 __int64 i64Total = 0;
1378 __int64 i64Overhead = 0;
1380 for (unsigned int nCounter = 0; nCounter < uiRepetitions; nCounter ++)
1382 i64Total += GetCyclesDifference (SystemInformationImplementation::Delay,
1383 uiMSecPerRepetition);
1384 i64Overhead +=
1385 GetCyclesDifference (SystemInformationImplementation::DelayOverhead,
1386 uiMSecPerRepetition);
1389 // Calculate the MHz speed.
1390 i64Total -= i64Overhead;
1391 i64Total /= uiRepetitions;
1392 i64Total /= uiMSecPerRepetition;
1393 i64Total /= 1000;
1395 // Save the CPU speed.
1396 this->CPUSpeedInMHz = (float) i64Total;
1398 return true;
1399 #else
1400 return false;
1401 #endif
1404 /** */
1405 bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed()
1407 #if USE_ASM_INSTRUCTIONS
1408 LARGE_INTEGER liStart, liEnd, liCountsPerSecond;
1409 double dFrequency, dDifference;
1411 // Attempt to get a starting tick count.
1412 QueryPerformanceCounter (&liStart);
1414 __try
1416 _asm
1418 mov eax, 0x80000000
1419 mov ebx, CLASSICAL_CPU_FREQ_LOOP
1420 Timer_Loop:
1421 bsf ecx,eax
1422 dec ebx
1423 jnz Timer_Loop
1426 __except(1)
1428 return false;
1431 // Attempt to get a starting tick count.
1432 QueryPerformanceCounter (&liEnd);
1434 // Get the difference... NB: This is in seconds....
1435 QueryPerformanceFrequency (&liCountsPerSecond);
1436 dDifference = (((double) liEnd.QuadPart - (double) liStart.QuadPart) / (double) liCountsPerSecond.QuadPart);
1438 // Calculate the clock speed.
1439 if (this->ChipID.Family == 3)
1441 // 80386 processors.... Loop time is 115 cycles!
1442 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1048576);
1444 else if (this->ChipID.Family == 4)
1446 // 80486 processors.... Loop time is 47 cycles!
1447 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1048576);
1449 else if (this->ChipID.Family == 5)
1451 // Pentium processors.... Loop time is 43 cycles!
1452 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1048576);
1455 // Save the clock speed.
1456 this->Features.CPUSpeed = (int) dFrequency;
1457 #else
1458 return true;
1459 #endif
1462 /** */
1463 bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULevelToCheck)
1465 int MaxCPUExtendedLevel = 0;
1467 // The extended CPUID is supported by various vendors starting with the following CPU models:
1469 // Manufacturer & Chip Name | Family Model Revision
1471 // AMD K6, K6-2 | 5 6 x
1472 // Cyrix GXm, Cyrix III "Joshua" | 5 4 x
1473 // IDT C6-2 | 5 8 x
1474 // VIA Cyrix III | 6 5 x
1475 // Transmeta Crusoe | 5 x x
1476 // Intel Pentium 4 | f x x
1479 // We check to see if a supported processor is present...
1480 if (this->ChipManufacturer == AMD)
1482 if (this->ChipID.Family < 5) return false;
1483 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6)) return false;
1485 else if (this->ChipManufacturer == Cyrix)
1487 if (this->ChipID.Family < 5) return false;
1488 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4)) return false;
1489 if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5)) return false;
1491 else if (this->ChipManufacturer == IDT)
1493 if (this->ChipID.Family < 5) return false;
1494 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8)) return false;
1496 else if (this->ChipManufacturer == Transmeta)
1498 if (this->ChipID.Family < 5) return false;
1500 else if (this->ChipManufacturer == Intel)
1502 if (this->ChipID.Family < 0xf)
1504 return false;
1508 #if USE_ASM_INSTRUCTIONS
1510 // Use assembly to detect CPUID information...
1511 __try {
1512 _asm {
1513 #ifdef CPUID_AWARE_COMPILER
1514 ; we must push/pop the registers <<CPUID>> writes to, as the
1515 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1516 ; these registers to change.
1517 push eax
1518 push ebx
1519 push ecx
1520 push edx
1521 #endif
1522 ; <<CPUID>>
1523 ; eax = 0x80000000 --> eax: maximum supported extended level
1524 mov eax,0x80000000
1525 CPUID_INSTRUCTION
1526 mov MaxCPUExtendedLevel, eax
1528 #ifdef CPUID_AWARE_COMPILER
1529 pop edx
1530 pop ecx
1531 pop ebx
1532 pop eax
1533 #endif
1536 __except(1)
1538 return false;
1540 #endif
1542 // Now we have to check the level wanted vs level returned...
1543 int nLevelWanted = (CPULevelToCheck & 0x7FFFFFFF);
1544 int nLevelReturn = (MaxCPUExtendedLevel & 0x7FFFFFFF);
1546 // Check to see if the level provided is supported...
1547 if (nLevelWanted > nLevelReturn)
1549 return false;
1552 return true;
1555 /** */
1556 bool SystemInformationImplementation::RetrieveExtendedCPUFeatures()
1559 // Check that we are not using an Intel processor as it does not support this.
1560 if (this->ChipManufacturer == Intel)
1562 return false;
1565 // Check to see if what we are about to do is supported...
1566 if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000001)))
1568 return false;
1570 #if USE_ASM_INSTRUCTIONS
1571 int localCPUExtendedFeatures = 0;
1573 // Use assembly to detect CPUID information...
1574 __try
1576 _asm
1578 #ifdef CPUID_AWARE_COMPILER
1579 ; we must push/pop the registers <<CPUID>> writes to, as the
1580 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1581 ; these registers to change.
1582 push eax
1583 push ebx
1584 push ecx
1585 push edx
1586 #endif
1587 ; <<CPUID>>
1588 ; eax = 0x80000001 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
1589 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
1590 ; edx: CPU feature flags
1591 mov eax,0x80000001
1592 CPUID_INSTRUCTION
1593 mov localCPUExtendedFeatures, edx
1595 #ifdef CPUID_AWARE_COMPILER
1596 pop edx
1597 pop ecx
1598 pop ebx
1599 pop eax
1600 #endif
1603 __except(1)
1605 return false;
1608 // Retrieve the extended features of CPU present.
1609 this->Features.ExtendedFeatures.Has3DNow = ((localCPUExtendedFeatures & 0x80000000) != 0); // 3DNow Present --> Bit 31.
1610 this->Features.ExtendedFeatures.Has3DNowPlus = ((localCPUExtendedFeatures & 0x40000000) != 0); // 3DNow+ Present -- > Bit 30.
1611 this->Features.ExtendedFeatures.HasSSEMMX = ((localCPUExtendedFeatures & 0x00400000) != 0); // SSE MMX Present --> Bit 22.
1612 this->Features.ExtendedFeatures.SupportsMP = ((localCPUExtendedFeatures & 0x00080000) != 0); // MP Capable -- > Bit 19.
1614 // Retrieve AMD specific extended features.
1615 if (this->ChipManufacturer == AMD)
1617 this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x00400000) != 0); // AMD specific: MMX-SSE --> Bit 22
1620 // Retrieve Cyrix specific extended features.
1621 if (this->ChipManufacturer == Cyrix)
1623 this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x01000000) != 0); // Cyrix specific: Extended MMX --> Bit 24
1625 #endif
1627 return true;
1630 /** */
1631 bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
1633 // Check to see if the processor supports the processor serial number.
1634 if (!this->Features.HasSerial)
1636 return false;
1639 #if USE_ASM_INSTRUCTIONS
1640 int SerialNumber[3];
1643 // Use assembly to detect CPUID information...
1644 __try {
1645 _asm {
1646 #ifdef CPUID_AWARE_COMPILER
1647 ; we must push/pop the registers <<CPUID>> writes to, as the
1648 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1649 ; these registers to change.
1650 push eax
1651 push ebx
1652 push ecx
1653 push edx
1654 #endif
1655 ; <<CPUID>>
1656 ; eax = 3 --> ebx: top 32 bits are the processor signature bits --> NB: Transmeta only ?!?
1657 ; ecx: middle 32 bits are the processor signature bits
1658 ; edx: bottom 32 bits are the processor signature bits
1659 mov eax, 3
1660 CPUID_INSTRUCTION
1661 mov SerialNumber[0 * TYPE int], ebx
1662 mov SerialNumber[1 * TYPE int], ecx
1663 mov SerialNumber[2 * TYPE int], edx
1665 #ifdef CPUID_AWARE_COMPILER
1666 pop edx
1667 pop ecx
1668 pop ebx
1669 pop eax
1670 #endif
1673 __except(1)
1675 return false;
1678 // Process the returned information.
1679 sprintf (this->ChipID.SerialNumber, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
1680 ((SerialNumber[0] & 0xff000000) >> 24),
1681 ((SerialNumber[0] & 0x00ff0000) >> 16),
1682 ((SerialNumber[0] & 0x0000ff00) >> 8),
1683 ((SerialNumber[0] & 0x000000ff) >> 0),
1684 ((SerialNumber[1] & 0xff000000) >> 24),
1685 ((SerialNumber[1] & 0x00ff0000) >> 16),
1686 ((SerialNumber[1] & 0x0000ff00) >> 8),
1687 ((SerialNumber[1] & 0x000000ff) >> 0),
1688 ((SerialNumber[2] & 0xff000000) >> 24),
1689 ((SerialNumber[2] & 0x00ff0000) >> 16),
1690 ((SerialNumber[2] & 0x0000ff00) >> 8),
1691 ((SerialNumber[2] & 0x000000ff) >> 0));
1692 #endif
1694 return true;
1697 /** */
1698 bool SystemInformationImplementation::RetrieveCPUPowerManagement()
1700 // Check to see if what we are about to do is supported...
1701 if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000007)))
1703 this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = false;
1704 this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = false;
1705 this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = false;
1706 return false;
1709 #if USE_ASM_INSTRUCTIONS
1710 int localCPUPowerManagement = 0;
1713 // Use assembly to detect CPUID information...
1714 __try {
1715 _asm {
1716 #ifdef CPUID_AWARE_COMPILER
1717 ; we must push/pop the registers <<CPUID>> writes to, as the
1718 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1719 ; these registers to change.
1720 push eax
1721 push ebx
1722 push ecx
1723 push edx
1724 #endif
1725 ; <<CPUID>>
1726 ; eax = 0x80000007 --> edx: get processor power management
1727 mov eax,0x80000007
1728 CPUID_INSTRUCTION
1729 mov localCPUPowerManagement, edx
1731 #ifdef CPUID_AWARE_COMPILER
1732 pop edx
1733 pop ecx
1734 pop ebx
1735 pop eax
1736 #endif
1739 __except(1)
1741 return false;
1744 // Check for the power management capabilities of the CPU.
1745 this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = ((localCPUPowerManagement & 0x00000001) != 0);
1746 this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = ((localCPUPowerManagement & 0x00000002) != 0);
1747 this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = ((localCPUPowerManagement & 0x00000004) != 0);
1749 #endif
1751 return true;
1754 /** */
1755 bool SystemInformationImplementation::RetrieveExtendedCPUIdentity()
1757 // Check to see if what we are about to do is supported...
1758 if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000002)))
1759 return false;
1760 if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000003)))
1761 return false;
1762 if (!RetrieveCPUExtendedLevelSupport(static_cast<int>(0x80000004)))
1763 return false;
1765 #if USE_ASM_INSTRUCTIONS
1766 int ProcessorNameStartPos = 0;
1767 int CPUExtendedIdentity[12];
1769 // Use assembly to detect CPUID information...
1770 __try {
1771 _asm {
1772 #ifdef CPUID_AWARE_COMPILER
1773 ; we must push/pop the registers <<CPUID>> writes to, as the
1774 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1775 ; these registers to change.
1776 push eax
1777 push ebx
1778 push ecx
1779 push edx
1780 #endif
1781 ; <<CPUID>>
1782 ; eax = 0x80000002 --> eax, ebx, ecx, edx: get processor name string (part 1)
1783 mov eax,0x80000002
1784 CPUID_INSTRUCTION
1785 mov CPUExtendedIdentity[0 * TYPE int], eax
1786 mov CPUExtendedIdentity[1 * TYPE int], ebx
1787 mov CPUExtendedIdentity[2 * TYPE int], ecx
1788 mov CPUExtendedIdentity[3 * TYPE int], edx
1790 ; <<CPUID>>
1791 ; eax = 0x80000003 --> eax, ebx, ecx, edx: get processor name string (part 2)
1792 mov eax,0x80000003
1793 CPUID_INSTRUCTION
1794 mov CPUExtendedIdentity[4 * TYPE int], eax
1795 mov CPUExtendedIdentity[5 * TYPE int], ebx
1796 mov CPUExtendedIdentity[6 * TYPE int], ecx
1797 mov CPUExtendedIdentity[7 * TYPE int], edx
1799 ; <<CPUID>>
1800 ; eax = 0x80000004 --> eax, ebx, ecx, edx: get processor name string (part 3)
1801 mov eax,0x80000004
1802 CPUID_INSTRUCTION
1803 mov CPUExtendedIdentity[8 * TYPE int], eax
1804 mov CPUExtendedIdentity[9 * TYPE int], ebx
1805 mov CPUExtendedIdentity[10 * TYPE int], ecx
1806 mov CPUExtendedIdentity[11 * TYPE int], edx
1808 #ifdef CPUID_AWARE_COMPILER
1809 pop edx
1810 pop ecx
1811 pop ebx
1812 pop eax
1813 #endif
1816 __except(1)
1818 return false;
1821 // Process the returned information.
1822 memcpy (this->ChipID.ProcessorName, &(CPUExtendedIdentity[0]), sizeof (int));
1823 memcpy (&(this->ChipID.ProcessorName[4]), &(CPUExtendedIdentity[1]), sizeof (int));
1824 memcpy (&(this->ChipID.ProcessorName[8]), &(CPUExtendedIdentity[2]), sizeof (int));
1825 memcpy (&(this->ChipID.ProcessorName[12]), &(CPUExtendedIdentity[3]), sizeof (int));
1826 memcpy (&(this->ChipID.ProcessorName[16]), &(CPUExtendedIdentity[4]), sizeof (int));
1827 memcpy (&(this->ChipID.ProcessorName[20]), &(CPUExtendedIdentity[5]), sizeof (int));
1828 memcpy (&(this->ChipID.ProcessorName[24]), &(CPUExtendedIdentity[6]), sizeof (int));
1829 memcpy (&(this->ChipID.ProcessorName[28]), &(CPUExtendedIdentity[7]), sizeof (int));
1830 memcpy (&(this->ChipID.ProcessorName[32]), &(CPUExtendedIdentity[8]), sizeof (int));
1831 memcpy (&(this->ChipID.ProcessorName[36]), &(CPUExtendedIdentity[9]), sizeof (int));
1832 memcpy (&(this->ChipID.ProcessorName[40]), &(CPUExtendedIdentity[10]), sizeof (int));
1833 memcpy (&(this->ChipID.ProcessorName[44]), &(CPUExtendedIdentity[11]), sizeof (int));
1834 this->ChipID.ProcessorName[48] = '\0';
1836 // Because some manufacturers have leading white space - we have to post-process the name.
1837 if (this->ChipManufacturer == Intel)
1839 for (int nCounter = 0; nCounter < CHIPNAME_STRING_LENGTH; nCounter ++)
1841 // There will either be NULL (\0) or spaces ( ) as the leading characters.
1842 if ((this->ChipID.ProcessorName[nCounter] != '\0') && (this->ChipID.ProcessorName[nCounter] != ' '))
1844 // We have found the starting position of the name.
1845 ProcessorNameStartPos = nCounter;
1846 // Terminate the loop.
1847 break;
1851 // Check to see if there is any white space at the start.
1852 if (ProcessorNameStartPos == 0)
1854 return true;
1857 // Now move the name forward so that there is no white space.
1858 memmove(this->ChipID.ProcessorName, &(this->ChipID.ProcessorName[ProcessorNameStartPos]), (CHIPNAME_STRING_LENGTH - ProcessorNameStartPos));
1860 #endif
1862 return true;
1865 /** */
1866 bool SystemInformationImplementation::RetrieveClassicalCPUIdentity()
1868 // Start by decided which manufacturer we are using....
1869 switch (this->ChipManufacturer)
1871 case Intel:
1872 // Check the family / model / revision to determine the CPU ID.
1873 switch (this->ChipID.Family) {
1874 case 3:
1875 sprintf (this->ChipID.ProcessorName, "Newer i80386 family");
1876 break;
1877 case 4:
1878 switch (this->ChipID.Model) {
1879 case 0: sprintf (this->ChipID.ProcessorName,"i80486DX-25/33"); break;
1880 case 1: sprintf (this->ChipID.ProcessorName,"i80486DX-50"); break;
1881 case 2: sprintf (this->ChipID.ProcessorName,"i80486SX"); break;
1882 case 3: sprintf (this->ChipID.ProcessorName,"i80486DX2"); break;
1883 case 4: sprintf (this->ChipID.ProcessorName,"i80486SL"); break;
1884 case 5: sprintf (this->ChipID.ProcessorName,"i80486SX2"); break;
1885 case 7: sprintf (this->ChipID.ProcessorName,"i80486DX2 WriteBack"); break;
1886 case 8: sprintf (this->ChipID.ProcessorName,"i80486DX4"); break;
1887 case 9: sprintf (this->ChipID.ProcessorName,"i80486DX4 WriteBack"); break;
1888 default: sprintf (this->ChipID.ProcessorName,"Unknown 80486 family"); return false;
1890 break;
1891 case 5:
1892 switch (this->ChipID.Model)
1894 case 0: sprintf (this->ChipID.ProcessorName,"P5 A-Step"); break;
1895 case 1: sprintf (this->ChipID.ProcessorName,"P5"); break;
1896 case 2: sprintf (this->ChipID.ProcessorName,"P54C"); break;
1897 case 3: sprintf (this->ChipID.ProcessorName,"P24T OverDrive"); break;
1898 case 4: sprintf (this->ChipID.ProcessorName,"P55C"); break;
1899 case 7: sprintf (this->ChipID.ProcessorName,"P54C"); break;
1900 case 8: sprintf (this->ChipID.ProcessorName,"P55C (0.25micron)"); break;
1901 default: sprintf (this->ChipID.ProcessorName,"Unknown Pentium family"); return false;
1903 break;
1904 case 6:
1905 switch (this->ChipID.Model)
1907 case 0: sprintf (this->ChipID.ProcessorName,"P6 A-Step"); break;
1908 case 1: sprintf (this->ChipID.ProcessorName,"P6"); break;
1909 case 3: sprintf (this->ChipID.ProcessorName,"Pentium II (0.28 micron)"); break;
1910 case 5: sprintf (this->ChipID.ProcessorName,"Pentium II (0.25 micron)"); break;
1911 case 6: sprintf (this->ChipID.ProcessorName,"Pentium II With On-Die L2 Cache"); break;
1912 case 7: sprintf (this->ChipID.ProcessorName,"Pentium III (0.25 micron)"); break;
1913 case 8: sprintf (this->ChipID.ProcessorName,"Pentium III (0.18 micron) With 256 KB On-Die L2 Cache "); break;
1914 case 0xa: sprintf (this->ChipID.ProcessorName,"Pentium III (0.18 micron) With 1 Or 2 MB On-Die L2 Cache "); break;
1915 case 0xb: sprintf (this->ChipID.ProcessorName,"Pentium III (0.13 micron) With 256 Or 512 KB On-Die L2 Cache "); break;
1916 default: sprintf (this->ChipID.ProcessorName,"Unknown P6 family"); return false;
1918 break;
1919 case 7:
1920 sprintf (this->ChipID.ProcessorName,"Intel Merced (IA-64)");
1921 break;
1922 case 0xf:
1923 // Check the extended family bits...
1924 switch (this->ChipID.ExtendedFamily)
1926 case 0:
1927 switch (this->ChipID.Model)
1929 case 0: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.18 micron)"); break;
1930 case 1: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.18 micron)"); break;
1931 case 2: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.13 micron)"); break;
1932 default: sprintf (this->ChipID.ProcessorName,"Unknown Pentium 4 family"); return false;
1934 break;
1935 case 1:
1936 sprintf (this->ChipID.ProcessorName,"Intel McKinley (IA-64)");
1937 break;
1938 default:
1939 sprintf (this->ChipID.ProcessorName,"Pentium");
1941 break;
1942 default:
1943 sprintf (this->ChipID.ProcessorName,"Unknown Intel family");
1944 return false;
1946 break;
1948 case AMD:
1949 // Check the family / model / revision to determine the CPU ID.
1950 switch (this->ChipID.Family)
1952 case 4:
1953 switch (this->ChipID.Model)
1955 case 3: sprintf (this->ChipID.ProcessorName,"80486DX2"); break;
1956 case 7: sprintf (this->ChipID.ProcessorName,"80486DX2 WriteBack"); break;
1957 case 8: sprintf (this->ChipID.ProcessorName,"80486DX4"); break;
1958 case 9: sprintf (this->ChipID.ProcessorName,"80486DX4 WriteBack"); break;
1959 case 0xe: sprintf (this->ChipID.ProcessorName,"5x86"); break;
1960 case 0xf: sprintf (this->ChipID.ProcessorName,"5x86WB"); break;
1961 default: sprintf (this->ChipID.ProcessorName,"Unknown 80486 family"); return false;
1963 break;
1964 case 5:
1965 switch (this->ChipID.Model)
1967 case 0: sprintf (this->ChipID.ProcessorName,"SSA5 (PR75, PR90, PR100)"); break;
1968 case 1: sprintf (this->ChipID.ProcessorName,"5k86 (PR120, PR133)"); break;
1969 case 2: sprintf (this->ChipID.ProcessorName,"5k86 (PR166)"); break;
1970 case 3: sprintf (this->ChipID.ProcessorName,"5k86 (PR200)"); break;
1971 case 6: sprintf (this->ChipID.ProcessorName,"K6 (0.30 micron)"); break;
1972 case 7: sprintf (this->ChipID.ProcessorName,"K6 (0.25 micron)"); break;
1973 case 8: sprintf (this->ChipID.ProcessorName,"K6-2"); break;
1974 case 9: sprintf (this->ChipID.ProcessorName,"K6-III"); break;
1975 case 0xd: sprintf (this->ChipID.ProcessorName,"K6-2+ or K6-III+ (0.18 micron)"); break;
1976 default: sprintf (this->ChipID.ProcessorName,"Unknown 80586 family"); return false;
1978 break;
1979 case 6:
1980 switch (this->ChipID.Model)
1982 case 1: sprintf (this->ChipID.ProcessorName,"Athlon- (0.25 micron)"); break;
1983 case 2: sprintf (this->ChipID.ProcessorName,"Athlon- (0.18 micron)"); break;
1984 case 3: sprintf (this->ChipID.ProcessorName,"Duron- (SF core)"); break;
1985 case 4: sprintf (this->ChipID.ProcessorName,"Athlon- (Thunderbird core)"); break;
1986 case 6: sprintf (this->ChipID.ProcessorName,"Athlon- (Palomino core)"); break;
1987 case 7: sprintf (this->ChipID.ProcessorName,"Duron- (Morgan core)"); break;
1988 case 8:
1989 if (this->Features.ExtendedFeatures.SupportsMP)
1990 sprintf (this->ChipID.ProcessorName,"Athlon - MP (Thoroughbred core)");
1991 else sprintf (this->ChipID.ProcessorName,"Athlon - XP (Thoroughbred core)");
1992 break;
1993 default: sprintf (this->ChipID.ProcessorName,"Unknown K7 family"); return false;
1995 break;
1996 default:
1997 sprintf (this->ChipID.ProcessorName,"Unknown AMD family");
1998 return false;
2000 break;
2002 case Transmeta:
2003 switch (this->ChipID.Family)
2005 case 5:
2006 switch (this->ChipID.Model)
2008 case 4: sprintf (this->ChipID.ProcessorName,"Crusoe TM3x00 and TM5x00"); break;
2009 default: sprintf (this->ChipID.ProcessorName,"Unknown Crusoe family"); return false;
2011 break;
2012 default:
2013 sprintf (this->ChipID.ProcessorName,"Unknown Transmeta family");
2014 return false;
2016 break;
2018 case Rise:
2019 switch (this->ChipID.Family)
2021 case 5:
2022 switch (this->ChipID.Model)
2024 case 0: sprintf (this->ChipID.ProcessorName,"mP6 (0.25 micron)"); break;
2025 case 2: sprintf (this->ChipID.ProcessorName,"mP6 (0.18 micron)"); break;
2026 default: sprintf (this->ChipID.ProcessorName,"Unknown Rise family"); return false;
2028 break;
2029 default:
2030 sprintf (this->ChipID.ProcessorName,"Unknown Rise family");
2031 return false;
2033 break;
2035 case UMC:
2036 switch (this->ChipID.Family)
2038 case 4:
2039 switch (this->ChipID.Model)
2041 case 1: sprintf (this->ChipID.ProcessorName,"U5D"); break;
2042 case 2: sprintf (this->ChipID.ProcessorName,"U5S"); break;
2043 default: sprintf (this->ChipID.ProcessorName,"Unknown UMC family"); return false;
2045 break;
2046 default:
2047 sprintf (this->ChipID.ProcessorName,"Unknown UMC family");
2048 return false;
2050 break;
2052 case IDT:
2053 switch (this->ChipID.Family)
2055 case 5:
2056 switch (this->ChipID.Model)
2058 case 4: sprintf (this->ChipID.ProcessorName,"C6"); break;
2059 case 8: sprintf (this->ChipID.ProcessorName,"C2"); break;
2060 case 9: sprintf (this->ChipID.ProcessorName,"C3"); break;
2061 default: sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family"); return false;
2063 break;
2064 case 6:
2065 switch (this->ChipID.Model)
2067 case 6: sprintf (this->ChipID.ProcessorName,"VIA Cyrix III - Samuel"); break;
2068 default: sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family"); return false;
2070 break;
2071 default:
2072 sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family");
2073 return false;
2075 break;
2077 case Cyrix:
2078 switch (this->ChipID.Family)
2080 case 4:
2081 switch (this->ChipID.Model)
2083 case 4: sprintf (this->ChipID.ProcessorName,"MediaGX GX, GXm"); break;
2084 case 9: sprintf (this->ChipID.ProcessorName,"5x86"); break;
2085 default: sprintf (this->ChipID.ProcessorName,"Unknown Cx5x86 family"); return false;
2087 break;
2088 case 5:
2089 switch (this->ChipID.Model)
2091 case 2: sprintf (this->ChipID.ProcessorName,"Cx6x86"); break;
2092 case 4: sprintf (this->ChipID.ProcessorName,"MediaGX GXm"); break;
2093 default: sprintf (this->ChipID.ProcessorName,"Unknown Cx6x86 family"); return false;
2095 break;
2096 case 6:
2097 switch (this->ChipID.Model)
2099 case 0: sprintf (this->ChipID.ProcessorName,"6x86MX"); break;
2100 case 5: sprintf (this->ChipID.ProcessorName,"Cyrix M2 Core"); break;
2101 case 6: sprintf (this->ChipID.ProcessorName,"WinChip C5A Core"); break;
2102 case 7: sprintf (this->ChipID.ProcessorName,"WinChip C5B\\C5C Core"); break;
2103 case 8: sprintf (this->ChipID.ProcessorName,"WinChip C5C-T Core"); break;
2104 default: sprintf (this->ChipID.ProcessorName,"Unknown 6x86MX\\Cyrix III family"); return false;
2106 break;
2107 default:
2108 sprintf (this->ChipID.ProcessorName,"Unknown Cyrix family");
2109 return false;
2111 break;
2113 case NexGen:
2114 switch (this->ChipID.Family)
2116 case 5:
2117 switch (this->ChipID.Model)
2119 case 0: sprintf (this->ChipID.ProcessorName,"Nx586 or Nx586FPU"); break;
2120 default: sprintf (this->ChipID.ProcessorName,"Unknown NexGen family"); return false;
2122 break;
2123 default:
2124 sprintf (this->ChipID.ProcessorName,"Unknown NexGen family");
2125 return false;
2127 break;
2129 case NSC:
2130 sprintf (this->ChipID.ProcessorName,"Cx486SLC \\ DLC \\ Cx486S A-Step");
2131 break;
2132 default:
2133 sprintf (this->ChipID.ProcessorName,"Unknown family"); // We cannot identify the processor.
2134 return false;
2137 return true;
2140 /** Extract a value from the CPUInfo file */
2141 kwsys_stl::string SystemInformationImplementation::ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,const char* word,size_t init)
2143 size_t pos = buffer.find(word,init);
2144 if(pos != buffer.npos)
2146 this->CurrentPositionInFile = pos;
2147 pos = buffer.find(":",pos);
2148 size_t pos2 = buffer.find("\n",pos);
2149 if(pos!=buffer.npos && pos2!=buffer.npos)
2151 return buffer.substr(pos+2,pos2-pos-2);
2154 this->CurrentPositionInFile = buffer.npos;
2155 return "";
2158 /** Query for the cpu status */
2159 int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
2161 this->NumberOfLogicalCPU = 0;
2162 this->NumberOfPhysicalCPU = 0;
2163 kwsys_stl::string buffer;
2165 FILE *fd = fopen("/proc/cpuinfo", "r" );
2166 if ( !fd )
2168 kwsys_ios::cout << "Problem opening /proc/cpuinfo" << kwsys_ios::endl;
2169 return 0;
2172 size_t fileSize = 0;
2173 while(!feof(fd))
2175 buffer += static_cast<char>(fgetc(fd));
2176 fileSize++;
2178 fclose( fd );
2179 buffer.resize(fileSize-2);
2180 // Number of logical CPUs (combination of multiple processors, multi-core
2181 // and hyperthreading)
2182 size_t pos = buffer.find("processor\t");
2183 while(pos != buffer.npos)
2185 this->NumberOfLogicalCPU++;
2186 pos = buffer.find("processor\t",pos+1);
2189 #ifdef __linux
2190 // Find the largest physical id.
2191 int maxId = -1;
2192 kwsys_stl::string idc =
2193 this->ExtractValueFromCpuInfoFile(buffer,"physical id");
2194 while(this->CurrentPositionInFile != buffer.npos)
2196 int id = atoi(idc.c_str());
2197 if(id > maxId)
2199 maxId=id;
2201 idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",
2202 this->CurrentPositionInFile+1);
2204 // Physical ids returned by Linux don't distinguish cores.
2205 // We want to record the total number of cores in this->NumberOfPhysicalCPU
2206 // (checking only the first proc)
2207 kwsys_stl::string cores =
2208 this->ExtractValueFromCpuInfoFile(buffer,"cpu cores");
2209 int numberOfCoresPerCPU=atoi(cores.c_str());
2210 this->NumberOfPhysicalCPU=static_cast<unsigned int>(
2211 numberOfCoresPerCPU*(maxId+1));
2213 #else // __CYGWIN__
2214 // does not have "physical id" entries, neither "cpu cores"
2215 // this has to be fixed for hyper-threading.
2216 kwsys_stl::string cpucount =
2217 this->ExtractValueFromCpuInfoFile(buffer,"cpu count");
2218 this->NumberOfPhysicalCPU=
2219 this->NumberOfLogicalCPU = atoi(cpucount.c_str());
2220 #endif
2221 // gotta have one, and if this is 0 then we get a / by 0n
2222 // beter to have a bad answer than a crash
2223 if(this->NumberOfPhysicalCPU <= 0)
2225 this->NumberOfPhysicalCPU = 1;
2227 // LogicalProcessorsPerPhysical>1 => hyperthreading.
2228 this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical=
2229 this->NumberOfLogicalCPU/this->NumberOfPhysicalCPU;
2231 // CPU speed (checking only the first proc
2232 kwsys_stl::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer,"cpu MHz");
2233 this->CPUSpeedInMHz = static_cast<float>(atof(CPUSpeed.c_str()));
2235 // Chip family
2236 this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str());
2238 // Chip Vendor
2239 strcpy(this->ChipID.Vendor,this->ExtractValueFromCpuInfoFile(buffer,"vendor_id").c_str());
2240 this->FindManufacturer();
2242 // Chip Model
2243 this->ChipID.Model = atoi(this->ExtractValueFromCpuInfoFile(buffer,"model").c_str());
2244 this->RetrieveClassicalCPUIdentity();
2246 // L1 Cache size
2247 kwsys_stl::string cacheSize = this->ExtractValueFromCpuInfoFile(buffer,"cache size");
2248 pos = cacheSize.find(" KB");
2249 if(pos!=cacheSize.npos)
2251 cacheSize = cacheSize.substr(0,pos);
2253 this->Features.L1CacheSize = atoi(cacheSize.c_str());
2254 return 1;
2257 /** Query for the memory status */
2258 int SystemInformationImplementation::QueryMemory()
2260 this->TotalVirtualMemory = 0;
2261 this->TotalPhysicalMemory = 0;
2262 this->AvailableVirtualMemory = 0;
2263 this->AvailablePhysicalMemory = 0;
2264 #ifdef __CYGWIN__
2265 return 0;
2266 #elif _WIN32
2267 #if _MSC_VER < 1300
2268 MEMORYSTATUS ms;
2269 GlobalMemoryStatus(&ms);
2270 #define MEM_VAL(value) dw##value
2271 #else
2272 MEMORYSTATUSEX ms;
2273 GlobalMemoryStatusEx(&ms);
2274 #define MEM_VAL(value) ull##value
2275 #endif
2276 unsigned long tv = ms.MEM_VAL(TotalVirtual);
2277 unsigned long tp = ms.MEM_VAL(TotalPhys);
2278 unsigned long av = ms.MEM_VAL(AvailVirtual);
2279 unsigned long ap = ms.MEM_VAL(AvailPhys);
2280 this->TotalVirtualMemory = tv>>10>>10;
2281 this->TotalPhysicalMemory = tp>>10>>10;
2282 this->AvailableVirtualMemory = av>>10>>10;
2283 this->AvailablePhysicalMemory = ap>>10>>10;
2284 return 1;
2285 #elif __linux
2286 unsigned long tv=0;
2287 unsigned long tp=0;
2288 unsigned long av=0;
2289 unsigned long ap=0;
2291 char buffer[1024]; // for skipping unused lines
2293 int linuxMajor = 0;
2294 int linuxMinor = 0;
2296 // Find the Linux kernel version first
2297 struct utsname unameInfo;
2298 int errorFlag = uname(&unameInfo);
2299 if( errorFlag!=0 )
2301 kwsys_ios::cout << "Problem calling uname(): " << strerror(errno) << kwsys_ios::endl;
2302 return 0;
2305 if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 )
2307 // release looks like "2.6.3-15mdk-i686-up-4GB"
2308 char majorChar=unameInfo.release[0];
2309 char minorChar=unameInfo.release[2];
2311 if( isdigit(majorChar) )
2313 linuxMajor=majorChar-'0';
2316 if( isdigit(minorChar) )
2318 linuxMinor=minorChar-'0';
2322 FILE *fd = fopen("/proc/meminfo", "r" );
2323 if ( !fd )
2325 kwsys_ios::cout << "Problem opening /proc/meminfo" << kwsys_ios::endl;
2326 return 0;
2329 if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) )
2331 // new /proc/meminfo format since kernel 2.6.x
2332 // Rigorously, this test should check from the developping version 2.5.x
2333 // that introduced the new format...
2335 unsigned long freeMem;
2336 unsigned long buffersMem;
2337 unsigned long cachedMem;
2339 int status;
2341 status=fscanf(fd,"MemTotal:%lu kB\n", &this->TotalPhysicalMemory);
2342 if(status==1)
2344 status+=fscanf(fd,"MemFree:%lu kB\n", &freeMem);
2346 if(status==2)
2348 status+=fscanf(fd,"Buffers:%lu kB\n", &buffersMem);
2350 if(status==3)
2352 status+=fscanf(fd,"Cached:%lu kB\n", &cachedMem);
2354 if(status==4)
2356 this->TotalPhysicalMemory /= 1024;
2357 this->AvailablePhysicalMemory = freeMem+cachedMem+buffersMem;
2358 this->AvailablePhysicalMemory /= 1024;
2361 // Skip SwapCached, Active, Inactive, HighTotal, HighFree, LowTotal
2362 // and LowFree.
2363 int i=0;
2364 bool success=status==4;
2365 while(i<7 && success)
2367 char *r=fgets(buffer, sizeof(buffer), fd); // skip a line
2368 success=r==buffer;
2369 ++i;
2372 if(success)
2374 status+=fscanf(fd,"SwapTotal:%lu kB\n", &this->TotalVirtualMemory);
2376 if(status==5)
2378 status+=fscanf(fd,"SwapFree:%lu kB\n", &this->AvailableVirtualMemory);
2380 if(status==6)
2382 this->TotalVirtualMemory /= 1024;
2383 this->AvailableVirtualMemory /= 1024;
2385 else
2387 kwsys_ios::cout << "Problem parsing /proc/meminfo" << kwsys_ios::endl;
2388 fclose(fd);
2389 return 0;
2392 else
2394 // /proc/meminfo format for kernel older than 2.6.x
2396 unsigned long temp;
2397 unsigned long cachedMem;
2398 unsigned long buffersMem;
2399 char *r=fgets(buffer, sizeof(buffer), fd); // Skip "total: used:..."
2400 int status=0;
2401 if(r==buffer)
2403 status+=fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n",
2404 &tp, &temp, &ap, &temp, &buffersMem, &cachedMem);
2406 if(status==6)
2408 status+=fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
2410 if(status==9)
2412 this->TotalVirtualMemory = tv>>10>>10;
2413 this->TotalPhysicalMemory = tp>>10>>10;
2414 this->AvailableVirtualMemory = av>>10>>10;
2415 this->AvailablePhysicalMemory = (ap+buffersMem+cachedMem)>>10>>10;
2417 else
2419 kwsys_ios::cout << "Problem parsing /proc/meminfo" << kwsys_ios::endl;
2420 fclose(fd);
2421 return 0;
2424 fclose( fd );
2425 return 1;
2426 #elif __hpux
2427 unsigned long tv=0;
2428 unsigned long tp=0;
2429 unsigned long av=0;
2430 unsigned long ap=0;
2431 struct pst_static pst;
2432 struct pst_dynamic pdy;
2434 unsigned long ps = 0;
2435 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1)
2437 ps = pst.page_size;
2438 tp = pst.physical_memory *ps;
2439 tv = (pst.physical_memory + pst.pst_maxmem) * ps;
2440 if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t) 1, 0) != -1)
2442 ap = tp - pdy.psd_rm * ps;
2443 av = tv - pdy.psd_vm;
2444 this->TotalVirtualMemory = tv>>10>>10;
2445 this->TotalPhysicalMemory = tp>>10>>10;
2446 this->AvailableVirtualMemory = av>>10>>10;
2447 this->AvailablePhysicalMemory = ap>>10>>10;
2448 return 1;
2451 return 0;
2452 #else
2453 return 0;
2454 #endif
2459 /** */
2460 unsigned long SystemInformationImplementation::GetTotalVirtualMemory()
2462 return this->TotalVirtualMemory;
2465 /** */
2466 unsigned long SystemInformationImplementation::GetAvailableVirtualMemory()
2468 return this->AvailableVirtualMemory;
2471 unsigned long SystemInformationImplementation::GetTotalPhysicalMemory()
2473 return this->TotalPhysicalMemory;
2476 /** */
2477 unsigned long SystemInformationImplementation::GetAvailablePhysicalMemory()
2479 return this->AvailablePhysicalMemory;
2482 /** Get Cycle differences */
2483 LongLong SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction,
2484 unsigned int uiParameter)
2486 #if USE_ASM_INSTRUCTIONS
2488 unsigned int edx1, eax1;
2489 unsigned int edx2, eax2;
2491 // Calculate the frequency of the CPU instructions.
2492 __try {
2493 _asm {
2494 push uiParameter ; push parameter param
2495 mov ebx, DelayFunction ; store func in ebx
2497 RDTSC_INSTRUCTION
2499 mov esi, eax ; esi = eax
2500 mov edi, edx ; edi = edx
2502 call ebx ; call the delay functions
2504 RDTSC_INSTRUCTION
2506 pop ebx
2508 mov edx2, edx ; edx2 = edx
2509 mov eax2, eax ; eax2 = eax
2511 mov edx1, edi ; edx2 = edi
2512 mov eax1, esi ; eax2 = esi
2515 __except(1)
2517 return -1;
2520 return ((((__int64) edx2 << 32) + eax2) - (((__int64) edx1 << 32) + eax1));
2522 #else
2523 (void)DelayFunction;
2524 (void)uiParameter;
2525 return -1;
2526 #endif
2529 /** Compute the delay overhead */
2530 void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
2532 #if _WIN32
2533 LARGE_INTEGER Frequency, StartCounter, EndCounter;
2534 __int64 x;
2536 // Get the frequency of the high performance counter.
2537 if(!QueryPerformanceFrequency (&Frequency))
2539 return;
2541 x = Frequency.QuadPart / 1000 * uiMS;
2543 // Get the starting position of the counter.
2544 QueryPerformanceCounter (&StartCounter);
2546 do {
2547 // Get the ending position of the counter.
2548 QueryPerformanceCounter (&EndCounter);
2549 } while (EndCounter.QuadPart - StartCounter.QuadPart == x);
2550 #endif
2551 (void)uiMS;
2554 /** Return the number of logical CPU per physical CPUs Works only for windows */
2555 unsigned char SystemInformationImplementation::LogicalCPUPerPhysicalCPU(void)
2557 unsigned int Regebx = 0;
2558 #if USE_ASM_INSTRUCTIONS
2559 if (!this->IsHyperThreadingSupported())
2561 return static_cast<unsigned char>(1); // HT not supported
2563 __asm
2565 mov eax, 1
2566 cpuid
2567 mov Regebx, ebx
2569 #endif
2570 return static_cast<unsigned char> ((Regebx & NUM_LOGICAL_BITS) >> 16);
2573 /** Works only for windows */
2574 unsigned int SystemInformationImplementation::IsHyperThreadingSupported()
2576 #if USE_ASM_INSTRUCTIONS
2577 unsigned int Regedx = 0,
2578 Regeax = 0,
2579 VendorId[3] = {0, 0, 0};
2580 __try // Verify cpuid instruction is supported
2582 __asm
2584 xor eax, eax // call cpuid with eax = 0
2585 cpuid // Get vendor id string
2586 mov VendorId, ebx
2587 mov VendorId + 4, edx
2588 mov VendorId + 8, ecx
2590 mov eax, 1 // call cpuid with eax = 1
2591 cpuid
2592 mov Regeax, eax // eax contains family processor type
2593 mov Regedx, edx // edx has info about the availability of hyper-Threading
2596 __except (EXCEPTION_EXECUTE_HANDLER)
2598 return(0); // cpuid is unavailable
2601 if (((Regeax & FAMILY_ID) == PENTIUM4_ID) || (Regeax & EXT_FAMILY_ID))
2603 if (VendorId[0] == 'uneG')
2605 if (VendorId[1] == 'Ieni')
2607 if (VendorId[2] == 'letn')
2609 return(Regedx & HT_BIT); // Genuine Intel with hyper-Threading technology
2614 #endif
2616 return 0; // Not genuine Intel processor
2619 /** Return the APIC Id. Works only for windows. */
2620 unsigned char SystemInformationImplementation::GetAPICId()
2622 unsigned int Regebx = 0;
2623 #if USE_ASM_INSTRUCTIONS
2624 if (!this->IsHyperThreadingSupported())
2626 return static_cast<unsigned char>(-1); // HT not supported
2627 } // Logical processor = 1
2628 __asm
2630 mov eax, 1
2631 cpuid
2632 mov Regebx, ebx
2634 #endif
2635 return static_cast<unsigned char>((Regebx & INITIAL_APIC_ID_BITS) >> 24);
2638 /** Count the number of CPUs. Works only on windows. */
2639 int SystemInformationImplementation::CPUCount()
2641 #if _WIN32
2642 unsigned char StatusFlag = 0;
2643 SYSTEM_INFO info;
2645 this->NumberOfPhysicalCPU = 0;
2646 this->NumberOfLogicalCPU = 0;
2647 info.dwNumberOfProcessors = 0;
2648 GetSystemInfo (&info);
2650 // Number of physical processors in a non-Intel system
2651 // or in a 32-bit Intel system with Hyper-Threading technology disabled
2652 this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors;
2654 if (this->IsHyperThreadingSupported())
2656 unsigned char HT_Enabled = 0;
2657 this->NumberOfLogicalCPU = this->LogicalCPUPerPhysicalCPU();
2658 if (this->NumberOfLogicalCPU >= 1) // >1 Doesn't mean HT is enabled in the BIOS
2660 HANDLE hCurrentProcessHandle;
2661 #ifndef _WIN64
2662 # define DWORD_PTR DWORD
2663 #endif
2664 DWORD_PTR dwProcessAffinity;
2665 DWORD_PTR dwSystemAffinity;
2666 DWORD dwAffinityMask;
2668 // Calculate the appropriate shifts and mask based on the
2669 // number of logical processors.
2670 unsigned int i = 1;
2671 unsigned char PHY_ID_MASK = 0xFF;
2672 //unsigned char PHY_ID_SHIFT = 0;
2674 while (i < this->NumberOfLogicalCPU)
2676 i *= 2;
2677 PHY_ID_MASK <<= 1;
2678 // PHY_ID_SHIFT++;
2681 hCurrentProcessHandle = GetCurrentProcess();
2682 GetProcessAffinityMask(hCurrentProcessHandle, &dwProcessAffinity,
2683 &dwSystemAffinity);
2685 // Check if available process affinity mask is equal to the
2686 // available system affinity mask
2687 if (dwProcessAffinity != dwSystemAffinity)
2689 StatusFlag = HT_CANNOT_DETECT;
2690 this->NumberOfPhysicalCPU = (unsigned char)-1;
2691 return StatusFlag;
2694 dwAffinityMask = 1;
2695 while (dwAffinityMask != 0 && dwAffinityMask <= dwProcessAffinity)
2697 // Check if this CPU is available
2698 if (dwAffinityMask & dwProcessAffinity)
2700 if (SetProcessAffinityMask(hCurrentProcessHandle,
2701 dwAffinityMask))
2703 unsigned char APIC_ID, LOG_ID;
2704 Sleep(0); // Give OS time to switch CPU
2706 APIC_ID = GetAPICId();
2707 LOG_ID = APIC_ID & ~PHY_ID_MASK;
2709 if (LOG_ID != 0)
2711 HT_Enabled = 1;
2715 dwAffinityMask = dwAffinityMask << 1;
2717 // Reset the processor affinity
2718 SetProcessAffinityMask(hCurrentProcessHandle, dwProcessAffinity);
2720 if (this->NumberOfLogicalCPU == 1) // Normal P4 : HT is disabled in hardware
2722 StatusFlag = HT_DISABLED;
2724 else
2726 if (HT_Enabled)
2728 // Total physical processors in a Hyper-Threading enabled system.
2729 this->NumberOfPhysicalCPU /= (this->NumberOfLogicalCPU);
2730 StatusFlag = HT_ENABLED;
2732 else
2734 StatusFlag = HT_SUPPORTED_NOT_ENABLED;
2739 else
2741 // Processors do not have Hyper-Threading technology
2742 StatusFlag = HT_NOT_CAPABLE;
2743 this->NumberOfLogicalCPU = 1;
2745 return StatusFlag;
2746 #else
2747 return 0;
2748 #endif
2751 /** Return the number of logical CPUs on the system */
2752 unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
2754 return this->NumberOfLogicalCPU;
2757 /** Return the number of physical CPUs on the system */
2758 unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
2760 return this->NumberOfPhysicalCPU;
2763 /** For Mac we Parse the sysctl -a output */
2764 bool SystemInformationImplementation::ParseSysCtl()
2766 // Extract the arguments from the command line
2767 kwsys_stl::vector<const char*> args;
2768 args.push_back("sysctl");
2769 args.push_back("-a");
2770 args.push_back(0);
2772 this->SysCtlBuffer = this->RunProcess(args);
2774 // Parse values for Mac
2775 this->TotalPhysicalMemory = static_cast<unsigned long>(
2776 atoi(this->ExtractValueFromSysCtl("hw.memsize:").c_str())/(1024*1024));
2777 this->TotalVirtualMemory = 0;
2778 this->AvailablePhysicalMemory = 0;
2779 this->AvailableVirtualMemory = 0;
2781 this->NumberOfPhysicalCPU = static_cast<unsigned int>(atoi(this->ExtractValueFromSysCtl("hw.physicalcpu:").c_str()));
2782 this->NumberOfLogicalCPU = static_cast<unsigned int>(atoi(this->ExtractValueFromSysCtl("hw.logicalcpu:").c_str()));
2784 if(this->NumberOfPhysicalCPU!=0)
2786 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2789 this->CPUSpeedInMHz = static_cast<float>(atoi(this->ExtractValueFromSysCtl("hw.cpufrequency:").c_str()));
2790 this->CPUSpeedInMHz /= 1000000;
2792 // Chip family
2793 this->ChipID.Family = atoi(this->ExtractValueFromSysCtl("machdep.cpu.family:").c_str());
2795 // Chip Vendor
2796 strcpy(this->ChipID.Vendor,this->ExtractValueFromSysCtl("machdep.cpu.vendor:").c_str());
2797 this->FindManufacturer();
2799 // Chip Model
2800 this->ChipID.Model = atoi(this->ExtractValueFromSysCtl("machdep.cpu.model:").c_str());
2801 this->RetrieveClassicalCPUIdentity();
2803 // Cache size
2804 this->Features.L1CacheSize = atoi(this->ExtractValueFromSysCtl("hw.l1icachesize:").c_str());
2805 this->Features.L2CacheSize = atoi(this->ExtractValueFromSysCtl("hw.l2cachesize:").c_str());
2807 return true;
2810 /** Extract a value from sysctl command */
2811 kwsys_stl::string SystemInformationImplementation::ExtractValueFromSysCtl(const char* word)
2813 size_t pos = this->SysCtlBuffer.find(word);
2814 if(pos != this->SysCtlBuffer.npos)
2816 pos = this->SysCtlBuffer.find(": ",pos);
2817 size_t pos2 = this->SysCtlBuffer.find("\n",pos);
2818 if(pos!=this->SysCtlBuffer.npos && pos2!=this->SysCtlBuffer.npos)
2820 return this->SysCtlBuffer.substr(pos+2,pos2-pos-2);
2823 return "";
2826 /** Run a given process */
2827 kwsys_stl::string SystemInformationImplementation::RunProcess(kwsys_stl::vector<const char*> args)
2829 kwsys_stl::string buffer = "";
2831 // Run the application
2832 kwsysProcess* gp = kwsysProcess_New();
2833 kwsysProcess_SetCommand(gp, &*args.begin());
2834 kwsysProcess_SetOption(gp,kwsysProcess_Option_HideWindow,1);
2836 kwsysProcess_Execute(gp);
2838 char* data = NULL;
2839 int length;
2840 double timeout = 255;
2842 while(kwsysProcess_WaitForData(gp,&data,&length,&timeout)) // wait for 1s
2844 for(int i=0;i<length;i++)
2846 buffer += data[i];
2849 kwsysProcess_WaitForExit(gp, 0);
2851 int result = 0;
2852 switch(kwsysProcess_GetState(gp))
2854 case kwsysProcess_State_Exited:
2856 result = kwsysProcess_GetExitValue(gp);
2857 } break;
2858 case kwsysProcess_State_Error:
2860 kwsys_ios::cerr << "Error: Could not run " << args[0] << ":\n";
2861 kwsys_ios::cerr << kwsysProcess_GetErrorString(gp) << "\n";
2862 } break;
2863 case kwsysProcess_State_Exception:
2865 kwsys_ios::cerr << "Error: " << args[0]
2866 << " terminated with an exception: "
2867 << kwsysProcess_GetExceptionString(gp) << "\n";
2868 } break;
2869 case kwsysProcess_State_Starting:
2870 case kwsysProcess_State_Executing:
2871 case kwsysProcess_State_Expired:
2872 case kwsysProcess_State_Killed:
2874 // Should not get here.
2875 kwsys_ios::cerr << "Unexpected ending state after running " << args[0]
2876 << kwsys_ios::endl;
2877 } break;
2879 kwsysProcess_Delete(gp);
2880 if(result)
2882 kwsys_ios::cerr << "Error " << args[0] << " returned :" << result << "\n";
2884 return buffer;
2888 kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const char* arguments)
2890 kwsys_stl::vector<const char*> args;
2891 args.clear();
2892 args.push_back("kstat");
2893 args.push_back("-p");
2895 kwsys_stl::string command = arguments;
2896 size_t start = command.npos;
2897 size_t pos = command.find(' ',0);
2898 while(pos!=command.npos)
2900 bool inQuotes = false;
2901 // Check if we are between quotes
2902 size_t b0 = command.find('"',0);
2903 size_t b1 = command.find('"',b0+1);
2904 while(b0 != command.npos && b1 != command.npos && b1>b0)
2906 if(pos>b0 && pos<b1)
2908 inQuotes = true;
2909 break;
2911 b0 = command.find('"',b1+1);
2912 b1 = command.find('"',b0+1);
2915 if(!inQuotes)
2917 kwsys_stl::string arg = command.substr(start+1,pos-start-1);
2919 // Remove the quotes if any
2920 size_t quotes = arg.find('"');
2921 while(quotes != arg.npos)
2923 arg.erase(quotes,1);
2924 quotes = arg.find('"');
2926 args.push_back(arg.c_str());
2927 start = pos;
2929 pos = command.find(' ',pos+1);
2931 kwsys_stl::string lastArg = command.substr(start+1,command.size()-start-1);
2932 args.push_back(lastArg.c_str());
2934 args.push_back(0);
2936 kwsys_stl::string buffer = this->RunProcess(args);
2938 kwsys_stl::string value = "";
2939 for(size_t i=buffer.size()-1;i>0;i--)
2941 if(buffer[i] == ' ' || buffer[i] == '\t')
2943 break;
2945 if(buffer[i] != '\n' && buffer[i] != '\r')
2947 kwsys_stl::string val = value;
2948 value = buffer[i];
2949 value += val;
2952 return value;
2955 /** Querying for system information from Solaris */
2956 bool SystemInformationImplementation::QuerySolarisInfo()
2958 // Parse values
2959 this->NumberOfPhysicalCPU = static_cast<unsigned int>(
2960 atoi(this->ParseValueFromKStat("-n syste_misc -s ncpus").c_str()));
2961 this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
2963 if(this->NumberOfPhysicalCPU!=0)
2965 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2968 this->CPUSpeedInMHz = static_cast<float>(atoi(this->ParseValueFromKStat("-s clock_MHz").c_str()));
2970 // Chip family
2971 this->ChipID.Family = 0;
2973 // Chip Vendor
2974 strcpy(this->ChipID.Vendor,"Sun");
2975 this->FindManufacturer();
2977 // Chip Model
2978 sprintf(this->ChipID.ProcessorName,"%s",this->ParseValueFromKStat("-s cpu_type").c_str());
2979 this->ChipID.Model = 0;
2981 // Cache size
2982 this->Features.L1CacheSize = 0;
2983 this->Features.L2CacheSize = 0;
2985 char* tail;
2986 unsigned long totalMemory =
2987 strtoul(this->ParseValueFromKStat("-s physmem").c_str(),&tail,0);
2988 this->TotalPhysicalMemory = totalMemory/1024;
2989 this->TotalPhysicalMemory *= 8192;
2990 this->TotalPhysicalMemory /= 1024;
2992 // Undefined values (for now at least)
2993 this->TotalVirtualMemory = 0;
2994 this->AvailablePhysicalMemory = 0;
2995 this->AvailableVirtualMemory = 0;
2997 return true;
3000 /** Querying for system information from Haiku OS */
3001 bool SystemInformationImplementation::QueryHaikuInfo()
3003 #if defined(__HAIKU__)
3005 system_info info;
3006 get_system_info(&info);
3008 this->NumberOfPhysicalCPU = info.cpu_count;
3009 this->CPUSpeedInMHz = info.cpu_clock_speed / 1000000.0F;
3011 // Physical Memory
3012 this->TotalPhysicalMemory = (info.max_pages * B_PAGE_SIZE) / (1024 * 1024) ;
3013 this->AvailablePhysicalMemory = this->TotalPhysicalMemory -
3014 ((info.used_pages * B_PAGE_SIZE) / (1024 * 1024));
3017 // NOTE: get_system_info_etc is currently a private call so just set to 0
3018 // until it becomes public
3019 this->TotalVirtualMemory = 0;
3020 this->AvailableVirtualMemory = 0;
3022 // Retrieve cpuid_info union for cpu 0
3023 cpuid_info cpu_info;
3024 get_cpuid(&cpu_info, 0, 0);
3026 // Chip Vendor
3027 // Use a temporary buffer so that we can add NULL termination to the string
3028 char vbuf[13];
3029 strncpy(vbuf, cpu_info.eax_0.vendor_id, 12);
3030 vbuf[12] = '\0';
3031 strcpy(this->ChipID.Vendor,vbuf);
3033 this->FindManufacturer();
3035 // Retrieve cpuid_info union for cpu 0 this time using a register value of 1
3036 get_cpuid(&cpu_info, 1, 0);
3038 this->NumberOfLogicalCPU = cpu_info.eax_1.logical_cpus;
3040 // Chip type
3041 this->ChipID.Type = cpu_info.eax_1.type;
3043 // Chip family
3044 this->ChipID.Family = cpu_info.eax_1.family;
3046 // Chip Model
3047 this->ChipID.Model = cpu_info.eax_1.model;
3049 // Chip Revision
3050 this->ChipID.Revision = cpu_info.eax_1.stepping;
3052 // Chip Extended Family
3053 this->ChipID.ExtendedFamily = cpu_info.eax_1.extended_family;
3055 // Chip Extended Model
3056 this->ChipID.ExtendedModel = cpu_info.eax_1.extended_model;
3058 // Get ChipID.ProcessorName from other information already gathered
3059 this->RetrieveClassicalCPUIdentity();
3061 // Cache size
3062 this->Features.L1CacheSize = 0;
3063 this->Features.L2CacheSize = 0;
3065 #endif
3066 return true;
3069 /** Query the operating system information */
3070 bool SystemInformationImplementation::QueryOSInformation()
3072 #if _WIN32
3074 this->OSName = "Windows";
3076 OSVERSIONINFOEX osvi;
3077 BOOL bIsWindows64Bit;
3078 BOOL bOsVersionInfoEx;
3079 char operatingSystem[256];
3081 // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
3082 ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX));
3083 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
3084 bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);
3085 if (!bOsVersionInfoEx)
3087 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
3088 if (!GetVersionEx ((OSVERSIONINFO *) &osvi))
3090 return false;
3094 switch (osvi.dwPlatformId)
3096 case VER_PLATFORM_WIN32_NT:
3097 // Test for the product.
3098 if (osvi.dwMajorVersion <= 4)
3100 this->OSRelease = "NT";
3102 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
3104 this->OSRelease = "2000";
3106 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3108 this->OSRelease = "XP";
3110 #ifdef VER_NT_WORKSTATION
3111 // Test for product type.
3112 if (bOsVersionInfoEx)
3114 if (osvi.wProductType == VER_NT_WORKSTATION)
3116 if (osvi.dwMajorVersion == 6)
3118 this->OSRelease = "Vista";
3120 // VER_SUITE_PERSONAL may not be defined
3121 #ifdef VER_SUITE_PERSONAL
3122 else
3124 if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
3126 this->OSRelease += " Personal";
3128 else
3130 this->OSRelease += " Professional";
3133 #endif
3135 else if (osvi.wProductType == VER_NT_SERVER)
3137 // Check for .NET Server instead of Windows XP.
3138 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3140 this->OSRelease = ".NET";
3143 // Continue with the type detection.
3144 if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
3146 this->OSRelease += " DataCenter Server";
3148 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
3150 this->OSRelease += " Advanced Server";
3152 else
3154 this->OSRelease += " Server";
3158 sprintf (operatingSystem, "%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
3159 this->OSVersion = operatingSystem;
3161 else
3162 #endif // VER_NT_WORKSTATION
3164 HKEY hKey;
3165 char szProductType[80];
3166 DWORD dwBufLen;
3168 // Query the registry to retrieve information.
3169 RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey);
3170 RegQueryValueEx (hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
3171 RegCloseKey (hKey);
3173 if (lstrcmpi ("WINNT", szProductType) == 0)
3175 this->OSRelease += " Professional";
3177 if (lstrcmpi ("LANMANNT", szProductType) == 0)
3179 // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
3180 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3182 this->OSRelease += " Standard Server";
3184 else
3186 this->OSRelease += " Server";
3189 if (lstrcmpi ("SERVERNT", szProductType) == 0)
3191 // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
3192 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3194 this->OSRelease += " Enterprise Server";
3196 else
3198 this->OSRelease += " Advanced Server";
3203 // Display version, service pack (if any), and build number.
3204 if (osvi.dwMajorVersion <= 4)
3206 // NB: NT 4.0 and earlier.
3207 sprintf (operatingSystem, "version %ld.%ld %s (Build %ld)",
3208 osvi.dwMajorVersion,
3209 osvi.dwMinorVersion,
3210 osvi.szCSDVersion,
3211 osvi.dwBuildNumber & 0xFFFF);
3212 this->OSVersion = operatingSystem;
3214 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3216 // Windows XP and .NET server.
3217 typedef BOOL (CALLBACK* LPFNPROC) (HANDLE, BOOL *);
3218 HINSTANCE hKernelDLL;
3219 LPFNPROC DLLProc;
3221 // Load the Kernel32 DLL.
3222 hKernelDLL = LoadLibrary ("kernel32");
3223 if (hKernelDLL != NULL) {
3224 // Only XP and .NET Server support IsWOW64Process so... Load dynamically!
3225 DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process");
3227 // If the function address is valid, call the function.
3228 if (DLLProc != NULL) (DLLProc) (GetCurrentProcess (), &bIsWindows64Bit);
3229 else bIsWindows64Bit = false;
3231 // Free the DLL module.
3232 FreeLibrary (hKernelDLL);
3235 else
3237 // Windows 2000 and everything else.
3238 sprintf (operatingSystem,"%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
3239 this->OSVersion = operatingSystem;
3241 break;
3243 case VER_PLATFORM_WIN32_WINDOWS:
3244 // Test for the product.
3245 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
3247 this->OSRelease = "95";
3248 if(osvi.szCSDVersion[1] == 'C')
3250 this->OSRelease += "OSR 2.5";
3252 else if(osvi.szCSDVersion[1] == 'B')
3254 this->OSRelease += "OSR 2";
3258 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
3260 this->OSRelease = "98";
3261 if (osvi.szCSDVersion[1] == 'A' )
3263 this->OSRelease += "SE";
3267 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
3269 this->OSRelease = "Me";
3271 break;
3273 case VER_PLATFORM_WIN32s:
3274 this->OSRelease = "Win32s";
3275 break;
3277 default:
3278 this->OSRelease = "Unknown";
3279 break;
3282 // Get the hostname
3283 WORD wVersionRequested;
3284 WSADATA wsaData;
3285 char name[255];
3286 wVersionRequested = MAKEWORD(2,0);
3288 if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
3290 gethostname(name,sizeof(name));
3291 WSACleanup( );
3293 this->Hostname = name;
3295 #else
3297 struct utsname unameInfo;
3298 int errorFlag = uname(&unameInfo);
3299 if(errorFlag == 0)
3301 this->OSName = unameInfo.sysname;
3302 this->Hostname = unameInfo.nodename;
3303 this->OSRelease = unameInfo.release;
3304 this->OSVersion = unameInfo.version;
3305 this->OSPlatform = unameInfo.machine;
3307 #endif
3309 return true;
3313 /** Return true if the machine is 64 bits */
3314 bool SystemInformationImplementation::Is64Bits()
3316 return (sizeof(void*) == 8);
3319 } // namespace @KWSYS_NAMESPACE@