Skip to content

Commit bdee09e

Browse files
committed
Port most of libdispatch tests to Linux (31 compile; 21 pass).
Made an initial sweep through the test suite to resolve the easy compilation issues. Summary of changes: (a) Guard mach specific code with #ifdef __APPLE__ (b) Changes to tests/Makefile.am to preserve information from configure to guide test selection, compilation, and execution. (c) Map sysctlbyname calls to sysconf (d) Simulate mach_absolute_time APIs (e) Guard code using F_NOCACHE and F_GLOBAL_NOCACHE (f) On Linux, simplify bsdtestharness.c to not use libdispatch to execute the test cases (some of the needed libdispatch functionality is not yet working). To track progress, we broke the TESTS list in tests/Makefile.am into UNPORTED_TESTS, PORTED_TESTS_FAILED, PORTED_TESTS_PASSED. TESTS is defined as the union of PORTED_TESTS_PASSED and PORTED_TESTS_FAILED. If the automake testing harness is only being used on Linux, it would be nice to merge this change back to coordinate progress. If you use this test harnesss on OS X too, we can do something different like tracking progress in a side file instead.
1 parent e7ca00f commit bdee09e

27 files changed

+304
-8
lines changed

configure.ac

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ AS_IF([test "x$DTRACE" != "x"], [use_dtrace=true],[
115115
AM_CONDITIONAL(USE_DTRACE, $use_dtrace)
116116
AC_PATH_PROG(LEAKS, leaks)
117117
AS_IF([test "x$LEAKS" != "x"],
118-
[AC_DEFINE(HAVE_LEAKS, 1, [Define if Apple leaks program is present])]
118+
[AC_DEFINE(HAVE_LEAKS, 1, [Define if Apple leaks program is present])
119+
have_leaks=true],
120+
[have_leaks=false]
119121
)
122+
AM_CONDITIONAL(HAVE_LEAKS, $have_leaks)
120123

121124
DISPATCH_C_ATOMIC_BUILTINS
122125

tests/Makefile.am

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,55 @@ check_PROGRAMS= \
1919

2020
noinst_SCRIPTS=leaks-wrapper.sh
2121

22-
TESTS= \
22+
UNPORTED_TESTS= \
23+
dispatch_deadname \
24+
dispatch_proc \
25+
dispatch_vm \
26+
dispatch_vnode \
27+
dispatch_select
28+
29+
PORTED_TESTS_FAILED= \
30+
dispatch_group \
31+
dispatch_priority \
32+
dispatch_priority2 \
33+
dispatch_concur \
34+
dispatch_context_for_key \
35+
dispatch_read \
36+
dispatch_read2 \
37+
dispatch_readsync \
38+
dispatch_io \
39+
dispatch_io_net
40+
41+
PORTED_TESTS_PASSED= \
2342
dispatch_apply \
2443
dispatch_api \
2544
dispatch_c99 \
26-
dispatch_deadname \
2745
dispatch_debug \
2846
dispatch_queue_finalizer \
47+
dispatch_overcommit \
48+
dispatch_pingpong \
49+
dispatch_plusplus \
50+
dispatch_after \
51+
dispatch_timer \
52+
dispatch_timer_short \
53+
dispatch_timer_timeout \
54+
dispatch_sema \
55+
dispatch_suspend_timer \
56+
dispatch_timer_bit31 \
57+
dispatch_timer_bit63 \
58+
dispatch_timer_set_time \
59+
dispatch_starfish \
60+
dispatch_cascade \
61+
dispatch_drift \
62+
dispatch_data
63+
64+
ORIGINAL_LIST_OF_TESTS= \
65+
dispatch_apply \
66+
dispatch_api \
67+
dispatch_c99 \
68+
dispatch_deadname \
69+
dispatch_debug \
70+
dispatch_queue_finalizer \
2971
dispatch_group \
3072
dispatch_overcommit \
3173
dispatch_pingpong \
@@ -57,6 +99,8 @@ TESTS= \
5799
dispatch_vnode \
58100
dispatch_select
59101

102+
TESTS=$(PORTED_TESTS_PASSED) $(PORTED_TESTS_FAILED)
103+
60104
dispatch_c99_CFLAGS=$(AM_CFLAGS) -std=c99
61105
dispatch_plusplus_SOURCES=dispatch_plusplus.cpp
62106
dispatch_priority2_SOURCES=dispatch_priority.c
@@ -65,15 +109,27 @@ dispatch_priority2_CPPFLAGS=$(AM_CPPFLAGS) -DUSE_SET_TARGET_QUEUE=1
65109
AM_CPPFLAGS=-I$(top_builddir) -I$(top_srcdir)
66110

67111
DISPATCH_TESTS_CFLAGS=-Wall -Wno-deprecated-declarations $(MARCH_FLAGS)
68-
AM_CFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CBLOCKS_FLAGS)
112+
AM_CFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CBLOCKS_FLAGS) $(KQUEUE_CFLAGS)
69113
AM_OBJCFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CBLOCKS_FLAGS)
70114
AM_CXXFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CXXBLOCKS_FLAGS)
71115
AM_OBJCXXFLAGS=$(DISPATCH_TESTS_CFLAGS) $(CXXBLOCKS_FLAGS)
72116

73-
LDADD=libbsdtests.la ../src/libdispatch.la
117+
if HAVE_PTHREAD_WORKQUEUES
118+
PTHREAD_WORKQUEUE_LIBS=-lpthread_workqueue
119+
endif
120+
121+
LDADD=libbsdtests.la ../src/libdispatch.la $(KQUEUE_LIBS) $(PTHREAD_WORKQUEUE_LIBS) $(BSD_OVERLAY_LIBS)
74122
libbsdtests_la_LDFLAGS=-avoid-version
75123

124+
bsdtestsummarize_LDADD=-lm
125+
dispatch_timer_short_LDADD=-lm $(LDADD)
126+
dispatch_group_LDADD=-lm $(LDADD)
127+
128+
if HAVE_LEAKS
76129
TESTS_ENVIRONMENT=./bsdtestharness
130+
else
131+
TESTS_ENVIRONMENT=NOLEAKS=1 ./bsdtestharness
132+
endif
77133
DISTCLEAN=Foundation/bench.cc
78134

79135
if HAVE_COREFOUNDATION
@@ -82,6 +138,7 @@ TESTS+= \
82138
dispatch_transform \
83139
dispatch_sync_on_main \
84140
cffd
141+
AM_CFLAGS+=-DHAVE_COREFOUNDATION
85142

86143
dispatch_cf_main_LDFLAGS=-framework CoreFoundation
87144
dispatch_transform_LDFLAGS=-framework CoreFoundation -framework Security
@@ -95,6 +152,7 @@ TESTS+= \
95152
dispatch_apply_gc \
96153
nsoperation \
97154
bench
155+
AM_CFLAGS+=-DHAVE_FOUNDATION
98156

99157
dispatch_sync_gc_SOURCES=Foundation/dispatch_sync_gc.m
100158
dispatch_sync_gc_LDFLAGS=-framework Foundation

tests/bsdtestharness.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@
2525
#include <stdlib.h>
2626
#include <unistd.h>
2727
#include <signal.h>
28+
#ifdef __APPLE__
2829
#include <mach/clock_types.h>
2930
#include <mach-o/arch.h>
31+
#endif
3032
#include <sys/resource.h>
3133
#include <sys/time.h>
3234

3335
#include <bsdtests.h>
3436

3537
extern char **environ;
3638

39+
#ifdef __linux__
40+
// FIXME: LINUX_PORT_HDD
41+
// For initial bringup, don't use libdispatch to test libdispatch!
42+
#define SIMPLE_TEST_HARNESS 1
43+
#else
44+
#define SIMPLE_TEST_HARNESS 0
45+
#endif
46+
3747
int
3848
main(int argc, char *argv[])
3949
{
@@ -46,10 +56,15 @@ main(int argc, char *argv[])
4656
exit(1);
4757
}
4858

59+
#ifdef __APPLE__
4960
short spawnflags = POSIX_SPAWN_START_SUSPENDED;
5061
#if TARGET_OS_EMBEDDED
5162
spawnflags |= POSIX_SPAWN_SETEXEC;
5263
#endif
64+
#else
65+
#define POSIX_SPAWN_SETEXEC 0 /* ignore... */
66+
short spawnflags = 0;
67+
#endif
5368

5469
posix_spawnattr_t attr;
5570
res = posix_spawnattr_init(&attr);
@@ -58,6 +73,7 @@ main(int argc, char *argv[])
5873
assert(res == 0);
5974

6075
uint64_t to = 0;
76+
#ifdef __APPLE__
6177
char *tos = getenv("BSDTEST_TIMEOUT");
6278
if (tos) {
6379
to = strtoul(tos, NULL, 0);
@@ -73,6 +89,7 @@ main(int argc, char *argv[])
7389
}
7490
}
7591

