Skip to content

Commit d76ef08

Browse files
committed
reduce impact of linux on the previous kevent changes
1 parent 907c677 commit d76ef08

File tree

4 files changed

+38
-125
lines changed

4 files changed

+38
-125
lines changed

configure.ac

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ AC_SEARCH_LIBS(pthread_create, pthread)
139139
AC_CHECK_HEADER(sys/event.h, [],
140140
[PKG_CHECK_MODULES(KQUEUE, libkqueue)]
141141
)
142-
AC_SEARCH_LIBS(kevent, kqueue)
143-
AC_SEARCH_LIBS(kevent64, kqueue,
144-
[AC_DEFINE(HAVE_KEVENT64, 1, [Define if kevent64 is present])],
145-
[AC_DEFINE(HAVE_KEVENT64, 0, [Define if kevent64 is present])])
146142

147143
#
148144
# Checks for header files.

os/linux_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct voucher_offsets_s {
8484
* Stub out defines for other missing types
8585
*/
8686

87-
#if !HAVE_KEVENT64
87+
#if __linux__
8888
// we fall back to use kevent
8989
#define kevent64_s kevent
9090
#define kevent64(kq,cl,nc,el,ne,f,to) kevent(kq,cl,nc,el,ne,to)

src/shims/linux_stubs.c

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -84,105 +84,6 @@ int sysctlbyname(const char *name, void *oldp, size_t *oldlenp,
8484
LINUX_PORT_ERROR();
8585
}
8686

87-
#if 0
88-
89-
// this code remains here purely for debugging purposes
90-
// ultimately it can be deleted
91-
92-
DISPATCH_NOINLINE
93-
static const char *
94-
_evfiltstr(short filt)
95-
{
96-
switch (filt) {
97-
#define _evfilt2(f) case (f): return #f
98-
_evfilt2(EVFILT_READ);
99-
_evfilt2(EVFILT_WRITE);
100-
_evfilt2(EVFILT_AIO);
101-
_evfilt2(EVFILT_VNODE);
102-
_evfilt2(EVFILT_PROC);
103-
_evfilt2(EVFILT_SIGNAL);
104-
_evfilt2(EVFILT_TIMER);
105-
#if HAVE_MACH
106-
_evfilt2(EVFILT_MACHPORT);
107-
_evfilt2(DISPATCH_EVFILT_MACH_NOTIFICATION);
108-
#endif
109-
_evfilt2(EVFILT_FS);
110-
_evfilt2(EVFILT_USER);
111-
#ifdef EVFILT_VM
112-
_evfilt2(EVFILT_VM);
113-
#endif
114-
#ifdef EVFILT_SOCK
115-
_evfilt2(EVFILT_SOCK);
116-
#endif
117-
#ifdef EVFILT_MEMORYSTATUS
118-
_evfilt2(EVFILT_MEMORYSTATUS);
119-
#endif
120-
121-
_evfilt2(DISPATCH_EVFILT_TIMER);
122-
_evfilt2(DISPATCH_EVFILT_CUSTOM_ADD);
123-
_evfilt2(DISPATCH_EVFILT_CUSTOM_OR);
124-
default:
125-
return "EVFILT_missing";
126-
}
127-
}
128-
129-
#if 0
130-
#define dbg_kevent64(fmt...) do { printf(fmt); } while(0)
131-
#define dbg_cond_kevent64(cond,fmt...) do { if (cond) printf(fmt); } while(0)
132-
#else
133-
#define dbg_kevent64(fmt...) do { } while(0)
134-
#define dbg_cond_kevent64(cond,fmt...) do { } while(0)
135-
#endif
136-
137-
138-
int kevent64(int kq, const struct kevent64_s *changelist_c, int nchanges, struct kevent64_s *eventlist,
139-
int nevents, unsigned int flags, const struct timespec *timeout)
140-
{
141-
// Documentation is not really clear. Instrument the code to make sure
142-
// we can do type conversions right now between kevent64 <-> kevent, where as
143-
// kevent64 uses the ext[2] extension. So far we only see these used in the EVFILT_TIMER.
144-
// right now we do this in the way into kevent, we also have to assert that
145-
// no more than 1 change or one event is passed until we get a better handle of the
146-
// usage pattern of this. (Hubertus Franke)
147-
148-
struct kevent64_s *changelist = (struct kevent64_s*) changelist_c; // so we can modify it
149-
150-
#if 1
151-
// lets put some checks in here to make sure we do it all correct
152-
// we can only convert kevent64_s -> kevent for a single entry since kevent64_s has ext[0:1] extension
153-
if ((nchanges > 1) || (nevents > 1))
154-
LINUX_PORT_ERROR();
155-
if (nchanges) {
156-
dbg_kevent64("kevent64(%s,%x,%x): cl.ext[0,1]=%lx:%ld %lx:%ld cl.data=%lx:%ld\n",
157-
_evfiltstr(changelist->filter), changelist->flags, changelist->fflags,
158-
changelist->ext[0], changelist->ext[0],
159-
changelist->ext[1], changelist->ext[1],
160-
changelist->data, changelist->data);
161-
if ((changelist->filter == EVFILT_TIMER) && (changelist->fflags & NOTE_ABSOLUTE)) {
162-
// NOTE_ABSOLUTE is not recognized by the current kevent we need to convert this
163-
// into a relative. Consider fiddling with creating relative events instead (didn't work
164-
// on first attempt). We also ignore the LEEWAY. Finally we must convert from
165-
// NSECS to MSECS (might have to expand to deal with OTHER NOTE_xSECS flags
166-
167-
//changelist->data -= _dispatch_get_nanoseconds();
168-
//changelist->data -= time(NULL) * NSEC_PER_SEC;
169-
dbg_kevent64("kevent64(%s,%x) data=%lx:%ld\n",
170-
_evfiltstr(changelist->filter),changelist->fflags,
171-
changelist->data,changelist->data);
172-
//changelist->data /= 1000000UL;
173-
//if ((int64_t)(changelist->data) <= 0) changelist->data = 1; // for some reason time turns negative
174-
}
175-
}
176-
#endif
177-
// eventlist can not return more than 1 event type coersion doesn't work
178-
int rc = kevent(kq,(struct kevent*) changelist,nchanges,(struct kevent*) eventlist,nevents,timeout);
179-
if (rc > 1)
180-
LINUX_PORT_ERROR();
181-
return rc;
182-
}
183-
184-
#endif
185-
18687
/*
18788
* Stubbed out static data
18889
*/

src/source.c

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,17 +1859,40 @@ _dispatch_timers_get_delay(uint64_t nows[], struct dispatch_timer_s timer[],
18591859
}
18601860

18611861

1862-
#if HAVE_KEVENT64
1863-
# define kevent_set_ext1(ke,val) (ke)->ext[1] = (val)
1864-
# define adjust_delay(delay,at) (delay) += (at)
1862+
#ifdef __linux__
1863+
// in linux we map the _dispatch_kevent_qos_s to struct kevent instead of struct kevent64.
1864+
// we loose the kevent.ext[] members and the time out is based on relavite msec based time
1865+
// vs. absolute nsec based time. For now we make the adjustments right here until the solution
1866+
// to either extend libkqueue with a proper kevent64 API or removing kevent all together
1867+
// and move to a lower API (e.g. epoll or kernel_module. Also leeway is ignored.
1868+
1869+
static void
1870+
kevent_set_delay(_dispatch_kevent_qos_s *ke, uint64_t delay,uint64_t leeway, uint64_t nows[])
1871+
{
1872+
_dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL); // called to return nows[]
1873+
1874+
delay /= 1000000L;
1875+
if ((int64_t)(delay) <= 0)
1876+
delay = 1; /* if he value is negative or 0 then the dispatch will stop */
1877+
ke->data = (int64_t)delay;
1878+
}
1879+
18651880
#else
1866-
# define kevent_set_ext1(ke,val) do { } while (0)
1867-
# define adjust_delay(delay,at) \
1868-
do { \
1869-
delay /= 1000000L; \
1870-
if ((int64_t)(delay) <= 0) delay = 1; /* for some reason time turns negative */ \
1871-
} while (0)
18721881

