/[CMake]/Source/kwsys/SystemInformation.cxx
ViewVC logotype

Contents of /Source/kwsys/SystemInformation.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.38 - (show annotations) (download)
Thu Feb 12 15:08:15 2009 UTC (6 years, 4 months ago) by hoffman
Branch: MAIN
CVS Tags: ITK-3-12-bp, VTK-5-4-2, VTK-5-4-0, VTK-5-4-1, VTK-5-4-2-rc1, VTK-5-4-2-rc2, VTK-5-4-2-rc3, VTK-5-4-0-rc1, VTK-5-4-bp, VTK-5-4-1-rc1
Branch point for: ITK-3-12, VTK-5-4
Changes since 1.37: +82 -2 lines
BUG: #8496 add support for system info on haiku
1 /*=========================================================================
2
3 Program: BatchMake
4 Module: $RCSfile: SystemInformation.cxx,v $
5 Language: C++
6 Date: $Date: 2009/02/12 15:08:15 $
7 Version: $Revision: 1.38 $
8 Copyright (c) 2005 Insight Consortium. All rights reserved.
9 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
10
11
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
15 =========================================================================*/
16 #ifdef _WIN32
17 # include <winsock.h> // WSADATA, include before sys/types.h
18 #endif
19
20 #include "kwsysPrivate.h"
21 #include KWSYS_HEADER(FundamentalType.h)
22 #include KWSYS_HEADER(stl/string)
23 #include KWSYS_HEADER(stl/vector)
24 #include KWSYS_HEADER(ios/iosfwd)
25 #include KWSYS_HEADER(SystemInformation.hxx)
26 #include KWSYS_HEADER(Process.h)
27 #include KWSYS_HEADER(ios/iostream)
28 #include KWSYS_HEADER(ios/sstream)
29 // Work-around CMake dependency scanning limitation. This must
30 // duplicate the above list of headers.
31 #if 0
32 # include "FundamentalType.h.in"
33 # include "SystemInformation.hxx.in"
34 # include "Process.h.in"
35 # include "Configure.hxx.in"
36 # include "kwsys_stl.hxx.in"
37 # include "kwsys_stl_vector.in"
38 # include "kwsys_stl_iosfwd.in"
39 # include "kwsys_ios_sstream.h.in"
40 # include "kwsys_ios_iostream.h.in"
41 #endif
42
43
44 #ifndef WIN32
45 # include <sys/utsname.h> // int uname(struct utsname *buf);
46 #endif
47
48 #ifdef _WIN32
49 # include <windows.h>
50 #endif
51
52 #ifdef __linux
53 # include <sys/procfs.h>
54 # include <sys/types.h>
55 # include <unistd.h>
56 # include <fcntl.h>
57 # include <ctype.h> // int isdigit(int c);
58 # include <errno.h> // extern int errno;
59 # include <sys/time.h>
60 #elif __hpux
61 # include <sys/param.h>
62 # include <sys/pstat.h>
63 #endif
64
65 #ifdef __HAIKU__
66 #include <OS.h>
67 #endif
68
69 #include <memory.h>
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <string.h>
73
74
75
76 namespace KWSYS_NAMESPACE
77 {
78
79 // Create longlong
80 #if KWSYS_USE_LONG_LONG
81 typedef long long LongLong;
82 #elif KWSYS_USE___INT64
83 typedef __int64 LongLong;
84 #else
85 # error "No Long Long"
86 #endif
87
88 // Define SystemInformationImplementation class
89 typedef void (*DELAY_FUNC)(unsigned int uiMS);
90
91 class SystemInformationImplementation
92 {
93
94 public:
95 SystemInformationImplementation ();
96 ~SystemInformationImplementation ();
97
98 const char * GetVendorString();
99 const char * GetVendorID();
100 kwsys_stl::string GetTypeID();
101 kwsys_stl::string GetFamilyID();
102 kwsys_stl::string GetModelID();
103 kwsys_stl::string GetSteppingCode();
104 const char * GetExtendedProcessorName();
105 const char * GetProcessorSerialNumber();
106 int GetProcessorCacheSize();
107 int GetLogicalProcessorsPerPhysical();
108 float GetProcessorClockFrequency();
109 int GetProcessorAPICID();
110 int GetProcessorCacheXSize(long int);
111 bool DoesCPUSupportFeature(long int);
112
113 const char * GetOSName();
114 const char * GetHostname();
115 const char * GetOSRelease();
116 const char * GetOSVersion();
117 const char * GetOSPlatform();
118
119 bool Is64Bits();
120
121 unsigned int GetNumberOfLogicalCPU(); // per physical cpu
122 unsigned int GetNumberOfPhysicalCPU();
123
124 bool DoesCPUSupportCPUID();
125
126 // Retrieve memory information in megabyte.
127 unsigned long GetTotalVirtualMemory();
128 unsigned long GetAvailableVirtualMemory();
129 unsigned long GetTotalPhysicalMemory();
130 unsigned long GetAvailablePhysicalMemory();
131
132 /** Run the different checks */
133 void RunCPUCheck();
134 void RunOSCheck();
135 void RunMemoryCheck();
136 public:
137 #define VENDOR_STRING_LENGTH (12 + 1)
138 #define CHIPNAME_STRING_LENGTH (48 + 1)
139 #define SERIALNUMBER_STRING_LENGTH (29 + 1)
140
141 typedef struct tagID
142 {
143 int Type;
144 int Family;
145 int Model;
146 int Revision;
147 int ExtendedFamily;
148 int ExtendedModel;
149 char ProcessorName[CHIPNAME_STRING_LENGTH];
150 char Vendor[VENDOR_STRING_LENGTH];
151 char SerialNumber[SERIALNUMBER_STRING_LENGTH];
152 } ID;
153
154 typedef struct tagCPUPowerManagement
155 {
156 bool HasVoltageID;
157 bool HasFrequencyID;
158 bool HasTempSenseDiode;
159 } CPUPowerManagement;
160
161 typedef struct tagCPUExtendedFeatures
162 {
163 bool Has3DNow;
164 bool Has3DNowPlus;
165 bool SupportsMP;
166 bool HasMMXPlus;
167 bool HasSSEMMX;
168 bool SupportsHyperthreading;
169 int LogicalProcessorsPerPhysical;
170 int APIC_ID;
171 CPUPowerManagement PowerManagement;
172 } CPUExtendedFeatures;
173
174 typedef struct CPUtagFeatures
175 {
176 bool HasFPU;
177 bool HasTSC;
178 bool HasMMX;
179 bool HasSSE;
180 bool HasSSEFP;
181 bool HasSSE2;
182 bool HasIA64;
183 bool HasAPIC;
184 bool HasCMOV;
185 bool HasMTRR;
186 bool HasACPI;
187 bool HasSerial;
188 bool HasThermal;
189 int CPUSpeed;
190 int L1CacheSize;
191 int L2CacheSize;
192 int L3CacheSize;
193 CPUExtendedFeatures ExtendedFeatures;
194 } CPUFeatures;
195
196 enum Manufacturer
197 {
198 AMD, Intel, NSC, UMC, Cyrix, NexGen, IDT, Rise, Transmeta, Sun, UnknownManufacturer
199 };
200 protected:
201
202 // Functions.
203 bool RetrieveCPUFeatures();
204 bool RetrieveCPUIdentity();
205 bool RetrieveCPUCacheDetails();
206 bool RetrieveClassicalCPUCacheDetails();
207 bool RetrieveCPUClockSpeed();
208 bool RetrieveClassicalCPUClockSpeed();
209 bool RetrieveCPUExtendedLevelSupport(int);
210 bool RetrieveExtendedCPUFeatures();
211 bool RetrieveProcessorSerialNumber();
212 bool RetrieveCPUPowerManagement();
213 bool RetrieveClassicalCPUIdentity();
214 bool RetrieveExtendedCPUIdentity();
215
216 Manufacturer ChipManufacturer;
217 CPUFeatures Features;
218 ID ChipID;
219 float CPUSpeedInMHz;
220 unsigned int NumberOfLogicalCPU;
221 unsigned int NumberOfPhysicalCPU;
222
223 int CPUCount();
224 unsigned char LogicalCPUPerPhysicalCPU();
225 unsigned char GetAPICId();
226 unsigned int IsHyperThreadingSupported();
227 LongLong GetCyclesDifference(DELAY_FUNC, unsigned int);
228
229 // For Linux and Cygwin, /proc/cpuinfo formats are slightly different
230 int RetreiveInformationFromCpuInfoFile();
231 kwsys_stl::string ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,
232 const char* word, size_t init=0);
233
234 static void Delay (unsigned int);
235 static void DelayOverhead (unsigned int);
236
237 void FindManufacturer();
238
239 // For Mac
240 bool ParseSysCtl();
241 kwsys_stl::string ExtractValueFromSysCtl(const char* word);
242 kwsys_stl::string SysCtlBuffer;
243
244 // For Solaris
245 bool QuerySolarisInfo();
246 kwsys_stl::string ParseValueFromKStat(const char* arguments);
247 kwsys_stl::string RunProcess(kwsys_stl::vector<const char*> args);
248
249 //For Haiku OS
250 bool QueryHaikuInfo();
251
252 // Evaluate the memory information.
253 int QueryMemory();
254 unsigned long TotalVirtualMemory;
255 unsigned long AvailableVirtualMemory;
256 unsigned long TotalPhysicalMemory;
257 unsigned long AvailablePhysicalMemory;
258
259 size_t CurrentPositionInFile;
260
261 // Operating System information
262 bool QueryOSInformation();
263 kwsys_stl::string OSName;
264 kwsys_stl::string Hostname;
265 kwsys_stl::string OSRelease;
266 kwsys_stl::string OSVersion;
267 kwsys_stl::string OSPlatform;
268 };
269
270
271
272
273
274 SystemInformation::SystemInformation()
275 {
276 this->Implementation = new SystemInformationImplementation;
277 }
278
279 SystemInformation::~SystemInformation ()
280 {
281 delete this->Implementation;
282 }
283
284 const char * SystemInformation::GetVendorString()
285 {
286 return this->Implementation->GetVendorString();
287 }
288 const char * SystemInformation::GetVendorID()
289 {
290 return this->Implementation->GetVendorID();
291 }
292 kwsys_stl::string SystemInformation::GetTypeID()
293 {
294 return this->Implementation->GetTypeID();
295 }
296 kwsys_stl::string SystemInformation::GetFamilyID()
297 {
298 return this->Implementation->GetFamilyID();
299 }
300 kwsys_stl::string SystemInformation::GetModelID()
301 {
302 return this->Implementation->GetModelID();
303 }
304 kwsys_stl::string SystemInformation::GetSteppingCode()
305 {
306 return this->Implementation->GetSteppingCode();
307 }
308 const char * SystemInformation::GetExtendedProcessorName()
309 {
310 return this->Implementation->GetExtendedProcessorName();
311 }
312 const char * SystemInformation::GetProcessorSerialNumber()
313 {
314 return this->Implementation->GetProcessorSerialNumber();
315 }
316 int SystemInformation::GetProcessorCacheSize()
317 {
318 return this->Implementation->GetProcessorCacheSize();
319 }
320 int SystemInformation::GetLogicalProcessorsPerPhysical()
321 {
322 return this->Implementation->GetLogicalProcessorsPerPhysical();
323 }
324 float SystemInformation::GetProcessorClockFrequency()
325 {
326 return this->Implementation->GetProcessorClockFrequency();
327 }
328 int SystemInformation::GetProcessorAPICID()
329 {
330 return this->Implementation->GetProcessorAPICID();
331 }
332 int SystemInformation::GetProcessorCacheXSize(long int l)
333 {
334 return this->Implementation->GetProcessorCacheXSize(l);
335 }
336 bool SystemInformation::DoesCPUSupportFeature(long int i)
337 {
338 return this->Implementation->DoesCPUSupportFeature(i);
339 }
340
341 const char * SystemInformation::GetOSName()
342 {
343 return this->Implementation->GetOSName();
344 }
345 const char * SystemInformation::GetHostname()
346 {
347 return this->Implementation->GetHostname();
348 }
349 const char * SystemInformation::GetOSRelease()
350 {
351 return this->Implementation->GetOSRelease();
352 }
353 const char * SystemInformation::GetOSVersion()
354 {
355 return this->Implementation->GetOSVersion();
356 }
357 const char * SystemInformation::GetOSPlatform()
358 {
359 return this->Implementation->GetOSPlatform();
360 }
361
362 bool SystemInformation::Is64Bits()
363 {
364 return this->Implementation->Is64Bits();
365 }
366
367 unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
368 {
369 return this->Implementation->GetNumberOfLogicalCPU();
370 }
371 unsigned int SystemInformation::GetNumberOfPhysicalCPU()
372 {
373 return this->Implementation->GetNumberOfPhysicalCPU();
374 }
375
376 bool SystemInformation::DoesCPUSupportCPUID()
377 {
378 return this->Implementation->DoesCPUSupportCPUID();
379 }
380
381 // Retrieve memory information in megabyte.
382 unsigned long SystemInformation::GetTotalVirtualMemory()
383 {
384 return this->Implementation->GetTotalVirtualMemory();
385 }
386 unsigned long SystemInformation::GetAvailableVirtualMemory()
387 {
388 return this->Implementation->GetAvailableVirtualMemory();
389 }
390 unsigned long SystemInformation::GetTotalPhysicalMemory()
391 {
392 return this->Implementation->GetTotalPhysicalMemory();
393 }
394
395 unsigned long SystemInformation::GetAvailablePhysicalMemory()
396 {
397 return this->Implementation->GetAvailablePhysicalMemory();
398 }
399
400 /** Run the different checks */
401 void SystemInformation::RunCPUCheck()
402 {
403 this->Implementation->RunCPUCheck();
404 }
405 void SystemInformation::RunOSCheck()
406 {
407 this->Implementation->RunOSCheck();
408 }
409 void SystemInformation::RunMemoryCheck()
410 {
411 this->Implementation->RunMemoryCheck();
412 }
413
414
415
416 // --------------------------------------------------------------
417 // SystemInformationImplementation starts here
418
419 #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64)
420 #define USE_ASM_INSTRUCTIONS 1
421 #else
422 #define USE_ASM_INSTRUCTIONS 0
423 #endif
424
425 #define STORE_TLBCACHE_INFO(x,y) x = (x < y) ? y : x
426 #define TLBCACHE_INFO_UNITS (15)
427 #define CLASSICAL_CPU_FREQ_LOOP 10000000
428 #define RDTSC_INSTRUCTION _asm _emit 0x0f _asm _emit 0x31
429
430 #define CPUID_AWARE_COMPILER
431 #ifdef CPUID_AWARE_COMPILER
432 #define CPUID_INSTRUCTION cpuid
433 #else
434 #define CPUID_INSTRUCTION _asm _emit 0x0f _asm _emit 0xa2
435 #endif
436
437 #define MMX_FEATURE 0x00000001
438 #define MMX_PLUS_FEATURE 0x00000002
439 #define SSE_FEATURE 0x00000004
440 #define SSE2_FEATURE 0x00000008
441 #define AMD_3DNOW_FEATURE 0x00000010
442 #define AMD_3DNOW_PLUS_FEATURE 0x00000020
443 #define IA64_FEATURE 0x00000040
444 #define MP_CAPABLE 0x00000080
445 #define HYPERTHREAD_FEATURE 0x00000100
446 #define SERIALNUMBER_FEATURE 0x00000200
447 #define APIC_FEATURE 0x00000400
448 #define SSE_FP_FEATURE 0x00000800
449 #define SSE_MMX_FEATURE 0x00001000
450 #define CMOV_FEATURE 0x00002000
451 #define MTRR_FEATURE 0x00004000
452 #define L1CACHE_FEATURE 0x00008000
453 #define L2CACHE_FEATURE 0x00010000
454 #define L3CACHE_FEATURE 0x00020000
455 #define ACPI_FEATURE 0x00040000
456 #define THERMALMONITOR_FEATURE 0x00080000
457 #define TEMPSENSEDIODE_FEATURE 0x00100000
458 #define FREQUENCYID_FEATURE 0x00200000
459 #define VOLTAGEID_FREQUENCY 0x00400000
460
461 // Status Flag
462 #define HT_NOT_CAPABLE 0
463 #define HT_ENABLED 1
464 #define HT_DISABLED 2
465 #define HT_SUPPORTED_NOT_ENABLED 3
466 #define HT_CANNOT_DETECT 4
467
468 // EDX[28] Bit 28 is set if HT is supported
469 #define HT_BIT 0x10000000
470
471 // EAX[11:8] Bit 8-11 contains family processor ID.
472 #define FAMILY_ID 0x0F00
473 #define PENTIUM4_ID 0x0F00
474 // EAX[23:20] Bit 20-23 contains extended family processor ID
475 #define EXT_FAMILY_ID 0x0F00000
476 // EBX[23:16] Bit 16-23 in ebx contains the number of logical
477 #define NUM_LOGICAL_BITS 0x00FF0000
478 // processors per physical processor when execute cpuid with
479 // eax set to 1
480 // EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
481 #define INITIAL_APIC_ID_BITS 0xFF000000
482 // initial APIC ID for the processor this code is running on.
483 // Default value = 0xff if HT is not supported
484
485
486
487 SystemInformationImplementation::SystemInformationImplementation()
488 {
489 this->TotalVirtualMemory = 0;
490 this->AvailableVirtualMemory = 0;
491 this->TotalPhysicalMemory = 0;
492 this->AvailablePhysicalMemory = 0;
493 this->CurrentPositionInFile = 0;
494 this->ChipManufacturer = UnknownManufacturer;
495 memset(&this->Features, 0, sizeof(CPUFeatures));
496 memset(&this->ChipID, 0, sizeof(ID));
497 this->CPUSpeedInMHz = 0;
498 this->NumberOfLogicalCPU = 0;
499 this->NumberOfPhysicalCPU = 0;
500 this->OSName = "";
501 this->Hostname = "";
502 this->OSRelease = "";
503 this->OSVersion = "";
504 this->OSPlatform = "";
505 }
506
507 SystemInformationImplementation::~SystemInformationImplementation()
508 {
509 }
510
511 void SystemInformationImplementation::RunCPUCheck()
512 {
513 #ifdef WIN32
514 // Check to see if this processor supports CPUID.
515 if (DoesCPUSupportCPUID())
516 {
517 // Retrieve the CPU details.
518 RetrieveCPUIdentity();
519 RetrieveCPUFeatures();
520 if (!RetrieveCPUClockSpeed())
521 {
522 RetrieveClassicalCPUClockSpeed();
523 }
524
525 // Attempt to retrieve cache information.
526 if (!RetrieveCPUCacheDetails())
527 {
528 RetrieveClassicalCPUCacheDetails();
529 }
530 // Retrieve the extended CPU details.
531 if (!RetrieveExtendedCPUIdentity())
532 {
533 RetrieveClassicalCPUIdentity();
534 }
535 RetrieveExtendedCPUFeatures();
536
537 // Now attempt to retrieve the serial number (if possible).
538 RetrieveProcessorSerialNumber();
539 }
540 this->CPUCount();
541 #elif defined(__APPLE__)
542 this->ParseSysCtl();
543 #elif defined (__SVR4) && defined (__sun)
544 this->QuerySolarisInfo();
545 #elif defined(__HAIKU__)
546 this->QueryHaikuInfo();
547 #else
548 this->RetreiveInformationFromCpuInfoFile();
549 #endif
550 }
551
552 void SystemInformationImplementation::RunOSCheck()
553 {
554 this->QueryOSInformation();
555 }
556
557 void SystemInformationImplementation::RunMemoryCheck()
558 {
559 #if defined(__APPLE__)
560 this->ParseSysCtl();
561 #elif defined (__SVR4) && defined (__sun)
562 this->QuerySolarisInfo();
563 #elif defined(__HAIKU__)
564 this->QueryHaikuInfo();
565 #else
566 this->QueryMemory();
567 #endif
568 }
569
570 /** Get the vendor string */
571 const char * SystemInformationImplementation::GetVendorString()
572 {
573 return this->ChipID.Vendor;
574 }
575
576 /** Get the OS Name */
577 const char * SystemInformationImplementation::GetOSName()
578 {
579 return this->OSName.c_str();
580 }
581
582 /** Get the hostname */
583 const char* SystemInformationImplementation::GetHostname()
584 {
585 return this->Hostname.c_str();
586 }
587
588 /** Get the OS release */
589 const char* SystemInformationImplementation::GetOSRelease()
590 {
591 return this->OSRelease.c_str();
592 }
593
594 /** Get the OS version */
595 const char* SystemInformationImplementation::GetOSVersion()
596 {
597 return this->OSVersion.c_str();
598 }
599
600 /** Get the OS platform */
601 const char* SystemInformationImplementation::GetOSPlatform()
602 {
603 return this->OSPlatform.c_str();
604 }
605
606 /** Get the vendor ID */
607 const char * SystemInformationImplementation::GetVendorID()
608 {
609 // Return the vendor ID.
610 switch (this->ChipManufacturer)
611 {
612 case Intel:
613 return "Intel Corporation";
614 case AMD:
615 return "Advanced Micro Devices";
616 case NSC:
617 return "National Semiconductor";
618 case Cyrix:
619 return "Cyrix Corp., VIA Inc.";
620 case NexGen:
621 return "NexGen Inc., Advanced Micro Devices";
622 case IDT:
623 return "IDT\\Centaur, Via Inc.";
624 case UMC:
625 return "United Microelectronics Corp.";
626 case Rise:
627 return "Rise";
628 case Transmeta:
629 return "Transmeta";
630 case Sun:
631 return "Sun Microelectronics";
632 default:
633 return "Unknown Manufacturer";
634 }
635 }
636
637 /** Return the type ID of the CPU */
638 kwsys_stl::string SystemInformationImplementation::GetTypeID()
639 {
640 kwsys_ios::ostringstream str;
641 str << this->ChipID.Type;
642 return str.str();
643 }
644
645 /** Return the family of the CPU present */
646 kwsys_stl::string SystemInformationImplementation::GetFamilyID()
647 {
648 kwsys_ios::ostringstream str;
649 str << this->ChipID.Family;
650 return str.str();
651 }
652
653 // Return the model of CPU present */
654 kwsys_stl::string SystemInformationImplementation::GetModelID()
655 {
656 kwsys_ios::ostringstream str;
657 str << this->ChipID.Model;
658 return str.str();
659 }
660
661 /** Return the stepping code of the CPU present. */
662 kwsys_stl::string SystemInformationImplementation::GetSteppingCode()
663 {
664 kwsys_ios::ostringstream str;
665 str << this->ChipID.Revision;
666 return str.str();
667 }
668
669 /** Return the stepping code of the CPU present. */
670 const char * SystemInformationImplementation::GetExtendedProcessorName()
671 {
672 return this->ChipID.ProcessorName;
673 }
674
675 /** Return the serial number of the processor
676 * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
677 const char * SystemInformationImplementation::GetProcessorSerialNumber()
678 {
679 return this->ChipID.SerialNumber;
680 }
681
682 /** Return the logical processors per physical */
683 int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
684 {
685 return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
686 }
687
688 /** Return the processor clock frequency. */
689 float SystemInformationImplementation::GetProcessorClockFrequency()
690 {
691 return this->CPUSpeedInMHz;
692 }
693
694 /** Return the APIC ID. */
695 int SystemInformationImplementation::GetProcessorAPICID()
696 {
697 return this->Features.ExtendedFeatures.APIC_ID;
698 }
699
700 /** Return the L1 cache size. */
701 int SystemInformationImplementation::GetProcessorCacheSize()
702 {
703 return this->Features.L1CacheSize;
704 }
705
706 /** Return the chosen cache size. */
707 int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
708 {
709 switch (dwCacheID)
710 {
711 case L1CACHE_FEATURE:
712 return this->Features.L1CacheSize;
713 case L2CACHE_FEATURE:
714 return this->Features.L2CacheSize;
715 case L3CACHE_FEATURE:
716 return this->Features.L3CacheSize;
717 }
718 return -1;
719 }
720
721 bool SystemInformationImplementation::DoesCPUSupportFeature(long int dwFeature)
722 {
723 bool bHasFeature = false;
724
725 // Check for MMX instructions.
726 if (((dwFeature & MMX_FEATURE) != 0) && this->Features.HasMMX) bHasFeature = true;
727
728 // Check for MMX+ instructions.
729 if (((dwFeature & MMX_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.HasMMXPlus) bHasFeature = true;
730
731 // Check for SSE FP instructions.
732 if (((dwFeature & SSE_FEATURE) != 0) && this->Features.HasSSE) bHasFeature = true;
733
734 // Check for SSE FP instructions.
735 if (((dwFeature & SSE_FP_FEATURE) != 0) && this->Features.HasSSEFP) bHasFeature = true;
736
737 // Check for SSE MMX instructions.
738 if (((dwFeature & SSE_MMX_FEATURE) != 0) && this->Features.ExtendedFeatures.HasSSEMMX) bHasFeature = true;
739
740 // Check for SSE2 instructions.
741 if (((dwFeature & SSE2_FEATURE) != 0) && this->Features.HasSSE2) bHasFeature = true;
742
743 // Check for 3DNow! instructions.
744 if (((dwFeature & AMD_3DNOW_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNow) bHasFeature = true;
745
746 // Check for 3DNow+ instructions.
747 if (((dwFeature & AMD_3DNOW_PLUS_FEATURE) != 0) && this->Features.ExtendedFeatures.Has3DNowPlus) bHasFeature = true;
748
749 // Check for IA64 instructions.
750 if (((dwFeature & IA64_FEATURE) != 0) && this->Features.HasIA64) bHasFeature = true;
751
752 // Check for MP capable.
753 if (((dwFeature & MP_CAPABLE) != 0) && this->Features.ExtendedFeatures.SupportsMP) bHasFeature = true;
754
755 // Check for a serial number for the processor.
756 if (((dwFeature & SERIALNUMBER_FEATURE) != 0) && this->Features.HasSerial) bHasFeature = true;
757
758 // Check for a local APIC in the processor.
759 if (((dwFeature & APIC_FEATURE) != 0) && this->Features.HasAPIC) bHasFeature = true;
760
761 // Check for CMOV instructions.
762 if (((dwFeature & CMOV_FEATURE) != 0) && this->Features.HasCMOV) bHasFeature = true;
763
764 // Check for MTRR instructions.
765 if (((dwFeature & MTRR_FEATURE) != 0) && this->Features.HasMTRR) bHasFeature = true;
766
767 // Check for L1 cache size.
768 if (((dwFeature & L1CACHE_FEATURE) != 0) && (this->Features.L1CacheSize != -1)) bHasFeature = true;
769
770 // Check for L2 cache size.
771 if (((dwFeature & L2CACHE_FEATURE) != 0) && (this->Features.L2CacheSize != -1)) bHasFeature = true;
772
773 // Check for L3 cache size.
774 if (((dwFeature & L3CACHE_FEATURE) != 0) && (this->Features.L3CacheSize != -1)) bHasFeature = true;
775
776 // Check for ACPI capability.
777 if (((dwFeature & ACPI_FEATURE) != 0) && this->Features.HasACPI) bHasFeature = true;
778
779 // Check for thermal monitor support.
780 if (((dwFeature & THERMALMONITOR_FEATURE) != 0) && this->Features.HasThermal) bHasFeature = true;
781
782 // Check for temperature sensing diode support.
783 if (((dwFeature & TEMPSENSEDIODE_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode) bHasFeature = true;
784
785 // Check for frequency ID support.
786 if (((dwFeature & FREQUENCYID_FEATURE) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID) bHasFeature = true;
787
788 // Check for voltage ID support.
789 if (((dwFeature & VOLTAGEID_FREQUENCY) != 0) && this->Features.ExtendedFeatures.PowerManagement.HasVoltageID) bHasFeature = true;
790
791 return bHasFeature;
792 }
793
794 void SystemInformationImplementation::Delay(unsigned int uiMS)
795 {
796 #ifdef WIN32
797 LARGE_INTEGER Frequency, StartCounter, EndCounter;
798 __int64 x;
799
800 // Get the frequency of the high performance counter.
801 if (!QueryPerformanceFrequency (&Frequency)) return;
802 x = Frequency.QuadPart / 1000 * uiMS;
803
804 // Get the starting position of the counter.
805 QueryPerformanceCounter (&StartCounter);
806
807 do {
808 // Get the ending position of the counter.
809 QueryPerformanceCounter (&EndCounter);
810 } while (EndCounter.QuadPart - StartCounter.QuadPart < x);
811 #endif
812 (void)uiMS;
813 }
814
815 bool SystemInformationImplementation::DoesCPUSupportCPUID()
816 {
817 #if USE_ASM_INSTRUCTIONS
818 // Use SEH to determine CPUID presence
819 __try {
820 _asm {
821 #ifdef CPUID_AWARE_COMPILER
822 ; we must push/pop the registers <<CPUID>> writes to, as the
823 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
824 ; these registers to change.
825 push eax
826 push ebx
827 push ecx
828 push edx
829 #endif
830 ; <<CPUID>>
831 mov eax, 0
832 CPUID_INSTRUCTION
833
834 #ifdef CPUID_AWARE_COMPILER
835 pop edx
836 pop ecx
837 pop ebx
838 pop eax
839 #endif
840 }
841 }
842 __except(1)
843 {
844 // Stop the class from trying to use CPUID again!
845 return false;
846 }
847
848 // The cpuid instruction succeeded.
849 return true;
850 #else
851 // Assume no cpuid instruction.
852 return false;
853 #endif
854 }
855
856 bool SystemInformationImplementation::RetrieveCPUFeatures()
857 {
858 #if USE_ASM_INSTRUCTIONS
859 int localCPUFeatures = 0;
860 int localCPUAdvanced = 0;
861
862
863 // Use assembly to detect CPUID information...
864 __try {
865 _asm {
866 #ifdef CPUID_AWARE_COMPILER
867 ; we must push/pop the registers <<CPUID>> writes to, as the
868 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
869 ; these registers to change.
870 push eax
871 push ebx
872 push ecx
873 push edx
874 #endif
875 ; <<CPUID>>
876 ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
877 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
878 ; edx: CPU feature flags
879 mov eax,1
880 CPUID_INSTRUCTION
881 mov localCPUFeatures, edx
882 mov localCPUAdvanced, ebx
883
884 #ifdef CPUID_AWARE_COMPILER
885 pop edx
886 pop ecx
887 pop ebx
888 pop eax
889 #endif
890 }
891 }
892 __except(1)
893 {
894 return false;
895 }
896
897 // Retrieve the features of CPU present.
898 this->Features.HasFPU = ((localCPUFeatures & 0x00000001) != 0); // FPU Present --> Bit 0
899 this->Features.HasTSC = ((localCPUFeatures & 0x00000010) != 0); // TSC Present --> Bit 4
900 this->Features.HasAPIC = ((localCPUFeatures & 0x00000200) != 0); // APIC Present --> Bit 9
901 this->Features.HasMTRR = ((localCPUFeatures & 0x00001000) != 0); // MTRR Present --> Bit 12
902 this->Features.HasCMOV = ((localCPUFeatures & 0x00008000) != 0); // CMOV Present --> Bit 15
903 this->Features.HasSerial = ((localCPUFeatures & 0x00040000) != 0); // Serial Present --> Bit 18
904 this->Features.HasACPI = ((localCPUFeatures & 0x00400000) != 0); // ACPI Capable --> Bit 22
905 this->Features.HasMMX = ((localCPUFeatures & 0x00800000) != 0); // MMX Present --> Bit 23
906 this->Features.HasSSE = ((localCPUFeatures & 0x02000000) != 0); // SSE Present --> Bit 25
907 this->Features.HasSSE2 = ((localCPUFeatures & 0x04000000) != 0); // SSE2 Present --> Bit 26
908 this->Features.HasThermal = ((localCPUFeatures & 0x20000000) != 0); // Thermal Monitor Present --> Bit 29
909 this->Features.HasIA64 = ((localCPUFeatures & 0x40000000) != 0); // IA64 Present --> Bit 30
910
911 // Retrieve extended SSE capabilities if SSE is available.
912 if (this->Features.HasSSE) {
913
914 // Attempt to __try some SSE FP instructions.
915 __try
916 {
917 // Perform: orps xmm0, xmm0
918 _asm
919 {
920 _emit 0x0f
921 _emit 0x56
922 _emit 0xc0
923 }
924
925 // SSE FP capable processor.
926 this->Features.HasSSEFP = true;
927 }
928 __except(1)
929 {
930 // bad instruction - processor or OS cannot handle SSE FP.
931 this->Features.HasSSEFP = false;
932 }
933 }
934 else
935 {
936 // Set the advanced SSE capabilities to not available.
937 this->Features.HasSSEFP = false;
938 }
939
940 // Retrieve Intel specific extended features.
941 if (this->ChipManufacturer == Intel)
942 {
943 this->Features.ExtendedFeatures.SupportsHyperthreading = ((localCPUFeatures & 0x10000000) != 0); // Intel specific: Hyperthreading --> Bit 28
944 this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = (this->Features.ExtendedFeatures.SupportsHyperthreading) ? ((localCPUAdvanced & 0x00FF0000) >> 16) : 1;
945
946 if ((this->Features.ExtendedFeatures.SupportsHyperthreading) && (this->Features.HasAPIC))
947 {
948 // Retrieve APIC information if there is one present.
949 this->Features.ExtendedFeatures.APIC_ID = ((localCPUAdvanced & 0xFF000000) >> 24);
950 }
951 }
952 #endif
953 return true;
954 }
955
956
957 /** Find the manufacturer given the vendor id */
958 void SystemInformationImplementation::FindManufacturer()
959 {
960 if (strcmp (this->ChipID.Vendor, "GenuineIntel") == 0) this->ChipManufacturer = Intel; // Intel Corp.
961 else if (strcmp (this->ChipID.Vendor, "UMC UMC UMC ") == 0) this->ChipManufacturer = UMC; // United Microelectronics Corp.
962 else if (strcmp (this->ChipID.Vendor, "AuthenticAMD") == 0) this->ChipManufacturer = AMD; // Advanced Micro Devices
963 else if (strcmp (this->ChipID.Vendor, "AMD ISBETTER") == 0) this->ChipManufacturer = AMD; // Advanced Micro Devices (1994)
964 else if (strcmp (this->ChipID.Vendor, "CyrixInstead") == 0) this->ChipManufacturer = Cyrix; // Cyrix Corp., VIA Inc.
965 else if (strcmp (this->ChipID.Vendor, "NexGenDriven") == 0) this->ChipManufacturer = NexGen; // NexGen Inc. (now AMD)
966 else if (strcmp (this->ChipID.Vendor, "CentaurHauls") == 0) this->ChipManufacturer = IDT; // IDT/Centaur (now VIA)
967 else if (strcmp (this->ChipID.Vendor, "RiseRiseRise") == 0) this->ChipManufacturer = Rise; // Rise
968 else if (strcmp (this->ChipID.Vendor, "GenuineTMx86") == 0) this->ChipManufacturer = Transmeta; // Transmeta
969 else if (strcmp (this->ChipID.Vendor, "TransmetaCPU") == 0) this->ChipManufacturer = Transmeta; // Transmeta
970 else if (strcmp (this->ChipID.Vendor, "Geode By NSC") == 0) this->ChipManufacturer = NSC; // National Semiconductor
971 else if (strcmp (this->ChipID.Vendor, "Sun") == 0) this->ChipManufacturer = Sun; // Sun Microelectronics
972 else this->ChipManufacturer = UnknownManufacturer; // Unknown manufacturer
973 }
974
975 /** */
976 bool SystemInformationImplementation::RetrieveCPUIdentity()
977 {
978 #if USE_ASM_INSTRUCTIONS
979 int localCPUVendor[3];
980 int localCPUSignature;
981
982 // Use assembly to detect CPUID information...
983 __try
984 {
985 _asm
986 {
987 #ifdef CPUID_AWARE_COMPILER
988 ; we must push/pop the registers <<CPUID>> writes to, as the
989 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
990 ; these registers to change.
991 push eax
992 push ebx
993 push ecx
994 push edx
995 #endif
996 ; <<CPUID>>
997 ; eax = 0 --> eax: maximum value of CPUID instruction.
998 ; ebx: part 1 of 3; CPU signature.
999 ; edx: part 2 of 3; CPU signature.
1000 ; ecx: part 3 of 3; CPU signature.
1001 mov eax, 0
1002 CPUID_INSTRUCTION
1003 mov localCPUVendor[0 * TYPE int], ebx
1004 mov localCPUVendor[1 * TYPE int], edx
1005 mov localCPUVendor[2 * TYPE int], ecx
1006
1007 ; <<CPUID>>
1008 ; eax = 1 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
1009 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
1010 ; edx: CPU feature flags
1011 mov eax,1
1012 CPUID_INSTRUCTION
1013 mov localCPUSignature, eax
1014
1015 #ifdef CPUID_AWARE_COMPILER
1016 pop edx
1017 pop ecx
1018 pop ebx
1019 pop eax
1020 #endif
1021 }
1022 }
1023 __except(1)
1024 {
1025 return false;
1026 }
1027
1028 // Process the returned information.
1029 memcpy (this->ChipID.Vendor, &(localCPUVendor[0]), sizeof (int));
1030 memcpy (&(this->ChipID.Vendor[4]), &(localCPUVendor[1]), sizeof (int));
1031 memcpy (&(this->ChipID.Vendor[8]), &(localCPUVendor[2]), sizeof (int));
1032 this->ChipID.Vendor[12] = '\0';
1033
1034 this->FindManufacturer();
1035
1036 // Retrieve the family of CPU present.
1037 this->ChipID.ExtendedFamily = ((localCPUSignature & 0x0FF00000) >> 20); // Bits 27..20 Used
1038 this->ChipID.ExtendedModel = ((localCPUSignature & 0x000F0000) >> 16); // Bits 19..16 Used
1039 this->ChipID.Type = ((localCPUSignature & 0x0000F000) >> 12); // Bits 15..12 Used
1040 this->ChipID.Family = ((localCPUSignature & 0x00000F00) >> 8); // Bits 11..8 Used
1041 this->ChipID.Model = ((localCPUSignature & 0x000000F0) >> 4); // Bits 7..4 Used
1042 this->ChipID.Revision = ((localCPUSignature & 0x0000000F) >> 0); // Bits 3..0 Used
1043 #endif
1044
1045 return true;
1046 }
1047
1048 /** */
1049 bool SystemInformationImplementation::RetrieveCPUCacheDetails()
1050 {
1051 #if USE_ASM_INSTRUCTIONS
1052 int L1Cache[4] = { 0, 0, 0, 0 };
1053 int L2Cache[4] = { 0, 0, 0, 0 };
1054
1055 // Check to see if what we are about to do is supported...
1056 if (RetrieveCPUExtendedLevelSupport (0x80000005))
1057 {
1058 // Use assembly to retrieve the L1 cache information ...
1059 __try
1060 {
1061 _asm
1062 {
1063 #ifdef CPUID_AWARE_COMPILER
1064 ; we must push/pop the registers <<CPUID>> writes to, as the
1065 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1066 ; these registers to change.
1067 push eax
1068 push ebx
1069 push ecx
1070 push edx
1071 #endif
1072 ; <<CPUID>>
1073 ; eax = 0x80000005 --> eax: L1 cache information - Part 1 of 4.
1074 ; ebx: L1 cache information - Part 2 of 4.
1075 ; edx: L1 cache information - Part 3 of 4.
1076 ; ecx: L1 cache information - Part 4 of 4.
1077 mov eax, 0x80000005
1078 CPUID_INSTRUCTION
1079 mov L1Cache[0 * TYPE int], eax
1080 mov L1Cache[1 * TYPE int], ebx
1081 mov L1Cache[2 * TYPE int], ecx
1082 mov L1Cache[3 * TYPE int], edx
1083
1084 #ifdef CPUID_AWARE_COMPILER
1085 pop edx
1086 pop ecx
1087 pop ebx
1088 pop eax
1089 #endif
1090 }
1091 }
1092 __except(1)
1093 {
1094 return false;
1095 }
1096 // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as data cache size from edx: bits 31..24.
1097 this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24);
1098 this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24);
1099 }
1100 else
1101 {
1102 // Store -1 to indicate the cache could not be queried.
1103 this->Features.L1CacheSize = -1;
1104 }
1105
1106 // Check to see if what we are about to do is supported...
1107 if (RetrieveCPUExtendedLevelSupport (0x80000006))
1108 {
1109 // Use assembly to retrieve the L2 cache information ...
1110 __try
1111 {
1112 _asm
1113 {
1114 #ifdef CPUID_AWARE_COMPILER
1115 ; we must push/pop the registers <<CPUID>> writes to, as the
1116 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1117 ; these registers to change.
1118 push eax
1119 push ebx
1120 push ecx
1121 push edx
1122 #endif
1123 ; <<CPUID>>
1124 ; eax = 0x80000006 --> eax: L2 cache information - Part 1 of 4.
1125 ; ebx: L2 cache information - Part 2 of 4.
1126 ; edx: L2 cache information - Part 3 of 4.
1127 ; ecx: L2 cache information - Part 4 of 4.
1128 mov eax, 0x80000006
1129 CPUID_INSTRUCTION
1130 mov L2Cache[0 * TYPE int], eax
1131 mov L2Cache[1 * TYPE int], ebx
1132 mov L2Cache[2 * TYPE int], ecx
1133 mov L2Cache[3 * TYPE int], edx
1134
1135 #ifdef CPUID_AWARE_COMPILER
1136 pop edx
1137 pop ecx
1138 pop ebx
1139 pop eax
1140 #endif
1141 }
1142 }
1143 __except(1)
1144 {
1145 return false;
1146 }
1147 // Save the L2 unified cache size (in KB) from ecx: bits 31..16.
1148 this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16);
1149 }
1150 else
1151 {
1152 // Store -1 to indicate the cache could not be queried.
1153 this->Features.L2CacheSize = -1;
1154 }
1155
1156 // Define L3 as being not present as we cannot test for it.
1157 this->Features.L3CacheSize = -1;
1158
1159 #endif
1160
1161 // Return failure if we cannot detect either cache with this method.
1162 return ((this->Features.L1CacheSize == -1) && (this->Features.L2CacheSize == -1)) ? false : true;
1163 }
1164
1165 /** */
1166 bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails()
1167 {
1168 #if USE_ASM_INSTRUCTIONS
1169 int TLBCode = -1, TLBData = -1, L1Code = -1, L1Data = -1, L1Trace = -1, L2Unified = -1, L3Unified = -1;
1170 int TLBCacheData[4] = { 0, 0, 0, 0 };
1171 int TLBPassCounter = 0;
1172 int TLBCacheUnit = 0;
1173
1174
1175 do {
1176 // Use assembly to retrieve the L2 cache information ...
1177 __try {
1178 _asm {
1179 #ifdef CPUID_AWARE_COMPILER
1180 ; we must push/pop the registers <<CPUID>> writes to, as the
1181 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1182 ; these registers to change.
1183 push eax
1184 push ebx
1185 push ecx
1186 push edx
1187 #endif
1188 ; <<CPUID>>
1189 ; eax = 2 --> eax: TLB and cache information - Part 1 of 4.
1190 ; ebx: TLB and cache information - Part 2 of 4.
1191 ; ecx: TLB and cache information - Part 3 of 4.
1192 ; edx: TLB and cache information - Part 4 of 4.
1193 mov eax, 2
1194 CPUID_INSTRUCTION
1195 mov TLBCacheData[0 * TYPE int], eax
1196 mov TLBCacheData[1 * TYPE int], ebx
1197 mov TLBCacheData[2 * TYPE int], ecx
1198 mov TLBCacheData[3 * TYPE int], edx
1199
1200 #ifdef CPUID_AWARE_COMPILER
1201 pop edx
1202 pop ecx
1203 pop ebx
1204 pop eax
1205 #endif
1206 }
1207 }
1208 __except(1)
1209 {
1210 return false;
1211 }
1212
1213 int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16);
1214 (void)bob;
1215 // Process the returned TLB and cache information.
1216 for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++)
1217 {
1218 // First of all - decide which unit we are dealing with.
1219 switch (nCounter)
1220 {
1221 // eax: bits 8..15 : bits 16..23 : bits 24..31
1222 case 0: TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8); break;
1223 case 1: TLBCacheUnit = ((TLBCacheData[0] & 0x00FF0000) >> 16); break;
1224 case 2: TLBCacheUnit = ((TLBCacheData[0] & 0xFF000000) >> 24); break;
1225
1226 // ebx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1227 case 3: TLBCacheUnit = ((TLBCacheData[1] & 0x000000FF) >> 0); break;
1228 case 4: TLBCacheUnit = ((TLBCacheData[1] & 0x0000FF00) >> 8); break;
1229 case 5: TLBCacheUnit = ((TLBCacheData[1] & 0x00FF0000) >> 16); break;
1230 case 6: TLBCacheUnit = ((TLBCacheData[1] & 0xFF000000) >> 24); break;
1231
1232 // ecx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1233 case 7: TLBCacheUnit = ((TLBCacheData[2] & 0x000000FF) >> 0); break;
1234 case 8: TLBCacheUnit = ((TLBCacheData[2] & 0x0000FF00) >> 8); break;
1235 case 9: TLBCacheUnit = ((TLBCacheData[2] & 0x00FF0000) >> 16); break;
1236 case 10: TLBCacheUnit = ((TLBCacheData[2] & 0xFF000000) >> 24); break;
1237
1238 // edx: bits 0..7 : bits 8..15 : bits 16..23 : bits 24..31
1239 case 11: TLBCacheUnit = ((TLBCacheData[3] & 0x000000FF) >> 0); break;
1240 case 12: TLBCacheUnit = ((TLBCacheData[3] & 0x0000FF00) >> 8); break;
1241 case 13: TLBCacheUnit = ((TLBCacheData[3] & 0x00FF0000) >> 16); break;
1242 case 14: TLBCacheUnit = ((TLBCacheData[3] & 0xFF000000) >> 24); break;
1243
1244 // Default case - an error has occured.
1245 default: return false;
1246 }
1247
1248 // Now process the resulting unit to see what it means....
1249 switch (TLBCacheUnit)
1250 {
1251 case 0x00: break;
1252 case 0x01: STORE_TLBCACHE_INFO (TLBCode, 4); break;
1253 case 0x02: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1254 case 0x03: STORE_TLBCACHE_INFO (TLBData, 4); break;
1255 case 0x04: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1256 case 0x06: STORE_TLBCACHE_INFO (L1Code, 8); break;
1257 case 0x08: STORE_TLBCACHE_INFO (L1Code, 16); break;
1258 case 0x0a: STORE_TLBCACHE_INFO (L1Data, 8); break;
1259 case 0x0c: STORE_TLBCACHE_INFO (L1Data, 16); break;
1260 case 0x10: STORE_TLBCACHE_INFO (L1Data, 16); break; // <-- FIXME: IA-64 Only
1261 case 0x15: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
1262 case 0x1a: STORE_TLBCACHE_INFO (L2Unified, 96); break; // <-- FIXME: IA-64 Only
1263 case 0x22: STORE_TLBCACHE_INFO (L3Unified, 512); break;
1264 case 0x23: STORE_TLBCACHE_INFO (L3Unified, 1024); break;
1265 case 0x25: STORE_TLBCACHE_INFO (L3Unified, 2048); break;
1266 case 0x29: STORE_TLBCACHE_INFO (L3Unified, 4096); break;
1267 case 0x39: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1268 case 0x3c: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1269 case 0x40: STORE_TLBCACHE_INFO (L2Unified, 0); break; // <-- FIXME: No integrated L2 cache (P6 core) or L3 cache (P4 core).
1270 case 0x41: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1271 case 0x42: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1272 case 0x43: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1273 case 0x44: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1274 case 0x45: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
1275 case 0x50: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1276 case 0x51: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1277 case 0x52: STORE_TLBCACHE_INFO (TLBCode, 4096); break;
1278 case 0x5b: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1279 case 0x5c: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1280 case 0x5d: STORE_TLBCACHE_INFO (TLBData, 4096); break;
1281 case 0x66: STORE_TLBCACHE_INFO (L1Data, 8); break;
1282 case 0x67: STORE_TLBCACHE_INFO (L1Data, 16); break;
1283 case 0x68: STORE_TLBCACHE_INFO (L1Data, 32); break;
1284 case 0x70: STORE_TLBCACHE_INFO (L1Trace, 12); break;
1285 case 0x71: STORE_TLBCACHE_INFO (L1Trace, 16); break;
1286 case 0x72: STORE_TLBCACHE_INFO (L1Trace, 32); break;
1287 case 0x77: STORE_TLBCACHE_INFO (L1Code, 16); break; // <-- FIXME: IA-64 Only
1288 case 0x79: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1289 case 0x7a: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1290 case 0x7b: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1291 case 0x7c: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1292 case 0x7e: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1293 case 0x81: STORE_TLBCACHE_INFO (L2Unified, 128); break;
1294 case 0x82: STORE_TLBCACHE_INFO (L2Unified, 256); break;
1295 case 0x83: STORE_TLBCACHE_INFO (L2Unified, 512); break;
1296 case 0x84: STORE_TLBCACHE_INFO (L2Unified, 1024); break;
1297 case 0x85: STORE_TLBCACHE_INFO (L2Unified, 2048); break;
1298 case 0x88: STORE_TLBCACHE_INFO (L3Unified, 2048); break; // <-- FIXME: IA-64 Only
1299 case 0x89: STORE_TLBCACHE_INFO (L3Unified, 4096); break; // <-- FIXME: IA-64 Only
1300 case 0x8a: STORE_TLBCACHE_INFO (L3Unified, 8192); break; // <-- FIXME: IA-64 Only
1301 case 0x8d: STORE_TLBCACHE_INFO (L3Unified, 3096); break; // <-- FIXME: IA-64 Only
1302 case 0x90: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1303 case 0x96: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1304 case 0x9b: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only
1305
1306 // Default case - an error has occured.
1307 default: return false;
1308 }
1309 }
1310
1311 // Increment the TLB pass counter.
1312 TLBPassCounter ++;
1313 } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter);
1314
1315 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1316 if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1))
1317 {
1318 this->Features.L1CacheSize = -1;
1319 }
1320 else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1))
1321 {
1322 this->Features.L1CacheSize = L1Trace;
1323 }
1324 else if ((L1Code != -1) && (L1Data == -1))
1325 {
1326 this->Features.L1CacheSize = L1Code;
1327 }
1328 else if ((L1Code == -1) && (L1Data != -1))
1329 {
1330 this->Features.L1CacheSize = L1Data;
1331 }
1332 else if ((L1Code != -1) && (L1Data != -1))
1333 {
1334 this->Features.L1CacheSize = L1Code + L1Data;
1335 }
1336 else
1337 {
1338 this->Features.L1CacheSize = -1;
1339 }
1340
1341 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1342 if (L2Unified == -1)
1343 {
1344 this->Features.L2CacheSize = -1;
1345 }
1346 else
1347 {
1348 this->Features.L2CacheSize = L2Unified;
1349 }
1350
1351 // Ok - we now have the maximum TLB, L1, L2, and L3 sizes...
1352 if (L3Unified == -1)
1353 {
1354 this->Features.L3CacheSize = -1;
1355 }
1356 else
1357 {
1358 this->Features.L3CacheSize = L3Unified;
1359 }
1360
1361 #endif
1362 return true;
1363 }
1364
1365 /** */
1366 bool SystemInformationImplementation::RetrieveCPUClockSpeed()
1367 {
1368 #if _WIN32
1369 // First of all we check to see if the RDTSC (0x0F, 0x31) instruction is supported.
1370 if (!this->Features.HasTSC)
1371 {
1372 return false;
1373 }
1374
1375 unsigned int uiRepetitions = 1;
1376 unsigned int uiMSecPerRepetition = 50;
1377 __int64 i64Total = 0;
1378 __int64 i64Overhead = 0;
1379
1380 for (unsigned int nCounter = 0; nCounter < uiRepetitions; nCounter ++)
1381 {
1382 i64Total += GetCyclesDifference (SystemInformationImplementation::Delay,
1383 uiMSecPerRepetition);
1384 i64Overhead +=
1385 GetCyclesDifference (SystemInformationImplementation::DelayOverhead,
1386 uiMSecPerRepetition);
1387 }
1388
1389 // Calculate the MHz speed.
1390 i64Total -= i64Overhead;
1391 i64Total /= uiRepetitions;
1392 i64Total /= uiMSecPerRepetition;
1393 i64Total /= 1000;
1394
1395 // Save the CPU speed.
1396 this->CPUSpeedInMHz = (float) i64Total;
1397
1398 return true;
1399 #else
1400 return false;
1401 #endif
1402 }
1403
1404 /** */
1405 bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed()
1406 {
1407 #if USE_ASM_INSTRUCTIONS
1408 LARGE_INTEGER liStart, liEnd, liCountsPerSecond;
1409 double dFrequency, dDifference;
1410
1411 // Attempt to get a starting tick count.
1412 QueryPerformanceCounter (&liStart);
1413
1414 __try
1415 {
1416 _asm
1417 {
1418 mov eax, 0x80000000
1419 mov ebx, CLASSICAL_CPU_FREQ_LOOP
1420 Timer_Loop:
1421 bsf ecx,eax
1422 dec ebx
1423 jnz Timer_Loop
1424 }
1425 }
1426 __except(1)
1427 {
1428 return false;
1429 }
1430
1431 // Attempt to get a starting tick count.
1432 QueryPerformanceCounter (&liEnd);
1433
1434 // Get the difference... NB: This is in seconds....
1435 QueryPerformanceFrequency (&liCountsPerSecond);
1436 dDifference = (((double) liEnd.QuadPart - (double) liStart.QuadPart) / (double) liCountsPerSecond.QuadPart);
1437
1438 // Calculate the clock speed.
1439 if (this->ChipID.Family == 3)
1440 {
1441 // 80386 processors.... Loop time is 115 cycles!
1442 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1048576);
1443 }
1444 else if (this->ChipID.Family == 4)
1445 {
1446 // 80486 processors.... Loop time is 47 cycles!
1447 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1048576);
1448 }
1449 else if (this->ChipID.Family == 5)
1450 {
1451 // Pentium processors.... Loop time is 43 cycles!
1452 dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1048576);
1453 }
1454
1455 // Save the clock speed.
1456 this->Features.CPUSpeed = (int) dFrequency;
1457 #else
1458 return true;
1459 #endif
1460 }
1461
1462 /** */
1463 bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULevelToCheck)
1464 {
1465 int MaxCPUExtendedLevel = 0;
1466
1467 // The extended CPUID is supported by various vendors starting with the following CPU models:
1468 //
1469 // Manufacturer & Chip Name | Family Model Revision
1470 //
1471 // AMD K6, K6-2 | 5 6 x
1472 // Cyrix GXm, Cyrix III "Joshua" | 5 4 x
1473 // IDT C6-2 | 5 8 x
1474 // VIA Cyrix III | 6 5 x
1475 // Transmeta Crusoe | 5 x x
1476 // Intel Pentium 4 | f x x
1477 //
1478
1479 // We check to see if a supported processor is present...
1480 if (this->ChipManufacturer == AMD)
1481 {
1482 if (this->ChipID.Family < 5) return false;
1483 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6)) return false;
1484 }
1485 else if (this->ChipManufacturer == Cyrix)
1486 {
1487 if (this->ChipID.Family < 5) return false;
1488 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4)) return false;
1489 if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5)) return false;
1490 }
1491 else if (this->ChipManufacturer == IDT)
1492 {
1493 if (this->ChipID.Family < 5) return false;
1494 if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8)) return false;
1495 }
1496 else if (this->ChipManufacturer == Transmeta)
1497 {
1498 if (this->ChipID.Family < 5) return false;
1499 }
1500 else if (this->ChipManufacturer == Intel)
1501 {
1502 if (this->ChipID.Family < 0xf)
1503 {
1504 return false;
1505 }
1506 }
1507
1508 #if USE_ASM_INSTRUCTIONS
1509
1510 // Use assembly to detect CPUID information...
1511 __try {
1512 _asm {
1513 #ifdef CPUID_AWARE_COMPILER
1514 ; we must push/pop the registers <<CPUID>> writes to, as the
1515 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1516 ; these registers to change.
1517 push eax
1518 push ebx
1519 push ecx
1520 push edx
1521 #endif
1522 ; <<CPUID>>
1523 ; eax = 0x80000000 --> eax: maximum supported extended level
1524 mov eax,0x80000000
1525 CPUID_INSTRUCTION
1526 mov MaxCPUExtendedLevel, eax
1527
1528 #ifdef CPUID_AWARE_COMPILER
1529 pop edx
1530 pop ecx
1531 pop ebx
1532 pop eax
1533 #endif
1534 }
1535 }
1536 __except(1)
1537 {
1538 return false;
1539 }
1540 #endif
1541
1542 // Now we have to check the level wanted vs level returned...
1543 int nLevelWanted = (CPULevelToCheck & 0x7FFFFFFF);
1544 int nLevelReturn = (MaxCPUExtendedLevel & 0x7FFFFFFF);
1545
1546 // Check to see if the level provided is supported...
1547 if (nLevelWanted > nLevelReturn)
1548 {
1549 return false;
1550 }
1551
1552 return true;
1553 }
1554
1555 /** */
1556 bool SystemInformationImplementation::RetrieveExtendedCPUFeatures()
1557 {
1558
1559 // Check that we are not using an Intel processor as it does not support this.
1560 if (this->ChipManufacturer == Intel)
1561 {
1562 return false;
1563 }
1564
1565 // Check to see if what we are about to do is supported...
1566 if (!RetrieveCPUExtendedLevelSupport (0x80000001))
1567 {
1568 return false;
1569 }
1570 #if USE_ASM_INSTRUCTIONS
1571 int localCPUExtendedFeatures = 0;
1572
1573 // Use assembly to detect CPUID information...
1574 __try
1575 {
1576 _asm
1577 {
1578 #ifdef CPUID_AWARE_COMPILER
1579 ; we must push/pop the registers <<CPUID>> writes to, as the
1580 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1581 ; these registers to change.
1582 push eax
1583 push ebx
1584 push ecx
1585 push edx
1586 #endif
1587 ; <<CPUID>>
1588 ; eax = 0x80000001 --> eax: CPU ID - bits 31..16 - unused, bits 15..12 - type, bits 11..8 - family, bits 7..4 - model, bits 3..0 - mask revision
1589 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID
1590 ; edx: CPU feature flags
1591 mov eax,0x80000001
1592 CPUID_INSTRUCTION
1593 mov localCPUExtendedFeatures, edx
1594
1595 #ifdef CPUID_AWARE_COMPILER
1596 pop edx
1597 pop ecx
1598 pop ebx
1599 pop eax
1600 #endif
1601 }
1602 }
1603 __except(1)
1604 {
1605 return false;
1606 }
1607
1608 // Retrieve the extended features of CPU present.
1609 this->Features.ExtendedFeatures.Has3DNow = ((localCPUExtendedFeatures & 0x80000000) != 0); // 3DNow Present --> Bit 31.
1610 this->Features.ExtendedFeatures.Has3DNowPlus = ((localCPUExtendedFeatures & 0x40000000) != 0); // 3DNow+ Present -- > Bit 30.
1611 this->Features.ExtendedFeatures.HasSSEMMX = ((localCPUExtendedFeatures & 0x00400000) != 0); // SSE MMX Present --> Bit 22.
1612 this->Features.ExtendedFeatures.SupportsMP = ((localCPUExtendedFeatures & 0x00080000) != 0); // MP Capable -- > Bit 19.
1613
1614 // Retrieve AMD specific extended features.
1615 if (this->ChipManufacturer == AMD)
1616 {
1617 this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x00400000) != 0); // AMD specific: MMX-SSE --> Bit 22
1618 }
1619
1620 // Retrieve Cyrix specific extended features.
1621 if (this->ChipManufacturer == Cyrix)
1622 {
1623 this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x01000000) != 0); // Cyrix specific: Extended MMX --> Bit 24
1624 }
1625 #endif
1626
1627 return true;
1628 }
1629
1630 /** */
1631 bool SystemInformationImplementation::RetrieveProcessorSerialNumber()
1632 {
1633 // Check to see if the processor supports the processor serial number.
1634 if (!this->Features.HasSerial)
1635 {
1636 return false;
1637 }
1638
1639 #if USE_ASM_INSTRUCTIONS
1640 int SerialNumber[3];
1641
1642
1643 // Use assembly to detect CPUID information...
1644 __try {
1645 _asm {
1646 #ifdef CPUID_AWARE_COMPILER
1647 ; we must push/pop the registers <<CPUID>> writes to, as the
1648 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1649 ; these registers to change.
1650 push eax
1651 push ebx
1652 push ecx
1653 push edx
1654 #endif
1655 ; <<CPUID>>
1656 ; eax = 3 --> ebx: top 32 bits are the processor signature bits --> NB: Transmeta only ?!?
1657 ; ecx: middle 32 bits are the processor signature bits
1658 ; edx: bottom 32 bits are the processor signature bits
1659 mov eax, 3
1660 CPUID_INSTRUCTION
1661 mov SerialNumber[0 * TYPE int], ebx
1662 mov SerialNumber[1 * TYPE int], ecx
1663 mov SerialNumber[2 * TYPE int], edx
1664
1665 #ifdef CPUID_AWARE_COMPILER
1666 pop edx
1667 pop ecx
1668 pop ebx
1669 pop eax
1670 #endif
1671 }
1672 }
1673 __except(1)
1674 {
1675 return false;
1676 }
1677
1678 // Process the returned information.
1679 sprintf (this->ChipID.SerialNumber, "%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x",
1680 ((SerialNumber[0] & 0xff000000) >> 24),
1681 ((SerialNumber[0] & 0x00ff0000) >> 16),
1682 ((SerialNumber[0] & 0x0000ff00) >> 8),
1683 ((SerialNumber[0] & 0x000000ff) >> 0),
1684 ((SerialNumber[1] & 0xff000000) >> 24),
1685 ((SerialNumber[1] & 0x00ff0000) >> 16),
1686 ((SerialNumber[1] & 0x0000ff00) >> 8),
1687 ((SerialNumber[1] & 0x000000ff) >> 0),
1688 ((SerialNumber[2] & 0xff000000) >> 24),
1689 ((SerialNumber[2] & 0x00ff0000) >> 16),
1690 ((SerialNumber[2] & 0x0000ff00) >> 8),
1691 ((SerialNumber[2] & 0x000000ff) >> 0));
1692 #endif
1693
1694 return true;
1695 }
1696
1697 /** */
1698 bool SystemInformationImplementation::RetrieveCPUPowerManagement()
1699 {
1700 // Check to see if what we are about to do is supported...
1701 if (!RetrieveCPUExtendedLevelSupport (0x80000007))
1702 {
1703 this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = false;
1704 this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = false;
1705 this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = false;
1706 return false;
1707 }
1708
1709 #if USE_ASM_INSTRUCTIONS
1710 int localCPUPowerManagement = 0;
1711
1712
1713 // Use assembly to detect CPUID information...
1714 __try {
1715 _asm {
1716 #ifdef CPUID_AWARE_COMPILER
1717 ; we must push/pop the registers <<CPUID>> writes to, as the
1718 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1719 ; these registers to change.
1720 push eax
1721 push ebx
1722 push ecx
1723 push edx
1724 #endif
1725 ; <<CPUID>>
1726 ; eax = 0x80000007 --> edx: get processor power management
1727 mov eax,0x80000007
1728 CPUID_INSTRUCTION
1729 mov localCPUPowerManagement, edx
1730
1731 #ifdef CPUID_AWARE_COMPILER
1732 pop edx
1733 pop ecx
1734 pop ebx
1735 pop eax
1736 #endif
1737 }
1738 }
1739 __except(1)
1740 {
1741 return false;
1742 }
1743
1744 // Check for the power management capabilities of the CPU.
1745 this->Features.ExtendedFeatures.PowerManagement.HasTempSenseDiode = ((localCPUPowerManagement & 0x00000001) != 0);
1746 this->Features.ExtendedFeatures.PowerManagement.HasFrequencyID = ((localCPUPowerManagement & 0x00000002) != 0);
1747 this->Features.ExtendedFeatures.PowerManagement.HasVoltageID = ((localCPUPowerManagement & 0x00000004) != 0);
1748
1749 #endif
1750
1751 return true;
1752 }
1753
1754 /** */
1755 bool SystemInformationImplementation::RetrieveExtendedCPUIdentity()
1756 {
1757 // Check to see if what we are about to do is supported...
1758 if (!RetrieveCPUExtendedLevelSupport(0x80000002)) return false;
1759 if (!RetrieveCPUExtendedLevelSupport(0x80000003)) return false;
1760 if (!RetrieveCPUExtendedLevelSupport(0x80000004)) return false;
1761
1762 #if USE_ASM_INSTRUCTIONS
1763 int ProcessorNameStartPos = 0;
1764 int CPUExtendedIdentity[12];
1765
1766 // Use assembly to detect CPUID information...
1767 __try {
1768 _asm {
1769 #ifdef CPUID_AWARE_COMPILER
1770 ; we must push/pop the registers <<CPUID>> writes to, as the
1771 ; optimiser doesn't know about <<CPUID>>, and so doesn't expect
1772 ; these registers to change.
1773 push eax
1774 push ebx
1775 push ecx
1776 push edx
1777 #endif
1778 ; <<CPUID>>
1779 ; eax = 0x80000002 --> eax, ebx, ecx, edx: get processor name string (part 1)
1780 mov eax,0x80000002
1781 CPUID_INSTRUCTION
1782 mov CPUExtendedIdentity[0 * TYPE int], eax
1783 mov CPUExtendedIdentity[1 * TYPE int], ebx
1784 mov CPUExtendedIdentity[2 * TYPE int], ecx
1785 mov CPUExtendedIdentity[3 * TYPE int], edx
1786
1787 ; <<CPUID>>
1788 ; eax = 0x80000003 --> eax, ebx, ecx, edx: get processor name string (part 2)
1789 mov eax,0x80000003
1790 CPUID_INSTRUCTION
1791 mov CPUExtendedIdentity[4 * TYPE int], eax
1792 mov CPUExtendedIdentity[5 * TYPE int], ebx
1793 mov CPUExtendedIdentity[6 * TYPE int], ecx
1794 mov CPUExtendedIdentity[7 * TYPE int], edx
1795
1796 ; <<CPUID>>
1797 ; eax = 0x80000004 --> eax, ebx, ecx, edx: get processor name string (part 3)
1798 mov eax,0x80000004
1799 CPUID_INSTRUCTION
1800 mov CPUExtendedIdentity[8 * TYPE int], eax
1801 mov CPUExtendedIdentity[9 * TYPE int], ebx
1802 mov CPUExtendedIdentity[10 * TYPE int], ecx
1803 mov CPUExtendedIdentity[11 * TYPE int], edx
1804
1805 #ifdef CPUID_AWARE_COMPILER
1806 pop edx
1807 pop ecx
1808 pop ebx
1809 pop eax
1810 #endif
1811 }
1812 }
1813 __except(1)
1814 {
1815 return false;
1816 }
1817
1818 // Process the returned information.
1819 memcpy (this->ChipID.ProcessorName, &(CPUExtendedIdentity[0]), sizeof (int));
1820 memcpy (&(this->ChipID.ProcessorName[4]), &(CPUExtendedIdentity[1]), sizeof (int));
1821 memcpy (&(this->ChipID.ProcessorName[8]), &(CPUExtendedIdentity[2]), sizeof (int));
1822 memcpy (&(this->ChipID.ProcessorName[12]), &(CPUExtendedIdentity[3]), sizeof (int));
1823 memcpy (&(this->ChipID.ProcessorName[16]), &(CPUExtendedIdentity[4]), sizeof (int));
1824 memcpy (&(this->ChipID.ProcessorName[20]), &(CPUExtendedIdentity[5]), sizeof (int));
1825 memcpy (&(this->ChipID.ProcessorName[24]), &(CPUExtendedIdentity[6]), sizeof (int));
1826 memcpy (&(this->ChipID.ProcessorName[28]), &(CPUExtendedIdentity[7]), sizeof (int));
1827 memcpy (&(this->ChipID.ProcessorName[32]), &(CPUExtendedIdentity[8]), sizeof (int));
1828 memcpy (&(this->ChipID.ProcessorName[36]), &(CPUExtendedIdentity[9]), sizeof (int));
1829 memcpy (&(this->ChipID.ProcessorName[40]), &(CPUExtendedIdentity[10]), sizeof (int));
1830 memcpy (&(this->ChipID.ProcessorName[44]), &(CPUExtendedIdentity[11]), sizeof (int));
1831 this->ChipID.ProcessorName[48] = '\0';
1832
1833 // Because some manufacturers have leading white space - we have to post-process the name.
1834 if (this->ChipManufacturer == Intel)
1835 {
1836 for (int nCounter = 0; nCounter < CHIPNAME_STRING_LENGTH; nCounter ++)
1837 {
1838 // There will either be NULL (\0) or spaces ( ) as the leading characters.
1839 if ((this->ChipID.ProcessorName[nCounter] != '\0') && (this->ChipID.ProcessorName[nCounter] != ' '))
1840 {
1841 // We have found the starting position of the name.
1842 ProcessorNameStartPos = nCounter;
1843 // Terminate the loop.
1844 break;
1845 }
1846 }
1847
1848 // Check to see if there is any white space at the start.
1849 if (ProcessorNameStartPos == 0)
1850 {
1851 return true;
1852 }
1853
1854 // Now move the name forward so that there is no white space.
1855 memmove(this->ChipID.ProcessorName, &(this->ChipID.ProcessorName[ProcessorNameStartPos]), (CHIPNAME_STRING_LENGTH - ProcessorNameStartPos));
1856 }
1857 #endif
1858
1859 return true;
1860 }
1861
1862 /** */
1863 bool SystemInformationImplementation::RetrieveClassicalCPUIdentity()
1864 {
1865 // Start by decided which manufacturer we are using....
1866 switch (this->ChipManufacturer)
1867 {
1868 case Intel:
1869 // Check the family / model / revision to determine the CPU ID.
1870 switch (this->ChipID.Family) {
1871 case 3:
1872 sprintf (this->ChipID.ProcessorName, "Newer i80386 family");
1873 break;
1874 case 4:
1875 switch (this->ChipID.Model) {
1876 case 0: sprintf (this->ChipID.ProcessorName,"i80486DX-25/33"); break;
1877 case 1: sprintf (this->ChipID.ProcessorName,"i80486DX-50"); break;
1878 case 2: sprintf (this->ChipID.ProcessorName,"i80486SX"); break;
1879 case 3: sprintf (this->ChipID.ProcessorName,"i80486DX2"); break;
1880 case 4: sprintf (this->ChipID.ProcessorName,"i80486SL"); break;
1881 case 5: sprintf (this->ChipID.ProcessorName,"i80486SX2"); break;
1882 case 7: sprintf (this->ChipID.ProcessorName,"i80486DX2 WriteBack"); break;
1883 case 8: sprintf (this->ChipID.ProcessorName,"i80486DX4"); break;
1884 case 9: sprintf (this->ChipID.ProcessorName,"i80486DX4 WriteBack"); break;
1885 default: sprintf (this->ChipID.ProcessorName,"Unknown 80486 family"); return false;
1886 }
1887 break;
1888 case 5:
1889 switch (this->ChipID.Model)
1890 {
1891 case 0: sprintf (this->ChipID.ProcessorName,"P5 A-Step"); break;
1892 case 1: sprintf (this->ChipID.ProcessorName,"P5"); break;
1893 case 2: sprintf (this->ChipID.ProcessorName,"P54C"); break;
1894 case 3: sprintf (this->ChipID.ProcessorName,"P24T OverDrive"); break;
1895 case 4: sprintf (this->ChipID.ProcessorName,"P55C"); break;
1896 case 7: sprintf (this->ChipID.ProcessorName,"P54C"); break;
1897 case 8: sprintf (this->ChipID.ProcessorName,"P55C (0.25micron)"); break;
1898 default: sprintf (this->ChipID.ProcessorName,"Unknown Pentium family"); return false;
1899 }
1900 break;
1901 case 6:
1902 switch (this->ChipID.Model)
1903 {
1904 case 0: sprintf (this->ChipID.ProcessorName,"P6 A-Step"); break;
1905 case 1: sprintf (this->ChipID.ProcessorName,"P6"); break;
1906 case 3: sprintf (this->ChipID.ProcessorName,"Pentium II (0.28 micron)"); break;
1907 case 5: sprintf (this->ChipID.ProcessorName,"Pentium II (0.25 micron)"); break;
1908 case 6: sprintf (this->ChipID.ProcessorName,"Pentium II With On-Die L2 Cache"); break;
1909 case 7: sprintf (this->ChipID.ProcessorName,"Pentium III (0.25 micron)"); break;
1910 case 8: sprintf (this->ChipID.ProcessorName,"Pentium III (0.18 micron) With 256 KB On-Die L2 Cache "); break;
1911 case 0xa: sprintf (this->ChipID.ProcessorName,"Pentium III (0.18 micron) With 1 Or 2 MB On-Die L2 Cache "); break;
1912 case 0xb: sprintf (this->ChipID.ProcessorName,"Pentium III (0.13 micron) With 256 Or 512 KB On-Die L2 Cache "); break;
1913 default: sprintf (this->ChipID.ProcessorName,"Unknown P6 family"); return false;
1914 }
1915 break;
1916 case 7:
1917 sprintf (this->ChipID.ProcessorName,"Intel Merced (IA-64)");
1918 break;
1919 case 0xf:
1920 // Check the extended family bits...
1921 switch (this->ChipID.ExtendedFamily)
1922 {
1923 case 0:
1924 switch (this->ChipID.Model)
1925 {
1926 case 0: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.18 micron)"); break;
1927 case 1: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.18 micron)"); break;
1928 case 2: sprintf (this->ChipID.ProcessorName,"Pentium IV (0.13 micron)"); break;
1929 default: sprintf (this->ChipID.ProcessorName,"Unknown Pentium 4 family"); return false;
1930 }
1931 break;
1932 case 1:
1933 sprintf (this->ChipID.ProcessorName,"Intel McKinley (IA-64)");
1934 break;
1935 default:
1936 sprintf (this->ChipID.ProcessorName,"Pentium");
1937 }
1938 break;
1939 default:
1940 sprintf (this->ChipID.ProcessorName,"Unknown Intel family");
1941 return false;
1942 }
1943 break;
1944
1945 case AMD:
1946 // Check the family / model / revision to determine the CPU ID.
1947 switch (this->ChipID.Family)
1948 {
1949 case 4:
1950 switch (this->ChipID.Model)
1951 {
1952 case 3: sprintf (this->ChipID.ProcessorName,"80486DX2"); break;
1953 case 7: sprintf (this->ChipID.ProcessorName,"80486DX2 WriteBack"); break;
1954 case 8: sprintf (this->ChipID.ProcessorName,"80486DX4"); break;
1955 case 9: sprintf (this->ChipID.ProcessorName,"80486DX4 WriteBack"); break;
1956 case 0xe: sprintf (this->ChipID.ProcessorName,"5x86"); break;
1957 case 0xf: sprintf (this->ChipID.ProcessorName,"5x86WB"); break;
1958 default: sprintf (this->ChipID.ProcessorName,"Unknown 80486 family"); return false;
1959 }
1960 break;
1961 case 5:
1962 switch (this->ChipID.Model)
1963 {
1964 case 0: sprintf (this->ChipID.ProcessorName,"SSA5 (PR75, PR90, PR100)"); break;
1965 case 1: sprintf (this->ChipID.ProcessorName,"5k86 (PR120, PR133)"); break;
1966 case 2: sprintf (this->ChipID.ProcessorName,"5k86 (PR166)"); break;
1967 case 3: sprintf (this->ChipID.ProcessorName,"5k86 (PR200)"); break;
1968 case 6: sprintf (this->ChipID.ProcessorName,"K6 (0.30 micron)"); break;
1969 case 7: sprintf (this->ChipID.ProcessorName,"K6 (0.25 micron)"); break;
1970 case 8: sprintf (this->ChipID.ProcessorName,"K6-2"); break;
1971 case 9: sprintf (this->ChipID.ProcessorName,"K6-III"); break;
1972 case 0xd: sprintf (this->ChipID.ProcessorName,"K6-2+ or K6-III+ (0.18 micron)"); break;
1973 default: sprintf (this->ChipID.ProcessorName,"Unknown 80586 family"); return false;
1974 }
1975 break;
1976 case 6:
1977 switch (this->ChipID.Model)
1978 {
1979 case 1: sprintf (this->ChipID.ProcessorName,"Athlon- (0.25 micron)"); break;
1980 case 2: sprintf (this->ChipID.ProcessorName,"Athlon- (0.18 micron)"); break;
1981 case 3: sprintf (this->ChipID.ProcessorName,"Duron- (SF core)"); break;
1982 case 4: sprintf (this->ChipID.ProcessorName,"Athlon- (Thunderbird core)"); break;
1983 case 6: sprintf (this->ChipID.ProcessorName,"Athlon- (Palomino core)"); break;
1984 case 7: sprintf (this->ChipID.ProcessorName,"Duron- (Morgan core)"); break;
1985 case 8:
1986 if (this->Features.ExtendedFeatures.SupportsMP)
1987 sprintf (this->ChipID.ProcessorName,"Athlon - MP (Thoroughbred core)");
1988 else sprintf (this->ChipID.ProcessorName,"Athlon - XP (Thoroughbred core)");
1989 break;
1990 default: sprintf (this->ChipID.ProcessorName,"Unknown K7 family"); return false;
1991 }
1992 break;
1993 default:
1994 sprintf (this->ChipID.ProcessorName,"Unknown AMD family");
1995 return false;
1996 }
1997 break;
1998
1999 case Transmeta:
2000 switch (this->ChipID.Family)
2001 {
2002 case 5:
2003 switch (this->ChipID.Model)
2004 {
2005 case 4: sprintf (this->ChipID.ProcessorName,"Crusoe TM3x00 and TM5x00"); break;
2006 default: sprintf (this->ChipID.ProcessorName,"Unknown Crusoe family"); return false;
2007 }
2008 break;
2009 default:
2010 sprintf (this->ChipID.ProcessorName,"Unknown Transmeta family");
2011 return false;
2012 }
2013 break;
2014
2015 case Rise:
2016 switch (this->ChipID.Family)
2017 {
2018 case 5:
2019 switch (this->ChipID.Model)
2020 {
2021 case 0: sprintf (this->ChipID.ProcessorName,"mP6 (0.25 micron)"); break;
2022 case 2: sprintf (this->ChipID.ProcessorName,"mP6 (0.18 micron)"); break;
2023 default: sprintf (this->ChipID.ProcessorName,"Unknown Rise family"); return false;
2024 }
2025 break;
2026 default:
2027 sprintf (this->ChipID.ProcessorName,"Unknown Rise family");
2028 return false;
2029 }
2030 break;
2031
2032 case UMC:
2033 switch (this->ChipID.Family)
2034 {
2035 case 4:
2036 switch (this->ChipID.Model)
2037 {
2038 case 1: sprintf (this->ChipID.ProcessorName,"U5D"); break;
2039 case 2: sprintf (this->ChipID.ProcessorName,"U5S"); break;
2040 default: sprintf (this->ChipID.ProcessorName,"Unknown UMC family"); return false;
2041 }
2042 break;
2043 default:
2044 sprintf (this->ChipID.ProcessorName,"Unknown UMC family");
2045 return false;
2046 }
2047 break;
2048
2049 case IDT:
2050 switch (this->ChipID.Family)
2051 {
2052 case 5:
2053 switch (this->ChipID.Model)
2054 {
2055 case 4: sprintf (this->ChipID.ProcessorName,"C6"); break;
2056 case 8: sprintf (this->ChipID.ProcessorName,"C2"); break;
2057 case 9: sprintf (this->ChipID.ProcessorName,"C3"); break;
2058 default: sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family"); return false;
2059 }
2060 break;
2061 case 6:
2062 switch (this->ChipID.Model)
2063 {
2064 case 6: sprintf (this->ChipID.ProcessorName,"VIA Cyrix III - Samuel"); break;
2065 default: sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family"); return false;
2066 }
2067 break;
2068 default:
2069 sprintf (this->ChipID.ProcessorName,"Unknown IDT\\Centaur family");
2070 return false;
2071 }
2072 break;
2073
2074 case Cyrix:
2075 switch (this->ChipID.Family)
2076 {
2077 case 4:
2078 switch (this->ChipID.Model)
2079 {
2080 case 4: sprintf (this->ChipID.ProcessorName,"MediaGX GX, GXm"); break;
2081 case 9: sprintf (this->ChipID.ProcessorName,"5x86"); break;
2082 default: sprintf (this->ChipID.ProcessorName,"Unknown Cx5x86 family"); return false;
2083 }
2084 break;
2085 case 5:
2086 switch (this->ChipID.Model)
2087 {
2088 case 2: sprintf (this->ChipID.ProcessorName,"Cx6x86"); break;
2089 case 4: sprintf (this->ChipID.ProcessorName,"MediaGX GXm"); break;
2090 default: sprintf (this->ChipID.ProcessorName,"Unknown Cx6x86 family"); return false;
2091 }
2092 break;
2093 case 6:
2094 switch (this->ChipID.Model)
2095 {
2096 case 0: sprintf (this->ChipID.ProcessorName,"6x86MX"); break;
2097 case 5: sprintf (this->ChipID.ProcessorName,"Cyrix M2 Core"); break;
2098 case 6: sprintf (this->ChipID.ProcessorName,"WinChip C5A Core"); break;
2099 case 7: sprintf (this->ChipID.ProcessorName,"WinChip C5B\\C5C Core"); break;
2100 case 8: sprintf (this->ChipID.ProcessorName,"WinChip C5C-T Core"); break;
2101 default: sprintf (this->ChipID.ProcessorName,"Unknown 6x86MX\\Cyrix III family"); return false;
2102 }
2103 break;
2104 default:
2105 sprintf (this->ChipID.ProcessorName,"Unknown Cyrix family");
2106 return false;
2107 }
2108 break;
2109
2110 case NexGen:
2111 switch (this->ChipID.Family)
2112 {
2113 case 5:
2114 switch (this->ChipID.Model)
2115 {
2116 case 0: sprintf (this->ChipID.ProcessorName,"Nx586 or Nx586FPU"); break;
2117 default: sprintf (this->ChipID.ProcessorName,"Unknown NexGen family"); return false;
2118 }
2119 break;
2120 default:
2121 sprintf (this->ChipID.ProcessorName,"Unknown NexGen family");
2122 return false;
2123 }
2124 break;
2125
2126 case NSC:
2127 sprintf (this->ChipID.ProcessorName,"Cx486SLC \\ DLC \\ Cx486S A-Step");
2128 break;
2129 default:
2130 sprintf (this->ChipID.ProcessorName,"Unknown family"); // We cannot identify the processor.
2131 return false;
2132 }
2133
2134 return true;
2135 }
2136
2137 /** Extract a value from the CPUInfo file */
2138 kwsys_stl::string SystemInformationImplementation::ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,const char* word,size_t init)
2139 {
2140 size_t pos = buffer.find(word,init);
2141 if(pos != buffer.npos)
2142 {
2143 this->CurrentPositionInFile = pos;
2144 pos = buffer.find(":",pos);
2145 size_t pos2 = buffer.find("\n",pos);
2146 if(pos!=buffer.npos && pos2!=buffer.npos)
2147 {
2148 return buffer.substr(pos+2,pos2-pos-2);
2149 }
2150 }
2151 this->CurrentPositionInFile = buffer.npos;
2152 return "";
2153 }
2154
2155 /** Query for the cpu status */
2156 int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile()
2157 {
2158 this->NumberOfLogicalCPU = 0;
2159 this->NumberOfPhysicalCPU = 0;
2160 kwsys_stl::string buffer;
2161
2162 FILE *fd = fopen("/proc/cpuinfo", "r" );
2163 if ( !fd )
2164 {
2165 kwsys_ios::cout << "Problem opening /proc/cpuinfo" << kwsys_ios::endl;
2166 return 0;
2167 }
2168
2169 size_t fileSize = 0;
2170 while(!feof(fd))
2171 {
2172 buffer += static_cast<unsigned char>(fgetc(fd));
2173 fileSize++;
2174 }
2175 fclose( fd );
2176 buffer.resize(fileSize-2);
2177 // Number of logical CPUs (combination of multiple processors, multi-core
2178 // and hyperthreading)
2179 size_t pos = buffer.find("processor\t");
2180 while(pos != buffer.npos)
2181 {
2182 this->NumberOfLogicalCPU++;
2183 pos = buffer.find("processor\t",pos+1);
2184 }
2185
2186 #ifdef __linux
2187 // Find the largest physical id.
2188 int maxId = -1;
2189 kwsys_stl::string idc =
2190 this->ExtractValueFromCpuInfoFile(buffer,"physical id");
2191 while(this->CurrentPositionInFile != buffer.npos)
2192 {
2193 int id = atoi(idc.c_str());
2194 if(id > maxId)
2195 {
2196 maxId=id;
2197 }
2198 idc = this->ExtractValueFromCpuInfoFile(buffer,"physical id",
2199 this->CurrentPositionInFile+1);
2200 }
2201 // Physical ids returned by Linux don't distinguish cores.
2202 // We want to record the total number of cores in this->NumberOfPhysicalCPU
2203 // (checking only the first proc)
2204 kwsys_stl::string cores =
2205 this->ExtractValueFromCpuInfoFile(buffer,"cpu cores");
2206 int numberOfCoresPerCPU=atoi(cores.c_str());
2207 this->NumberOfPhysicalCPU=numberOfCoresPerCPU*(maxId+1);
2208
2209 #else // __CYGWIN__
2210 // does not have "physical id" entries, neither "cpu cores"
2211 // this has to be fixed for hyper-threading.
2212 kwsys_stl::string cpucount =
2213 this->ExtractValueFromCpuInfoFile(buffer,"cpu count");
2214 this->NumberOfPhysicalCPU=
2215 this->NumberOfLogicalCPU = atoi(cpucount.c_str());
2216 #endif
2217 // gotta have one, and if this is 0 then we get a / by 0n
2218 // beter to have a bad answer than a crash
2219 if(this->NumberOfPhysicalCPU <= 0)
2220 {
2221 this->NumberOfPhysicalCPU = 1;
2222 }
2223 // LogicalProcessorsPerPhysical>1 => hyperthreading.
2224 this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical=
2225 this->NumberOfLogicalCPU/this->NumberOfPhysicalCPU;
2226
2227 // CPU speed (checking only the first proc
2228 kwsys_stl::string CPUSpeed = this->ExtractValueFromCpuInfoFile(buffer,"cpu MHz");
2229 this->CPUSpeedInMHz = static_cast<float>(atof(CPUSpeed.c_str()));
2230
2231 // Chip family
2232 this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str());
2233
2234 // Chip Vendor
2235 strcpy(this->ChipID.Vendor,this->ExtractValueFromCpuInfoFile(buffer,"vendor_id").c_str());
2236 this->FindManufacturer();
2237
2238 // Chip Model
2239 this->ChipID.Model = atoi(this->ExtractValueFromCpuInfoFile(buffer,"model").c_str());
2240 this->RetrieveClassicalCPUIdentity();
2241
2242 // L1 Cache size
2243 kwsys_stl::string cacheSize = this->ExtractValueFromCpuInfoFile(buffer,"cache size");
2244 pos = cacheSize.find(" KB");
2245 if(pos!=cacheSize.npos)
2246 {
2247 cacheSize = cacheSize.substr(0,pos);
2248 }
2249 this->Features.L1CacheSize = atoi(cacheSize.c_str());
2250 return 1;
2251 }
2252
2253 /** Query for the memory status */
2254 int SystemInformationImplementation::QueryMemory()
2255 {
2256 this->TotalVirtualMemory = 0;
2257 this->TotalPhysicalMemory = 0;
2258 this->AvailableVirtualMemory = 0;
2259 this->AvailablePhysicalMemory = 0;
2260 #ifdef __CYGWIN__
2261 return 0;
2262 #elif _WIN32
2263 #if _MSC_VER < 1300
2264 MEMORYSTATUS ms;
2265 GlobalMemoryStatus(&ms);
2266 #define MEM_VAL(value) dw##value
2267 #else
2268 MEMORYSTATUSEX ms;
2269 GlobalMemoryStatusEx(&ms);
2270 #define MEM_VAL(value) ull##value
2271 #endif
2272 unsigned long tv = ms.MEM_VAL(TotalVirtual);
2273 unsigned long tp = ms.MEM_VAL(TotalPhys);
2274 unsigned long av = ms.MEM_VAL(AvailVirtual);
2275 unsigned long ap = ms.MEM_VAL(AvailPhys);
2276 this->TotalVirtualMemory = tv>>10>>10;
2277 this->TotalPhysicalMemory = tp>>10>>10;
2278 this->AvailableVirtualMemory = av>>10>>10;
2279 this->AvailablePhysicalMemory = ap>>10>>10;
2280 return 1;
2281 #elif __linux
2282 unsigned long tv=0;
2283 unsigned long tp=0;
2284 unsigned long av=0;
2285 unsigned long ap=0;
2286
2287 char buffer[1024]; // for skipping unused lines
2288
2289 int linuxMajor = 0;
2290 int linuxMinor = 0;
2291
2292 // Find the Linux kernel version first
2293 struct utsname unameInfo;
2294 int errorFlag = uname(&unameInfo);
2295 if( errorFlag!=0 )
2296 {
2297 kwsys_ios::cout << "Problem calling uname(): " << strerror(errno) << kwsys_ios::endl;
2298 return 0;
2299 }
2300
2301 if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 )
2302 {
2303 // release looks like "2.6.3-15mdk-i686-up-4GB"
2304 char majorChar=unameInfo.release[0];
2305 char minorChar=unameInfo.release[2];
2306
2307 if( isdigit(majorChar) )
2308 {
2309 linuxMajor=majorChar-'0';
2310 }
2311
2312 if( isdigit(minorChar) )
2313 {
2314 linuxMinor=minorChar-'0';
2315 }
2316 }
2317
2318 FILE *fd = fopen("/proc/meminfo", "r" );
2319 if ( !fd )
2320 {
2321 kwsys_ios::cout << "Problem opening /proc/meminfo" << kwsys_ios::endl;
2322 return 0;
2323 }
2324
2325 if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) )
2326 {
2327 // new /proc/meminfo format since kernel 2.6.x
2328 // Rigorously, this test should check from the developping version 2.5.x
2329 // that introduced the new format...
2330
2331 long freeMem;
2332 long buffersMem;
2333 long cachedMem;
2334
2335 fscanf(fd,"MemTotal:%ld kB\n", &this->TotalPhysicalMemory);
2336 fscanf(fd,"MemFree:%ld kB\n", &freeMem);
2337 fscanf(fd,"Buffers:%ld kB\n", &buffersMem);
2338 fscanf(fd,"Cached:%ld kB\n", &cachedMem);
2339
2340 this->TotalPhysicalMemory /= 1024;
2341 this->AvailablePhysicalMemory = freeMem+cachedMem+buffersMem;
2342 this->AvailablePhysicalMemory /= 1024;
2343
2344 // Skip SwapCached, Active, Inactive, HighTotal, HighFree, LowTotal
2345 // and LowFree.
2346 int i=0;
2347 while(i<7)
2348 {
2349 fgets(buffer, sizeof(buffer), fd); // skip a line
2350 ++i;
2351 }
2352
2353 fscanf(fd,"SwapTotal:%ld kB\n", &this->TotalVirtualMemory);
2354 fscanf(fd,"SwapFree:%ld kB\n", &this->AvailableVirtualMemory);
2355
2356 this->TotalVirtualMemory /= 1024;
2357 this->AvailableVirtualMemory /= 1024;
2358 }
2359 else
2360 {
2361 // /proc/meminfo format for kernel older than 2.6.x
2362
2363 unsigned long temp;
2364 unsigned long cachedMem;
2365 unsigned long buffersMem;
2366 fgets(buffer, sizeof(buffer), fd); // Skip "total: used:..."
2367
2368 fscanf(fd, "Mem: %lu %lu %lu %lu %lu %lu\n",
2369 &tp, &temp, &ap, &temp, &buffersMem, &cachedMem);
2370 fscanf(fd, "Swap: %lu %lu %lu\n", &tv, &temp, &av);
2371
2372 this->TotalVirtualMemory = tv>>10>>10;
2373 this->TotalPhysicalMemory = tp>>10>>10;
2374 this->AvailableVirtualMemory = av>>10>>10;
2375 this->AvailablePhysicalMemory = (ap+buffersMem+cachedMem)>>10>>10;
2376 }
2377 fclose( fd );
2378 return 1;
2379 #elif __hpux
2380 unsigned long tv=0;
2381 unsigned long tp=0;
2382 unsigned long av=0;
2383 unsigned long ap=0;
2384 struct pst_static pst;
2385 struct pst_dynamic pdy;
2386
2387 unsigned long ps = 0;
2388 if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1)
2389 {
2390 ps = pst.page_size;
2391 tp = pst.physical_memory *ps;
2392 tv = (pst.physical_memory + pst.pst_maxmem) * ps;
2393 if (pstat_getdynamic(&pdy, sizeof(pdy), (size_t) 1, 0) != -1)
2394 {
2395 ap = tp - pdy.psd_rm * ps;
2396 av = tv - pdy.psd_vm;
2397 this->TotalVirtualMemory = tv>>10>>10;
2398 this->TotalPhysicalMemory = tp>>10>>10;
2399 this->AvailableVirtualMemory = av>>10>>10;
2400 this->AvailablePhysicalMemory = ap>>10>>10;
2401 return 1;
2402 }
2403 }
2404 return 0;
2405 #else
2406 return 0;
2407 #endif
2408
2409
2410 }
2411
2412 /** */
2413 unsigned long SystemInformationImplementation::GetTotalVirtualMemory()
2414 {
2415 return this->TotalVirtualMemory;
2416 }
2417
2418 /** */
2419 unsigned long SystemInformationImplementation::GetAvailableVirtualMemory()
2420 {
2421 return this->AvailableVirtualMemory;
2422 }
2423
2424 unsigned long SystemInformationImplementation::GetTotalPhysicalMemory()
2425 {
2426 return this->TotalPhysicalMemory;
2427 }
2428
2429 /** */
2430 unsigned long SystemInformationImplementation::GetAvailablePhysicalMemory()
2431 {
2432 return this->AvailablePhysicalMemory;
2433 }
2434
2435 /** Get Cycle differences */
2436 LongLong SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction,
2437 unsigned int uiParameter)
2438 {
2439 #if USE_ASM_INSTRUCTIONS
2440
2441 unsigned int edx1, eax1;
2442 unsigned int edx2, eax2;
2443
2444 // Calculate the frequency of the CPU instructions.
2445 __try {
2446 _asm {
2447 push uiParameter ; push parameter param
2448 mov ebx, DelayFunction ; store func in ebx
2449
2450 RDTSC_INSTRUCTION
2451
2452 mov esi, eax ; esi = eax
2453 mov edi, edx ; edi = edx
2454
2455 call ebx ; call the delay functions
2456
2457 RDTSC_INSTRUCTION
2458
2459 pop ebx
2460
2461 mov edx2, edx ; edx2 = edx
2462 mov eax2, eax ; eax2 = eax
2463
2464 mov edx1, edi ; edx2 = edi
2465 mov eax1, esi ; eax2 = esi
2466 }
2467 }
2468 __except(1)
2469 {
2470 return -1;
2471 }
2472
2473 return ((((__int64) edx2 << 32) + eax2) - (((__int64) edx1 << 32) + eax1));
2474
2475 #else
2476 (void)DelayFunction;
2477 (void)uiParameter;
2478 return -1;
2479 #endif
2480 }
2481
2482 /** Compute the delay overhead */
2483 void SystemInformationImplementation::DelayOverhead(unsigned int uiMS)
2484 {
2485 #if _WIN32
2486 LARGE_INTEGER Frequency, StartCounter, EndCounter;
2487 __int64 x;
2488
2489 // Get the frequency of the high performance counter.
2490 if(!QueryPerformanceFrequency (&Frequency))
2491 {
2492 return;
2493 }
2494 x = Frequency.QuadPart / 1000 * uiMS;
2495
2496 // Get the starting position of the counter.
2497 QueryPerformanceCounter (&StartCounter);
2498
2499 do {
2500 // Get the ending position of the counter.
2501 QueryPerformanceCounter (&EndCounter);
2502 } while (EndCounter.QuadPart - StartCounter.QuadPart == x);
2503 #endif
2504 (void)uiMS;
2505 }
2506
2507 /** Return the number of logical CPU per physical CPUs Works only for windows */
2508 unsigned char SystemInformationImplementation::LogicalCPUPerPhysicalCPU(void)
2509 {
2510 unsigned int Regebx = 0;
2511 #if USE_ASM_INSTRUCTIONS
2512 if (!this->IsHyperThreadingSupported())
2513 {
2514 return static_cast<unsigned char>(1); // HT not supported
2515 }
2516 __asm
2517 {
2518 mov eax, 1
2519 cpuid
2520 mov Regebx, ebx
2521 }
2522 #endif
2523 return static_cast<unsigned char> ((Regebx & NUM_LOGICAL_BITS) >> 16);
2524 }
2525
2526 /** Works only for windows */
2527 unsigned int SystemInformationImplementation::IsHyperThreadingSupported()
2528 {
2529 #if USE_ASM_INSTRUCTIONS
2530 unsigned int Regedx = 0,
2531 Regeax = 0,
2532 VendorId[3] = {0, 0, 0};
2533 __try // Verify cpuid instruction is supported
2534 {
2535 __asm
2536 {
2537 xor eax, eax // call cpuid with eax = 0
2538 cpuid // Get vendor id string
2539 mov VendorId, ebx
2540 mov VendorId + 4, edx
2541 mov VendorId + 8, ecx
2542
2543 mov eax, 1 // call cpuid with eax = 1
2544 cpuid
2545 mov Regeax, eax // eax contains family processor type
2546 mov Regedx, edx // edx has info about the availability of hyper-Threading
2547 }
2548 }
2549 __except (EXCEPTION_EXECUTE_HANDLER)
2550 {
2551 return(0); // cpuid is unavailable
2552 }
2553
2554 if (((Regeax & FAMILY_ID) == PENTIUM4_ID) || (Regeax & EXT_FAMILY_ID))
2555 {
2556 if (VendorId[0] == 'uneG')
2557 {
2558 if (VendorId[1] == 'Ieni')
2559 {
2560 if (VendorId[2] == 'letn')
2561 {
2562 return(Regedx & HT_BIT); // Genuine Intel with hyper-Threading technology
2563 }
2564 }
2565 }
2566 }
2567 #endif
2568
2569 return 0; // Not genuine Intel processor
2570 }
2571
2572 /** Return the APIC Id. Works only for windows. */
2573 unsigned char SystemInformationImplementation::GetAPICId()
2574 {
2575 unsigned int Regebx = 0;
2576 #if USE_ASM_INSTRUCTIONS
2577 if (!this->IsHyperThreadingSupported())
2578 {
2579 return static_cast<unsigned char>(-1); // HT not supported
2580 } // Logical processor = 1
2581 __asm
2582 {
2583 mov eax, 1
2584 cpuid
2585 mov Regebx, ebx
2586 }
2587 #endif
2588 return static_cast<unsigned char>((Regebx & INITIAL_APIC_ID_BITS) >> 24);
2589 }
2590
2591 /** Count the number of CPUs. Works only on windows. */
2592 int SystemInformationImplementation::CPUCount()
2593 {
2594 #if _WIN32
2595 unsigned char StatusFlag = 0;
2596 SYSTEM_INFO info;
2597
2598 this->NumberOfPhysicalCPU = 0;
2599 this->NumberOfLogicalCPU = 0;
2600 info.dwNumberOfProcessors = 0;
2601 GetSystemInfo (&info);
2602
2603 // Number of physical processors in a non-Intel system
2604 // or in a 32-bit Intel system with Hyper-Threading technology disabled
2605 this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors;
2606
2607 if (this->IsHyperThreadingSupported())
2608 {
2609 unsigned char HT_Enabled = 0;
2610 this->NumberOfLogicalCPU = this->LogicalCPUPerPhysicalCPU();
2611 if (this->NumberOfLogicalCPU >= 1) // >1 Doesn't mean HT is enabled in the BIOS
2612 {
2613 HANDLE hCurrentProcessHandle;
2614 #ifndef _WIN64
2615 # define DWORD_PTR DWORD
2616 #endif
2617 DWORD_PTR dwProcessAffinity;
2618 DWORD_PTR dwSystemAffinity;
2619 DWORD dwAffinityMask;
2620
2621 // Calculate the appropriate shifts and mask based on the
2622 // number of logical processors.
2623 unsigned int i = 1;
2624 unsigned char PHY_ID_MASK = 0xFF;
2625 //unsigned char PHY_ID_SHIFT = 0;
2626
2627 while (i < this->NumberOfLogicalCPU)
2628 {
2629 i *= 2;
2630 PHY_ID_MASK <<= 1;
2631 // PHY_ID_SHIFT++;
2632 }
2633
2634 hCurrentProcessHandle = GetCurrentProcess();
2635 GetProcessAffinityMask(hCurrentProcessHandle, &dwProcessAffinity,
2636 &dwSystemAffinity);
2637
2638 // Check if available process affinity mask is equal to the
2639 // available system affinity mask
2640 if (dwProcessAffinity != dwSystemAffinity)
2641 {
2642 StatusFlag = HT_CANNOT_DETECT;
2643 this->NumberOfPhysicalCPU = (unsigned char)-1;
2644 return StatusFlag;
2645 }
2646
2647 dwAffinityMask = 1;
2648 while (dwAffinityMask != 0 && dwAffinityMask <= dwProcessAffinity)
2649 {
2650 // Check if this CPU is available
2651 if (dwAffinityMask & dwProcessAffinity)
2652 {
2653 if (SetProcessAffinityMask(hCurrentProcessHandle,
2654 dwAffinityMask))
2655 {
2656 unsigned char APIC_ID, LOG_ID;
2657 Sleep(0); // Give OS time to switch CPU
2658
2659 APIC_ID = GetAPICId();
2660 LOG_ID = APIC_ID & ~PHY_ID_MASK;
2661
2662 if (LOG_ID != 0)
2663 {
2664 HT_Enabled = 1;
2665 }
2666 }
2667 }
2668 dwAffinityMask = dwAffinityMask << 1;
2669 }
2670 // Reset the processor affinity
2671 SetProcessAffinityMask(hCurrentProcessHandle, dwProcessAffinity);
2672
2673 if (this->NumberOfLogicalCPU == 1) // Normal P4 : HT is disabled in hardware
2674 {
2675 StatusFlag = HT_DISABLED;
2676 }
2677 else
2678 {
2679 if (HT_Enabled)
2680 {
2681 // Total physical processors in a Hyper-Threading enabled system.
2682 this->NumberOfPhysicalCPU /= (this->NumberOfLogicalCPU);
2683 StatusFlag = HT_ENABLED;
2684 }
2685 else
2686 {
2687 StatusFlag = HT_SUPPORTED_NOT_ENABLED;
2688 }
2689 }
2690 }
2691 }
2692 else
2693 {
2694 // Processors do not have Hyper-Threading technology
2695 StatusFlag = HT_NOT_CAPABLE;
2696 this->NumberOfLogicalCPU = 1;
2697 }
2698 return StatusFlag;
2699 #else
2700 return 0;
2701 #endif
2702 }
2703
2704 /** Return the number of logical CPUs on the system */
2705 unsigned int SystemInformationImplementation::GetNumberOfLogicalCPU()
2706 {
2707 return this->NumberOfLogicalCPU;
2708 }
2709
2710 /** Return the number of physical CPUs on the system */
2711 unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU()
2712 {
2713 return this->NumberOfPhysicalCPU;
2714 }
2715
2716 /** For Mac we Parse the sysctl -a output */
2717 bool SystemInformationImplementation::ParseSysCtl()
2718 {
2719 // Extract the arguments from the command line
2720 kwsys_stl::vector<const char*> args;
2721 args.push_back("sysctl");
2722 args.push_back("-a");
2723 args.push_back(0);
2724
2725 this->SysCtlBuffer = this->RunProcess(args);
2726
2727 // Parse values for Mac
2728 this->TotalPhysicalMemory = atoi(this->ExtractValueFromSysCtl("hw.memsize:").c_str())/(1024*1024);
2729 this->TotalVirtualMemory = 0;
2730 this->AvailablePhysicalMemory = 0;
2731 this->AvailableVirtualMemory = 0;
2732
2733 this->NumberOfPhysicalCPU = atoi(this->ExtractValueFromSysCtl("hw.physicalcpu:").c_str());
2734 this->NumberOfLogicalCPU = atoi(this->ExtractValueFromSysCtl("hw.logicalcpu:").c_str());
2735
2736 if(this->NumberOfPhysicalCPU!=0)
2737 {
2738 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2739 }
2740
2741 this->CPUSpeedInMHz = static_cast<float>(atoi(this->ExtractValueFromSysCtl("hw.cpufrequency:").c_str()));
2742 this->CPUSpeedInMHz /= 1000000;
2743
2744 // Chip family
2745 this->ChipID.Family = atoi(this->ExtractValueFromSysCtl("machdep.cpu.family:").c_str());
2746
2747 // Chip Vendor
2748 strcpy(this->ChipID.Vendor,this->ExtractValueFromSysCtl("machdep.cpu.vendor:").c_str());
2749 this->FindManufacturer();
2750
2751 // Chip Model
2752 this->ChipID.Model = atoi(this->ExtractValueFromSysCtl("machdep.cpu.model:").c_str());
2753 this->RetrieveClassicalCPUIdentity();
2754
2755 // Cache size
2756 this->Features.L1CacheSize = atoi(this->ExtractValueFromSysCtl("hw.l1icachesize:").c_str());
2757 this->Features.L2CacheSize = atoi(this->ExtractValueFromSysCtl("hw.l2cachesize:").c_str());
2758
2759 return true;
2760 }
2761
2762 /** Extract a value from sysctl command */
2763 kwsys_stl::string SystemInformationImplementation::ExtractValueFromSysCtl(const char* word)
2764 {
2765 size_t pos = this->SysCtlBuffer.find(word);
2766 if(pos != this->SysCtlBuffer.npos)
2767 {
2768 pos = this->SysCtlBuffer.find(": ",pos);
2769 size_t pos2 = this->SysCtlBuffer.find("\n",pos);
2770 if(pos!=this->SysCtlBuffer.npos && pos2!=this->SysCtlBuffer.npos)
2771 {
2772 return this->SysCtlBuffer.substr(pos+2,pos2-pos-2);
2773 }
2774 }
2775 return "";
2776 }
2777
2778 /** Run a given process */
2779 kwsys_stl::string SystemInformationImplementation::RunProcess(kwsys_stl::vector<const char*> args)
2780 {
2781 kwsys_stl::string buffer = "";
2782
2783 // Run the application
2784 kwsysProcess* gp = kwsysProcess_New();
2785 kwsysProcess_SetCommand(gp, &*args.begin());
2786 kwsysProcess_SetOption(gp,kwsysProcess_Option_HideWindow,1);
2787
2788 kwsysProcess_Execute(gp);
2789
2790 char* data = NULL;
2791 int length;
2792 double timeout = 255;
2793
2794 while(kwsysProcess_WaitForData(gp,&data,&length,&timeout)) // wait for 1s
2795 {
2796 for(int i=0;i<length;i++)
2797 {
2798 buffer += data[i];
2799 }
2800 }
2801 kwsysProcess_WaitForExit(gp, 0);
2802
2803 int result = 0;
2804 switch(kwsysProcess_GetState(gp))
2805 {
2806 case kwsysProcess_State_Exited:
2807 {
2808 result = kwsysProcess_GetExitValue(gp);
2809 } break;
2810 case kwsysProcess_State_Error:
2811 {
2812 kwsys_ios::cerr << "Error: Could not run " << args[0] << ":\n";
2813 kwsys_ios::cerr << kwsysProcess_GetErrorString(gp) << "\n";
2814 } break;
2815 case kwsysProcess_State_Exception:
2816 {
2817 kwsys_ios::cerr << "Error: " << args[0]
2818 << " terminated with an exception: "
2819 << kwsysProcess_GetExceptionString(gp) << "\n";
2820 } break;
2821 case kwsysProcess_State_Starting:
2822 case kwsysProcess_State_Executing:
2823 case kwsysProcess_State_Expired:
2824 case kwsysProcess_State_Killed:
2825 {
2826 // Should not get here.
2827 kwsys_ios::cerr << "Unexpected ending state after running " << args[0]
2828 << kwsys_ios::endl;
2829 } break;
2830 }
2831 kwsysProcess_Delete(gp);
2832 if(result)
2833 {
2834 kwsys_ios::cerr << "Error " << args[0] << " returned :" << result << "\n";
2835 }
2836 return buffer;
2837 }
2838
2839
2840 kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const char* arguments)
2841 {
2842 kwsys_stl::vector<const char*> args;
2843 args.clear();
2844 args.push_back("kstat");
2845 args.push_back("-p");
2846
2847 kwsys_stl::string command = arguments;
2848 size_t start = command.npos;
2849 size_t pos = command.find(' ',0);
2850 while(pos!=command.npos)
2851 {
2852 bool inQuotes = false;
2853 // Check if we are between quotes
2854 size_t b0 = command.find('"',0);
2855 size_t b1 = command.find('"',b0+1);
2856 while(b0 != command.npos && b1 != command.npos && b1>b0)
2857 {
2858 if(pos>b0 && pos<b1)
2859 {
2860 inQuotes = true;
2861 break;
2862 }
2863 b0 = command.find('"',b1+1);
2864 b1 = command.find('"',b0+1);
2865 }
2866
2867 if(!inQuotes)
2868 {
2869 kwsys_stl::string arg = command.substr(start+1,pos-start-1);
2870
2871 // Remove the quotes if any
2872 size_t quotes = arg.find('"');
2873 while(quotes != arg.npos)
2874 {
2875 arg.erase(quotes,1);
2876 quotes = arg.find('"');
2877 }
2878 args.push_back(arg.c_str());
2879 start = pos;
2880 }
2881 pos = command.find(' ',pos+1);
2882 }
2883 kwsys_stl::string lastArg = command.substr(start+1,command.size()-start-1);
2884 args.push_back(lastArg.c_str());
2885
2886 args.push_back(0);
2887
2888 kwsys_stl::string buffer = this->RunProcess(args);
2889
2890 kwsys_stl::string value = "";
2891 for(size_t i=buffer.size()-1;i>0;i--)
2892 {
2893 if(buffer[i] == ' ' || buffer[i] == '\t')
2894 {
2895 break;
2896 }
2897 if(buffer[i] != '\n' && buffer[i] != '\r')
2898 {
2899 kwsys_stl::string val = value;
2900 value = buffer[i];
2901 value += val;
2902 }
2903 }
2904 return value;
2905 }
2906
2907 /** Querying for system information from Solaris */
2908 bool SystemInformationImplementation::QuerySolarisInfo()
2909 {
2910 // Parse values
2911 this->NumberOfPhysicalCPU = atoi(this->ParseValueFromKStat("-n syste_misc -s ncpus").c_str());
2912 this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU;
2913
2914 if(this->NumberOfPhysicalCPU!=0)
2915 {
2916 this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU;
2917 }
2918
2919 this->CPUSpeedInMHz = static_cast<float>(atoi(this->ParseValueFromKStat("-s clock_MHz").c_str()));
2920
2921 // Chip family
2922 this->ChipID.Family = 0;
2923
2924 // Chip Vendor
2925 strcpy(this->ChipID.Vendor,"Sun");
2926 this->FindManufacturer();
2927
2928 // Chip Model
2929 sprintf(this->ChipID.ProcessorName,"%s",this->ParseValueFromKStat("-s cpu_type").c_str());
2930 this->ChipID.Model = 0;
2931
2932 // Cache size
2933 this->Features.L1CacheSize = 0;
2934 this->Features.L2CacheSize = 0;
2935
2936 char* tail;
2937 unsigned long totalMemory =
2938 strtoul(this->ParseValueFromKStat("-s physmem").c_str(),&tail,0);
2939 this->TotalPhysicalMemory = totalMemory/1024;
2940 this->TotalPhysicalMemory *= 8192;
2941 this->TotalPhysicalMemory /= 1024;
2942
2943 // Undefined values (for now at least)
2944 this->TotalVirtualMemory = 0;
2945 this->AvailablePhysicalMemory = 0;
2946 this->AvailableVirtualMemory = 0;
2947
2948 return true;
2949 }
2950
2951 /** Querying for system information from Haiku OS */
2952 bool SystemInformationImplementation::QueryHaikuInfo()
2953 {
2954 #if defined(__HAIKU__)
2955
2956 system_info info;
2957 get_system_info(&info);
2958
2959 this->NumberOfPhysicalCPU = info.cpu_count;
2960 this->CPUSpeedInMHz = info.cpu_clock_speed / 1000000.0F;
2961
2962 // Physical Memory
2963 this->TotalPhysicalMemory = (info.max_pages * B_PAGE_SIZE) / (1024 * 1024) ;
2964 this->AvailablePhysicalMemory = this->TotalPhysicalMemory -
2965 ((info.used_pages * B_PAGE_SIZE) / (1024 * 1024));
2966
2967
2968 // NOTE: get_system_info_etc is currently a private call so just set to 0
2969 // until it becomes public
2970 this->TotalVirtualMemory = 0;
2971 this->AvailableVirtualMemory = 0;
2972
2973 // Retrieve cpuid_info union for cpu 0
2974 cpuid_info cpu_info;
2975 get_cpuid(&cpu_info, 0, 0);
2976
2977 // Chip Vendor
2978 // Use a temporary buffer so that we can add NULL termination to the string
2979 char vbuf[13];
2980 strncpy(vbuf, cpu_info.eax_0.vendor_id, 12);
2981 vbuf[12] = '\0';
2982 strcpy(this->ChipID.Vendor,vbuf);
2983
2984 this->FindManufacturer();
2985
2986 // Retrieve cpuid_info union for cpu 0 this time using a register value of 1
2987 get_cpuid(&cpu_info, 1, 0);
2988
2989 this->NumberOfLogicalCPU = cpu_info.eax_1.logical_cpus;
2990
2991 // Chip type
2992 this->ChipID.Type = cpu_info.eax_1.type;
2993
2994 // Chip family
2995 this->ChipID.Family = cpu_info.eax_1.family;
2996
2997 // Chip Model
2998 this->ChipID.Model = cpu_info.eax_1.model;
2999
3000 // Chip Revision
3001 this->ChipID.Revision = cpu_info.eax_1.stepping;
3002
3003 // Chip Extended Family
3004 this->ChipID.ExtendedFamily = cpu_info.eax_1.extended_family;
3005
3006 // Chip Extended Model
3007 this->ChipID.ExtendedModel = cpu_info.eax_1.extended_model;
3008
3009 // Get ChipID.ProcessorName from other information already gathered
3010 this->RetrieveClassicalCPUIdentity();
3011
3012 // Cache size
3013 this->Features.L1CacheSize = 0;
3014 this->Features.L2CacheSize = 0;
3015
3016 #endif
3017 return true;
3018 }
3019
3020 /** Query the operating system information */
3021 bool SystemInformationImplementation::QueryOSInformation()
3022 {
3023 #if _WIN32
3024
3025 this->OSName = "Windows";
3026
3027 OSVERSIONINFOEX osvi;
3028 BOOL bIsWindows64Bit;
3029 BOOL bOsVersionInfoEx;
3030 char operatingSystem[256];
3031
3032 // Try calling GetVersionEx using the OSVERSIONINFOEX structure.
3033 ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX));
3034 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
3035 bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi);
3036 if (!bOsVersionInfoEx)
3037 {
3038 osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
3039 if (!GetVersionEx ((OSVERSIONINFO *) &osvi))
3040 {
3041 return false;
3042 }
3043 }
3044
3045 switch (osvi.dwPlatformId)
3046 {
3047 case VER_PLATFORM_WIN32_NT:
3048 // Test for the product.
3049 if (osvi.dwMajorVersion <= 4)
3050 {
3051 this->OSRelease = "NT";
3052 }
3053 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
3054 {
3055 this->OSRelease = "2000";
3056 }
3057 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3058 {
3059 this->OSRelease = "XP";
3060 }
3061 #ifdef VER_NT_WORKSTATION
3062 // Test for product type.
3063 if (bOsVersionInfoEx)
3064 {
3065 if (osvi.wProductType == VER_NT_WORKSTATION)
3066 {
3067 if (osvi.dwMajorVersion == 6)
3068 {
3069 this->OSRelease = "Vista";
3070 }
3071 // VER_SUITE_PERSONAL may not be defined
3072 #ifdef VER_SUITE_PERSONAL
3073 else
3074 {
3075 if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
3076 {
3077 this->OSRelease += " Personal";
3078 }
3079 else
3080 {
3081 this->OSRelease += " Professional";
3082 }
3083 }
3084 #endif
3085 }
3086 else if (osvi.wProductType == VER_NT_SERVER)
3087 {
3088 // Check for .NET Server instead of Windows XP.
3089 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3090 {
3091 this->OSRelease = ".NET";
3092 }
3093
3094 // Continue with the type detection.
3095 if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
3096 {
3097 this->OSRelease += " DataCenter Server";
3098 }
3099 else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
3100 {
3101 this->OSRelease += " Advanced Server";
3102 }
3103 else
3104 {
3105 this->OSRelease += " Server";
3106 }
3107 }
3108
3109 sprintf (operatingSystem, "%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
3110 this->OSVersion = operatingSystem;
3111 }
3112 else
3113 #endif // VER_NT_WORKSTATION
3114 {
3115 HKEY hKey;
3116 char szProductType[80];
3117 DWORD dwBufLen;
3118
3119 // Query the registry to retrieve information.
3120 RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey);
3121 RegQueryValueEx (hKey, "ProductType", NULL, NULL, (LPBYTE) szProductType, &dwBufLen);
3122 RegCloseKey (hKey);
3123
3124 if (lstrcmpi ("WINNT", szProductType) == 0)
3125 {
3126 this->OSRelease += " Professional";
3127 }
3128 if (lstrcmpi ("LANMANNT", szProductType) == 0)
3129 {
3130 // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
3131 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3132 {
3133 this->OSRelease += " Standard Server";
3134 }
3135 else
3136 {
3137 this->OSRelease += " Server";
3138 }
3139 }
3140 if (lstrcmpi ("SERVERNT", szProductType) == 0)
3141 {
3142 // Decide between Windows 2000 Advanced Server and Windows .NET Enterprise Server.
3143 if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3144 {
3145 this->OSRelease += " Enterprise Server";
3146 }
3147 else
3148 {
3149 this->OSRelease += " Advanced Server";
3150 }
3151 }
3152 }
3153
3154 // Display version, service pack (if any), and build number.
3155 if (osvi.dwMajorVersion <= 4)
3156 {
3157 // NB: NT 4.0 and earlier.
3158 sprintf (operatingSystem, "version %ld.%ld %s (Build %ld)",
3159 osvi.dwMajorVersion,
3160 osvi.dwMinorVersion,
3161 osvi.szCSDVersion,
3162 osvi.dwBuildNumber & 0xFFFF);
3163 this->OSVersion = operatingSystem;
3164 }
3165 else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
3166 {
3167 // Windows XP and .NET server.
3168 typedef BOOL (CALLBACK* LPFNPROC) (HANDLE, BOOL *);
3169 HINSTANCE hKernelDLL;
3170 LPFNPROC DLLProc;
3171
3172 // Load the Kernel32 DLL.
3173 hKernelDLL = LoadLibrary ("kernel32");
3174 if (hKernelDLL != NULL) {
3175 // Only XP and .NET Server support IsWOW64Process so... Load dynamically!
3176 DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process");
3177
3178 // If the function address is valid, call the function.
3179 if (DLLProc != NULL) (DLLProc) (GetCurrentProcess (), &bIsWindows64Bit);
3180 else bIsWindows64Bit = false;
3181
3182 // Free the DLL module.
3183 FreeLibrary (hKernelDLL);
3184 }
3185 }
3186 else
3187 {
3188 // Windows 2000 and everything else.
3189 sprintf (operatingSystem,"%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF);
3190 this->OSVersion = operatingSystem;
3191 }
3192 break;
3193
3194 case VER_PLATFORM_WIN32_WINDOWS:
3195 // Test for the product.
3196 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
3197 {
3198 this->OSRelease = "95";
3199 if(osvi.szCSDVersion[1] == 'C')
3200 {
3201 this->OSRelease += "OSR 2.5";
3202 }
3203 else if(osvi.szCSDVersion[1] == 'B')
3204 {
3205 this->OSRelease += "OSR 2";
3206 }
3207 }
3208
3209 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
3210 {
3211 this->OSRelease = "98";
3212 if (osvi.szCSDVersion[1] == 'A' )
3213 {
3214 this->OSRelease += "SE";
3215 }
3216 }
3217
3218 if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
3219 {
3220 this->OSRelease = "Me";
3221 }
3222 break;
3223
3224 case VER_PLATFORM_WIN32s:
3225 this->OSRelease = "Win32s";
3226 break;
3227
3228 default:
3229 this->OSRelease = "Unknown";
3230 break;
3231 }
3232
3233 // Get the hostname
3234 WORD wVersionRequested;
3235 WSADATA wsaData;
3236 char name[255];
3237 wVersionRequested = MAKEWORD(2,0);
3238
3239 if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
3240 {
3241 gethostname(name,sizeof(name));
3242 WSACleanup( );
3243 }
3244 this->Hostname = name;
3245
3246 #else
3247
3248 struct utsname unameInfo;
3249 int errorFlag = uname(&unameInfo);
3250 if(errorFlag == 0)
3251 {
3252 this->OSName = unameInfo.sysname;
3253 this->Hostname = unameInfo.nodename;
3254 this->OSRelease = unameInfo.release;
3255 this->OSVersion = unameInfo.version;
3256 this->OSPlatform = unameInfo.machine;
3257 }
3258 #endif
3259
3260 return true;
3261
3262 }
3263
3264 /** Return true if the machine is 64 bits */
3265 bool SystemInformationImplementation::Is64Bits()
3266 {
3267 return (sizeof(void*) == 8);
3268 }
3269
3270 } // namespace @KWSYS_NAMESPACE@

  ViewVC Help
Powered by ViewVC 1.1.5