ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.pod
(Generate patch)

Comparing libev/ev.pod (file contents):
Revision 1.198 by root, Thu Oct 23 06:30:48 2008 UTC vs.
Revision 1.199 by root, Thu Oct 23 07:18:21 2008 UTC

1288passed, but if multiple timers become ready during the same loop iteration 1288passed, but if multiple timers become ready during the same loop iteration
1289then order of execution is undefined. 1289then order of execution is undefined.
1290 1290
1291=head3 Be smart about timeouts 1291=head3 Be smart about timeouts
1292 1292
1293Many real-world problems invole some kind of time-out, usually for error 1293Many real-world problems involve some kind of timeout, usually for error
1294recovery. A typical example is an HTTP request - if the other side hangs, 1294recovery. A typical example is an HTTP request - if the other side hangs,
1295you want to raise some error after a while. 1295you want to raise some error after a while.
1296 1296
1297Here are some ways on how to handle this problem, from simple and 1297What follows are some ways to handle this problem, from obvious and
1298inefficient to very efficient. 1298inefficient to smart and efficient.
1299 1299
1300In the following examples a 60 second activity timeout is assumed - a 1300In the following, a 60 second activity timeout is assumed - a timeout that
1301timeout that gets reset to 60 seconds each time some data ("a lifesign") 1301gets reset to 60 seconds each time there is activity (e.g. each time some
1302was received. 1302data or other life sign was received).
1303 1303
1304=over 4 1304=over 4
1305 1305
1306=item 1. Use a timer and stop, reinitialise, start it on activity. 1306=item 1. Use a timer and stop, reinitialise and start it on activity.
1307 1307
1308This is the most obvious, but not the most simple way: In the beginning, 1308This is the most obvious, but not the most simple way: In the beginning,
1309start the watcher: 1309start the watcher:
1310 1310
1311 ev_timer_init (timer, callback, 60., 0.); 1311 ev_timer_init (timer, callback, 60., 0.);
1312 ev_timer_start (loop, timer); 1312 ev_timer_start (loop, timer);
1313 1313
1314Then, each time there is some activity, C<ev_timer_stop> the timer, 1314Then, each time there is some activity, C<ev_timer_stop> it, initialise it
1315initialise it again, and start it: 1315and start it again:
1316 1316
1317 ev_timer_stop (loop, timer); 1317 ev_timer_stop (loop, timer);
1318 ev_timer_set (timer, 60., 0.); 1318 ev_timer_set (timer, 60., 0.);
1319 ev_timer_start (loop, timer); 1319 ev_timer_start (loop, timer);
1320 1320
1321This is relatively simple to implement, but means that each time there 1321This is relatively simple to implement, but means that each time there is
1322is some activity, libev will first have to remove the timer from it's 1322some activity, libev will first have to remove the timer from its internal
1323internal data strcuture and then add it again. 1323data structure and then add it again. Libev tries to be fast, but it's
1324still not a constant-time operation.
1324 1325
1325=item 2. Use a timer and re-start it with C<ev_timer_again> inactivity. 1326=item 2. Use a timer and re-start it with C<ev_timer_again> inactivity.
1326 1327
1327This is the easiest way, and involves using C<ev_timer_again> instead of 1328This is the easiest way, and involves using C<ev_timer_again> instead of
1328C<ev_timer_start>. 1329C<ev_timer_start>.
1329 1330
1330For this, configure an C<ev_timer> with a C<repeat> value of C<60> and 1331To implement this, configure an C<ev_timer> with a C<repeat> value
1331then call C<ev_timer_again> at start and each time you successfully read 1332of C<60> and then call C<ev_timer_again> at start and each time you
1332or write some data. If you go into an idle state where you do not expect 1333successfully read or write some data. If you go into an idle state where
1333data to travel on the socket, you can C<ev_timer_stop> the timer, and 1334you do not expect data to travel on the socket, you can C<ev_timer_stop>
1334C<ev_timer_again> will automatically restart it if need be. 1335the timer, and C<ev_timer_again> will automatically restart it if need be.
1335 1336
1336That means you can ignore the C<after> value and C<ev_timer_start> 1337That means you can ignore both the C<ev_timer_start> function and the
1337altogether and only ever use the C<repeat> value and C<ev_timer_again>. 1338C<after> argument to C<ev_timer_set>, and only ever use the C<repeat>
1339member and C<ev_timer_again>.
1338 1340
1339At start: 1341At start:
1340 1342
1341 ev_timer_init (timer, callback, 0., 60.); 1343 ev_timer_init (timer, callback);
1344 timer->repeat = 60.;
1342 ev_timer_again (loop, timer); 1345 ev_timer_again (loop, timer);
1343 1346
1344Each time you receive some data: 1347Each time there is some activity:
1345 1348
1346 ev_timer_again (loop, timer); 1349 ev_timer_again (loop, timer);
1347 1350
1348It is even possible to change the time-out on the fly: 1351It is even possible to change the time-out on the fly, regardless of
1352whether the watcher is active or not:
1349 1353
1350 timer->repeat = 30.; 1354 timer->repeat = 30.;
1351 ev_timer_again (loop, timer); 1355 ev_timer_again (loop, timer);
1352 1356
1353This is slightly more efficient then stopping/starting the timer each time 1357This is slightly more efficient then stopping/starting the timer each time
1354you want to modify its timeout value, as libev does not have to completely 1358you want to modify its timeout value, as libev does not have to completely
1355remove and re-insert the timer from/into it's internal data structure. 1359remove and re-insert the timer from/into its internal data structure.
1360
1361It is, however, even simpler than the "obvious" way to do it.
1356 1362
1357=item 3. Let the timer time out, but then re-arm it as required. 1363=item 3. Let the timer time out, but then re-arm it as required.
1358 1364
1359This method is more tricky, but usually most efficient: Most timeouts are 1365This method is more tricky, but usually most efficient: Most timeouts are
1360relatively long compared to the loop iteration time - in our example, 1366relatively long compared to the intervals between other activity - in
1361within 60 seconds, there are usually many I/O events with associated 1367our example, within 60 seconds, there are usually many I/O events with
1362activity resets. 1368associated activity resets.
1363 1369
1364In this case, it would be more efficient to leave the C<ev_timer> alone, 1370In this case, it would be more efficient to leave the C<ev_timer> alone,
1365but remember the time of last activity, and check for a real timeout only 1371but remember the time of last activity, and check for a real timeout only
1366within the callback: 1372within the callback:
1367 1373
1368 ev_tstamp last_activity; // time of last activity 1374 ev_tstamp last_activity; // time of last activity
1369 1375
1370 static void 1376 static void
1371 callback (EV_P_ ev_timer *w, int revents) 1377 callback (EV_P_ ev_timer *w, int revents)
1372 { 1378 {
1373 ev_tstamp now = ev_now (EV_A); 1379 ev_tstamp now = ev_now (EV_A);
1374 ev_tstamp timeout = last_activity + 60.; 1380 ev_tstamp timeout = last_activity + 60.;
1375 1381
1376 // if last_activity is older than now - timeout, we did time out 1382 // if last_activity + 60. is older than now, we did time out
1377 if (timeout < now) 1383 if (timeout < now)
1378 { 1384 {
1379 // timeout occured, take action 1385 // timeout occured, take action
1380 } 1386 }
1381 else 1387 else
1382 { 1388 {
1383 // callback was invoked, but there was some activity, re-arm 1389 // callback was invoked, but there was some activity, re-arm
1384 // to fire in last_activity + 60. 1390 // the watcher to fire in last_activity + 60, which is
1391 // guaranteed to be in the future, so "again" is positive:
1385 w->again = timeout - now; 1392 w->again = timeout - now;
1386 ev_timer_again (EV_A_ w); 1393 ev_timer_again (EV_A_ w);
1387 } 1394 }
1388 } 1395 }
1389 1396
1390To summarise the callback: first calculate the real time-out (defined as 1397To summarise the callback: first calculate the real timeout (defined
1391"60 seconds after the last activity"), then check if that time has been 1398as "60 seconds after the last activity"), then check if that time has
1392reached, which means there was a real timeout. Otherwise the callback was 1399been reached, which means something I<did>, in fact, time out. Otherwise
1393invoked too early (timeout is in the future), so re-schedule the timer to 1400the callback was invoked too early (C<timeout> is in the future), so
1394fire at that future time. 1401re-schedule the timer to fire at that future time, to see if maybe we have
1402a timeout then.
1395 1403
1396Note how C<ev_timer_again> is used, taking advantage of the 1404Note how C<ev_timer_again> is used, taking advantage of the
1397C<ev_timer_again> optimisation when the timer is already running. 1405C<ev_timer_again> optimisation when the timer is already running.
1398 1406
1399This scheme causes more callback invocations (about one every 60 seconds), 1407This scheme causes more callback invocations (about one every 60 seconds
1400but virtually no calls to libev to change the timeout. 1408minus half the average time between activity), but virtually no calls to
1409libev to change the timeout.
1401 1410
1402To start the timer, simply intiialise the watcher and C<last_activity>, 1411To start the timer, simply initialise the watcher and set C<last_activity>
1403then call the callback: 1412to the current time (meaning we just have some activity :), then call the
1413callback, which will "do the right thing" and start the timer:
1404 1414
1405 ev_timer_init (timer, callback); 1415 ev_timer_init (timer, callback);
1406 last_activity = ev_now (loop); 1416 last_activity = ev_now (loop);
1407 callback (loop, timer, EV_TIMEOUT); 1417 callback (loop, timer, EV_TIMEOUT);
1408 1418
1409And when there is some activity, simply remember the time in 1419And when there is some activity, simply store the current time in
1410C<last_activity>: 1420C<last_activity>, no libev calls at all:
1411 1421
1412 last_actiivty = ev_now (loop); 1422 last_actiivty = ev_now (loop);
1413 1423
1414This technique is slightly more complex, but in most cases where the 1424This technique is slightly more complex, but in most cases where the
1415time-out is unlikely to be triggered, much more efficient. 1425time-out is unlikely to be triggered, much more efficient.
1416 1426
1427Changing the timeout is trivial as well (if it isn't hard-coded in the
1428callback :) - just change the timeout and invoke the callback, which will
1429fix things for you.
1430
1431=item 4. Whee, use a double-linked list for your timeouts.
1432
1433If there is not one request, but many thousands, all employing some kind
1434of timeout with the same timeout value, then one can do even better:
1435
1436When starting the timeout, calculate the timeout value and put the timeout
1437at the I<end> of the list.
1438
1439Then use an C<ev_timer> to fire when the timeout at the I<beginning> of
1440the list is expected to fire (for example, using the technique #3).
1441
1442When there is some activity, remove the timer from the list, recalculate
1443the timeout, append it to the end of the list again, and make sure to
1444update the C<ev_timer> if it was taken from the beginning of the list.
1445
1446This way, one can manage an unlimited number of timeouts in O(1) time for
1447starting, stopping and updating the timers, at the expense of a major
1448complication, and having to use a constant timeout. The constant timeout
1449ensures that the list stays sorted.
1450
1417=back 1451=back
1452
1453So what method is the best?
1454
1455The method #2 is a simple no-brain-required solution that is adequate in
1456most situations. Method #3 requires a bit more thinking, but handles many
1457cases better, and isn't very complicated either. In most case, choosing
1458either one is fine.
1459
1460Method #1 is almost always a bad idea, and buys you nothing. Method #4 is
1461rather complicated, but extremely efficient, something that really pays
1462off after the first or so million of active timers, i.e. it's usually
1463overkill :)
1418 1464
1419=head3 The special problem of time updates 1465=head3 The special problem of time updates
1420 1466
1421Establishing the current time is a costly operation (it usually takes at 1467Establishing the current time is a costly operation (it usually takes at
1422least two system calls): EV therefore updates its idea of the current 1468least two system calls): EV therefore updates its idea of the current

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines