SystemInformation.cxx 145 KB
Newer Older
Kitware Robot's avatar
Kitware Robot committed
1
2
3
4
5
6
7
8
9
10
11
/*============================================================================
  KWSys - Kitware System Library
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
Kitware Robot's avatar
Kitware Robot committed
12
13
14

#if defined(_WIN32)
# define NOMINMAX // use our min,max
Kitware Robot's avatar
Kitware Robot committed
15
16
17
# if !defined(_WIN32_WINNT) && !(defined(_MSC_VER) && _MSC_VER < 1300)
#  define _WIN32_WINNT 0x0501
# endif
Kitware Robot's avatar
Kitware Robot committed
18
19
20
# include <winsock.h> // WSADATA, include before sys/types.h
#endif

Kitware Robot's avatar
Kitware Robot committed
21
22
23
24
#if (defined(__GNUC__) || defined(__PGI)) && !defined(_GNU_SOURCE)
# define _GNU_SOURCE
#endif

Kitware Robot's avatar
Kitware Robot committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// TODO:
// We need an alternative implementation for many functions in this file
// when USE_ASM_INSTRUCTIONS gets defined as 0.
//
// Consider using these on Win32/Win64 for some of them:
//
// IsProcessorFeaturePresent
// http://msdn.microsoft.com/en-us/library/ms724482(VS.85).aspx
//
// GetProcessMemoryInfo
// http://msdn.microsoft.com/en-us/library/ms683219(VS.85).aspx

#include "kwsysPrivate.h"
#include KWSYS_HEADER(stl/string)
#include KWSYS_HEADER(stl/vector)
#include KWSYS_HEADER(ios/iosfwd)
#include KWSYS_HEADER(SystemInformation.hxx)
#include KWSYS_HEADER(Process.h)
#include KWSYS_HEADER(ios/iostream)
#include KWSYS_HEADER(ios/sstream)
Kitware Robot's avatar
Kitware Robot committed
45
#include KWSYS_HEADER(ios/fstream)
Kitware Robot's avatar
Kitware Robot committed
46
47
48
49
50
51
52
53
54
55
56
57

// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
# include "SystemInformation.hxx.in"
# include "Process.h.in"
# include "Configure.hxx.in"
# include "kwsys_stl.hxx.in"
# include "kwsys_stl_vector.in"
# include "kwsys_stl_iosfwd.in"
# include "kwsys_ios_sstream.h.in"
# include "kwsys_ios_iostream.h.in"
Kitware Robot's avatar
Kitware Robot committed
58
# include "kwsys_ios_fstream.h.in"
Kitware Robot's avatar
Kitware Robot committed
59
60
#endif

Kitware Robot's avatar
Kitware Robot committed
61
#if defined(_WIN32)
Kitware Robot's avatar
Kitware Robot committed
62
# include <windows.h>
Kitware Robot's avatar
Kitware Robot committed
63
# include <errno.h>
Kitware Robot's avatar
Kitware Robot committed
64
65
66
67
68
69
70
71
72
73
# if defined(KWSYS_SYS_HAS_PSAPI)
#  include <psapi.h>
# endif
# if !defined(siginfo_t)
typedef int siginfo_t;
# endif
#else
# include <sys/types.h>
# include <sys/time.h>
# include <sys/utsname.h> // int uname(struct utsname *buf);
Kitware Robot's avatar
Kitware Robot committed
74
# include <sys/resource.h> // getrlimit
Kitware Robot's avatar
Kitware Robot committed
75
76
77
78
# include <unistd.h>
# include <signal.h>
# include <fcntl.h>
# include <errno.h> // extern int errno;
Kitware Robot's avatar
Kitware Robot committed
79
80
#endif

Kitware Robot's avatar
Kitware Robot committed
81
82
83
84
85
86
87
88
89
90
91
92
#ifdef __FreeBSD__
# include <sys/sysctl.h>
# include <fenv.h>
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
# if defined(KWSYS_SYS_HAS_IFADDRS_H)
#  include <ifaddrs.h>
#  define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
# endif
#endif

Kitware Robot's avatar
Kitware Robot committed
93
94
95
96
97
98
99
100
101
102
103
104
105
#if defined(__OpenBSD__) || defined(__NetBSD__)
# include <sys/param.h>
# include <sys/sysctl.h>
#endif

#if defined(KWSYS_SYS_HAS_MACHINE_CPU_H)
# include <machine/cpu.h>
#endif

#if defined(__DragonFly__)
# include <sys/sysctl.h>
#endif

Kitware Robot's avatar
Kitware Robot committed
106
#ifdef __APPLE__
Kitware Robot's avatar
Kitware Robot committed
107
108
109
110
111
112
113
114
115
# include <sys/sysctl.h>
# include <mach/vm_statistics.h>
# include <mach/host_info.h>
# include <mach/mach.h>
# include <mach/mach_types.h>
# include <fenv.h>
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
Kitware Robot's avatar
Kitware Robot committed
116
117
118
119
# if defined(KWSYS_SYS_HAS_IFADDRS_H)
#  include <ifaddrs.h>
#  define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
# endif
Kitware Robot's avatar
Kitware Robot committed
120
121
# if !(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__-0 >= 1050)
#  undef KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE
Kitware Robot's avatar
Kitware Robot committed
122
# endif
Kitware Robot's avatar
Kitware Robot committed
123
124
125
#endif

#ifdef __linux
Kitware Robot's avatar
Kitware Robot committed
126
127
128
129
# include <fenv.h>
# include <sys/socket.h>
# include <netdb.h>
# include <netinet/in.h>
Kitware Robot's avatar
Kitware Robot committed
130
131
132
133
134
135
# if defined(KWSYS_SYS_HAS_IFADDRS_H)
#  include <ifaddrs.h>
#  if !defined(__LSB_VERSION__) /* LSB has no getifaddrs */
#   define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN
#  endif
# endif
Kitware Robot's avatar
Kitware Robot committed
136
137
138
139
140
141
142
# if defined(KWSYS_CXX_HAS_RLIMIT64)
typedef struct rlimit64 ResourceLimitType;
#  define GetResourceLimit getrlimit64
# else
typedef struct rlimit ResourceLimitType;
#  define GetResourceLimit getrlimit
# endif
Kitware Robot's avatar
Kitware Robot committed
143
144
145
#elif defined( __hpux )
# include <sys/param.h>
# include <sys/pstat.h>
Kitware Robot's avatar
Kitware Robot committed
146
147
148
# if defined(KWSYS_SYS_HAS_MPCTL_H)
#  include <sys/mpctl.h>
# endif
Kitware Robot's avatar
Kitware Robot committed
149
150
151
#endif

#ifdef __HAIKU__
Kitware Robot's avatar
Kitware Robot committed
152
# include <OS.h>
Kitware Robot's avatar
Kitware Robot committed
153
154
#endif

Kitware Robot's avatar
Kitware Robot committed
155
156
157
158
159
160
161
162
163
164
165
166
167
#if defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
# include <execinfo.h>
# if defined(KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE)
#  include <cxxabi.h>
# endif
# if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
#  include <dlfcn.h>
# endif
#else
# undef KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE
# undef KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP
#endif

Kitware Robot's avatar
Kitware Robot committed
168
169
170
171
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Kitware Robot's avatar
Kitware Robot committed
172
#include <ctype.h> // int isdigit(int c);
Kitware Robot's avatar
Kitware Robot committed
173

Kitware Robot's avatar
Kitware Robot committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#if defined(KWSYS_USE_LONG_LONG)
# if defined(KWSYS_IOS_HAS_OSTREAM_LONG_LONG)
#  define iostreamLongLong(x) (x)
# else
#  define iostreamLongLong(x) ((long)x)
# endif
#elif defined(KWSYS_USE___INT64)
# if defined(KWSYS_IOS_HAS_OSTREAM___INT64)
#  define iostreamLongLong(x) (x)
# else
#  define iostreamLongLong(x) ((long)x)
# endif
#else
# error "No Long Long"
#endif

#if defined(KWSYS_CXX_HAS_ATOLL)
# define atoLongLong atoll
#else
# if defined(KWSYS_CXX_HAS__ATOI64)
#  define atoLongLong _atoi64
# elif defined(KWSYS_CXX_HAS_ATOL)
#  define atoLongLong atol
# else
#  define atoLongLong atoi
# endif
#endif

Kitware Robot's avatar
Kitware Robot committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64)
#define USE_ASM_INSTRUCTIONS 1
#else
#define USE_ASM_INSTRUCTIONS 0
#endif

#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#include <intrin.h>
#define USE_CPUID_INTRINSICS 1
#else
#define USE_CPUID_INTRINSICS 0
#endif

#if USE_ASM_INSTRUCTIONS || USE_CPUID_INTRINSICS || defined(KWSYS_CXX_HAS_BORLAND_ASM_CPUID)
# define USE_CPUID 1
#else
# define USE_CPUID 0
#endif

#if USE_CPUID

