diff options
Diffstat (limited to 'targets')
| -rw-r--r-- | targets/agent_bench.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/targets/agent_bench.c b/targets/agent_bench.c new file mode 100644 index 0000000..e1add0e --- /dev/null +++ b/targets/agent_bench.c @@ -0,0 +1,45 @@ +// Loops to measure the agent with: `agent_bench WHAT N` runs one N times +// and prints the nanoseconds an iteration took. +// malloc a malloc and a free +// syscall a getppid +// call a call of `work`, which writes one word of `kb64` +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/syscall.h> +#include <time.h> +#include <unistd.h> + +// Memory to watch, of three sizes. +long kb1[1 << 7], kb64[1 << 13], mb1[1 << 17]; + +__attribute__((noinline)) long work(long i) { + kb64[i & 8191] = i; + return i * 3; +} + +static double now(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1e9 + ts.tv_nsec; +} + +int main(int argc, char **argv) { + if (argc < 3) return 2; + long n = atol(argv[2]); + long sum = 0; + void *slots[64] = {0}; + double start = now(); + if (!strcmp(argv[1], "malloc")) { + for (long i = 0; i < n; i++) { + free(slots[i & 63]); + slots[i & 63] = malloc(16 + (i & 255)); + } + } else if (!strcmp(argv[1], "syscall")) { + for (long i = 0; i < n; i++) sum += syscall(SYS_getppid); + } else if (!strcmp(argv[1], "call")) { + for (long i = 0; i < n; i++) sum += work(i); + } else return 2; + printf("%s: %.1f ns each (%ld)\n", argv[1], (now() - start) / n, sum & 1); + return 0; +} |
