1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #ifndef POLYGRAPH__BASE_LEVELSTAT_H
7 #define POLYGRAPH__BASE_LEVELSTAT_H
8 
9 #include "xstd/Time.h"
10 
11 class String;
12 class OLog;
13 class ILog;
14 
15 // [concurrency] level statistics
16 class LevelStat {
17 	public:
18 		LevelStat();
19 
20 		LevelStat &operator ++() { inc(); return *this; }
21 		LevelStat &operator --() { dec(); return *this; }
22 
active()23 		bool active() const { return theIncCnt > 0 || theDecCnt > 0; }
known()24 		bool known() const { return theLevel != 0 || theStart >= 0; }
incCnt()25 		int incCnt() const { return theIncCnt; }
decCnt()26 		int decCnt() const { return theDecCnt; }
level()27 		int level() const { return theLevel; }
28 		double mean() const;
29 
30 		void restart();
31 		void store(OLog &log) const;
32 		void load(ILog &log);
33 		bool sane() const;
34 
35 		void keepLevel(const LevelStat &prevLevel);
36 		void merge(const LevelStat &s);
37 		void concat(const LevelStat &s);
38 
39 		ostream &print(ostream &os, const String &pfx) const;
40 
41 	protected:
inc()42 		void inc() { change(); theIncCnt++; theLevel++; }
dec()43 		void dec() { change(); theDecCnt++; theLevel--; }
44 		void change();
45 
46 		void join(const LevelStat &s);
47 
48 	private:
49 		double nom() const;
50 		double denom() const;
51 
52 	protected:
53 		int theIncCnt;
54 		int theDecCnt;
55 		int theLevel;     // internal, not reset on restart
56 
57 		Time theStart;    // start of measurements
58 		Time theCurStart; // start of the current level
59 		Time theSum;      // "level-seconds" (nominator of an average)
60 
61 	private:
62 		double theNom;    // these are used for joining
63 		double theDenom;  // LevelStats only
64 };
65 
66 
67 inline OLog &operator <<(OLog &log, const LevelStat &ls) { ls.store(log); return log; }
68 inline ILog &operator >>(ILog &log, LevelStat &ls) { ls.load(log); return log; }
69 
70 
71 #endif
72