open-syscall-all-cpus.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "evsel.h"
  2. #include "tests.h"
  3. #include "thread_map.h"
  4. #include "cpumap.h"
  5. #include "debug.h"
  6. int test__open_syscall_event_on_all_cpus(void)
  7. {
  8. int err = -1, fd, cpu;
  9. struct cpu_map *cpus;
  10. struct perf_evsel *evsel;
  11. unsigned int nr_open_calls = 111, i;
  12. cpu_set_t cpu_set;
  13. struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
  14. if (threads == NULL) {
  15. pr_debug("thread_map__new\n");
  16. return -1;
  17. }
  18. cpus = cpu_map__new(NULL);
  19. if (cpus == NULL) {
  20. pr_debug("cpu_map__new\n");
  21. goto out_thread_map_delete;
  22. }
  23. CPU_ZERO(&cpu_set);
  24. evsel = perf_evsel__newtp("syscalls", "sys_enter_open", 0);
  25. if (evsel == NULL) {
  26. pr_debug("is debugfs mounted on /sys/kernel/debug?\n");
  27. goto out_thread_map_delete;
  28. }
  29. if (perf_evsel__open(evsel, cpus, threads) < 0) {
  30. pr_debug("failed to open counter: %s, "
  31. "tweak /proc/sys/kernel/perf_event_paranoid?\n",
  32. strerror(errno));
  33. goto out_evsel_delete;
  34. }
  35. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  36. unsigned int ncalls = nr_open_calls + cpu;
  37. /*
  38. * XXX eventually lift this restriction in a way that
  39. * keeps perf building on older glibc installations
  40. * without CPU_ALLOC. 1024 cpus in 2010 still seems
  41. * a reasonable upper limit tho :-)
  42. */
  43. if (cpus->map[cpu] >= CPU_SETSIZE) {
  44. pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
  45. continue;
  46. }
  47. CPU_SET(cpus->map[cpu], &cpu_set);
  48. if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
  49. pr_debug("sched_setaffinity() failed on CPU %d: %s ",
  50. cpus->map[cpu],
  51. strerror(errno));
  52. goto out_close_fd;
  53. }
  54. for (i = 0; i < ncalls; ++i) {
  55. fd = open("/etc/passwd", O_RDONLY);
  56. close(fd);
  57. }
  58. CPU_CLR(cpus->map[cpu], &cpu_set);
  59. }
  60. /*
  61. * Here we need to explicitely preallocate the counts, as if
  62. * we use the auto allocation it will allocate just for 1 cpu,
  63. * as we start by cpu 0.
  64. */
  65. if (perf_evsel__alloc_counts(evsel, cpus->nr) < 0) {
  66. pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
  67. goto out_close_fd;
  68. }
  69. err = 0;
  70. for (cpu = 0; cpu < cpus->nr; ++cpu) {
  71. unsigned int expected;
  72. if (cpus->map[cpu] >= CPU_SETSIZE)
  73. continue;
  74. if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
  75. pr_debug("perf_evsel__read_on_cpu\n");
  76. err = -1;
  77. break;
  78. }
  79. expected = nr_open_calls + cpu;
  80. if (evsel->counts->cpu[cpu].val != expected) {
  81. pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
  82. expected, cpus->map[cpu], evsel->counts->cpu[cpu].val);
  83. err = -1;
  84. }
  85. }
  86. perf_evsel__free_counts(evsel);
  87. out_close_fd:
  88. perf_evsel__close_fd(evsel, 1, threads->nr);
  89. out_evsel_delete:
  90. perf_evsel__delete(evsel);
  91. out_thread_map_delete:
  92. thread_map__delete(threads);
  93. return err;
  94. }