sw-clock.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <errno.h>
  3. #include <inttypes.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <sys/mman.h>
  8. #include <linux/string.h>
  9. #include "tests.h"
  10. #include "util/debug.h"
  11. #include "util/evsel.h"
  12. #include "util/evlist.h"
  13. #include "util/cpumap.h"
  14. #include "util/mmap.h"
  15. #include "util/sample.h"
  16. #include "util/thread_map.h"
  17. #include <perf/evlist.h>
  18. #include <perf/mmap.h>
  19. #define NR_LOOPS 10000000
  20. /*
  21. * This test will open software clock events (cpu-clock, task-clock)
  22. * then check their frequency -> period conversion has no artifact of
  23. * setting period to 1 forcefully.
  24. */
  25. static int __test__sw_clock_freq(enum perf_sw_ids clock_id)
  26. {
  27. int i, err = -1;
  28. volatile int tmp = 0;
  29. u64 total_periods = 0;
  30. int nr_samples = 0;
  31. char sbuf[STRERR_BUFSIZE];
  32. union perf_event *event;
  33. struct evsel *evsel;
  34. struct evlist *evlist;
  35. struct perf_event_attr attr = {
  36. .type = PERF_TYPE_SOFTWARE,
  37. .config = clock_id,
  38. .sample_type = PERF_SAMPLE_PERIOD,
  39. .exclude_kernel = 1,
  40. .disabled = 1,
  41. .freq = 1,
  42. };
  43. struct perf_cpu_map *cpus = NULL;
  44. struct perf_thread_map *threads = NULL;
  45. struct mmap *md;
  46. attr.sample_freq = 500;
  47. evlist = evlist__new();
  48. if (evlist == NULL) {
  49. pr_debug("evlist__new\n");
  50. return -1;
  51. }
  52. evsel = evsel__new(&attr);
  53. if (evsel == NULL) {
  54. pr_debug("evsel__new\n");
  55. goto out_delete_evlist;
  56. }
  57. evlist__add(evlist, evsel);
  58. cpus = perf_cpu_map__new_any_cpu();
  59. threads = thread_map__new_by_tid(getpid());
  60. if (!cpus || !threads) {
  61. err = -ENOMEM;
  62. pr_debug("Not enough memory to create thread/cpu maps\n");
  63. goto out_delete_evlist;
  64. }
  65. perf_evlist__set_maps(&evlist->core, cpus, threads);
  66. if (evlist__open(evlist)) {
  67. const char *knob = "/proc/sys/kernel/perf_event_max_sample_rate";
  68. err = -errno;
  69. pr_debug("Couldn't open evlist: %s\nHint: check %s, using %" PRIu64 " in this test.\n",
  70. str_error_r(errno, sbuf, sizeof(sbuf)),
  71. knob, (u64)attr.sample_freq);
  72. goto out_delete_evlist;
  73. }
  74. err = evlist__mmap(evlist, 128);
  75. if (err < 0) {
  76. pr_debug("failed to mmap event: %d (%s)\n", errno,
  77. str_error_r(errno, sbuf, sizeof(sbuf)));
  78. goto out_delete_evlist;
  79. }
  80. evlist__enable(evlist);
  81. /* collect samples */
  82. for (i = 0; i < NR_LOOPS; i++)
  83. tmp++;
  84. evlist__disable(evlist);
  85. md = &evlist->mmap[0];
  86. if (perf_mmap__read_init(&md->core) < 0)
  87. goto out_init;
  88. while ((event = perf_mmap__read_event(&md->core)) != NULL) {
  89. struct perf_sample sample;
  90. if (event->header.type != PERF_RECORD_SAMPLE)
  91. goto next_event;
  92. err = evlist__parse_sample(evlist, event, &sample);
  93. if (err < 0) {
  94. pr_debug("Error during parse sample\n");
  95. goto out_delete_evlist;
  96. }
  97. total_periods += sample.period;
  98. nr_samples++;
  99. next_event:
  100. perf_mmap__consume(&md->core);
  101. }
  102. perf_mmap__read_done(&md->core);
  103. out_init:
  104. if ((u64) nr_samples == total_periods) {
  105. pr_debug("All (%d) samples have period value of 1!\n",
  106. nr_samples);
  107. err = -1;
  108. }
  109. out_delete_evlist:
  110. perf_cpu_map__put(cpus);
  111. perf_thread_map__put(threads);
  112. evlist__delete(evlist);
  113. return err;
  114. }
  115. static int test__sw_clock_freq(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
  116. {
  117. int ret;
  118. ret = __test__sw_clock_freq(PERF_COUNT_SW_CPU_CLOCK);
  119. if (!ret)
  120. ret = __test__sw_clock_freq(PERF_COUNT_SW_TASK_CLOCK);
  121. return ret;
  122. }
  123. DEFINE_SUITE("Software clock events period values", sw_clock_freq);