map_perf_test_user.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* Copyright (c) 2016 Facebook
  2. *
  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.
  6. */
  7. #define _GNU_SOURCE
  8. #include <sched.h>
  9. #include <stdio.h>
  10. #include <sys/types.h>
  11. #include <asm/unistd.h>
  12. #include <unistd.h>
  13. #include <assert.h>
  14. #include <sys/wait.h>
  15. #include <stdlib.h>
  16. #include <signal.h>
  17. #include <linux/bpf.h>
  18. #include <string.h>
  19. #include <time.h>
  20. #include <sys/resource.h>
  21. #include "libbpf.h"
  22. #include "bpf_load.h"
  23. #define MAX_CNT 1000000
  24. static __u64 time_get_ns(void)
  25. {
  26. struct timespec ts;
  27. clock_gettime(CLOCK_MONOTONIC, &ts);
  28. return ts.tv_sec * 1000000000ull + ts.tv_nsec;
  29. }
  30. #define HASH_PREALLOC (1 << 0)
  31. #define PERCPU_HASH_PREALLOC (1 << 1)
  32. #define HASH_KMALLOC (1 << 2)
  33. #define PERCPU_HASH_KMALLOC (1 << 3)
  34. #define LRU_HASH_PREALLOC (1 << 4)
  35. #define PERCPU_LRU_HASH_PREALLOC (1 << 5)
  36. #define LPM_KMALLOC (1 << 6)
  37. #define HASH_LOOKUP (1 << 7)
  38. #define ARRAY_LOOKUP (1 << 8)
  39. static int test_flags = ~0;
  40. static void test_hash_prealloc(int cpu)
  41. {
  42. __u64 start_time;
  43. int i;
  44. start_time = time_get_ns();
  45. for (i = 0; i < MAX_CNT; i++)
  46. syscall(__NR_getuid);
  47. printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
  48. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  49. }
  50. static void test_lru_hash_prealloc(int cpu)
  51. {
  52. __u64 start_time;
  53. int i;
  54. start_time = time_get_ns();
  55. for (i = 0; i < MAX_CNT; i++)
  56. syscall(__NR_getpid);
  57. printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
  58. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  59. }
  60. static void test_percpu_lru_hash_prealloc(int cpu)
  61. {
  62. __u64 start_time;
  63. int i;
  64. start_time = time_get_ns();
  65. for (i = 0; i < MAX_CNT; i++)
  66. syscall(__NR_getppid);
  67. printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
  68. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  69. }
  70. static void test_percpu_hash_prealloc(int cpu)
  71. {
  72. __u64 start_time;
  73. int i;
  74. start_time = time_get_ns();
  75. for (i = 0; i < MAX_CNT; i++)
  76. syscall(__NR_geteuid);
  77. printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
  78. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  79. }
  80. static void test_hash_kmalloc(int cpu)
  81. {
  82. __u64 start_time;
  83. int i;
  84. start_time = time_get_ns();
  85. for (i = 0; i < MAX_CNT; i++)
  86. syscall(__NR_getgid);
  87. printf("%d:hash_map_perf kmalloc %lld events per sec\n",
  88. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  89. }
  90. static void test_percpu_hash_kmalloc(int cpu)
  91. {
  92. __u64 start_time;
  93. int i;
  94. start_time = time_get_ns();
  95. for (i = 0; i < MAX_CNT; i++)
  96. syscall(__NR_getegid);
  97. printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
  98. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  99. }
  100. static void test_lpm_kmalloc(int cpu)
  101. {
  102. __u64 start_time;
  103. int i;
  104. start_time = time_get_ns();
  105. for (i = 0; i < MAX_CNT; i++)
  106. syscall(__NR_gettid);
  107. printf("%d:lpm_perf kmalloc %lld events per sec\n",
  108. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  109. }
  110. static void test_hash_lookup(int cpu)
  111. {
  112. __u64 start_time;
  113. int i;
  114. start_time = time_get_ns();
  115. for (i = 0; i < MAX_CNT; i++)
  116. syscall(__NR_getpgid, 0);
  117. printf("%d:hash_lookup %lld lookups per sec\n",
  118. cpu, MAX_CNT * 1000000000ll * 64 / (time_get_ns() - start_time));
  119. }
  120. static void test_array_lookup(int cpu)
  121. {
  122. __u64 start_time;
  123. int i;
  124. start_time = time_get_ns();
  125. for (i = 0; i < MAX_CNT; i++)
  126. syscall(__NR_getpgrp, 0);
  127. printf("%d:array_lookup %lld lookups per sec\n",
  128. cpu, MAX_CNT * 1000000000ll * 64 / (time_get_ns() - start_time));
  129. }
  130. static void loop(int cpu)
  131. {
  132. cpu_set_t cpuset;
  133. CPU_ZERO(&cpuset);
  134. CPU_SET(cpu, &cpuset);
  135. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  136. if (test_flags & HASH_PREALLOC)
  137. test_hash_prealloc(cpu);
  138. if (test_flags & PERCPU_HASH_PREALLOC)
  139. test_percpu_hash_prealloc(cpu);
  140. if (test_flags & HASH_KMALLOC)
  141. test_hash_kmalloc(cpu);
  142. if (test_flags & PERCPU_HASH_KMALLOC)
  143. test_percpu_hash_kmalloc(cpu);
  144. if (test_flags & LRU_HASH_PREALLOC)
  145. test_lru_hash_prealloc(cpu);
  146. if (test_flags & PERCPU_LRU_HASH_PREALLOC)
  147. test_percpu_lru_hash_prealloc(cpu);
  148. if (test_flags & LPM_KMALLOC)
  149. test_lpm_kmalloc(cpu);
  150. if (test_flags & HASH_LOOKUP)
  151. test_hash_lookup(cpu);
  152. if (test_flags & ARRAY_LOOKUP)
  153. test_array_lookup(cpu);
  154. }
  155. static void run_perf_test(int tasks)
  156. {
  157. pid_t pid[tasks];
  158. int i;
  159. for (i = 0; i < tasks; i++) {
  160. pid[i] = fork();
  161. if (pid[i] == 0) {
  162. loop(i);
  163. exit(0);
  164. } else if (pid[i] == -1) {
  165. printf("couldn't spawn #%d process\n", i);
  166. exit(1);
  167. }
  168. }
  169. for (i = 0; i < tasks; i++) {
  170. int status;
  171. assert(waitpid(pid[i], &status, 0) == pid[i]);
  172. assert(status == 0);
  173. }
  174. }
  175. static void fill_lpm_trie(void)
  176. {
  177. struct bpf_lpm_trie_key *key;
  178. unsigned long value = 0;
  179. unsigned int i;
  180. int r;
  181. key = alloca(sizeof(*key) + 4);
  182. key->prefixlen = 32;
  183. for (i = 0; i < 512; ++i) {
  184. key->prefixlen = rand() % 33;
  185. key->data[0] = rand() & 0xff;
  186. key->data[1] = rand() & 0xff;
  187. key->data[2] = rand() & 0xff;
  188. key->data[3] = rand() & 0xff;
  189. r = bpf_map_update_elem(map_fd[6], key, &value, 0);
  190. assert(!r);
  191. }
  192. key->prefixlen = 32;
  193. key->data[0] = 192;
  194. key->data[1] = 168;
  195. key->data[2] = 0;
  196. key->data[3] = 1;
  197. value = 128;
  198. r = bpf_map_update_elem(map_fd[6], key, &value, 0);
  199. assert(!r);
  200. }
  201. int main(int argc, char **argv)
  202. {
  203. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  204. char filename[256];
  205. int num_cpu = 8;
  206. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  207. setrlimit(RLIMIT_MEMLOCK, &r);
  208. if (argc > 1)
  209. test_flags = atoi(argv[1]) ? : test_flags;
  210. if (argc > 2)
  211. num_cpu = atoi(argv[2]) ? : num_cpu;
  212. if (load_bpf_file(filename)) {
  213. printf("%s", bpf_log_buf);
  214. return 1;
  215. }
  216. fill_lpm_trie();
  217. run_perf_test(num_cpu);
  218. return 0;
  219. }