cache.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdint.h>
  3. #include "resctrl.h"
  4. char llc_occup_path[1024];
  5. void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config)
  6. {
  7. memset(pea, 0, sizeof(*pea));
  8. pea->type = PERF_TYPE_HARDWARE;
  9. pea->size = sizeof(*pea);
  10. pea->read_format = PERF_FORMAT_GROUP;
  11. pea->exclude_kernel = 1;
  12. pea->exclude_hv = 1;
  13. pea->exclude_idle = 1;
  14. pea->exclude_callchain_kernel = 1;
  15. pea->inherit = 1;
  16. pea->exclude_guest = 1;
  17. pea->disabled = 1;
  18. pea->config = config;
  19. }
  20. /* Start counters to log values */
  21. int perf_event_reset_enable(int pe_fd)
  22. {
  23. int ret;
  24. ret = ioctl(pe_fd, PERF_EVENT_IOC_RESET, 0);
  25. if (ret < 0)
  26. return ret;
  27. ret = ioctl(pe_fd, PERF_EVENT_IOC_ENABLE, 0);
  28. if (ret < 0)
  29. return ret;
  30. return 0;
  31. }
  32. void perf_event_initialize_read_format(struct perf_event_read *pe_read)
  33. {
  34. memset(pe_read, 0, sizeof(*pe_read));
  35. pe_read->nr = 1;
  36. }
  37. int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no)
  38. {
  39. int pe_fd;
  40. pe_fd = perf_event_open(pea, pid, cpu_no, -1, PERF_FLAG_FD_CLOEXEC);
  41. if (pe_fd == -1) {
  42. ksft_perror("Error opening leader");
  43. return -1;
  44. }
  45. perf_event_reset_enable(pe_fd);
  46. return pe_fd;
  47. }
  48. /*
  49. * Get LLC Occupancy as reported by RESCTRL FS
  50. * For CMT,
  51. * 1. If con_mon grp and mon grp given, then read from mon grp in
  52. * con_mon grp
  53. * 2. If only con_mon grp given, then read from con_mon grp
  54. * 3. If both not given, then read from root con_mon grp
  55. * For CAT,
  56. * 1. If con_mon grp given, then read from it
  57. * 2. If con_mon grp not given, then read from root con_mon grp
  58. *
  59. * Return: =0 on success. <0 on failure.
  60. */
  61. static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
  62. {
  63. FILE *fp;
  64. fp = fopen(llc_occup_path, "r");
  65. if (!fp) {
  66. ksft_perror("Failed to open results file");
  67. return -1;
  68. }
  69. if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
  70. ksft_perror("Could not get llc occupancy");
  71. fclose(fp);
  72. return -1;
  73. }
  74. fclose(fp);
  75. return 0;
  76. }
  77. /*
  78. * print_results_cache: the cache results are stored in a file
  79. * @filename: file that stores the results
  80. * @bm_pid: child pid that runs benchmark
  81. * @llc_value: perf miss value /
  82. * llc occupancy value reported by resctrl FS
  83. *
  84. * Return: 0 on success, < 0 on error.
  85. */
  86. static int print_results_cache(const char *filename, pid_t bm_pid, __u64 llc_value)
  87. {
  88. FILE *fp;
  89. if (strcmp(filename, "stdio") == 0 || strcmp(filename, "stderr") == 0) {
  90. printf("Pid: %d \t LLC_value: %llu\n", (int)bm_pid, llc_value);
  91. } else {
  92. fp = fopen(filename, "a");
  93. if (!fp) {
  94. ksft_perror("Cannot open results file");
  95. return -1;
  96. }
  97. fprintf(fp, "Pid: %d \t llc_value: %llu\n", (int)bm_pid, llc_value);
  98. fclose(fp);
  99. }
  100. return 0;
  101. }
  102. /*
  103. * perf_event_measure - Measure perf events
  104. * @filename: Filename for writing the results
  105. * @bm_pid: PID that runs the benchmark
  106. *
  107. * Measures perf events (e.g., cache misses) and writes the results into
  108. * @filename. @bm_pid is written to the results file along with the measured
  109. * value.
  110. *
  111. * Return: =0 on success. <0 on failure.
  112. */
  113. int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
  114. const char *filename, pid_t bm_pid)
  115. {
  116. int ret;
  117. /* Stop counters after one span to get miss rate */
  118. ret = ioctl(pe_fd, PERF_EVENT_IOC_DISABLE, 0);
  119. if (ret < 0)
  120. return ret;
  121. ret = read(pe_fd, pe_read, sizeof(*pe_read));
  122. if (ret == -1) {
  123. ksft_perror("Could not get perf value");
  124. return -1;
  125. }
  126. return print_results_cache(filename, bm_pid, pe_read->values[0].value);
  127. }
  128. /*
  129. * measure_llc_resctrl - Measure resctrl LLC value from resctrl
  130. * @filename: Filename for writing the results
  131. * @bm_pid: PID that runs the benchmark
  132. *
  133. * Measures LLC occupancy from resctrl and writes the results into @filename.
  134. * @bm_pid is written to the results file along with the measured value.
  135. *
  136. * Return: =0 on success. <0 on failure.
  137. */
  138. int measure_llc_resctrl(const char *filename, pid_t bm_pid)
  139. {
  140. unsigned long llc_occu_resc = 0;
  141. int ret;
  142. ret = get_llc_occu_resctrl(&llc_occu_resc);
  143. if (ret < 0)
  144. return ret;
  145. return print_results_cache(filename, bm_pid, llc_occu_resc);
  146. }
  147. /*
  148. * show_cache_info - Show generic cache test information
  149. * @no_of_bits: Number of bits
  150. * @avg_llc_val: Average of LLC cache result data
  151. * @cache_span: Cache span
  152. * @lines: @cache_span in lines or bytes
  153. */
  154. void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines)
  155. {
  156. ksft_print_msg("Number of bits: %d\n", no_of_bits);
  157. ksft_print_msg("Average LLC val: %llu\n", avg_llc_val);
  158. ksft_print_msg("Cache span (%s): %zu\n", lines ? "lines" : "bytes",
  159. cache_span);
  160. }