1 #ifndef RATE_H 2 #define RATE_H 3 4 #include "def.h" 5 #include <inttypes.h> 6 #include <sys/types.h> 7 #include <time.h> 8 9 typedef struct _bwsample{ 10 double timestamp; 11 unsigned long bytes; 12 struct _bwsample *next; 13 }BWSAMPLE; 14 15 class Rate{ 16 private: 17 time_t m_last_timestamp; 18 time_t m_total_timeused; 19 uint64_t m_count_bytes; 20 // m_last: tracks recent xfer(s) for timing & limit comparison 21 // m_recent: the most recent measurable xfer 22 // m_prev: the prior m_recent 23 double m_last_realtime, m_recent_realtime, m_prev_realtime; 24 size_t m_last_size, m_recent_size, m_prev_size; 25 double m_late; 26 size_t m_nominal; 27 time_t m_nom_time; 28 struct{ 29 size_t value; 30 time_t lasttime; 31 double recent; 32 } m_lastrate; 33 34 unsigned char m_ontime:1; 35 unsigned char m_update_nominal:1; 36 unsigned char m_reserved:6; 37 38 BWSAMPLE *m_history, *m_history_last; // bandwidth history data 39 40 Rate *m_selfrate; 41 42 static BWSAMPLE *NewSample(); 43 44 public: 45 Rate(); 46 47 void Reset(); 48 void StartTimer(); 49 void StopTimer(); 50 void ClearHistory(); 51 void Cleanup(); 52 void CountAdd(size_t nbytes); 53 void UnCount(size_t nbytes); 54 void RateAdd(size_t nbytes, size_t bwlimit); 55 void RateAdd(size_t nbytes, size_t bwlimit, double timestamp); 56 void operator=(const Rate &ra); Count()57 uint64_t Count() const { return m_count_bytes; } 58 size_t CurrentRate(); 59 size_t NominalRate(); 60 size_t RateMeasure(); 61 size_t RateMeasure(const Rate &ra); 62 time_t TimeUsed(); LastRealtime()63 double LastRealtime() const { return m_last_realtime; } LastSize()64 size_t LastSize() const { return m_last_size; } SetSelf(Rate * rate)65 void SetSelf(Rate *rate) { m_selfrate = rate; } Late()66 double Late() const { return m_late; } Ontime()67 int Ontime() const { return m_ontime ? 1 : 0; } Ontime(int yn)68 void Ontime(int yn) { m_ontime = yn ? 1 : 0; } 69 }; 70 71 #endif 72