open-syscall-all-cpus.c 2.9 KB

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