root/hydranode/hnbase/speedmeter.h

Revision 2613, 4.3 kB (checked in by madcat, 3 years ago)

Copyright notice update for 2006.

Line 
1 /*
2  *  Copyright (C) 2004-2006 Alo Sarv <madcat_@users.sourceforge.net>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifndef __SPEEDMETER_H__
20 #define __SPEEDMETER_H__
21
22 /**
23  * \file speedmeter.h   Interface for SpeedMeter class
24  */
25
26 #include <hnbase/utils.h>
27 #include <hnbase/event.h>
28 #include <deque>
29
30 /**
31  * SpeedMeter class provides a generic, customizable speed-calculation API.
32  * It allows setting the history size, as well as resolution upon construction,
33  * and provides accessor for average speed over a specified time, defaulting
34  * to last 1000ms.
35  */
36 class HNBASE_EXPORT SpeedMeter : public Trackable {
37 public:
38         /**
39          * Default constructor, defaults to histSize 10 and 100ms resolution.
40          */
41         SpeedMeter();
42
43         /**
44          * Allows customizing history size and resolution.
45          *
46          * @param histSize      Number of values to keep in history
47          * @param resolution    Worst-case expected precision for getSpeed()
48          */
49         SpeedMeter(uint32_t histSize, uint32_t resolution);
50
51         /**
52          * Aquire the current speed.
53          *
54          * @param range        Time range for the requested speed, in
55          *                     milliseconds, e.g. 1000 for last seconds average,
56          *                     5000 for last 5 seconds average, et al. Note that
57          *                     if there's not enough history data for the
58          *                     calculation, it's "guessed" from known data.
59          * @returns            The current speed, in worst-case-precision of
60          *                     <b>resolution</b>.
61          *
62          * \note The const-cast is an implementation-artifact - in order to
63          *       aquire current speed, the internal structures must be refeshed,
64          *       however this method must remain const to the user.
65          */
66         uint32_t getSpeed(float range = 1000.0) const {
67                 *const_cast<SpeedMeter*>(this) += 0;
68                 return static_cast<uint32_t>(
69                         m_current / (m_histSize * m_res / range)
70                 );
71         }
72
73         /**
74          * @name Generic accessors
75          */
76         //!@{
77         uint64_t getTotal()      const { return m_total;    }
78         uint32_t getHistSize()   const { return m_histSize; }
79         uint32_t getResolution() const { return m_res;      }
80         //!@}
81
82         /**
83          * Add data to the meter.
84          *
85          * @param amount       Amount to be added
86          * @returns            *this pointer
87          */
88         SpeedMeter& operator+=(uint32_t amount);
89
90         /**
91          * By default, SpeedMeter uses Utils::getTick() calls directly for time-
92          * calculations, but this may not be desired (for platforms where
93          * Utils::getTick() is expensive). Hence, this method allows setting a
94          * reference variable, which holds current tick value (probably set from
95          * application mainloop or similar), thus avoiding direct getTick()
96          * calls.
97          *
98          * \note SpeedMeter keeps a pointer to this value until unsetTicker()
99          *       is called. Make sure to call unsetTicker() before destroying
100          *       this variable.
101          */
102         static void setTicker(const uint64_t &tick);
103
104         /**
105          * Removes the external ticker variable.
106          */
107         static void unsetTicker();
108 private:
109         std::deque<uint32_t> m_history;    //!< History data
110         uint32_t m_recent;                 //!< Most recent history value
111         uint32_t m_current;                //!< Current speed
112         uint64_t m_total;                  //!< Total data added
113         uint64_t m_lastReset;              //!< Time of last history update
114
115         uint32_t m_histSize;               //!< Size of history
116         uint32_t m_res;                    //!< History values resolution
117
118         static const uint64_t *m_ticker;   //!< Optional tick container
119 };
120
121 /**
122  * Output operator to streams writes human-readable (converted to MB/s et al)
123  * version of current speed, using default getSpeed() value.
124  */
125 inline std::ostream& operator<<(std::ostream &o, const SpeedMeter &speed) {
126         return o << Utils::bytesToString(speed.getSpeed());
127 }
128
129 #endif
Note: See TracBrowser for help on using the browser.