map_perf_test_user.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. static int test_flags = ~0;
  37. static void test_hash_prealloc(int cpu)
  38. {
  39. __u64 start_time;
  40. int i;
  41. start_time = time_get_ns();
  42. for (i = 0; i < MAX_CNT; i++)
  43. syscall(__NR_getuid);
  44. printf("%d:hash_map_perf pre-alloc %lld events per sec\n",
  45. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  46. }
  47. static void test_lru_hash_prealloc(int cpu)
  48. {
  49. __u64 start_time;
  50. int i;
  51. start_time = time_get_ns();
  52. for (i = 0; i < MAX_CNT; i++)
  53. syscall(__NR_getpid);
  54. printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
  55. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  56. }
  57. static void test_percpu_lru_hash_prealloc(int cpu)
  58. {
  59. __u64 start_time;
  60. int i;
  61. start_time = time_get_ns();
  62. for (i = 0; i < MAX_CNT; i++)
  63. syscall(__NR_getppid);
  64. printf("%d:lru_hash_map_perf pre-alloc %lld events per sec\n",
  65. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  66. }
  67. static void test_percpu_hash_prealloc(int cpu)
  68. {
  69. __u64 start_time;
  70. int i;
  71. start_time = time_get_ns();
  72. for (i = 0; i < MAX_CNT; i++)
  73. syscall(__NR_geteuid);
  74. printf("%d:percpu_hash_map_perf pre-alloc %lld events per sec\n",
  75. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  76. }
  77. static void test_hash_kmalloc(int cpu)
  78. {
  79. __u64 start_time;
  80. int i;
  81. start_time = time_get_ns();
  82. for (i = 0; i < MAX_CNT; i++)
  83. syscall(__NR_getgid);
  84. printf("%d:hash_map_perf kmalloc %lld events per sec\n",
  85. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  86. }
  87. static void test_percpu_hash_kmalloc(int cpu)
  88. {
  89. __u64 start_time;
  90. int i;
  91. start_time = time_get_ns();
  92. for (i = 0; i < MAX_CNT; i++)
  93. syscall(__NR_getegid);
  94. printf("%d:percpu_hash_map_perf kmalloc %lld events per sec\n",
  95. cpu, MAX_CNT * 1000000000ll / (time_get_ns() - start_time));
  96. }
  97. static void loop(int cpu)
  98. {
  99. cpu_set_t cpuset;
  100. CPU_ZERO(&cpuset);
  101. CPU_SET(cpu, &cpuset);
  102. sched_setaffinity(0, sizeof(cpuset), &cpuset);
  103. if (test_flags & HASH_PREALLOC)
  104. test_hash_prealloc(cpu);
  105. if (test_flags & PERCPU_HASH_PREALLOC)
  106. test_percpu_hash_prealloc(cpu);
  107. if (test_flags & HASH_KMALLOC)
  108. test_hash_kmalloc(cpu);
  109. if (test_flags & PERCPU_HASH_KMALLOC)
  110. test_percpu_hash_kmalloc(cpu);
  111. if (test_flags & LRU_HASH_PREALLOC)
  112. test_lru_hash_prealloc(cpu);
  113. if (test_flags & PERCPU_LRU_HASH_PREALLOC)
  114. test_percpu_lru_hash_prealloc(cpu);
  115. }
  116. static void run_perf_test(int tasks)
  117. {
  118. pid_t pid[tasks];
  119. int i;
  120. for (i = 0; i < tasks; i++) {
  121. pid[i] = fork();
  122. if (pid[i] == 0) {
  123. loop(i);
  124. exit(0);
  125. } else if (pid[i] == -1) {
  126. printf("couldn't spawn #%d process\n", i);
  127. exit(1);
  128. }
  129. }
  130. for (i = 0; i < tasks; i++) {
  131. int status;
  132. assert(waitpid(pid[i], &status, 0) == pid[i]);
  133. assert(status == 0);
  134. }
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  139. char filename[256];
  140. int num_cpu = 8;
  141. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  142. setrlimit(RLIMIT_MEMLOCK, &r);
  143. if (argc > 1)
  144. test_flags = atoi(argv[1]) ? : test_flags;
  145. if (argc > 2)
  146. num_cpu = atoi(argv[2]) ? : num_cpu;
  147. if (load_bpf_file(filename)) {
  148. printf("%s", bpf_log_buf);
  149. return 1;
  150. }
  151. run_perf_test(num_cpu);
  152. return 0;
  153. }