map_perf_test_kern.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include <linux/skbuff.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/version.h>
  10. #include <uapi/linux/bpf.h>
  11. #include "bpf_helpers.h"
  12. #define MAX_ENTRIES 1000
  13. struct bpf_map_def SEC("maps") hash_map = {
  14. .type = BPF_MAP_TYPE_HASH,
  15. .key_size = sizeof(u32),
  16. .value_size = sizeof(long),
  17. .max_entries = MAX_ENTRIES,
  18. };
  19. struct bpf_map_def SEC("maps") lru_hash_map = {
  20. .type = BPF_MAP_TYPE_LRU_HASH,
  21. .key_size = sizeof(u32),
  22. .value_size = sizeof(long),
  23. .max_entries = 10000,
  24. };
  25. struct bpf_map_def SEC("maps") percpu_lru_hash_map = {
  26. .type = BPF_MAP_TYPE_LRU_HASH,
  27. .key_size = sizeof(u32),
  28. .value_size = sizeof(long),
  29. .max_entries = 10000,
  30. .map_flags = BPF_F_NO_COMMON_LRU,
  31. };
  32. struct bpf_map_def SEC("maps") percpu_hash_map = {
  33. .type = BPF_MAP_TYPE_PERCPU_HASH,
  34. .key_size = sizeof(u32),
  35. .value_size = sizeof(long),
  36. .max_entries = MAX_ENTRIES,
  37. };
  38. struct bpf_map_def SEC("maps") hash_map_alloc = {
  39. .type = BPF_MAP_TYPE_HASH,
  40. .key_size = sizeof(u32),
  41. .value_size = sizeof(long),
  42. .max_entries = MAX_ENTRIES,
  43. .map_flags = BPF_F_NO_PREALLOC,
  44. };
  45. struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
  46. .type = BPF_MAP_TYPE_PERCPU_HASH,
  47. .key_size = sizeof(u32),
  48. .value_size = sizeof(long),
  49. .max_entries = MAX_ENTRIES,
  50. .map_flags = BPF_F_NO_PREALLOC,
  51. };
  52. struct bpf_map_def SEC("maps") lpm_trie_map_alloc = {
  53. .type = BPF_MAP_TYPE_LPM_TRIE,
  54. .key_size = 8,
  55. .value_size = sizeof(long),
  56. .max_entries = 10000,
  57. .map_flags = BPF_F_NO_PREALLOC,
  58. };
  59. struct bpf_map_def SEC("maps") array_map = {
  60. .type = BPF_MAP_TYPE_ARRAY,
  61. .key_size = sizeof(u32),
  62. .value_size = sizeof(long),
  63. .max_entries = MAX_ENTRIES,
  64. };
  65. SEC("kprobe/sys_getuid")
  66. int stress_hmap(struct pt_regs *ctx)
  67. {
  68. u32 key = bpf_get_current_pid_tgid();
  69. long init_val = 1;
  70. long *value;
  71. bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
  72. value = bpf_map_lookup_elem(&hash_map, &key);
  73. if (value)
  74. bpf_map_delete_elem(&hash_map, &key);
  75. return 0;
  76. }
  77. SEC("kprobe/sys_geteuid")
  78. int stress_percpu_hmap(struct pt_regs *ctx)
  79. {
  80. u32 key = bpf_get_current_pid_tgid();
  81. long init_val = 1;
  82. long *value;
  83. bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
  84. value = bpf_map_lookup_elem(&percpu_hash_map, &key);
  85. if (value)
  86. bpf_map_delete_elem(&percpu_hash_map, &key);
  87. return 0;
  88. }
  89. SEC("kprobe/sys_getgid")
  90. int stress_hmap_alloc(struct pt_regs *ctx)
  91. {
  92. u32 key = bpf_get_current_pid_tgid();
  93. long init_val = 1;
  94. long *value;
  95. bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
  96. value = bpf_map_lookup_elem(&hash_map_alloc, &key);
  97. if (value)
  98. bpf_map_delete_elem(&hash_map_alloc, &key);
  99. return 0;
  100. }
  101. SEC("kprobe/sys_getegid")
  102. int stress_percpu_hmap_alloc(struct pt_regs *ctx)
  103. {
  104. u32 key = bpf_get_current_pid_tgid();
  105. long init_val = 1;
  106. long *value;
  107. bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
  108. value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
  109. if (value)
  110. bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
  111. return 0;
  112. }
  113. SEC("kprobe/sys_getpid")
  114. int stress_lru_hmap_alloc(struct pt_regs *ctx)
  115. {
  116. u32 key = bpf_get_prandom_u32();
  117. long val = 1;
  118. bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
  119. return 0;
  120. }
  121. SEC("kprobe/sys_getppid")
  122. int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
  123. {
  124. u32 key = bpf_get_prandom_u32();
  125. long val = 1;
  126. bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
  127. return 0;
  128. }
  129. SEC("kprobe/sys_gettid")
  130. int stress_lpm_trie_map_alloc(struct pt_regs *ctx)
  131. {
  132. union {
  133. u32 b32[2];
  134. u8 b8[8];
  135. } key;
  136. unsigned int i;
  137. key.b32[0] = 32;
  138. key.b8[4] = 192;
  139. key.b8[5] = 168;
  140. key.b8[6] = 0;
  141. key.b8[7] = 1;
  142. #pragma clang loop unroll(full)
  143. for (i = 0; i < 32; ++i)
  144. bpf_map_lookup_elem(&lpm_trie_map_alloc, &key);
  145. return 0;
  146. }
  147. SEC("kprobe/sys_getpgid")
  148. int stress_hash_map_lookup(struct pt_regs *ctx)
  149. {
  150. u32 key = 1, i;
  151. long *value;
  152. #pragma clang loop unroll(full)
  153. for (i = 0; i < 64; ++i)
  154. value = bpf_map_lookup_elem(&hash_map, &key);
  155. return 0;
  156. }
  157. SEC("kprobe/sys_getpgrp")
  158. int stress_array_map_lookup(struct pt_regs *ctx)
  159. {
  160. u32 key = 1, i;
  161. long *value;
  162. #pragma clang loop unroll(full)
  163. for (i = 0; i < 64; ++i)
  164. value = bpf_map_lookup_elem(&array_map, &key);
  165. return 0;
  166. }
  167. char _license[] SEC("license") = "GPL";
  168. u32 _version SEC("version") = LINUX_VERSION_CODE;