libperf-counting.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. libperf-counting(7)
  2. ===================
  3. NAME
  4. ----
  5. libperf-counting - counting interface
  6. DESCRIPTION
  7. -----------
  8. The counting interface provides API to measure and get count for specific perf events.
  9. The following test tries to explain count on `counting.c` example.
  10. It is by no means complete guide to counting, but shows libperf basic API for counting.
  11. The `counting.c` comes with libperf package and can be compiled and run like:
  12. [source,bash]
  13. --
  14. $ gcc -o counting counting.c -lperf
  15. $ sudo ./counting
  16. count 176792, enabled 176944, run 176944
  17. count 176242, enabled 176242, run 176242
  18. --
  19. It requires root access, because of the `PERF_COUNT_SW_CPU_CLOCK` event,
  20. which is available only for root.
  21. The `counting.c` example monitors two events on the current process and displays
  22. their count, in a nutshell it:
  23. * creates events
  24. * adds them to the event list
  25. * opens and enables events through the event list
  26. * does some workload
  27. * disables events
  28. * reads and displays event counts
  29. * destroys the event list
  30. The first thing you need to do before using libperf is to call init function:
  31. [source,c]
  32. --
  33. 8 static int libperf_print(enum libperf_print_level level,
  34. 9 const char *fmt, va_list ap)
  35. 10 {
  36. 11 return vfprintf(stderr, fmt, ap);
  37. 12 }
  38. 14 int main(int argc, char **argv)
  39. 15 {
  40. ...
  41. 35 libperf_init(libperf_print);
  42. --
  43. It will setup the library and sets function for debug output from library.
  44. The `libperf_print` callback will receive any message with its debug level,
  45. defined as:
  46. [source,c]
  47. --
  48. enum libperf_print_level {
  49. LIBPERF_ERR,
  50. LIBPERF_WARN,
  51. LIBPERF_INFO,
  52. LIBPERF_DEBUG,
  53. LIBPERF_DEBUG2,
  54. LIBPERF_DEBUG3,
  55. };
  56. --
  57. Once the setup is complete we start by defining specific events using the `struct perf_event_attr`.
  58. We create software events for cpu and task:
  59. [source,c]
  60. --
  61. 20 struct perf_event_attr attr1 = {
  62. 21 .type = PERF_TYPE_SOFTWARE,
  63. 22 .config = PERF_COUNT_SW_CPU_CLOCK,
  64. 23 .read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING,
  65. 24 .disabled = 1,
  66. 25 };
  67. 26 struct perf_event_attr attr2 = {
  68. 27 .type = PERF_TYPE_SOFTWARE,
  69. 28 .config = PERF_COUNT_SW_TASK_CLOCK,
  70. 29 .read_format = PERF_FORMAT_TOTAL_TIME_ENABLED|PERF_FORMAT_TOTAL_TIME_RUNNING,
  71. 30 .disabled = 1,
  72. 31 };
  73. --
  74. The `read_format` setup tells perf to include timing details together with each count.
  75. Next step is to prepare threads map.
  76. In this case we will monitor current process, so we create threads map with single pid (0):
  77. [source,c]
  78. --
  79. 37 threads = perf_thread_map__new_dummy();
  80. 38 if (!threads) {
  81. 39 fprintf(stderr, "failed to create threads\n");
  82. 40 return -1;
  83. 41 }
  84. 42
  85. 43 perf_thread_map__set_pid(threads, 0, 0);
  86. --
  87. Now we create libperf's event list, which will serve as holder for the events we want:
  88. [source,c]
  89. --
  90. 45 evlist = perf_evlist__new();
  91. 46 if (!evlist) {
  92. 47 fprintf(stderr, "failed to create evlist\n");
  93. 48 goto out_threads;
  94. 49 }
  95. --
  96. We create libperf's events for the attributes we defined earlier and add them to the list:
  97. [source,c]
  98. --
  99. 51 evsel = perf_evsel__new(&attr1);
  100. 52 if (!evsel) {
  101. 53 fprintf(stderr, "failed to create evsel1\n");
  102. 54 goto out_evlist;
  103. 55 }
  104. 56
  105. 57 perf_evlist__add(evlist, evsel);
  106. 58
  107. 59 evsel = perf_evsel__new(&attr2);
  108. 60 if (!evsel) {
  109. 61 fprintf(stderr, "failed to create evsel2\n");
  110. 62 goto out_evlist;
  111. 63 }
  112. 64
  113. 65 perf_evlist__add(evlist, evsel);
  114. --
  115. Configure event list with the thread map and open events:
  116. [source,c]
  117. --
  118. 67 perf_evlist__set_maps(evlist, NULL, threads);
  119. 68
  120. 69 err = perf_evlist__open(evlist);
  121. 70 if (err) {
  122. 71 fprintf(stderr, "failed to open evsel\n");
  123. 72 goto out_evlist;
  124. 73 }
  125. --
  126. Both events are created as disabled (note the `disabled = 1` assignment above),
  127. so we need to enable the whole list explicitly (both events).
  128. From this moment events are counting and we can do our workload.
  129. When we are done we disable the events list.
  130. [source,c]
  131. --
  132. 75 perf_evlist__enable(evlist);
  133. 76
  134. 77 while (count--);
  135. 78
  136. 79 perf_evlist__disable(evlist);
  137. --
  138. Now we need to get the counts from events, following code iterates through the
  139. events list and read counts:
  140. [source,c]
  141. --
  142. 81 perf_evlist__for_each_evsel(evlist, evsel) {
  143. 82 perf_evsel__read(evsel, 0, 0, &counts);
  144. 83 fprintf(stdout, "count %llu, enabled %llu, run %llu\n",
  145. 84 counts.val, counts.ena, counts.run);
  146. 85 }
  147. --
  148. And finally cleanup.
  149. We close the whole events list (both events) and remove it together with the threads map:
  150. [source,c]
  151. --
  152. 87 perf_evlist__close(evlist);
  153. 88
  154. 89 out_evlist:
  155. 90 perf_evlist__delete(evlist);
  156. 91 out_threads:
  157. 92 perf_thread_map__put(threads);
  158. 93 return err;
  159. 94 }
  160. --
  161. REPORTING BUGS
  162. --------------
  163. Report bugs to <[email protected]>.
  164. LICENSE
  165. -------
  166. libperf is Free Software licensed under the GNU LGPL 2.1
  167. RESOURCES
  168. ---------
  169. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
  170. SEE ALSO
  171. --------
  172. libperf(3), libperf-sampling(7)