92+
#endif
7693
int i;
7794
char** newargv = calloc(argc, sizeof(void*));
7895
for (i = 1; i < argc; ++i) {
@@ -98,6 +115,29 @@ main(int argc, char *argv[])
98115
//fprintf(stderr, "pid = %d\n", pid);
99116
assert(pid > 0);
100117

118+
#if SIMPLE_TEST_HARNESS
119+
int status;
120+
struct rusage usage;
121+
struct timeval tv_stop, tv_wall;
122+
123+
gettimeofday(&tv_stop, NULL);
124+
tv_wall.tv_sec = tv_stop.tv_sec - tv_start.tv_sec;
125+
tv_wall.tv_sec -= (tv_stop.tv_usec < tv_start.tv_usec);
126+
tv_wall.tv_usec = abs(tv_stop.tv_usec - tv_start.tv_usec);
127+
128+
int res2 = wait4(pid, &status, 0, &usage);
129+
assert(res2 != -1);
130+
test_long("Process exited", (WIFEXITED(status) && WEXITSTATUS(status) && WEXITSTATUS(status) != 0xff) || WIFSIGNALED(status), 0);
131+
printf("[PERF]\twall time: %ld.%06d\n", tv_wall.tv_sec, tv_wall.tv_usec);
132+
printf("[PERF]\tuser time: %ld.%06d\n", usage.ru_utime.tv_sec, usage.ru_utime.tv_usec);
133+
printf("[PERF]\tsystem time: %ld.%06d\n", usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
134+
printf("[PERF]\tmax resident set size: %ld\n", usage.ru_maxrss);
135+
printf("[PERF]\tpage faults: %ld\n", usage.ru_majflt);
136+
printf("[PERF]\tswaps: %ld\n", usage.ru_nswap);
137+
printf("[PERF]\tvoluntary context switches: %ld\n", usage.ru_nvcsw);
138+
printf("[PERF]\tinvoluntary context switches: %ld\n", usage.ru_nivcsw);
139+
exit((WIFEXITED(status) && WEXITSTATUS(status)) || WIFSIGNALED(status));
140+
#else
101141
dispatch_queue_t main_q = dispatch_get_main_queue();
102142

103143
tmp_ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, main_q);
@@ -155,6 +195,7 @@ main(int argc, char *argv[])
155195
kill(pid, SIGCONT);
156196

157197
dispatch_main();
198+
#endif // SIMPLE_TEST_HARNESS
158199

159200
return 0;
160201
}

tests/bsdtests.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
#include <errno.h>
2626
#include <sys/errno.h>
2727
#include <string.h>
28+
#ifdef __APPLE__
2829
#include <crt_externs.h>
2930
#include <mach/mach_error.h>
31+
#endif
3032
#include <spawn.h>
3133
#include <inttypes.h>
3234
#include "bsdtests.h"
@@ -308,6 +310,7 @@ test_errno_format(long actual, long expected, const char *format, ...)
308310
_test_errno(NULL, 0, desc, actual, expected);
309311
}
310312

313+
#ifdef __APPLE__
311314
void
312315
_test_mach_error(const char* file, long line, const char* desc,
313316
mach_error_t actual, mach_error_t expected)
@@ -328,6 +331,7 @@ test_mach_error_format(mach_error_t actual, mach_error_t expected, const char *f
328331
GENERATE_DESC
329332
_test_mach_error(NULL, 0, desc, actual, expected);
330333
}
334+
#endif
331335

332336
void
333337
_test_skip(const char* file, long line, const char* desc)
@@ -400,6 +404,19 @@ test_start(const char* desc)
400404
usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
401405
}
402406

