1 /* Copyright (c) 2016 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
10 #include <sys/types.h>
11 #include <asm/unistd.h>
17 #include <linux/bpf.h>
20 #include <sys/resource.h>
24 #define MAX_CNT 1000000
26 static __u64 time_get_ns(void)
30 clock_gettime(CLOCK_MONOTONIC, &ts);
31 return ts.tv_sec * 1000000000ull + ts.tv_nsec;
34 #define HASH_PREALLOC (1 << 0)
35 #define PERCPU_HASH_PREALLOC (1 << 1)
36 #define HASH_KMALLOC (1 << 2)
37 #define PERCPU_HASH_KMALLOC (1 << 3)
39 static int test_flags = ~0;
41 static void test_hash_prealloc(int cpu)
46 start_time = time_get_ns();
47 for (i = 0; i < MAX_CNT; i++)
49 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
50 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
53 static void test_percpu_hash_prealloc(int cpu)
58 start_time = time_get_ns();
59 for (i = 0; i < MAX_CNT; i++)
60 syscall(__NR_geteuid);
61 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
62 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
65 static void test_hash_kmalloc(int cpu)
70 start_time = time_get_ns();
71 for (i = 0; i < MAX_CNT; i++)
73 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
74 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
77 static void test_percpu_hash_kmalloc(int cpu)
82 start_time = time_get_ns();
83 for (i = 0; i < MAX_CNT; i++)
84 syscall(__NR_getegid);
85 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
86 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
89 static void loop(int cpu)
94 CPU_SET(cpu, &cpuset);
95 sched_setaffinity(0, sizeof(cpuset), &cpuset);
97 if (test_flags & HASH_PREALLOC)
98 test_hash_prealloc(cpu);
100 if (test_flags & PERCPU_HASH_PREALLOC)
101 test_percpu_hash_prealloc(cpu);
103 if (test_flags & HASH_KMALLOC)
104 test_hash_kmalloc(cpu);
106 if (test_flags & PERCPU_HASH_KMALLOC)
107 test_percpu_hash_kmalloc(cpu);
110 static void run_perf_test(int tasks)
115 for (i = 0; i < tasks; i++) {
120 } else if (pid[i] == -1) {
121 printf("couldn't spawn #%d process\n", i);
125 for (i = 0; i < tasks; i++) {
128 assert(waitpid(pid[i], &status, 0) == pid[i]);
133 int main(int argc, char **argv)
135 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
139 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
140 setrlimit(RLIMIT_MEMLOCK, &r);
143 test_flags = atoi(argv[1]) ? : test_flags;
146 num_cpu = atoi(argv[2]) ? : num_cpu;
148 if (load_bpf_file(filename)) {
149 printf("%s", bpf_log_buf);
153 run_perf_test(num_cpu);