Skip to content

Commit 6dbebd6

Browse files
committed
Merge pull request swiftlang#8 from dgrove-oss/linux-port-hdd-timers2
Linux port hdd timers2
2 parents 8904f45 + 8b0e8f8 commit 6dbebd6

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

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 __linux__
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: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ 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)
88-
{
89-
return kevent(kq,changelist,nchanges,eventlist,nevents,timeout);
90-
}
91-
9287
/*
9388
* Stubbed out static data
9489
*/

src/source.c

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

1861+
1862+
#ifdef __linux__
1863+
// in linux we map the _dispatch_kevent_qos_s to struct kevent instead
1864+
// of struct kevent64. We loose the kevent.ext[] members and the time
1865+
// out is based on relavite msec based time vs. absolute nsec based time.
1866+
// For now we make the adjustments right here until the solution
1867+
// to either extend libkqueue with a proper kevent64 API or removing kevent
1868+
// all together and move to a lower API (e.g. epoll or kernel_module.
1869+
// Also leeway is ignored.
1870+
1871+
static void
1872+
_dispatch_kevent_timer_set_delay(_dispatch_kevent_qos_s *ke, uint64_t delay,
1873+
uint64_t leeway, uint64_t nows[])
1874+
{
1875+
// call to return nows[]
1876+
_dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1877+
// adjust nsec based delay to msec based and ignore leeway
1878+
delay /= 1000000L;
1879+
if ((int64_t)(delay) <= 0) {
1880+
delay = 1; // if value <= 0 the dispatch will stop
1881+
}
1882+
ke->data = (int64_t)delay;
1883+
}
1884+
1885+
#else
1886+
1887+
static void
1888+
_dispatch_kevent_timer_set_delay(_dispatch_kevent_qos_s *ke, uint64_t delay,
1889+
uint64_t leeway, uint64_t nows[])
1890+
{
1891+
delay += _dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1892+
if (slowpath(_dispatch_timers_force_max_leeway)) {
1893+
ke->data = (int64_t)(delay + leeway);
1894+
ke->ext[1] = 0;
1895+
} else {
1896+
ke->data = (int64_t)delay;
1897+
ke->ext[1] = leeway;
1898+
}
1899+
}
1900+
#endif
1901+
18611902
static bool
18621903
_dispatch_timers_program2(uint64_t nows[], _dispatch_kevent_qos_s *ke,
18631904
unsigned int qos)
@@ -1881,14 +1922,7 @@ _dispatch_timers_program2(uint64_t nows[], _dispatch_kevent_qos_s *ke,
18811922
_dispatch_trace_next_timer_set(
18821923
TAILQ_FIRST(&_dispatch_kevent_timer[tidx].dk_sources), qos);
18831924
_dispatch_trace_next_timer_program(delay, qos);
1884-
delay += _dispatch_source_timer_now(nows, DISPATCH_TIMER_KIND_WALL);
1885-
if (slowpath(_dispatch_timers_force_max_leeway)) {
1886-
ke->data = (int64_t)(delay + leeway);
1887-
ke->ext[1] = 0;
1888-
} else {
1889-
ke->data = (int64_t)delay;
1890-
ke->ext[1] = leeway;
1891-
}
1925+
_dispatch_kevent_timer_set_delay(ke, delay, leeway, nows);
18921926
ke->flags |= EV_ADD|EV_ENABLE;
18931927
ke->flags &= ~EV_DELETE;
18941928
}

0 commit comments

Comments
 (0)