Skip to content

Commit aca0d3c

Browse files
committed
Cleanup of kevent usage
- reduce kevent64 to kevent usage: implies time management in dispatch_timers_program2 moves from absolute to relative and to msecs as that's what kevent can handle right now - properly defined NOTE_xxxxx macros - configure.ac change to define HAVE_KEVENT64 Squashed commit of the following: commit dfe7b8b Merge: e25ec26 6293e78 Author: Hubertus Franke <frankeh@us.ibm.com> Date: Wed Dec 9 15:38:11 2015 -0500 Merge branch 'linux-port-hdd' of github.ibm.com:ibmswift/swift-corelibs-libdispatch into linux-port-hdd commit e25ec26 Author: Hubertus Franke <frankeh@us.ibm.com> Date: Wed Dec 9 15:37:38 2015 -0500 reduce the impact of kevent64 commit 6293e78 Author: David Grove <groved@us.ibm.com> Date: Wed Dec 9 17:59:59 2015 +0000 kqueue check now also looks for kevent64 and sets config.h accordingly commit 53a8aa6 Merge: b02031c cff30c4 Author: David Grove <groved@us.ibm.com> Date: Wed Dec 9 15:16:21 2015 +0000 Merge branch 'linux-port-hdd' of github.ibm.com:ibmswift/swift-corelibs-libdispatch into linux-port-hdd commit cff30c4 Author: Hubertus Franke <frankeh@us.ibm.com> Date: Wed Dec 9 06:59:09 2015 -0500 check only one event is returned in kevent() call to allow type coercion commit 884bd36 Author: Hubertus Franke <frankeh@us.ibm.com> Date: Tue Dec 8 22:58:41 2015 -0500 kevent64 shim turns time negative in absolute to relative conversion. Checked for it commit e48115b Author: Hubertus Franke <frankeh@us.ibm.com> Date: Tue Dec 8 21:49:29 2015 -0500 fix periodic timers through kevent rewrite and fix definition for linux commit b02031c Merge: 7f64b1c 14f0250 Author: David Grove <groved@us.ibm.com> Date: Sun Dec 6 19:21:44 2015 +0000 Merge branch 'master' of github.com:apple/swift-corelibs-libdispatch into linux-port-hdd commit 7f64b1c Author: David Grove <groved@us.ibm.com> Date: Sat Dec 5 16:55:30 2015 +0000 fix mistake made in merge commit d2f10b3 Merge: fae40d1 394d9a1 Author: David Grove <groved@us.ibm.com> Date: Sat Dec 5 16:38:51 2015 +0000 Merge branch 'master' into linux-port-hdd commit fae40d1 Merge: a93074c acd56f6 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 20:49:43 2015 +0000 Merge branch 'master' into linux-port-hdd commit a93074c Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 20:44:13 2015 +0000 cleanups commit 10b9e94 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 20:36:14 2015 +0000 don't add os/base.h commit bc04960 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 20:27:25 2015 +0000 WIP cleanup commit b741481 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 20:02:14 2015 +0000 addressing commenrts from pull request commit 6de4767 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 18:08:40 2015 +0000 use __APPLE__ instead of HAVE_MACH commit f87caf9 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 18:05:06 2015 +0000 Copy BSD sys/queue.h to shims/sys_queue.h and remove HACK around SAFE macros commit e3652d6 Author: David Grove <groved@us.ibm.com> Date: Fri Dec 4 16:26:21 2015 +0000 Expand comments and reorganize; remove mistaken redefine of DISPATCH_EXPORT macros commit dd656be Author: David Grove <groved@us.ibm.com> Date: Thu Dec 3 22:20:46 2015 +0000 revert back to matching kevent64_s definition from OS X man page for kevent commit 02616b0 Merge: 2453edc 650f82a Author: Hubertus Franke <frankeh@us.ibm.com> Date: Thu Dec 3 17:04:24 2015 -0500 merged conflicts commit 2453edc Author: Hubertus Franke <frankeh@us.ibm.com> Date: Thu Dec 3 17:00:27 2015 -0500 fixed kevent, running till dispatch with bugs still in queue counts commit 650f82a Author: David Grove <groved@us.ibm.com> Date: Thu Dec 3 01:16:32 2015 +0000 license headers on added files commit 16d8a2c Author: David Grove <groved@us.ibm.com> Date: Wed Dec 2 22:26:40 2015 +0000 fix stubbed functions to have the right prototype and abort in their bodies when called commit a6ede63 Author: David Grove <groved@us.ibm.com> Date: Wed Dec 2 17:07:16 2015 +0000 update stubs and typedefs after merge of pull-request 2 from master commit f38ebb9 Author: David Grove <groved@us.ibm.com> Date: Wed Dec 2 17:05:57 2015 +0000 remove workaround; type now defined in proper header file commit e0f0522 Merge: 8d8bb46 39ac720 Author: David Grove <groved@us.ibm.com> Date: Wed Dec 2 16:46:42 2015 +0000 Merge branch 'master' into linux-port-hdd commit 8d8bb46 Author: David Grove <groved@us.ibm.com> Date: Wed Dec 2 01:40:23 2015 +0000 stub out missing functions and statics with dummy definitions commit 9f4f103 Author: David Grove <groved@us.ibm.com> Date: Tue Dec 1 16:02:11 2015 +0000 Update list of header files to be installed for dispatch and os. Move stubs.h to stubs_internal.h and include it from internal.h (so we don't need to install stubs to get a complete set of header files). commit 9a39fef Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 21:52:38 2015 +0000 clang on linux just ignores -compatibility_version instead of failing with an unknown error message (despite the fact that ld won't actually understand the argument, so linking will actually fail in the end). This is a hack to generate workable makefiles; it may not be the right fix... commit efa2a28 Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 21:51:59 2015 +0000 ignore .la files commit 35984ca Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 21:34:28 2015 +0000 WIP on getting code to compile commit 2ab2c26 Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 21:04:26 2015 +0000 slightly suspect changes to get semaphore.c to compile. The existing code was clearly wrong, but this might not be the right way to fix it either... commit d7fe4b0 Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 20:48:04 2015 +0000 tweaks to get it to compile commit da3b73d Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 19:06:27 2015 +0000 WIP on stubbing out missing pieces to get code to compile commit 4f9b58c Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 18:06:33 2015 +0000 synthesize declarations of missing observer types commit fdeae8a Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 18:05:58 2015 +0000 guard include of mach.h with HAVE_MACH commit f013a99 Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 18:05:02 2015 +0000 ignore autotools and libtool generated files commit f22bc4b Author: David Grove <groved@us.ibm.com> Date: Mon Nov 30 18:02:50 2015 +0000 header files to stub out types/functions for linux port
1 parent 14f0250 commit aca0d3c

