map_perf_test_user.c 3.2 KB

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