OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2Stats.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // network statistics and information dialogs
17 
18 #ifndef INC_C4Network2Stats
19 #define INC_C4Network2Stats
20 
21 #include "game/C4Application.h"
22 #include "lib/StdBuf.h"
23 
24 #include <algorithm>
25 
26 // (int) value by time function
27 class C4Graph
28 {
29 public:
30  typedef float ValueType;
31  typedef int TimeType;
32 
33 private:
34  StdStrBuf szTitle;
35  DWORD dwColor;
36 
37 public:
38  C4Graph();
39  virtual ~C4Graph() { }
40 
41  void SetTitle(const char *szNewTitle) { szTitle.Copy(szNewTitle); }
42  void SetColorDw(DWORD dwClr) { dwColor = dwClr; }
43 
44  // retrieve timeframe covered by backlog
45  virtual TimeType GetStartTime() const = 0; // inclusively
46  virtual TimeType GetEndTime() const = 0; // exclusively
47 
48  // retrieve values within backlog timeframe - guarantueed to be working inside [GetStartTime(), GetEndTime()[
49  virtual ValueType GetValue(TimeType iAtTime) const = 0;
50  virtual ValueType GetMedianValue(TimeType iStartTime, TimeType iEndTime) const = 0; // median within [iStartTime, iEndTime[
51  virtual ValueType GetMinValue() const = 0;
52  virtual ValueType GetMaxValue() const = 0;
53 
54  // base graph has always just one series (self)
55  virtual int GetSeriesCount() const = 0;
56  virtual const C4Graph *GetSeries(int iIndex) const = 0;
57 
58  DWORD GetColorDw() const { return dwColor; }
59  const char *GetTitle() const { return szTitle.getData(); }
60 
61  // called before drawing procedure: Make sure graph values are up-to-date
62  virtual void Update() const {}
63 
64  virtual void SetAverageTime(int iToTime) = 0;
65  virtual void SetMultiplier(ValueType fToVal) = 0;
66 };
67 
68 // Standard graph: Assume constant time and one call each time frame
69 class C4TableGraph : public C4Graph
70 {
71 private:
72  // recorded backlog
73  int iBackLogLength;
74  ValueType *pValues;
75  ValueType fMultiplier; // multiplicative factor used for all value returns
76  mutable ValueType *pAveragedValues; // equals pValues if no average is being calculated
77 
78  // currently valid backlog timeframe
79  int iBackLogPos; // position of next entry to be written
80  bool fWrapped; // if true, the buffer has been cycled already
81 
82  TimeType iInitialStartTime; // initial table start time; used to calc offset in buffer
83  TimeType iTime; // next timeframe to be recorded
84  mutable TimeType iAveragedTime; // last timeframe for which average has been calculated
85  StdStrBuf szDumpFile; // if set, the buffer is dumped to file regularly
86  TimeType iAvgRange; // range used for averaging
87 
88 public:
89  enum { DefaultBlockLength = 256 }; // default backlog
90 
91  C4TableGraph(int iBackLogLength=DefaultBlockLength, TimeType iStartTime = 0);
92  ~C4TableGraph();
93 
94  // flush dump; reset time, etc
95  void Reset(TimeType iToTime);
96 
97  void SetDumpFile(StdStrBuf &szFile) { szDumpFile = szFile; }
98 
99  // retrieve timeframe covered by backlog
100  virtual TimeType GetStartTime() const; // inclusively
101  virtual TimeType GetEndTime() const; // exclusively
102 
103  // retrieve values within backlog timeframe - guarantueed to be working inside [GetStartTime(), GetEndTime()[
104  virtual ValueType GetValue(TimeType iAtTime) const; // retrieve averaged value!
105  ValueType GetAtValue(TimeType iAtTime) const; // retrieve not-averaged value
106  void SetAvgValue(TimeType iAtTime, ValueType iValue) const; // overwrite avg value at time - considered const because it modifies mutable only
107  virtual ValueType GetMedianValue(TimeType iStartTime, TimeType iEndTime) const; // median within [iStartTime, iEndTime[
108  virtual ValueType GetMinValue() const;
109  virtual ValueType GetMaxValue() const;
110 
111  // record a value for this frame; increases time by one
112  void RecordValue(ValueType iValue);
113 
114  // write table to file
115  virtual bool DumpToFile(const StdStrBuf &rszFilename, bool fAppend) const;
116 
117  // base graph has always just one series (self)
118  virtual int GetSeriesCount() const { return 1; }
119  virtual const C4Graph *GetSeries(int iIndex) const { return iIndex ? nullptr : this; }
120 
121  virtual void SetAverageTime(int iToTime);
122  virtual void Update() const; // make sure average times are correctly calculated
123 
124  virtual void SetMultiplier(ValueType fToVal) { fMultiplier = fToVal; }
125 };
126 
127 // A graph collection; grouping similar graphs
128 // Does not delete graph pointers
129 class C4GraphCollection : public C4Graph, private std::vector<C4Graph *>
130 {
131 private:
132  // if 0, keep individual for each graph
133  int iCommonAvgTime;
134  ValueType fMultiplier;
135 public:
136  C4GraphCollection() : iCommonAvgTime(0), fMultiplier(0) {}
137 
138  // retrieve max timeframe covered by all graphs
139  virtual C4Graph::TimeType GetStartTime() const;
140  virtual C4Graph::TimeType GetEndTime() const;
141 
142  // must be called on base series only!
143  virtual C4Graph::ValueType GetValue(C4Graph::TimeType iAtTime) const { assert(0); return 0; }
144  virtual C4Graph::ValueType GetMedianValue(C4Graph::TimeType iStartTime, C4Graph::TimeType iEndTime) const { assert(0); return 0; }
145 
146  // get overall min/max for all series
147  virtual C4Graph::ValueType GetMinValue() const;
148  virtual C4Graph::ValueType GetMaxValue() const;
149 
150  // retrieve child (or grandchild) graphs
151  virtual int GetSeriesCount() const;
152  virtual const C4Graph *GetSeries(int iIndex) const;
153 
154  void AddGraph(C4Graph *pAdd) { push_back(pAdd); if (iCommonAvgTime) pAdd->SetAverageTime(iCommonAvgTime); if (fMultiplier) pAdd->SetMultiplier(fMultiplier); }
155  void RemoveGraph(const C4Graph *pRemove) { iterator i=std::find(begin(), end(), pRemove); if (i!=end()) erase(i); }
156 
157  // update all children
158  virtual void Update() const;
159 
160  // force values for all children
161  virtual void SetAverageTime(int iToTime);
162  virtual void SetMultiplier(ValueType fToVal);
163 };
164 
165 // network stat collection wrapper
167 {
168 private:
169 
170  // per-frame stats
171  C4TableGraph statObjCount;
172 
173  // per-second stats
174  C4TableGraph statFPS;
175 
176  // overall network i/o
177  C4TableGraph statNetI, statNetO;
178  C4GraphCollection graphNetIO;
179 
180 protected:
181  C4GraphCollection statPings; // for all clients
182 
183  // per-controlframe stats
186 
187  int SecondCounter; // seconds passed in measured time by network stats module
188  int ControlCounter; // control frames passed in measured time by network stats module
189 
190  friend class C4Player;
191  friend class C4Network2Client;
192 
193 public:
194  C4Network2Stats();
195  virtual ~C4Network2Stats();
196 
197  // periodic callbacks
198  void ExecuteFrame();
199  void ExecuteSecond();
200  void ExecuteControlFrame();
201 
202  virtual void OnSec1Timer() { ExecuteSecond(); }
203 
204  C4Graph *GetGraphByName(const StdStrBuf &rszName, bool &rfIsTemp);
205 };
206 
207 #endif // INC_C4Network2Stats
virtual ValueType GetMaxValue() const
const char * getData() const
Definition: StdBuf.h:450
virtual void Update() const
virtual void SetAverageTime(int iToTime)
virtual const C4Graph * GetSeries(int iIndex) const =0
C4GraphCollection statControls
virtual void SetAverageTime(int iToTime)=0
virtual ValueType GetMedianValue(TimeType iStartTime, TimeType iEndTime) const =0
virtual C4Graph::ValueType GetMinValue() const
virtual TimeType GetStartTime() const
virtual void Update() const
void RemoveGraph(const C4Graph *pRemove)
C4Graph * GetGraphByName(const StdStrBuf &rszName, bool &rfIsTemp)
C4GraphCollection statPings
virtual int GetSeriesCount() const
virtual void SetMultiplier(ValueType fToVal)
virtual const C4Graph * GetSeries(int iIndex) const
virtual ValueType GetValue(TimeType iAtTime) const =0
virtual const C4Graph * GetSeries(int iIndex) const
virtual int GetSeriesCount() const =0
virtual ValueType GetMedianValue(TimeType iStartTime, TimeType iEndTime) const
virtual ~C4Network2Stats()
virtual C4Graph::TimeType GetEndTime() const
virtual C4Graph::ValueType GetMaxValue() const
virtual void Update() const
virtual ValueType GetMinValue() const =0
C4GraphCollection statActions
virtual C4Graph::ValueType GetValue(C4Graph::TimeType iAtTime) const
virtual ValueType GetValue(TimeType iAtTime) const
ValueType GetAtValue(TimeType iAtTime) const
void SetDumpFile(StdStrBuf &szFile)
virtual bool DumpToFile(const StdStrBuf &rszFilename, bool fAppend) const
virtual void SetMultiplier(ValueType fToVal)=0
virtual ~C4Graph()
virtual C4Graph::TimeType GetStartTime() const
virtual void SetAverageTime(int iToTime)
virtual TimeType GetStartTime() const =0
void RecordValue(ValueType iValue)
virtual ValueType GetMaxValue() const =0
virtual int GetSeriesCount() const
virtual void OnSec1Timer()
float ValueType
virtual ValueType GetMinValue() const
DWORD GetColorDw() const
virtual void SetMultiplier(ValueType fToVal)
void SetAvgValue(TimeType iAtTime, ValueType iValue) const
void SetTitle(const char *szNewTitle)
virtual TimeType GetEndTime() const
virtual C4Graph::ValueType GetMedianValue(C4Graph::TimeType iStartTime, C4Graph::TimeType iEndTime) const
uint32_t DWORD
void AddGraph(C4Graph *pAdd)
C4TableGraph(int iBackLogLength=DefaultBlockLength, TimeType iStartTime=0)
void Copy()
Definition: StdBuf.h:475
void Reset(TimeType iToTime)
virtual TimeType GetEndTime() const =0
void SetColorDw(DWORD dwClr)
const char * GetTitle() const