]> git.ipfire.org Git - thirdparty/squid.git/blob - src/event.cc
Various audit updates
[thirdparty/squid.git] / src / event.cc
1 /*
2 * DEBUG: section 41 Event Processing
3 * AUTHOR: Henrik Nordstrom
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34 #include "compat/drand48.h"
35 #include "event.h"
36 #include "mgr/Registration.h"
37 #include "profiler/Profiler.h"
38 #include "SquidTime.h"
39 #include "Store.h"
40 #include "tools.h"
41
42 #include <cmath>
43
44 /* The list of event processes */
45
46 static OBJH eventDump;
47 static const char *last_event_ran = NULL;
48
49 // This AsyncCall dialer can be configured to check that the event cbdata is
50 // valid before calling the event handler
51 class EventDialer: public CallDialer
52 {
53 public:
54 typedef CallDialer Parent;
55
56 EventDialer(EVH *aHandler, void *anArg, bool lockedArg);
57 EventDialer(const EventDialer &d);
58 virtual ~EventDialer();
59
60 virtual void print(std::ostream &os) const;
61 virtual bool canDial(AsyncCall &call);
62
63 void dial(AsyncCall &) { theHandler(theArg); }
64
65 private:
66 EVH *theHandler;
67 void *theArg;
68 bool isLockedArg;
69 };
70
71 EventDialer::EventDialer(EVH *aHandler, void *anArg, bool lockedArg):
72 theHandler(aHandler), theArg(anArg), isLockedArg(lockedArg)
73 {
74 if (isLockedArg)
75 (void)cbdataReference(theArg);
76 }
77
78 EventDialer::EventDialer(const EventDialer &d):
79 theHandler(d.theHandler), theArg(d.theArg), isLockedArg(d.isLockedArg)
80 {
81 if (isLockedArg)
82 (void)cbdataReference(theArg);
83 }
84
85 EventDialer::~EventDialer()
86 {
87 if (isLockedArg)
88 cbdataReferenceDone(theArg);
89 }
90
91 bool
92 EventDialer::canDial(AsyncCall &call)
93 {
94 // TODO: add Parent::canDial() that always returns true
95 //if (!Parent::canDial())
96 // return false;
97
98 if (isLockedArg && !cbdataReferenceValid(theArg))
99 return call.cancel("stale handler data");
100
101 return true;
102 }
103
104 void
105 EventDialer::print(std::ostream &os) const
106 {
107 os << '(';
108 if (theArg)
109 os << theArg << (isLockedArg ? "*?" : "");
110 os << ')';
111 }
112
113 ev_entry::ev_entry(char const * aName, EVH * aFunction, void * aArgument, double evWhen,
114 int aWeight, bool haveArgument) : name(aName), func(aFunction),
115 arg(haveArgument ? cbdataReference(aArgument) : aArgument), when(evWhen), weight(aWeight),
116 cbdata(haveArgument)
117 {
118 }
119
120 ev_entry::~ev_entry()
121 {
122 if (cbdata)
123 cbdataReferenceDone(arg);
124 }
125
126 void
127 eventAdd(const char *name, EVH * func, void *arg, double when, int weight, bool cbdata)
128 {
129 EventScheduler::GetInstance()->schedule(name, func, arg, when, weight, cbdata);
130 }
131
132 /* same as eventAdd but adds a random offset within +-1/3 of delta_ish */
133 void
134 eventAddIsh(const char *name, EVH * func, void *arg, double delta_ish, int weight)
135 {
136 if (delta_ish >= 3.0) {
137 const double two_third = (2.0 * delta_ish) / 3.0;
138 delta_ish = two_third + (drand48() * two_third);
139 /*
140 * I'm sure drand48() isn't portable. Tell me what function
141 * you have that returns a random double value in the range 0,1.
142 */
143 }
144
145 eventAdd(name, func, arg, delta_ish, weight);
146 }
147
148 void
149 eventDelete(EVH * func, void *arg)
150 {
151 EventScheduler::GetInstance()->cancel(func, arg);
152 }
153
154 void
155 eventInit(void)
156 {
157 Mgr::RegisterAction("events", "Event Queue", eventDump, 0, 1);
158 }
159
160 static void
161 eventDump(StoreEntry * sentry)
162 {
163 EventScheduler::GetInstance()->dump(sentry);
164 }
165
166 void
167 eventFreeMemory(void)
168 {
169 EventScheduler::GetInstance()->clean();
170 }
171
172 int
173 eventFind(EVH * func, void *arg)
174 {
175 return EventScheduler::GetInstance()->find(func, arg);
176 }
177
178 EventScheduler EventScheduler::_instance;
179
180 EventScheduler::EventScheduler(): tasks(NULL)
181 {}
182
183 EventScheduler::~EventScheduler()
184 {
185 clean();
186 }
187
188 void
189 EventScheduler::cancel(EVH * func, void *arg)
190 {
191 ev_entry **E;
192 ev_entry *event;
193
194 for (E = &tasks; (event = *E) != NULL; E = &(*E)->next) {
195 if (event->func != func)
196 continue;
197
198 if (arg && event->arg != arg)
199 continue;
200
201 *E = event->next;
202
203 delete event;
204
205 if (arg)
206 return;
207 /*
208 * DPW 2007-04-12
209 * Since this method may now delete multiple events (when
210 * arg is NULL) it no longer returns after a deletion and
211 * we have a potential NULL pointer problem. If we just
212 * deleted the last event in the list then *E is now equal
213 * to NULL. We need to break here or else we'll get a NULL
214 * pointer dereference in the last clause of the for loop.
215 */
216 if (NULL == *E)
217 break;
218 }
219
220 if (arg)
221 debug_trap("eventDelete: event not found");
222 }
223
224 // The event API does not guarantee exact timing, but guarantees that no event
225 // is fired before it is due. We may delay firing, but never fire too early.
226 int
227 EventScheduler::timeRemaining() const
228 {
229 if (!tasks)
230 return EVENT_IDLE;
231
232 if (tasks->when <= current_dtime) // we are on time or late
233 return 0; // fire the event ASAP
234
235 const double diff = tasks->when - current_dtime; // microseconds
236 // Round UP: If we come back a nanosecond earlier, we will wait again!
237 const int timeLeft = static_cast<int>(ceil(1000*diff)); // milliseconds
238 // Avoid hot idle: A series of rapid select() calls with zero timeout.
239 const int minDelay = 1; // millisecond
240 return max(minDelay, timeLeft);
241 }
242
243 int
244 EventScheduler::checkEvents(int timeout)
245 {
246 int result = timeRemaining();
247 if (result != 0)
248 return result;
249
250 PROF_start(eventRun);
251
252 do {
253 ev_entry *event = tasks;
254 assert(event);
255
256 /* XXX assumes event->name is static memory! */
257 AsyncCall::Pointer call = asyncCall(41,5, event->name,
258 EventDialer(event->func, event->arg, event->cbdata));
259 ScheduleCallHere(call);
260
261 last_event_ran = event->name; // XXX: move this to AsyncCallQueue
262 const bool heavy = event->weight &&
263 (!event->cbdata || cbdataReferenceValid(event->arg));
264
265 tasks = event->next;
266 delete event;
267
268 result = timeRemaining();
269
270 // XXX: We may be called again during the same event loop iteration.
271 // Is there a point in breaking now?
272 if (heavy)
273 break; // do not dequeue events following a heavy event
274 } while (result == 0);
275
276 PROF_stop(eventRun);
277 return result;
278 }
279
280 void
281 EventScheduler::clean()
282 {
283 while (ev_entry * event = tasks) {
284 tasks = event->next;
285 delete event;
286 }
287
288 tasks = NULL;
289 }
290
291 void
292 EventScheduler::dump(StoreEntry * sentry)
293 {
294
295 ev_entry *e = tasks;
296
297 if (last_event_ran)
298 storeAppendPrintf(sentry, "Last event to run: %s\n\n", last_event_ran);
299
300 storeAppendPrintf(sentry, "%-25s\t%-15s\t%s\t%s\n",
301 "Operation",
302 "Next Execution",
303 "Weight",
304 "Callback Valid?");
305
306 while (e != NULL) {
307 storeAppendPrintf(sentry, "%-25s\t%0.3f sec\t%5d\t %s\n",
308 e->name, e->when ? e->when - current_dtime : 0, e->weight,
309 (e->arg && e->cbdata) ? cbdataReferenceValid(e->arg) ? "yes" : "no" : "N/A");
310 e = e->next;
311 }
312 }
313
314 bool
315 EventScheduler::find(EVH * func, void * arg)
316 {
317
318 ev_entry *event;
319
320 for (event = tasks; event != NULL; event = event->next) {
321 if (event->func == func && event->arg == arg)
322 return true;
323 }
324
325 return false;
326 }
327
328 EventScheduler *
329 EventScheduler::GetInstance()
330 {
331 return &_instance;
332 }
333
334 void
335 EventScheduler::schedule(const char *name, EVH * func, void *arg, double when, int weight, bool cbdata)
336 {
337 // Use zero timestamp for when=0 events: Many of them are async calls that
338 // must fire in the submission order. We cannot use current_dtime for them
339 // because it may decrease if system clock is adjusted backwards.
340 const double timestamp = when > 0.0 ? current_dtime + when : 0;
341 ev_entry *event = new ev_entry(name, func, arg, timestamp, weight, cbdata);
342
343 ev_entry **E;
344 debugs(41, 7, HERE << "schedule: Adding '" << name << "', in " << when << " seconds");
345 /* Insert after the last event with the same or earlier time */
346
347 for (E = &tasks; *E; E = &(*E)->next) {
348 if ((*E)->when > event->when)
349 break;
350 }
351
352 event->next = *E;
353 *E = event;
354 }