Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / SystemInformation.cxx
blob48d58f980e8bc060ba6e430e88cb4254b19d36ca
1 /*=========================================================================
3 Program: BatchMake
4 Module: $RCSfile: SystemInformation.cxx,v $
5 Language: C++
6 Date: $Date: 2008/02/03 13:20:11 $
7 Version: $Revision: 1.22 $
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 #include "kwsysPrivate.h"
17 #include KWSYS_HEADER(FundamentalType.h)
18 #include KWSYS_HEADER(stl/string)
19 #include KWSYS_HEADER(stl/vector)
20 #include KWSYS_HEADER(ios/iosfwd)
21 #include KWSYS_HEADER(SystemInformation.hxx)
22 #include KWSYS_HEADER(Process.h)
23 #include KWSYS_HEADER(ios/iostream)
24 #include KWSYS_HEADER(ios/sstream)
25 // Work-around CMake dependency scanning limitation. This must
26 // duplicate the above list of headers.
27 #if 0
28 # include "FundamentalType.h.in"
29 # include "SystemInformation.hxx.in"
30 # include "Process.h.in"
31 # include "Configure.hxx.in"
32 # include "kwsys_stl.hxx.in"
33 # include "kwsys_stl_vector.in"
34 # include "kwsys_stl_iosfwd.in"
35 # include "kwsys_ios_sstream.h.in"
36 # include "kwsys_ios_iostream.h.in"
37 #endif
40 #ifndef WIN32
41 # include <sys/utsname.h> // int uname(struct utsname *buf);
42 #endif
44 #ifdef _WIN32
45 # include <windows.h>
46 #endif
48 #ifdef __linux
49 # include <sys/procfs.h>
50 # include <sys/types.h>
51 # include <unistd.h>
52 # include <fcntl.h>
53 # include <ctype.h> // int isdigit(int c);
54 # include <errno.h> // extern int errno;
55 # include <sys/time.h>
56 #elif __hpux
57 # include <sys/param.h>
58 # include <sys/pstat.h>
59 #endif
61 #include <memory.h>
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <string.h>
68 namespace KWSYS_NAMESPACE
71 // Create longlong
72 #if KWSYS_USE_LONG_LONG
73 typedef long long LongLong;
74 #elif KWSYS_USE___INT64
75 typedef __int64 LongLong;
76 #else
77 # error "No Long Long"
78 #endif
80 // Define SystemInformationImplementation class
81 typedef void (*DELAY_FUNC)(unsigned int uiMS);
83 class SystemInformationImplementation
86 public:
87 SystemInformationImplementation ();
88 ~SystemInformationImplementation ();
90 const char * GetVendorString();
91 const char * GetVendorID();
92 kwsys_stl::string GetTypeID();
93 kwsys_stl::string GetFamilyID();
94 kwsys_stl::string GetModelID();
95 kwsys_stl::string GetSteppingCode();
96 const char * GetExtendedProcessorName();
97 const char * GetProcessorSerialNumber();
98 int GetProcessorCacheSize();
99 int GetLogicalProcessorsPerPhysical();
100 float GetProcessorClockFrequency();
101 int GetProcessorAPICID();
102 int GetProcessorCacheXSize(long int);
103 bool DoesCPUSupportFeature(long int);
105 const char * GetOSName();
106 const char * GetHostname();
107 const char * GetOSRelease();
108 const char * GetOSVersion();
109 const char * GetOSPlatform();
111 bool Is64Bits();
113 unsigned int GetNumberOfLogicalCPU(); // per physical cpu
114 unsigned int GetNumberOfPhysicalCPU();
116 bool DoesCPUSupportCPUID();
118 // Retrieve memory information in megabyte.
119 unsigned long GetTotalVirtualMemory();
120 unsigned long GetAvailableVirtualMemory();
121 unsigned long GetTotalPhysicalMemory();
122 unsigned long GetAvailablePhysicalMemory();
124 /** Run the different checks */
125 void RunCPUCheck();
126 void RunOSCheck();
127 void RunMemoryCheck();
128 public:
129 #define VENDOR_STRING_LENGTH (12 + 1)
130 #define CHIPNAME_STRING_LENGTH (48 + 1)
131 #define SERIALNUMBER_STRING_LENGTH (29 + 1)
133 typedef struct tagID
135 int Type;
136 int Family;
137 int Model;
138 int Revision;
139 int ExtendedFamily;
140 int ExtendedModel;
141 char ProcessorName[CHIPNAME_STRING_LENGTH];
142 char Vendor[VENDOR_STRING_LENGTH];
143 char SerialNumber[SERIALNUMBER_STRING_LENGTH];
144 } ID;
146 typedef struct tagCPUPowerManagement
148 bool HasVoltageID;
149 bool HasFrequencyID;
150 bool HasTempSenseDiode;
151 } CPUPowerManagement;
153 typedef struct tagCPUExtendedFeatures
155 bool Has3DNow;
156 bool Has3DNowPlus;
157 bool SupportsMP;
158 bool HasMMXPlus;
159 bool HasSSEMMX;
160 bool SupportsHyperthreading;
161 int LogicalProcessorsPerPhysical;
162 int APIC_ID;
163 CPUPowerManagement PowerManagement;
164 } CPUExtendedFeatures;
166 typedef struct CPUtagFeatures
168 bool HasFPU;
169 bool HasTSC;
170 bool HasMMX;
171 bool HasSSE;
172 bool HasSSEFP;
173 bool HasSSE2;
174 bool HasIA64;
175 bool HasAPIC;
176 bool HasCMOV;
177 bool HasMTRR;
178 bool HasACPI;
179 bool HasSerial;
180 bool HasThermal;
181 int CPUSpeed;
182 int L1CacheSize;
183 int L2CacheSize;
184 int L3CacheSize;
185 CPUExtendedFeatures ExtendedFeatures;
186 } CPUFeatures;
188 enum Manufacturer
190 AMD, Intel, NSC, UMC, Cyrix, NexGen, IDT, Rise, Transmeta, Sun, UnknownManufacturer
192 protected:
194 // Functions.
195 bool RetrieveCPUFeatures();
196 bool RetrieveCPUIdentity();
197 bool RetrieveCPUCacheDetails();
198 bool RetrieveClassicalCPUCacheDetails();
199 bool RetrieveCPUClockSpeed();
200 bool RetrieveClassicalCPUClockSpeed();
201 bool RetrieveCPUExtendedLevelSupport(int);
202 bool RetrieveExtendedCPUFeatures();
203 bool RetrieveProcessorSerialNumber();
204 bool RetrieveCPUPowerManagement();
205 bool RetrieveClassicalCPUIdentity();
206 bool RetrieveExtendedCPUIdentity();
208 Manufacturer ChipManufacturer;
209 CPUFeatures Features;
210 ID ChipID;
211 float CPUSpeedInMHz;
212 unsigned int NumberOfLogicalCPU;
213 unsigned int NumberOfPhysicalCPU;
215 int CPUCount();
216 unsigned char LogicalCPUPerPhysicalCPU();
217 unsigned char GetAPICId();
218 unsigned int IsHyperThreadingSupported();
219 LongLong GetCyclesDifference(DELAY_FUNC, unsigned int);
221 // For Linux
222 int RetreiveInformationFromCpuInfoFile();
223 kwsys_stl::string ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,
224 const char* word, size_t init=0);
226 static void Delay (unsigned int);
227 static void DelayOverhead (unsigned int);
229 void FindManufacturer();
231 // For Mac
232 bool ParseSysCtl();
233 kwsys_stl::string ExtractValueFromSysCtl(const char* word);
234 kwsys_stl::string SysCtlBuffer;
236 // For Solaris
237 bool QuerySolarisInfo();
238 kwsys_stl::string ParseValueFromKStat(const char* arguments);
239 kwsys_stl::string RunProcess(kwsys_stl::vector<const char*> args);
241 // Evaluate the memory information.
242 int QueryMemory();
243 unsigned long TotalVirtualMemory;
244 unsigned long AvailableVirtualMemory;
245 unsigned long TotalPhysicalMemory;
246 unsigned long AvailablePhysicalMemory;
248 size_t CurrentPositionInFile;
250 // Operating System information
251 bool QueryOSInformation();
252 kwsys_stl::string OSName;
253 kwsys_stl::string Hostname;
254 kwsys_stl::string OSRelease;
255 kwsys_stl::string OSVersion;
256 kwsys_stl::string OSPlatform;
263 SystemInformation::SystemInformation()
265 this->Implementation = new SystemInformationImplementation;
268 SystemInformation::~SystemInformation ()
270 delete this->Implementation;
273 const char * SystemInformation::GetVendorString()
275 return this->Implementation->GetVendorString();
277 const char * SystemInformation::GetVendorID()
279 return this->Implementation->GetVendorID();
281 kwsys_stl::string SystemInformation::GetTypeID()
283 return this->Implementation->GetTypeID();
285 kwsys_stl::string SystemInformation::GetFamilyID()
287 return this->Implementation->GetFamilyID();
289 kwsys_stl::string SystemInformation::GetModelID()
291 return this->Implementation->GetModelID();
293 kwsys_stl::string SystemInformation::GetSteppingCode()
295 return this->Implementation->GetSteppingCode();
297 const char * SystemInformation::GetExtendedProcessorName()
299 return this->Implementation->GetExtendedProcessorName();
301 const char * SystemInformation::GetProcessorSerialNumber()
303 return this->Implementation->GetProcessorSerialNumber();
305 int SystemInformation::GetProcessorCacheSize()
307 return this->Implementation->GetProcessorCacheSize();
309 int SystemInformation::GetLogicalProcessorsPerPhysical()
311 return this->Implementation->GetLogicalProcessorsPerPhysical();
313 float SystemInformation::GetProcessorClockFrequency()
315 return this->Implementation->GetProcessorClockFrequency();
317 int SystemInformation::GetProcessorAPICID()
319 return this->Implementation->GetProcessorAPICID();
321 int SystemInformation::GetProcessorCacheXSize(long int l)
323 return this->Implementation->GetProcessorCacheXSize(l);
325 bool SystemInformation::DoesCPUSupportFeature(long int i)
327 return this->Implementation->DoesCPUSupportFeature(i);
330 const char * SystemInformation::GetOSName()
332 return this->Implementation->GetOSName();
334 const char * SystemInformation::GetHostname()
336 return this->Implementation->GetHostname();
338 const char * SystemInformation::GetOSRelease()
340 return this->Implementation->GetOSRelease();
342 const char * SystemInformation::GetOSVersion()
344 return this->Implementation->GetOSVersion();
346 const char * SystemInformation::GetOSPlatform()
348 return this->Implementation->GetOSPlatform();
351 bool SystemInformation::Is64Bits()
353 return this->Implementation->Is64Bits();
356 unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
358 return this->Implementation->GetNumberOfLogicalCPU();
360 unsigned int SystemInformation::GetNumberOfPhysicalCPU()
362 return this->Implementation->GetNumberOfPhysicalCPU();
365 bool SystemInformation::DoesCPUSupportCPUID()
367 return this->Implementation->DoesCPUSupportCPUID();
370 // Retrieve memory information in megabyte.
371 unsigned long SystemInformation::GetTotalVirtualMemory()
373 return this->Implementation->GetTotalVirtualMemory();
375 unsigned long SystemInformation::GetAvailableVirtualMemory()
377 return this->Implementation->GetAvailableVirtualMemory();
379 unsigned long SystemInformation::GetTotalPhysicalMemory()
381 return this->Implementation->GetTotalPhysicalMemory();
384 unsigned long SystemInformation::GetAvailablePhysicalMemory()
386 return this->Implementation->GetAvailablePhysicalMemory();
389 /** Run the different checks */
390 void SystemInformation::RunCPUCheck()
392 this->Implementation->RunCPUCheck();
394 void SystemInformation::RunOSCheck()
396 this->Implementation->RunOSCheck();
398 void SystemInformation::RunMemoryCheck()
400 this->Implementation->RunMemoryCheck();
405 // --------------------------------------------------------------
406 // SystemInformationImplementation starts here
408 #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64)
409 #define USE_ASM_INSTRUCTIONS 1
410 #else
411 #define USE_ASM_INSTRUCTIONS 0
412 #endif
414 #define STORE_TLBCACHE_INFO(x,y) x = (x < y) ? y : x
415 #define TLBCACHE_INFO_UNITS (15)
416 #define CLASSICAL_CPU_FREQ_LOOP 10000000
417 #define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
419 #define CPUID_AWARE_COMPILER
420 #ifdef CPUID_AWARE_COMPILER
421 #define CPUID_INSTRUCTION cpuid
422 #else
423 #define CPUID_INSTRUCTION _asm _emit 0x0f _asm _emit 0xa2
424 #endif
426 #define MMX_FEATURE 0x00000001
427 #define MMX_PLUS_FEATURE 0x00000002
428 #define SSE_FEATURE 0x00000004
429 #define SSE2_FEATURE 0x00000008
430 #define AMD_3DNOW_FEATURE 0x00000010
431 #define AMD_3DNOW_PLUS_FEATURE 0x00000020
432 #define IA64_FEATURE 0x00000040
433 #define MP_CAPABLE 0x00000080
434 #define HYPERTHREAD_FEATURE 0x00000100
435 #define SERIALNUMBER_FEATURE 0x00000200
436 #define APIC_FEATURE 0x00000400
437 #define SSE_FP_FEATURE 0x00000800
438 #define SSE_MMX_FEATURE 0x00001000
439 #define CMOV_FEATURE 0x00002000
440 #define MTRR_FEATURE 0x00004000
441 #define L1CACHE_FEATURE 0x00008000
442 #define L2CACHE_FEATURE 0x00010000
443 #define L3CACHE_FEATURE 0x00020000
444 #define ACPI_FEATURE 0x00040000
445 #define THERMALMONITOR_FEATURE 0x00080000
446 #define TEMPSENSEDIODE_FEATURE 0x00100000
447 #define FREQUENCYID_FEATURE 0x00200000
448 #define VOLTAGEID_FREQUENCY 0x00400000
450 // Status Flag
451 #define HT_NOT_CAPABLE 0
452 #define HT_ENABLED 1
453 #define HT_DISABLED 2
454 #define HT_SUPPORTED_NOT_ENABLED 3
455 #define HT_CANNOT_DETECT 4
457 // EDX[28] Bit 28 is set if HT is supported
458 #define HT_BIT 0x10000000
460 // EAX[11:8] Bit 8-11 contains family processor ID.
461 #define FAMILY_ID 0x0F00
462 #define PENTIUM4_ID 0x0F00
463 // EAX[23:20] Bit 20-23 contains extended family processor ID
464 #define EXT_FAMILY_ID 0x0F00000
465 // EBX[23:16] Bit 16-23 in ebx contains the number of logical
466 #define NUM_LOGICAL_BITS 0x00FF0000
467 // processors per physical processor when execute cpuid with
468 // eax set to 1
469 // EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
470 #define INITIAL_APIC_ID_BITS 0xFF000000
471 // initial APIC ID for the processor this code is running on.
472 // Default value = 0xff if HT is not supported
476 SystemInformationImplementation::SystemInformationImplementation()
478 this->TotalVirtualMemory = 0;
479 this->AvailableVirtualMemory = 0;
480 this->TotalPhysicalMemory = 0;
481 this->AvailablePhysicalMemory = 0;
482 this->CurrentPositionInFile = 0;
483 this->ChipManufacturer = UnknownManufacturer;
484 memset(&this->Features, 0, sizeof(CPUFeatures));
485 memset(&this->ChipID, 0, sizeof(ID));
486 this->CPUSpeedInMHz = 0;
487 this->NumberOfLogicalCPU = 0;
488 this->NumberOfPhysicalCPU = 0;
489 this->OSName = "";
490 this->Hostname = "";
491 this->OSRelease = "";
492 this->OSVersion = "";
493 this->OSPlatform = "";
496 SystemInformationImplementation::~SystemInformationImplementation()
500 void SystemInformationImplementation::RunCPUCheck()
502 #ifdef WIN32
503 // Check to see if this processor supports CPUID.
504 if (DoesCPUSupportCPUID())
506 // Retrieve the CPU details.
507 RetrieveCPUIdentity();
508 RetrieveCPUFeatures();
509 if (!RetrieveCPUClockSpeed())
511 RetrieveClassicalCPUClockSpeed();
514 // Attempt to retrieve cache information.
515 if (!RetrieveCPUCacheDetails())
517 RetrieveClassicalCPUCacheDetails();
519 // Retrieve the extended CPU details.
520 if (!RetrieveExtendedCPUIdentity())
522 RetrieveClassicalCPUIdentity();
524 RetrieveExtendedCPUFeatures();
526 // Now attempt to retrieve the serial number (if possible).
527 RetrieveProcessorSerialNumber();
529 this->CPUCount();
530 #elif defined(__APPLE__)
531 this->ParseSysCtl();
532 #elif defined (__SVR4) && defined (__sun)
533 this->QuerySolarisInfo();
534 #else
535 this->RetreiveInformationFromCpuInfoFile();
536 #endif
539 void SystemInformationImplementation::RunOSCheck()
541 this->QueryOSInformation();
544 void SystemInformationImplementation::RunMemoryCheck()
546 #if defined(__APPLE__)
547 this->ParseSysCtl();
548 #elif defined (__SVR4) && defined (__sun)
549 this->QuerySolarisInfo();
550 #else
551 this->QueryMemory();
552 #endif
555 /** Get the vendor string */
556 const char * SystemInformationImplementation::GetVendorString()
558 return this->ChipID.Vendor;
561 /** Get the OS Name */
562 const char * SystemInformationImplementation::GetOSName()
564 return this->OSName.c_str();
567 /** Get the hostname */
568 const char* SystemInformationImplementation::GetHostname()
570 return this->Hostname.c_str();
573 /** Get the OS release */
574 const char* SystemInformationImplementation::GetOSRelease()
576 return this->OSRelease.c_str();
579 /** Get the OS version */
580 const char* SystemInformationImplementation::GetOSVersion()
582 return this->OSVersion.c_str();
585 /** Get the OS platform */
586 const char* SystemInformationImplementation::GetOSPlatform()
588 return this->OSPlatform.c_str();
591 /** Get the vendor ID */
592 const char * SystemInformationImplementation::GetVendorID()
594 // Return the vendor ID.
595 switch (this->ChipManufacturer)
597 case Intel:
598 return "Intel Corporation";
599 case AMD:
600 return "Advanced Micro Devices";
601 case NSC:
602 return "National Semiconductor";
603 case Cyrix:
604 return "Cyrix Corp., VIA Inc.";
605 case NexGen:
606 return "NexGen Inc., Advanced Micro Devices";
607 case IDT:
608 return "IDT\\Centaur, Via Inc.";
609 case UMC:
610 return "United Microelectronics Corp.";
611 case Rise:
612 return "Rise";
613 case Transmeta:
614 return "Transmeta";
615 case Sun:
616 return "Sun Microelectronics";
617 default:
618 return "Unknown Manufacturer";
622 /** Return the type ID of the CPU */
623 kwsys_stl::string SystemInformationImplementation::GetTypeID()
625 kwsys_ios::ostringstream str;
626 str << this->ChipID.Type;
627 return str.str();
630 /** Return the family of the CPU present */
631 kwsys_stl::string SystemInformationImplementation::GetFamilyID()
633 kwsys_ios::ostringstream str;
634 str << this->ChipID.Family;
635 return str.str();
638 // Return the model of CPU present */
639 kwsys_stl::string SystemInformationImplementation::GetModelID()
641 kwsys_ios::ostringstream str;
642 str << this->ChipID.Model;
643 return str.str();
646 /** Return the stepping code of the CPU present. */
647 kwsys_stl::string SystemInformationImplementation::GetSteppingCode()
649 kwsys_ios::ostringstream str;
650 str << this->ChipID.Revision;
651 return str.str();
654 /** Return the stepping code of the CPU present. */
655 const char * SystemInformationImplementation::GetExtendedProcessorName()
657 return this->ChipID.ProcessorName;
660 /** Return the serial number of the processor
661 * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
662 const char * SystemInformationImplementation::GetProcessorSerialNumber()
664 return this->ChipID.SerialNumber;
667 /** Return the logical processors per physical */
668 int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
670 return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
673 /** Return the processor clock frequency. */
674 float SystemInformationImplementation::GetProcessorClockFrequency()
676 return this->CPUSpeedInMHz;
679 /** Return the APIC ID. */
680 int SystemInformationImplementation::GetProcessorAPICID()
682 return this->Features.ExtendedFeatures.APIC_ID;
685 /** Return the L1 cache size. */
686 int SystemInformationImplementation::GetProcessorCacheSize()
688 return this->Features.L1CacheSize;
691 /** Return the chosen cache size. */
692 int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
694 switch (dwCacheID)
696 case L1CACHE_FEATURE:
697 return this->Features.L1CacheSize;
698 case L2CACHE_FEATURE:
699 return this->Features.L2CacheSize;
700 case L3CACHE_FEATURE:
701 return this->Features.L3CacheSize;
703 return -1;
706 bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
708 bool bHasFeature = false;
710 // Check for MMX instructions.
711 if (((dwFeature & MMX_FEATURE) != 0) && this->Features.HasMMX) bHasFeature = true;
713 // Check for MMX+ instructions.
714 if (((dwFeature & MMX_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.HasMMXPlus) bHasFeature = true;
716 // Check for SSE FP instructions.
717 if (((dwFeature & SSE_FEATURE) != 0) && this->Features.HasSSE) bHasFeature = true;
719 // Check for SSE FP instructions.
720 if (((dwFeature & SSE_FP_FEATURE) != 0) && this->Features.HasSSEFP) bHasFeature = true;
722 // Check for SSE MMX instructions.
723 if (((dwFeature & SSE_MMX_FEATURE) != 0) && this->Features.ExtendedFeatures.HasSSEMMX) bHasFeature = true;
725 // Check for SSE2 instructions.
726 if (((dwFeature & SSE2_FEATURE) != 0) && this->Features.HasSSE2) bHasFeature = true;
728 // Check for 3DNow! instructions.
729 if (((dwFeature & AMD_3DNOW_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNow) bHasFeature = true;
731 // Check for 3DNow+ instructions.
732 if (((dwFeature & AMD_3DNOW_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNowPlus) bHasFeature = true;
734 // Check for IA64 instructions.
735 if (((dwFeature & IA64_FEATURE) != 0) && this->Features.HasIA64) bHasFeature = true;
737 // Check for MP capable.
738 if (((dwFeature & MP_CAPABLE) != 0) && this->Features.ExtendedFeatures.SupportsMP) bHasFeature = true;
740 // Check for a serial number for the processor.
741 if (((dwFeature & SERIALNUMBER_FEATURE) != 0) && this->Features.HasSerial) bHasFeature = true;
743 // Check for a local APIC in the processor.
744 if (((dwFeature & APIC_FEATURE) != 0) && this->Features.HasAPIC) bHasFeature = true;
746 // Check for CMOV instructions.
747 if (((dwFeature & CMOV_FEATURE) != 0) && this->Features.HasCMOV) bHasFeature = true;
749 // Check for MTRR instructions.
750 if (((dwFeature & MTRR_FEATURE) != 0) && this->Features.HasMTRR) bHasFeature = true;
752 // Check for L1 cache size.
753 if (((dwFeature & L1CACHE_FEATURE) != 0) && (this->Features.L1CacheSize != -1)) bHasFeature = true;
755 // Check for L2 cache size.
756 if (((dwFeature & L2CACHE_FEATURE) != 0) && (this->Features.L2CacheSize != -1)) bHasFeature = true;
758 // Check for L3 cache size.
759 if (((dwFeature & L3CACHE_FEATURE) != 0) && (this->Features.L3CacheSize != -1)) bHasFeature = true;
761 // Check for ACPI capability.
762 if (((dwFeature & ACPI_FEATURE) != 0) && this->Features.HasACPI) bHasFeature = true;
764 // Check for thermal monitor support.
765 if (((dwFeature & THERMALMONITOR_FEATURE) != 0) && this->Features.HasThermal) bHasFeature = true;
767 // Check for temperature sensing diode support.
768 if (((dwFeature & TEMPSENSEDIODE_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode) bHasFeature = true;
770 // Check for frequency ID support.
771 if (((dwFeature & FREQUENCYID_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID) bHasFeature = true;
773 // Check for voltage ID support.
774 if (((dwFeature & VOLTAGEID_FREQUENCY) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasVoltageID) bHasFeature = true;
776 return bHasFeature;
779 void SystemInformationImplementation::Delay(unsigned int uiMS)
781 #ifdef WIN32
782 LARGE_INTEGER Frequency, StartCounter, EndCounter;
783 __int64 x;
785 // Get the frequency of the high performance counter.
786 if (!QueryPerformanceFrequency (&Frequency)) return;
787 x = Frequency.QuadPart / 1000 * uiMS;
789 // Get the starting position of the counter.
790 QueryPerformanceCounter (&StartCounter);
792 do {
793 // Get the ending position of the counter.
794 QueryPerformanceCounter (&EndCounter);
795 } while (EndCounter.QuadPart - StartCounter.QuadPart < x);
796 #endif
797 (void)uiMS;
800 bool SystemInformationImplementation::DoesCPUSupportCPUID()
802 int CPUIDPresent = 0;
804 #if USE_ASM_INSTRUCTIONS
805 // Use SEH to determine CPUID presence
806 __try {
807 _asm {
808 #ifdef CPUID_AWARE_COMPILER
809 ; we must push/pop the registers <<CPUID>> writes to, as the
810 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
811 ; these registers to change.
812 push eax
813 push ebx
814 push ecx
815 push edx
816 #endif
817 ; <<CPUID>>
818 mov eax, 0
819 CPUID_INSTRUCTION
821 #ifdef CPUID_AWARE_COMPILER
822 pop edx
823 pop ecx
824 pop ebx
825 pop eax
826 #endif
829 __except(1)
831 // Stop the class from trying to use CPUID again!
832 CPUIDPresent = false;
833 return false;
835 #else
836 CPUIDPresent = false;
837 #endif
839 // Return true to indicate support or false to indicate lack.
840 return (CPUIDPresent == 0) ? true : false;
843 bool SystemInformationImplementation::RetrieveCPUFeatures()
845 #if USE_ASM_INSTRUCTIONS
846 int localCPUFeatures = 0;
847 int localCPUAdvanced = 0;
850 // Use assembly to detect CPUID information...
851 __try {
852 _asm {
853 #ifdef CPUID_AWARE_COMPILER
854 ; we must push/pop the registers <<CPUID>> writes to, as the
855 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
856 ; these registers to change.
857 push eax
858 push ebx
859 push ecx
860 push edx
861 #endif
862 ; <<CPUID>>
863 ; 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
864 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
865 ; edx: CPU feature flags
866 mov eax,1
867 CPUID_INSTRUCTION
868 mov localCPUFeatures, edx
869 mov localCPUAdvanced, ebx
871 #ifdef CPUID_AWARE_COMPILER
872 pop edx
873 pop ecx
874 pop ebx
875 pop eax
876 #endif
879 __except(1)
881 return false;
884 // Retrieve the features of CPU present.
885 this->Features.HasFPU = ((localCPUFeatures & 0x00000001) != 0); // FPU Present --> Bit 0
886 this->Features.HasTSC = ((localCPUFeatures & 0x00000010) != 0); // TSC Present --> Bit 4
887 this->Features.HasAPIC = ((localCPUFeatures & 0x00000200) != 0); // APIC Present --> Bit 9
888 this->Features.HasMTRR = ((localCPUFeatures & 0x00001000) != 0); // MTRR Present --> Bit 12
889 this->Features.HasCMOV = ((localCPUFeatures & 0x00008000) != 0); // CMOV Present --> Bit 15
890 this->Features.HasSerial = ((localCPUFeatures & 0x00040000) != 0); // Serial Present --> Bit 18
891 this->Features.HasACPI = ((localCPUFeatures & 0x00400000) != 0); // ACPI Capable --> Bit 22
892 this->Features.HasMMX = ((localCPUFeatures & 0x00800000) != 0); // MMX Present --> Bit 23
893 this->Features.HasSSE = ((localCPUFeatures & 0x02000000) != 0); // SSE Present --> Bit 25
894 this->Features.HasSSE2 = ((localCPUFeatures & 0x04000000) != 0); // SSE2 Present --> Bit 26
895 this->Features.HasThermal = ((localCPUFeatures & 0x20000000) != 0); // Thermal Monitor Present --> Bit 29
896 this->Features.HasIA64 = ((localCPUFeatures & 0x40000000) != 0); // IA64 Present --> Bit 30
898 // Retrieve extended SSE capabilities if SSE is available.
899 if (this->Features.HasSSE) {
901 // Attempt to __try some SSE FP instructions.
902 __try
904 // Perform: orps xmm0, xmm0
905 _asm
907 _emit 0x0f
908 _emit 0x56
909 _emit 0xc0
912 // SSE FP capable processor.
913 this->Features.HasSSEFP = true;
915 __except(1)
917 // bad instruction - processor or OS cannot handle SSE FP.
918 this->Features.HasSSEFP = false;
921 else
923 // Set the advanced SSE capabilities to not available.
924 this->Features.HasSSEFP = false;
927 // Retrieve Intel specific extended features.
928 if (this->ChipManufacturer == Intel)
930 this->Features.ExtendedFeatures.SupportsHyperthreading = ((localCPUFeatures & 0x10000000) != 0); // Intel specific: Hyperthreading --> Bit 28
931 this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = (this->Features.ExtendedFeatures.SupportsHyperthreading) ? ((localCPUAdvanced & 0x00FF0000) >> 16) : 1;
933 if ((this->Features.ExtendedFeatures.SupportsHyperthreading) && (this->Features.HasAPIC))
935 // Retrieve APIC information if there is one present.
936 this->Features.ExtendedFeatures.APIC_ID = ((localCPUAdvanced & 0xFF000000) >> 24);
939 #endif
940 return true;
944 /** Find the manufacturer given the vendor id */
945 void SystemInformationImplementation::FindManufacturer()
947 if (strcmp (this->ChipID.Vendor, "GenuineIntel") == 0) this->ChipManufacturer = Intel; // Intel Corp.
948 else if (strcmp (this->ChipID.Vendor, "UMC UMC UMC ") == 0) this->ChipManufacturer = UMC; // United Microelectronics Corp.
949 else if (strcmp (this->ChipID.Vendor, "AuthenticAMD") == 0) this->ChipManufacturer = AMD; // Advanced Micro Devices
950 else if (strcmp (this->ChipID.Vendor, "AMD ISBETTER") == 0) this->ChipManufacturer = AMD; // Advanced Micro Devices (1994)
951 else if (strcmp (this->ChipID.Vendor, "CyrixInstead") == 0) this->ChipManufacturer = Cyrix; // Cyrix Corp., VIA Inc.
952 else if (strcmp (this->ChipID.Vendor, "NexGenDriven") == 0) this->ChipManufacturer = NexGen; // NexGen Inc. (now AMD)
953 else if (strcmp (this->ChipID.Vendor, "CentaurHauls") == 0) this->ChipManufacturer = IDT; // IDT/Centaur (now VIA)
954 else if (strcmp (this->ChipID.Vendor, "RiseRiseRise") == 0) this->ChipManufacturer = Rise; // Rise
955 else if (strcmp (this->ChipID.Vendor, "GenuineTMx86") == 0) this->ChipManufacturer = Transmeta; // Transmeta
956 else if (strcmp (this->ChipID.Vendor, "TransmetaCPU") == 0) this->ChipManufacturer = Transmeta; // Transmeta
957 else if (strcmp (this->ChipID.Vendor, "Geode By NSC") == 0) this->ChipManufacturer = NSC; // National Semiconductor
958 else if (strcmp (this->ChipID.Vendor, "Sun") == 0) this->ChipManufacturer = Sun; // Sun Microelectronics
959 else this->ChipManufacturer = UnknownManufacturer; // Unknown manufacturer
962 /** */
963 bool SystemInformationImplementation::RetrieveCPUIdentity()
965 #if USE_ASM_INSTRUCTIONS
966 int localCPUVendor[3];
967 int localCPUSignature;
969 // Use assembly to detect CPUID information...
970 __try
972 _asm
974 #ifdef CPUID_AWARE_COMPILER
975 ; we must push/pop the registers <<CPUID>> writes to, as the
976 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
977 ; these registers to change.
978 push eax
979 push ebx
980 push ecx
981 push edx
982 #endif
983 ; <<CPUID>>
984 ; eax = 0 --> eax: maximum value of CPUID instruction.
985 ; ebx: part 1 of 3; CPU signature.
986 ; edx: part 2 of 3; CPU signature.
987 ; ecx: part 3 of 3; CPU signature.
988 mov eax, 0
989 CPUID_INSTRUCTION
990 mov localCPUVendor[0 * TYPE int], ebx
991 mov localCPUVendor[1 * TYPE int], edx
992 mov localCPUVendor[2 * TYPE int], ecx
994 ; <<CPUID>>
995 ; 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
996 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
997 ; edx: CPU feature flags
998 mov eax,1
999 CPUID_INSTRUCTION
1000 mov localCPUSignature, eax
1002 #ifdef CPUID_AWARE_COMPILER
1003 pop edx
1004 pop ecx
1005 pop ebx
1006 pop eax
1007 #endif
1010 __except(1)
1012 return false;
1015 // Process the returned information.
1016 memcpy (this->ChipID.Vendor, &(localCPUVendor[0]), sizeof (int));
1017 memcpy (&(this->ChipID.Vendor[4]), &(localCPUVendor[1]), sizeof (int));
1018 memcpy (&(this->ChipID.Vendor[8]), &(localCPUVendor[2]), sizeof (int));
1019 this->ChipID.Vendor[12] = '\0';
1021 this->FindManufacturer();
1023 // Retrieve the family of CPU present.
1024 this->ChipID.ExtendedFamily = ((localCPUSignature & 0x0FF00000) >> 20); // Bits 27..20 Used
1025 this->ChipID.ExtendedModel = ((localCPUSignature & 0x000F0000) >> 16); // Bits 19..16 Used
1026 this->ChipID.Type = ((localCPUSignature & 0x0000F000) >> 12); // Bits 15..12 Used
1027 this->ChipID.Family = ((localCPUSignature & 0x00000F00) >> 8); // Bits 11..8 Used
1028 this->ChipID.Model = ((localCPUSignature & 0x000000F0) >> 4); // Bits 7..4 Used
1029 this->ChipID.Revision = ((localCPUSignature & 0x0000000F) >> 0); // Bits 3..0 Used
1030 #endif
1032 return true;
1035 /** */
1036 bool SystemInformationImplementation::RetrieveCPUCacheDetails()
1038 #if USE_ASM_INSTRUCTIONS
1039 int L1Cache[4] = { 0, 0, 0, 0 };
1040 int L2Cache[4] = { 0, 0, 0, 0 };
1042 // Check to see if what we are about to do is supported...
1043 if (RetrieveCPUExtendedLevelSupport (0x80000005))
1045 // Use assembly to retrieve the L1 cache information ...
1046 __try
1048 _asm
1050 #ifdef CPUID_AWARE_COMPILER
1051 ; we must push/pop the registers <<CPUID>> writes to, as the
1052 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1053 ; these registers to change.
1054 push eax
1055 push ebx
1056 push ecx
1057 push edx
1058 #endif
1059 ; <<CPUID>>
1060 ; eax = 0x80000005 --> eax: L1 cache information - Part 1 of 4.
1061 ; ebx: L1 cache information - Part 2 of 4.
1062 ; edx: L1 cache information - Part 3 of 4.
1063 ; ecx: L1 cache information - Part 4 of 4.
1064 mov eax, 0x80000005
1065 CPUID_INSTRUCTION
1066 mov L1Cache[0 * TYPE int], eax
1067 mov L1Cache[1 * TYPE int], ebx
1068 mov L1Cache[2 * TYPE int], ecx
1069 mov L1Cache[3 * TYPE int], edx
1071 #ifdef CPUID_AWARE_COMPILER
1072 pop edx
1073 pop ecx
1074 pop ebx
1075 pop eax
1076 #endif
1079 __except(1)
1081 return false;
1083 // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as data cache size from edx: bits 31..24.
1084 this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24);
1085 this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24);
1087 else
1089 // Store -1 to indicate the cache could not be queried.
1090 this->Features.L1CacheSize = -1;
1093 // Check to see if what we are about to do is supported...
1094 if (RetrieveCPUExtendedLevelSupport (0x80000006))
1096 // Use assembly to retrieve the L2 cache information ...
1097 __try
1099 _asm
1101 #ifdef CPUID_AWARE_COMPILER
1102 ; we must push/pop the registers <<CPUID>> writes to, as the
1103 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1104 ; these registers to change.
1105 push eax
1106 push ebx
1107 push ecx
1108 push edx
1109 #endif
1110 ; <<CPUID>>
1111 ; eax = 0x80000006 --> eax: L2 cache information - Part 1 of 4.
1112 ; ebx: L2 cache information - Part 2 of 4.
1113 ; edx: L2 cache information - Part 3 of 4.
1114 ; ecx: L2 cache information - Part 4 of 4.
1115 mov eax, 0x80000006
1116 CPUID_INSTRUCTION
1117 mov L2Cache[0 * TYPE int], eax
1118 mov L2Cache[1 * TYPE int], ebx
1119 mov L2Cache[2 * TYPE int], ecx
1120 mov L2Cache[3 * TYPE int], edx
1122 #ifdef CPUID_AWARE_COMPILER
1123 pop edx
1124 pop ecx
1125 pop ebx
1126 pop eax
1127 #endif
1130 __except(1)
1132 return false;
1134 // Save the L2 unified cache size (in KB) from ecx: bits 31..16.
1135 this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16);
1137 else
1139 // Store -1 to indicate the cache could not be queried.
1140 this->Features.L2CacheSize = -1;
1143 // Define L3 as being not present as we cannot test for it.
1144 this->Features.L3CacheSize = -1;
1146 #endif
1148 // Return failure if we cannot detect either cache with this method.
1149 return ((this->Features.L1CacheSize == -1) && (this->Features.L2CacheSize == -1)) ? false : true;
1152 /** */
1153 bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails()
1155 #if USE_ASM_INSTRUCTIONS
1156 int TLBCode = -1, TLBData = -1, L1Code = -1, L1Data = -1, L1Trace = -1, L2Unified = -1, L3Unified = -1;
1157 int TLBCacheData[4] = { 0, 0, 0, 0 };
1158 int TLBPassCounter = 0;
1159 int TLBCacheUnit = 0;
1162 do {
1163 // Use assembly to retrieve the L2 cache information ...
1164 __try {
1165 _asm {
1166 #ifdef CPUID_AWARE_COMPILER
1167 ; we must push/pop the registers <<CPUID>> writes to, as the
1168 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1169 ; these registers to change.
1170 push eax
1171 push ebx
1172 push ecx
1173 push edx
1174 #endif
1175 ; <<CPUID>>
1176 ; eax = 2 --> eax: TLB and cache information - Part 1 of 4.
1177 ; ebx: TLB and cache information - Part 2 of 4.
1178 ; ecx: TLB and cache information - Part 3 of 4.
1179 ; edx: TLB and cache information - Part 4 of 4.
1180 mov eax, 2
1181 CPUID_INSTRUCTION
1182 mov TLBCacheData[0 * TYPE int], eax
1183 mov TLBCacheData[1 * TYPE int], ebx
1184 mov TLBCacheData[2 * TYPE int], ecx
1185 mov TLBCacheData[3 * TYPE int], edx
1187 #ifdef CPUID_AWARE_COMPILER
1188 pop edx
1189 pop ecx
1190 pop ebx
1191 pop eax
1192 #endif
1195 __except(1)
1197 return false;
1200 int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16);
1201 (void)bob;
1202 // Process the returned TLB and cache information.
1203 for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++)
1205 // First of all - decide which unit we are dealing with.
1206 switch (nCounter)
1208 // eax: bits 8..15 : bits 16..23 : bits 24..31
1209 case 0: TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8); break;
1210 case 1: TLBCacheUnit = ((TLBCacheData[0] & 0x00FF0000) >> 16); break;
1211 case 2: TLBCacheUnit = ((TLBCacheData[0] & 0xFF000000) >> 24); break;
1213 // ebx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1214 case 3: TLBCacheUnit = ((TLBCacheData[1] & 0x000000FF) >> 0); break;
1215 case 4: TLBCacheUnit = ((TLBCacheData[1] & 0x0000FF00) >> 8); break;
1216 case 5: TLBCacheUnit = ((TLBCacheData[1] & 0x00FF0000) >> 16); break;
1217 case 6: TLBCacheUnit = ((TLBCacheData[1] & 0xFF000000) >> 24); break;
1219 // ecx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1220 case 7: TLBCacheUnit = ((TLBCacheData[2] & 0x000000FF) >> 0); break;
1221 case 8: TLBCacheUnit = ((TLBCacheData[2] & 0x0000FF00) >> 8); break;
1222 case 9: TLBCacheUnit = ((TLBCacheData[2] & 0x00FF0000) >> 16); break;
1223 case 10: TLBCacheUnit = ((TLBCacheData[2] & 0xFF000000) >> 24); break;
1225 // edx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1226 case 11: TLBCacheUnit = ((TLBCacheData[3] & 0x000000FF) >> 0); break;
1227 case 12: TLBCacheUnit = ((TLBCacheData[3] & 0x0000FF00) >> 8); break;
1228 case 13: TLBCacheUnit = ((TLBCacheData[3] & 0x00FF0000) >> 16); break;
1229 case 14: TLBCacheUnit = ((TLBCacheData[3] & 0xFF000000) >> 24); break;
1231 // Default case - an error has occured.
1232 default: return false;
1235 // Now process the resulting unit to see what it means....
1236 switch (TLBCacheUnit)
1238 case 0x00: break;
1239 case 0x01: STORE_TLBCACHE_INFO (TLBCode, 4); break;
1240 case 0x02: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1241 case 0x03: STORE_TLBCACHE_INFO (TLBData, 4); break;
1242 case 0x04: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1243 case 0x06: STORE_TLBCACHE_INFO (L1Code, 8); break;
1244 case 0x08: STORE_TLBCACHE_INFO (L1Code, 16); break;
1245 case 0x0a: STORE_TLBCACHE_INFO (L1Data, 8); break;
1246 case 0x0c: STORE_TLBCACHE_INFO (L1Data, 16); break;
1247 case 0x10: STORE_TLBCACHE_INFO (L1Data, 16); break; // <-- FIXME: IA-64 Only
1248 case 0x15: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
1249 case 0x1a: STORE_TLBCACHE_INFO (L2Unified, 96); break; // <-- FIXME: IA-64 Only
1250 case 0x22: STORE_TLBCACHE_INFO (L3Unified, 512); break;
1251 case 0x23: STORE_TLBCACHE_INFO (L3Unified, 1024); break;
1252 case 0x25: STORE_TLBCACHE_INFO (L3Unified, 2048); break;
1253 case 0x29: STORE_TLBCACHE_INFO (L3Unified, 4096); break;
1254 case 0x39: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1255 case 0x3c: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1256 case 0x40: STORE_TLBCACHE_INFO (L2Unified, 0); break; // <-- FIXME: No integrated L2 cache (P6 core) or L3 cache (P4 core).
1257 case 0x41: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1258 case 0x42: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1259 case 0x43: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1260 case 0x44: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1261 case 0x45: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
1262 case 0x50: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1263 case 0x51: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1264 case 0x52: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1265 case 0x5b: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1266 case 0x5c: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1267 case 0x5d: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1268 case 0x66: STORE_TLBCACHE_INFO (L1Data, 8); break;
1269 case 0x67: STORE_TLBCACHE_INFO (L1Data, 16); break;
1270 case 0x68: STORE_TLBCACHE_INFO (L1Data, 32); break;
1271 case 0x70: STORE_TLBCACHE_INFO (L1Trace, 12); break;
1272 case 0x71: STORE_TLBCACHE_INFO (L1Trace, 16); break;
1273 case 0x72: STORE_TLBCACHE_INFO (L1Trace, 32); break;
1274 case 0x77: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
1275 case 0x79: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1276 case 0x7a: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1277 case 0x7b: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1278 case 0x7c: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1279 case 0x7e: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1280 case 0x81: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1281 case 0x82: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1282 case 0x83: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1283 case 0x84: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1284 case 0x85: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
1285 case 0x88: STORE_TLBCACHE_INFO (L3Unified, 2048); break; // <-- FIXME: IA-64 Only
1286 case 0x89: STORE_TLBCACHE_INFO (L3Unified, 4096); break; // <-- FIXME: IA-64 Only
1287 case 0x8a: STORE_TLBCACHE_INFO (L3Unified, 8192); break; // <-- FIXME: IA-64 Only
1288 case 0x8d: STORE_TLBCACHE_INFO (L3Unified, 3096); break; // <-- FIXME: IA-64 Only
1289 case 0x90: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1290 case 0x96: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1291 case 0x9b: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1293 // Default case - an error has occured.
1294 default: return false;
1298 // Increment the TLB pass counter.
1299 TLBPassCounter ++;
1300 } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter);
1302 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1303 if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1))
1305 this->Features.L1CacheSize = -1;
1307 else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1))
1309 this->Features.L1CacheSize = L1Trace;
1311 else if ((L1Code != -1) && (L1Data == -1))
1313 this->Features.L1CacheSize = L1Code;
1315 else if ((L1Code == -1) && (L1Data != -1))
1317 this->Features.L1CacheSize = L1Data;
1319 else if ((L1Code != -1) && (L1Data != -1))
1321 this->Features.L1CacheSize = L1Code + L1Data;
1323 else
1325 this->Features.L1CacheSize = -1;
1328 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1329 if (L2Unified == -1)
1331 this->Features.L2CacheSize = -1;
1333 else
1335 this->Features.L2CacheSize = L2Unified;
1338 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1339 if (L3Unified == -1)
1341 this->Features.L3CacheSize = -1;
1343 else
1345 this->Features.L3CacheSize = L3Unified;
1348 #endif
1349 return true;
1352 /** */
1353 bool SystemInformationImplementation::RetrieveCPUClockSpeed()
1355 #if _WIN32
1356 // First of all we check to see if the RDTSC (0x0F, 0x31) instruction is supported.
1357 if (!this->Features.HasTSC)
1359 return false;
1362 unsigned int uiRepetitions = 1;
1363 unsigned int uiMSecPerRepetition = 50;
1364 __int64 i64Total = 0;
1365 __int64 i64Overhead = 0;
1367 for (unsigned int nCounter = 0; nCounter < uiRepetitions; nCounter ++)
1369 i64Total += GetCyclesDifference (SystemInformationImplementation::Delay,
1370 uiMSecPerRepetition);
1371 i64Overhead +=
1372 GetCyclesDifference (SystemInformationImplementation::DelayOverhead,
1373 uiMSecPerRepetition);
1376 // Calculate the MHz speed.
1377 i64Total -= i64Overhead;
1378 i64Total /= uiRepetitions;
1379 i64Total /= uiMSecPerRepetition;
1380 i64Total /= 1000;
1382 // Save the CPU speed.
1383 this->CPUSpeedInMHz = (float) i64Total;
1385 return true;
1386 #else
1387 return false;
1388 #endif
1391 /** */
1392 bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed()
1394 #if USE_ASM_INSTRUCTIONS
1395 LARGE_INTEGER liStart, liEnd, liCountsPerSecond;
1396 double dFrequency, dDifference;
1398 // Attempt to get a starting tick count.
1399 QueryPerformanceCounter (&liStart);
1401 __try
1403 _asm
1405 mov eax, 0x80000000
1406 mov ebx, CLASSICAL_CPU_FREQ_LOOP
1407 Timer_Loop:
1408 bsf ecx,eax
1409 dec ebx
1410 jnz Timer_Loop
1413 __except(1)
1415 return false;
1418 // Attempt to get a starting tick count.
1419 QueryPerformanceCounter (&liEnd);
1421 // Get the difference... NB: This is in seconds....
1422 QueryPerformanceFrequency (&liCountsPerSecond);
1423 dDifference = (((double) liEnd.QuadPart - (double) liStart.QuadPart) / (double) liCountsPerSecond.QuadPart);
1425 // Calculate the clock speed.
1426 if (this->ChipID.Family == 3)
1428 // 80386 processors.... Loop time is 115 cycles!
1429 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1048576);
1431 else if (this->ChipID.Family == 4)
1433 // 80486 processors.... Loop time is 47 cycles!
1434 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1048576);
1436 else if (this->ChipID.Family == 5)
1438 // Pentium processors.... Loop time is 43 cycles!
1439 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1048576);
1442 // Save the clock speed.
1443 this->Features.CPUSpeed = (int) dFrequency;
1444 #else
1445 return true;
1446 #endif
1449 /** */
1450 bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULevelToCheck)
1452 int MaxCPUExtendedLevel = 0;
1454 // The extended CPUID is supported by various vendors starting with the following CPU models:
1456 // Manufacturer & Chip Name | Family Model Revision
1458 // AMD K6, K6-2 | 5 6 x
1459 // Cyrix GXm, Cyrix III "Joshua" | 5 4 x
1460 // IDT C6-2 | 5 8 x
1461 // VIA Cyrix III | 6 5 x
1462 // Transmeta Crusoe | 5 x x
1463 // Intel Pentium 4 | f x x
1466 // We check to see if a supported processor is present...
1467 if (this->ChipManufacturer == AMD)
1469 if (this->ChipID.Family < 5) return false;
1470 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6)) return false;
1472 else if (this->ChipManufacturer == Cyrix)
1474 if (this->ChipID.Family < 5) return false;
1475 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4)) return false;
1476 if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5)) return false;
1478 else if (this->ChipManufacturer == IDT)
1480 if (this->ChipID.Family < 5) return false;
1481 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8)) return false;
1483 else if (this->ChipManufacturer == Transmeta)
1485 if (this->ChipID.Family < 5) return false;
1487 else if (this->ChipManufacturer == Intel)
1489 if (this->ChipID.Family < 0xf)
1491 return false;
1495 #if USE_ASM_INSTRUCTIONS
1497 // Use assembly to detect CPUID information...
1498 __try {
1499 _asm {
1500 #ifdef CPUID_AWARE_COMPILER
1501 ; we must push/pop the registers <<CPUID>> writes to, as the
1502 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1503 ; these registers to change.
1504 push eax
1505 push ebx
1506 push ecx
1507 push edx
1508 #endif
1509 ; <<CPUID>>
1510 ; eax = 0x80000000 --> eax: maximum supported extended level
1511 mov eax,0x80000000
1512 CPUID_INSTRUCTION
1513 mov MaxCPUExtendedLevel, eax
1515 #ifdef CPUID_AWARE_COMPILER
1516 pop edx
1517 pop ecx
1518 pop ebx
1519 pop eax
1520 #endif
1523 __except(1)
1525 return false;
1527 #endif
1529 // Now we have to check the level wanted vs level returned...
1530 int nLevelWanted = (CPULevelToCheck & 0x7FFFFFFF);
1531 int nLevelReturn = (MaxCPUExtendedLevel & 0x7FFFFFFF);
1533 // Check to see if the level provided is supported...
1534 if (nLevelWanted > nLevelReturn)
1536 return false;
1539 return true;
1542 /** */
1543 bool SystemInformationImplementation::RetrieveExtendedCPUFeatures()
1546 // Check that we are not using an Intel processor as it does not support this.
1547 if (this->ChipManufacturer == Intel)
1549 return false;
1552 // Check to see if what we are about to do is supported...
1553 if (!RetrieveCPUExtendedLevelSupport (0x80000001))
1555 return false;
1557 #if USE_ASM_INSTRUCTIONS
1558 int localCPUExtendedFeatures = 0;
1560 // Use assembly to detect CPUID information...
1561 __try
1563 _asm
1565 #ifdef CPUID_AWARE_COMPILER
1566 ; we must push/pop the registers <<CPUID>> writes to, as the
1567 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1568 ; these registers to change.
1569 push eax
1570 push ebx
1571 push ecx
1572 push edx
1573 #endif
1574 ; <<CPUID>>
1575 ; 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
1576 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
1577 ; edx: CPU feature flags
1578 mov eax,0x80000001
1579 CPUID_INSTRUCTION
1580 mov localCPUExtendedFeatures, edx
1582 #ifdef CPUID_AWARE_COMPILER
1583 pop edx
1584 pop ecx
1585 pop ebx
1586 pop eax
1587 #endif
1590 __except(1)
1592 return false;
1595 // Retrieve the extended features of CPU present.
1596 this->Features.ExtendedFeatures.Has3DNow = ((localCPUExtendedFeatures & 0x80000000) != 0); // 3DNow Present --> Bit 31.
1597 this->Features.ExtendedFeatures.Has3DNowPlus = ((localCPUExtendedFeatures & 0x40000000) != 0); // 3DNow+ Present -- > Bit 30.
1598 this->Features.ExtendedFeatures.HasSSEMMX = ((localCPUExtendedFeatures & 0x00400000) != 0); // SSE MMX Present --> Bit 22.
1599 this->Features.ExtendedFeatures.SupportsMP = ((localCPUExtendedFeatures & 0x00080000) != 0); // MP Capable -- > Bit 19.
1601 // Retrieve AMD specific extended features.
1602 if (this->ChipManufacturer == AMD)
1604 this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x00400000) != 0); // AMD specific: MMX-SSE --> Bit 22
1607 // Retrieve Cyrix specific extended features.
1608 if (this->ChipManufacturer == Cyrix)
1610 this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x01000000) != 0); // Cyrix specific: Extended MMX --> Bit 24
1612 #endif
1614 return true;
1617 /** */
1618 bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
1620 // Check to see if the processor supports the processor serial number.
1621 if (!this->Features.HasSerial)
1623 return false;
1626 #if USE_ASM_INSTRUCTIONS
1627 int SerialNumber[3];
1630 // Use assembly to detect CPUID information...
1631 __try {
1632 _asm {
1633 #ifdef CPUID_AWARE_COMPILER
1634 ; we must push/pop the registers <<CPUID>> writes to, as the
1635 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1636 ; these registers to change.
1637 push eax
1638 push ebx
1639 push ecx
1640 push edx
1641 #endif
1642 ; <<CPUID>>
1643 ; eax = 3 --> ebx: top 32 bits are the processor signature bits --> NB: Transmeta only ?!?
1644 ; ecx: middle 32 bits are the processor signature bits
1645 ; edx: bottom 32 bits are the processor signature bits
1646 mov eax, 3
1647 CPUID_INSTRUCTION
1648 mov SerialNumber[0 * TYPE int], ebx
1649 mov SerialNumber[1 * TYPE int], ecx
1650 mov SerialNumber[2 * TYPE int], edx
1652 #ifdef CPUID_AWARE_COMPILER
1653 pop edx
1654 pop ecx
1655 pop ebx
1656 pop eax
1657 #endif
1660 __except(1)
1662 return false;
1665 // Process the returned information.
1666 sprintf (this->ChipID.SerialNumber, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
1667 ((SerialNumber[0] & 0xff000000) >> 24),
1668 ((SerialNumber[0] & 0x00ff0000) >> 16),
1669 ((SerialNumber[0] & 0x0000ff00) >> 8),
1670 ((SerialNumber[0] & 0x000000ff) >> 0),
1671 ((SerialNumber[1] & 0xff000000) >> 24),
1672 ((SerialNumber[1] & 0x00ff0000) >> 16),
1673 ((SerialNumber[1] & 0x0000ff00) >> 8),
1674 ((SerialNumber[1] & 0x000000ff) >> 0),
1675 ((SerialNumber[2] & 0xff000000) >> 24),
1676 ((SerialNumber[2] & 0x00ff0000) >> 16),
1677 ((SerialNumber[2] & 0x0000ff00) >> 8),
1678 ((SerialNumber[2] & 0x000000ff) >> 0));
1679 #endif
1681 return true;
1684 /** */
1685 bool SystemInformationImplementation::RetrieveCPUPowerManagement()
1687 // Check to see if what we are about to do is supported...
1688 if (!RetrieveCPUExtendedLevelSupport (0x80000007))
1690 this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = false;
1691 this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = false;
1692 this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = false;
1693 return false;
1696 #if USE_ASM_INSTRUCTIONS
1697 int localCPUPowerManagement = 0;
1700 // Use assembly to detect CPUID information...
1701 __try {
1702 _asm {
1703 #ifdef CPUID_AWARE_COMPILER
1704 ; we must push/pop the registers <<CPUID>> writes to, as the
1705 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1706 ; these registers to change.
1707 push eax
1708 push ebx
1709 push ecx
1710 push edx
1711 #endif
1712 ; <<CPUID>>
1713 ; eax = 0x80000007 --> edx: get processor power management
1714 mov eax,0x80000007
1715 CPUID_INSTRUCTION
1716 mov localCPUPowerManagement, edx
1718 #ifdef CPUID_AWARE_COMPILER
1719 pop edx
1720 pop ecx
1721 pop ebx
1722 pop eax
1723 #endif
1726 __except(1)
1728 return false;
1731 // Check for the power management capabilities of the CPU.
1732 this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = ((localCPUPowerManagement & 0x00000001) != 0);
1733 this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = ((localCPUPowerManagement & 0x00000002) != 0);
1734 this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = ((localCPUPowerManagement & 0x00000004) != 0);
1736 #endif
1738 return true;
1741 /** */
1742 bool SystemInformationImplementation::RetrieveExtendedCPUIdentity()
1744 // Check to see if what we are about to do is supported...
1745 if (!RetrieveCPUExtendedLevelSupport(0x80000002)) return false;
1746 if (!RetrieveCPUExtendedLevelSupport(0x80000003)) return false;
1747 if (!RetrieveCPUExtendedLevelSupport(0x80000004)) return false;
1749 #if USE_ASM_INSTRUCTIONS
1750 int ProcessorNameStartPos = 0;
1751 int CPUExtendedIdentity[12];
1753 // Use assembly to detect CPUID information...
1754 __try {
1755 _asm {
1756 #ifdef CPUID_AWARE_COMPILER
1757 ; we must push/pop the registers <<CPUID>> writes to, as the
1758 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1759 ; these registers to change.
1760 push eax
1761 push ebx
1762 push ecx
1763 push edx
1764 #endif
1765 ; <<CPUID>>
1766 ; eax = 0x80000002 --> eax, ebx, ecx, edx: get processor name string (part 1)
1767 mov eax,0x80000002
1768 CPUID_INSTRUCTION
1769 mov CPUExtendedIdentity[0 * TYPE int], eax
1770 mov CPUExtendedIdentity[1 * TYPE int], ebx
1771 mov CPUExtendedIdentity[2 * TYPE int], ecx
1772 mov CPUExtendedIdentity[3 * TYPE int], edx
1774 ; <<CPUID>>
1775 ; eax = 0x80000003 --> eax, ebx, ecx, edx: get processor name string (part 2)
1776 mov eax,0x80000003
1777 CPUID_INSTRUCTION
1778 mov CPUExtendedIdentity[4 * TYPE int], eax
1779 mov CPUExtendedIdentity[5 * TYPE int], ebx
1780 mov CPUExtendedIdentity[6 * TYPE int], ecx
1781 mov CPUExtendedIdentity[7 * TYPE int], edx
1783 ; <<CPUID>>
1784 ; eax = 0x80000004 --> eax, ebx, ecx, edx: get processor name string (part 3)
1785 mov eax,0x80000004
1786 CPUID_INSTRUCTION
1787 mov CPUExtendedIdentity[8 * TYPE int], eax
1788 mov CPUExtendedIdentity[9 * TYPE int], ebx
1789 mov CPUExtendedIdentity[10 * TYPE int], ecx
1790 mov CPUExtendedIdentity[11 * TYPE int], edx
1792 #ifdef CPUID_AWARE_COMPILER
1793 pop edx
1794 pop ecx
1795 pop ebx
1796 pop eax
1797 #endif
1800 __except(1)
1802 return false;
1805 // Process the returned information.
1806 memcpy (this->ChipID.ProcessorName, &(CPUExtendedIdentity[0]), sizeof (int));
1807 memcpy (&(this->ChipID.ProcessorName[4]), &(CPUExtendedIdentity[1]), sizeof (int));
1808 memcpy (&(this->ChipID.ProcessorName[8]), &(CPUExtendedIdentity[2]), sizeof (int));
1809 memcpy (&(this->ChipID.ProcessorName[12]), &(CPUExtendedIdentity[3]), sizeof (int));
1810 memcpy (&(this->ChipID.ProcessorName[16]), &(CPUExtendedIdentity[4]), sizeof (int));
1811 memcpy (&(this->ChipID.ProcessorName[20]), &(CPUExtendedIdentity[5]), sizeof (int));
1812 memcpy (&(this->ChipID.ProcessorName[24]), &(CPUExtendedIdentity[6]), sizeof (int));
1813 memcpy (&(this->ChipID.ProcessorName[28]), &(CPUExtendedIdentity[7]), sizeof (int));
1814 memcpy (&(this->ChipID.ProcessorName[32]), &(CPUExtendedIdentity[8]), sizeof (int));
1815 memcpy (&(this->ChipID.ProcessorName[36]), &(CPUExtendedIdentity[9]), sizeof (int));
1816 memcpy (&(this->ChipID.ProcessorName[40]), &(CPUExtendedIdentity[10]), sizeof (int));
1817 memcpy (&(this->ChipID.ProcessorName[44]), &(CPUExtendedIdentity[11]), sizeof (int));
1818 this->ChipID.ProcessorName[48] = '\0';
1820 // Because some manufacturers have leading white space - we have to post-process the name.
1821 if (this->ChipManufacturer == Intel)
1823 for (int nCounter = 0; nCounter < CHIPNAME_STRING_LENGTH; nCounter ++)
1825 // There will either be NULL (\0) or spaces ( ) as the leading characters.
1826 if ((this->ChipID.ProcessorName[nCounter] != '\0') && (this->ChipID.ProcessorName[nCounter] != ' '))
1828 // We have found the starting position of the name.
1829 ProcessorNameStartPos = nCounter;
1830 // Terminate the loop.
1831 break;
1835 // Check to see if there is any white space at the start.
1836 if (ProcessorNameStartPos == 0)
1838 return true;
1841 // Now move the name forward so that there is no white space.
1842 memmove(this->ChipID.ProcessorName, &(this->ChipID.ProcessorName[ProcessorNameStartPos]), (CHIPNAME_STRING_LENGTH - ProcessorNameStartPos));
1844 #endif
1846 return true;
1849 /** */
1850 bool SystemInformationImplementation::RetrieveClassicalCPUIdentity()
1852 // Start by decided which manufacturer we are using....
1853 switch (this->ChipManufacturer)
1855 case Intel:
1856 // Check the family / model / revision to determine the CPU ID.
1857 switch (this->ChipID.Family) {
1858 case 3:
1859 sprintf (this->ChipID.ProcessorName, "Newer i80386 family");
1860 break;
1861 case 4:
1862 switch (this->ChipID.Model) {
1863 case 0: sprintf (this->ChipID.ProcessorName,"i80486DX-25/33"); break;
1864 case 1: sprintf (this->ChipID.ProcessorName,"i80486DX-50"); break;
1865 case 2: sprintf (this->ChipID.ProcessorName,"i80486SX"); break;
1866 case 3: sprintf (this->ChipID.ProcessorName,"i80486DX2"); break;
1867 case 4: sprintf (this->ChipID.ProcessorName,"i80486SL"); break;
1868 case 5: sprintf (this->ChipID.ProcessorName,"i80486SX2"); break;
1869 case 7: sprintf (this->ChipID.ProcessorName,"i80486DX2 WriteBack"); break;
1870 case 8: sprintf (this->ChipID.ProcessorName,"i80486DX4"); break;
1871 case 9: sprintf (this->ChipID.ProcessorName,"i80486DX4 WriteBack"); break;
1872 default: sprintf (this->ChipID.ProcessorName,"Unknown 80486 family"); return false;
1874 break;
1875 case 5:
1876 switch (this->ChipID.Model)
1878 case 0: sprintf (this->ChipID.ProcessorName,"P5 A-Step"); break;
1879 case 1: sprintf (this->ChipID.ProcessorName,"P5"); break;
1880 case 2: sprintf (this->ChipID.ProcessorName,"P54C"); break;
1881 case 3: sprintf (this->ChipID.ProcessorName,"P24T OverDrive"); break;
1882 case 4: sprintf (this->ChipID.ProcessorName,"P55C"); break;
1883 case 7: sprintf (this->ChipID.ProcessorName,"P54C"); break;
1884 case 8: sprintf (this->ChipID.ProcessorName,"P55C (0.25micron)"); break;
1885 default: sprintf (this->ChipID.ProcessorName,"Unknown Pentium family"); return false;
1887 break;
1888 case 6:
1889 switch (this->ChipID.Model)
1891 case 0: sprintf (this->ChipID.ProcessorName,"P6 A-Step"); break;
1892 case 1: sprintf (this->ChipID.ProcessorName,"P6"); break;
1893 case 3: sprintf (this->ChipID.ProcessorName,"Pentium II (0.28 micron)"); break;
1894 case 5: sprintf (this->ChipID.ProcessorName,"Pentium II (0.25 micron)"); break;
1895 case 6: sprintf (this->ChipID.ProcessorName,"Pentium II With On-Die L2 Cache"); break;
1896 case 7: sprintf (this->ChipID.ProcessorName,"Pentium III (0.25 micron)"); break;
1897 case 8: sprintf (this->ChipID.ProcessorName,"Pentium III (0.18 micron) With 256 KB On-Die L2 Cache "); break;
1898 case 0xa: sprintf (this->ChipID.ProcessorName,"Pentium III (0.18 micron) With 1 Or 2 MB On-Die L2 Cache "); break;
1899 case 0xb: sprintf (this->ChipID.ProcessorName,"Pentium III (0.13 micron) With 256 Or 512 KB On-Die L2 Cache "); break;
1900 default: sprintf (this->ChipID.ProcessorName,"Unknown P6 family"); return false;
1902 break;
1903 case 7:
1904 sprintf (this->ChipID.ProcessorName,"Intel Merced (IA-64)");
1905 break;
1906 case 0xf:
1907 // Check the extended family bits...
1908 switch (this->ChipID.ExtendedFamily)
1910 case 0:
1911 switch (this->ChipID.Model)
1913 case 0: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.18 micron)"); break;
1914 case 1: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.18 micron)"); break;
1915 case 2: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.13 micron)"); break;
1916 default: sprintf (this->ChipID.ProcessorName,"Unknown Pentium 4 family"); return false;
1918 break;
1919 case 1:
1920 sprintf (this->ChipID.ProcessorName,"Intel McKinley (IA-64)");
1921 break;
1922 default:
1923 sprintf (this->ChipID.ProcessorName,"Pentium");
1925 break;
1926 default:
1927 sprintf (this->ChipID.ProcessorName,"Unknown Intel family");
1928 return false;
1930 break;
1932 case AMD:
1933 // Check the family / model / revision to determine the CPU ID.
1934 switch (this->ChipID.Family)
1936 case 4:
1937 switch (this->ChipID.Model)
1939 case 3: sprintf (this->ChipID.ProcessorName,"80486DX2"); break;
1940 case 7: sprintf (this->ChipID.ProcessorName,"80486DX2 WriteBack"); break;
1941 case 8: sprintf (this->ChipID.ProcessorName,"80486DX4"); break;
1942 case 9: sprintf (this->ChipID.ProcessorName,"80486DX4 WriteBack"); break;
1943 case 0xe: sprintf (this->ChipID.ProcessorName,"5x86"); break;
1944 case 0xf: sprintf (this->ChipID.ProcessorName,"5x86WB"); break;
1945 default: sprintf (this->ChipID.ProcessorName,"Unknown 80486 family"); return false;
1947 break;
1948 case 5:
1949 switch (this->ChipID.Model)
1951 case 0: sprintf (this->ChipID.ProcessorName,"SSA5 (PR75, PR90, PR100)"); break;
1952 case 1: sprintf (this->ChipID.ProcessorName,"5k86 (PR120, PR133)"); break;
1953 case 2: sprintf (this->ChipID.ProcessorName,"5k86 (PR166)"); break;
1954 case 3: sprintf (this->ChipID.ProcessorName,"5k86 (PR200)"); break;
1955 case 6: sprintf (this->ChipID.ProcessorName,"K6 (0.30 micron)"); break;
1956 case 7: sprintf (this->ChipID.ProcessorName,"K6 (0.25 micron)"); break;
1957 case 8: sprintf (this->ChipID.ProcessorName,"K6-2"); break;
1958 case 9: sprintf (this->ChipID.ProcessorName,"K6-III"); break;
1959 case 0xd: sprintf (this->ChipID.ProcessorName,"K6-2+ or K6-III+ (0.18 micron)"); break;
1960 default: sprintf (this->ChipID.ProcessorName,"Unknown 80586 family"); return false;
1962 break;
1963 case 6:
1964 switch (this->ChipID.Model)
1966 case 1: sprintf (this->ChipID.ProcessorName,"Athlonâ„¢ (0.25 micron)"); break;
1967 case 2: sprintf (this->ChipID.ProcessorName,"Athlonâ„¢ (0.18 micron)"); break;
1968 case 3: sprintf (this->ChipID.ProcessorName,"Duronâ„¢ (SF core)"); break;
1969 case 4: sprintf (this->ChipID.ProcessorName,"Athlonâ„¢ (Thunderbird core)"); break;
1970 case 6: sprintf (this->ChipID.ProcessorName,"Athlonâ„¢ (Palomino core)"); break;
1971 case 7: sprintf (this->ChipID.ProcessorName,"Duronâ„¢ (Morgan core)"); break;
1972 case 8:
1973 if (this->Features.ExtendedFeatures.SupportsMP)
1974 sprintf (this->ChipID.ProcessorName,"Athlonâ„¢ MP (Thoroughbred core)");
1975 else sprintf (this->ChipID.ProcessorName,"Athlonâ„¢ XP (Thoroughbred core)");
1976 break;
1977 default: sprintf (this->ChipID.ProcessorName,"Unknown K7 family"); return false;
1979 break;
1980 default:
1981 sprintf (this->ChipID.ProcessorName,"Unknown AMD family");
1982 return false;
1984 break;
1986 case Transmeta:
1987 switch (this->ChipID.Family)
1989 case 5:
1990 switch (this->ChipID.Model)
1992 case 4: sprintf (this->ChipID.ProcessorName,"Crusoe TM3x00 and TM5x00"); break;
1993 default: sprintf (this->ChipID.ProcessorName,"Unknown Crusoe family"); return false;
1995 break;
1996 default:
1997 sprintf (this->ChipID.ProcessorName,"Unknown Transmeta family");
1998 return false;
2000 break;
2002 case Rise:
2003 switch (this->ChipID.Family)
2005 case 5:
2006 switch (this->ChipID.Model)
2008 case 0: sprintf (this->ChipID.ProcessorName,"mP6 (0.25 micron)"); break;
2009 case 2: sprintf (this->ChipID.ProcessorName,"mP6 (0.18 micron)"); break;
2010 default: sprintf (this->ChipID.ProcessorName,"Unknown Rise family"); return false;
2012 break;
2013 default:
2014 sprintf (this->ChipID.ProcessorName,"Unknown Rise family");
2015 return false;
2017 break;
2019 case UMC:
2020 switch (this->ChipID.Family)
2022 case 4:
2023 switch (this->ChipID.Model)
2025 case 1: sprintf (this->ChipID.ProcessorName,"U5D"); break;
2026 case 2: sprintf (this->ChipID.ProcessorName,"U5S"); break;
2027 default: sprintf (this->ChipID.ProcessorName,"Unknown UMC family"); return false;
2029 break;
2030 default:
2031 sprintf (this->ChipID.ProcessorName,"Unknown UMC family");
2032 return false;
2034 break;
2036 case IDT:
2037 switch (this->ChipID.Family)
2039 case 5:
2040 switch (this->ChipID.Model)
2042 case 4: sprintf (this->ChipID.ProcessorName,"C6"); break;
2043 case 8: sprintf (this->ChipID.ProcessorName,"C2"); break;
2044 case 9: sprintf (this->ChipID.ProcessorName,"C3"); break;
2045 default: sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family"); return false;
2047 break;
2048 case 6:
2049 switch (this->ChipID.Model)
2051 case 6: sprintf (this->ChipID.ProcessorName,"VIA Cyrix III - Samuel"); break;
2052 default: sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family"); return false;
2054 break;
2055 default:
2056 sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family");
2057 return false;
2059 break;
2061 case Cyrix:
2062 switch (this->ChipID.Family)
2064 case 4:
2065 switch (this->ChipID.Model)
2067 case 4: sprintf (this->ChipID.ProcessorName,"MediaGX GX, GXm"); break;
2068 case 9: sprintf (this->ChipID.ProcessorName,"5x86"); break;
2069 default: sprintf (this->ChipID.ProcessorName,"Unknown Cx5x86 family"); return false;
2071 break;
2072 case 5:
2073 switch (this->ChipID.Model)
2075 case 2: sprintf (this->ChipID.ProcessorName,"Cx6x86"); break;
2076 case 4: sprintf (this->ChipID.ProcessorName,"MediaGX GXm"); break;
2077 default: sprintf (this->ChipID.ProcessorName,"Unknown Cx6x86 family"); return false;
2079 break;
2080 case 6:
2081 switch (this->ChipID.Model)
2083 case 0: sprintf (this->ChipID.ProcessorName,"6x86MX"); break;
2084 case 5: sprintf (this->ChipID.ProcessorName,"Cyrix M2 Core"); break;
2085 case 6: sprintf (this->ChipID.ProcessorName,"WinChip C5A Core"); break;
2086 case 7: sprintf (this->ChipID.ProcessorName,"WinChip C5B\\C5C Core"); break;
2087 case 8: sprintf (this->ChipID.ProcessorName,"WinChip C5C-T Core"); break;
2088 default: sprintf (this->ChipID.ProcessorName,"Unknown 6x86MX\\Cyrix III family"); return false;
2090 break;
2091 default:
2092 sprintf (this->ChipID.ProcessorName,"Unknown Cyrix family");
2093 return false;
2095 break;
2097 case NexGen:
2098 switch (this->ChipID.Family)
2100 case 5:
2101 switch (this->ChipID.Model)
2103 case 0: sprintf (this->ChipID.ProcessorName,"Nx586 or Nx586FPU"); break;
2104 default: sprintf (this->ChipID.ProcessorName,"Unknown NexGen family"); return false;
2106 break;
2107 default:
2108 sprintf (this->ChipID.ProcessorName,"Unknown NexGen family");
2109 return false;
2111 break;
2113 case NSC:
2114 sprintf (this->ChipID.ProcessorName,"Cx486SLC \\ DLC \\ Cx486S A-Step");
2115 break;
2116 default:
2117 sprintf (this->ChipID.ProcessorName,"Unknown family"); // We cannot identify the processor.
2118 return false;
2121 return true;
2124 /** Extract a value from the CPUInfo file */
2125 kwsys_stl::string SystemInformationImplementation::ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,const char* word,size_t init)
2127 size_t pos = buffer.find(word,init);
2128 if(pos != buffer.npos)
2130 this->CurrentPositionInFile = pos;
2131 pos = buffer.find(":",pos);
2132 size_t pos2 = buffer.find("\n",pos);
2133 if(pos!=buffer.npos && pos2!=buffer.npos)
2135 return buffer.substr(pos+2,pos2-pos-2);
2138 this->CurrentPositionInFile = buffer.npos;
2139 return "";
2142 /** Query for the cpu status */
2143 int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
2145 this->NumberOfLogicalCPU = 0;
2146 this->NumberOfPhysicalCPU = 0;
2147 kwsys_stl::string buffer;
2149 FILE *fd = fopen("/proc/cpuinfo", "r" );
2150 if ( !fd )
2152 kwsys_ios::cout << "Problem opening /proc/cpuinfo" << kwsys_stl::endl;
2153 return 0;
2156 size_t fileSize = 0;
2157 while(!feof(fd))
2159 buffer += fgetc(fd);
2160 fileSize++;
2162 fclose( fd );
2164 buffer.resize(fileSize-2);
2166 // Number of CPUs
2167 size_t pos = buffer.find("processor\t");
2168 while(pos != buffer.npos)
2170 this->NumberOfLogicalCPU++;
2171 this->NumberOfPhysicalCPU++;
2172 pos = buffer.find("processor\t",pos+1);
2175 // Count the number of physical ids that are the same
2176 int currentId = -1;
2177 kwsys_stl::string idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id");
2179 while(this->CurrentPositionInFile != buffer.npos)
2181 int id = atoi(idc.c_str());
2182 if(id == currentId)
2184 this->NumberOfPhysicalCPU--;
2186 currentId = id;
2187 idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",this->CurrentPositionInFile+1);
2190 if(this->NumberOfPhysicalCPU>0)
2192 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2195 // CPU speed (checking only the first proc
2196 kwsys_stl::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer,"cpu MHz");
2197 this->CPUSpeedInMHz = (float)atof(CPUSpeed.c_str());
2199 // Chip family
2200 this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str());
2202 // Chip Vendor
2203 strcpy(this->ChipID.Vendor,this->ExtractValueFromCpuInfoFile(buffer,"vendor_id").c_str());
2204 this->FindManufacturer();
2206 // Chip Model
2207 this->ChipID.Model = atoi(this->ExtractValueFromCpuInfoFile(buffer,"model").c_str());
2208 this->RetrieveClassicalCPUIdentity();
2210 // L1 Cache size
2211 kwsys_stl::string cacheSize = this->ExtractValueFromCpuInfoFile(buffer,"cache size");
2212 pos = cacheSize.find(" KB");
2213 if(pos!=cacheSize.npos)
2215 cacheSize = cacheSize.substr(0,pos);
2217 this->Features.L1CacheSize = atoi(cacheSize.c_str());
2220 return 1;
2223 /** Query for the memory status */
2224 int SystemInformationImplementation::QueryMemory()
2226 this->TotalVirtualMemory = 0;
2227 this->TotalPhysicalMemory = 0;
2228 this->AvailableVirtualMemory = 0;
2229 this->AvailablePhysicalMemory = 0;
2230 #ifdef __CYGWIN__
2231 return 0;
2232 #elif _WIN32
2233 MEMORYSTATUS ms;
2234 GlobalMemoryStatus(&ms);
2236 unsigned long tv = ms.dwTotalVirtual;
2237 unsigned long tp = ms.dwTotalPhys;
2238 unsigned long av = ms.dwAvailVirtual;
2239 unsigned long ap = ms.dwAvailPhys;
2240 this->TotalVirtualMemory = tv>>10>>10;
2241 this->TotalPhysicalMemory = tp>>10>>10;
2242 this->AvailableVirtualMemory = av>>10>>10;
2243 this->AvailablePhysicalMemory = ap>>10>>10;
2244 return 1;
2245 #elif __linux
2246 unsigned long tv=0;
2247 unsigned long tp=0;
2248 unsigned long av=0;
2249 unsigned long ap=0;
2251 char buffer[1024]; // for skipping unused lines
2253 int linuxMajor = 0;
2254 int linuxMinor = 0;
2256 // Find the Linux kernel version first
2257 struct utsname unameInfo;
2258 int errorFlag = uname(&unameInfo);
2259 if( errorFlag!=0 )
2261 kwsys_ios::cout << "Problem calling uname(): " << strerror(errno) << kwsys_stl::endl;
2262 return 0;
2265 if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 )
2267 // release looks like "2.6.3-15mdk-i686-up-4GB"
2268 char majorChar=unameInfo.release[0];
2269 char minorChar=unameInfo.release[2];
2271 if( isdigit(majorChar) )
2273 linuxMajor=majorChar-'0';
2276 if( isdigit(minorChar) )
2278 linuxMinor=minorChar-'0';
2282 FILE *fd = fopen("/proc/meminfo", "r" );
2283 if ( !fd )
2285 kwsys_ios::cout << "Problem opening /proc/meminfo" << kwsys_stl::endl;
2286 return 0;
2289 if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) )
2291 // new /proc/meminfo format since kernel 2.6.x
2292 // Rigorously, this test should check from the developping version 2.5.x
2293 // that introduced the new format...
2295 long freeMem;
2296 long buffersMem;
2297 long cachedMem;
2299 fscanf(fd,"MemTotal:%ld kB\n", &this->TotalPhysicalMemory);
2300 fscanf(fd,"MemFree:%ld kB\n", &freeMem);
2301 fscanf(fd,"Buffers:%ld kB\n", &buffersMem);
2302 fscanf(fd,"Cached:%ld kB\n", &cachedMem);
2304 this->TotalPhysicalMemory /= 1024;
2305 this->AvailablePhysicalMemory = freeMem+cachedMem+buffersMem;
2306 this->AvailablePhysicalMemory /= 1024;
2308 // Skip SwapCached, Active, Inactive, HighTotal, HighFree, LowTotal
2309 // and LowFree.
2310 int i=0;
2311 while(i<7)
2313 fgets(buffer, sizeof(buffer), fd); // skip a line
2314 ++i;
2317 fscanf(fd,"SwapTotal:%ld kB\n", &this->TotalVirtualMemory);
2318 fscanf(fd,"SwapFree:%ld kB\n", &this->AvailableVirtualMemory);
2320 this->TotalVirtualMemory /= 1024;
2321 this->AvailableVirtualMemory /= 1024;
2323 else
2325 // /proc/meminfo format for kernel older than 2.6.x
2327 unsigned long temp;
2328 unsigned long cachedMem;
2329 unsigned long buffersMem;
2330 fgets(buffer, sizeof(buffer), fd); // Skip "total: used:..."
2332 fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n",
2333 &tp, &temp, &ap, &temp, &buffersMem, &cachedMem);
2334 fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
2336 this->TotalVirtualMemory = tv>>10>>10;
2337 this->TotalPhysicalMemory = tp>>10>>10;
2338 this->AvailableVirtualMemory = av>>10>>10;
2339 this->AvailablePhysicalMemory = (ap+buffersMem+cachedMem)>>10>>10;
2341 fclose( fd );
2342 return 1;
2343 #elif __hpux
2344 unsigned long tv=0;
2345 unsigned long tp=0;
2346 unsigned long av=0;
2347 unsigned long ap=0;
2348 struct pst_static pst;
2349 struct pst_dynamic pdy;
2351 unsigned long ps = 0;
2352 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1)
2354 ps = pst.page_size;
2355 tp = pst.physical_memory *ps;
2356 tv = (pst.physical_memory + pst.pst_maxmem) * ps;
2357 if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t) 1, 0) != -1)
2359 ap = tp - pdy.psd_rm * ps;
2360 av = tv - pdy.psd_vm;
2361 this->TotalVirtualMemory = tv>>10>>10;
2362 this->TotalPhysicalMemory = tp>>10>>10;
2363 this->AvailableVirtualMemory = av>>10>>10;
2364 this->AvailablePhysicalMemory = ap>>10>>10;
2365 return 1;
2368 return 0;
2369 #else
2370 return 0;
2371 #endif
2376 /** */
2377 unsigned long SystemInformationImplementation::GetTotalVirtualMemory()
2379 return this->TotalVirtualMemory;
2382 /** */
2383 unsigned long SystemInformationImplementation::GetAvailableVirtualMemory()
2385 return this->AvailableVirtualMemory;
2388 unsigned long SystemInformationImplementation::GetTotalPhysicalMemory()
2390 return this->TotalPhysicalMemory;
2393 /** */
2394 unsigned long SystemInformationImplementation::GetAvailablePhysicalMemory()
2396 return this->AvailablePhysicalMemory;
2399 /** Get Cycle differences */
2400 LongLong SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction,
2401 unsigned int uiParameter)
2403 #if USE_ASM_INSTRUCTIONS
2405 unsigned int edx1, eax1;
2406 unsigned int edx2, eax2;
2408 // Calculate the frequency of the CPU instructions.
2409 __try {
2410 _asm {
2411 push uiParameter ; push parameter param
2412 mov ebx, DelayFunction ; store func in ebx
2414 RDTSC_INSTRUCTION
2416 mov esi, eax ; esi = eax
2417 mov edi, edx ; edi = edx
2419 call ebx ; call the delay functions
2421 RDTSC_INSTRUCTION
2423 pop ebx
2425 mov edx2, edx ; edx2 = edx
2426 mov eax2, eax ; eax2 = eax
2428 mov edx1, edi ; edx2 = edi
2429 mov eax1, esi ; eax2 = esi
2432 __except(1)
2434 return -1;
2437 return ((((__int64) edx2 << 32) + eax2) - (((__int64) edx1 << 32) + eax1));
2439 #else
2440 (void)DelayFunction;
2441 (void)uiParameter;
2442 return -1;
2443 #endif
2446 /** Compute the delay overhead */
2447 void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
2449 #if _WIN32
2450 LARGE_INTEGER Frequency, StartCounter, EndCounter;
2451 __int64 x;
2453 // Get the frequency of the high performance counter.
2454 if(!QueryPerformanceFrequency (&Frequency))
2456 return;
2458 x = Frequency.QuadPart / 1000 * uiMS;
2460 // Get the starting position of the counter.
2461 QueryPerformanceCounter (&StartCounter);
2463 do {
2464 // Get the ending position of the counter.
2465 QueryPerformanceCounter (&EndCounter);
2466 } while (EndCounter.QuadPart - StartCounter.QuadPart == x);
2467 #endif
2468 (void)uiMS;
2471 /** Return the number of logical CPU per physical CPUs Works only for windows */
2472 unsigned char SystemInformationImplementation::LogicalCPUPerPhysicalCPU(void)
2474 unsigned int Regebx = 0;
2475 #if USE_ASM_INSTRUCTIONS
2476 if (!this->IsHyperThreadingSupported())
2478 return (unsigned char) 1; // HT not supported
2480 __asm
2482 mov eax, 1
2483 cpuid
2484 mov Regebx, ebx
2486 #endif
2487 return (unsigned char) ((Regebx & NUM_LOGICAL_BITS) >> 16);
2490 /** Works only for windows */
2491 unsigned int SystemInformationImplementation::IsHyperThreadingSupported()
2493 #if USE_ASM_INSTRUCTIONS
2494 unsigned int Regedx = 0,
2495 Regeax = 0,
2496 VendorId[3] = {0, 0, 0};
2497 __try // Verify cpuid instruction is supported
2499 __asm
2501 xor eax, eax // call cpuid with eax = 0
2502 cpuid // Get vendor id string
2503 mov VendorId, ebx
2504 mov VendorId + 4, edx
2505 mov VendorId + 8, ecx
2507 mov eax, 1 // call cpuid with eax = 1
2508 cpuid
2509 mov Regeax, eax // eax contains family processor type
2510 mov Regedx, edx // edx has info about the availability of hyper-Threading
2513 __except (EXCEPTION_EXECUTE_HANDLER)
2515 return(0); // cpuid is unavailable
2518 if (((Regeax & FAMILY_ID) == PENTIUM4_ID) || (Regeax & EXT_FAMILY_ID))
2520 if (VendorId[0] == 'uneG')
2522 if (VendorId[1] == 'Ieni')
2524 if (VendorId[2] == 'letn')
2526 return(Regedx & HT_BIT); // Genuine Intel with hyper-Threading technology
2531 #endif
2533 return 0; // Not genuine Intel processor
2536 /** Return the APIC Id. Works only for windows. */
2537 unsigned char SystemInformationImplementation::GetAPICId()
2539 unsigned int Regebx = 0;
2540 #if USE_ASM_INSTRUCTIONS
2541 if (!this->IsHyperThreadingSupported())
2543 return (unsigned char) -1; // HT not supported
2544 } // Logical processor = 1
2545 __asm
2547 mov eax, 1
2548 cpuid
2549 mov Regebx, ebx
2551 #endif
2552 return (unsigned char) ((Regebx & INITIAL_APIC_ID_BITS) >> 24);
2555 /** Count the number of CPUs. Works only on windows. */
2556 int SystemInformationImplementation::CPUCount()
2558 #if _WIN32
2559 unsigned char StatusFlag = 0;
2560 SYSTEM_INFO info;
2562 this->NumberOfPhysicalCPU = 0;
2563 this->NumberOfLogicalCPU = 0;
2564 info.dwNumberOfProcessors = 0;
2565 GetSystemInfo (&info);
2567 // Number of physical processors in a non-Intel system
2568 // or in a 32-bit Intel system with Hyper-Threading technology disabled
2569 this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors;
2571 if (this->IsHyperThreadingSupported())
2573 unsigned char HT_Enabled = 0;
2574 this->NumberOfLogicalCPU = this->LogicalCPUPerPhysicalCPU();
2575 if (this->NumberOfLogicalCPU >= 1) // >1 Doesn't mean HT is enabled in the BIOS
2577 HANDLE hCurrentProcessHandle;
2578 #ifndef _WIN64
2579 # define DWORD_PTR DWORD
2580 #endif
2581 DWORD_PTR dwProcessAffinity;
2582 DWORD_PTR dwSystemAffinity;
2583 DWORD dwAffinityMask;
2585 // Calculate the appropriate shifts and mask based on the
2586 // number of logical processors.
2587 unsigned int i = 1;
2588 unsigned char PHY_ID_MASK = 0xFF;
2589 unsigned char PHY_ID_SHIFT = 0;
2591 while (i < this->NumberOfLogicalCPU)
2593 i *= 2;
2594 PHY_ID_MASK <<= 1;
2595 PHY_ID_SHIFT++;
2598 hCurrentProcessHandle = GetCurrentProcess();
2599 GetProcessAffinityMask(hCurrentProcessHandle, &dwProcessAffinity,
2600 &dwSystemAffinity);
2602 // Check if available process affinity mask is equal to the
2603 // available system affinity mask
2604 if (dwProcessAffinity != dwSystemAffinity)
2606 StatusFlag = HT_CANNOT_DETECT;
2607 this->NumberOfPhysicalCPU = (unsigned char)-1;
2608 return StatusFlag;
2611 dwAffinityMask = 1;
2612 while (dwAffinityMask != 0 && dwAffinityMask <= dwProcessAffinity)
2614 // Check if this CPU is available
2615 if (dwAffinityMask & dwProcessAffinity)
2617 if (SetProcessAffinityMask(hCurrentProcessHandle,
2618 dwAffinityMask))
2620 unsigned char APIC_ID, LOG_ID;
2621 Sleep(0); // Give OS time to switch CPU
2623 APIC_ID = GetAPICId();
2624 LOG_ID = APIC_ID & ~PHY_ID_MASK;
2626 if (LOG_ID != 0)
2628 HT_Enabled = 1;
2632 dwAffinityMask = dwAffinityMask << 1;
2634 // Reset the processor affinity
2635 SetProcessAffinityMask(hCurrentProcessHandle, dwProcessAffinity);
2637 if (this->NumberOfLogicalCPU == 1) // Normal P4 : HT is disabled in hardware
2639 StatusFlag = HT_DISABLED;
2641 else
2643 if (HT_Enabled)
2645 // Total physical processors in a Hyper-Threading enabled system.
2646 this->NumberOfPhysicalCPU /= (this->NumberOfLogicalCPU);
2647 StatusFlag = HT_ENABLED;
2649 else
2651 StatusFlag = HT_SUPPORTED_NOT_ENABLED;
2656 else
2658 // Processors do not have Hyper-Threading technology
2659 StatusFlag = HT_NOT_CAPABLE;
2660 this->NumberOfLogicalCPU = 1;
2662 return StatusFlag;
2663 #else
2664 return 0;
2665 #endif
2668 /** Return the number of logical CPUs on the system */
2669 unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
2671 return this->NumberOfLogicalCPU;
2674 /** Return the number of physical CPUs on the system */
2675 unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
2677 return this->NumberOfPhysicalCPU;
2680 /** For Mac we Parse the sysctl -a output */
2681 bool SystemInformationImplementation::ParseSysCtl()
2683 // Extract the arguments from the command line
2684 kwsys_stl::vector<const char*> args;
2685 args.push_back("sysctl");
2686 args.push_back("-a");
2687 args.push_back(0);
2689 this->SysCtlBuffer = this->RunProcess(args);
2691 // Parse values for Mac
2692 this->TotalPhysicalMemory = atoi(this->ExtractValueFromSysCtl("hw.memsize:").c_str())/(1024*1024);
2693 this->TotalVirtualMemory = 0;
2694 this->AvailablePhysicalMemory = 0;
2695 this->AvailableVirtualMemory = 0;
2697 this->NumberOfPhysicalCPU = atoi(this->ExtractValueFromSysCtl("hw.physicalcpu:").c_str());
2698 this->NumberOfLogicalCPU = atoi(this->ExtractValueFromSysCtl("hw.logicalcpu:").c_str());
2700 if(this->NumberOfPhysicalCPU!=0)
2702 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2705 this->CPUSpeedInMHz = atoi(this->ExtractValueFromSysCtl("hw.cpufrequency:").c_str());
2706 this->CPUSpeedInMHz /= 1000000;
2708 // Chip family
2709 this->ChipID.Family = atoi(this->ExtractValueFromSysCtl("machdep.cpu.family:").c_str());
2711 // Chip Vendor
2712 strcpy(this->ChipID.Vendor,this->ExtractValueFromSysCtl("machdep.cpu.vendor:").c_str());
2713 this->FindManufacturer();
2715 // Chip Model
2716 this->ChipID.Model = atoi(this->ExtractValueFromSysCtl("machdep.cpu.model:").c_str());
2717 this->RetrieveClassicalCPUIdentity();
2719 // Cache size
2720 this->Features.L1CacheSize = atoi(this->ExtractValueFromSysCtl("hw.l1icachesize:").c_str());
2721 this->Features.L2CacheSize = atoi(this->ExtractValueFromSysCtl("hw.l2cachesize:").c_str());
2723 return true;
2726 /** Extract a value from sysctl command */
2727 kwsys_stl::string SystemInformationImplementation::ExtractValueFromSysCtl(const char* word)
2729 size_t pos = this->SysCtlBuffer.find(word);
2730 if(pos != this->SysCtlBuffer.npos)
2732 pos = this->SysCtlBuffer.find(": ",pos);
2733 size_t pos2 = this->SysCtlBuffer.find("\n",pos);
2734 if(pos!=this->SysCtlBuffer.npos && pos2!=this->SysCtlBuffer.npos)
2736 return this->SysCtlBuffer.substr(pos+2,pos2-pos-2);
2739 return "";
2742 /** Run a given process */
2743 kwsys_stl::string SystemInformationImplementation::RunProcess(kwsys_stl::vector<const char*> args)
2745 kwsys_stl::string buffer = "";
2747 // Run the application
2748 kwsysProcess* gp = kwsysProcess_New();
2749 kwsysProcess_SetCommand(gp, &*args.begin());
2750 kwsysProcess_SetOption(gp,kwsysProcess_Option_HideWindow,1);
2752 kwsysProcess_Execute(gp);
2754 char* data = NULL;
2755 int length;
2756 double timeout = 255;
2758 while(kwsysProcess_WaitForData(gp,&data,&length,&timeout)) // wait for 1s
2760 for(int i=0;i<length;i++)
2762 buffer += data[i];
2765 kwsysProcess_WaitForExit(gp, 0);
2767 int result = 0;
2768 switch(kwsysProcess_GetState(gp))
2770 case kwsysProcess_State_Exited:
2772 result = kwsysProcess_GetExitValue(gp);
2773 } break;
2774 case kwsysProcess_State_Error:
2776 kwsys_ios::cerr << "Error: Could not run " << args[0] << ":\n";
2777 kwsys_ios::cerr << kwsysProcess_GetErrorString(gp) << "\n";
2778 } break;
2779 case kwsysProcess_State_Exception:
2781 kwsys_ios::cerr << "Error: " << args[0]
2782 << " terminated with an exception: "
2783 << kwsysProcess_GetExceptionString(gp) << "\n";
2784 } break;
2785 case kwsysProcess_State_Starting:
2786 case kwsysProcess_State_Executing:
2787 case kwsysProcess_State_Expired:
2788 case kwsysProcess_State_Killed:
2790 // Should not get here.
2791 kwsys_ios::cerr << "Unexpected ending state after running " << args[0]
2792 << kwsys_stl::endl;
2793 } break;
2795 kwsysProcess_Delete(gp);
2796 if(result)
2798 kwsys_ios::cerr << "Error " << args[0] << " returned :" << result << "\n";
2800 return buffer;
2804 kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const char* arguments)
2806 kwsys_stl::vector<const char*> args;
2807 args.clear();
2808 args.push_back("kstat");
2809 args.push_back("-p");
2811 kwsys_stl::string command = arguments;
2812 size_t start = command.npos;
2813 size_t pos = command.find(' ',0);
2814 while(pos!=command.npos)
2816 bool inQuotes = false;
2817 // Check if we are between quotes
2818 size_t b0 = command.find('"',0);
2819 size_t b1 = command.find('"',b0+1);
2820 while(b0 != command.npos && b1 != command.npos && b1>b0)
2822 if(pos>b0 && pos<b1)
2824 inQuotes = true;
2825 break;
2827 b0 = command.find('"',b1+1);
2828 b1 = command.find('"',b0+1);
2831 if(!inQuotes)
2833 kwsys_stl::string arg = command.substr(start+1,pos-start-1);
2835 // Remove the quotes if any
2836 size_t quotes = arg.find('"');
2837 while(quotes != arg.npos)
2839 arg.erase(quotes,1);
2840 quotes = arg.find('"');
2842 args.push_back(arg.c_str());
2843 start = pos;
2845 pos = command.find(' ',pos+1);
2847 kwsys_stl::string lastArg = command.substr(start+1,command.size()-start-1);
2848 args.push_back(lastArg.c_str());
2850 args.push_back(0);
2852 kwsys_stl::string buffer = this->RunProcess(args);
2854 kwsys_stl::string value = "";
2855 for(size_t i=buffer.size()-1;i>0;i--)
2857 if(buffer[i] == ' ' || buffer[i] == '\t')
2859 break;
2861 if(buffer[i] != '\n' && buffer[i] != '\r')
2863 kwsys_stl::string val = value;
2864 value = buffer[i];
2865 value += val;
2868 return value;
2871 /** Querying for system information from Solaris */
2872 bool SystemInformationImplementation::QuerySolarisInfo()
2874 // Parse values
2875 this->NumberOfPhysicalCPU = atoi(this->ParseValueFromKStat("-n systethis->misc -s ncpus").c_str());
2876 this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
2878 if(this->NumberOfPhysicalCPU!=0)
2880 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2883 this->CPUSpeedInMHz = atoi(this->ParseValueFromKStat("-s clock_MHz").c_str());
2885 // Chip family
2886 this->ChipID.Family = 0;
2888 // Chip Vendor
2889 strcpy(this->ChipID.Vendor,"Sun");
2890 this->FindManufacturer();
2892 // Chip Model
2893 sprintf(this->ChipID.ProcessorName,"%s",this->ParseValueFromKStat("-s cpu_type").c_str());
2894 this->ChipID.Model = 0;
2896 // Cache size
2897 this->Features.L1CacheSize = 0;
2898 this->Features.L2CacheSize = 0;
2900 char* tail;
2901 unsigned long totalMemory =
2902 strtoul(this->ParseValueFromKStat("-s physmem").c_str(),&tail,0);
2903 this->TotalPhysicalMemory = totalMemory/1024;
2904 this->TotalPhysicalMemory *= 8192;
2905 this->TotalPhysicalMemory /= 1024;
2907 // Undefined values (for now at least)
2908 this->TotalVirtualMemory = 0;
2909 this->AvailablePhysicalMemory = 0;
2910 this->AvailableVirtualMemory = 0;
2912 return true;
2915 /** Query the operating system information */
2916 bool SystemInformationImplementation::QueryOSInformation()
2918 #if _WIN32
2920 this->OSName = "Windows";
2922 OSVERSIONINFOEX osvi;
2923 BOOL bIsWindows64Bit;
2924 BOOL bOsVersionInfoEx;
2925 char * operatingSystem = new char [256];
2927 // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
2928 ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX));
2929 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
2930 bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);
2931 if (!bOsVersionInfoEx)
2933 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
2934 if (!GetVersionEx ((OSVERSIONINFO *) &osvi))
2936 return false;
2940 switch (osvi.dwPlatformId)
2942 case VER_PLATFORM_WIN32_NT:
2943 // Test for the product.
2944 if (osvi.dwMajorVersion <= 4)
2946 this->OSRelease = "NT";
2948 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
2950 this->OSRelease = "2000";
2952 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
2954 this->OSRelease = "XP";
2956 #ifdef VER_NT_WORKSTATION
2957 // Test for product type.
2958 if (bOsVersionInfoEx)
2960 if (osvi.wProductType == VER_NT_WORKSTATION)
2962 // VER_SUITE_PERSONAL may not be defined
2963 #ifdef VER_SUITE_PERSONAL
2964 if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
2966 this->OSRelease += " Personal";
2968 else
2970 this->OSRelease += " Professional";
2972 #endif
2974 else if (osvi.wProductType == VER_NT_SERVER)
2976 // Check for .NET Server instead of Windows XP.
2977 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
2979 this->OSRelease = ".NET";
2982 // Continue with the type detection.
2983 if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
2985 this->OSRelease += " DataCenter Server";
2987 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
2989 this->OSRelease += " Advanced Server";
2991 else
2993 this->OSRelease += " Server";
2997 sprintf (operatingSystem, "%s(Build %d)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
2998 this->OSVersion = operatingSystem;
3000 else
3001 #endif // VER_NT_WORKSTATION
3003 HKEY hKey;
3004 char szProductType[80];
3005 DWORD dwBufLen;
3007 // Query the registry to retrieve information.
3008 RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey);
3009 RegQueryValueEx (hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
3010 RegCloseKey (hKey);
3012 if (lstrcmpi ("WINNT", szProductType) == 0)
3014 this->OSRelease += " Professional";
3016 if (lstrcmpi ("LANMANNT", szProductType) == 0)
3018 // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
3019 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3021 this->OSRelease += " Standard Server";
3023 else
3025 this->OSRelease += " Server";
3028 if (lstrcmpi ("SERVERNT", szProductType) == 0)
3030 // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
3031 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3033 this->OSRelease += " Enterprise Server";
3035 else
3037 this->OSRelease += " Advanced Server";
3042 // Display version, service pack (if any), and build number.
3043 if (osvi.dwMajorVersion <= 4)
3045 // NB: NT 4.0 and earlier.
3046 sprintf (operatingSystem, "version %d.%d %s (Build %d)",
3047 osvi.dwMajorVersion,
3048 osvi.dwMinorVersion,
3049 osvi.szCSDVersion,
3050 osvi.dwBuildNumber & 0xFFFF);
3051 this->OSVersion = operatingSystem;
3053 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3055 // Windows XP and .NET server.
3056 typedef BOOL (CALLBACK* LPFNPROC) (HANDLE, BOOL *);
3057 HINSTANCE hKernelDLL;
3058 LPFNPROC DLLProc;
3060 // Load the Kernel32 DLL.
3061 hKernelDLL = LoadLibrary ("kernel32");
3062 if (hKernelDLL != NULL) {
3063 // Only XP and .NET Server support IsWOW64Process so... Load dynamically!
3064 DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process");
3066 // If the function address is valid, call the function.
3067 if (DLLProc != NULL) (DLLProc) (GetCurrentProcess (), &bIsWindows64Bit);
3068 else bIsWindows64Bit = false;
3070 // Free the DLL module.
3071 FreeLibrary (hKernelDLL);
3074 else
3076 // Windows 2000 and everything else.
3077 sprintf (operatingSystem,"%s(Build %d)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
3078 this->OSVersion = operatingSystem;
3080 break;
3082 case VER_PLATFORM_WIN32_WINDOWS:
3083 // Test for the product.
3084 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
3086 this->OSRelease = "95";
3087 if(osvi.szCSDVersion[1] == 'C')
3089 this->OSRelease += "OSR 2.5";
3091 else if(osvi.szCSDVersion[1] == 'B')
3093 this->OSRelease += "OSR 2";
3097 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
3099 this->OSRelease = "98";
3100 if (osvi.szCSDVersion[1] == 'A' )
3102 this->OSRelease += "SE";
3106 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
3108 this->OSRelease = "Me";
3110 break;
3112 case VER_PLATFORM_WIN32s:
3113 this->OSRelease = "Win32s";
3114 break;
3116 default:
3117 this->OSRelease = "Unknown";
3118 break;
3120 delete [] operatingSystem;
3121 operatingSystem = 0;
3123 // Get the hostname
3124 WORD wVersionRequested;
3125 WSADATA wsaData;
3126 char name[255];
3127 wVersionRequested = MAKEWORD(2,0);
3129 if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
3131 gethostname(name,sizeof(name));
3132 WSACleanup( );
3134 this->Hostname = name;
3136 #else
3138 struct utsname unameInfo;
3139 int errorFlag = uname(&unameInfo);
3140 if(errorFlag == 0)
3142 this->OSName = unameInfo.sysname;
3143 this->Hostname = unameInfo.nodename;
3144 this->OSRelease = unameInfo.release;
3145 this->OSVersion = unameInfo.version;
3146 this->OSPlatform = unameInfo.machine;
3148 #endif
3150 return true;
3154 /** Return true if the machine is 64 bits */
3155 bool SystemInformationImplementation::Is64Bits()
3157 return (sizeof(void*) == 8);
3160 } // namespace @KWSYS_NAMESPACE@