map_perf_test_kern.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. SEC("kprobe/sys_getuid")
  53. int stress_hmap(struct pt_regs *ctx)
  54. {
  55. u32 key = bpf_get_current_pid_tgid();
  56. long init_val = 1;
  57. long *value;
  58. bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
  59. value = bpf_map_lookup_elem(&hash_map, &key);
  60. if (value)
  61. bpf_map_delete_elem(&hash_map, &key);
  62. return 0;
  63. }
  64. SEC("kprobe/sys_geteuid")
  65. int stress_percpu_hmap(struct pt_regs *ctx)
  66. {
  67. u32 key = bpf_get_current_pid_tgid();
  68. long init_val = 1;
  69. long *value;
  70. bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
  71. value = bpf_map_lookup_elem(&percpu_hash_map, &key);
  72. if (value)
  73. bpf_map_delete_elem(&percpu_hash_map, &key);
  74. return 0;
  75. }
  76. SEC("kprobe/sys_getgid")
  77. int stress_hmap_alloc(struct pt_regs *ctx)
  78. {
  79. u32 key = bpf_get_current_pid_tgid();
  80. long init_val = 1;
  81. long *value;
  82. bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
  83. value = bpf_map_lookup_elem(&hash_map_alloc, &key);
  84. if (value)
  85. bpf_map_delete_elem(&hash_map_alloc, &key);
  86. return 0;
  87. }
  88. SEC("kprobe/sys_getegid")
  89. int stress_percpu_hmap_alloc(struct pt_regs *ctx)
  90. {
  91. u32 key = bpf_get_current_pid_tgid();
  92. long init_val = 1;
  93. long *value;
  94. bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
  95. value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
  96. if (value)
  97. bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
  98. return 0;
  99. }
  100. SEC("kprobe/sys_getpid")
  101. int stress_lru_hmap_alloc(struct pt_regs *ctx)
  102. {
  103. u32 key = bpf_get_prandom_u32();
  104. long val = 1;
  105. bpf_map_update_elem(&lru_hash_map, &key, &val, BPF_ANY);
  106. return 0;
  107. }
  108. SEC("kprobe/sys_getppid")
  109. int stress_percpu_lru_hmap_alloc(struct pt_regs *ctx)
  110. {
  111. u32 key = bpf_get_prandom_u32();
  112. long val = 1;
  113. bpf_map_update_elem(&percpu_lru_hash_map, &key, &val, BPF_ANY);
  114. return 0;
  115. }
  116. char _license[] SEC("license") = "GPL";
  117. u32 _version SEC("version") = LINUX_VERSION_CODE;