sw-clock.c 2.9 KB

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