map_perf_test_kern.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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") percpu_hash_map = {
  20. .type = BPF_MAP_TYPE_PERCPU_HASH,
  21. .key_size = sizeof(u32),
  22. .value_size = sizeof(long),
  23. .max_entries = MAX_ENTRIES,
  24. };
  25. struct bpf_map_def SEC("maps") hash_map_alloc = {
  26. .type = BPF_MAP_TYPE_HASH,
  27. .key_size = sizeof(u32),
  28. .value_size = sizeof(long),
  29. .max_entries = MAX_ENTRIES,
  30. .map_flags = BPF_F_NO_PREALLOC,
  31. };
  32. struct bpf_map_def SEC("maps") percpu_hash_map_alloc = {
  33. .type = BPF_MAP_TYPE_PERCPU_HASH,
  34. .key_size = sizeof(u32),
  35. .value_size = sizeof(long),
  36. .max_entries = MAX_ENTRIES,
  37. .map_flags = BPF_F_NO_PREALLOC,
  38. };
  39. SEC("kprobe/sys_getuid")
  40. int stress_hmap(struct pt_regs *ctx)
  41. {
  42. u32 key = bpf_get_current_pid_tgid();
  43. long init_val = 1;
  44. long *value;
  45. bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
  46. value = bpf_map_lookup_elem(&hash_map, &key);
  47. if (value)
  48. bpf_map_delete_elem(&hash_map, &key);
  49. return 0;
  50. }
  51. SEC("kprobe/sys_geteuid")
  52. int stress_percpu_hmap(struct pt_regs *ctx)
  53. {
  54. u32 key = bpf_get_current_pid_tgid();
  55. long init_val = 1;
  56. long *value;
  57. bpf_map_update_elem(&percpu_hash_map, &key, &init_val, BPF_ANY);
  58. value = bpf_map_lookup_elem(&percpu_hash_map, &key);
  59. if (value)
  60. bpf_map_delete_elem(&percpu_hash_map, &key);
  61. return 0;
  62. }
  63. SEC("kprobe/sys_getgid")
  64. int stress_hmap_alloc(struct pt_regs *ctx)
  65. {
  66. u32 key = bpf_get_current_pid_tgid();
  67. long init_val = 1;
  68. long *value;
  69. bpf_map_update_elem(&hash_map_alloc, &key, &init_val, BPF_ANY);
  70. value = bpf_map_lookup_elem(&hash_map_alloc, &key);
  71. if (value)
  72. bpf_map_delete_elem(&hash_map_alloc, &key);
  73. return 0;
  74. }
  75. SEC("kprobe/sys_getegid")
  76. int stress_percpu_hmap_alloc(struct pt_regs *ctx)
  77. {
  78. u32 key = bpf_get_current_pid_tgid();
  79. long init_val = 1;
  80. long *value;
  81. bpf_map_update_elem(&percpu_hash_map_alloc, &key, &init_val, BPF_ANY);
  82. value = bpf_map_lookup_elem(&percpu_hash_map_alloc, &key);
  83. if (value)
  84. bpf_map_delete_elem(&percpu_hash_map_alloc, &key);
  85. return 0;
  86. }
  87. char _license[] SEC("license") = "GPL";
  88. u32 _version SEC("version") = LINUX_VERSION_CODE;