#define CPUID_AWARE_COMPILER

/**
 * call CPUID instruction
 *
 * Will return false if the instruction failed.
 */
static bool call_cpuid(int select, int result[4])
{
#if USE_CPUID_INTRINSICS
  __cpuid(result, select);
  return true;
#else
  int tmp[4];
#if defined(_MSC_VER)
  // Use SEH to determine CPUID presence
  __try {
    _asm {
#ifdef CPUID_AWARE_COMPILER
      ; we must push/pop the registers <<CPUID>> writes to, as the
Kitware Robot's avatar
Kitware Robot committed
243
      ; optimiser does not know about <<CPUID>>, and so does not expect
Kitware Robot's avatar
Kitware Robot committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
      ; these registers to change.
      push eax
      push ebx
      push ecx
      push edx
#endif
      ; <<CPUID>>
      mov eax, select
#ifdef CPUID_AWARE_COMPILER
      cpuid
#else
      _asm _emit 0x0f
      _asm _emit 0xa2
#endif
      mov tmp[0 * TYPE int], eax
      mov tmp[1 * TYPE int], ebx
      mov tmp[2 * TYPE int], ecx
      mov tmp[3 * TYPE int], edx

#ifdef CPUID_AWARE_COMPILER
      pop edx
      pop ecx
      pop ebx
      pop eax
#endif
      }
    }
  __except(1)
    {
    return false;
    }

    memcpy(result, tmp, sizeof(tmp));
#elif defined(KWSYS_CXX_HAS_BORLAND_ASM_CPUID)
  unsigned int a, b, c, d;
  __asm {
    mov EAX, select;
    cpuid
    mov a, EAX;
    mov b, EBX;
    mov c, ECX;
    mov d, EDX;
  }

  result[0] = a;
  result[1] = b;
  result[2] = c;
  result[3] = d;
#endif

  // The cpuid instruction succeeded.
  return true;
#endif
}
#endif


Kitware Robot's avatar
Kitware Robot committed
301
302
namespace KWSYS_NAMESPACE
{
Kitware Robot's avatar
Kitware Robot committed
303
304
template<typename T>
T min(T a, T b){ return a<b ? a : b; }
Kitware Robot's avatar
Kitware Robot committed
305

Kitware Robot's avatar
Kitware Robot committed
306
extern "C" { typedef void (*SigAction)(int,siginfo_t*,void*); }
Kitware Robot's avatar
Kitware Robot committed
307
308
309
310
311
312
313

//  Define SystemInformationImplementation class
typedef  void (*DELAY_FUNC)(unsigned int uiMS);

class SystemInformationImplementation
{
public:
Kitware Robot's avatar
Kitware Robot committed
314
  typedef SystemInformation::LongLong LongLong;
Kitware Robot's avatar
Kitware Robot committed
315
316
317
318
319
320
321
322
  SystemInformationImplementation ();
  ~SystemInformationImplementation ();

  const char * GetVendorString();
  const char * GetVendorID();
  kwsys_stl::string GetTypeID();
  kwsys_stl::string GetFamilyID();
  kwsys_stl::string GetModelID();
Kitware Robot's avatar
Kitware Robot committed
323
  kwsys_stl::string GetModelName();
Kitware Robot's avatar
Kitware Robot committed
324
325
326
327
328
329
330
331
332
  kwsys_stl::string GetSteppingCode();
  const char * GetExtendedProcessorName();
  const char * GetProcessorSerialNumber();
  int GetProcessorCacheSize();
  unsigned int GetLogicalProcessorsPerPhysical();
  float GetProcessorClockFrequency();
  int GetProcessorAPICID();
  int GetProcessorCacheXSize(long int);
  bool DoesCPUSupportFeature(long int);
Kitware Robot's avatar
Kitware Robot committed
333

Kitware Robot's avatar
Kitware Robot committed
334
335
  const char * GetOSName();
  const char * GetHostname();
Kitware Robot's avatar
Kitware Robot committed
336
  int GetFullyQualifiedDomainName(kwsys_stl::string &fqdn);
Kitware Robot's avatar
Kitware Robot committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  const char * GetOSRelease();
  const char * GetOSVersion();
  const char * GetOSPlatform();

  bool Is64Bits();

  unsigned int GetNumberOfLogicalCPU(); // per physical cpu
  unsigned int GetNumberOfPhysicalCPU();

  bool DoesCPUSupportCPUID();

  // Retrieve memory information in megabyte.
  size_t GetTotalVirtualMemory();
  size_t GetAvailableVirtualMemory();
  size_t GetTotalPhysicalMemory();
Kitware Robot's avatar
Kitware Robot committed
352
353
354
355
356
  size_t GetAvailablePhysicalMemory();

  LongLong GetProcessId();

  // Retrieve memory information in kib
Kitware Robot's avatar
Kitware Robot committed
357
358
359
360
361
362
363
364
  LongLong GetHostMemoryTotal();
  LongLong GetHostMemoryAvailable(const char *envVarName);
  LongLong GetHostMemoryUsed();

  LongLong GetProcMemoryAvailable(
        const char *hostLimitEnvVarName,
        const char *procLimitEnvVarName);
  LongLong GetProcMemoryUsed();
Kitware Robot's avatar
Kitware Robot committed
365
366
367
368

  // enable/disable stack trace signal handler.
  static
  void SetStackTraceOnError(int enable);
Kitware Robot's avatar
Kitware Robot committed
369

Kitware Robot's avatar
Kitware Robot committed
370
371
372
373
  // get current stack
  static
  kwsys_stl::string GetProgramStack(int firstFrame, int wholePath);

Kitware Robot's avatar
Kitware Robot committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  /** Run the different checks */
  void RunCPUCheck();
  void RunOSCheck();
  void RunMemoryCheck();

public:
  typedef struct tagID
    {
    int Type;
    int Family;
    int Model;
    int Revision;
    int ExtendedFamily;
    int ExtendedModel;
    kwsys_stl::string ProcessorName;
    kwsys_stl::string Vendor;
    kwsys_stl::string SerialNumber;
Kitware Robot's avatar
Kitware Robot committed
391
    kwsys_stl::string ModelName;
Kitware Robot's avatar
Kitware Robot committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
    } ID;

  typedef struct tagCPUPowerManagement
    {
    bool HasVoltageID;
    bool HasFrequencyID;
    bool HasTempSenseDiode;
    } CPUPowerManagement;

  typedef struct tagCPUExtendedFeatures
    {
    bool Has3DNow;
    bool Has3DNowPlus;
    bool SupportsMP;
    bool HasMMXPlus;
    bool HasSSEMMX;
    bool SupportsHyperthreading;
    unsigned int LogicalProcessorsPerPhysical;
    int APIC_ID;
    CPUPowerManagement PowerManagement;
    } CPUExtendedFeatures;

  typedef struct CPUtagFeatures
    {
    bool HasFPU;
    bool HasTSC;
    bool HasMMX;
    bool HasSSE;
    bool HasSSEFP;
    bool HasSSE2;
    bool HasIA64;
    bool HasAPIC;
    bool HasCMOV;
    bool HasMTRR;
    bool HasACPI;
    bool HasSerial;
    bool HasThermal;
    int CPUSpeed;
    int L1CacheSize;
    int L2CacheSize;
    int L3CacheSize;
    CPUExtendedFeatures ExtendedFeatures;
    } CPUFeatures;

  enum Manufacturer
    {
    AMD, Intel, NSC, UMC, Cyrix, NexGen, IDT, Rise, Transmeta, Sun, IBM,
Kitware Robot's avatar
Kitware Robot committed
439
    Motorola, HP, UnknownManufacturer
Kitware Robot's avatar
Kitware Robot committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
    };

protected:
  // Functions.
  bool RetrieveCPUFeatures();
  bool RetrieveCPUIdentity();
  bool RetrieveCPUCacheDetails();
  bool RetrieveClassicalCPUCacheDetails();
  bool RetrieveCPUClockSpeed();
  bool RetrieveClassicalCPUClockSpeed();
  bool RetrieveCPUExtendedLevelSupport(int);
  bool RetrieveExtendedCPUFeatures();
  bool RetrieveProcessorSerialNumber();
  bool RetrieveCPUPowerManagement();
  bool RetrieveClassicalCPUIdentity();
  bool RetrieveExtendedCPUIdentity();

  Manufacturer  ChipManufacturer;
  CPUFeatures   Features;
  ID            ChipID;
  float         CPUSpeedInMHz;
  unsigned int  NumberOfLogicalCPU;
  unsigned int  NumberOfPhysicalCPU;

  int CPUCount();
  unsigned char LogicalCPUPerPhysicalCPU();
  unsigned char GetAPICId();
Kitware Robot's avatar
Kitware Robot committed
467
468
  bool IsHyperThreadingSupported();
  static LongLong GetCyclesDifference(DELAY_FUNC, unsigned int);
Kitware Robot's avatar
Kitware Robot committed
469
470

