Branch data Line data Source code
1 : : // $Id: Timer.h 6219 2008-10-01 05:39:07Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef timer_h
6 : : #define timer_h
7 : :
8 : : #include <string>
9 : :
10 : : #include <string>
11 : : #include "SerialObj.h"
12 : : #include "PriorityQueue.h"
13 : :
14 : : extern "C" {
15 : : #include "cq.h"
16 : : }
17 : :
18 : : // If you add a timer here, adjust TimerNames in Timer.cc.
19 : : enum TimerType {
20 : : TIMER_BACKDOOR,
21 : : TIMER_BREAKPOINT,
22 : : TIMER_CONN_DELETE,
23 : : TIMER_CONN_EXPIRE,
24 : : TIMER_CONN_INACTIVITY,
25 : : TIMER_CONN_STATUS_UPDATE,
26 : : TIMER_DNS_EXPIRE,
27 : : TIMER_FRAG,
28 : : TIMER_INCREMENTAL_SEND,
29 : : TIMER_INCREMENTAL_WRITE,
30 : : TIMER_INTERCONN,
31 : : TIMER_NB_EXPIRE,
32 : : TIMER_NETWORK,
33 : : TIMER_NTP_EXPIRE,
34 : : TIMER_PROFILE,
35 : : TIMER_ROTATE,
36 : : TIMER_REMOVE_CONNECTION,
37 : : TIMER_RPC_EXPIRE,
38 : : TIMER_SCHEDULE,
39 : : TIMER_TABLE_VAL,
40 : : TIMER_TCP_ATTEMPT,
41 : : TIMER_TCP_DELETE,
42 : : TIMER_TCP_EXPIRE,
43 : : TIMER_TCP_PARTIAL_CLOSE,
44 : : TIMER_TCP_RESET,
45 : : TIMER_TRIGGER,
46 : : TIMER_TIMERMGR_EXPIRE,
47 : : };
48 : : const int NUM_TIMER_TYPES = int(TIMER_TIMERMGR_EXPIRE) + 1;
49 : :
50 : : extern const char* timer_type_to_string(TimerType type);
51 : :
52 : : class Serializer;
53 : : class ODesc;
54 : :
55 : : class Timer : public SerialObj, public PQ_Element {
56 : : public:
57 : 29889 : Timer(double t, TimerType arg_type) : PQ_Element(t)
58 : 29889 : { type = (char) arg_type; }
59 [ # # ][ # # ]: 0 : virtual ~Timer() { }
60 : :
61 : 117102 : TimerType Type() const { return (TimerType) type; }
62 : :
63 : : // t gives the dispatch time. is_expire is true if the
64 : : // timer is being dispatched because we're expiring all
65 : : // pending timers.
66 : : virtual void Dispatch(double t, int is_expire) = 0;
67 : :
68 : : void Describe(ODesc* d) const;
69 : :
70 : : bool Serialize(SerialInfo* info) const;
71 : : static Timer* Unserialize(UnserialInfo* info);
72 : :
73 : : protected:
74 : 0 : Timer() {}
75 : :
76 : : DECLARE_ABSTRACT_SERIAL(Timer);
77 : :
78 : : unsigned int type:8;
79 : : };
80 : :
81 : : class TimerMgr {
82 : : public:
83 : : virtual ~TimerMgr();
84 : :
85 : : virtual void Add(Timer* timer) = 0;
86 : :
87 : : // Advance the clock to time t, expiring at most max_expire timers.
88 : : // Returns number of timers expired.
89 : : int Advance(double t, int max_expire);
90 : :
91 : : // Returns the number of timers expired (so far) during the current
92 : : // or most recent advance.
93 : : int NumExpiredDuringCurrentAdvance() { return num_expired; }
94 : :
95 : : // Expire all timers.
96 : : virtual void Expire() = 0;
97 : :
98 : : // Cancel() is a method separate from Remove because
99 : : // (1) Remove is protected, but, more importantly, (2) in some
100 : : // timer schemes we have wound up separating timer cancelation
101 : : // from removing it from the manager's data structures, because
102 : : // the manager lacked an efficient way to find it.
103 : 2310 : void Cancel(Timer* timer) { Remove(timer); }
104 : :
105 [ + + ]: 42699 : double Time() const { return t ? t : 1; } // 1 > 0
106 : :
107 : : typedef std::string Tag;
108 : 0 : const Tag& GetTag() const { return tag; }
109 : :
110 : : virtual int Size() const = 0;
111 : : virtual int PeakSize() const = 0;
112 : :
113 : 0 : double LastTimestamp() const { return last_timestamp; }
114 : : // Returns time of last advance in global network time.
115 : 0 : double LastAdvance() const { return last_advance; }
116 : :
117 : 0 : static unsigned int* CurrentTimers() { return current_timers; }
118 : :
119 : : protected:
120 : 3 : TimerMgr(const Tag& arg_tag)
121 : 3 : {
122 : 3 : t = 0.0;
123 : 3 : num_expired = 0;
124 : 3 : last_advance = last_timestamp = 0;
125 : 3 : tag = arg_tag;
126 : 3 : }
127 : :
128 : : virtual int DoAdvance(double t, int max_expire) = 0;
129 : : virtual void Remove(Timer* timer) = 0;
130 : :
131 : : double t;
132 : : double last_timestamp;
133 : : double last_advance;
134 : : Tag tag;
135 : :
136 : : int num_expired;
137 : :
138 : : static unsigned int current_timers[NUM_TIMER_TYPES];
139 : : };
140 : :
141 : : class PQ_TimerMgr : public TimerMgr {
142 : : public:
143 : : PQ_TimerMgr(const Tag& arg_tag);
144 : : ~PQ_TimerMgr();
145 : :
146 : : void Add(Timer* timer);
147 : : void Expire();
148 : :
149 : 0 : int Size() const { return q->Size(); }
150 : 0 : int PeakSize() const { return q->PeakSize(); }
151 : : unsigned int MemoryUsage() const;
152 : :
153 : : protected:
154 : : int DoAdvance(double t, int max_expire);
155 : : void Remove(Timer* timer);
156 : :
157 [ + + ]: 27509 : Timer* Remove() { return (Timer*) q->Remove(); }
158 [ + - ]: 48810 : Timer* Top() { return (Timer*) q->Top(); }
159 : :
160 : : PriorityQueue* q;
161 : : };
162 : :
163 : : class CQ_TimerMgr : public TimerMgr {
164 : : public:
165 : : CQ_TimerMgr(const Tag& arg_tag);
166 : : ~CQ_TimerMgr();
167 : :
168 : : void Add(Timer* timer);
169 : : void Expire();
170 : :
171 : 0 : int Size() const { return cq_size(cq); }
172 : 0 : int PeakSize() const { return cq_max_size(cq); }
173 : : unsigned int MemoryUsage() const;
174 : :
175 : : protected:
176 : : int DoAdvance(double t, int max_expire);
177 : : void Remove(Timer* timer);
178 : :
179 : : struct cq_handle *cq;
180 : : };
181 : :
182 : : extern TimerMgr* timer_mgr;
183 : :
184 : : #endif
|