sw-clock.c 2.9 KB

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