Trace some infos about the current OS at startup (name, version, nb of bits, ...)

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@5067 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: c005b39304949a349750d7e57a8178dc2497f1a1
Former-commit-id: d00bcf46def9ce2f1d6994d6851ec1c959fcad53
This commit is contained in:
pouillot 2012-12-15 17:35:33 +00:00
parent 399bf7ddc0
commit fb94b2fb9f
6 changed files with 970 additions and 875 deletions

View file

@ -72,6 +72,35 @@ GfApplication::GfApplication(const char* pszName, const char* pszVersion, const
// Initialize the gaming framework.
GfInit();
// Trace current OS information.
std::string strName;
int nMajor, nMinor, nPatch, nBits;
if (GfGetOSInfo(strName, nMajor, nMinor, nPatch, nBits))
{
GfLogInfo("Current OS is %s", (strName.empty() ? "unknown" : strName.c_str()));
if (nMajor >= 0)
{
GfLogInfo(" (R%d", nMajor);
if (nMinor >= 0)
{
GfLogInfo(".%d", nMinor);
if (nPatch >= 0)
GfLogInfo(".%d", nPatch);
}
}
if (nBits >= 0)
{
if (nMajor >= 0)
GfLogInfo(", ");
else
GfLogInfo(" (");
GfLogInfo("%d bits", nBits);
}
if (nMajor >= 0 || nBits >= 0)
GfLogInfo(")");
GfLogInfo("\n");
}
// Trace build information.
GfLogInfo("Built on %s\n", SD_BUILD_INFO_SYSTEM);
GfLogInfo(" with CMake %s, '%s' generator\n",

View file

@ -34,6 +34,7 @@
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/utsname.h> // uname
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
#include <sys/param.h>
@ -629,7 +630,7 @@ unsigned linuxGetNumberOfCPUs()
if (nCPUs == 0)
{
// MacOS X, FreeBSD, OpenBSD, NetBSD, etc ...
// MacOS X, FreeBSD, OpenBSD, NetBSD, etc ...
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
nt mib[4];
@ -650,12 +651,12 @@ unsigned linuxGetNumberOfCPUs()
sysctl(mib, 2, &nCPUs, &len, NULL, 0);
}
// Linux, Solaris, AIX
// Linux, Solaris, AIX
#elif defined(linux) || defined(__linux__)
nCPUs = (unsigned)sysconf(_SC_NPROCESSORS_ONLN);
// Anything else ... not supported.
// Anything else ... not supported.
#else
#warning "Unsupported Linux OS"
@ -707,13 +708,13 @@ std::string cpuSet2String(const cpu_set_t* pCPUSet)
bool
linuxSetThreadAffinity(int nCPUId)
{
// MacOS X, FreeBSD, OpenBSD, NetBSD, etc ...
// MacOS X, FreeBSD, OpenBSD, NetBSD, etc ...
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
GfLogWarning("Thread affinity not yet implemented on Mac OS X or BSD.\n");
// TODO.
// Linux, Solaris, AIX ... with NPTL (Native POSIX Threads Library)
// Linux, Solaris, AIX ... with NPTL (Native POSIX Threads Library)
#elif defined(linux) || defined(__linux__)
// Get the handle for the current thread.
@ -750,7 +751,7 @@ linuxSetThreadAffinity(int nCPUId)
return true;
// Anything else ... not supported.
// Anything else ... not supported.
#else
#warning "linuxspec.cpp::linuxSetThreadAffinity : Unsupported Linux OS"
@ -761,6 +762,53 @@ linuxSetThreadAffinity(int nCPUId)
return false;
}
/*
* Function
* linuxGetOSInfo
*
* Description
* Get some info about the running OS (name, x.y.z release, and nb of bits).
*
* Parameters
* strName : target string for the OS name ("" if any error occurred)
* nMajor : target OS major version integer (-1 if could not be obtained)
* nMinor : target OS minor version integer (-1 if could not be obtained)
* nPatch : target OS patch version integer (-1 if could not be obtained)
* nBits : target OS number of bits (32 or 64) integer (or -1 if could not be obtained)
*
* Return
* True if OK, False otherwise.
*/
static bool
linuxGetOSInfo(std::string& strName, int& nMajor, int& nMinor, int& nPatch, int& nBits)
{
struct utsname utsName;
if(uname(&utsName) < 0)
{
const int errnum = errno; // Get errno before it is overwritten by some system call.
GfLogWarning("Could not get OS info through uname (%s).\n", strerror(errnum));
return false;
}
//GfLogDebug("linuxGetOSInfo : name='%s', version='%s', release='%s'\n",
// utsName.sysname, utsName.version, utsName.release);
strName = utsName.sysname;
strName += " ";
strName += utsName.release;
strName += " ";
strName += utsName.version;
const int nNums = sscanf(utsName.release, "%d.%d.%d", &nMajor, &nMinor, &nPatch);
if (nNums < 1)
nMajor = -1;
if (nNums < 2)
nMinor = -1;
if (nNums < 3)
nPatch = -1;
nBits = strstr(utsName.release, "64") ? 64 : 32;
return true;
}
/*
* Function
* LinuxSpecInit
@ -792,5 +840,5 @@ LinuxSpecInit(void)
GfOs.timeClock = linuxTimeClock;
GfOs.sysGetNumberOfCPUs = linuxGetNumberOfCPUs;
GfOs.sysSetThreadAffinity = linuxSetThreadAffinity;
GfOs.sysGetOSInfo = linuxGetOSInfo;
}

View file

@ -79,7 +79,7 @@ unsigned GfGetNumberOfCPUs()
/* Force the current thread to run on the specified CPU.
* @param nCPUId the index in [0, # of actual CPUs on the system [ (any other value will actually reset the thread affinity to the "system" affinity mask, meaning no special processor / core assignement)
* @return true if any error occured, false otherwise
* @return false if any error occurred, false otherwise
*/
bool GfSetThreadAffinity(int nCPUId)
{
@ -89,3 +89,19 @@ bool GfSetThreadAffinity(int nCPUId)
return false;
}
}
/* Get some info about the running OS.
* @param strName target string for the OS name
* @param nMajor target OS major version integer
* @param nMinor target OS minor version integer
* @param nBits target OS number of bits (32 or 64) integer
* @return false if any error occurred, false otherwise
*/
bool GfGetOSInfo(std::string& strName, int& nMajor, int& nMinor, int& nPatch, int& nBits)
{
if (GfOs.sysGetOSInfo) {
return GfOs.sysGetOSInfo(strName, nMajor, nMinor, nPatch, nBits);
} else {
return false;
}
}

View file

@ -40,6 +40,7 @@ typedef double (*tfTimeClock)(void);
/* System interface */
typedef unsigned (*tfSysGetNumberOfCPUs)(void);
typedef bool (*tfSysSetThreadAffinity)(int nCPUId);
typedef bool (*tfSysGetOSInfo)(std::string&, int&, int&, int&, int&);
typedef struct {
tfModLoad modLoad;
@ -53,6 +54,7 @@ typedef struct {
tfTimeClock timeClock;
tfSysSetThreadAffinity sysSetThreadAffinity;
tfSysGetNumberOfCPUs sysGetNumberOfCPUs;
tfSysGetOSInfo sysGetOSInfo;
} tGfOs;
TGF_API extern tGfOs GfOs;

View file

@ -45,6 +45,7 @@
#include "modinfo.h" // Don't move this include line : needs TGF_API definition.
#include <string>
/** Floating point type used everywhere.
@ingroup definitions
@ -508,6 +509,7 @@ TGF_API bool GfSetThreadAffinity(int nCPUId);
TGF_API void GfSleep(double seconds);
TGF_API bool GfGetOSInfo(std::string& strName, int& nMajor, int& nMinor, int& nPatch, int& nBits);
/***************************
* Run-time dirs accessors *

View file

@ -589,21 +589,21 @@ windowsTimeClock(void)
* windowsGetOSInfo
*
* Description
* Get some info about the running Windows OS.
* Get some info about the running OS (name, x.y.z release, and nb of bits).
*
* Parameters
* pnMajor : pointer to the target OS major version integer
* pnMinor : pointer to the target OS minor version integer
* pnBits : pointer to the target OS number of bits (32 or 64) integer
* strName : target string for the OS name ("" if any error occurred)
* nMajor : target OS major version integer (-1 if could not be obtained)
* nMinor : target OS minor version integer (-1 if could not be obtained)
* nPatch : target OS patch version integer (-1 if could not be obtained)
* nBits : target OS number of bits (32 or 64) integer (or -1 if could not be obtained)
*
* Return
* none
* True if OK, False otherwise.
*/
static bool
windowsGetOSInfo(int* pnMajor, int* pnMinor, int* pnBits)
windowsGetOSInfo(std::string& strName, int& nMajor, int& nMinor, int& nPatch, int& nBits)
{
static char pszVerSionString[512];
OSVERSIONINFOEX osvi;
SYSTEM_INFO si;
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
@ -627,34 +627,34 @@ windowsGetOSInfo(int* pnMajor, int* pnMinor, int* pnBits)
else
GetSystemInfo(&si);
*pnMajor = osvi.dwMajorVersion;
*pnMinor = osvi.dwMinorVersion;
*pnBits = 32; // Default value, fixed afterward if necessary.
nMajor = osvi.dwMajorVersion;
nMinor = osvi.dwMinorVersion;
nBits = 32; // Default value, fixed afterward if necessary.
strcpy(pszVerSionString, "Windows ");
strName = "Windows ";
// Windows <= 4
if (osvi.dwPlatformId != VER_PLATFORM_WIN32_NT || osvi.dwMajorVersion <= 4)
{
strcat(pszVerSionString, "NT 4 or older");
strName += "NT 4 or older";
}
// Windows Vista
else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0)
{
if( osvi.wProductType == VER_NT_WORKSTATION)
strcat(pszVerSionString, "Vista ");
strName += "Vista ";
else
strcat(pszVerSionString, "Server 2008 ");
strName += "Server 2008 ";
}
// Windows 7
else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1)
{
if( osvi.wProductType == VER_NT_WORKSTATION)
strcat(pszVerSionString, "7 ");
strName += "7 ";
else
strcat(pszVerSionString, "Server 2008 R2 ");
strName += "Server 2008 R2 ";
}
// Windows Server or XP Pro x64
@ -663,89 +663,86 @@ windowsGetOSInfo(int* pnMajor, int* pnMinor, int* pnBits)
if (osvi.wProductType == VER_NT_WORKSTATION
&& si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
{
*pnBits = 64;
strcat(pszVerSionString, "XP Professional x64 Edition");
nBits = 64;
strName += "XP Professional x64 Edition";
}
else
strcat(pszVerSionString,
"Server 2003 / 2003 R2 / Storage Server 2003 or Home Server, ");
strName +=
"Server 2003 / 2003 R2 / Storage Server 2003 or Home Server, ";
// Test for the server type.
if (osvi.wProductType != VER_NT_WORKSTATION)
{
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
{
*pnBits = 64;
strcat(pszVerSionString, "Some Itanium Edition");
nBits = 64;
strName += "Some Itanium Edition";
}
else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
*pnBits = 64;
strcat(pszVerSionString, "Some x64 Edition");
nBits = 64;
strName += "Some x64 Edition";
}
else
strcat(pszVerSionString, "Some 32bit Edition");
strName += "Some 32bit Edition";
}
}
// Windows XP
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
{
strcat(pszVerSionString, "XP ");
strName += "XP ";
if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
strcat(pszVerSionString, "Home Edition");
strName += "Home Edition";
else
strcat(pszVerSionString, "Professional");
strName += "Professional";
}
// Windows 2000
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
{
strcat(pszVerSionString, "2000 ");
strName += "2000 ";
if (osvi.wProductType == VER_NT_WORKSTATION)
{
strcat(pszVerSionString, "Professional");
strName += "Professional";
}
else
{
if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
strcat(pszVerSionString, "Datacenter Server");
strName += "Datacenter Server";
else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
strcat(pszVerSionString, "Advanced Server");
strName += "Advanced Server";
else
strcat(pszVerSionString, "Server");
strName += "Server";
}
}
// Include service pack and build number.
if (strlen(osvi.szCSDVersion) > 0)
{
strcat(pszVerSionString, " ");
strcat(pszVerSionString, osvi.szCSDVersion);
strName += " ";
strName += osvi.szCSDVersion;
}
// Include build number.
char buf[80];
sprintf(buf, " (build %ld)", osvi.dwBuildNumber);
strcat(pszVerSionString, buf);
snprintf(buf, sizeof(buf), " (build %ld)", osvi.dwBuildNumber);
strName += buf;
// Other 64 bit cases.
if (osvi.dwMajorVersion >= 6)
{
if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
{
strcat(pszVerSionString, ", 64-bit");
*pnBits = 64;
}
else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL)
{
strcat(pszVerSionString, ", 32-bit");
nBits = 64;
}
}
GfLogInfo("Detected %s (%d.%d)\n", pszVerSionString, *pnMajor, *pnMinor);
// No patch release information (see build number).
nPatch = -1;
return true;
}
@ -897,4 +894,5 @@ WindowsSpecInit(void)
GfOs.timeClock = windowsTimeClock;
GfOs.sysGetNumberOfCPUs = windowsGetNumberOfCPUs;
GfOs.sysSetThreadAffinity = windowsSetThreadAffinity;
GfOs.sysGetOSInfo = windowsGetOSInfo;
}