Back to index

nordugrid-arc-nox  1.1.0~rc6
sysinfo.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 
00006 #include <iostream>
00007 #include <fstream>
00008 #include <vector>
00009 #include <arc/StringConv.h>
00010 
00011 #include "sysinfo.h"
00012 #ifndef WIN32
00013 #include <sys/utsname.h> // uname
00014 #include "fsusage.h"
00015 #else
00016 #include <arc/win32.h>
00017 #endif
00018 
00019 namespace Paul
00020 {
00021 
00022 #ifndef WIN32
00023 
00024 // Linux specific
00025 SysInfo::SysInfo(void)
00026 {
00027     struct utsname u;
00028     if (uname(&u) == 0) {
00029         if (strcmp(u.sysname, "Linux") == 0) {
00030             osFamily = "linux";
00031         } else {
00032             osFamily = "unknown";
00033         }
00034         osName = "generallinux";
00035         osVersion = u.version;
00036     }
00037     
00038     std::ifstream p("/proc/cpuinfo");
00039     std::string s;
00040     unsigned int p_num = 0;
00041     while (getline(p, s)) {
00042         // found processor line
00043         if (s.find("processor", 0) != std::string::npos) {
00044             p_num++;
00045         }
00046         if ((s.find("vendor_id", 0) != std::string::npos) && platform.empty()) {
00047             std::vector<std::string> t;
00048             Arc::tokenize(s, t, ":");
00049             std::string p = Arc::trim(t[1]);
00050             if (p == "GenuineIntel") {
00051                 platform = "i386";
00052             } else if (p == "AMD64") { // XXX not sure
00053                 platform = "amd64";
00054             } else {
00055                 platform = "unknown";
00056             }
00057         }
00058     }
00059     p.close();
00060     physicalCPUs = p_num;
00061     logicalCPUs = p_num;
00062 
00063     std::ifstream m("/proc/meminfo");
00064     unsigned int swap_size = 0;
00065     while (getline(m, s)) {
00066         if (s.find("MemTotal:") != std::string::npos) {
00067             std::vector<std::string> t;
00068             Arc::tokenize(s, t, ":");
00069             std::string size = Arc::trim(t[1]);
00070             mainMemorySize = Arc::stringtoui(size)/1024;
00071         }
00072         if (s.find("SwapTotal:") != std::string::npos) {
00073             std::vector<std::string> t;
00074             Arc::tokenize(s, t, ":");
00075             std::string size = Arc::trim(t[1]);
00076             swap_size = Arc::stringtoui(size);
00077         }
00078     }
00079     virtualMemorySize = (mainMemorySize + swap_size)/1024;
00080     m.close();
00081 }
00082 
00083 unsigned int SysInfo::diskAvailable(const std::string &path)
00084 {
00085     struct fs_usage fsu;
00086     int ret;
00087     ret = get_fs_usage(path.c_str(), NULL, &fsu);
00088     if (ret == 0) {
00089         return (fsu.fsu_blocksize * fsu.fsu_bavail)/(1024*1024);
00090     }
00091     return 0;
00092 }
00093 
00094 unsigned int SysInfo::diskTotal(const std::string &path)
00095 {
00096     struct fs_usage fsu;
00097     int ret;
00098     ret = get_fs_usage(path.c_str(), NULL, &fsu);
00099     if (ret == 0) {
00100         return (fsu.fsu_blocksize * fsu.fsu_blocks)/(1024*1024);
00101     }
00102     return 0;
00103 }
00104 
00105 unsigned int SysInfo::diskFree(const std::string &path)
00106 {
00107     struct fs_usage fsu;
00108     int ret;
00109     ret = get_fs_usage(path.c_str(), NULL, &fsu);
00110     if (ret == 0) {
00111         return (fsu.fsu_blocksize * fsu.fsu_bfree)/(1024*1024);
00112     }
00113     return 0;
00114 }
00115 
00116 void SysInfo::refresh(void)
00117 {
00118     // NOP
00119 }
00120 #else
00121 
00122 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
00123 typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, PDWORD);
00124 
00125 // Win32 specific
00126 SysInfo::SysInfo(void)
00127 {
00128     // get operating system version 
00129     OSVERSIONINFO os_ver;
00130     SYSTEM_INFO si;
00131     PGNSI pGNSI;
00132     PGPI pGPI;
00133 
00134     ZeroMemory(&os_ver, sizeof(OSVERSIONINFO));
00135     os_ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00136     ZeroMemory(&si, sizeof(SYSTEM_INFO));
00137 
00138     if (GetVersionEx(&os_ver) == true) {
00139         osFamily = "windows";
00140         if (os_ver.dwMajorVersion == 5 && os_ver.dwMinorVersion == 0) {
00141             osName = "windows2000";
00142         } else if (os_ver.dwMajorVersion == 5 && os_ver.dwMinorVersion == 1) {
00143             osName = "windowsxp";
00144         } else if (os_ver.dwMajorVersion == 5 && os_ver.dwMinorVersion == 2) {
00145             osName = "windows2003";
00146         } else if (os_ver.dwMajorVersion == 6) {
00147             osName = "windowsxp";
00148         }
00149         osVersion = os_ver.szCSDVersion;
00150     }
00151      
00152     pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
00153     if(pGNSI != NULL) {
00154         pGNSI(&si);
00155     } else {
00156         GetSystemInfo(&si);
00157     }
00158     if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
00159         platform = "amd64";
00160     } else if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) {
00161         platform = "i386";
00162     } else if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
00163         platform = "itanium";
00164     }
00165     
00166     physicalCPUs = si.dwNumberOfProcessors;
00167     logicalCPUs = si.dwNumberOfProcessors;
00168 
00169     // Get Memory Information
00170     MEMORYSTATUSEX memory_status;
00171     memory_status.dwLength = sizeof(memory_status);
00172     GlobalMemoryStatusEx(&memory_status);
00173     mainMemorySize = memory_status.ullTotalPhys/(1024*1024);
00174     virtualMemorySize = memory_status.ullTotalVirtual/(1024*1024);
00175 }
00176 
00177 unsigned int SysInfo::diskAvailable(const std::string &path)
00178 {
00179     ULARGE_INTEGER available_bytes, total_bytes, total_free_bytes;
00180     GetDiskFreeSpaceEx(path.c_str(), &available_bytes, &total_bytes, &total_free_bytes);
00181     return (Int64ShraMod32(available_bytes.QuadPart, 10)/1024);
00182 }
00183 
00184 unsigned int SysInfo::diskTotal(const std::string &path)
00185 {
00186     ULARGE_INTEGER available_bytes, total_bytes, total_free_bytes;
00187     GetDiskFreeSpaceEx(path.c_str(), &available_bytes, &total_bytes, &total_free_bytes);
00188     return (Int64ShraMod32(total_bytes.QuadPart, 10)/1024);
00189 }
00190 
00191 unsigned int SysInfo::diskFree(const std::string &path)
00192 {
00193     ULARGE_INTEGER available_bytes, total_bytes, total_free_bytes;
00194     GetDiskFreeSpaceEx(path.c_str(), &available_bytes, &total_bytes, &total_free_bytes);
00195     return (Int64ShraMod32(total_free_bytes.QuadPart, 10)/1024);
00196 }
00197 
00198 void SysInfo::refresh(void)
00199 {
00200     // NOP
00201 }
00202 #endif
00203 
00204 } // namespace Paul