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>
23 #define MAX_CNT 1000000
25 static __u64
time_get_ns(void)
29 clock_gettime(CLOCK_MONOTONIC
, &ts
);
30 return ts
.tv_sec
* 1000000000ull + ts
.tv_nsec
;
33 #define HASH_PREALLOC (1 << 0)
34 #define PERCPU_HASH_PREALLOC (1 << 1)
35 #define HASH_KMALLOC (1 << 2)
36 #define PERCPU_HASH_KMALLOC (1 << 3)
38 static int test_flags
= ~0;
40 static void test_hash_prealloc(int cpu
)
45 start_time
= time_get_ns();
46 for (i
= 0; i
< MAX_CNT
; i
++)
48 printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
49 cpu
, MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
52 static void test_percpu_hash_prealloc(int cpu
)
57 start_time
= time_get_ns();
58 for (i
= 0; i
< MAX_CNT
; i
++)
59 syscall(__NR_geteuid
);
60 printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
61 cpu
, MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
64 static void test_hash_kmalloc(int cpu
)
69 start_time
= time_get_ns();
70 for (i
= 0; i
< MAX_CNT
; i
++)
72 printf("%d:hash_map_perf kmalloc %lld events per sec\n",
73 cpu
, MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
76 static void test_percpu_hash_kmalloc(int cpu
)
81 start_time
= time_get_ns();
82 for (i
= 0; i
< MAX_CNT
; i
++)
83 syscall(__NR_getegid
);
84 printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
85 cpu
, MAX_CNT
* 1000000000ll / (time_get_ns() - start_time
));
88 static void loop(int cpu
)
93 CPU_SET(cpu
, &cpuset
);
94 sched_setaffinity(0, sizeof(cpuset
), &cpuset
);
96 if (test_flags
& HASH_PREALLOC
)
97 test_hash_prealloc(cpu
);
99 if (test_flags
& PERCPU_HASH_PREALLOC
)
100 test_percpu_hash_prealloc(cpu
);
102 if (test_flags
& HASH_KMALLOC
)
103 test_hash_kmalloc(cpu
);
105 if (test_flags
& PERCPU_HASH_KMALLOC
)
106 test_percpu_hash_kmalloc(cpu
);
109 static void run_perf_test(int tasks
)
114 for (i
= 0; i
< tasks
; i
++) {
119 } else if (pid
[i
] == -1) {
120 printf("couldn't spawn #%d process\n", i
);
124 for (i
= 0; i
< tasks
; i
++) {
127 assert(waitpid(pid
[i
], &status
, 0) == pid
[i
]);
132 int main(int argc
, char **argv
)
134 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
138 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
139 setrlimit(RLIMIT_MEMLOCK
, &r
);
142 test_flags
= atoi(argv
[1]) ? : test_flags
;
145 num_cpu
= atoi(argv
[2]) ? : num_cpu
;
147 if (load_bpf_file(filename
)) {
148 printf("%s", bpf_log_buf
);
152 run_perf_test(num_cpu
);
This page took 0.072465 seconds and 5 git commands to generate.