diff options
-rw-r--r-- | CTestCustom.cmake.in | 1 | ||||
-rw-r--r-- | Source/kwsys/CMakeLists.txt | 105 | ||||
-rw-r--r-- | Source/kwsys/CTestConfig.cmake | 11 | ||||
-rw-r--r-- | Source/kwsys/Configure.h.in | 14 | ||||
-rw-r--r-- | Source/kwsys/ProcessUNIX.c | 1 | ||||
-rw-r--r-- | Source/kwsys/SystemInformation.cxx | 1671 | ||||
-rw-r--r-- | Source/kwsys/SystemInformation.hxx.in | 87 | ||||
-rw-r--r-- | Source/kwsys/SystemTools.cxx | 2 | ||||
-rw-r--r-- | Source/kwsys/kwsysPlatformTestsCXX.cxx | 66 | ||||
-rw-r--r-- | Source/kwsys/testSystemInformation.cxx | 34 | ||||
-rwxr-xr-x | Utilities/Git/pre-commit | 14 |
11 files changed, 1647 insertions, 359 deletions
diff --git a/CTestCustom.cmake.in b/CTestCustom.cmake.in index 1e6224b4be..495d156cc6 100644 --- a/CTestCustom.cmake.in +++ b/CTestCustom.cmake.in @@ -31,6 +31,7 @@ set(CTEST_CUSTOM_WARNING_EXCEPTION "remark: .*LOOP WAS VECTORIZED" "warning .980: wrong number of actual arguments to intrinsic function .std::basic_" "LINK : warning LNK4089: all references to.*ADVAPI32.dll.*discarded by /OPT:REF" + "LINK : warning LNK4089: all references to.*PSAPI.DLL.*discarded by /OPT:REF" "LINK : warning LNK4089: all references to.*USER32.dll.*discarded by /OPT:REF" "Warning: library was too large for page size.*" "Warning: public.*_archive_.*in module.*archive_*clashes with prior module.*archive_.*" diff --git a/Source/kwsys/CMakeLists.txt b/Source/kwsys/CMakeLists.txt index a132357f39..777d76f4b2 100644 --- a/Source/kwsys/CMakeLists.txt +++ b/Source/kwsys/CMakeLists.txt @@ -509,19 +509,28 @@ ENDIF(KWSYS_USE_FundamentalType) IF(KWSYS_USE_IOStream) # Determine whether iostreams support long long. + SET(KWSYS_PLATFORM_CXX_TEST_DEFINES + -DKWSYS_IOS_USE_ANSI=${KWSYS_IOS_USE_ANSI} + -DKWSYS_IOS_HAVE_STD=${KWSYS_IOS_HAVE_STD}) IF(KWSYS_CXX_HAS_LONG_LONG) - SET(KWSYS_PLATFORM_CXX_TEST_DEFINES - -DKWSYS_IOS_USE_ANSI=${KWSYS_IOS_USE_ANSI} - -DKWSYS_IOS_HAVE_STD=${KWSYS_IOS_HAVE_STD}) KWSYS_PLATFORM_CXX_TEST(KWSYS_IOS_HAS_ISTREAM_LONG_LONG "Checking if istream supports long long" DIRECT) KWSYS_PLATFORM_CXX_TEST(KWSYS_IOS_HAS_OSTREAM_LONG_LONG "Checking if ostream supports long long" DIRECT) - SET(KWSYS_PLATFORM_CXX_TEST_DEFINES) ELSE() SET(KWSYS_IOS_HAS_ISTREAM_LONG_LONG 0) SET(KWSYS_IOS_HAS_OSTREAM_LONG_LONG 0) ENDIF() + IF(KWSYS_CXX_HAS___INT64) + KWSYS_PLATFORM_CXX_TEST(KWSYS_IOS_HAS_ISTREAM___INT64 + "Checking if istream supports __int64" DIRECT) + KWSYS_PLATFORM_CXX_TEST(KWSYS_IOS_HAS_OSTREAM___INT64 + "Checking if ostream supports __int64" DIRECT) + ELSE() + SET(KWSYS_IOS_HAS_ISTREAM___INT64 0) + SET(KWSYS_IOS_HAS_OSTREAM___INT64 0) + ENDIF() + SET(KWSYS_PLATFORM_CXX_TEST_DEFINES) ENDIF(KWSYS_USE_IOStream) IF(KWSYS_NAMESPACE MATCHES "^kwsys$") @@ -566,9 +575,76 @@ IF(KWSYS_USE_SystemTools) ENDIF() IF(KWSYS_USE_SystemInformation) - SET_PROPERTY(SOURCE SystemInformation.cxx PROPERTY - COMPILE_DEFINITIONS KWSYS_USE_LONG_LONG=${KWSYS_USE_LONG_LONG} - KWSYS_USE___INT64=${KWSYS_USE___INT64}) + IF(NOT CYGWIN) + INCLUDE(CheckIncludeFiles) + CHECK_INCLUDE_FILES("sys/types.h;ifaddrs.h" KWSYS_SYS_HAS_IFADDRS_H) + IF(KWSYS_SYS_HAS_IFADDRS_H) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_SYS_HAS_IFADDRS_H=1) + ENDIF() + ENDIF() + IF(WIN32) + INCLUDE(CheckSymbolExists) + SET(CMAKE_REQUIRED_LIBRARIES Psapi) + CHECK_SYMBOL_EXISTS(GetProcessMemoryInfo "windows.h;psapi.h" KWSYS_SYS_HAS_PSAPI) + UNSET(CMAKE_REQUIRED_LIBRARIES) + IF(KWSYS_SYS_HAS_PSAPI) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_SYS_HAS_PSAPI=1) + IF(MSVC70 OR MSVC71) + # Suppress LNK4089: all references to 'PSAPI.DLL' discarded by /OPT:REF + SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /IGNORE:4089") + ENDIF() + ENDIF() + ENDIF() + IF(KWSYS_LFS_AVAILABLE AND NOT KWSYS_LFS_DISABLE) + SET(KWSYS_PLATFORM_CXX_TEST_DEFINES -DKWSYS_HAS_LFS=1) + ENDIF() + KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_RLIMIT64 + "Checking whether CXX compiler has rlimit64" DIRECT) + SET(KWSYS_PLATFORM_CXX_TEST_DEFINES) + IF(KWSYS_CXX_HAS_RLIMIT64) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_CXX_HAS_RLIMIT64=1) + ENDIF() + KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_ATOL + "Checking whether CXX compiler has atol" DIRECT) + IF(KWSYS_CXX_HAS_ATOL) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_CXX_HAS_ATOL=1) + ENDIF() + KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_ATOLL + "Checking whether CXX compiler has atoll" DIRECT) + IF(KWSYS_CXX_HAS_ATOLL) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_CXX_HAS_ATOLL=1) + ENDIF() + KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS__ATOI64 + "Checking whether CXX compiler has _atoi64" DIRECT) + IF(KWSYS_CXX_HAS__ATOI64) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_CXX_HAS__ATOI64=1) + ENDIF() + IF(KWSYS_USE___INT64) + SET_PROPERTY(SOURCE SystemInformation.cxx testSystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_USE___INT64=1) + ENDIF() + IF(KWSYS_USE_LONG_LONG) + SET_PROPERTY(SOURCE SystemInformation.cxx testSystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_USE_LONG_LONG=1) + ENDIF() + IF(KWSYS_IOS_HAS_OSTREAM_LONG_LONG) + SET_PROPERTY(SOURCE SystemInformation.cxx testSystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_IOS_HAS_OSTREAM_LONG_LONG=1) + ENDIF() + IF(KWSYS_IOS_HAS_OSTREAM___INT64) + SET_PROPERTY(SOURCE SystemInformation.cxx testSystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_IOS_HAS_OSTREAM___INT64=1) + ENDIF() + IF(KWSYS_BUILD_SHARED) + SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY + COMPILE_DEFINITIONS KWSYS_BUILD_SHARED=1) + ENDIF() ENDIF() #----------------------------------------------------------------------------- @@ -802,9 +878,12 @@ IF(KWSYS_C_SRCS OR KWSYS_CXX_SRCS) TARGET_LINK_LIBRARIES(${KWSYS_NAMESPACE} ${CMAKE_DL_LIBS}) ENDIF(UNIX) ENDIF(KWSYS_USE_DynamicLoader) - + IF(KWSYS_USE_SystemInformation AND WIN32) TARGET_LINK_LIBRARIES(${KWSYS_NAMESPACE} ws2_32) + IF(KWSYS_SYS_HAS_PSAPI) + TARGET_LINK_LIBRARIES(${KWSYS_NAMESPACE} Psapi) + ENDIF() ENDIF(KWSYS_USE_SystemInformation AND WIN32) # Apply user-defined target properties to the library. @@ -842,6 +921,16 @@ ENDIF(KWSYS_ENABLE_C AND KWSYS_C_SRCS) # line to configure the namespace in the C and C++ source files. ADD_DEFINITIONS("-DKWSYS_NAMESPACE=${KWSYS_NAMESPACE}") +# Disable deprecation warnings for standard C functions. +IF(MSVC OR (WIN32 AND "${CMAKE_C_COMPILER_ID}" MATCHES "^(Intel)$")) + ADD_DEFINITIONS( + -D_CRT_NONSTDC_NO_DEPRECATE + -D_CRT_SECURE_NO_DEPRECATE + -D_CRT_SECURE_NO_WARNINGS + -D_SCL_SECURE_NO_DEPRECATE + ) +ENDIF() + IF(KWSYS_USE_String) # Activate code in "String.c". See the comment in the source. SET_SOURCE_FILES_PROPERTIES(String.c PROPERTIES diff --git a/Source/kwsys/CTestConfig.cmake b/Source/kwsys/CTestConfig.cmake index 9ab6ed86cb..d977b472a1 100644 --- a/Source/kwsys/CTestConfig.cmake +++ b/Source/kwsys/CTestConfig.cmake @@ -1,6 +1,6 @@ #============================================================================= # KWSys - Kitware System Library -# Copyright 2000-2009 Kitware, Inc., Insight Software Consortium +# Copyright 2000-2012 Kitware, Inc., Insight Software Consortium # # Distributed under the OSI-approved BSD License (the "License"); # see accompanying file Copyright.txt for details. @@ -9,10 +9,9 @@ # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the License for more information. #============================================================================= -set (CTEST_PROJECT_NAME "kwsys") -set (CTEST_NIGHTLY_START_TIME "21:00:00 EDT") -set (CTEST_DART_SERVER_VERSION "2") +set(CTEST_PROJECT_NAME "KWSys") +set(CTEST_NIGHTLY_START_TIME "21:00:00 EDT") set(CTEST_DROP_METHOD "http") -set(CTEST_DROP_SITE "www.cdash.org") -set(CTEST_DROP_LOCATION "/CDash/submit.php?project=PublicDashboard") +set(CTEST_DROP_SITE "open.cdash.org") +set(CTEST_DROP_LOCATION "/submit.php?project=PublicDashboard") set(CTEST_DROP_SITE_CDASH TRUE) diff --git a/Source/kwsys/Configure.h.in b/Source/kwsys/Configure.h.in index 15986cfea7..70cf8442be 100644 --- a/Source/kwsys/Configure.h.in +++ b/Source/kwsys/Configure.h.in @@ -37,20 +37,6 @@ /* Whether kwsys namespace is "kwsys". */ #define @KWSYS_NAMESPACE@_NAME_IS_KWSYS @KWSYS_NAME_IS_KWSYS@ -/* If we are building a kwsys .c or .cxx file, suppress the Microsoft - deprecation warnings. */ -#if defined(KWSYS_NAMESPACE) -# ifndef _CRT_NONSTDC_NO_DEPRECATE -# define _CRT_NONSTDC_NO_DEPRECATE -# endif -# ifndef _CRT_SECURE_NO_DEPRECATE -# define _CRT_SECURE_NO_DEPRECATE -# endif -# ifndef _SCL_SECURE_NO_DEPRECATE -# define _SCL_SECURE_NO_DEPRECATE -# endif -#endif - /* Whether Large File Support is requested. */ #define @KWSYS_NAMESPACE@_LFS_REQUESTED @KWSYS_LFS_REQUESTED@ diff --git a/Source/kwsys/ProcessUNIX.c b/Source/kwsys/ProcessUNIX.c index 9c66a44866..19922111aa 100644 --- a/Source/kwsys/ProcessUNIX.c +++ b/Source/kwsys/ProcessUNIX.c @@ -2732,6 +2732,7 @@ static void kwsysProcessesSignalHandler(int signum kwsysProcess* cp = kwsysProcesses.Processes[i]; kwsysProcess_ssize_t status= read(cp->PipeReadEnds[KWSYSPE_PIPE_SIGNAL], &buf, 1); + (void)status; status=write(cp->SignalPipe, &buf, 1); (void)status; } diff --git a/Source/kwsys/SystemInformation.cxx b/Source/kwsys/SystemInformation.cxx index e1ee873b4f..6b63b94695 100644 --- a/Source/kwsys/SystemInformation.cxx +++ b/Source/kwsys/SystemInformation.cxx @@ -9,7 +9,12 @@ implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more information. ============================================================================*/ -#ifdef _WIN32 + +#if defined(_WIN32) +# define NOMINMAX // use our min,max +# if !defined(_WIN32_WINNT) && !(defined(_MSC_VER) && _MSC_VER < 1300) +# define _WIN32_WINNT 0x0501 +# endif # include <winsock.h> // WSADATA, include before sys/types.h #endif @@ -33,6 +38,7 @@ #include KWSYS_HEADER(Process.h) #include KWSYS_HEADER(ios/iostream) #include KWSYS_HEADER(ios/sstream) +#include KWSYS_HEADER(ios/fstream) // Work-around CMake dependency scanning limitation. This must // duplicate the above list of headers. @@ -45,66 +51,142 @@ # include "kwsys_stl_iosfwd.in" # include "kwsys_ios_sstream.h.in" # include "kwsys_ios_iostream.h.in" +# include "kwsys_ios_fstream.h.in" #endif -#ifndef WIN32 +#if defined(_WIN32) +# include <windows.h> +# include <errno.h> +# 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); +# include <sys/resource.h> // getrlimit +# include <unistd.h> +# include <signal.h> +# include <fcntl.h> +# include <errno.h> // extern int errno; #endif -#ifdef _WIN32 -# include <windows.h> +#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 #ifdef __APPLE__ -#include <sys/sysctl.h> -#include <mach/vm_statistics.h> -#include <mach/host_info.h> -#include <mach/mach.h> -#include <mach/mach_types.h> +# 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> +# if defined(KWSYS_SYS_HAS_IFADDRS_H) +# include <ifaddrs.h> +# define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN +# endif +# if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__-0 >= 1050 +# include <execinfo.h> +# define KWSYS_SYSTEMINFORMATION_HAVE_BACKTRACE +# endif #endif #ifdef __linux -# include <sys/types.h> -# include <unistd.h> -# include <fcntl.h> -# include <ctype.h> // int isdigit(int c); -# include <errno.h> // extern int errno; -# include <sys/time.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> +# if !defined(__LSB_VERSION__) /* LSB has no getifaddrs */ +# define KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN +# endif +# endif +# if defined(__GNUG__) +# include <execinfo.h> +# if !(defined(__LSB_VERSION__) && __LSB_VERSION__ < 41) +# define KWSYS_SYSTEMINFORMATION_HAVE_BACKTRACE +# endif +# endif +# if defined(KWSYS_CXX_HAS_RLIMIT64) +typedef struct rlimit64 ResourceLimitType; +# define GetResourceLimit getrlimit64 +# else +typedef struct rlimit ResourceLimitType; +# define GetResourceLimit getrlimit +# endif #elif defined( __hpux ) # include <sys/param.h> # include <sys/pstat.h> #endif #ifdef __HAIKU__ -#include <OS.h> +# include <OS.h> #endif #include <memory.h> #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <ctype.h> // int isdigit(int c); + +#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 namespace KWSYS_NAMESPACE { +template<typename T> +T min(T a, T b){ return a<b ? a : b; } -// Create longlong -#if KWSYS_USE_LONG_LONG - typedef long long LongLong; -#elif KWSYS_USE___INT64 - typedef __int64 LongLong; -#else -# error "No Long Long" -#endif - +extern "C" { typedef void (*SigAction)(int,siginfo_t*,void*); } // Define SystemInformationImplementation class typedef void (*DELAY_FUNC)(unsigned int uiMS); - class SystemInformationImplementation { public: + typedef SystemInformation::LongLong LongLong; SystemInformationImplementation (); ~SystemInformationImplementation (); @@ -113,6 +195,7 @@ public: kwsys_stl::string GetTypeID(); kwsys_stl::string GetFamilyID(); kwsys_stl::string GetModelID(); + kwsys_stl::string GetModelName(); kwsys_stl::string GetSteppingCode(); const char * GetExtendedProcessorName(); const char * GetProcessorSerialNumber(); @@ -122,9 +205,10 @@ public: int GetProcessorAPICID(); int GetProcessorCacheXSize(long int); bool DoesCPUSupportFeature(long int); - + const char * GetOSName(); const char * GetHostname(); + int GetFullyQualifiedDomainName(kwsys_stl::string &fqdn); const char * GetOSRelease(); const char * GetOSVersion(); const char * GetOSPlatform(); @@ -140,7 +224,23 @@ public: size_t GetTotalVirtualMemory(); size_t GetAvailableVirtualMemory(); size_t GetTotalPhysicalMemory(); - size_t GetAvailablePhysicalMemory(); + size_t GetAvailablePhysicalMemory(); + + LongLong GetProcessId(); + + // Retrieve memory information in kib + LongLong GetHostMemoryTotal(); + LongLong GetHostMemoryAvailable(const char *envVarName); + LongLong GetHostMemoryUsed(); + + LongLong GetProcMemoryAvailable( + const char *hostLimitEnvVarName, + const char *procLimitEnvVarName); + LongLong GetProcMemoryUsed(); + + // enable/disable stack trace signal handler. + static + void SetStackTraceOnError(int enable); /** Run the different checks */ void RunCPUCheck(); @@ -159,6 +259,7 @@ public: kwsys_stl::string ProcessorName; kwsys_stl::string Vendor; kwsys_stl::string SerialNumber; + kwsys_stl::string ModelName; } ID; typedef struct tagCPUPowerManagement @@ -249,7 +350,7 @@ protected: // For Mac bool ParseSysCtl(); - void CallSwVers(); + int CallSwVers(const char *arg, kwsys_stl::string &ver); void TrimNewline(kwsys_stl::string&); kwsys_stl::string ExtractValueFromSysCtl(const char* word); kwsys_stl::string SysCtlBuffer; @@ -281,7 +382,7 @@ protected: kwsys_stl::string Hostname; kwsys_stl::string OSRelease; kwsys_stl::string OSVersion; - kwsys_stl::string OSPlatform; + kwsys_stl::string OSPlatform; }; @@ -320,6 +421,11 @@ kwsys_stl::string SystemInformation::GetModelID() return this->Implementation->GetModelID(); } +kwsys_stl::string SystemInformation::GetModelName() +{ + return this->Implementation->GetModelName(); +} + kwsys_stl::string SystemInformation::GetSteppingCode() { return this->Implementation->GetSteppingCode(); @@ -365,6 +471,37 @@ bool SystemInformation::DoesCPUSupportFeature(long int i) return this->Implementation->DoesCPUSupportFeature(i); } +kwsys_stl::string SystemInformation::GetCPUDescription() +{ + kwsys_ios::ostringstream oss; + 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; +} + const char * SystemInformation::GetOSName() { return this->Implementation->GetOSName(); @@ -375,6 +512,13 @@ const char * SystemInformation::GetHostname() return this->Implementation->GetHostname(); } +kwsys_stl::string SystemInformation::GetFullyQualifiedDomainName() +{ + kwsys_stl::string fqdn; + this->Implementation->GetFullyQualifiedDomainName(fqdn); + return fqdn; +} + const char * SystemInformation::GetOSRelease() { return this->Implementation->GetOSRelease(); @@ -390,6 +534,46 @@ const char * SystemInformation::GetOSPlatform() return this->Implementation->GetOSPlatform(); } +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() +{ + kwsys_ios::ostringstream oss; + oss + << this->GetOSName() + << " " + << this->GetOSRelease() + << " " + << this->GetOSVersion(); + + return oss.str(); +} + bool SystemInformation::Is64Bits() { return this->Implementation->Is64Bits(); @@ -431,6 +615,66 @@ size_t SystemInformation::GetAvailablePhysicalMemory() return this->Implementation->GetAvailablePhysicalMemory(); } +kwsys_stl::string SystemInformation::GetMemoryDescription( + const char *hostLimitEnvVarName, + const char *procLimitEnvVarName) +{ + kwsys_ios::ostringstream oss; + oss + << "Host Total: " + << iostreamLongLong(this->GetHostMemoryTotal()) + << " KiB, Host Available: " + << iostreamLongLong(this->GetHostMemoryAvailable(hostLimitEnvVarName)) + << " KiB, Process Available: " + << iostreamLongLong( + this->GetProcMemoryAvailable(hostLimitEnvVarName,procLimitEnvVarName)) + << " KiB"; + return oss.str(); +} + +// host memory info in units of KiB. +SystemInformation::LongLong SystemInformation::GetHostMemoryTotal() +{ + return this->Implementation->GetHostMemoryTotal(); +} + +SystemInformation::LongLong +SystemInformation::GetHostMemoryAvailable(const char *hostLimitEnvVarName) +{ + 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(); +} + +SystemInformation::LongLong SystemInformation::GetProcessId() +{ + return this->Implementation->GetProcessId(); +} + +void SystemInformation::SetStackTraceOnError(int enable) +{ + SystemInformationImplementation::SetStackTraceOnError(enable); +} + /** Run the different checks */ void SystemInformation::RunCPUCheck() { @@ -501,22 +745,365 @@ void SystemInformation::RunMemoryCheck() #define HT_CANNOT_DETECT 4 // EDX[28] Bit 28 is set if HT is supported -#define HT_BIT 0x10000000 +#define HT_BIT 0x10000000 // EAX[11:8] Bit 8-11 contains family processor ID. #define FAMILY_ID 0x0F00 -#define PENTIUM4_ID 0x0F00 +#define PENTIUM4_ID 0x0F00 // EAX[23:20] Bit 20-23 contains extended family processor ID -#define EXT_FAMILY_ID 0x0F00000 +#define EXT_FAMILY_ID 0x0F00000 // EBX[23:16] Bit 16-23 in ebx contains the number of logical -#define NUM_LOGICAL_BITS 0x00FF0000 -// processors per physical processor when execute cpuid with +#define NUM_LOGICAL_BITS 0x00FF0000 +// processors per physical processor when execute cpuid with // eax set to 1 -// EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique -#define INITIAL_APIC_ID_BITS 0xFF000000 +// EBX[31:24] Bits 24-31 (8 bits) return the 8-bit unique +#define INITIAL_APIC_ID_BITS 0xFF000000 // initial APIC ID for the processor this code is running on. // Default value = 0xff if HT is not supported +// Hide implementation details in an anonymous namespace. +namespace { +// ***************************************************************************** +#if defined(__linux) || defined(__APPLE__) +int LoadLines( + FILE *file, + 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'}; + while (!feof(file) && !ferror(file)) + { + errno=0; + if (fgets(buf,bufSize,file) == 0) + { + if (ferror(file) && (errno==EINTR)) + { + clearerr(file); + } + continue; + } + lines.push_back(buf); + ++nRead; + } + if (ferror(file)) + { + return 0; + } + return nRead; +} + +# if defined(__linux) +// ***************************************************************************** +int LoadLines( + const char *fileName, + kwsys_stl::vector<kwsys_stl::string> &lines) +{ + FILE *file=fopen(fileName,"r"); + if (file==0) + { + return 0; + } + int nRead=LoadLines(file,lines); + fclose(file); + return nRead; +} +# endif + +// **************************************************************************** +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) + { + size_t at=lines[i].find(name); + if (at==kwsys_stl::string::npos) + { + continue; + } + kwsys_ios::istringstream is(lines[i].substr(at+name.size())); + is >> value; + return 0; + } + return -1; +} +#endif + +#if defined(__linux) +// **************************************************************************** +template<typename T> +int GetFieldsFromFile( + const char *fileName, + const char **fieldNames, + T *values) +{ + kwsys_stl::vector<kwsys_stl::string> fields; + if (!LoadLines(fileName,fields)) + { + return -1; + } + 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> +int GetFieldFromFile( + const char *fileName, + const char *fieldName, + T &value) +{ + const char *fieldNames[2]={fieldName,NULL}; + T values[1]={T(0)}; + int ierr=GetFieldsFromFile(fileName,fieldNames,values); + if (ierr) + { + return ierr; + } + value=values[0]; + return 0; +} +#endif + +// **************************************************************************** +#if defined(__APPLE__) +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; + } + return 0; +} +#endif + +// **************************************************************************** +#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) +void StacktraceSignalHandler( + int sigNo, + siginfo_t *sigInfo, + void * /*sigContext*/) +{ +#if defined(__linux) || defined(__APPLE__) + kwsys_ios::ostringstream oss; + oss + << "=========================================================" << kwsys_ios::endl + << "Process id " << getpid() << " "; + switch (sigNo) + { + case SIGFPE: + oss << "Caught SIGFPE "; + switch (sigInfo->si_code) + { +# if defined(FPE_INTDIV) + case FPE_INTDIV: + oss << "integer division by zero"; + break; +# endif + +# if defined(FPE_INTOVF) + case FPE_INTOVF: + oss << "integer overflow"; + break; +# endif + + case FPE_FLTDIV: + oss << "floating point divide by zero"; + break; + + case FPE_FLTOVF: + oss << "floating point overflow"; + break; + + case FPE_FLTUND: + oss << "floating point underflow"; + break; + + case FPE_FLTRES: + oss << "floating point inexact result"; + break; + + case FPE_FLTINV: + oss << "floating point invalid operation"; + break; + +#if defined(FPE_FLTSUB) + case FPE_FLTSUB: + oss << "floating point subscript out of range"; + break; +#endif + + default: + oss << "code " << sigInfo->si_code; + break; + } + break; + + case SIGSEGV: + oss << "Caught SIGSEGV "; + switch (sigInfo->si_code) + { + case SEGV_MAPERR: + oss << "address not mapped to object"; + break; + + case SEGV_ACCERR: + oss << "invalid permission for mapped object"; + break; + + default: + oss << "code " << sigInfo->si_code; + break; + } + break; + + case SIGINT: + oss << "Caught SIGTERM"; + break; + + case SIGTERM: + oss << "Caught SIGTERM"; + break; + + case SIGBUS: + oss << "Caught SIGBUS type "; + switch (sigInfo->si_code) + { + case BUS_ADRALN: + oss << "invalid address alignment"; + break; + +# if defined(BUS_ADRERR) + case BUS_ADRERR: + oss << "non-exestent physical address"; + break; +# endif + +# if defined(BUS_OBJERR) + case BUS_OBJERR: + oss << "object specific hardware error"; + break; +# endif + + default: + oss << "code " << sigInfo->si_code; + break; + } + break; + + case SIGILL: + oss << "Caught SIGILL "; + switch (sigInfo->si_code) + { + case ILL_ILLOPC: + oss << "illegal opcode"; + break; + +# if defined(ILL_ILLOPN) + case ILL_ILLOPN: + oss << "illegal operand"; + break; +# endif + +# if defined(ILL_ILLADR) + case ILL_ILLADR: + oss << "illegal addressing mode."; + break; +# endif + + case ILL_ILLTRP: + oss << "illegal trap"; + + case ILL_PRVOPC: + oss << "privileged opcode"; + break; + +# if defined(ILL_PRVREG) + case ILL_PRVREG: + oss << "privileged register"; + break; +# endif + +# if defined(ILL_COPROC) + case ILL_COPROC: + oss << "co-processor error"; + break; +# endif + +# if defined(ILL_BADSTK) + case ILL_BADSTK: + oss << "internal stack error"; + break; +# endif + + default: + oss << "code " << sigInfo->si_code; + break; + } + break; + + default: + oss << "Caught " << sigNo << " code " << sigInfo->si_code; + break; + } + oss << kwsys_ios::endl; +#if defined(KWSYS_SYSTEMINFORMATION_HAVE_BACKTRACE) + oss << "Program Stack:" << kwsys_ios::endl; + void *stackSymbols[128]; + int n=backtrace(stackSymbols,128); + char **stackText=backtrace_symbols(stackSymbols,n); + for (int i=0; i<n; ++i) + { + oss << " " << stackText[i] << kwsys_ios::endl; + } +#endif + oss + << "=========================================================" << kwsys_ios::endl; + kwsys_ios::cerr << oss.str() << kwsys_ios::endl; + abort(); +#else + // avoid warning C4100 + (void)sigNo; + (void)sigInfo; +#endif +} +#endif +} // anonymous namespace SystemInformationImplementation::SystemInformationImplementation() { @@ -573,13 +1160,13 @@ void SystemInformationImplementation::RunCPUCheck() if (supportsCPUID) { // Retrieve cache information. - if (!RetrieveCPUCacheDetails()) + if (!RetrieveCPUCacheDetails()) { RetrieveClassicalCPUCacheDetails(); } // Retrieve the extended CPU details. - if (!RetrieveExtendedCPUIdentity()) + if (!RetrieveExtendedCPUIdentity()) { RetrieveClassicalCPUIdentity(); } @@ -641,9 +1228,145 @@ const char * SystemInformationImplementation::GetOSName() /** Get the hostname */ const char* SystemInformationImplementation::GetHostname() { + 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 + } return this->Hostname.c_str(); } +/** 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; + +#elif defined(KWSYS_SYSTEMINFORMATION_IMPLEMENT_FQDN) + // 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 + // 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. + + 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) + { + int fam=ifa->ifa_addr->sa_family; + if ((fam==AF_INET) || (fam==AF_INET6)) + { + char host[NI_MAXHOST]={'\0'}; + + int addrlen + = (fam==AF_INET?sizeof(struct sockaddr_in):sizeof(struct sockaddr_in6)); + + ierr=getnameinfo( + ifa->ifa_addr, + addrlen, + host, + NI_MAXHOST, + NULL, + 0, + NI_NAMEREQD); + if (ierr) + { + // don't report the failure now since we may succeed on another + // interface. If all attempts fail then return the failure code. + 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. */ + fqdn=this->GetHostname(); + return -1; +#endif +} + /** Get the OS release */ const char* SystemInformationImplementation::GetOSRelease() { @@ -721,9 +1444,15 @@ kwsys_stl::string SystemInformationImplementation::GetModelID() return str.str(); } +// Return the model name of CPU present */ +kwsys_stl::string SystemInformationImplementation::GetModelName() +{ + return this->ChipID.ModelName; +} + /** Return the stepping code of the CPU present. */ kwsys_stl::string SystemInformationImplementation::GetSteppingCode() -{ +{ kwsys_ios::ostringstream str; str << this->ChipID.Revision; return str.str(); @@ -734,8 +1463,8 @@ const char * SystemInformationImplementation::GetExtendedProcessorName() { return this->ChipID.ProcessorName.c_str(); } - -/** Return the serial number of the processor + +/** Return the serial number of the processor * in hexadecimal: xxxx-xxxx-xxxx-xxxx-xxxx-xxxx. */ const char * SystemInformationImplementation::GetProcessorSerialNumber() { @@ -870,7 +1599,7 @@ void SystemInformationImplementation::Delay(unsigned int uiMS) QueryPerformanceCounter (&StartCounter); do { - // Get the ending position of the counter. + // Get the ending position of the counter. QueryPerformanceCounter (&EndCounter); } while (EndCounter.QuadPart - StartCounter.QuadPart < x); #endif @@ -893,7 +1622,7 @@ bool SystemInformationImplementation::DoesCPUSupportCPUID() push ecx push edx #endif - ; <<CPUID>> + ; <<CPUID>> mov eax, 0 CPUID_INSTRUCTION @@ -905,7 +1634,7 @@ bool SystemInformationImplementation::DoesCPUSupportCPUID() #endif } } - __except(1) + __except(1) { // Stop the class from trying to use CPUID again! return false; @@ -939,7 +1668,7 @@ bool SystemInformationImplementation::RetrieveCPUFeatures() push ecx push edx #endif - ; <<CPUID>> + ; <<CPUID>> ; 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 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID ; edx: CPU feature flags @@ -956,7 +1685,7 @@ bool SystemInformationImplementation::RetrieveCPUFeatures() #endif } } - __except(1) + __except(1) { return false; } @@ -977,39 +1706,39 @@ bool SystemInformationImplementation::RetrieveCPUFeatures() // Retrieve extended SSE capabilities if SSE is available. if (this->Features.HasSSE) { - + // Attempt to __try some SSE FP instructions. - __try + __try { // Perform: orps xmm0, xmm0 - _asm + _asm { _emit 0x0f _emit 0x56 - _emit 0xc0 + _emit 0xc0 } // SSE FP capable processor. this->Features.HasSSEFP = true; - } - __except(1) + } + __except(1) { // bad instruction - processor or OS cannot handle SSE FP. this->Features.HasSSEFP = false; } - } - else + } + else { // Set the advanced SSE capabilities to not available. this->Features.HasSSEFP = false; } // Retrieve Intel specific extended features. - if (this->ChipManufacturer == Intel) + if (this->ChipManufacturer == Intel) { this->Features.ExtendedFeatures.SupportsHyperthreading = ((localCPUFeatures & 0x10000000) != 0); // Intel specific: Hyperthreading --> Bit 28 this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = (this->Features.ExtendedFeatures.SupportsHyperthreading) ? ((localCPUAdvanced & 0x00FF0000) >> 16) : 1; - + if ((this->Features.ExtendedFeatures.SupportsHyperthreading) && (this->Features.HasAPIC)) { // Retrieve APIC information if there is one present. @@ -1054,9 +1783,9 @@ bool SystemInformationImplementation::RetrieveCPUIdentity() int localCPUSignature; // Use assembly to detect CPUID information... - __try + __try { - _asm + _asm { #ifdef CPUID_AWARE_COMPILER ; we must push/pop the registers <<CPUID>> writes to, as the @@ -1078,7 +1807,7 @@ bool SystemInformationImplementation::RetrieveCPUIdentity() mov localCPUVendor[1 * TYPE int], edx mov localCPUVendor[2 * TYPE int], ecx - ; <<CPUID>> + ; <<CPUID>> ; 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 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID ; edx: CPU feature flags @@ -1094,7 +1823,7 @@ bool SystemInformationImplementation::RetrieveCPUIdentity() #endif } } - __except(1) + __except(1) { return false; } @@ -1133,12 +1862,12 @@ bool SystemInformationImplementation::RetrieveCPUCacheDetails() int L2Cache[4] = { 0, 0, 0, 0 }; // Check to see if what we are about to do is supported... - if (RetrieveCPUExtendedLevelSupport (0x80000005)) + if (RetrieveCPUExtendedLevelSupport (0x80000005)) { // Use assembly to retrieve the L1 cache information ... - __try + __try { - _asm + _asm { #ifdef CPUID_AWARE_COMPILER ; we must push/pop the registers <<CPUID>> writes to, as the @@ -1169,27 +1898,27 @@ bool SystemInformationImplementation::RetrieveCPUCacheDetails() #endif } } - __except(1) + __except(1) { return false; } // Save the L1 data cache size (in KB) from ecx: bits 31..24 as well as data cache size from edx: bits 31..24. this->Features.L1CacheSize = ((L1Cache[2] & 0xFF000000) >> 24); this->Features.L1CacheSize += ((L1Cache[3] & 0xFF000000) >> 24); - } - else + } + else { // Store -1 to indicate the cache could not be queried. this->Features.L1CacheSize = -1; } // Check to see if what we are about to do is supported... - if (RetrieveCPUExtendedLevelSupport (0x80000006)) + if (RetrieveCPUExtendedLevelSupport (0x80000006)) { // Use assembly to retrieve the L2 cache information ... - __try + __try { - _asm + _asm { #ifdef CPUID_AWARE_COMPILER ; we must push/pop the registers <<CPUID>> writes to, as the @@ -1220,19 +1949,19 @@ bool SystemInformationImplementation::RetrieveCPUCacheDetails() #endif } } - __except(1) + __except(1) { return false; } // Save the L2 unified cache size (in KB) from ecx: bits 31..16. this->Features.L2CacheSize = ((L2Cache[2] & 0xFFFF0000) >> 16); - } + } else { // Store -1 to indicate the cache could not be queried. this->Features.L2CacheSize = -1; } - + // Define L3 as being not present as we cannot test for it. this->Features.L3CacheSize = -1; @@ -1294,10 +2023,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails() int bob = ((TLBCacheData[0] & 0x00FF0000) >> 16); (void)bob; // Process the returned TLB and cache information. - for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++) + for (int nCounter = 0; nCounter < TLBCACHE_INFO_UNITS; nCounter ++) { // First of all - decide which unit we are dealing with. - switch (nCounter) + switch (nCounter) { // eax: bits 8..15 : bits 16..23 : bits 24..31 case 0: TLBCacheUnit = ((TLBCacheData[0] & 0x0000FF00) >> 8); break; @@ -1327,7 +2056,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails() } // Now process the resulting unit to see what it means.... - switch (TLBCacheUnit) + switch (TLBCacheUnit) { case 0x00: break; case 0x01: STORE_TLBCACHE_INFO (TLBCode, 4); break; @@ -1383,7 +2112,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails() case 0x90: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only case 0x96: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only case 0x9b: STORE_TLBCACHE_INFO (TLBCode, 262144); break; // <-- FIXME: IA-64 Only - + // Default case - an error has occured. default: return false; } @@ -1394,47 +2123,47 @@ bool SystemInformationImplementation::RetrieveClassicalCPUCacheDetails() } while ((TLBCacheData[0] & 0x000000FF) > TLBPassCounter); // Ok - we now have the maximum TLB, L1, L2, and L3 sizes... - if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1)) + if ((L1Code == -1) && (L1Data == -1) && (L1Trace == -1)) { this->Features.L1CacheSize = -1; } - else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1)) + else if ((L1Code == -1) && (L1Data == -1) && (L1Trace != -1)) { this->Features.L1CacheSize = L1Trace; } - else if ((L1Code != -1) && (L1Data == -1)) + else if ((L1Code != -1) && (L1Data == -1)) { this->Features.L1CacheSize = L1Code; } - else if ((L1Code == -1) && (L1Data != -1)) + else if ((L1Code == -1) && (L1Data != -1)) { this->Features.L1CacheSize = L1Data; } - else if ((L1Code != -1) && (L1Data != -1)) + else if ((L1Code != -1) && (L1Data != -1)) { this->Features.L1CacheSize = L1Code + L1Data; } - else + else { this->Features.L1CacheSize = -1; } // Ok - we now have the maximum TLB, L1, L2, and L3 sizes... - if (L2Unified == -1) + if (L2Unified == -1) { this->Features.L2CacheSize = -1; } - else + else { this->Features.L2CacheSize = L2Unified; } // Ok - we now have the maximum TLB, L1, L2, and L3 sizes... - if (L3Unified == -1) + if (L3Unified == -1) { this->Features.L3CacheSize = -1; } - else + else { this->Features.L3CacheSize = L3Unified; } @@ -1526,19 +2255,19 @@ bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed() // Attempt to get a starting tick count. QueryPerformanceCounter (&liStart); - __try + __try { - _asm + _asm { mov eax, 0x80000000 mov ebx, CLASSICAL_CPU_FREQ_LOOP - Timer_Loop: + Timer_Loop: bsf ecx,eax dec ebx jnz Timer_Loop - } + } } - __except(1) + __except(1) { return false; } @@ -1551,22 +2280,22 @@ bool SystemInformationImplementation::RetrieveClassicalCPUClockSpeed() dDifference = (((double) liEnd.QuadPart - (double) liStart.QuadPart) / (double) liCountsPerSecond.QuadPart); // Calculate the clock speed. - if (this->ChipID.Family == 3) + if (this->ChipID.Family == 3) { // 80386 processors.... Loop time is 115 cycles! dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 115) / dDifference) / 1000000); - } - else if (this->ChipID.Family == 4) + } + else if (this->ChipID.Family == 4) { // 80486 processors.... Loop time is 47 cycles! dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 47) / dDifference) / 1000000); - } - else if (this->ChipID.Family == 5) + } + else if (this->ChipID.Family == 5) { // Pentium processors.... Loop time is 43 cycles! dFrequency = (((CLASSICAL_CPU_FREQ_LOOP * 43) / dDifference) / 1000000); } - + // Save the clock speed. this->Features.CPUSpeed = (int) dFrequency; @@ -1583,7 +2312,7 @@ bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULev { int MaxCPUExtendedLevel = 0; - // The extended CPUID is supported by various vendors starting with the following CPU models: + // The extended CPUID is supported by various vendors starting with the following CPU models: // // Manufacturer & Chip Name | Family Model Revision // @@ -1596,27 +2325,27 @@ bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULev // // We check to see if a supported processor is present... - if (this->ChipManufacturer == AMD) + if (this->ChipManufacturer == AMD) { if (this->ChipID.Family < 5) return false; if ((this->ChipID.Family == 5) && (this->ChipID.Model < 6)) return false; - } - else if (this->ChipManufacturer == Cyrix) + } + else if (this->ChipManufacturer == Cyrix) { if (this->ChipID.Family < 5) return false; if ((this->ChipID.Family == 5) && (this->ChipID.Model < 4)) return false; if ((this->ChipID.Family == 6) && (this->ChipID.Model < 5)) return false; - } - else if (this->ChipManufacturer == IDT) + } + else if (this->ChipManufacturer == IDT) { if (this->ChipID.Family < 5) return false; if ((this->ChipID.Family == 5) && (this->ChipID.Model < 8)) return false; - } - else if (this->ChipManufacturer == Transmeta) + } + else if (this->ChipManufacturer == Transmeta) { if (this->ChipID.Family < 5) return false; - } - else if (this->ChipManufacturer == Intel) + } + else if (this->ChipManufacturer == Intel) { if (this->ChipID.Family < 0xf) { @@ -1638,7 +2367,7 @@ bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULev push ecx push edx #endif - ; <<CPUID>> + ; <<CPUID>> ; eax = 0x80000000 --> eax: maximum supported extended level mov eax,0x80000000 CPUID_INSTRUCTION @@ -1652,7 +2381,7 @@ bool SystemInformationImplementation::RetrieveCPUExtendedLevelSupport(int CPULev #endif } } - __except(1) + __except(1) { return false; } @@ -1677,7 +2406,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUFeatures() { // Check that we are not using an Intel processor as it does not support this. - if (this->ChipManufacturer == Intel) + if (this->ChipManufacturer == Intel) { return false; } @@ -1692,9 +2421,9 @@ bool SystemInformationImplementation::RetrieveExtendedCPUFeatures() int localCPUExtendedFeatures = 0; // Use assembly to detect CPUID information... - __try + __try { - _asm + _asm { #ifdef CPUID_AWARE_COMPILER ; we must push/pop the registers <<CPUID>> writes to, as the @@ -1705,7 +2434,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUFeatures() push ecx push edx #endif - ; <<CPUID>> + ; <<CPUID>> ; 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 ; ebx: 31..24 - default APIC ID, 23..16 - logical processsor ID, 15..8 - CFLUSH chunk size , 7..0 - brand ID ; edx: CPU feature flags @@ -1721,7 +2450,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUFeatures() #endif } } - __except(1) + __except(1) { return false; } @@ -1731,15 +2460,15 @@ bool SystemInformationImplementation::RetrieveExtendedCPUFeatures() this->Features.ExtendedFeatures.Has3DNowPlus = ((localCPUExtendedFeatures & 0x40000000) != 0); // 3DNow+ Present -- > Bit 30. this->Features.ExtendedFeatures.HasSSEMMX = ((localCPUExtendedFeatures & 0x00400000) != 0); // SSE MMX Present --> Bit 22. this->Features.ExtendedFeatures.SupportsMP = ((localCPUExtendedFeatures & 0x00080000) != 0); // MP Capable -- > Bit 19. - + // Retrieve AMD specific extended features. - if (this->ChipManufacturer == AMD) + if (this->ChipManufacturer == AMD) { this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x00400000) != 0); // AMD specific: MMX-SSE --> Bit 22 } // Retrieve Cyrix specific extended features. - if (this->ChipManufacturer == Cyrix) + if (this->ChipManufacturer == Cyrix) { this->Features.ExtendedFeatures.HasMMXPlus = ((localCPUExtendedFeatures & 0x01000000) != 0); // Cyrix specific: Extended MMX --> Bit 24 } @@ -1794,7 +2523,7 @@ bool SystemInformationImplementation::RetrieveProcessorSerialNumber() #endif } } - __except(1) + __except(1) { return false; } @@ -1851,12 +2580,12 @@ bool SystemInformationImplementation::RetrieveCPUPowerManagement() push ecx push edx #endif - ; <<CPUID>> + ; <<CPUID>> ; eax = 0x80000007 --> edx: get processor power management mov eax,0x80000007 CPUID_INSTRUCTION mov localCPUPowerManagement, edx - + #ifdef CPUID_AWARE_COMPILER pop edx pop ecx @@ -1865,7 +2594,7 @@ bool SystemInformationImplementation::RetrieveCPUPowerManagement() #endif } } - __except(1) + __except(1) { return false; } @@ -1918,7 +2647,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUIdentity() push ecx push edx #endif - ; <<CPUID>> + ; <<CPUID>> ; eax = 0x80000002 --> eax, ebx, ecx, edx: get processor name string (part 1) mov eax,0x80000002 CPUID_INSTRUCTION @@ -1927,7 +2656,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUIdentity() mov CPUExtendedIdentity[2 * TYPE int], ecx mov CPUExtendedIdentity[3 * TYPE int], edx - ; <<CPUID>> + ; <<CPUID>> ; eax = 0x80000003 --> eax, ebx, ecx, edx: get processor name string (part 2) mov eax,0x80000003 CPUID_INSTRUCTION @@ -1936,7 +2665,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUIdentity() mov CPUExtendedIdentity[6 * TYPE int], ecx mov CPUExtendedIdentity[7 * TYPE int], edx - ; <<CPUID>> + ; <<CPUID>> ; eax = 0x80000004 --> eax, ebx, ecx, edx: get processor name string (part 3) mov eax,0x80000004 CPUID_INSTRUCTION @@ -1953,7 +2682,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUIdentity() #endif } } - __except(1) + __except(1) { return false; } @@ -1974,6 +2703,7 @@ bool SystemInformationImplementation::RetrieveExtendedCPUIdentity() memcpy (&(nbuf[44]), &(CPUExtendedIdentity[11]), sizeof (int)); nbuf[48] = '\0'; this->ChipID.ProcessorName = nbuf; + this->ChipID.ModelName = nbuf; // Because some manufacturers have leading white space - we have to post-process the name. SystemInformationStripLeadingSpace(this->ChipID.ProcessorName); @@ -1988,13 +2718,13 @@ bool SystemInformationImplementation::RetrieveExtendedCPUIdentity() bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() { // Start by decided which manufacturer we are using.... - switch (this->ChipManufacturer) + switch (this->ChipManufacturer) { case Intel: // Check the family / model / revision to determine the CPU ID. switch (this->ChipID.Family) { case 3: - this->ChipID.ProcessorName = "Newer i80386 family"; + this->ChipID.ProcessorName = "Newer i80386 family"; break; case 4: switch (this->ChipID.Model) { @@ -2011,7 +2741,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "P5 A-Step"; break; case 1: this->ChipID.ProcessorName = "P5"; break; @@ -2024,7 +2754,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 6: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "P6 A-Step"; break; case 1: this->ChipID.ProcessorName = "P6"; break; @@ -2044,10 +2774,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case 0xf: // Check the extended family bits... - switch (this->ChipID.ExtendedFamily) + switch (this->ChipID.ExtendedFamily) { case 0: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "Pentium IV (0.18 micron)"; break; case 1: this->ChipID.ProcessorName = "Pentium IV (0.18 micron)"; break; @@ -2070,10 +2800,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() case AMD: // Check the family / model / revision to determine the CPU ID. - switch (this->ChipID.Family) + switch (this->ChipID.Family) { case 4: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 3: this->ChipID.ProcessorName = "80486DX2"; break; case 7: this->ChipID.ProcessorName = "80486DX2 WriteBack"; break; @@ -2085,7 +2815,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "SSA5 (PR75, PR90 = PR100)"; break; case 1: this->ChipID.ProcessorName = "5k86 (PR120 = PR133)"; break; @@ -2100,7 +2830,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 6: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 1: this->ChipID.ProcessorName = "Athlon- (0.25 micron)"; break; case 2: this->ChipID.ProcessorName = "Athlon- (0.18 micron)"; break; @@ -2108,9 +2838,9 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() case 4: this->ChipID.ProcessorName = "Athlon- (Thunderbird core)"; break; case 6: this->ChipID.ProcessorName = "Athlon- (Palomino core)"; break; case 7: this->ChipID.ProcessorName = "Duron- (Morgan core)"; break; - case 8: + case 8: if (this->Features.ExtendedFeatures.SupportsMP) - this->ChipID.ProcessorName = "Athlon - MP (Thoroughbred core)"; + this->ChipID.ProcessorName = "Athlon - MP (Thoroughbred core)"; else this->ChipID.ProcessorName = "Athlon - XP (Thoroughbred core)"; break; default: this->ChipID.ProcessorName = "Unknown K7 family"; return false; @@ -2123,10 +2853,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case Transmeta: - switch (this->ChipID.Family) - { + switch (this->ChipID.Family) + { case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 4: this->ChipID.ProcessorName = "Crusoe TM3x00 and TM5x00"; break; default: this->ChipID.ProcessorName = "Unknown Crusoe family"; return false; @@ -2139,10 +2869,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case Rise: - switch (this->ChipID.Family) - { + switch (this->ChipID.Family) + { case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "mP6 (0.25 micron)"; break; case 2: this->ChipID.ProcessorName = "mP6 (0.18 micron)"; break; @@ -2156,10 +2886,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case UMC: - switch (this->ChipID.Family) - { + switch (this->ChipID.Family) + { case 4: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 1: this->ChipID.ProcessorName = "U5D"; break; case 2: this->ChipID.ProcessorName = "U5S"; break; @@ -2173,10 +2903,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case IDT: - switch (this->ChipID.Family) - { + switch (this->ChipID.Family) + { case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 4: this->ChipID.ProcessorName = "C6"; break; case 8: this->ChipID.ProcessorName = "C2"; break; @@ -2185,7 +2915,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 6: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 6: this->ChipID.ProcessorName = "VIA Cyrix III - Samuel"; break; default: this->ChipID.ProcessorName = "Unknown IDT\\Centaur family"; return false; @@ -2198,10 +2928,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case Cyrix: - switch (this->ChipID.Family) - { + switch (this->ChipID.Family) + { case 4: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 4: this->ChipID.ProcessorName = "MediaGX GX = GXm"; break; case 9: this->ChipID.ProcessorName = "5x86"; break; @@ -2209,7 +2939,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 2: this->ChipID.ProcessorName = "Cx6x86"; break; case 4: this->ChipID.ProcessorName = "MediaGX GXm"; break; @@ -2217,7 +2947,7 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() } break; case 6: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "6x86MX"; break; case 5: this->ChipID.ProcessorName = "Cyrix M2 Core"; break; @@ -2234,10 +2964,10 @@ bool SystemInformationImplementation::RetrieveClassicalCPUIdentity() break; case NexGen: - switch (this->ChipID.Family) - { + switch (this->ChipID.Family) + { case 5: - switch (this->ChipID.Model) + switch (this->ChipID.Model) { case 0: this->ChipID.ProcessorName = "Nx586 or Nx586FPU"; break; default: this->ChipID.ProcessorName = "Unknown NexGen family"; return false; @@ -2287,12 +3017,12 @@ int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile() kwsys_stl::string buffer; FILE *fd = fopen("/proc/cpuinfo", "r" ); - if ( !fd ) + if ( !fd ) { kwsys_ios::cout << "Problem opening /proc/cpuinfo" << kwsys_ios::endl; return 0; } - + size_t fileSize = 0; while(!feof(fd)) { @@ -2336,13 +3066,13 @@ int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile() #else // __CYGWIN__ // does not have "physical id" entries, neither "cpu cores" - // this has to be fixed for hyper-threading. + // this has to be fixed for hyper-threading. kwsys_stl::string cpucount = this->ExtractValueFromCpuInfoFile(buffer,"cpu count"); this->NumberOfPhysicalCPU= this->NumberOfLogicalCPU = atoi(cpucount.c_str()); #endif - // gotta have one, and if this is 0 then we get a / by 0n + // gotta have one, and if this is 0 then we get a / by 0n // beter to have a bad answer than a crash if(this->NumberOfPhysicalCPU <= 0) { @@ -2358,15 +3088,18 @@ int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile() // Chip family this->ChipID.Family = atoi(this->ExtractValueFromCpuInfoFile(buffer,"cpu family").c_str()); - + // Chip Vendor this->ChipID.Vendor = this->ExtractValueFromCpuInfoFile(buffer,"vendor_id"); this->FindManufacturer(); - + // Chip Model this->ChipID.Model = atoi(this->ExtractValueFromCpuInfoFile(buffer,"model").c_str()); this->RetrieveClassicalCPUIdentity(); + // Chip Model Name + this->ChipID.ModelName = this->ExtractValueFromCpuInfoFile(buffer,"model name").c_str(); + // L1 Cache size kwsys_stl::string cacheSize = this->ExtractValueFromCpuInfoFile(buffer,"cache size"); pos = cacheSize.find(" KB"); @@ -2378,6 +3111,333 @@ int SystemInformationImplementation::RetreiveInformationFromCpuInfoFile() return 1; } +/** +Get total system RAM in units of KiB. +*/ +SystemInformation::LongLong +SystemInformationImplementation::GetHostMemoryTotal() +{ +#if defined(_WIN32) +# if defined(_MSC_VER) && _MSC_VER < 1300 + MEMORYSTATUS stat; + stat.dwLength = sizeof(stat); + GlobalMemoryStatus(&stat); + return stat.dwTotalPhys/1024; +# else + MEMORYSTATUSEX statex; + statex.dwLength=sizeof(statex); + GlobalMemoryStatusEx(&statex); + return statex.ullTotalPhys/1024; +# endif +#elif defined(__linux) + SystemInformation::LongLong memTotal=0; + int ierr=GetFieldFromFile("/proc/meminfo","MemTotal:",memTotal); + if (ierr) + { + return -1; + } + return memTotal; +#elif defined(__APPLE__) + uint64_t mem; + size_t len = sizeof(mem); + int ierr=sysctlbyname("hw.memsize", &mem, &len, NULL, 0); + if (ierr) + { + return -1; + } + return mem/1024; +#else + return 0; +#endif +} + +/** +Get total system RAM in units of KiB. This may differ from the +host total if a host-wide resource limit is applied. +*/ +SystemInformation::LongLong +SystemInformationImplementation::GetHostMemoryAvailable(const char *hostLimitEnvVarName) +{ + SystemInformation::LongLong memTotal=this->GetHostMemoryTotal(); + + // the following mechanism is provided for systems that + // apply resource limits across groups of processes. + // this is of use on certain SMP systems (eg. SGI UV) + // where the host has a large amount of ram but a given user's + // access to it is severly restricted. The system will + // apply a limit across a set of processes. Units are in KiB. + if (hostLimitEnvVarName) + { + const char *hostLimitEnvVarValue=getenv(hostLimitEnvVarName); + if (hostLimitEnvVarValue) + { + SystemInformation::LongLong hostLimit=atoLongLong(hostLimitEnvVarValue); + if (hostLimit>0) + { + memTotal=min(hostLimit,memTotal); + } + } + } + + return memTotal; +} + +/** +Get total system RAM in units of KiB. This may differ from the +host total if a per-process resource limit is applied. +*/ +SystemInformation::LongLong +SystemInformationImplementation::GetProcMemoryAvailable( + const char *hostLimitEnvVarName, + const char *procLimitEnvVarName) +{ + SystemInformation::LongLong memAvail + = this->GetHostMemoryAvailable(hostLimitEnvVarName); + + // the following mechanism is provide for systems where rlimits + // are not employed. Units are in KiB. + if (procLimitEnvVarName) + { + const char *procLimitEnvVarValue=getenv(procLimitEnvVarName); + if (procLimitEnvVarValue) + { + SystemInformation::LongLong procLimit=atoLongLong(procLimitEnvVarValue); + if (procLimit>0) + { + memAvail=min(procLimit,memAvail); + } + } + } + +#if defined(__linux) + int ierr; + ResourceLimitType rlim; + ierr=GetResourceLimit(RLIMIT_DATA,&rlim); + if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY)) + { + memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail); + } + + ierr=GetResourceLimit(RLIMIT_AS,&rlim); + if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY)) + { + memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail); + } +#elif defined(__APPLE__) + struct rlimit rlim; + int ierr; + ierr=getrlimit(RLIMIT_DATA,&rlim); + if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY)) + { + memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail); + } + + ierr=getrlimit(RLIMIT_RSS,&rlim); + if ((ierr==0) && (rlim.rlim_cur != RLIM_INFINITY)) + { + memAvail=min((SystemInformation::LongLong)rlim.rlim_cur/1024,memAvail); + } +#endif + + return memAvail; +} + +/** +Get RAM used by all processes in the host, in units of KiB. +*/ +SystemInformation::LongLong +SystemInformationImplementation::GetHostMemoryUsed() +{ +#if defined(_WIN32) +# if defined(_MSC_VER) && _MSC_VER < 1300 + MEMORYSTATUS stat; + stat.dwLength = sizeof(stat); + GlobalMemoryStatus(&stat); + return (stat.dwTotalPhys - stat.dwAvailPhys)/1024; +# else + MEMORYSTATUSEX statex; + statex.dwLength=sizeof(statex); + GlobalMemoryStatusEx(&statex); + return (statex.ullTotalPhys - statex.ullAvailPhys)/1024; +# endif +#elif defined(__linux) + const char *names[3]={"MemTotal:","MemFree:",NULL}; + SystemInformation::LongLong values[2]={SystemInformation::LongLong(0)}; + int ierr=GetFieldsFromFile("/proc/meminfo",names,values); + if (ierr) + { + return ierr; + } + SystemInformation::LongLong &memTotal=values[0]; + SystemInformation::LongLong &memFree=values[1]; + return memTotal - memFree; +#elif defined(__APPLE__) + SystemInformation::LongLong psz=getpagesize(); + if (psz<1) + { + return -1; + } + const char *names[4]={"Pages active:","Pages inactive:","Pages wired down:",NULL}; + SystemInformation::LongLong values[3]={SystemInformation::LongLong(0)}; + int ierr=GetFieldsFromCommand("vm_stat", names, values); + if (ierr) + { + return -1; + } + SystemInformation::LongLong &vmActive=values[0]; + SystemInformation::LongLong &vmInactive=values[1]; + SystemInformation::LongLong &vmWired=values[2]; + return ((vmActive+vmInactive+vmWired)*psz)/1024; +#else + return 0; +#endif +} + +/** +Get system RAM used by the process associated with the given +process id in units of KiB. +*/ +SystemInformation::LongLong +SystemInformationImplementation::GetProcMemoryUsed() +{ +#if defined(_WIN32) && defined(KWSYS_SYS_HAS_PSAPI) + long pid=GetCurrentProcessId(); + HANDLE hProc; + hProc=OpenProcess( + PROCESS_QUERY_INFORMATION|PROCESS_VM_READ, + false, + pid); + if (hProc==0) + { + return -1; + } + PROCESS_MEMORY_COUNTERS pmc; + int ok=GetProcessMemoryInfo(hProc,&pmc,sizeof(pmc)); + CloseHandle(hProc); + if (!ok) + { + return -2; + } + return pmc.WorkingSetSize/1024; +#elif defined(__linux) + SystemInformation::LongLong memUsed=0; + int ierr=GetFieldFromFile("/proc/self/status","VmRSS:",memUsed); + if (ierr) + { + return -1; + } + return memUsed; +#elif defined(__APPLE__) + SystemInformation::LongLong memUsed=0; + pid_t pid=getpid(); + kwsys_ios::ostringstream oss; + oss << "ps -o rss= -p " << pid; + FILE *file=popen(oss.str().c_str(),"r"); + if (file==0) + { + return -1; + } + oss.str(""); + while (!feof(file) && !ferror(file)) + { + char buf[256]={'\0'}; + errno=0; + size_t nRead=fread(buf,1,256,file); + if (ferror(file) && (errno==EINTR)) + { + clearerr(file); + } + if (nRead) oss << buf; + } + int ierr=ferror(file); + pclose(file); + if (ierr) + { + return -2; + } + kwsys_ios::istringstream iss(oss.str()); + iss >> memUsed; + return memUsed; +#else + return 0; +#endif +} + +/** +Get the process id of the running process. +*/ +SystemInformation::LongLong +SystemInformationImplementation::GetProcessId() +{ +#if defined(_WIN32) + return GetCurrentProcessId(); +#elif defined(__linux) || defined(__APPLE__) + return getpid(); +#else + return -1; +#endif +} + +/** +when set print stack trace in response to common signals. +*/ +void SystemInformationImplementation::SetStackTraceOnError(int enable) +{ +#if !defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) + static int saOrigValid=0; + static struct sigaction saSEGVOrig; + static struct sigaction saTERMOrig; + static struct sigaction saINTOrig; + static struct sigaction saILLOrig; + static struct sigaction saBUSOrig; + static struct sigaction saFPEOrig; + + if (enable && !saOrigValid) + { + // save the current actions + sigaction(SIGSEGV,0,&saSEGVOrig); + sigaction(SIGTERM,0,&saTERMOrig); + sigaction(SIGINT,0,&saINTOrig); + sigaction(SIGILL,0,&saILLOrig); + sigaction(SIGBUS,0,&saBUSOrig); + sigaction(SIGFPE,0,&saFPEOrig); + + // enable read, disable write + saOrigValid=1; + + // install ours + struct sigaction sa; + sa.sa_sigaction=(SigAction)StacktraceSignalHandler; + sa.sa_flags=SA_SIGINFO|SA_RESTART; + sigemptyset(&sa.sa_mask); + + sigaction(SIGSEGV,&sa,0); + sigaction(SIGTERM,&sa,0); + sigaction(SIGINT,&sa,0); + sigaction(SIGILL,&sa,0); + sigaction(SIGBUS,&sa,0); + sigaction(SIGFPE,&sa,0); + } + else + if (!enable && saOrigValid) + { + // restore previous actions + sigaction(SIGSEGV,&saSEGVOrig,0); + sigaction(SIGTERM,&saTERMOrig,0); + sigaction(SIGINT,&saINTOrig,0); + sigaction(SIGILL,&saILLOrig,0); + sigaction(SIGBUS,&saBUSOrig,0); + sigaction(SIGFPE,&saFPEOrig,0); + + // enable write, disable read + saOrigValid=0; + } +#else + // avoid warning C4100 + (void)enable; +#endif +} + /** Query for the memory status */ int SystemInformationImplementation::QueryMemory() { @@ -2388,13 +3448,13 @@ int SystemInformationImplementation::QueryMemory() #ifdef __CYGWIN__ return 0; #elif defined(_WIN32) -#if _MSC_VER < 1300 +# if defined(_MSC_VER) && _MSC_VER < 1300 MEMORYSTATUS ms; unsigned long tv, tp, av, ap; ms.dwLength = sizeof(ms); GlobalMemoryStatus(&ms); - #define MEM_VAL(value) dw##value -#else +# define MEM_VAL(value) dw##value +# else MEMORYSTATUSEX ms; DWORDLONG tv, tp, av, ap; ms.dwLength = sizeof(ms); @@ -2402,8 +3462,8 @@ int SystemInformationImplementation::QueryMemory() { return 0; } -#define MEM_VAL(value) ull##value -#endif +# define MEM_VAL(value) ull##value +# endif tv = ms.MEM_VAL(TotalVirtual); tp = ms.MEM_VAL(TotalPhys); av = ms.MEM_VAL(AvailVirtual); @@ -2418,12 +3478,12 @@ int SystemInformationImplementation::QueryMemory() unsigned long tp=0; unsigned long av=0; unsigned long ap=0; - + char buffer[1024]; // for reading lines - + int linuxMajor = 0; int linuxMinor = 0; - + // Find the Linux kernel version first struct utsname unameInfo; int errorFlag = uname(&unameInfo); @@ -2432,31 +3492,31 @@ int SystemInformationImplementation::QueryMemory() kwsys_ios::cout << "Problem calling uname(): " << strerror(errno) << kwsys_ios::endl; return 0; } - + if( unameInfo.release!=0 && strlen(unameInfo.release)>=3 ) { // release looks like "2.6.3-15mdk-i686-up-4GB" char majorChar=unameInfo.release[0]; char minorChar=unameInfo.release[2]; - + if( isdigit(majorChar) ) { linuxMajor=majorChar-'0'; } - + if( isdigit(minorChar) ) { linuxMinor=minorChar-'0'; } } - + FILE *fd = fopen("/proc/meminfo", "r" ); - if ( !fd ) + if ( !fd ) { kwsys_ios::cout << "Problem opening /proc/meminfo" << kwsys_ios::endl; return 0; } - + if( linuxMajor>=3 || ( (linuxMajor>=2) && (linuxMinor>=6) ) ) { // new /proc/meminfo format since kernel 2.6.x @@ -2499,7 +3559,7 @@ int SystemInformationImplementation::QueryMemory() else { // /proc/meminfo format for kernel older than 2.6.x - + unsigned long temp; unsigned long cachedMem; unsigned long buffersMem; @@ -2537,7 +3597,7 @@ int SystemInformationImplementation::QueryMemory() unsigned long ap=0; struct pst_static pst; struct pst_dynamic pdy; - + unsigned long ps = 0; if (pstat_getstatic(&pst, sizeof(pst), (size_t) 1, 0) != -1) { @@ -2559,35 +3619,34 @@ int SystemInformationImplementation::QueryMemory() #else return 0; #endif - - } /** */ -size_t SystemInformationImplementation::GetTotalVirtualMemory() -{ - return this->TotalVirtualMemory; +size_t SystemInformationImplementation::GetTotalVirtualMemory() +{ + return this->TotalVirtualMemory; } /** */ -size_t SystemInformationImplementation::GetAvailableVirtualMemory() -{ - return this->AvailableVirtualMemory; +size_t SystemInformationImplementation::GetAvailableVirtualMemory() +{ + return this->AvailableVirtualMemory; } -size_t SystemInformationImplementation::GetTotalPhysicalMemory() -{ - return this->TotalPhysicalMemory; +size_t SystemInformationImplementation::GetTotalPhysicalMemory() +{ + return this->TotalPhysicalMemory; } /** */ -size_t SystemInformationImplementation::GetAvailablePhysicalMemory() -{ - return this->AvailablePhysicalMemory; +size_t SystemInformationImplementation::GetAvailablePhysicalMemory() +{ + return this->AvailablePhysicalMemory; } /** Get Cycle differences */ -LongLong SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction, +SystemInformation::LongLong +SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayFunction, unsigned int uiParameter) { #if USE_ASM_INSTRUCTIONS @@ -2619,7 +3678,7 @@ LongLong SystemInformationImplementation::GetCyclesDifference (DELAY_FUNC DelayF mov eax1, esi ; eax2 = esi } } - __except(1) + __except(1) { return -1; } @@ -2642,7 +3701,7 @@ void SystemInformationImplementation::DelayOverhead(unsigned int uiMS) __int64 x; // Get the frequency of the high performance counter. - if(!QueryPerformanceFrequency (&Frequency)) + if(!QueryPerformanceFrequency (&Frequency)) { return; } @@ -2650,9 +3709,9 @@ void SystemInformationImplementation::DelayOverhead(unsigned int uiMS) // Get the starting position of the counter. QueryPerformanceCounter (&StartCounter); - + do { - // Get the ending position of the counter. + // Get the ending position of the counter. QueryPerformanceCounter (&EndCounter); } while (EndCounter.QuadPart - StartCounter.QuadPart == x); #endif @@ -2706,7 +3765,7 @@ unsigned int SystemInformationImplementation::IsHyperThreadingSupported() mov VendorId, ebx mov VendorId + 4, edx mov VendorId + 8, ecx - + mov eax, 1 // call cpuid with eax = 1 cpuid mov Regeax, eax // eax contains family processor type @@ -2743,7 +3802,7 @@ unsigned char SystemInformationImplementation::GetAPICId() unsigned int Regebx = 0; #if USE_ASM_INSTRUCTIONS - if (!this->IsHyperThreadingSupported()) + if (!this->IsHyperThreadingSupported()) { return static_cast<unsigned char>(-1); // HT not supported } // Logical processor = 1 @@ -2773,7 +3832,7 @@ int SystemInformationImplementation::CPUCount() // Number of physical processors in a non-Intel system // or in a 32-bit Intel system with Hyper-Threading technology disabled - this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors; + this->NumberOfPhysicalCPU = (unsigned char) info.dwNumberOfProcessors; if (this->IsHyperThreadingSupported()) { @@ -2789,7 +3848,7 @@ int SystemInformationImplementation::CPUCount() DWORD_PTR dwSystemAffinity; DWORD dwAffinityMask; - // Calculate the appropriate shifts and mask based on the + // Calculate the appropriate shifts and mask based on the // number of logical processors. unsigned int i = 1; unsigned char PHY_ID_MASK = 0xFF; @@ -2801,7 +3860,7 @@ int SystemInformationImplementation::CPUCount() PHY_ID_MASK <<= 1; // PHY_ID_SHIFT++; } - + hCurrentProcessHandle = GetCurrentProcess(); GetProcessAffinityMask(hCurrentProcessHandle, &dwProcessAffinity, &dwSystemAffinity); @@ -2829,8 +3888,8 @@ int SystemInformationImplementation::CPUCount() APIC_ID = GetAPICId(); LOG_ID = APIC_ID & ~PHY_ID_MASK; - - if (LOG_ID != 0) + + if (LOG_ID != 0) { HT_Enabled = 1; } @@ -2840,7 +3899,7 @@ int SystemInformationImplementation::CPUCount() } // Reset the processor affinity SetProcessAffinityMask(hCurrentProcessHandle, dwProcessAffinity); - + if (this->NumberOfLogicalCPU == 1) // Normal P4 : HT is disabled in hardware { StatusFlag = HT_DISABLED; @@ -2853,7 +3912,7 @@ int SystemInformationImplementation::CPUCount() this->NumberOfPhysicalCPU /= (this->NumberOfLogicalCPU); StatusFlag = HT_ENABLED; } - else + else { StatusFlag = HT_SUPPORTED_NOT_ENABLED; } @@ -2891,6 +3950,7 @@ unsigned int SystemInformationImplementation::GetNumberOfPhysicalCPU() bool SystemInformationImplementation::ParseSysCtl() { #if defined(__APPLE__) + char retBuf[128]; int err = 0; uint64_t value = 0; size_t len = sizeof(value); @@ -2901,9 +3961,10 @@ bool SystemInformationImplementation::ParseSysCtl() this->AvailablePhysicalMemory = 0; vm_statistics_data_t vmstat; mach_msg_type_number_t count = HOST_VM_INFO_COUNT; - if ( host_statistics(mach_host_self(), HOST_VM_INFO, + if ( host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t) &vmstat, &count) == KERN_SUCCESS ) { + len = sizeof(value); err = sysctlbyname("hw.pagesize", &value, &len, NULL, 0); int64_t available_memory = vmstat.free_count * value; this->AvailablePhysicalMemory = static_cast< size_t >( available_memory / 1048576 ); @@ -2914,7 +3975,7 @@ bool SystemInformationImplementation::ParseSysCtl() int mib[2] = { CTL_VM, VM_SWAPUSAGE }; size_t miblen = sizeof(mib) / sizeof(mib[0]); struct xsw_usage swap; - len = sizeof(struct xsw_usage); + len = sizeof(swap); err = sysctl(mib, miblen, &swap, &len, NULL, 0); if (err == 0) { @@ -2929,8 +3990,9 @@ bool SystemInformationImplementation::ParseSysCtl() // CPU Info len = sizeof(this->NumberOfPhysicalCPU); sysctlbyname("hw.physicalcpu", &this->NumberOfPhysicalCPU, &len, NULL, 0); + len = sizeof(this->NumberOfLogicalCPU); sysctlbyname("hw.logicalcpu", &this->NumberOfLogicalCPU, &len, NULL, 0); - this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = + this->Features.ExtendedFeatures.LogicalProcessorsPerPhysical = this->LogicalCPUPerPhysicalCPU(); len = sizeof(value); @@ -2947,16 +4009,16 @@ bool SystemInformationImplementation::ParseSysCtl() if (err != 0) // Go back to names we know but are less descriptive { this->ChipID.Family = 0; - char retBuf[32]; - ::memset(retBuf, 0, 32); + ::memset(retBuf, 0, 128); len = 32; - err = sysctlbyname("hw.machine", &retBuf, &len, NULL, 0); + err = sysctlbyname("hw.machine", &retBuf, &len, NULL, 0); kwsys_stl::string machineBuf(retBuf); if (machineBuf.find_first_of("Power") != kwsys_stl::string::npos) { this->ChipID.Vendor = "IBM"; - len = 4; + len = sizeof(this->ChipID.Family); err = sysctlbyname("hw.cputype", &this->ChipID.Family, &len, NULL, 0); + len = sizeof(this->ChipID.Model); err = sysctlbyname("hw.cpusubtype", &this->ChipID.Model, &len, NULL, 0); this->FindManufacturer(); } @@ -2964,35 +4026,40 @@ bool SystemInformationImplementation::ParseSysCtl() else // Should be an Intel Chip. { len = sizeof(this->ChipID.Family); - err = + err = sysctlbyname("machdep.cpu.family", &this->ChipID.Family, &len, NULL, 0); - - char retBuf[128]; + ::memset(retBuf, 0, 128); len = 128; err = sysctlbyname("machdep.cpu.vendor", retBuf, &len, NULL, 0); // Chip Vendor this->ChipID.Vendor = retBuf; this->FindManufacturer(); - - ::memset(retBuf, 0, 128); - err = - sysctlbyname("machdep.cpu.brand_string", - retBuf, &len, NULL, 0); - this->ChipID.ProcessorName = retBuf; // Chip Model len = sizeof(value); err = sysctlbyname("machdep.cpu.model", &value, &len, NULL, 0); this->ChipID.Model = static_cast< int >( value ); } + + // brand string + ::memset(retBuf, 0, sizeof(retBuf)); + len = sizeof(retBuf); + err = sysctlbyname("machdep.cpu.brand_string", retBuf, &len, NULL, 0); + if (!err) + { + this->ChipID.ProcessorName = retBuf; + this->ChipID.ModelName = retBuf; + } + // Cache size len = sizeof(value); err = sysctlbyname("hw.l1icachesize", &value, &len, NULL, 0); this->Features.L1CacheSize = static_cast< int >( value ); + len = sizeof(value); err = sysctlbyname("hw.l2cachesize", &value, &len, NULL, 0); this->Features.L2CacheSize = static_cast< int >( value ); - + return true; #else return false; @@ -3019,7 +4086,7 @@ kwsys_stl::string SystemInformationImplementation::ExtractValueFromSysCtl(const /** Run a given process */ kwsys_stl::string SystemInformationImplementation::RunProcess(kwsys_stl::vector<const char*> args) -{ +{ kwsys_stl::string buffer = ""; // Run the application @@ -3085,7 +4152,7 @@ kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const cha args.clear(); args.push_back("kstat"); args.push_back("-p"); - + kwsys_stl::string command = arguments; size_t start = command.npos; size_t pos = command.find(' ',0); @@ -3105,7 +4172,7 @@ kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const cha b0 = command.find('"',b1+1); b1 = command.find('"',b0+1); } - + if(!inQuotes) { kwsys_stl::string arg = command.substr(start+1,pos-start-1); @@ -3117,7 +4184,7 @@ kwsys_stl::string SystemInformationImplementation::ParseValueFromKStat(const cha arg.erase(quotes,1); quotes = arg.find('"'); } - args.push_back(arg.c_str()); + args.push_back(arg.c_str()); start = pos; } pos = command.find(' ',pos+1); @@ -3154,7 +4221,7 @@ bool SystemInformationImplementation::QuerySolarisInfo() this->NumberOfPhysicalCPU = static_cast<unsigned int>( atoi(this->ParseValueFromKStat("-n syste_misc -s ncpus").c_str())); this->NumberOfLogicalCPU = this->NumberOfPhysicalCPU; - + if(this->NumberOfPhysicalCPU!=0) { this->NumberOfLogicalCPU /= this->NumberOfPhysicalCPU; @@ -3163,19 +4230,19 @@ bool SystemInformationImplementation::QuerySolarisInfo() this->CPUSpeedInMHz = static_cast<float>(atoi(this->ParseValueFromKStat("-s clock_MHz").c_str())); // Chip family - this->ChipID.Family = 0; - + this->ChipID.Family = 0; + // Chip Vendor this->ChipID.Vendor = "Sun"; this->FindManufacturer(); - + // Chip Model this->ChipID.ProcessorName = this->ParseValueFromKStat("-s cpu_type"); this->ChipID.Model = 0; // Cache size - this->Features.L1CacheSize = 0; - this->Features.L2CacheSize = 0; + this->Features.L1CacheSize = 0; + this->Features.L2CacheSize = 0; char* tail; unsigned long totalMemory = @@ -3200,16 +4267,16 @@ bool SystemInformationImplementation::QueryHaikuInfo() system_info info; get_system_info(&info); - + this->NumberOfPhysicalCPU = info.cpu_count; this->CPUSpeedInMHz = info.cpu_clock_speed / 1000000.0F; // Physical Memory this->TotalPhysicalMemory = (info.max_pages * B_PAGE_SIZE) / (1024 * 1024) ; - this->AvailablePhysicalMemory = this->TotalPhysicalMemory - + this->AvailablePhysicalMemory = this->TotalPhysicalMemory - ((info.used_pages * B_PAGE_SIZE) / (1024 * 1024)); - + // NOTE: get_system_info_etc is currently a private call so just set to 0 // until it becomes public this->TotalVirtualMemory = 0; @@ -3237,8 +4304,8 @@ bool SystemInformationImplementation::QueryHaikuInfo() this->ChipID.Type = cpu_info.eax_1.type; // Chip family - this->ChipID.Family = cpu_info.eax_1.family; - + this->ChipID.Family = cpu_info.eax_1.family; + // Chip Model this->ChipID.Model = cpu_info.eax_1.model; @@ -3365,16 +4432,16 @@ bool SystemInformationImplementation::QueryOSInformation() ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX); bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi); - if (!bOsVersionInfoEx) + if (!bOsVersionInfoEx) { osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); - if (!GetVersionEx ((OSVERSIONINFO *) &osvi)) + if (!GetVersionEx ((OSVERSIONINFO *) &osvi)) { return false; } } - switch (osvi.dwPlatformId) + switch (osvi.dwPlatformId) { case VER_PLATFORM_WIN32_NT: // Test for the product. @@ -3417,40 +4484,40 @@ bool SystemInformationImplementation::QueryOSInformation() { this->OSRelease += " Personal"; } - else + else { this->OSRelease += " Professional"; } } #endif - } + } else if (osvi.wProductType == VER_NT_SERVER) { // Check for .NET Server instead of Windows XP. - if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) + if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) { this->OSRelease = ".NET"; } // Continue with the type detection. - if (osvi.wSuiteMask & VER_SUITE_DATACENTER) + if (osvi.wSuiteMask & VER_SUITE_DATACENTER) { this->OSRelease += " DataCenter Server"; } - else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) + else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) { this->OSRelease += " Advanced Server"; } - else + else { this->OSRelease += " Server"; } } sprintf (operatingSystem, "%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); - this->OSVersion = operatingSystem; + this->OSVersion = operatingSystem; } - else + else #endif // VER_NT_WORKSTATION { HKEY hKey; @@ -3473,7 +4540,7 @@ bool SystemInformationImplementation::QueryOSInformation() { this->OSRelease += " Standard Server"; } - else + else { this->OSRelease += " Server"; } @@ -3485,7 +4552,7 @@ bool SystemInformationImplementation::QueryOSInformation() { this->OSRelease += " Enterprise Server"; } - else + else { this->OSRelease += " Advanced Server"; } @@ -3493,7 +4560,7 @@ bool SystemInformationImplementation::QueryOSInformation() } // Display version, service pack (if any), and build number. - if (osvi.dwMajorVersion <= 4) + if (osvi.dwMajorVersion <= 4) { // NB: NT 4.0 and earlier. sprintf (operatingSystem, "version %ld.%ld %s (Build %ld)", @@ -3502,30 +4569,30 @@ bool SystemInformationImplementation::QueryOSInformation() osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); this->OSVersion = operatingSystem; - } - else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) + } + else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) { // Windows XP and .NET server. typedef BOOL (CALLBACK* LPFNPROC) (HANDLE, BOOL *); - HINSTANCE hKernelDLL; + HINSTANCE hKernelDLL; LPFNPROC DLLProc; - + // Load the Kernel32 DLL. hKernelDLL = LoadLibrary ("kernel32"); - if (hKernelDLL != NULL) { + if (hKernelDLL != NULL) { // Only XP and .NET Server support IsWOW64Process so... Load dynamically! - DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process"); - + DLLProc = (LPFNPROC) GetProcAddress (hKernelDLL, "IsWow64Process"); + // If the function address is valid, call the function. if (DLLProc != NULL) (DLLProc) (GetCurrentProcess (), &bIsWindows64Bit); else bIsWindows64Bit = false; - + // Free the DLL module. - FreeLibrary (hKernelDLL); - } - } - else - { + FreeLibrary (hKernelDLL); + } + } + else + { // Windows 2000 and everything else. sprintf (operatingSystem,"%s (Build %ld)", osvi.szCSDVersion, osvi.dwBuildNumber & 0xFFFF); this->OSVersion = operatingSystem; @@ -3534,32 +4601,32 @@ bool SystemInformationImplementation::QueryOSInformation() case VER_PLATFORM_WIN32_WINDOWS: // Test for the product. - if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) + if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) { this->OSRelease = "95"; - if(osvi.szCSDVersion[1] == 'C') + if(osvi.szCSDVersion[1] == 'C') { this->OSRelease += "OSR 2.5"; } - else if(osvi.szCSDVersion[1] == 'B') + else if(osvi.szCSDVersion[1] == 'B') { this->OSRelease += "OSR 2"; } - } + } - if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) + if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) { this->OSRelease = "98"; - if (osvi.szCSDVersion[1] == 'A' ) + if (osvi.szCSDVersion[1] == 'A' ) { this->OSRelease += "SE"; } - } + } - if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) + if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) { this->OSRelease = "Me"; - } + } break; case VER_PLATFORM_WIN32s: @@ -3583,7 +4650,7 @@ bool SystemInformationImplementation::QueryOSInformation() WSACleanup( ); } this->Hostname = name; - + const char* arch = getenv("PROCESSOR_ARCHITECTURE"); if(arch) { @@ -3604,51 +4671,41 @@ bool SystemInformationImplementation::QueryOSInformation() } #ifdef __APPLE__ - this->CallSwVers(); + this->OSName="Unknown Apple OS"; + this->OSRelease="Unknown product version"; + this->OSVersion="Unknown build version"; + + this->CallSwVers("-productName",this->OSName); + this->CallSwVers("-productVersion",this->OSRelease); + this->CallSwVers("-buildVersion",this->OSVersion); #endif #endif return true; - } - -void SystemInformationImplementation::CallSwVers() +int SystemInformationImplementation::CallSwVers( + const char *arg, + kwsys_stl::string &ver) { #ifdef __APPLE__ - kwsys_stl::string output; kwsys_stl::vector<const char*> args; - args.clear(); - - args.push_back("sw_vers"); - args.push_back("-productName"); - args.push_back(0); - output = this->RunProcess(args); - this->TrimNewline(output); - this->OSName = output; - args.clear(); - args.push_back("sw_vers"); - args.push_back("-productVersion"); + args.push_back(arg); args.push_back(0); - output = this->RunProcess(args); - this->TrimNewline(output); - this->OSRelease = output; - args.clear(); - - args.push_back("sw_vers"); - args.push_back("-buildVersion"); - args.push_back(0); - output = this->RunProcess(args); - this->TrimNewline(output); - this->OSVersion = output; + ver = this->RunProcess(args); + this->TrimNewline(ver); +#else + // avoid C4100 + (void)arg; + (void)ver; #endif + return 0; } - void SystemInformationImplementation::TrimNewline(kwsys_stl::string& output) -{ +{ // remove \r kwsys_stl::string::size_type pos=0; while((pos = output.find("\r", pos)) != kwsys_stl::string::npos) diff --git a/Source/kwsys/SystemInformation.hxx.in b/Source/kwsys/SystemInformation.hxx.in index b6ebe6aee5..8f4cb4e5a8 100644 --- a/Source/kwsys/SystemInformation.hxx.in +++ b/Source/kwsys/SystemInformation.hxx.in @@ -24,14 +24,22 @@ namespace @KWSYS_NAMESPACE@ { - -// forward declare the implementation class +// forward declare the implementation class class SystemInformationImplementation; - -class @KWSYS_NAMESPACE@_EXPORT SystemInformation -{ +class @KWSYS_NAMESPACE@_EXPORT SystemInformation +{ +#if @KWSYS_USE_LONG_LONG@ + typedef long long LongLong; +#elif @KWSYS_USE___INT64@ + typedef __int64 LongLong; +#else +# error "No Long Long" +#endif + friend class SystemInformationImplementation; + SystemInformationImplementation* Implementation; public: + SystemInformation (); ~SystemInformation (); @@ -40,6 +48,7 @@ public: kwsys_stl::string GetTypeID(); kwsys_stl::string GetFamilyID(); kwsys_stl::string GetModelID(); + kwsys_stl::string GetModelName(); kwsys_stl::string GetSteppingCode(); const char * GetExtendedProcessorName(); const char * GetProcessorSerialNumber(); @@ -49,13 +58,27 @@ public: int GetProcessorAPICID(); int GetProcessorCacheXSize(long int); bool DoesCPUSupportFeature(long int); - - const char * GetOSName(); + + // returns an informative general description of the cpu + // on this system. + kwsys_stl::string GetCPUDescription(); + const char * GetHostname(); + kwsys_stl::string GetFullyQualifiedDomainName(); + + const char * GetOSName(); const char * GetOSRelease(); const char * GetOSVersion(); const char * GetOSPlatform(); + int GetOSIsWindows(); + int GetOSIsLinux(); + int GetOSIsApple(); + + // returns an informative general description of the os + // on this system. + kwsys_stl::string GetOSDescription(); + bool Is64Bits(); unsigned int GetNumberOfLogicalCPU(); // per physical cpu @@ -63,20 +86,62 @@ public: bool DoesCPUSupportCPUID(); + // Retrieve id of the current running process + LongLong GetProcessId(); + // Retrieve memory information in megabyte. size_t GetTotalVirtualMemory(); size_t GetAvailableVirtualMemory(); size_t GetTotalPhysicalMemory(); - size_t GetAvailablePhysicalMemory(); + size_t GetAvailablePhysicalMemory(); + + // returns an informative general description if the installed and + // available ram on this system. See the GetHostMmeoryTotal, and + // Get{Host,Proc}MemoryAvailable methods for more information. + kwsys_stl::string GetMemoryDescription( + const char *hostLimitEnvVarName=NULL, + const char *procLimitEnvVarName=NULL); + + // Retrieve amount of physical memory installed on the system in KiB + // units. + LongLong GetHostMemoryTotal(); + + // Get total system RAM in units of KiB available colectivley to all + // processes in a process group. An example of a process group + // are the processes comprising an mpi program which is running in + // parallel. The amount of memory reported may differ from the host + // total if a host wide resource limit is applied. Such reource limits + // are reported to us via an applicaiton specified environment variable. + LongLong GetHostMemoryAvailable(const char *hostLimitEnvVarName=NULL); + + // Get total system RAM in units of KiB available to this process. + // This may differ from the host available if a per-process resource + // limit is applied. per-process memory limits are applied on unix + // system via rlimit api. Resource limits that are not imposed via + // rlimit api may be reported to us via an application specified + // environment variable. + LongLong GetProcMemoryAvailable( + const char *hostLimitEnvVarName=NULL, + const char *procLimitEnvVarName=NULL); + + // Get the system RAM used by all processes on the host, in units of KiB. + LongLong GetHostMemoryUsed(); + + // Get system RAM used by this process id in units of KiB. + LongLong GetProcMemoryUsed(); + + // enable/disable stack trace signal handler. In order to + // produce an informative stack trace the application should + // be dynamically linked and compiled with debug symbols. + static + void SetStackTraceOnError(int enable); /** Run the different checks */ void RunCPUCheck(); void RunOSCheck(); void RunMemoryCheck(); -private: - SystemInformationImplementation* Implementation; - }; + } // namespace @KWSYS_NAMESPACE@ /* Undefine temporary macros. */ diff --git a/Source/kwsys/SystemTools.cxx b/Source/kwsys/SystemTools.cxx index 66850e9ac8..085d988360 100644 --- a/Source/kwsys/SystemTools.cxx +++ b/Source/kwsys/SystemTools.cxx @@ -1665,7 +1665,7 @@ kwsys_stl::string SystemTools::EscapeChars( kwsys_stl::string n; if (str) { - if (!chars_to_escape | !*chars_to_escape) + if (!chars_to_escape || !*chars_to_escape) { n.append(str); } diff --git a/Source/kwsys/kwsysPlatformTestsCXX.cxx b/Source/kwsys/kwsysPlatformTestsCXX.cxx index 7b73d06df6..ae58703423 100644 --- a/Source/kwsys/kwsysPlatformTestsCXX.cxx +++ b/Source/kwsys/kwsysPlatformTestsCXX.cxx @@ -358,6 +358,30 @@ int main() } #endif +#ifdef TEST_KWSYS_IOS_HAS_ISTREAM___INT64 +int test_istream(kwsys_ios::istream& is, __int64& x) +{ + return (is >> x)? 1:0; +} +int main() +{ + __int64 x = 0; + return test_istream(kwsys_ios::cin, x); +} +#endif + +#ifdef TEST_KWSYS_IOS_HAS_OSTREAM___INT64 +int test_ostream(kwsys_ios::ostream& os, __int64 x) +{ + return (os << x)? 1:0; +} +int main() +{ + __int64 x = 0; + return test_ostream(kwsys_ios::cout, x); +} +#endif + #ifdef TEST_KWSYS_CHAR_IS_SIGNED /* Return 0 for char signed and 1 for char unsigned. */ int main() @@ -428,6 +452,48 @@ int main() } #endif +#ifdef TEST_KWSYS_CXX_HAS_RLIMIT64 +# if defined(KWSYS_HAS_LFS) +# define _LARGEFILE_SOURCE +# define _LARGEFILE64_SOURCE +# define _LARGE_FILES +# define _FILE_OFFSET_BITS 64 +# endif +# include <sys/resource.h> +int main() +{ + struct rlimit64 rlim; + return getrlimit64(0,&rlim); +} +#endif + +#ifdef TEST_KWSYS_CXX_HAS_ATOLL +#include <stdlib.h> +int main() +{ + const char *str="1024"; + return static_cast<int>(atoll(str)); +} +#endif + +#ifdef TEST_KWSYS_CXX_HAS_ATOL +#include <stdlib.h> +int main() +{ + const char *str="1024"; + return static_cast<int>(atol(str)); +} +#endif + +#ifdef TEST_KWSYS_CXX_HAS__ATOI64 +#include <stdlib.h> +int main() +{ + const char *str="1024"; + return static_cast<int>(_atoi64(str)); +} +#endif + #ifdef TEST_KWSYS_CXX_TYPE_INFO /* Collect fundamental type information and save it to a CMake script. */ diff --git a/Source/kwsys/testSystemInformation.cxx b/Source/kwsys/testSystemInformation.cxx index b3afc9de99..41fcf384b6 100644 --- a/Source/kwsys/testSystemInformation.cxx +++ b/Source/kwsys/testSystemInformation.cxx @@ -13,8 +13,6 @@ #include KWSYS_HEADER(SystemInformation.hxx) #include KWSYS_HEADER(ios/iostream) - - // Work-around CMake dependency scanning limitation. This must // duplicate the above list of headers. #if 0 @@ -22,12 +20,31 @@ # include "kwsys_ios_iostream.h.in" #endif -#define printMethod(inof, m) kwsys_ios::cout << #m << ": " \ +#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 + +#define printMethod(info, m) kwsys_ios::cout << #m << ": " \ << info.m() << "\n" -#define printMethod2(inof, m, unit) kwsys_ios::cout << #m << ": " \ +#define printMethod2(info, m, unit) kwsys_ios::cout << #m << ": " \ << info.m() << " " << unit << "\n" +#define printMethod3(info, m, unit) kwsys_ios::cout << #m << ": " \ +<< iostreamLongLong(info.m) << " " << unit << "\n" + int testSystemInformation(int, char*[]) { kwsys::SystemInformation info; @@ -35,7 +52,11 @@ int testSystemInformation(int, char*[]) info.RunOSCheck(); info.RunMemoryCheck(); printMethod(info, GetOSName); + printMethod(info, GetOSIsLinux); + printMethod(info, GetOSIsApple); + printMethod(info, GetOSIsWindows); printMethod(info, GetHostname); + printMethod(info, GetFullyQualifiedDomainName); printMethod(info, GetOSRelease); printMethod(info, GetOSVersion); printMethod(info, GetOSPlatform); @@ -58,6 +79,11 @@ int testSystemInformation(int, char*[]) printMethod2(info, GetAvailableVirtualMemory, "MB"); printMethod2(info, GetTotalPhysicalMemory, "MB"); printMethod2(info, GetAvailablePhysicalMemory, "MB"); + printMethod3(info, GetHostMemoryTotal(), "KiB"); + printMethod3(info, GetHostMemoryAvailable("KWSHL"), "KiB"); + printMethod3(info, GetProcMemoryAvailable("KWSHL","KWSPL"), "KiB"); + printMethod3(info, GetHostMemoryUsed(), "KiB"); + printMethod3(info, GetProcMemoryUsed(), "KiB"); //int GetProcessorCacheXSize(long int); // bool DoesCPUSupportFeature(long int); diff --git a/Utilities/Git/pre-commit b/Utilities/Git/pre-commit index d308a81b5d..a35d111025 100755 --- a/Utilities/Git/pre-commit +++ b/Utilities/Git/pre-commit @@ -47,18 +47,16 @@ if test -z "$HOOKS_ALLOW_KWSYS"; then '"$(echo "$files" | sed 's/^/ /')"' -cannot be committed through Git. KWSys is kept in a CVS repository -shared by several projects. A robot replays changes committed there -into the Source/kwsys directory in CMake. Please send changes to -this directory separately. Run +should not be made directly in CMake. KWSys is kept in its own Git +repository and shared by several projects. Please visit - git reset HEAD -- Source/kwsys + http://public.kitware.com/Wiki/KWSys/Git -to unstage these changes and then +to contribute changes directly to KWSys. Run - git diff -- Source/kwsys > kwsys.patch + git reset HEAD -- Source/kwsys -to construct the patch. Alternatively, set environment variable +to unstage these changes. Alternatively, set environment variable HOOKS_ALLOW_KWSYS=1 |