Timer.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 CSIRO ICT Centre
00003  *
00004  * $Id: Timer.h 4896 2008-07-08 14:29:34Z RossCollins $
00005  */
00006 
00007 #ifndef TIMER_DEFINED
00008 #define TIMER_DEFINED
00009 
00010 #include <iostream>
00011 #include "String.h"
00012 #include <sys/time.h>
00013 #include <sys/resource.h>
00014 
00015 #include "Destroyer.h"
00016 
00017 
00018 class Timer;
00019 
00020 std::ostream & operator<<(std::ostream & os, Timer & timer);
00021 
00022 
00023 class Timer
00024 {
00025   public:
00026 #ifdef TIMER
00027     //
00028     // There is a global timer used to time the whole system. It is
00029     // instantiated once as a static variable. This static method
00030     // provides the only access.
00031     //
00032     static Timer * const globalTimer()
00033     {
00034       if (s_instance == 0)
00035       {
00036         String name("GlobalTimer");
00037         s_instance = new Timer(name, Timer::Running);
00038         DestroyerInit(s_instance)
00039       }
00040       return s_instance;
00041     }
00042     DestroyerFriend(Timer)
00043 
00044     static void reportTimes();
00045 #endif
00046 
00047     typedef enum { Running, Paused } State;
00048     enum { MaxNameLength = 32 };
00049 
00050     Timer(String const & name, State initialState = Paused);
00051 
00052     ~Timer();
00053 
00054     void reset(String const & name, State newState);
00055 
00056     String const & name() const;
00057 
00058     double elapsed(bool timeCalled = false);
00059     double cpu(bool called = false);
00060     double user_cpu(bool called = false);
00061     double system_cpu(bool called = false);
00062     long max_memory(bool called = false);
00063     long shared_memory(bool called = false);
00064     long unshared_data_memory(bool called = false);
00065     long unshared_stack_memory(bool called = false);
00066     long minor_page_faults(bool called = false);
00067     long major_page_faults(bool called = false);
00068     long page_faults(bool called = false);
00069     long blocked_input_ops(bool called = false);
00070     long blocked_output_ops(bool called = false);
00071 
00072     void pause();
00073         // Pause the timer
00074 
00075     void cont();
00076         // Continue the timer
00077 
00078     void getTimes(double & elapsed, double & user_cpu,
00079              double & system_cpu, double & cpu,
00080              long & max_memory, long & shared_memory,
00081              long & unshared_data_memory, long & unshared_stack_memory,
00082              long & page_faults, long & minor_page_faults,
00083              long & major_page_faults, long & blocked_input_ops,
00084              long & blocked_output_ops);
00085 
00086   private:
00087 #ifdef TIMER
00088     DestroyerDefine(Timer);
00089     static Timer * s_instance;
00090 #endif
00091 
00092     State state;
00093 
00094     time_t time_now;
00095     struct rusage resource_usage;
00096 
00097     String label;
00098     double time_start;
00099     double cpu_start;
00100     double user_cpu_start;
00101     double system_cpu_start;
00102     long max_memory_start;
00103     long shared_memory_start;
00104     long unshared_data_memory_start;
00105     long unshared_stack_memory_start;
00106     long page_faults_start;
00107     long major_page_faults_start;
00108     long minor_page_faults_start;
00109     long blocked_input_ops_start;
00110     long blocked_output_ops_start;
00111 
00112     double time_accum;
00113     double cpu_accum;
00114     double user_cpu_accum;
00115     double system_cpu_accum;
00116     long max_memory_accum;
00117     long shared_memory_accum;
00118     long unshared_data_memory_accum;
00119     long unshared_stack_memory_accum;
00120     long page_faults_accum;
00121     long minor_page_faults_accum;
00122     long major_page_faults_accum;
00123     long blocked_input_ops_accum;
00124     long blocked_output_ops_accum;
00125 
00126     Timer(Timer const &);
00127       // Don't want this defined by the compiler
00128 
00129     Timer& operator=(Timer const &);
00130       // Don't want this defined by the compiler
00131 
00132     void updateResourceUsage()
00133     {
00134       getrusage(RUSAGE_SELF, &resource_usage);
00135     };
00136 
00137     void updateTime()
00138     {
00139       time_now = time(0);
00140     };
00141 
00142     double currentElapsed()
00143     {
00144       return (double)time_now;
00145     };
00146 
00147     double currentCpu()
00148     {
00149       return currentUserCpu() + currentSystemCpu();
00150     };
00151 
00152     double currentUserCpu()
00153     {
00154       return double(resource_usage.ru_utime.tv_sec)
00155            + double(resource_usage.ru_utime.tv_usec)/1000000.0;
00156     }
00157 
00158     double currentSystemCpu()
00159     {
00160       return double(resource_usage.ru_stime.tv_sec)
00161            + double(resource_usage.ru_stime.tv_usec)/1000000.0;
00162     }
00163 
00164     long currentMaxMemory()
00165     {
00166       return resource_usage.ru_maxrss;
00167     }
00168 
00169     long currentSharedMemory()
00170     {
00171       return resource_usage.ru_ixrss;
00172     }
00173 
00174     long currentUnsharedDataMemory()
00175     {
00176       return resource_usage.ru_idrss;
00177     }
00178 
00179     long currentUnsharedStackMemory()
00180     {
00181       return resource_usage.ru_isrss;
00182     }
00183 
00184     long currentPageFaults()
00185     {
00186       return currentMinorPageFaults() + currentMajorPageFaults();
00187     }
00188 
00189     long currentMinorPageFaults()
00190     {
00191       return resource_usage.ru_minflt;
00192     }
00193 
00194     long currentMajorPageFaults()
00195     {
00196       return resource_usage.ru_majflt;
00197     }
00198 
00199     long currentBlockedInputOps()
00200     {
00201       return resource_usage.ru_inblock;
00202     }
00203 
00204     long currentBlockedOutputOps()
00205     {
00206       return resource_usage.ru_oublock;
00207     }
00208 
00209     void start()
00210     {
00211       updateTime();
00212       time_start = currentElapsed();
00213       updateResourceUsage();
00214       cpu_start = currentCpu();
00215       user_cpu_start = currentUserCpu();
00216       system_cpu_start = currentSystemCpu();
00217       max_memory_start = currentMaxMemory();
00218       shared_memory_start = currentSharedMemory();
00219       unshared_data_memory_start = currentUnsharedDataMemory();
00220       unshared_stack_memory_start = currentUnsharedStackMemory();
00221       page_faults_start = currentPageFaults();
00222       minor_page_faults_start = currentMinorPageFaults();
00223       major_page_faults_start = currentMajorPageFaults();
00224       blocked_input_ops_start = currentBlockedInputOps();
00225       blocked_output_ops_start = currentBlockedOutputOps();
00226     };
00227 };
00228 
00229 
00230 #endif
Generated on Mon Oct 4 10:39:55 2010 for Matching.kdevelop by  doxygen 1.6.3