00001 // BLRealTimeMonitor.hh 00002 /* 00003 This source file is part of G4beamline, http://g4beamline.muonsinc.com 00004 Copyright (C) 2003,2004,2005,2006 by Tom Roberts, all rights reserved. 00005 00006 This program is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU General Public License 00008 as published by the Free Software Foundation; either version 2 00009 of the License, or (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 http://www.gnu.org/copyleft/gpl.html 00017 */ 00018 00019 #ifndef BLREALTIMEMONITOR_HH 00020 #define BLREALTIMEMONITOR_HH 00021 00022 #include "BLTime.hh" 00023 00024 /** class BLRealTimeMonitor creates a versatile multi-state monitor 00025 * of real time usage, with resolution of microseconds (or the 00026 * system clock resolution). 00027 * 00028 * This entire class is inline. 00029 **/ 00030 class BLRealTimeMonitor { 00031 long long total[8]; 00032 int state; 00033 long long last; 00034 public: 00035 /// constructor and destructor. 00036 BLRealTimeMonitor() { for(int i=0; i<8; ++i) total[i]=0LL; state=-1; 00037 last=0LL; 00038 } 00039 ~BLRealTimeMonitor() { } 00040 00041 /// setState() stops accumulating in the previous state, and starts 00042 /// accumulating in the new state. 00043 /// state can range from 0 to 7; -1 means no accumulation. 00044 /// initial state is -1. 00045 void setState(int newState) { 00046 long long now = BLTime::timeus(); 00047 if(now < last) fprintf(stderr,"now=%lld last=%lld\n",now,last); 00048 last = now; 00049 if(state >= 0 && state < 8) total[state] += now; 00050 if(state>=0 && total[state] < 0LL) fprintf(stderr,"state=%d total=%lld now=%lld last=%lld\n",state,total[state],now,last); 00051 state = newState; 00052 if(state >= 0 && state < 8) total[state] -= now; 00053 } 00054 00055 /// incrState() adds the increment to the current state. 00056 void incrState(int incr=1) { setState(state+incr); } 00057 00058 /// getTime() returns the total elapsed time (in seconds) while in the 00059 /// specified state. 00060 float getTime(int _state) { 00061 if(_state < 0 || _state >= 8) return 0.0; 00062 long long v=total[_state]; 00063 if(state == _state) v += BLTime::timeus(); 00064 return (float)v/1.0E6; 00065 } 00066 00067 /// estimateResolution() will estimate the resolution of the 00068 /// real-time monitor. 00069 static float estimateResolution() { 00070 float v=0.0; 00071 for(int i=0; i<100; ++i) { 00072 v += estimateResolution1(); 00073 if(v > 0.1) 00074 return v/(i+1); 00075 } 00076 return v/100.0; 00077 } 00078 00079 /// estimateResolution1() will make a single estimate of the 00080 /// resolution of the real-time monitor. 00081 static float estimateResolution1() { 00082 long long now=0LL, prev=BLTime::timeus(); 00083 do { 00084 now = BLTime::timeus(); 00085 } while(now == prev); 00086 prev = now; 00087 do { 00088 now = BLTime::timeus(); 00089 } while(now == prev); 00090 return (float)(now-prev)/1.0E6; 00091 } 00092 }; 00093 00094 #endif // BLREALTIMEMONITOR_HH