diff options
Diffstat (limited to 'tests/syscount.c')
| -rw-r--r-- | tests/syscount.c | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/tests/syscount.c b/tests/syscount.c new file mode 100644 index 0000000..ec45c15 --- /dev/null +++ b/tests/syscount.c | |||
| @@ -0,0 +1,144 @@ | |||
| 1 | /* Counts the system calls a run makes, by standing in front of libc. | ||
| 2 | |||
| 3 | cc -O2 -shared -fPIC -o syscount.so tests/syscount.c -ldl | ||
| 4 | LD_PRELOAD=./syscount.so ./opentracker -f conf (DYLD_INSERT_LIBRARIES on macOS) | ||
| 5 | |||
| 6 | The tally goes to stderr when the process exits. Reading the source tells | ||
| 7 | you which calls a path should make; this tells you which it does make, | ||
| 8 | which is not always the same thing. */ | ||
| 9 | |||
| 10 | #define _GNU_SOURCE | ||
| 11 | #include <dlfcn.h> | ||
| 12 | #include <stdarg.h> | ||
| 13 | #include <stdatomic.h> | ||
| 14 | #include <stdio.h> | ||
| 15 | #include <sys/socket.h> | ||
| 16 | #include <sys/types.h> | ||
| 17 | #include <unistd.h> | ||
| 18 | |||
| 19 | #ifdef __linux__ | ||
| 20 | #include <sys/epoll.h> | ||
| 21 | #endif | ||
| 22 | |||
| 23 | #define COUNTER(name) static _Atomic long g_##name | ||
| 24 | #define BUMP(name) atomic_fetch_add_explicit(&g_##name, 1, memory_order_relaxed) | ||
| 25 | #define REPORT(name) fprintf(stderr, " %-14s %10ld\n", #name, atomic_load(&g_##name)) | ||
| 26 | |||
| 27 | COUNTER(accept); | ||
| 28 | COUNTER(accept4); | ||
| 29 | COUNTER(read); | ||
| 30 | COUNTER(write); | ||
| 31 | COUNTER(close); | ||
| 32 | COUNTER(fcntl); | ||
| 33 | COUNTER(recvfrom); | ||
| 34 | COUNTER(sendto); | ||
| 35 | COUNTER(epoll_ctl); | ||
| 36 | COUNTER(epoll_wait); | ||
| 37 | |||
| 38 | typedef int (*fn_accept)(int, struct sockaddr *, socklen_t *); | ||
| 39 | typedef int (*fn_accept4)(int, struct sockaddr *, socklen_t *, int); | ||
| 40 | typedef ssize_t (*fn_read)(int, void *, size_t); | ||
| 41 | typedef ssize_t (*fn_write)(int, const void *, size_t); | ||
| 42 | typedef int (*fn_close)(int); | ||
| 43 | typedef int (*fn_fcntl)(int, int, ...); | ||
| 44 | typedef ssize_t (*fn_recvfrom)(int, void *, size_t, int, struct sockaddr *, socklen_t *); | ||
| 45 | typedef ssize_t (*fn_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t); | ||
| 46 | |||
| 47 | static fn_accept real_accept; | ||
| 48 | static fn_accept4 real_accept4; | ||
| 49 | static fn_read real_read; | ||
| 50 | static fn_write real_write; | ||
| 51 | static fn_close real_close; | ||
| 52 | static fn_fcntl real_fcntl; | ||
| 53 | static fn_recvfrom real_recvfrom; | ||
| 54 | static fn_sendto real_sendto; | ||
| 55 | |||
| 56 | /* Resolving lazily keeps the library out of the way until something is | ||
| 57 | actually called, which matters because dlsym itself allocates */ | ||
| 58 | #define WRAP(name) \ | ||
| 59 | if (!real_##name) \ | ||
| 60 | real_##name = (fn_##name)dlsym(RTLD_NEXT, #name); \ | ||
| 61 | BUMP(name) | ||
| 62 | |||
| 63 | int accept(int s, struct sockaddr *addr, socklen_t *len) { | ||
| 64 | WRAP(accept); | ||
| 65 | return real_accept(s, addr, len); | ||
| 66 | } | ||
| 67 | |||
| 68 | #ifdef SOCK_NONBLOCK | ||
| 69 | int accept4(int s, struct sockaddr *addr, socklen_t *len, int flags) { | ||
| 70 | WRAP(accept4); | ||
| 71 | return real_accept4(s, addr, len, flags); | ||
| 72 | } | ||
| 73 | #endif | ||
| 74 | |||
| 75 | ssize_t read(int fd, void *buf, size_t count) { | ||
| 76 | WRAP(read); | ||
| 77 | return real_read(fd, buf, count); | ||
| 78 | } | ||
| 79 | |||
| 80 | ssize_t write(int fd, const void *buf, size_t count) { | ||
| 81 | WRAP(write); | ||
| 82 | return real_write(fd, buf, count); | ||
| 83 | } | ||
| 84 | |||
| 85 | int close(int fd) { | ||
| 86 | WRAP(close); | ||
| 87 | return real_close(fd); | ||
| 88 | } | ||
| 89 | |||
| 90 | int fcntl(int fd, int cmd, ...) { | ||
| 91 | va_list args; | ||
| 92 | long arg; | ||
| 93 | |||
| 94 | va_start(args, cmd); | ||
| 95 | arg = va_arg(args, long); | ||
| 96 | va_end(args); | ||
| 97 | WRAP(fcntl); | ||
| 98 | return real_fcntl(fd, cmd, arg); | ||
| 99 | } | ||
| 100 | |||
| 101 | ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) { | ||
| 102 | WRAP(recvfrom); | ||
| 103 | return real_recvfrom(s, buf, len, flags, from, fromlen); | ||
| 104 | } | ||
| 105 | |||
| 106 | ssize_t sendto(int s, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) { | ||
| 107 | WRAP(sendto); | ||
| 108 | return real_sendto(s, buf, len, flags, to, tolen); | ||
| 109 | } | ||
| 110 | |||
| 111 | #ifdef __linux__ | ||
| 112 | typedef int (*fn_epoll_ctl)(int, int, int, struct epoll_event *); | ||
| 113 | typedef int (*fn_epoll_wait)(int, struct epoll_event *, int, int); | ||
| 114 | static fn_epoll_ctl real_epoll_ctl; | ||
| 115 | static fn_epoll_wait real_epoll_wait; | ||
| 116 | |||
| 117 | int epoll_ctl(int ep, int op, int fd, struct epoll_event *ev) { | ||
| 118 | WRAP(epoll_ctl); | ||
| 119 | return real_epoll_ctl(ep, op, fd, ev); | ||
| 120 | } | ||
| 121 | |||
| 122 | int epoll_wait(int ep, struct epoll_event *events, int max, int timeout) { | ||
| 123 | WRAP(epoll_wait); | ||
| 124 | return real_epoll_wait(ep, events, max, timeout); | ||
| 125 | } | ||
| 126 | #endif | ||
| 127 | |||
| 128 | __attribute__((destructor)) static void report(void) { | ||
| 129 | fprintf(stderr, "\n system calls this run\n"); | ||
| 130 | REPORT(accept); | ||
| 131 | #ifdef SOCK_NONBLOCK | ||
| 132 | REPORT(accept4); | ||
| 133 | #endif | ||
| 134 | REPORT(read); | ||
| 135 | REPORT(write); | ||
| 136 | REPORT(close); | ||
| 137 | REPORT(fcntl); | ||
| 138 | REPORT(recvfrom); | ||
| 139 | REPORT(sendto); | ||
| 140 | #ifdef __linux__ | ||
| 141 | REPORT(epoll_ctl); | ||
| 142 | REPORT(epoll_wait); | ||
| 143 | #endif | ||
| 144 | } | ||
