Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

toolbox/resourceMonitor.hh
Go to the documentation of this file.
00001 /* resource monitor
00002  */
00003 
00004 #ifndef resourceMonitor_hh
00005 #define resourceMonitor_hh
00006 
00007 #include <iostream>
00008 
00009 #include <sys/time.h>
00010 #include <sys/resource.h>
00011 #include <unistd.h>
00012 #include <string.h>
00013 #include <time.h>
00014 
00015 extern "C" { 
00016 #ifdef __sun__
00017   // Solaris 2.5 does not have getrusage in <sys/resource.h>
00018   int getrusage(int who, struct rusage* ru);
00019 #endif
00020 }
00021 
00022 namespace concepts {
00023 
00024   // ******************************************************* ResourceMonitor **
00025 
00027   class ResourceMonitor {
00028     friend std::ostream& operator <<(std::ostream& os, ResourceMonitor& rm) { 
00029       return rm.sample(os); }
00030 
00034     rusage ru_;
00035   public:
00039     ResourceMonitor() { getrusage(RUSAGE_SELF, &ru_); }
00040 
00054     std::ostream& sample(std::ostream& os);
00055 
00059     float utime();
00060   };
00061   
00062 }
00063 
00067 class ResourceTimer {
00068 public:
00069 
00070   ResourceTimer() {
00071     memset(&t_old, 0, sizeof(t_old));
00072     memset(&t_cur, 0, sizeof(t_cur));
00073     start();
00074   }
00075 
00076   ~ResourceTimer() { }
00077 
00078   void start();
00079   double stop(int nrun = 1);
00080 
00081   double reset(int nrun = 1) {
00082     double t = stop(nrun);
00083     //start();
00084     t_old = t_cur;
00085     return t;
00086   }
00087 
00088 
00089   timespec t_old;
00090   timespec t_cur;
00091 };
00092 
00093 #endif // resourceMonitor_hh

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)