------------------------------------------------------------ revno: 12647 revision-id: squid3@treenet.co.nz-20131103083757-bjt1kj7pg3dbhwqv parent: squid3@treenet.co.nz-20131103083521-mds5qybjg9nl7bh4 author: Alex Rousskov committer: Amos Jeffries branch nick: 3.3 timestamp: Sun 2013-11-03 01:37:57 -0700 message: Avoid "hot idle": A series of rapid select() calls with zero timeout. Squid uses "infinite" precision when it comes to deciding whether the next timed event is ready but uses millisecond (1e-3) precision when deciding how long to wait before the next event will be ready. This inconsistency results in the EventScheduler engine telling the main loop that it has 0 milliseconds to poll pending I/O, but when asked again (after the I/O is quickly polled), the EventScheduler engine often does not schedule the promised event and tells the main loop to poll for another 0 milliseconds again. This cycling may happen many times in a row (until enough time is wasted for the next event to become ready using higher precision). The fixed code adds a minimum 1ms delay for not-yet-ready events. It also places both decisions into one method (EventScheduler::timeRemaining), and tries to polish/document decision logic (which is more complex than it may seem) because the code has to avoid both inconsistent decisions and hot idle loops while maintaining the traditional "no event is fired before it is due" guarantee. TODO: Idle Squid still runs hotter than it should because the maximum waiting time is artificially capped outside the event queue to EVENT_LOOP_TIMEOUT=1s. This causes at most one extra loop iteration per second. ------------------------------------------------------------ # Bazaar merge directive format 2 (Bazaar 0.90) # revision_id: squid3@treenet.co.nz-20131103083757-bjt1kj7pg3dbhwqv # target_branch: http://bzr.squid-cache.org/bzr/squid3/3.3 # testament_sha1: 96c6cbb607aeae6ee43c9d9b842f568eaac8d18e # timestamp: 2013-11-03 08:45:18 +0000 # source_branch: http://bzr.squid-cache.org/bzr/squid3/3.3 # base_revision_id: squid3@treenet.co.nz-20131103083521-\ # mds5qybjg9nl7bh4 # # Begin patch === modified file 'src/event.cc' --- src/event.cc 2012-09-01 14:38:36 +0000 +++ src/event.cc 2013-11-03 08:37:57 +0000 @@ -39,6 +39,10 @@ #include "profiler/Profiler.h" #include "tools.h" +#if HAVE_MATH_H +#include +#endif + /* The list of event processes */ static OBJH eventDump; @@ -219,39 +223,37 @@ debug_trap("eventDelete: event not found"); } +// The event API does not guarantee exact timing, but guarantees that no event +// is fired before it is due. We may delay firing, but never fire too early. int -EventScheduler::checkDelay() +EventScheduler::timeRemaining() const { if (!tasks) return EVENT_IDLE; - int result = (int) ((tasks->when - current_dtime) * 1000); - - if (result < 0) - return 0; - - return result; + if (tasks->when <= current_dtime) // we are on time or late + return 0; // fire the event ASAP + + const double diff = tasks->when - current_dtime; // microseconds + // Round UP: If we come back a nanosecond earlier, we will wait again! + const int timeLeft = static_cast(ceil(1000*diff)); // milliseconds + // Avoid hot idle: A series of rapid select() calls with zero timeout. + const int minDelay = 1; // millisecond + return max(minDelay, timeLeft); } int EventScheduler::checkEvents(int timeout) { - - ev_entry *event = NULL; - - if (NULL == tasks) - return checkDelay(); - - if (tasks->when > current_dtime) - return checkDelay(); + int result = timeRemaining(); + if (result != 0) + return result; PROF_start(eventRun); - debugs(41, 5, HERE << "checkEvents"); - - while ((event = tasks)) { - if (event->when > current_dtime) - break; + do { + ev_entry *event = tasks; + assert(event); /* XXX assumes event->name is static memory! */ AsyncCall::Pointer call = asyncCall(41,5, event->name, @@ -265,14 +267,16 @@ tasks = event->next; delete event; + result = timeRemaining(); + // XXX: We may be called again during the same event loop iteration. // Is there a point in breaking now? if (heavy) break; // do not dequeue events following a heavy event - } + } while (result == 0); PROF_stop(eventRun); - return checkDelay(); + return result; } void === modified file 'src/event.h' --- src/event.h 2012-09-20 11:28:21 +0000 +++ src/event.h 2013-11-03 08:37:57 +0000 @@ -80,8 +80,8 @@ void cancel(EVH * func, void * arg); /* clean up the used memory in the scheduler */ void clean(); - /* how long until the next event ? */ - int checkDelay(); + /* either EVENT_IDLE or milliseconds remaining until the next event */ + int timeRemaining() const; /* cache manager output for the event queue */ void dump(StoreEntry *); /* find a scheduled event */ === modified file 'src/tests/stub_event.cc' --- src/tests/stub_event.cc 2012-01-20 18:55:04 +0000 +++ src/tests/stub_event.cc 2013-11-03 08:37:57 +0000 @@ -21,8 +21,8 @@ EventScheduler::EventScheduler() STUB EventScheduler::~EventScheduler() STUB void EventScheduler::cancel(EVH * func, void * arg) STUB +int EventScheduler::timeRemaining() const STUB_RETVAL(1) void EventScheduler::clean() STUB -int EventScheduler::checkDelay() STUB_RETVAL(-1) void EventScheduler::dump(StoreEntry *) STUB bool EventScheduler::find(EVH * func, void * arg) STUB_RETVAL(false) void EventScheduler::schedule(const char *name, EVH * func, void *arg, double when, int weight, bool cbdata) STUB