407+
#if __linux__
408+
static char** get_environment(void)
409+
{
410+
extern char **environ;
411+
return environ;
412+
}
413+
#else
414+
static char** get_environment(void)
415+
{
416+
return (* _NSGetEnviron());
417+
}
418+
#endif
419+
403420
void
404421
test_leaks_pid(const char *name, pid_t pid)
405422
{
@@ -435,7 +452,7 @@ test_leaks_pid(const char *name, pid_t pid)
435452
snprintf(pidstr, sizeof(pidstr), "%d", pid);
436453

437454
char* args[] = { "./leaks-wrapper", pidstr, NULL };
438-
res = posix_spawnp(&pid, args[0], NULL, NULL, args, * _NSGetEnviron());
455+
res = posix_spawnp(&pid, args[0], NULL, NULL, args, get_environment());
439456
if (res == 0 && pid > 0) {
440457
int status;
441458
waitpid(pid, &status, 0);

tests/bsdtests.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@
2222
#define __BSD_TEST_H__
2323

2424
#include <errno.h>
25+
#ifdef __APPLE__
2526
#include <mach/error.h>
27+
#endif
28+
#if defined(__APPLE__) || defined(HAVE_COREFOUNDATION)
2629
#include <CoreFoundation/CoreFoundation.h>
30+
#endif
31+
32+
#ifdef __linux__
33+
#define __printflike(a,b) __attribute__((format(printf, a, b)))
34+
#include <inttypes.h>
35+
#endif
2736

2837
static inline const char*
2938
__BASENAME__(const char *_str_)
@@ -121,12 +130,16 @@ void _test_errno(const char* file, long line, const char* desc, long actual, lon
121130
#define test_errno(a,b,c) _test_errno(__SOURCE_FILE__, __LINE__, a, b, c)
122131
void test_errno_format(long actual, long expected, const char *format, ...) __printflike(3,4);
123132

133+
#ifndef __linux__
124134
void _test_mach_error(const char* file, long line, const char* desc, mach_error_t actual, mach_error_t expected);
125135
#define test_mach_error(a,b,c) _test_mach_error(__SOURCE_FILE__, __LINE__, a, b, c)
126136
void test_mach_error_format(mach_error_t actual, mach_error_t expected, const char *format, ...) __printflike(3,4);
137+
#endif
127138

139+
#if defined(__APPLE__) || defined(HAVE_COREFOUNDATION)
128140
void test_cferror(const char* desc, CFErrorRef actual, CFIndex expectedCode);
129141
void test_cferror_format(CFErrorRef actual, CFIndex expectedCode, const char *format, ...) __printflike(3,4);
142+
#endif
130143

131144
void _test_skip(const char* file, long line, const char* desc);
132145
#define test_skip(m) _test_skip(__SOURCE_FILE__, __LINE__, m)

tests/bsdtestsummarize.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
#include <string.h>
2525
#include <sys/param.h>
2626

27+
#ifndef __APPLE__
28+
static char*
29+
fgetln(FILE *stream, size_t *len)
30+
{
31+
static char buf[1024];
32+
char *cp = fgets(buf,1024,stream);
33+
if (cp)
34+
*len = strlen(cp);
35+
else
36+
*len = 0;
37+
return cp;
38+
}
39+
#endif
40+
2741
int
2842
has_prefix(const char* str, const char* prefix)
2943
{

tests/dispatch_after.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@
2323
#include <unistd.h>
2424
#include <stdlib.h>
2525
#include <assert.h>
26+
#ifdef __APPLE__
2627
#include <libkern/OSAtomic.h>
28+
#endif
2729

2830
#include <bsdtests.h>
2931
#include <Block.h>
3032

3133
#include "dispatch_test.h"
3234

3335
void
34-
done(void *arg __unused)
36+
done(void *arg /*__unused */)
3537
{
3638
sleep(1);
3739
test_stop();

tests/dispatch_apply.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include <unistd.h>
2424
#include <stdlib.h>
2525
#include <assert.h>
26+
#ifdef __APPLE__
2627
#include <libkern/OSAtomic.h>
28+
#endif
2729
#include <sys/types.h>
2830
#include <sys/sysctl.h>
2931

@@ -74,8 +76,12 @@ void busythread(void *ignored)
7476
void test_apply_contended(dispatch_queue_t dq)
7577
{
7678
uint32_t activecpu;
79+
#ifdef __linux__
80+
activecpu = sysconf(_SC_NPROCESSORS_ONLN);
81+
#else
7782
size_t s = sizeof(activecpu);
7883
sysctlbyname("hw.activecpu", &activecpu, &s, NULL, 0);
84+
#endif
7985
int tIndex, n_threads = activecpu;
8086
dispatch_group_t grp = dispatch_group_create();
8187

tests/dispatch_cf_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
#include <dispatch/dispatch.h>
2222
#include <stdio.h>
2323
#include <CoreFoundation/CoreFoundation.h>
24+
#ifdef __APPLE__
2425
#include <libkern/OSAtomic.h>
26+
#endif
2527

2628
#include <bsdtests.h>
2729
#include "dispatch_test.h"

tests/dispatch_concur.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ main(int argc __attribute__((unused)), char* argv[] __attribute__((unused)))
202202
dispatch_test_start("Dispatch Private Concurrent/Wide Queue"); // <rdar://problem/8049506&8169448&8186485>
203203

204204
uint32_t activecpu;
205+
#ifdef __linux__
206+
activecpu = sysconf(_SC_NPROCESSORS_ONLN);
207+
#else
205208
size_t s = sizeof(activecpu);
206209
sysctlbyname("hw.activecpu", &activecpu, &s, NULL, 0);
210+
#endif
207211
size_t n = activecpu / 2 > 1 ? activecpu / 2 : 1, w = activecpu * 2;
208212
dispatch_queue_t tq, ttq;
209213
long qw, tqw, ttqw;

0 commit comments

Comments
 (0)