#include "resusage.h" #include #include #include #include #include /*! \class SOPHYA::ResourceUsage \ingroup SysTools This class gives acces to various system resource usage (CPU, memory, ...). All returned values are in kilobytes for memory consumptions and in milli-seconds fo CPU and elapsed times. The information should be updated through the call to the Update() method. Note: The information is obtained through \c getrusage() and \c getrlimit() system calls, and depending on the OS, not all of the memory usage values are correctly filled. \code // How to check resource usage for a given part of the program ResourceUsage res; // --- Part of the program to be checked : Start // ... res.Update(); cout << " Memory size increase (KB):" << res.getDeltaMemorySize() << endl; cout << " Resource usage info : \n" << res << endl; \endcode */ /*! Constructor. the \b Update() method is called. */ ResourceUsage::ResourceUsage() { struct rlimit rl; getrlimit(RLIMIT_DATA, &rl); max_datasz = rl.rlim_cur/1024; #if defined(SunOS) // Max resident size ne semble pas etre defini sur SunOS max_rss = max_datasz; #else getrlimit(RLIMIT_RSS, &rl); max_rss = rl.rlim_cur/1024; #endif getrlimit(RLIMIT_STACK, &rl); max_stack = rl.rlim_cur/1024; cur_datasz = 0; cur_stack = 0; cur_rss = 0; delta_rss = 0; cur_tottm = 0; cur_usrtm = 0; cur_systm = 0; elapsed_time = 0; delta_rss = 0; delta_tottm = 0; delta_elapsed_time = 0; Update(); } /*! Destructor. */ ResourceUsage::~ResourceUsage() { } inline uint_8 s_timeval2msec(struct timeval & tv) { uint_8 ret = tv.tv_sec; ret *= 1000; ret += tv.tv_usec/1000; return(ret); } /*! Update the CPU and memory usage information. */ int ResourceUsage::Update() { struct rusage rsu; int rc = getrusage(RUSAGE_SELF, &rsu); delta_tottm = cur_tottm; delta_rss = cur_rss; cur_usrtm = s_timeval2msec(rsu.ru_utime); cur_systm = s_timeval2msec(rsu.ru_stime); cur_tottm = cur_usrtm+cur_systm; cur_rss = rsu.ru_maxrss; cur_datasz = rsu.ru_idrss; cur_stack = rsu.ru_isrss; delta_tottm = cur_tottm-delta_tottm; delta_rss = cur_rss-delta_rss; delta_elapsed_time = elapsed_time; time_t tm = time(NULL); if (elapsed_time == 0) t0_time = tm; elapsed_time = (tm - t0_time)*1000; if (elapsed_time < 1) elapsed_time = 1; delta_elapsed_time = elapsed_time-delta_elapsed_time; return(rc); } /*! Prints the CPU and memory usage information. \param os : The output stream \param lp : The print level (0 .. 2) \param upd : if \c true , the Update method is called. */ void ResourceUsage::Print(ostream& os, int lp, bool upd) { if (upd) Update(); os << " --------------- ResourceUsage::Print(lp=" << lp <<" ) --------------- " << endl; int load = getAverageCPULoad()*100.; os << " CPU-Usage= " << getCPUTime() << " Elapsed= " << getElapsedTime() << " msec - Load= " << load << " %" << endl; long fracmem = getMemorySize()*100/getMaxMemorySize(); os << " MemoryUsage= " << getMemorySize() << " /Max= " << getMaxMemorySize() << " kbytes (" << fracmem << " %)" << endl; if (lp < 1) return; os << " CPU-Usage= " << cur_tottm << "ms (usr,sys)=(" << cur_usrtm << "," << cur_systm << ")" << endl; os << " DataSize=" << cur_datasz << " /Max " << max_datasz << " kbytes" << endl; os << " StackSize=" << cur_stack << " /Max " << max_stack << " kbytes" << endl; os << " ResSize=" << cur_rss << " /Max " << max_rss << " kbytes" << endl; }