Skip to content

Instantly share code, notes, and snippets.

@pcarrier
Last active August 29, 2015 14:25
Show Gist options
  • Save pcarrier/f9ead17d2e85575a15b7 to your computer and use it in GitHub Desktop.
Save pcarrier/f9ead17d2e85575a15b7 to your computer and use it in GitHub Desktop.
callbench: clock timing
#include <fcntl.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/auxv.h>
#include <sys/time.h>
#include <syscall.h>
#include <time.h>
#include <unistd.h>
extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
extern void *vdso_sym(const char *version, const char *name);
int main(int argc, char **argv) {
int m = 0, i, n = 1000000;
int (*cgt)(clockid_t, struct timespec *);
void *e;
struct timespec ts;
if (argc > 2)
n = atoi(argv[2]);
if (argc > 1)
m = atoi(argv[1]);
switch (m) {
case 0:
for (i = 0; i < n; i++)
clock_gettime(CLOCK_MONOTONIC, &ts);
break;
case 1:
for (i = 0; i < n; i++)
syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts);
break;
case 2:
vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
cgt = (int (*)(clockid_t, struct timespec*)) vdso_sym("LINUX_2.6", "clock_gettime");
if (cgt == NULL)
exit(1);
for (i = 0; i < n; i++)
cgt(CLOCK_MONOTONIC, &ts);
break;
case 10:
e = sbrk(0);
for (i = 0; i < n; i++)
brk(e);
break;
case 11:
for (i = 0; i < n; i++)
fcntl(-1, 0, 0);
break;
default:
fprintf(stderr, "Usage: %s cmd cycles\n"
"0=clock_gettime, 1=SYS_clock_gettime, 2=vDSO, 10=brk, 11=fcntl\n",
argv[0]);
exit(1);
break;
}
}
callbench: callbench.o parse_vdso.o
musl-gcc -o $@ $? -lrt -static
sstrip $@
%.o: %.c
musl-gcc -c $< -o $@
clean:
rm *.o callbench
// From Documentation/vDSO/parse_vdso.c in linux.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment