Skip to content

Commit b9d342f

Browse files
dgrove-ossdas
authored andcommitted
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 Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
1 parent 8caced6 commit b9d342f

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
@@ -14,6 +14,7 @@
1414
#define __OS_LINUX_BASE__
1515

1616
// #include <sys/event.h>
17+
#include <sys/user.h>
1718

1819
// marker for hacks we have made to make progress
1920
#define __LINUX_PORT_HDD__ 1
@@ -70,34 +71,41 @@ struct voucher_offsets_s {
7071
};
7172

7273

74+
// Print a warning when an unported code path executes.
75+
#define LINUX_PORT_ERROR() do { printf("LINUX_PORT_ERROR_CALLED %s:%d: %s\n",__FILE__,__LINE__,__FUNCTION__); } while (0)
76+
7377
/*
7478
* Stub out defines for other missing types
7579
*/
7680

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

89-
// PAGE_SIZE and SIZE_T_MAX should not be hardcoded like this here.
90-
#define PAGE_SIZE (4096)
87+
// SIZE_T_MAX should not be hardcoded like this here.
9188
#define SIZE_T_MAX (0x7fffffff)
9289

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

102110
/*
103111
* Stub out misc linking and compilation attributes
@@ -129,8 +137,5 @@ struct kevent64_s {
129137
#define __OSX_AVAILABLE_BUT_DEPRECATED_MSG(a,b,c,d,msg) //
130138

131139

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

136141
#endif /* __OS_LINUX_BASE__ */

src/shims/linux_stubs.c

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

80-
int kevent64(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents,unsigned int flags, const struct timespec *timeout)
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)
81133
{
82-
return kevent(kq,changelist,nchanges,eventlist,nevents,timeout);
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;
83175
}
84176

177+
#endif
178+
85179
/*
86180
* Stubbed out static data
87181
*/

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)