// 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 #include #include #include #include #include // 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; }