  // For Linux and Cygwin, /proc/cpuinfo formats are slightly different
Kitware Robot's avatar
Kitware Robot committed
471
  bool RetreiveInformationFromCpuInfoFile();
Kitware Robot's avatar
Kitware Robot committed
472
473
474
  kwsys_stl::string ExtractValueFromCpuInfoFile(kwsys_stl::string buffer,
                                          const char* word, size_t init=0);

Kitware Robot's avatar
Kitware Robot committed
475
476
477
  bool QueryLinuxMemory();
  bool QueryCygwinMemory();

Kitware Robot's avatar
Kitware Robot committed
478
479
480
  static void Delay (unsigned int);
  static void DelayOverhead (unsigned int);

Kitware Robot's avatar
Kitware Robot committed
481
  void FindManufacturer(const kwsys_stl::string &family = "");
Kitware Robot's avatar
Kitware Robot committed
482
483
484

  // For Mac
  bool ParseSysCtl();
Kitware Robot's avatar
Kitware Robot committed
485
  int CallSwVers(const char *arg, kwsys_stl::string &ver);
Kitware Robot's avatar
Kitware Robot committed
486
487
488
489
490
  void TrimNewline(kwsys_stl::string&);
  kwsys_stl::string ExtractValueFromSysCtl(const char* word);
  kwsys_stl::string SysCtlBuffer;

  // For Solaris
Kitware Robot's avatar
Kitware Robot committed
491
492
  bool QuerySolarisMemory();
  bool QuerySolarisProcessor();
Kitware Robot's avatar
Kitware Robot committed
493
494
495
496
497
498
499
500
501
502
  kwsys_stl::string ParseValueFromKStat(const char* arguments);
  kwsys_stl::string RunProcess(kwsys_stl::vector<const char*> args);

  //For Haiku OS
  bool QueryHaikuInfo();

  //For QNX
  bool QueryQNXMemory();
  bool QueryQNXProcessor();

Kitware Robot's avatar
Kitware Robot committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
  //For OpenBSD, FreeBSD, NetBSD, DragonFly
  bool QueryBSDMemory();
  bool QueryBSDProcessor();

  //For HP-UX
  bool QueryHPUXMemory();
  bool QueryHPUXProcessor();

  //For Microsoft Windows
  bool QueryWindowsMemory();

  //For AIX
  bool QueryAIXMemory();

  bool QueryProcessorBySysconf();
  bool QueryProcessor();

Kitware Robot's avatar
Kitware Robot committed
520
  // Evaluate the memory information.
Kitware Robot's avatar
Kitware Robot committed
521
522
  bool QueryMemoryBySysconf();
  bool QueryMemory();
Kitware Robot's avatar
Kitware Robot committed
523
524
525
526
527
528
529
530
531
532
533
534
535
  size_t TotalVirtualMemory;
  size_t AvailableVirtualMemory;
  size_t TotalPhysicalMemory;
  size_t AvailablePhysicalMemory;

  size_t CurrentPositionInFile;

