sw-clock.c 3.0 KB

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