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 public: 00034 /// constructor and destructor. 00035 BLRealTimeMonitor() { for(int i=0; i<8; ++i) total[i]=0LL; state=-1; 00036 } 00037 ~BLRealTimeMonitor() { } 00038 00039 /// setState() stops accumulating in the previous state, and starts 00040 /// accumulating in the new state. 00041 /// state can range from 0 to 7; -1 means no accumulation. 00042 /// initial state is -1. 00043 void setState(int newState) { 00044 long long now = BLTime::timeus(); 00045 if(state >= 0 && state < 8) total[state] += now; 00046 state = newState; 00047 if(state >= 0 && state < 8) total[state] -= now; 00048 } 00049 00050 /// incrState() adds the increment to the current state. 00051 void incrState(int incr=1) { setState(state+incr); } 00052 00053 /// getTime() returns the total elapsed time (in seconds) while in the 00054 /// specified state. 00055 float getTime(int _state) { 00056 if(_state < 0 || _state >= 8) return 0.0; 00057 long long v=total[_state]; 00058 if(state == _state) v += BLTime::timeus(); 00059 return (float)v/1.0E6; 00060 } 00061 00062 /// estimateResolution() will estimate the resolution of the 00063 /// real-time monitor. 00064 static float estimateResolution() { 00065 float v=0.0; 00066 for(int i=0; i<100; ++i) { 00067 v += estimateResolution1(); 00068 if(v > 0.1) 00069 return v/(i+1); 00070 } 00071 return v/100.0; 00072 } 00073 00074 /// estimateResolution1() will make a single estimate of the 00075 /// resolution of the real-time monitor. 00076 static float estimateResolution1() { 00077 long long now=0LL, prev=BLTime::timeus(); 00078 do { 00079 now = BLTime::timeus(); 00080 } while(now == prev); 00081 prev = now; 00082 do { 00083 now = BLTime::timeus(); 00084 } while(now == prev); 00085 return (float)(now-prev)/1.0E6; 00086 } 00087 }; 00088 00089 #endif // BLREALTIMEMONITOR_HH