map_perf_test_user.c 3.2 KB

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