From a0bd892cb4aff49fbbd6c77c1cd02af27bcf9752 Mon Sep 17 00:00:00 2001 From: Brad King <brad.king@kitware.com> Date: Fri, 22 May 2020 12:27:33 -0400 Subject: [PATCH] SystemInformation: Use 'long long' unconditionally Now that KWSys requires C++11, we can assume `long long` and `std:atoll` are available. --- CMakeLists.txt | 18 ------ SystemInformation.cxx | 118 +++++++++++++++----------------------- SystemInformation.hxx.in | 21 +++---- kwsysPlatformTestsCXX.cxx | 27 --------- 4 files changed, 53 insertions(+), 131 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 10732bca..7d43c68c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -546,24 +546,6 @@ if(KWSYS_USE_SystemInformation) 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(UNIX) include(CheckIncludeFileCXX) # check for simple stack trace diff --git a/SystemInformation.cxx b/SystemInformation.cxx index 6be9d434..1c3ada99 100644 --- a/SystemInformation.cxx +++ b/SystemInformation.cxx @@ -169,18 +169,6 @@ typedef struct rlimit ResourceLimitType; #include <cstring> #include <memory.h> -#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 - #if defined(_MSC_VER) && (_MSC_VER >= 1300) && !defined(_WIN64) && \ !defined(__clang__) # define USE_ASM_INSTRUCTIONS 1 @@ -297,7 +285,6 @@ using DELAY_FUNC = void (*)(unsigned int); class SystemInformationImplementation { public: - using LongLong = SystemInformation::LongLong; SystemInformationImplementation(); ~SystemInformationImplementation() = default; @@ -337,16 +324,16 @@ public: size_t GetTotalPhysicalMemory() const; size_t GetAvailablePhysicalMemory() const; - LongLong GetProcessId(); + long long GetProcessId(); // Retrieve memory information in KiB. - LongLong GetHostMemoryTotal(); - LongLong GetHostMemoryAvailable(const char* hostLimitEnvVarName); - LongLong GetHostMemoryUsed(); + long long GetHostMemoryTotal(); + long long GetHostMemoryAvailable(const char* hostLimitEnvVarName); + long long GetHostMemoryUsed(); - LongLong GetProcMemoryAvailable(const char* hostLimitEnvVarName, - const char* procLimitEnvVarName); - LongLong GetProcMemoryUsed(); + long long GetProcMemoryAvailable(const char* hostLimitEnvVarName, + const char* procLimitEnvVarName); + long long GetProcMemoryUsed(); double GetLoadAverage(); @@ -505,7 +492,8 @@ protected: void CPUCountWindows(); // For windows unsigned char GetAPICId(); // For windows bool IsSMTSupported() const; - static LongLong GetCyclesDifference(DELAY_FUNC, unsigned int); // For windows + static long long GetCyclesDifference(DELAY_FUNC, + unsigned int); // For windows // For Linux and Cygwin, /proc/cpuinfo formats are slightly different bool RetreiveInformationFromCpuInfoFile(); @@ -806,31 +794,31 @@ std::string SystemInformation::GetMemoryDescription( } // host memory info in units of KiB. -SystemInformation::LongLong SystemInformation::GetHostMemoryTotal() +long long SystemInformation::GetHostMemoryTotal() { return this->Implementation->GetHostMemoryTotal(); } -SystemInformation::LongLong SystemInformation::GetHostMemoryAvailable( +long long SystemInformation::GetHostMemoryAvailable( const char* hostLimitEnvVarName) { return this->Implementation->GetHostMemoryAvailable(hostLimitEnvVarName); } -SystemInformation::LongLong SystemInformation::GetHostMemoryUsed() +long long SystemInformation::GetHostMemoryUsed() { return this->Implementation->GetHostMemoryUsed(); } // process memory info in units of KiB. -SystemInformation::LongLong SystemInformation::GetProcMemoryAvailable( +long long SystemInformation::GetProcMemoryAvailable( const char* hostLimitEnvVarName, const char* procLimitEnvVarName) { return this->Implementation->GetProcMemoryAvailable(hostLimitEnvVarName, procLimitEnvVarName); } -SystemInformation::LongLong SystemInformation::GetProcMemoryUsed() +long long SystemInformation::GetProcMemoryUsed() { return this->Implementation->GetProcMemoryUsed(); } @@ -840,7 +828,7 @@ double SystemInformation::GetLoadAverage() return this->Implementation->GetLoadAverage(); } -SystemInformation::LongLong SystemInformation::GetProcessId() +long long SystemInformation::GetProcessId() { return this->Implementation->GetProcessId(); } @@ -3611,8 +3599,7 @@ bool SystemInformationImplementation::QueryProcessor() /** Get total system RAM in units of KiB. */ -SystemInformation::LongLong -SystemInformationImplementation::GetHostMemoryTotal() +long long SystemInformationImplementation::GetHostMemoryTotal() { #if defined(_WIN32) # if defined(_MSC_VER) && _MSC_VER < 1300 @@ -3627,7 +3614,7 @@ SystemInformationImplementation::GetHostMemoryTotal() return statex.ullTotalPhys / 1024; # endif #elif defined(__linux) - SystemInformation::LongLong memTotal = 0; + long long memTotal = 0; int ierr = GetFieldFromFile("/proc/meminfo", "MemTotal:", memTotal); if (ierr) { return -1; @@ -3650,11 +3637,10 @@ SystemInformationImplementation::GetHostMemoryTotal() 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( +long long SystemInformationImplementation::GetHostMemoryAvailable( const char* hostLimitEnvVarName) { - SystemInformation::LongLong memTotal = this->GetHostMemoryTotal(); + long long memTotal = this->GetHostMemoryTotal(); // the following mechanism is provided for systems that // apply resource limits across groups of processes. @@ -3665,8 +3651,7 @@ SystemInformationImplementation::GetHostMemoryAvailable( if (hostLimitEnvVarName) { const char* hostLimitEnvVarValue = getenv(hostLimitEnvVarName); if (hostLimitEnvVarValue) { - SystemInformation::LongLong hostLimit = - atoLongLong(hostLimitEnvVarValue); + long long hostLimit = std::atoll(hostLimitEnvVarValue); if (hostLimit > 0) { memTotal = min(hostLimit, memTotal); } @@ -3680,20 +3665,17 @@ SystemInformationImplementation::GetHostMemoryAvailable( 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( +long long SystemInformationImplementation::GetProcMemoryAvailable( const char* hostLimitEnvVarName, const char* procLimitEnvVarName) { - SystemInformation::LongLong memAvail = - this->GetHostMemoryAvailable(hostLimitEnvVarName); + long long 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); + long long procLimit = std::atoll(procLimitEnvVarValue); if (procLimit > 0) { memAvail = min(procLimit, memAvail); } @@ -3705,28 +3687,24 @@ SystemInformationImplementation::GetProcMemoryAvailable( ResourceLimitType rlim; ierr = GetResourceLimit(RLIMIT_DATA, &rlim); if ((ierr == 0) && (rlim.rlim_cur != RLIM_INFINITY)) { - memAvail = - min((SystemInformation::LongLong)rlim.rlim_cur / 1024, memAvail); + memAvail = min((long long)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); + memAvail = min((long long)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); + memAvail = min((long long)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); + memAvail = min((long long)rlim.rlim_cur / 1024, memAvail); } #endif @@ -3736,8 +3714,7 @@ SystemInformationImplementation::GetProcMemoryAvailable( /** Get RAM used by all processes in the host, in units of KiB. */ -SystemInformation::LongLong -SystemInformationImplementation::GetHostMemoryUsed() +long long SystemInformationImplementation::GetHostMemoryUsed() { #if defined(_WIN32) # if defined(_MSC_VER) && _MSC_VER < 1300 @@ -3754,39 +3731,38 @@ SystemInformationImplementation::GetHostMemoryUsed() #elif defined(__linux) // First try to use MemAvailable, but it only works on newer kernels const char* names2[3] = { "MemTotal:", "MemAvailable:", nullptr }; - SystemInformation::LongLong values2[2] = { SystemInformation::LongLong(0) }; + long long values2[2] = { 0 }; int ierr = GetFieldsFromFile("/proc/meminfo", names2, values2); if (ierr) { const char* names4[5] = { "MemTotal:", "MemFree:", "Buffers:", "Cached:", nullptr }; - SystemInformation::LongLong values4[4] = { SystemInformation::LongLong( - 0) }; + long long values4[4] = { 0 }; ierr = GetFieldsFromFile("/proc/meminfo", names4, values4); if (ierr) { return ierr; } - SystemInformation::LongLong& memTotal = values4[0]; - SystemInformation::LongLong& memFree = values4[1]; - SystemInformation::LongLong& memBuffers = values4[2]; - SystemInformation::LongLong& memCached = values4[3]; + long long& memTotal = values4[0]; + long long& memFree = values4[1]; + long long& memBuffers = values4[2]; + long long& memCached = values4[3]; return memTotal - memFree - memBuffers - memCached; } - SystemInformation::LongLong& memTotal = values2[0]; - SystemInformation::LongLong& memAvail = values2[1]; + long long& memTotal = values2[0]; + long long& memAvail = values2[1]; return memTotal - memAvail; #elif defined(__APPLE__) - SystemInformation::LongLong psz = getpagesize(); + long long psz = getpagesize(); if (psz < 1) { return -1; } const char* names[3] = { "Pages wired down:", "Pages active:", nullptr }; - SystemInformation::LongLong values[2] = { SystemInformation::LongLong(0) }; + long long values[2] = { 0 }; int ierr = GetFieldsFromCommand("vm_stat", names, values); if (ierr) { return -1; } - SystemInformation::LongLong& vmWired = values[0]; - SystemInformation::LongLong& vmActive = values[1]; + long long& vmWired = values[0]; + long long& vmActive = values[1]; return ((vmActive + vmWired) * psz) / 1024; #else return 0; @@ -3797,8 +3773,7 @@ SystemInformationImplementation::GetHostMemoryUsed() Get system RAM used by the process associated with the given process id in units of KiB. */ -SystemInformation::LongLong -SystemInformationImplementation::GetProcMemoryUsed() +long long SystemInformationImplementation::GetProcMemoryUsed() { #if defined(_WIN32) && defined(KWSYS_SYS_HAS_PSAPI) long pid = GetCurrentProcessId(); @@ -3815,14 +3790,14 @@ SystemInformationImplementation::GetProcMemoryUsed() } return pmc.WorkingSetSize / 1024; #elif defined(__linux) - SystemInformation::LongLong memUsed = 0; + long long memUsed = 0; int ierr = GetFieldFromFile("/proc/self/status", "VmRSS:", memUsed); if (ierr) { return -1; } return memUsed; #elif defined(__APPLE__) - SystemInformation::LongLong memUsed = 0; + long long memUsed = 0; pid_t pid = getpid(); std::ostringstream oss; oss << "ps -o rss= -p " << pid; @@ -3886,7 +3861,7 @@ double SystemInformationImplementation::GetLoadAverage() /** Get the process id of the running process. */ -SystemInformation::LongLong SystemInformationImplementation::GetProcessId() +long long SystemInformationImplementation::GetProcessId() { #if defined(_WIN32) return GetCurrentProcessId(); @@ -4299,9 +4274,8 @@ size_t SystemInformationImplementation::GetAvailablePhysicalMemory() const } /** Get Cycle differences */ -SystemInformation::LongLong -SystemInformationImplementation::GetCyclesDifference(DELAY_FUNC DelayFunction, - unsigned int uiParameter) +long long SystemInformationImplementation::GetCyclesDifference( + DELAY_FUNC DelayFunction, unsigned int uiParameter) { #if defined(_MSC_VER) && (_MSC_VER >= 1400) unsigned __int64 stamp1, stamp2; diff --git a/SystemInformation.hxx.in b/SystemInformation.hxx.in index fc42e9dc..c8efd51d 100644 --- a/SystemInformation.hxx.in +++ b/SystemInformation.hxx.in @@ -15,13 +15,6 @@ class SystemInformationImplementation; 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; @@ -104,7 +97,7 @@ public: bool DoesCPUSupportCPUID(); // Retrieve id of the current running process - LongLong GetProcessId(); + long long GetProcessId(); // Retrieve memory information in MiB. size_t GetTotalVirtualMemory(); @@ -120,7 +113,7 @@ public: // Retrieve amount of physical memory installed on the system in KiB // units. - LongLong GetHostMemoryTotal(); + long long GetHostMemoryTotal(); // Get total system RAM in units of KiB available colectivley to all // processes in a process group. An example of a process group @@ -128,7 +121,7 @@ public: // 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 application specified environment variable. - LongLong GetHostMemoryAvailable(const char* hostLimitEnvVarName = nullptr); + long long GetHostMemoryAvailable(const char* hostLimitEnvVarName = nullptr); // Get total system RAM in units of KiB available to this process. // This may differ from the host available if a per-process resource @@ -136,14 +129,14 @@ public: // 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 = nullptr, - const char* procLimitEnvVarName = nullptr); + long long GetProcMemoryAvailable(const char* hostLimitEnvVarName = nullptr, + const char* procLimitEnvVarName = nullptr); // Get the system RAM used by all processes on the host, in units of KiB. - LongLong GetHostMemoryUsed(); + long long GetHostMemoryUsed(); // Get system RAM used by this process id in units of KiB. - LongLong GetProcMemoryUsed(); + long long GetProcMemoryUsed(); // Return the load average of the machine or -0.0 if it cannot // be determined. diff --git a/kwsysPlatformTestsCXX.cxx b/kwsysPlatformTestsCXX.cxx index 0bf23e96..d154fbc5 100644 --- a/kwsysPlatformTestsCXX.cxx +++ b/kwsysPlatformTestsCXX.cxx @@ -132,33 +132,6 @@ int main() } #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_HAS_UTIMES # include <sys/time.h> int main() -- GitLab