  // Operating System information
  bool QueryOSInformation();
  kwsys_stl::string OSName;
  kwsys_stl::string Hostname;
  kwsys_stl::string OSRelease;
  kwsys_stl::string OSVersion;
Kitware Robot's avatar
Kitware Robot committed
536
  kwsys_stl::string OSPlatform;
Kitware Robot's avatar
Kitware Robot committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
};


SystemInformation::SystemInformation()
{
  this->Implementation = new SystemInformationImplementation;
}

SystemInformation::~SystemInformation()
{
  delete this->Implementation;
}

const char * SystemInformation::GetVendorString()
{
  return this->Implementation->GetVendorString();
}

const char * SystemInformation::GetVendorID()
{
  return this->Implementation->GetVendorID();
}

kwsys_stl::string SystemInformation::GetTypeID()
{
  return this->Implementation->GetTypeID();
}

kwsys_stl::string SystemInformation::GetFamilyID()
{
  return this->Implementation->GetFamilyID();
}

kwsys_stl::string SystemInformation::GetModelID()
{
  return this->Implementation->GetModelID();
}

Kitware Robot's avatar
Kitware Robot committed
575
576
577
578
579
kwsys_stl::string SystemInformation::GetModelName()
{
  return this->Implementation->GetModelName();
}

Kitware Robot's avatar
Kitware Robot committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
kwsys_stl::string SystemInformation::GetSteppingCode()
{
  return this->Implementation->GetSteppingCode();
}

const char * SystemInformation::GetExtendedProcessorName()
{
  return this->Implementation->GetExtendedProcessorName();
}

const char * SystemInformation::GetProcessorSerialNumber()
{
  return this->Implementation->GetProcessorSerialNumber();
}

int SystemInformation::GetProcessorCacheSize()
{
  return this->Implementation->GetProcessorCacheSize();
}

unsigned int SystemInformation::GetLogicalProcessorsPerPhysical()
{
  return this->Implementation->GetLogicalProcessorsPerPhysical();
}

float SystemInformation::GetProcessorClockFrequency()
{
  return this->Implementation->GetProcessorClockFrequency();
}

int SystemInformation::GetProcessorAPICID()
{
  return this->Implementation->GetProcessorAPICID();
}

int SystemInformation::GetProcessorCacheXSize(long int l)
{
  return this->Implementation->GetProcessorCacheXSize(l);
}

bool SystemInformation::DoesCPUSupportFeature(long int i)
{
  return this->Implementation->DoesCPUSupportFeature(i);
}

Kitware Robot's avatar
Kitware Robot committed
625
626
kwsys_stl::string SystemInformation::GetCPUDescription()
{
Kitware Robot's avatar
Kitware Robot committed
627
  kwsys_ios::ostringstream oss;
Kitware Robot's avatar
Kitware Robot committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
  oss
    << this->GetNumberOfPhysicalCPU()
    << " core ";
  if (this->GetModelName().empty())
    {
    oss
      << this->GetProcessorClockFrequency()
      << " MHz "
      << this->GetVendorString()
      << " "
      << this->GetExtendedProcessorName();
    }
  else
    {
    oss << this->GetModelName();
    }

  // remove extra spaces
  kwsys_stl::string tmp=oss.str();
  size_t pos;
  while( (pos=tmp.find("  "))!=kwsys_stl::string::npos)
    {
    tmp.replace(pos,2," ");
    }

  return tmp;
}

Kitware Robot's avatar
Kitware Robot committed
656
657
658
659
660
661
662
663
664
665
const char * SystemInformation::GetOSName()
{
  return this->Implementation->GetOSName();
}

const char * SystemInformation::GetHostname()
{
  return this->Implementation->GetHostname();
}

Kitware Robot's avatar
Kitware Robot committed
666
667
668
kwsys_stl::string SystemInformation::GetFullyQualifiedDomainName()
{
  kwsys_stl::string fqdn;
Kitware Robot's avatar
Kitware Robot committed
669
  this->Implementation->GetFullyQualifiedDomainName(fqdn);
Kitware Robot's avatar
Kitware Robot committed
670
671
672
  return fqdn;
}

Kitware Robot's avatar
Kitware Robot committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
const char * SystemInformation::GetOSRelease()
{
  return this->Implementation->GetOSRelease();
}

const char * SystemInformation::GetOSVersion()
{
  return this->Implementation->GetOSVersion();
}

const char * SystemInformation::GetOSPlatform()
{
  return this->Implementation->GetOSPlatform();
}

Kitware Robot's avatar
Kitware Robot committed
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
int SystemInformation::GetOSIsWindows()
{
#if defined(_WIN32)
  return 1;
#else
  return 0;
#endif
}

int SystemInformation::GetOSIsLinux()
{
#if defined(__linux)
  return 1;
#else
  return 0;
#endif
}

int SystemInformation::GetOSIsApple()
{
#if defined(__APPLE__)
  return 1;
#else
  return 0;
#endif
}

kwsys_stl::string SystemInformation::GetOSDescription()
{
Kitware Robot's avatar
Kitware Robot committed
717
  kwsys_ios::ostringstream oss;
Kitware Robot's avatar
Kitware Robot committed
718
719
720
721
722
723
724
725
726
727
  oss
    << this->GetOSName()
    << " "
    << this->GetOSRelease()
    << " "
    << this->GetOSVersion();

  return oss.str();
}

Kitware Robot's avatar
Kitware Robot committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
bool SystemInformation::Is64Bits()
{
  return this->Implementation->Is64Bits();
}

unsigned int SystemInformation::GetNumberOfLogicalCPU() // per physical cpu
{
  return this->Implementation->GetNumberOfLogicalCPU();
}

unsigned int SystemInformation::GetNumberOfPhysicalCPU()
{
  return this->Implementation->GetNumberOfPhysicalCPU();
}

bool SystemInformation::DoesCPUSupportCPUID()
{
  return this->Implementation->DoesCPUSupportCPUID();
}

// Retrieve memory information in megabyte.
size_t SystemInformation::GetTotalVirtualMemory()
{
  return this->Implementation->GetTotalVirtualMemory();
}

size_t SystemInformation::GetAvailableVirtualMemory()
{
  return this->Implementation->GetAvailableVirtualMemory();
}

size_t SystemInformation::GetTotalPhysicalMemory()
{
  return this->Implementation->GetTotalPhysicalMemory();
}

size_t SystemInformation::GetAvailablePhysicalMemory()
{
  return this->Implementation->GetAvailablePhysicalMemory();
}

Kitware Robot's avatar
Kitware Robot committed
769
770
771
kwsys_stl::string SystemInformation::GetMemoryDescription(
      const char *hostLimitEnvVarName,
      const char *procLimitEnvVarName)
Kitware Robot's avatar
Kitware Robot committed
772
{
Kitware Robot's avatar
Kitware Robot committed
773
  kwsys_ios::ostringstream oss;
Kitware Robot's avatar
Kitware Robot committed
774
  oss
Kitware Robot's avatar
Kitware Robot committed
775
776
777
778
779
780
781
782
    << "Host Total: "
    << iostreamLongLong(this->GetHostMemoryTotal())
    << " KiB, Host Available: "
    << iostreamLongLong(this->GetHostMemoryAvailable(hostLimitEnvVarName))
    << " KiB, Process Available: "
    << iostreamLongLong(
         this->GetProcMemoryAvailable(hostLimitEnvVarName,procLimitEnvVarName))
    << " KiB";
Kitware Robot's avatar
Kitware Robot committed
783
784
785
  return oss.str();
}

Kitware Robot's avatar
Kitware Robot committed
786
787
// host memory info in units of KiB.
SystemInformation::LongLong SystemInformation::GetHostMemoryTotal()
Kitware Robot's avatar
Kitware Robot committed
788
{
Kitware Robot's avatar
Kitware Robot committed
789
  return this->Implementation->GetHostMemoryTotal();
Kitware Robot's avatar
Kitware Robot committed
790
791
}

Kitware Robot's avatar
Kitware Robot committed
792
793
SystemInformation::LongLong
SystemInformation::GetHostMemoryAvailable(const char *hostLimitEnvVarName)
Kitware Robot's avatar
Kitware Robot committed
794
{
Kitware Robot's avatar
Kitware Robot committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
  return this->Implementation->GetHostMemoryAvailable(hostLimitEnvVarName);
}

SystemInformation::LongLong SystemInformation::GetHostMemoryUsed()
{
  return this->Implementation->GetHostMemoryUsed();
}

// process memory info in units of KiB.
SystemInformation::LongLong
SystemInformation::GetProcMemoryAvailable(
        const char *hostLimitEnvVarName,
        const char *procLimitEnvVarName)
{
  return this->Implementation->GetProcMemoryAvailable(
          hostLimitEnvVarName,
          procLimitEnvVarName);
}

SystemInformation::LongLong SystemInformation::GetProcMemoryUsed()
{
  return this->Implementation->GetProcMemoryUsed();
Kitware Robot's avatar
Kitware Robot committed
817
818
819
820
821
822
823
824
825
826
827
828
}

SystemInformation::LongLong SystemInformation::GetProcessId()
{
  return this->Implementation->GetProcessId();
}

void SystemInformation::SetStackTraceOnError(int enable)
{
  SystemInformationImplementation::SetStackTraceOnError(enable);
}

Kitware Robot's avatar
Kitware Robot committed
829
830
831
832
833
kwsys_stl::string SystemInformation::GetProgramStack(int firstFrame, int wholePath)
{
  return SystemInformationImplementation::GetProgramStack(firstFrame, wholePath);
}

Kitware Robot's avatar
Kitware Robot committed
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/** Run the different checks */
void SystemInformation::RunCPUCheck()
{
  this->Implementation->RunCPUCheck();
}

void SystemInformation::RunOSCheck()
{
  this->Implementation->RunOSCheck();
}

void SystemInformation::RunMemoryCheck()
{
  this->Implementation->RunMemoryCheck();
}


// --------------------------------------------------------------
// SystemInformationImplementation starts here

#define STORE_TLBCACHE_INFO(x,y)  x = (x < y) ? y : x
#define TLBCACHE_INFO_UNITS      (15)
#define CLASSICAL_CPU_FREQ_LOOP    10000000
#define RDTSC_INSTRUCTION      _asm _emit 0x0f _asm _emit 0x31

#define MMX_FEATURE            0x00000001
#define MMX_PLUS_FEATURE       0x00000002
#define SSE_FEATURE            0x00000004
#define SSE2_FEATURE           0x00000008
#define AMD_3DNOW_FEATURE      0x00000010
#define AMD_3DNOW_PLUS_FEATURE 0x00000020
#define IA64_FEATURE           0x00000040
#define MP_CAPABLE             0x00000080
#define HYPERTHREAD_FEATURE    0x00000100
#define SERIALNUMBER_FEATURE   0x00000200
#define APIC_FEATURE           0x00000400
#define SSE_FP_FEATURE         0x00000800
#define SSE_MMX_FEATURE        0x00001000
#define CMOV_FEATURE           0x00002000
#define MTRR_FEATURE           0x00004000
#define L1CACHE_FEATURE        0x00008000
#define L2CACHE_FEATURE        0x00010000
#define L3CACHE_FEATURE        0x00020000
#define ACPI_FEATURE           0x00040000
#define THERMALMONITOR_FEATURE 0x00080000
#define TEMPSENSEDIODE_FEATURE 0x00100000
#define FREQUENCYID_FEATURE    0x00200000
#define VOLTAGEID_FREQUENCY    0x00400000

// Status Flag
#define HT_NOT_CAPABLE           0
#define HT_ENABLED               1
#define HT_DISABLED              2
#define HT_SUPPORTED_NOT_ENABLED 3
#define HT_CANNOT_DETECT         4

// EDX[28]  Bit 28 is set if HT is supported
Kitware Robot's avatar
Kitware Robot committed
891
#define HT_BIT             0x10000000
Kitware Robot's avatar
Kitware Robot committed
892
893
894

// EAX[11:8] Bit 8-11 contains family processor ID.
#define FAMILY_ID          0x0F00
Kitware Robot's avatar
Kitware Robot committed
895
#define PENTIUM4_ID        0x0F00
Kitware Robot's avatar
Kitware Robot committed
896
// EAX[23:20] Bit 20-23 contains extended family processor ID
Kitware Robot's avatar
Kitware Robot committed
897
#define EXT_FAMILY_ID      0x0F00000
Kitware Robot's avatar
Kitware Robot committed
898
// EBX[23:16] Bit 16-23 in ebx contains the number of logical
Kitware Robot's avatar
Kitware Robot committed
899
900
#define NUM_LOGICAL_BITS   0x00FF0000
// processors per physical processor when execute cpuid with
Kitware Robot's avatar
Kitware Robot committed
901
// eax set to 1
Kitware Robot's avatar
Kitware Robot committed
902
903
// EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique
#define INITIAL_APIC_ID_BITS  0xFF000000
Kitware Robot's avatar
Kitware Robot committed
904
905
906
// initial APIC ID for the processor this code is running on.
// Default value = 0xff if HT is not supported

Kitware Robot's avatar
Kitware Robot committed
907
908
// Hide implementation details in an anonymous namespace.
namespace {
Kitware Robot's avatar
Kitware Robot committed
909
// *****************************************************************************
Kitware Robot's avatar
Kitware Robot committed
910
#if defined(__linux) || defined(__APPLE__)
Kitware Robot's avatar
Kitware Robot committed
911
int LoadLines(
Kitware Robot's avatar
Kitware Robot committed
912
      FILE *file,
Kitware Robot's avatar
Kitware Robot committed
913
914
915
916
917
918
      kwsys_stl::vector<kwsys_stl::string> &lines)
{
  // Load each line in the given file into a the vector.
  int nRead=0;
  const int bufSize=1024;
  char buf[bufSize]={'\0'};
Kitware Robot's avatar
Kitware Robot committed
919
920
921
922
923
924
925
926
927
928
929
  while (!feof(file) && !ferror(file))
    {
    errno=0;
    if (fgets(buf,bufSize,file) == 0)
      {
      if (ferror(file) && (errno==EINTR))
        {
        clearerr(file);
        }
      continue;
      }
Kitware Robot's avatar
Kitware Robot committed
930
931
932
933
934
935
    char *pBuf=buf;
    while(*pBuf)
      {
      if (*pBuf=='\n') *pBuf='\0';
      pBuf+=1;
      }
Kitware Robot's avatar
Kitware Robot committed
936
937
938
939
    lines.push_back(buf);
    ++nRead;
    }
  if (ferror(file))
Kitware Robot's avatar
Kitware Robot committed
940
941
942
    {
    return 0;
    }
Kitware Robot's avatar
Kitware Robot committed
943
944
945
  return nRead;
}

Kitware Robot's avatar
Kitware Robot committed
946
# if defined(__linux)
Kitware Robot's avatar
Kitware Robot committed
947
948
949
950
951
952
953
// *****************************************************************************
int LoadLines(
      const char *fileName,
      kwsys_stl::vector<kwsys_stl::string> &lines)
{
  FILE *file=fopen(fileName,"r");
  if (file==0)
Kitware Robot's avatar
Kitware Robot committed
954
    {
Kitware Robot's avatar
Kitware Robot committed
955
    return 0;
Kitware Robot's avatar
Kitware Robot committed
956
    }
Kitware Robot's avatar
Kitware Robot committed
957
958
  int nRead=LoadLines(file,lines);
  fclose(file);
Kitware Robot's avatar
Kitware Robot committed
959
960
  return nRead;
}
Kitware Robot's avatar
Kitware Robot committed
961
# endif
Kitware Robot's avatar
Kitware Robot committed
962
963
964
965
966
967
968
969
970
971

// ****************************************************************************
template<typename T>
int NameValue(
      kwsys_stl::vector<kwsys_stl::string> &lines,
      kwsys_stl::string name, T &value)
{
  size_t nLines=lines.size();
  for (size_t i=0; i<nLines; ++i)
    {
Kitware Robot's avatar
Kitware Robot committed
972
973
    size_t at=lines[i].find(name);
    if (at==kwsys_stl::string::npos)
Kitware Robot's avatar
Kitware Robot committed
974
      {
Kitware Robot's avatar
Kitware Robot committed
975
      continue;
Kitware Robot's avatar
Kitware Robot committed
976
      }
Kitware Robot's avatar
Kitware Robot committed
977
    kwsys_ios::istringstream is(lines[i].substr(at+name.size()));
Kitware Robot's avatar
Kitware Robot committed
978
979
    is >> value;
    return 0;
Kitware Robot's avatar
Kitware Robot committed
980
981
982
    }
  return -1;
}
Kitware Robot's avatar
Kitware Robot committed
983
#endif
Kitware Robot's avatar
Kitware Robot committed
984

Kitware Robot's avatar
Kitware Robot committed
985
#if defined(__linux)
Kitware Robot's avatar
Kitware Robot committed
986
987
988
989
990
991
// ****************************************************************************
template<typename T>
int GetFieldsFromFile(
      const char *fileName,
      const char **fieldNames,
      T *values)
Kitware Robot's avatar
Kitware Robot committed
992
993
994
995
996
997
{
  kwsys_stl::vector<kwsys_stl::string> fields;
  if (!LoadLines(fileName,fields))
    {
    return -1;
    }
Kitware Robot's avatar
Kitware Robot committed
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
  int i=0;
  while (fieldNames[i]!=NULL)
    {
    int ierr=NameValue(fields,fieldNames[i],values[i]);
    if (ierr)
      {
      return -(i+2);
      }
    i+=1;
    }
  return 0;
}

// ****************************************************************************
template<typename T>
Kitware Robot's avatar
Kitware Robot committed
1013
1014
int GetFieldFromFile(
      const char *fileName,
Kitware Robot's avatar
Kitware Robot committed
1015
1016
1017
      const char *fieldName,
      T &value)
{
Kitware Robot's avatar
Kitware Robot committed
1018
  const char *fieldNames[2]={fieldName,NULL};
Kitware Robot's avatar
Kitware Robot committed
1019
  T values[1]={T(0)};
Kitware Robot's avatar
Kitware Robot committed
1020
  int ierr=GetFieldsFromFile(fileName,fieldNames,values);
Kitware Robot's avatar
Kitware Robot committed
1021
1022
  if (ierr)
    {
Kitware Robot's avatar
Kitware Robot committed
1023
1024
1025
1026
1027
    return ierr;
    }
  value=values[0];
  return 0;
}
Kitware Robot's avatar
Kitware Robot committed
1028
#endif
Kitware Robot's avatar
Kitware Robot committed
1029
1030

// ****************************************************************************
Kitware Robot's avatar
Kitware Robot committed
1031
#if defined(__APPLE__)
Kitware Robot's avatar
Kitware Robot committed
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
template<typename T>
int GetFieldsFromCommand(
      const char *command,
      const char **fieldNames,
      T *values)
{
  FILE *file=popen(command,"r");
  if (file==0)
    {
    return -1;
    }
  kwsys_stl::vector<kwsys_stl::string> fields;
  int nl=LoadLines(file,fields);
  pclose(file);
  if (nl==0)
    {
    return -1;
    }
  int i=0;
  while (fieldNames[i]!=NULL)
    {
    int ierr=NameValue(fields,fieldNames[i],values[i]);
    if (ierr)
      {
      return -(i+2);
      }
    i+=1;
Kitware Robot's avatar
Kitware Robot committed
1059
1060
1061
    }
  return 0;
}
Kitware Robot's avatar
Kitware Robot committed
1062
#endif
Kitware Robot's avatar
Kitware Robot committed
1063

Kitware Robot's avatar
Kitware Robot committed
1064
// ****************************************************************************
Kitware Robot's avatar
Kitware Robot committed
1065
#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
Kitware Robot's avatar
Kitware Robot committed
1066
1067
1068
1069
1070
1071
void StacktraceSignalHandler(
      int sigNo,
      siginfo_t *sigInfo,
      void * /*sigContext*/)
{
#if defined(__linux) || defined(__APPLE__)
Kitware Robot's avatar
Kitware Robot committed
1072
1073
  kwsys_ios::ostringstream oss;
  oss
Kitware Robot's avatar
Kitware Robot committed
1074
     << kwsys_ios::endl
Kitware Robot's avatar
Kitware Robot committed
1075
1076
     << "=========================================================" << kwsys_ios::endl
     << "Process id " << getpid() << " ";
Kitware Robot's avatar
Kitware Robot committed
1077
1078
  switch (sigNo)
    {
Kitware Robot's avatar
Kitware Robot committed
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
    case SIGINT:
      oss << "Caught SIGINT";
      break;

    case SIGTERM:
      oss << "Caught SIGTERM";
      break;

    case SIGABRT:
      oss << "Caught SIGABRT";
      break;

Kitware Robot's avatar
Kitware Robot committed
1091
    case SIGFPE:
Kitware Robot's avatar
Kitware Robot committed
1092
1093
1094
1095
1096
      oss
        << "Caught SIGFPE at "
        << (sigInfo->si_addr==0?"0x":"")
        << sigInfo->si_addr
        <<  " ";
Kitware Robot's avatar
Kitware Robot committed
1097
1098
1099
1100
      switch (sigInfo->si_code)
        {
# if defined(FPE_INTDIV)
        case FPE_INTDIV:
Kitware Robot's avatar
Kitware Robot committed
1101
          oss << "integer division by zero";
Kitware Robot's avatar
Kitware Robot committed
1102
1103
1104
1105
1106
          break;
# endif

# if defined(FPE_INTOVF)
        case FPE_INTOVF:
Kitware Robot's avatar
Kitware Robot committed
1107
          oss << "integer overflow";
Kitware Robot's avatar
Kitware Robot committed
1108
1109
1110
1111
          break;
# endif

        case FPE_FLTDIV:
Kitware Robot's avatar
Kitware Robot committed
1112
          oss << "floating point divide by zero";
Kitware Robot's avatar
Kitware Robot committed
1113
1114
1115
          break;

        case FPE_FLTOVF:
Kitware Robot's avatar
Kitware Robot committed
1116
          oss << "floating point overflow";
Kitware Robot's avatar
Kitware Robot committed
1117
1118
1119
          break;

        case FPE_FLTUND:
Kitware Robot's avatar
Kitware Robot committed
1120
          oss << "floating point underflow";
Kitware Robot's avatar
Kitware Robot committed
1121
1122
1123
          break;

        case FPE_FLTRES:
Kitware Robot's avatar
Kitware Robot committed
1124
          oss << "floating point inexact result";
Kitware Robot's avatar
Kitware Robot committed
1125
1126
1127
          break;

        case FPE_FLTINV:
Kitware Robot's avatar
Kitware Robot committed
1128
          oss << "floating point invalid operation";
Kitware Robot's avatar
Kitware Robot committed
1129
1130
1131
1132
          break;

#if defined(FPE_FLTSUB)
        case FPE_FLTSUB:
Kitware Robot's avatar
Kitware Robot committed
1133
          oss << "floating point subscript out of range";
Kitware Robot's avatar
Kitware Robot committed
1134
1135
1136
1137
          break;
#endif

        default:
Kitware Robot's avatar
Kitware Robot committed
1138
          oss << "code " << sigInfo->si_code;
Kitware Robot's avatar
Kitware Robot committed
1139
1140
1141
1142
1143
          break;
        }
      break;

    case SIGSEGV:
Kitware Robot's avatar
Kitware Robot committed
1144
1145
1146
1147
1148
      oss
        << "Caught SIGSEGV at "
        << (sigInfo->si_addr==0?"0x":"")
        << sigInfo->si_addr
        <<  " ";
Kitware Robot's avatar
Kitware Robot committed
1149
1150
1151
      switch (sigInfo->si_code)
        {
        case SEGV_MAPERR:
Kitware Robot's avatar
Kitware Robot committed
1152
          oss << "address not mapped to object";
Kitware Robot's avatar
Kitware Robot committed
1153
1154
1155
          break;

        case SEGV_ACCERR:
Kitware Robot's avatar
Kitware Robot committed
1156
          oss << "invalid permission for mapped object";
Kitware Robot's avatar
Kitware Robot committed
1157
1158
1159
          break;

        default:
Kitware Robot's avatar
Kitware Robot committed
1160
          oss << "code " << sigInfo->si_code;
Kitware Robot's avatar
Kitware Robot committed
1161
1162
1163
1164
1165
          break;
        }
      break;

    case SIGBUS:
Kitware Robot's avatar
Kitware Robot committed
1166
1167
1168
1169
1170
      oss
        << "Caught SIGBUS at "
        << (sigInfo->si_addr==0?"0x":"")
        << sigInfo->si_addr
        <<  " ";
Kitware Robot's avatar
Kitware Robot committed
1171
1172
1173
      switch (sigInfo->si_code)
        {
        case BUS_ADRALN:
Kitware Robot's avatar
Kitware Robot committed
1174
          oss << "invalid address alignment";
Kitware Robot's avatar
Kitware Robot committed
1175
1176
1177
1178
          break;

# if defined(BUS_ADRERR)
        case BUS_ADRERR:
Kitware Robot's avatar
Kitware Robot committed
1179
          oss << "nonexistent physical address";
Kitware Robot's avatar
Kitware Robot committed
1180
1181
1182
1183
1184
          break;
# endif

# if defined(BUS_OBJERR)
        case BUS_OBJERR:
Kitware Robot's avatar
Kitware Robot committed
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
          oss << "object-specific hardware error";
          break;
# endif

# if defined(BUS_MCEERR_AR)
        case BUS_MCEERR_AR:
          oss << "Hardware memory error consumed on a machine check; action required.";
          break;
# endif

# if defined(BUS_MCEERR_AO)
        case BUS_MCEERR_AO:
          oss << "Hardware memory error detected in process but not consumed; action optional.";
Kitware Robot's avatar
Kitware Robot committed
1198
1199
1200
1201
          break;
# endif

        default:
Kitware Robot's avatar
Kitware Robot committed
1202
          oss << "code " << sigInfo->si_code;
Kitware Robot's avatar
Kitware Robot committed
1203
1204
1205
1206
1207
          break;
        }
      break;

    case SIGILL:
Kitware Robot's avatar
Kitware Robot committed
1208
1209
1210
1211
1212
      oss
        << "Caught SIGILL at "
        << (sigInfo->si_addr==0?"0x":"")
        << sigInfo->si_addr
        <<  " ";
Kitware Robot's avatar
Kitware Robot committed
1213
1214
1215
      switch (sigInfo->si_code)
        {
        case ILL_ILLOPC:
Kitware Robot's avatar
Kitware Robot committed
1216
          oss << "illegal opcode";
Kitware Robot's avatar
Kitware Robot committed
1217
1218
1219
1220
          break;

# if defined(ILL_ILLOPN)
        case ILL_ILLOPN:
Kitware Robot's avatar
Kitware Robot committed
1221
          oss << "illegal operand";
Kitware Robot's avatar
Kitware Robot committed
1222
1223
1224
1225
1226
          break;
# endif

# if defined(ILL_ILLADR)
        case ILL_ILLADR:
Kitware Robot's avatar
Kitware Robot committed
1227
          oss << "illegal addressing mode.";
Kitware Robot's avatar
Kitware Robot committed
1228
1229
1230
1231
          break;
# endif

        case ILL_ILLTRP:
Kitware Robot's avatar
Kitware Robot committed
1232
          oss << "illegal trap";
Kitware Robot's avatar
Kitware Robot committed
1233
1234

        case ILL_PRVOPC:
Kitware Robot's avatar
Kitware Robot committed
1235
          oss << "privileged opcode";
Kitware Robot's avatar
Kitware Robot committed
1236
1237
1238
1239
          break;

# if defined(ILL_PRVREG)
        case ILL_PRVREG:
Kitware Robot's avatar
Kitware Robot committed
1240
          oss << "privileged register";
Kitware Robot's avatar
Kitware Robot committed
1241
1242
1243
1244
1245
          break;
# endif

# if defined(ILL_COPROC)
        case ILL_COPROC:
Kitware Robot's avatar
Kitware Robot committed
1246
          oss << "co-processor error";
Kitware Robot's avatar
Kitware Robot committed
1247
1248
1249
1250
1251
          break;
# endif

# if defined(ILL_BADSTK)
        case ILL_BADSTK:
Kitware Robot's avatar
Kitware Robot committed
1252
          oss << "internal stack error";
Kitware Robot's avatar
Kitware Robot committed
1253
1254
1255
1256
          break;
# endif

        default:
Kitware Robot's avatar
Kitware Robot committed
1257
          oss << "code " << sigInfo->si_code;
Kitware Robot's avatar
Kitware Robot committed
1258
1259
1260
1261
1262
          break;
        }
      break;

    default:
Kitware Robot's avatar
Kitware Robot committed
1263
      oss << "Caught " << sigNo << " code " << sigInfo->si_code;
Kitware Robot's avatar
Kitware Robot committed
1264
1265
      break;
    }
Kitware Robot's avatar
Kitware Robot committed
1266
  oss
Kitware Robot's avatar
Kitware Robot committed
1267
1268
1269
1270
    << kwsys_ios::endl
    << "Program Stack:" << kwsys_ios::endl
    << SystemInformationImplementation::GetProgramStack(2,0)
    << "=========================================================" << kwsys_ios::endl;
Kitware Robot's avatar
Kitware Robot committed
1271
  kwsys_ios::cerr << oss.str() << kwsys_ios::endl;
Kitware Robot's avatar
Kitware Robot committed
1272
1273
1274
1275

  // restore the previously registered handlers
  // and abort
  SystemInformationImplementation::SetStackTraceOnError(0);
Kitware Robot's avatar
Kitware Robot committed
1276
1277
1278
1279
1280
1281
1282
  abort();
#else
  // avoid warning C4100
  (void)sigNo;
  (void)sigInfo;
#endif
}
Kitware Robot's avatar
Kitware Robot committed
1283
#endif
Kitware Robot's avatar
Kitware Robot committed
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487

#if defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
#define safes(_arg)((_arg)?(_arg):"???")

// Description:
// A container for symbol properties. Each instance
// must be Initialized.
class SymbolProperties
{
public:
  SymbolProperties();

