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)
38 #define LRU_HASH_PREALLOC (1 << 4)
39 #define PERCPU_LRU_HASH_PREALLOC (1 << 5)
41 static int test_flags = ~0;
43 static void test_hash_prealloc(int cpu)
48 start_time = time_get_ns();
49 for (i = 0; i < MAX_CNT; i++)
51 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
52 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
55 static void test_lru_hash_prealloc(int cpu)
60 start_time = time_get_ns();
61 for (i = 0; i < MAX_CNT; i++)
63 printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
64 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
67 static void test_percpu_lru_hash_prealloc(int cpu)
72 start_time = time_get_ns();
73 for (i = 0; i < MAX_CNT; i++)
74 syscall(__NR_getppid);
75 printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
76 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
79 static void test_percpu_hash_prealloc(int cpu)
84 start_time = time_get_ns();
85 for (i = 0; i < MAX_CNT; i++)
86 syscall(__NR_geteuid);
87 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
88 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
91 static void test_hash_kmalloc(int cpu)
96 start_time = time_get_ns();
97 for (i = 0; i < MAX_CNT; i++)
99 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
100 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
103 static void test_percpu_hash_kmalloc(int cpu)
108 start_time = time_get_ns();
109 for (i = 0; i < MAX_CNT; i++)
110 syscall(__NR_getegid);
111 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
112 cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
115 static void loop(int cpu)
120 CPU_SET(cpu, &cpuset);
121 sched_setaffinity(0, sizeof(cpuset), &cpuset);
123 if (test_flags & HASH_PREALLOC)
124 test_hash_prealloc(cpu);
126 if (test_flags & PERCPU_HASH_PREALLOC)
127 test_percpu_hash_prealloc(cpu);
129 if (test_flags & HASH_KMALLOC)
130 test_hash_kmalloc(cpu);
132 if (test_flags & PERCPU_HASH_KMALLOC)
133 test_percpu_hash_kmalloc(cpu);
135 if (test_flags & LRU_HASH_PREALLOC)
136 test_lru_hash_prealloc(cpu);
138 if (test_flags & PERCPU_LRU_HASH_PREALLOC)
139 test_percpu_lru_hash_prealloc(cpu);
142 static void run_perf_test(int tasks)
147 for (i = 0; i < tasks; i++) {
152 } else if (pid[i] == -1) {
153 printf("couldn't spawn #%d process\n", i);
157 for (i = 0; i < tasks; i++) {
160 assert(waitpid(pid[i], &status, 0) == pid[i]);
165 int main(int argc, char **argv)
167 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
171 snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
172 setrlimit(RLIMIT_MEMLOCK, &r);
175 test_flags = atoi(argv[1]) ? : test_flags;
178 num_cpu = atoi(argv[2]) ? : num_cpu;
180 if (load_bpf_file(filename)) {
181 printf("%s", bpf_log_buf);
185 run_perf_test(num_cpu);