1882+
static void
1883+
kevent_set_delay(_dispatch_kevent_qos_s *ke, uint64_t delay,uint64_t leeway, uint64_t nows[])
1884+
{
1885+
1886+
delay += _dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1887+
1888+
if (slowpath(_dispatch_timers_force_max_leeway)) {
1889+
ke->data = (int64_t)(delay + leeway);
1890+
ke->ext[1] = 0;
1891+
} else {
1892+
ke->data = (int64_t)delay;
1893+
ke->ext[1] = leeway;
1894+
}
1895+
}
18731896
#endif
18741897

18751898
static bool
@@ -1878,7 +1901,7 @@ _dispatch_timers_program2(uint64_t nows[], _dispatch_kevent_qos_s *ke,
18781901
{
18791902
unsigned int tidx;
18801903
bool poll;
1881-
uint64_t delay, leeway, nowtime;
1904+
uint64_t delay, leeway;
18821905

18831906
tidx = _dispatch_timers_get_delay(nows, _dispatch_timer, &delay, &leeway,
18841907
(int)qos);
@@ -1895,19 +1918,12 @@ _dispatch_timers_program2(uint64_t nows[], _dispatch_kevent_qos_s *ke,
18951918
_dispatch_trace_next_timer_set(
18961919
TAILQ_FIRST(&_dispatch_kevent_timer[tidx].dk_sources), qos);
18971920
_dispatch_trace_next_timer_program(delay, qos);
1898-
nowtime =_dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1899-
adjust_delay(delay,nowtime);
19001921

1901-
if (slowpath(_dispatch_timers_force_max_leeway)) {
1902-
ke->data = (int64_t)(delay + leeway);
1903-
kevent_set_ext1(ke,0);
1904-
} else {
1905-
ke->data = (int64_t)delay;
1906-
kevent_set_ext1(ke,leeway);
1907-
}
1908-
ke->flags |= EV_ADD|EV_ENABLE;
1909-
ke->flags &= ~EV_DELETE;
1922+
kevent_set_delay(ke,delay,leeway,nows);
19101923
}
1924+
1925+
ke->flags |= EV_ADD|EV_ENABLE;
1926+
ke->flags &= ~EV_DELETE;
19111927
_dispatch_kq_update(ke);
19121928
return poll;
19131929
}

0 commit comments

Comments
 (0)