Thread: [Compbench-devel] CompBenchmarks++/libcompbenchmarks/System Makefile.am, NONE, 1.1 System.cpp, NONE
Brought to you by:
xfred
From: Frederic T. <xf...@us...> - 2007-01-22 17:51:34
|
Update of /cvsroot/compbench/CompBenchmarks++/libcompbenchmarks/System In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv7044 Added Files: Makefile.am System.cpp System-Cygwin.cpp System-Cygwin.h System-FreeBSD.cpp System-FreeBSD.h System.h System-Linux.cpp System-Linux.h System-Unix.cpp System-Unix.h Log Message: libcompbenchmarks moved in a separate directory. --- NEW FILE: System-Cygwin.cpp --- /* ---------------------------------------------------------------------------- $Id: System-Cygwin.cpp,v 1.1 2007/01/22 17:50:39 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #include <System/System-Cygwin.h> #include <string> #include <config.h> /* !!! */ SystemCygwin::SystemCygwin() : SystemUnix() { std::string cmd = "cscript.exe /nologo systemInfo.wmi"; exec(cmd, cache); } std::string SystemCygwin::processor_name(void) { std::string r = Split(cache, "\n", 2); Chomp(r); return(r); } std::string SystemCygwin::processor_mhz(void) { std::string r = Split(cache, "\n", 0); Chomp(r); return(r); } std::string SystemCygwin::processor_cache(void) { std::string r = Split(cache, "\n", 1); Chomp(r); return(r); } std::string SystemCygwin::processor_number(void) { std::string r = Split(cache, "\n", 3); Chomp(r); return(r); } SystemCygwin::~SystemCygwin() { } --- NEW FILE: System-Cygwin.h --- /* ---------------------------------------------------------------------------- $Id: System-Cygwin.h,v 1.1 2007/01/22 17:50:40 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMSYSTEMCYGWIN #define H_CBMSYSTEMCYGWIN #include <System/System-Unix.h> namespace CBM { /** \brief Cygwin operating system support class. * * Implements a few methods not defined by SystemUnix to handle properly * Cygwin systems in CompBenchmarks. */ class SystemCygwin : public SystemUnix { private: std::string cache; protected: public: /** Constructor */ SystemCygwin(); /** Returns processor name * Uses WMI to get the name of the first installed processor. * \return Processor name, as clear text * \sa processor_number() */ virtual std::string processor_name(void); /** Returns processor speed * Uses WMI to get the speed, in MHz, of the first installed processor. * \return Processor speed in MHz, as clear text (integer returned) * \sa processor_number() */ virtual std::string processor_mhz(void); /** Returns processor cache * Uses WMI to get processor second level cache in Kb. Integer expected. * \return std::string coding (first) processor second level cache in Kb. */ virtual std::string processor_cache(void); /** Returns processors' number * Uses WMI to get information. * \return std::string coding the number of processors (physical and logical) on host */ virtual std::string processor_number(void); /** Virtual destructor */ virtual ~SystemCygwin(); }; } #endif --- NEW FILE: System-Linux.cpp --- /* ---------------------------------------------------------------------------- $Id: System-Linux.cpp,v 1.1 2007/01/22 17:50:41 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #include <System/System-Linux.h> #include <string> #include <config.h> /* !!! */ using namespace CBM; SystemLinux::SystemLinux() : SystemUnix() { } std::string SystemLinux::processor_name(void) { std::string r; r=exec0("cat /proc/cpuinfo | grep 'model name' | head -1 | cut -f2 -d':' | sed 's/^ //' 2> /dev/null"); Chomp(r); return(r); } std::string SystemLinux::processor_mhz(void) { std::string r = exec0("(cat /proc/cpuinfo | grep 'cpu MHz' | head -1 | cut -f2 -d':' | sed 's/^ //' | cut -f1 -d'.') 2> /dev/null"); Chomp(r); return(r); } std::string SystemLinux::processor_cache(void) { std::string r = exec0("(cat /proc/cpuinfo | grep 'cache size' | head -1 | cut -f2 -d':' | sed 's/^ //' | sed 's/KB//') 2> /dev/null"); Chomp(r); return(r); } std::string SystemLinux::processor_number(void) { std::string r = exec0("(cat /proc/cpuinfo | grep 'model name' | wc -l) 2> /dev/null"); Chomp(r); return(r); } SystemLinux::~SystemLinux() { } --- NEW FILE: System.h --- /* ---------------------------------------------------------------------------- $Id: System.h,v 1.1 2007/01/22 17:50:40 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMSYSTEM #define H_CBMSYSTEM #include <vector> #include <string> #include <time.h> namespace CBM { /** Defines a vector of packages. */ typedef std::vector<std::string> PackageVector; /** \brief Operating system (and optionnaly hardware) abstraction layer. * * This class defines the base abstract (pure) object for all supported operating * systems. It may also introduces hardware-related dependencies. */ class System { public: /** Temporary directory. * Defines typedef used to query temporary directories used by CompBenchmarks. */ typedef enum Temp { Root, /*!< Root directory */ HostDep, /*!< Prefix for host-dependend directory */ Extract, /*!< Archive extraction temporary directory */ Patches, /*!< Directory holding patches */ OS, /*!< System specific material */ Download, /*!< Downloads temporary directory */ Status, /*!< Directory holding status of packages and * benchmarks */ Configuration /*!< Directory for configuration related * informations */ }; private: /** Check if the given directory holds patches. * Check if given arguments holds patches needed by libcompbenchmarks. * \param dir directory to check * \return non-null if ok. */ virtual int verifyPatchDirectory(std::string dir); protected: /** \sa Packages() */ PackageVector packages; /** Stores the last (shell) command executed by compbenchmarks library. */ std::string lastCommand; /** Stores the output of the last (shell) command executed by * compbenchmarks library. */ std::string lastCommandOutput; /** Constructor. */ System(); /** Virtual destructor */ virtual ~System(); /** External command execution. * Uses shell to execute command. * \param command shell command to execute * \return stdout dump * \sa exec() */ virtual std::string exec0(std::string& command); virtual std::string exec0(char *command); /** Checks a given directory. Directory is given according to its purpose in CompBenchmarks. \sa Temp \sa checkDirectory() \return 1 if ok. */ virtual int CheckDirectory(Temp where); public: /** Initialize for CompBenchmarks usage. * Checks for temporary directories. * \return 1 if ok. * \sa CheckDirectory() */ virtual int init(void); /** Get host ID * As hostid command does on UNIX. * \return Host ID as string.*/ virtual std::string hostid(void); /** Returns hostname * Returns the shortname. * \return hostname */ virtual std::string hostname(void); /** Returns operating system name * \return Operating system name (as uname command on UNIX) */ virtual std::string os(void); /** Returns operating system version * \return Operating system version (as uname -r command on most UNIXes) */ virtual std::string os_version(void); /** Returns processor name * Here we infer that each processor on the node are identical, which is the case * on x86 and compatible hardware. * \return Processor name, as clear text * \sa processor_number() */ virtual std::string processor_name(void); /** Returns processor speed * Processor speed in MHz. Integer expected. * \return std::string coding an integer matching the (first) processor speed, in MHz. */ virtual std::string processor_mhz(void); /** Returns processor cache * Processor second level cache in Kb. Integer expected. * \return std::string coding the (first) processor second level cache in Kb. */ virtual std::string processor_cache(void); /** Returns processors' number * \return std::string coding the number of processors (physical and logical) on host */ virtual std::string processor_number(void); /** Returns host's architecture. * As uname -m on UNIX. * \return Host's architecture */ virtual std::string arch(void); /** Give a list containing the compatible benchmarks * in declared directories. * \return a std::string list of benchmarks (id) * \sa Package() */ virtual PackageVector& packageList(int _force = 0); /** Get supported packages' number * \return integer * \sa Package() */ virtual int packageNumber(void); /** Get a package according to its index * \return Package instance * \sa packageNumber() */ virtual class Package *Package(int index); /** Get a package acoording to its internal identifier * \param pid std::string containing the package's identifier * \return CBMPackage instance * \sa packageList() */ virtual class Package *Package(std::string pid); /** Get a benchmark according to its internal identifier * \param bid std:string containing the benchmark's identifier * \return Benchmark instance */ virtual class Benchmark *Benchmark(std::string bid); /** Display informations about all packages * Used by compbenchmarks-core */ virtual void displayAllPackages(void); /** Display informations about all benchmarks * Used by compbenchmarks-core */ virtual void displayAllBenchmarks(void); /** Split a std::string. * Split given string as sub-strings, according to an arbitrary delimiter. * \param str string to split * \param delim delimiter * \param wantedIndex sub-string index */ virtual std::string Split(const std::string& str, const std::string& delim, unsigned int wantedIndex); /** Remove trailing line-feed (and carriage return) from specified string. * \param str the string */ virtual void Chomp(std::string& str); /** Describe host. * Displays informations about processors, OS, and so on. */ virtual void display(void); /** Describe external programs. * Displays informations about external programs used in CompBenchmarks or in * compbenchmark-config program. */ virtual void displayPrograms(void); /** External command execution. * Uses shell to execute command. * \param command shell command to execute * \param result stdout dump * \return return code of the shell command */ virtual int exec(std::string& command, std::string& result) = 0; /** Computes MD5 from a string * \param data Data to checksum * \return std::string coding data's checksum */ virtual std::string md5(std::string& data); /** Computes MD5 from a file * \param filename absolute filename * \return MD5 of the file passed. May be NULL (""). */ virtual std::string md5File(std::string& filename); /** Download a file or a page to a physical file. * Uses wget for now. * \param url URL containing data to download * \param localfile local filename used to store data * \return 1 if ok. */ \ virtual int download(std::string url, std::string localfile); /** Copy a local file. * Will uses cp on UNIX. * \param from source file to be copied * \param to destination of the file * \return 1 if ok. */ virtual int copy(std::string from, std::string to) = 0; /** Creates a directory * \param dir directory name * \return 1 if ok. */ virtual int mkdir(std::string dir) = 0; /** Checks if a directory is presents and has correct rights * \param dir directory to check * \return 1 if ok. */ virtual int checkDirectory(std::string dir) = 0; /** Gives an absolute directory * According to the where parameter, returns the temporary directory to use for * patches, download, or whatever. Note that System::Extract, * System::Status and System::Configuration are host specific in * System::Root. * \param where directory type * \return an absolute directory name */ virtual std::string temporaryDirectory(Temp where); /** Removes (unlink) a file * \param localfile file to remove * \return 1 if Ok. */ virtual int unlink(std::string localfile) = 0; /** Stores an couple key/value Each key must be unique. Value can be text, or number. \param key Key name \param value Value for the key. \return 1 if ok. */ virtual int store(char *key, std::string value); /** Retrieves the value of a key * \param key key name * \return key's value */ virtual std::string read(char *key); /** Tests if a file exists * \param fn (absolute) filename to test for * \return 1 if the file exists */ virtual int fileExists(std::string fn); /** Returns current time in seconds, since EPOC. * \return Current time. */ virtual time_t Time(void); /** Release the object. Frees internal pointers, and removes lockfiles. \sa init() \return 1 if ok. */ virtual int done(void); /** Returns the last command executed by compbenchmarks library. \sa lastCommand \sa getLastCommandOutput() \result a std::string containing the command */ virtual std::string& getLastCommand(void); /** Returns the last command's output executed by compbenchmarks library. \sa lastCommandOutput \sa getLastCommand() \result a std::string containing the command's output */ virtual std::string& getLastCommandOutput(void); }; extern System *cbmSystem; } #endif --- NEW FILE: System-FreeBSD.cpp --- /* ---------------------------------------------------------------------------- $Id: System-FreeBSD.cpp,v 1.1 2007/01/22 17:50:40 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #include <System/System-FreeBSD.h> #include <string> #include <config.h> /* !!! */ using namespace CBM; SystemFreeBSD::SystemFreeBSD() : SystemUnix() { } std::string SystemFreeBSD::processor_name(void) { std::string r = exec0("echo $(/sbin/sysctl hw.model | head -1 | cut -f2 -d'=')"); Chomp(r); return(r); } std::string SystemFreeBSD::processor_mhz(void) { std::string r = exec0("echo $(/sbin/sysctl hw.cpuspeed | head -1 | cut -f2 -d'=')"); Chomp(r); return(r); } std::string SystemFreeBSD::processor_cache(void) { std::string r = exec0("echo $(/sbin/sysctl hw.l2cachesize 2> /dev/null | head -1 | cut -f2 -d'=')"); Chomp(r); return(r); } std::string SystemFreeBSD::processor_number(void) { std::string r = exec0("echo $(/sbin/sysctl hw.ncpu | head -1 | cut -f2 -d'=')"); Chomp(r); return(r); } SystemFreeBSD::~SystemFreeBSD() { } --- NEW FILE: Makefile.am --- # ----------------------------------------------------------------------------- # $Id: Makefile.am,v 1.1 2007/01/22 17:50:39 xfred Exp $ # # This is free software. # For details, see the GNU Public License in the COPYING file, or # Look http://www.fsf.org # ----------------------------------------------------------------------------- noinst_LTLIBRARIES = libSystem.la if CBM_SYSTEM_LINUX source_sys = System-Linux.cpp else if CBM_SYSTEM_CYGWIN source_sys = System-Cygwin.cpp else if CBM_SYSTEM_FREEBSD source_sys = System-FreeBSD.cpp endif endif endif sources = System.cpp System-Unix.cpp libSystem_la_SOURCES = $(sources) $(source_sys) EXTRA_DIST = System-Linux.cpp System-Cygwin.cpp System-FreeBSD.cpp libSysteminclude_HEADERS= $(sources:.cpp=.h) $(EXTRA_DIST:.cpp=.h) libSystemincludedir = $(includedir)/compbenchmarks/System AM_CXXFLAGS = -DERRNO_DONE=1 -DDATAROOTDIR=\"@datarootdir@\" INCLUDES = -I $(top_srcdir)/libcompbenchmarks # -I /usr/include/w3c-libwww # !!! --- NEW FILE: System.cpp --- #include <System/System.h> #include <Base/md5.h> #include <Benchmark/Benchmark-DLLoader.h> #include <UI/UI.h> #include <iostream> #include <fstream> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <ctype.h> #include <dirent.h> #include <config.h> #define BUF 65535 using namespace CBM; System *CBM::cbmSystem =0; System::System() { std::string tmp; cbmSystem=this; if (!cbmUI) cbmUI=new CBM::UI; tmp=PREFIX; tmp+="/lib"; CBM::cbmlib_paths.push_back(tmp); CBM::cbmlib_paths.push_back("./SupportedBenchmarks/.libs"); CBM::cbmlib_paths.push_back("../../SupportedBenchmarks/.libs"); } int System::verifyPatchDirectory(std::string dir) { std::string fn; if (!checkDirectory(dir)) { return(0); } fn+="/scimark2.patch.gz"; return(fileExists(fn)); } int System::CheckDirectory(Temp where) { std::string dir = temporaryDirectory(where); int r; cbmUI->Information(CBM::UI::DirectoryCheck, dir); if (!checkDirectory(dir)) { r=mkdir(dir); if (!r) { std::string msg = "Can't create "; msg+=dir; cbmUI->Fatal(msg); } } return(1); } int System::download(std::string url, std::string localfile) { /* HTRequest * request; FILE * fp = NULL; HTProfile_newPreemptiveClient("TestApp", "1.0"); HTAlert_setInteractive(NO); request = HTRequest_new(); printf("download %s lpa='%s'\n", url.c_str(), localfile.c_str()); printf("%d\n", HTLoadToFile(url.c_str(), request, localfile.c_str())); printf("here\n"); HTRequest_delete(request); HTProfile_delete(); */ std::string cmd = CBM_PROG_WGET; cmd+=" --timeout=60 -t1 -q "; cmd+=url; cmd+=" -O "; cmd+=localfile; exec0(cmd); return(1); } int System::init(void) { std::string pid; pid=read("lockfile.pid"); if (pid!="") { cbmUI->Fatal("According to lockfile.pid, process " \ " already uses Compbenchmarks' administrative files."); } else { CheckDirectory(Root); CheckDirectory(HostDep); CheckDirectory(Extract); CheckDirectory(Download); CheckDirectory(Status); CheckDirectory(Configuration); char c[16] = { 0 }; std::string tmp; sprintf(c, "%d", (int) getpid()); tmp=c; packageList(); return(store("lockfile.pid", tmp)); } return(0); } std::string System::arch(void) { return(""); } std::string System::hostid(void) { return(""); } std::string System::hostname(void) { return(""); } std::string System::os(void) { return(""); } std::string System::os_version(void) { return(""); } std::string System::processor_name(void) { return(""); } std::string System::processor_mhz(void) { return(""); } std::string System::processor_cache(void) { return(""); } std::string System::processor_number(void) { return(""); } PackageVector &System::packageList(int _force) { DIR *fd; struct dirent *de; std::string dum; unsigned int e; int p_i; int p_n = CBM::cbmlib_paths.size(); std::string dir; int r_i; int r_n; int inserted; if ((packages.size()!=0) && (!_force)) return(packages); for(p_i=0;p_i<p_n;p_i++) { dir= CBM::cbmlib_paths[p_i]; fd=opendir(dir.c_str()); if (fd) { while ( (de=readdir(fd)) != 0) { dum=de->d_name; if (dum.size()<=3) continue; if (dum.substr(dum.size()-3) != ".so") continue; if (dum.substr(0, 21) != "libcompbenchmarks-bm-") continue; e=dum.find(".so", 0); dum=dum.substr(21, e-21); inserted=0; r_n=packages.size(); for(r_i=0;r_i<r_n;r_i++) { if (packages[r_i] == dum) { inserted=1; break; } } if (!inserted) packages.push_back(dum); } } closedir(fd); } return(packages); } int System::packageNumber(void) { return(packages.size()); } Package *System::Package(int index) { return(Package(packages[index])); } Package *System::Package(std::string pid) { CBM::cbmlib_internal *p = CBM::cbmlib_load((char*) pid.c_str()); if (p) return(p->PackageGet()); return(0); } Benchmark *System::Benchmark(std::string bid) { std::string pid; CBM::Package *P = 0; pid=Split(bid, "-", 0); P=Package(pid); if (P) { return(P->Benchmark(bid)); } return(0); } void System::displayAllPackages(void) { int i; int n = packageNumber(); CBM::Package *P; for(i=0; i<n; i++) { P=Package(i); P->display(); } } void System::displayAllBenchmarks(void) { int i; int n = packageNumber(); CBM::Package *P; for(i=0; i<n; i++) { P=Package(i); P->display(); P->displayBenchmarks(); } } std::string System::Split(const std::string& str, const std::string& delim, unsigned int wantedIndex) { unsigned int offset = 0; unsigned int delimIndex = 0; unsigned int nowIndex = 0; delimIndex = str.find(delim, offset); while (delimIndex != std::string::npos) { if (wantedIndex==nowIndex) return(str.substr(offset, delimIndex - offset)); offset += delimIndex - offset + delim.length(); delimIndex = str.find(delim, offset); nowIndex++; } if (wantedIndex==nowIndex) return(str.substr(offset)); else return(""); } void System::Chomp(std::string& str) { int l = str.length(); int i; int sp = 1; std::string dum; while (l) { if ((str[l-1]=='\n') || (str[l-1]=='\r') || (isspace(str[l-1]))) { str.resize(l-1); l--; } else break; } l=str.length(); for(i=0;i<l;i++) { if ((!sp) || (!(isspace((int) str[i])))) { sp=0; dum+=str[i]; } } str=dum; } void System::display(void) { std::cout << "host::arch=" << arch() << std::endl << "host::hostid=" << hostid() << std::endl << "host::hostname=" << hostname() << std::endl << "host::os=" << os() << std::endl << "host::os_version=" << os_version() << std::endl << "host::processor_name=" << processor_name() << std::endl << "host::processor_mhz=" << processor_mhz() << std::endl << "host::processor_cache=" << processor_cache() << std::endl << "host::processor_number=" << processor_number() << std::endl; } void System::displayPrograms(void) { std::cout << "BZIP2:" << CBM_PROG_BZIP2 << std::endl << "CUT:" << CBM_PROG_CUT << std::endl << "DD:" << CBM_PROG_DD << std::endl << "DIALOG:" << CBM_PROG_DIALOG << std::endl << "GREP:" << CBM_PROG_GREP << std::endl << "GZIP:" << CBM_PROG_GZIP << std::endl << "HEAD:" << CBM_PROG_HEAD << std::endl << "MAKE:" << CBM_PROG_MAKE << std::endl << "MKDIR:" << CBM_PROG_MKDIR << std::endl << "PATCH:" << CBM_PROG_PATCH << std::endl << "RM:" << CBM_PROG_RM << std::endl << "TAIL:" << CBM_PROG_TAIL << std::endl << "TAR:" << CBM_PROG_TAR << std::endl << "UNZIP:" << CBM_PROG_UNZIP << std::endl << "WGET:" << CBM_PROG_WGET << std::endl << "ZCAT:" << CBM_PROG_ZCAT << std::endl << "CAT:" << CBM_PROG_CAT << std::endl << "PERL:" << CBM_PROG_PERL << std::endl; } std::string System::exec0(std::string& command) { std::string result; if (!exec(command, result)) { return(result); } else { return(0); } } std::string System::exec0(char *command) { std::string tmp = command; return(exec0(tmp)); } std::string System::md5(std::string& data) { md5_state_t state; md5_byte_t digest[16]; std::string result; int i; char tmp[3] = { 0 }; md5_init(&state); md5_append(&state, (const md5_byte_t *)data.c_str(), data.length()); md5_finish(&state, digest); for(i=0; i<16; i++) { sprintf(tmp, "%02x", (unsigned int) digest[i]); result+=tmp; } return(result); } std::string System::md5File(std::string& filename) { std::ifstream IN(filename.c_str(), std::ios::in | std::ios::binary | std::ios::ate); char *buf; std::ifstream::pos_type size; std::string result; std::string tmp; tmp=""; if (IN.is_open()) { size=IN.tellg(); buf=new char[size]; IN.seekg(0, std::ios::beg); IN.read(buf, size); IN.close(); tmp.append(buf, size); delete[](buf); result=md5(tmp); } else { std::string str; str="Invalid MD5 for "; str+=filename; cbmUI->Fatal(str); result=""; } return(result); } std::string System::temporaryDirectory(Temp where) { std::string result; std::string host; result=getenv("HOME"); result+="/.compbenchmarks"; host=hostname(); switch(where) { case Root: break; case HostDep: result+="/"; result+=host; break; case Extract: result+="/"; result+=host; result+="/Extracts"; break; case Download: result+="/Downloads"; break; case Status: result+="/"; result+=host; result+="/Status"; break; case Configuration: result+="/"; result+=host; result+="/Configuration"; break; case Patches: result=DATAROOTDIR; result+="/"; result+=PACKAGE; result+="/"; result+=VERSION; result+="/"; result+="patches"; if (!verifyPatchDirectory(result)) { result=getenv("PWD"); result+="/share/patches"; if (!verifyPatchDirectory(result)) { result=getenv("PWD"); result+="/../share/patches"; } } break; case OS: result=DATAROOTDIR; result+="/"; result+=PACKAGE; result+="/"; result+=VERSION; result+="/"; result+="system"; if (!checkDirectory(result)) { result=getenv("PWD"); result+="/share/system/"; #ifdef CBM_SYSTEM_CYGWIN result+="cygwin"; #else result=""; #endif } break; default: cbmUI->Fatal("Internal : Bad 'where' parameters in System::temporaryDirectory();"); return(""); } return(result); } int System::store(char *key, std::string value) { std::string fn; std::ofstream OUT; fn=temporaryDirectory(System::Status); fn+="/"; fn+=key; OUT.open(fn.c_str(),std::ios::out); OUT << value; OUT.close(); /* !!! */ return(1); } std::string System::read(char *key) { std::string fn; std::ifstream IN; std::ifstream::pos_type size; char *buf; std::string result; fn=temporaryDirectory(System::Status); fn+="/"; fn+=key; IN.open(fn.c_str(), std::ios::in | std::ios::binary | std::ios::ate); if (IN.is_open()) { size=IN.tellg(); buf=new char[size]; IN.seekg(0, std::ios::beg); IN.read(buf, size); IN.close(); result.append(buf, size); delete[](buf); } else { /* !!! */ } return(result); } int System::fileExists(std::string fn) { std::ifstream IN; IN.open(fn.c_str(), std::ios::in | std::ios::binary | std::ios::ate); if (IN.is_open()) { IN.close(); return(1); } else { return(0); } } time_t System::Time(void) { return(time(NULL)); } int System::done(void) { std::string fn; CBM::cbmlib_done(); fn=temporaryDirectory(System::Status); fn+="/"; fn+="lockfile.pid"; return(unlink(fn)); } std::string& System::getLastCommand(void) { return(lastCommand); } std::string& System::getLastCommandOutput(void) { return(lastCommandOutput); } System::~System() { } --- NEW FILE: System-Linux.h --- /* ---------------------------------------------------------------------------- $Id: System-Linux.h,v 1.1 2007/01/22 17:50:41 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMSYSTEMLINUX #define H_CBMSYSTEMLINUX #include <System/System-Unix.h> namespace CBM { /** \brief Linux operating system support class. * * Implements a few methods not defined by SystemUnix to handle properly * Linux systems in CompBenchmarks. */ class SystemLinux : public SystemUnix { protected: public: /** Constructor */ SystemLinux(); /** Returns processor name * Uses /proc to get the name of the first installed processor. * \return Processor name, as clear text * \sa processor_number() */ virtual std::string processor_name(void); /** Returns processor speed * Uses /proc to get the speed, in MHz, of the first installed processor. * \return Processor speed in MHz, as clear text (integer returned) * \sa processor_number() */ virtual std::string processor_mhz(void); /** Returns processor cache * Uses /proc to get processor second level cache in Kb. Integer expected. * \return std::string coding (first) processor second level cache in Kb. */ virtual std::string processor_cache(void); /** Returns processors' number * Uses /proc to get information. * \return std::string coding the number of processors (physical and logical) on host */ virtual std::string processor_number(void); /** Virtual destructor */ virtual ~SystemLinux(); }; } #endif --- NEW FILE: System-FreeBSD.h --- /* ---------------------------------------------------------------------------- $Id: System-FreeBSD.h,v 1.1 2007/01/22 17:50:40 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMSYSTEMFREEBSD #define H_CBMSYSTEMFREEBSD #include <System/System-Unix.h> namespace CBM { /** \brief FreeBSD operating system support class. * * Implements a few methods not defined by SystemUnix to handle properly * FreeBSD systems in CompBenchmarks. */ class SystemFreeBSD : public SystemUnix { protected: public: /** Constructor */ SystemFreeBSD(); /** Returns processor name * Uses /sbin/sysctl to get the name of the first installed processor. * \return Processor name, as clear text * \sa processor_number() */ virtual std::string processor_name(void); /** Returns processor speed * Uses /sbin/sysctl to get the speed, in MHz, of the first installed processor. * \return Processor speed in MHz, as clear text (integer returned) * \sa processor_number() */ virtual std::string processor_mhz(void); /** Returns processor cache * Uses /sbin/sysctl to get processor second level cache in Kb. Integer expected. * \return std::string coding (first) processor second level cache in Kb. */ virtual std::string processor_cache(void); /** Returns processors' number * Uses /sbin/sysctl to get information. * \return std::string coding the number of processors (physical and logical) on host */ virtual std::string processor_number(void); /** Virtual destructor */ virtual ~SystemFreeBSD(); }; } #endif --- NEW FILE: System-Unix.cpp --- #include <System/System-Unix.h> #include <libcompbenchmarks.h> #include <UI/UI.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> #include <iostream> #include <stdio.h> using namespace CBM; void sysunix_sighandler(int signal) { std::cout << "Signal #" << signal << "received. Exiting properly" << std::endl; if (cbmSystem) cbmSystem->done(); exit(0); } SystemUnix::SystemUnix() : System() { signal(SIGINT, sysunix_sighandler); } int SystemUnix::copy(std::string from, std::string to) { std::string cmd; std::string sstdout; cmd="cp -f "; cmd+=from; cmd+=" "; cmd+=to; if (! UO_verbose) { cmd+=" 2> /dev/null"; } return(exec(cmd, sstdout)); } int SystemUnix::exec(std::string& command, std::string& result) { FILE *fpipe; char line[256]; result=""; lastCommand=command; if ( !(fpipe = (FILE*)popen(command.c_str(),"r")) ) { cbmUI->Fatal("Can't create pipe."); } while ( fgets( line, sizeof line, fpipe)) { result+=line; } lastCommandOutput=result; return(pclose(fpipe)); } int SystemUnix::mkdir(std::string dir) { return(!::mkdir(dir.c_str(), 0755)); } int SystemUnix::checkDirectory(std::string dir) { struct stat buf; stat(dir.c_str(), &buf); return( S_ISDIR(buf.st_mode) && ((S_IRUSR & buf.st_mode) == S_IRUSR) && ((S_IWUSR & buf.st_mode) == S_IWUSR) && ((S_IXUSR & buf.st_mode) == S_IXUSR) ); } std::string SystemUnix::arch(void) { std::string r = exec0("uname -m 2> /dev/null"); Chomp(r); return(r); } std::string SystemUnix::hostid(void) { std::string r = exec0("hostid 2> /dev/null"); Chomp(r); return(r); } std::string SystemUnix::hostname(void) { std::string r = exec0("hostname 2> /dev/null"); Chomp(r); return(r); } std::string SystemUnix::os(void) { std::string r = exec0("uname 2> /dev/null"); Chomp(r); return(r); } std::string SystemUnix::os_version(void) { std::string r = exec0("uname -r 2> /dev/null"); Chomp(r); return(r); } int SystemUnix::unlink(std::string localfile) { return(!::unlink(localfile.c_str())); } SystemUnix::~SystemUnix() { } --- NEW FILE: System-Unix.h --- /* ---------------------------------------------------------------------------- $Id: System-Unix.h,v 1.1 2007/01/22 17:50:41 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMSYSTEMUNIX #define H_CBMSYSTEMUNIX #include <System/System.h> namespace CBM { /** \brief UNIX operating system basic support class. * * Implements methods not defined by SystemUnix to handle properly * UNIX and compatibles systems in CompBenchmarks. This class may be overloaded. */ class SystemUnix : public System { protected: /** External command execution. * Uses system() call to execute command. * \param command shell command to execute * \param result stdout dump * \return return code of the shell command */ virtual int exec(std::string& command, std::string& result); public: /** Constructor. */ SystemUnix(); virtual int copy(std::string from, std::string to); /** Creates a directory * Uses mkdir() POSIX call. * \param dir directory name * \return 1 if ok. */ virtual int mkdir(std::string dir); /** Checks if a directory is presents and has correct rights * Expected rights are rwx access for user. stat() call used. * \param dir directory to check * \return 1 if ok. */ virtual int checkDirectory(std::string dir); /** Returns host's architecture. * Uses uname. * \return Host's architecture */ virtual std::string arch(void); /** Get host ID * Uses hostid command. * \return hostid, as std::string. */ virtual std::string hostid(void); /** Returns hostname * Returns the shortname, using hostname external command. * \return hostname as std::string*/ virtual std::string hostname(void); /** Returns operating system name * Uses uname. * \return Operating system name, as std::string */ virtual std::string os(void); /** Returns operating system version * Uses uname -r. * \return Operating system version, as std::string */ virtual std::string os_version(void); /** Removes (unlink) a file * unlink() POSIX function used. * \param localfile file to remove * \return 1 if Ok. */ virtual int unlink(std::string localfile); /** Virtual destructor. */ virtual ~SystemUnix(); }; } #endif |