  // Description:
  // The SymbolProperties instance must be initialized by
  // passing a stack address.
  void Initialize(void *address);

  // Description:
  // Get the symbol's stack address.
  void *GetAddress() const { return this->Address; }

  // Description:
  // If not set paths will be removed. eg, from a binary
  // or source file.
  void SetReportPath(int rp){ this->ReportPath=rp; }

  // Description:
  // Set/Get the name of the binary file that the symbol
  // is found in.
  void SetBinary(const char *binary)
    { this->Binary=safes(binary); }

  kwsys_stl::string GetBinary() const;

  // Description:
  // Set the name of the function that the symbol is found in.
  // If c++ demangling is supported it will be demangled.
  void SetFunction(const char *function)
    { this->Function=this->Demangle(function); }

  kwsys_stl::string GetFunction() const
    { return this->Function; }

  // Description:
  // Set/Get the name of the source file where the symbol
  // is defined.
  void SetSourceFile(const char *sourcefile)
    { this->SourceFile=safes(sourcefile); }

  kwsys_stl::string GetSourceFile() const
    { return this->GetFileName(this->SourceFile); }

  // Description:
  // Set/Get the line number where the symbol is defined
  void SetLineNumber(long linenumber){ this->LineNumber=linenumber; }
  long GetLineNumber() const { return this->LineNumber; }

