X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flibsystemd-bus%2Fsd-event.c;h=b5ddf71ebede89439e76adf1a3ba33fab830be05;hp=ec18af43d51f385d90ccef2bcaca8cf58bb48c02;hb=2a16a986ce5f1bdb7e96abfe14fcb9f34c9364b6;hpb=2576a19ed25dff1adc7fd2c0b874c74946fb35b4 diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index ec18af43d..b5ddf71eb 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -584,7 +584,7 @@ _public_ int sd_event_add_io( assert_return(e, -EINVAL); assert_return(fd >= 0, -EINVAL); - assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP)), -EINVAL); + assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL); assert_return(callback, -EINVAL); assert_return(ret, -EINVAL); assert_return(e->state != SD_EVENT_FINISHED, -ESTALE); @@ -640,15 +640,16 @@ static int event_setup_timer_fd( } /* When we sleep for longer, we try to realign the wakeup to - the same time wihtin each second, so that events all across - the system can be coalesced into a single CPU - wakeup. However, let's take some system-specific randomness - for this value, so that in a network of systems with synced - clocks timer events are distributed a bit. Here, we - calculate a perturbation usec offset from the boot ID. */ + the same time wihtin each minute/second/250ms, so that + events all across the system can be coalesced into a single + CPU wakeup. However, let's take some system-specific + randomness for this value, so that in a network of systems + with synced clocks timer events are distributed a + bit. Here, we calculate a perturbation usec offset from the + boot ID. */ if (sd_id128_get_boot(&bootid) >= 0) - e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_SEC; + e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE; *timer_fd = fd; return 0; @@ -983,7 +984,7 @@ _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) { return NULL; } -_public_ sd_event *sd_event_get(sd_event_source *s) { +_public_ sd_event *sd_event_source_get_event(sd_event_source *s) { assert_return(s, NULL); return s->event; @@ -1021,7 +1022,7 @@ _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) assert_return(s, -EINVAL); assert_return(s->type == SOURCE_IO, -EDOM); - assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP)), -EINVAL); + assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL); assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE); assert_return(!event_pid_changed(s->event), -ECHILD); @@ -1363,13 +1364,24 @@ static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) { dispatch as much as possible on the entire system. We implement this by waking up everywhere at the same time - within any given second if we can, synchronised via the + within any given minute if we can, synchronised via the perturbation value determined from the boot ID. If we can't, - then we try to find the same spot in every a 250ms + then we try to find the same spot in every 1s and then 250ms step. Otherwise, we pick the last possible time to wake up. */ - c = (b / USEC_PER_SEC) * USEC_PER_SEC + e->perturb; + c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb; + if (c >= b) { + if (_unlikely_(c < USEC_PER_MINUTE)) + return b; + + c -= USEC_PER_MINUTE; + } + + if (c >= a) + return c; + + c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC); if (c >= b) { if (_unlikely_(c < USEC_PER_SEC)) return b; @@ -1412,6 +1424,9 @@ static int event_arm_timer( a = prioq_peek(earliest); if (!a || a->enabled == SD_EVENT_OFF) { + if (timer_fd < 0) + return 0; + if (*next == (usec_t) -1) return 0; @@ -1753,18 +1768,16 @@ _public_ int sd_event_run(sd_event *e, uint64_t timeout) { if (r < 0) goto finish; - if (event_next_pending(e) || e->need_process_child) - timeout = 0; + r = event_arm_timer(e, e->monotonic_fd, e->monotonic_earliest, e->monotonic_latest, &e->monotonic_next); + if (r < 0) + goto finish; - if (timeout > 0) { - r = event_arm_timer(e, e->monotonic_fd, e->monotonic_earliest, e->monotonic_latest, &e->monotonic_next); - if (r < 0) - goto finish; + r = event_arm_timer(e, e->realtime_fd, e->realtime_earliest, e->realtime_latest, &e->realtime_next); + if (r < 0) + goto finish; - r = event_arm_timer(e, e->realtime_fd, e->realtime_earliest, e->realtime_latest, &e->realtime_next); - if (r < 0) - goto finish; - } + if (event_next_pending(e) || e->need_process_child) + timeout = 0; m = epoll_wait(e->epoll_fd, ev_queue, EPOLL_QUEUE_MAX, timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));