Skip to content

Commit 96821cd

Browse files
frankehdas
authored andcommitted
reduce impact of linux on the previous kevent changes
Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
1 parent b19242a commit 96821cd

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
@@ -78,7 +78,7 @@ struct voucher_offsets_s {
7878
* Stub out defines for other missing types
7979
*/
8080

81-
#if !HAVE_KEVENT64
81+
#if __linux__
8282
// we fall back to use kevent
8383
#define kevent64_s kevent
8484
#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
@@ -77,105 +77,6 @@ int sysctlbyname(const char *name, void *oldp, size_t *oldlenp,
7777
LINUX_PORT_ERROR();
7878
}
7979

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

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)