summaryrefslogtreecommitdiff
path: root/targets/agent_bench.c
blob: e1add0e54c2592f55b9fd94e3e9fca305578cfb2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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;
}