File tree

4 files changed

+150
-29
lines changed

4 files changed

+150
-29
lines changed

configure.ac

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ AC_SEARCH_LIBS(pthread_create, pthread)
137137
# Prefer native kqueue(2); otherwise use libkqueue if present.
138138
#
139139
AC_CHECK_HEADER(sys/event.h, [],
140-
[PKG_CHECK_MODULES(KQUEUE, libkqueue)]
140+
[PKG_CHECK_MODULES(KQUEUE, libkqueue)
141+
AC_SEARCH_LIBS(kevent, kqueue)
142+
AC_SEARCH_LIBS(kevent64, kqueue,
143+
[AC_DEFINE(HAVE_KEVENT64, 1, [Define if kevent64 is present])],
144+
[AC_DEFINE(HAVE_KEVENT64, 0, [Define if kevent64 is present])])]
141145
)
142146

147+
143148
#
144149
# Checks for header files.
145150
#

os/linux_base.h

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define __OS_LINUX_BASE__
2121

2222
// #include <sys/event.h>
23+
#include <sys/user.h>
2324

2425
// marker for hacks we have made to make progress
2526
#define __LINUX_PORT_HDD__ 1
@@ -76,34 +77,41 @@ struct voucher_offsets_s {
7677
};
7778

7879

80+
// Print a warning when an unported code path executes.
81+
#define LINUX_PORT_ERROR() do { printf("LINUX_PORT_ERROR_CALLED %s:%d: %s\n",__FILE__,__LINE__,__FUNCTION__); } while (0)
82+
7983
/*
8084
* Stub out defines for other missing types
8185
*/
8286

83-
// Pulled from OS X man page for kevent
84-
struct kevent64_s {
85-
uint64_t ident; /* identifier for this event */
86-
int16_t filter; /* filter for event */
87-
uint16_t flags; /* general flags */
88-
uint32_t fflags; /* filter-specific flags */
89-
int64_t data; /* filter-specific data */
90-
uint64_t udata; /* opaque user data identifier */
91-
uint64_t ext[2]; /* filter-specific extensions */
92-
};
93-
87+
#if !HAVE_KEVENT64
88+
// we fall back to use kevent
89+
#define kevent64_s kevent
90+
#define kevent64(kq,cl,nc,el,ne,f,to) kevent(kq,cl,nc,el,ne,to)
91+
#endif
9492

95-
// PAGE_SIZE and SIZE_T_MAX should not be hardcoded like this here.
96-
#define PAGE_SIZE (4096)
93+
// SIZE_T_MAX should not be hardcoded like this here.
9794
#define SIZE_T_MAX (0x7fffffff)
9895

9996
// Define to 0 the NOTE_ values that are not present on Linux.
10097
// Revisit this...would it be better to ifdef out the uses instead??
101-
#define NOTE_VM_PRESSURE 0
102-
#define NOTE_ABSOLUTE 0
103-
#define NOTE_NSECONDS 0
104-
#define NOTE_LEEWAY 0
105-
#define NOTE_CRITICAL 0
106-
#define NOTE_BACKGROUND 0
98+
99+
// The following values are passed as part of the EVFILT_TIMER requests
100+
101+
#define IGNORE_KEVENT64_EXT /* will force the kevent64_s.ext[] to not be used -> leeway ignored */
102+
103+
#define NOTE_SECONDS 0x01
104+
#define NOTE_USECONDS 0x02
105+
#define NOTE_NSECONDS 0x04
106+
#define NOTE_ABSOLUTE 0x08
107+
#define NOTE_CRITICAL 0x10
108+
#define NOTE_BACKGROUND 0x20
109+
#define NOTE_LEEWAY 0x40
110+
111+
// need to catch the following usage if it happens ..
112+
// we simply return '0' as a value probably not correct
113+
114+
#define NOTE_VM_PRESSURE ({LINUX_PORT_ERROR(); 0;})
107115

108116
/*
109117
* Stub out misc linking and compilation attributes
@@ -135,8 +143,5 @@ struct kevent64_s {
135143
#define __OSX_AVAILABLE_BUT_DEPRECATED_MSG(a,b,c,d,msg) //
136144

137145

138-
// Print a warning when an unported code path executes.
139-
#define LINUX_PORT_ERROR() do { printf("LINUX_PORT_ERROR_CALLED %s:%d: %s\n",__FILE__,__LINE__,__FUNCTION__); } while (0)
140-
141146

142147
#endif /* __OS_LINUX_BASE__ */

src/shims/linux_stubs.c

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

87-
int kevent64(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents,unsigned int flags, const struct timespec *timeout)
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)
88140
{
89-
return kevent(kq,changelist,nchanges,eventlist,nevents,timeout);
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;
90182
}
91183

184+
#endif
185+
92186
/*
93187
* Stubbed out static data
94188
*/

src/source.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,13 +1858,22 @@ _dispatch_timers_get_delay(uint64_t nows[], struct dispatch_timer_s timer[],
18581858
return ridx;
18591859
}
18601860

1861+
1862+
#if HAVE_KEVENT64
1863+
# define kevent_set_ext1(ke,val) (ke)->ext[1] = (val)
1864+
# define delay_add_wall(delay,at) (delay) += (at)
1865+
#else
1866+
# define kevent_set_ext1(ke,val) do { } while (0)
1867+
# define delay_add_wall(delay,at) do { } while (0)
1868+
#endif
1869+
18611870
static bool
18621871
_dispatch_timers_program2(uint64_t nows[], _dispatch_kevent_qos_s *ke,
18631872
unsigned int qos)
18641873
{
18651874
unsigned int tidx;
18661875
bool poll;
1867-
uint64_t delay, leeway;
1876+
uint64_t delay, leeway, nowtime;
18681877

18691878
tidx = _dispatch_timers_get_delay(nows, _dispatch_timer, &delay, &leeway,
18701879
(int)qos);
@@ -1881,13 +1890,21 @@ _dispatch_timers_program2(uint64_t nows[], _dispatch_kevent_qos_s *ke,
18811890
_dispatch_trace_next_timer_set(
18821891
TAILQ_FIRST(&_dispatch_kevent_timer[tidx].dk_sources), qos);
18831892
_dispatch_trace_next_timer_program(delay, qos);
1884-
delay += _dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1893+
nowtime =_dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1894+
delay_add_wall(delay,nowtime);
1895+
1896+
//printf("%s: delay %ld nsecs\n",__FUNCTION__,delay);
1897+
// convert delay into msecs
1898+
delay /= 1000000L;
1899+
if ((int64_t)(delay) <= 0) delay = 1; // for some reason time turns negative
1900+
//if ((int64_t)(delay) <= 0) printf("%s: delay =%d\n",__FUNCTION__,(int64_t)delay);
1901+
18851902
if (slowpath(_dispatch_timers_force_max_leeway)) {
18861903
ke->data = (int64_t)(delay + leeway);
1887-
ke->ext[1] = 0;
1904+
kevent_set_ext1(ke,0);
18881905
} else {
18891906
ke->data = (int64_t)delay;
1890-
ke->ext[1] = leeway;
1907+
kevent_set_ext1(ke,leeway);
18911908
}
18921909
ke->flags |= EV_ADD|EV_ENABLE;
18931910
ke->flags &= ~EV_DELETE;

0 commit comments

Comments
 (0)