affinity.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Manage affinity to optimize IPIs inside the kernel perf API. */
  3. #define _GNU_SOURCE 1
  4. #include <sched.h>
  5. #include <stdlib.h>
  6. #include <linux/bitmap.h>
  7. #include <linux/zalloc.h>
  8. #include <perf/cpumap.h>
  9. #include "perf.h"
  10. #include "cpumap.h"
  11. #include "affinity.h"
  12. static int get_cpu_set_size(void)
  13. {
  14. int sz = cpu__max_cpu().cpu + 8 - 1;
  15. /*
  16. * sched_getaffinity doesn't like masks smaller than the kernel.
  17. * Hopefully that's big enough.
  18. */
  19. if (sz < 4096)
  20. sz = 4096;
  21. return sz / 8;
  22. }
  23. int affinity__setup(struct affinity *a)
  24. {
  25. int cpu_set_size = get_cpu_set_size();
  26. a->orig_cpus = bitmap_zalloc(cpu_set_size * 8);
  27. if (!a->orig_cpus)
  28. return -1;
  29. sched_getaffinity(0, cpu_set_size, (cpu_set_t *)a->orig_cpus);
  30. a->sched_cpus = bitmap_zalloc(cpu_set_size * 8);
  31. if (!a->sched_cpus) {
  32. zfree(&a->orig_cpus);
  33. return -1;
  34. }
  35. bitmap_zero((unsigned long *)a->sched_cpus, cpu_set_size);
  36. a->changed = false;
  37. return 0;
  38. }
  39. /*
  40. * perf_event_open does an IPI internally to the target CPU.
  41. * It is more efficient to change perf's affinity to the target
  42. * CPU and then set up all events on that CPU, so we amortize
  43. * CPU communication.
  44. */
  45. void affinity__set(struct affinity *a, int cpu)
  46. {
  47. int cpu_set_size = get_cpu_set_size();
  48. /*
  49. * Return:
  50. * - if cpu is -1
  51. * - restrict out of bound access to sched_cpus
  52. */
  53. if (cpu == -1 || ((cpu >= (cpu_set_size * 8))))
  54. return;
  55. a->changed = true;
  56. __set_bit(cpu, a->sched_cpus);
  57. /*
  58. * We ignore errors because affinity is just an optimization.
  59. * This could happen for example with isolated CPUs or cpusets.
  60. * In this case the IPIs inside the kernel's perf API still work.
  61. */
  62. sched_setaffinity(0, cpu_set_size, (cpu_set_t *)a->sched_cpus);
  63. __clear_bit(cpu, a->sched_cpus);
  64. }
  65. static void __affinity__cleanup(struct affinity *a)
  66. {
  67. int cpu_set_size = get_cpu_set_size();
  68. if (a->changed)
  69. sched_setaffinity(0, cpu_set_size, (cpu_set_t *)a->orig_cpus);
  70. zfree(&a->sched_cpus);
  71. zfree(&a->orig_cpus);
  72. }
  73. void affinity__cleanup(struct affinity *a)
  74. {
  75. if (a != NULL)
  76. __affinity__cleanup(a);
  77. }
  78. void cpu_map__set_affinity(const struct perf_cpu_map *cpumap)
  79. {
  80. int cpu_set_size = get_cpu_set_size();
  81. unsigned long *cpuset = bitmap_zalloc(cpu_set_size * 8);
  82. struct perf_cpu cpu;
  83. int idx;
  84. if (!cpuset)
  85. return;
  86. perf_cpu_map__for_each_cpu_skip_any(cpu, idx, cpumap)
  87. __set_bit(cpu.cpu, cpuset);
  88. sched_setaffinity(0, cpu_set_size, (cpu_set_t *)cpuset);
  89. zfree(&cpuset);
  90. }