  // Description:
  // Set the address where the biinary image is mapped
  // into memory.
  void SetBinaryBaseAddress(void *address)
    { this->BinaryBaseAddress=address; }

private:
  void *GetRealAddress() const
    { return (void*)((char*)this->Address-(char*)this->BinaryBaseAddress); }

  kwsys_stl::string GetFileName(const kwsys_stl::string &path) const;
  kwsys_stl::string Demangle(const char *symbol) const;

private:
  kwsys_stl::string Binary;
  void *BinaryBaseAddress;
  void *Address;
  kwsys_stl::string SourceFile;
  kwsys_stl::string Function;
  long LineNumber;
  int ReportPath;
};

// --------------------------------------------------------------------------
kwsys_ios::ostream &operator<<(
      kwsys_ios::ostream &os,
      const SymbolProperties &sp)
{
#if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
  os
    << kwsys_ios::hex << sp.GetAddress() << " : "
    << sp.GetFunction()
    << " [(" << sp.GetBinary() << ") "
    << sp.GetSourceFile() << ":"
    << kwsys_ios::dec << sp.GetLineNumber() << "]";
#elif defined(KWSYS_SYSTEMINFORMATION_HAS_BACKTRACE)
  void *addr = sp.GetAddress();
  char **syminfo = backtrace_symbols(&addr,1);
  os << safes(syminfo[0]);
  free(syminfo);
#else
  (void)os;
  (void)sp;
#endif
  return os;
}

// --------------------------------------------------------------------------
SymbolProperties::SymbolProperties()
{
  // not using an initializer list
  // to avoid some PGI compiler warnings
  this->SetBinary("???");
  this->SetBinaryBaseAddress(NULL);
  this->Address = NULL;
  this->SetSourceFile("???");
  this->SetFunction("???");
  this->SetLineNumber(-1);
  this->SetReportPath(0);
  // avoid PGI compiler warnings
  this->GetRealAddress();
  this->GetFunction();
  this->GetSourceFile();
  this->GetLineNumber();
}

// --------------------------------------------------------------------------
kwsys_stl::string SymbolProperties::GetFileName(const kwsys_stl::string &path) const
{
  kwsys_stl::string file(path);
  if (!this->ReportPath)
    {
    size_t at = file.rfind("/");
    if (at!=kwsys_stl::string::npos)
      {
      file = file.substr(at+1,kwsys_stl::string::npos);
      }
    }
  return file;
}

// --------------------------------------------------------------------------
kwsys_stl::string SymbolProperties::GetBinary() const
{
// only linux has proc fs
#if defined(__linux__)
  if (this->Binary=="/proc/self/exe")
    {
    kwsys_stl::string binary;
    char buf[1024]={'\0'};
    ssize_t ll=0;
    if ((ll=readlink("/proc/self/exe",buf,1024))>0)
      {
      buf[ll]='\0';
      binary=buf;
      }
    else
      {
      binary="/proc/self/exe";
      }
    return this->GetFileName(binary);
    }
#endif
  return this->GetFileName(this->Binary);
}

// --------------------------------------------------------------------------
kwsys_stl::string SymbolProperties::Demangle(const char *symbol) const
{
  kwsys_stl::string result = safes(symbol);
#if defined(KWSYS_SYSTEMINFORMATION_HAS_CPP_DEMANGLE)
  int status = 0;
  size_t bufferLen = 1024;
  char *buffer = (char*)malloc(1024);
  char *demangledSymbol =
    abi::__cxa_demangle(symbol, buffer, &bufferLen, &status);
  if (!status)
    {
    result = demangledSymbol;
    }
  free(buffer);
#else
  (void)symbol;
#endif
  return result;
}

// --------------------------------------------------------------------------
void SymbolProperties::Initialize(void *address)
{
  this->Address = address;
#if defined(KWSYS_SYSTEMINFORMATION_HAS_SYMBOL_LOOKUP)
  // first fallback option can demangle c++ functions
  Dl_info info;
  int ierr=dladdr(this->Address,&info);
  if (ierr && info.dli_sname && info.dli_saddr)
    {
    this->SetBinary(info.dli_fname);
    this->SetFunction(info.dli_sname);
    }
#else
  // second fallback use builtin backtrace_symbols
  // to decode the bactrace.
#endif
}
#endif // don't define this class if we're not using it

Kitware Robot's avatar
Kitware Robot committed
1488
} // anonymous namespace
Kitware Robot's avatar
Kitware Robot committed
1489

Kitware Robot's avatar
Kitware Robot committed
1490

Kitware Robot's avatar
Kitware Robot committed
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
SystemInformationImplementation::SystemInformationImplementation()
{
  this->TotalVirtualMemory = 0;
  this->AvailableVirtualMemory = 0;
  this->TotalPhysicalMemory = 0;
  this->AvailablePhysicalMemory = 0;
  this->CurrentPositionInFile = 0;
  this->ChipManufacturer = UnknownManufacturer;
  memset(&this->Features, 0, sizeof(CPUFeatures));
  this->ChipID.Type = 0;
  this->ChipID.Family = 0;
  this->ChipID.Model = 0;
  this->ChipID.Revision = 0;
  this->ChipID.ExtendedFamily = 0;
  this->ChipID.ExtendedModel = 0;
  this->CPUSpeedInMHz = 0;
  this->NumberOfLogicalCPU = 0;
  this->NumberOfPhysicalCPU = 0;
  this->OSName = "";
  this->Hostname = "";
  this->OSRelease = "";
  this->OSVersion = "";
  this->OSPlatform = "";
}

SystemInformationImplementation::~SystemInformationImplementation()
{
}

void SystemInformationImplementation::RunCPUCheck()
{
#ifdef WIN32
  // Check to see if this processor supports CPUID.
  bool supportsCPUID = DoesCPUSupportCPUID();

  if (supportsCPUID)
    {
    // Retrieve the CPU details.
    RetrieveCPUIdentity();
Kitware Robot's avatar
Kitware Robot committed
1530
    this->FindManufacturer();
Kitware Robot's avatar
Kitware Robot committed
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
    RetrieveCPUFeatures();
    }

  // These two may be called without support for the CPUID instruction.
  // (But if the instruction is there, they should be called *after*
  // the above call to RetrieveCPUIdentity... that's why the two if
  // blocks exist with the same "if (supportsCPUID)" logic...
  //
  if (!RetrieveCPUClockSpeed())
    {
    RetrieveClassicalCPUClockSpeed();
    }

  if (supportsCPUID)
    {
    // Retrieve cache information.
Kitware Robot's avatar
Kitware Robot committed
1547
    if (!RetrieveCPUCacheDetails())
Kitware Robot's avatar
Kitware Robot committed
1548
1549
1550
1551
1552
      {
      RetrieveClassicalCPUCacheDetails();
      }

    // Retrieve the extended CPU details.
Kitware Robot's avatar
Kitware Robot committed
1553
    if (!RetrieveExtendedCPUIdentity())
Kitware Robot's avatar
Kitware Robot committed
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
      {
      RetrieveClassicalCPUIdentity();
      }

    RetrieveExtendedCPUFeatures();
    RetrieveCPUPowerManagement();

    // Now attempt to retrieve the serial number (if possible).
    RetrieveProcessorSerialNumber();
    }

  this->CPUCount();

#elif defined(__APPLE__)
  this->ParseSysCtl();
#elif defined (__SVR4) && defined (__sun)
Kitware Robot's avatar
Kitware Robot committed
1570
  this->QuerySolarisProcessor();
Kitware Robot's avatar
Kitware Robot committed
1571
1572
1573
1574
#elif defined(__HAIKU__)
  this->QueryHaikuInfo();
#elif defined(__QNX__)
  this->QueryQNXProcessor();
Kitware Robot's avatar
Kitware Robot committed
1575
1576
1577
1578
1579
#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
  this->QueryBSDProcessor();
#elif defined(__hpux)
  this->QueryHPUXProcessor();
#elif defined(__linux) || defined(__CYGWIN__)
Kitware Robot's avatar
Kitware Robot committed
1580
  this->RetreiveInformationFromCpuInfoFile();
Kitware Robot's avatar
Kitware Robot committed
1581
1582
#else
  this->QueryProcessor();
Kitware Robot's avatar
Kitware Robot committed
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
#endif
}

void SystemInformationImplementation::RunOSCheck()
{
  this->QueryOSInformation();
}

void SystemInformationImplementation::RunMemoryCheck()
{
#if defined(__APPLE__)
  this->ParseSysCtl();
#elif defined (__SVR4) && defined (__sun)
Kitware Robot's avatar
Kitware Robot committed
1596
  this->QuerySolarisMemory();
Kitware Robot's avatar
Kitware Robot committed
1597
1598
1599
1600
#elif defined(__HAIKU__)
  this->QueryHaikuInfo();
#elif defined(__QNX__)
  this->QueryQNXMemory();
Kitware Robot's avatar
Kitware Robot committed
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
#elif defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
  this->QueryBSDMemory();
#elif defined(__CYGWIN__)
  this->QueryCygwinMemory();
#elif defined(_WIN32)
  this->QueryWindowsMemory();
#elif defined(__hpux)
  this->QueryHPUXMemory();
#elif defined(__linux)
  this->QueryLinuxMemory();
#elif defined(_AIX)
  this->QueryAIXMemory();
Kitware Robot's avatar
Kitware Robot committed
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
#else
  this->QueryMemory();
#endif
}

/** Get the vendor string */
const char * SystemInformationImplementation::GetVendorString()
{
  return this->ChipID.Vendor.c_str();
}

/** Get the OS Name */
const char * SystemInformationImplementation::GetOSName()
{
  return this->OSName.c_str();
}

/** Get the hostname */
const char* SystemInformationImplementation::GetHostname()
{
Kitware Robot's avatar
Kitware Robot committed
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
  if (this->Hostname.empty())
    {
    this->Hostname="localhost";
#if defined(_WIN32)
    WORD wVersionRequested;
    WSADATA wsaData;
    char name[255];
    wVersionRequested = MAKEWORD(2,0);
    if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
      {
      gethostname(name,sizeof(name));
      WSACleanup( );
      }
    this->Hostname = name;
#else
    struct utsname unameInfo;
    int errorFlag = uname(&unameInfo);
    if(errorFlag == 0)
      {
      this->Hostname = unameInfo.nodename;
      }
#endif
    }
Kitware Robot's avatar
Kitware Robot committed
1656
1657
1658
  return this->Hostname.c_str();
}

Kitware Robot's avatar
Kitware Robot committed
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
/** Get the FQDN */
int SystemInformationImplementation::GetFullyQualifiedDomainName(
      kwsys_stl::string &fqdn)
{
  // in the event of absolute failure return localhost.
  fqdn="localhost";

#if defined(_WIN32)
  int ierr;
  // TODO - a more robust implementation for windows, see comments
  // in unix implementation.
  WSADATA wsaData;
  WORD ver=MAKEWORD(2,0);
  ierr=WSAStartup(ver,&wsaData);
  if (ierr)
    {
    return -1;
    }

  char base[256]={'\0'};
  ierr=gethostname(base,256);
  if (ierr)
    {
    WSACleanup();
    return -2;
    }
  fqdn=base;

  HOSTENT *hent=gethostbyname(base);
  if (hent)
    {
    fqdn=hent->h_name;
    }

  WSACleanup();
  return 0;

Kitware Robot's avatar
Kitware Robot committed
1696
#elif defined(KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN)
Kitware Robot's avatar
Kitware Robot committed
1697
1698
1699
1700
  // gethostname typical returns an alias for loopback interface
  // we want the fully qualified domain name. Because there are
  // any number of interfaces on this system we look for the
  // first of these that contains the name returned by gethostname
Kitware Robot's avatar
Kitware Robot committed
1701
1702
1703
1704
1705
1706
  // and is longer. failing that we return gethostname and indicate
  // with a failure code. Return of a failure code is not necessarilly
  // an indication of an error. for instance gethostname may return
  // the fully qualified domain name, or there may not be one if the
  // system lives on a private network such as in the case of a cluster
  // node.
Kitware Robot's avatar
Kitware Robot committed
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727

  int ierr=0;
  char base[NI_MAXHOST];
  ierr=gethostname(base,NI_MAXHOST);
  if (ierr)
    {
    return -1;
    }
  size_t baseSize=strlen(base);
  fqdn=base;

  struct ifaddrs *ifas;
  struct ifaddrs *ifa;
  ierr=getifaddrs(&ifas);
  if (ierr)
    {
    return -2;
    }

  for (ifa=ifas; ifa!=NULL; ifa=ifa->ifa_next)
    {
Kitware Robot's avatar
Kitware Robot committed
1728
    int fam = ifa->ifa_addr? ifa->ifa_addr->sa_family : -1;
Kitware Robot's avatar
Kitware Robot committed
1729
1730
1731
1732
    if ((fam==AF_INET) || (fam==AF_INET6))
      {
      char host[NI_MAXHOST]={'\0'};

Kitware Robot's avatar
Kitware Robot committed
1733
      const size_t addrlen
Kitware Robot's avatar
Kitware Robot committed
1734
1735
1736
1737
        = (fam==AF_INET?sizeof(struct sockaddr_in):sizeof(struct sockaddr_in6));

      ierr=getnameinfo(
            ifa->ifa_addr,
Kitware Robot's avatar
Kitware Robot committed
1738
            static_cast<socklen_t>(addrlen),
Kitware Robot's avatar
Kitware Robot committed
1739
1740
1741
1742
1743
1744
1745
            host,
            NI_MAXHOST,
            NULL,
            0,
            NI_NAMEREQD);
      if (ierr)
        {
Kitware Robot's avatar
Kitware Robot committed
1746
1747
        // don't report the failure now since we may succeed on another
        // interface. If all attempts fail then return the failure code.
Kitware Robot's avatar
Kitware Robot committed
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
        ierr=-3;
        continue;
        }

      kwsys_stl::string candidate=host;
      if ((candidate.find(base)!=kwsys_stl::string::npos) && baseSize<candidate.size())
        {
        // success, stop now.
        ierr=0;
        fqdn=candidate;
        break;
        }
      }
    }
  freeifaddrs(ifas);

  return ierr;
#else
  /* TODO: Implement on more platforms.  */
Kitware Robot's avatar
Kitware Robot committed
1767
  fqdn=this->GetHostname();
Kitware Robot's avatar
Kitware Robot committed
1768
1769
1770
1771
  return -1;
#endif
}

Kitware Robot's avatar
Kitware Robot committed
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
/** Get the OS release */
const char* SystemInformationImplementation::GetOSRelease()
{
  return this->OSRelease.c_str();
}

/** Get the OS version */
const char* SystemInformationImplementation::GetOSVersion()
{
  return this->OSVersion.c_str();
}

/** Get the OS platform */
const char* SystemInformationImplementation::GetOSPlatform()
{
  return this->OSPlatform.c_str();
}

/** Get the vendor ID */
const char * SystemInformationImplementation::GetVendorID()
{
  // Return the vendor ID.
  switch (this->ChipManufacturer)
    {
    case Intel:
      return "Intel Corporation";
    case AMD:
      return "Advanced Micro Devices";
    case NSC:
      return "National Semiconductor";
    case Cyrix:
      return "Cyrix Corp., VIA Inc.";
    case NexGen:
      return "NexGen Inc., Advanced Micro Devices";
    case IDT:
      return "IDT\\Centaur, Via Inc.";
    case UMC:
      return "United Microelectronics Corp.";
    case Rise:
      return "Rise";
    case Transmeta:
      return "Transmeta";
    case Sun:
      return "Sun Microelectronics";
    case IBM:
      return "IBM";
    case Motorola:
      return "Motorola";
Kitware Robot's avatar
Kitware Robot committed
1820
1821
    case HP:
      return "Hewlett-Packard";
Kitware Robot's avatar
Kitware Robot committed
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
    default:
      return "Unknown Manufacturer";
    }
}

/** Return the type ID of the CPU */
kwsys_stl::string SystemInformationImplementation::GetTypeID()
{
  kwsys_ios::ostringstream str;
  str << this->ChipID.Type;
  return str.str();
}

/** Return the family of the CPU present */
kwsys_stl::string SystemInformationImplementation::GetFamilyID()
{
  kwsys_ios::ostringstream str;
  str << this->ChipID.Family;
  return str.str();
}

// Return the model of CPU present */
kwsys_stl::string SystemInformationImplementation::GetModelID()
{
  kwsys_ios::ostringstream str;
  str << this->ChipID.Model;
  return str.str();
}

Kitware Robot's avatar
Kitware Robot committed
1851
1852
1853
1854
1855
1856
// Return the model name of CPU present */
kwsys_stl::string SystemInformationImplementation::GetModelName()
{
  return this->ChipID.ModelName;
}

Kitware Robot's avatar
Kitware Robot committed
1857
1858
/** Return the stepping code of the CPU present. */
kwsys_stl::string SystemInformationImplementation::GetSteppingCode()
Kitware Robot's avatar
Kitware Robot committed
1859
{
Kitware Robot's avatar
Kitware Robot committed
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
  kwsys_ios::ostringstream str;
  str << this->ChipID.Revision;
  return str.str();
}

/** Return the stepping code of the CPU present. */
const char * SystemInformationImplementation::GetExtendedProcessorName()
{
  return this->ChipID.ProcessorName.c_str();
}
Kitware Robot's avatar
Kitware Robot committed
1870
1871

/** Return the serial number of the processor
Kitware Robot's avatar
Kitware Robot committed
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
 *  in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */
const char * SystemInformationImplementation::GetProcessorSerialNumber()
{
  return this->ChipID.SerialNumber.c_str();
}

/** Return the logical processors per physical */
unsigned int SystemInformationImplementation::GetLogicalProcessorsPerPhysical()
{
  return this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical;
}

/** Return the processor clock frequency. */
float SystemInformationImplementation::GetProcessorClockFrequency()
{
  return this->CPUSpeedInMHz;
}

/**  Return the APIC ID. */
int SystemInformationImplementation::GetProcessorAPICID()
{
  return this->Features.ExtendedFeatures.APIC_ID;
}

/** Return the L1 cache size. */
int SystemInformationImplementation::GetProcessorCacheSize()
{
  return this->Features.L1CacheSize;
}

/** Return the chosen cache size. */
int SystemInformationImplementation::GetProcessorCacheXSize(long int dwCacheID)
{
  switch (dwCacheID)
    {
    case L1CACHE_FEATURE:
      return this->Features.L1CacheSize;
    case L2CACHE_FEATURE:
      return this->Features.L2CacheSize;
    case L3CACHE_FEATURE:
      return this->