resctrl.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef RESCTRL_H
  3. #define RESCTRL_H
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <errno.h>
  7. #include <sched.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <dirent.h>
  13. #include <stdbool.h>
  14. #include <sys/stat.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/mount.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <sys/select.h>
  20. #include <sys/time.h>
  21. #include <sys/eventfd.h>
  22. #include <asm/unistd.h>
  23. #include <linux/perf_event.h>
  24. #include "../kselftest.h"
  25. #define MB (1024 * 1024)
  26. #define RESCTRL_PATH "/sys/fs/resctrl"
  27. #define PHYS_ID_PATH "/sys/devices/system/cpu/cpu"
  28. #define INFO_PATH "/sys/fs/resctrl/info"
  29. /*
  30. * CPU vendor IDs
  31. *
  32. * Define as bits because they're used for vendor_specific bitmask in
  33. * the struct resctrl_test.
  34. */
  35. #define ARCH_INTEL 1
  36. #define ARCH_AMD 2
  37. #define END_OF_TESTS 1
  38. #define BENCHMARK_ARGS 64
  39. #define DEFAULT_SPAN (250 * MB)
  40. /*
  41. * user_params: User supplied parameters
  42. * @cpu: CPU number to which the benchmark will be bound to
  43. * @bits: Number of bits used for cache allocation size
  44. * @benchmark_cmd: Benchmark command to run during (some of the) tests
  45. */
  46. struct user_params {
  47. int cpu;
  48. int bits;
  49. const char *benchmark_cmd[BENCHMARK_ARGS];
  50. };
  51. /*
  52. * resctrl_test: resctrl test definition
  53. * @name: Test name
  54. * @group: Test group - a common name for tests that share some characteristic
  55. * (e.g., L3 CAT test belongs to the CAT group). Can be NULL
  56. * @resource: Resource to test (e.g., MB, L3, L2, etc.)
  57. * @vendor_specific: Bitmask for vendor-specific tests (can be 0 for universal tests)
  58. * @disabled: Test is disabled
  59. * @feature_check: Callback to check required resctrl features
  60. * @run_test: Callback to run the test
  61. * @cleanup: Callback to cleanup after the test
  62. */
  63. struct resctrl_test {
  64. const char *name;
  65. const char *group;
  66. const char *resource;
  67. unsigned int vendor_specific;
  68. bool disabled;
  69. bool (*feature_check)(const struct resctrl_test *test);
  70. int (*run_test)(const struct resctrl_test *test,
  71. const struct user_params *uparams);
  72. void (*cleanup)(void);
  73. };
  74. /*
  75. * resctrl_val_param: resctrl test parameters
  76. * @ctrlgrp: Name of the control monitor group (con_mon grp)
  77. * @mongrp: Name of the monitor group (mon grp)
  78. * @filename: Name of file to which the o/p should be written
  79. * @init: Callback function to initialize test environment
  80. * @setup: Callback function to setup per test run environment
  81. * @measure: Callback that performs the measurement (a single test)
  82. */
  83. struct resctrl_val_param {
  84. const char *ctrlgrp;
  85. const char *mongrp;
  86. char filename[64];
  87. unsigned long mask;
  88. int num_of_runs;
  89. int (*init)(const struct resctrl_val_param *param,
  90. int domain_id);
  91. int (*setup)(const struct resctrl_test *test,
  92. const struct user_params *uparams,
  93. struct resctrl_val_param *param);
  94. int (*measure)(const struct user_params *uparams,
  95. struct resctrl_val_param *param,
  96. pid_t bm_pid);
  97. };
  98. struct perf_event_read {
  99. __u64 nr; /* The number of events */
  100. struct {
  101. __u64 value; /* The value of the event */
  102. } values[2];
  103. };
  104. /*
  105. * Memory location that consumes values compiler must not optimize away.
  106. * Volatile ensures writes to this location cannot be optimized away by
  107. * compiler.
  108. */
  109. extern volatile int *value_sink;
  110. extern char llc_occup_path[1024];
  111. int get_vendor(void);
  112. bool check_resctrlfs_support(void);
  113. int filter_dmesg(void);
  114. int get_domain_id(const char *resource, int cpu_no, int *domain_id);
  115. int mount_resctrlfs(void);
  116. int umount_resctrlfs(void);
  117. const char *get_bw_report_type(const char *bw_report);
  118. bool resctrl_resource_exists(const char *resource);
  119. bool resctrl_mon_feature_exists(const char *resource, const char *feature);
  120. bool resource_info_file_exists(const char *resource, const char *file);
  121. bool test_resource_feature_check(const struct resctrl_test *test);
  122. char *fgrep(FILE *inf, const char *str);
  123. int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity);
  124. int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity);
  125. int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
  126. const char *resource);
  127. int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp);
  128. int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
  129. int group_fd, unsigned long flags);
  130. unsigned char *alloc_buffer(size_t buf_size, int memflush);
  131. void mem_flush(unsigned char *buf, size_t buf_size);
  132. void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
  133. int run_fill_buf(size_t buf_size, int memflush, int op, bool once);
  134. int initialize_mem_bw_imc(void);
  135. int measure_mem_bw(const struct user_params *uparams,
  136. struct resctrl_val_param *param, pid_t bm_pid,
  137. const char *bw_report);
  138. void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
  139. int domain_id);
  140. int resctrl_val(const struct resctrl_test *test,
  141. const struct user_params *uparams,
  142. const char * const *benchmark_cmd,
  143. struct resctrl_val_param *param);
  144. unsigned long create_bit_mask(unsigned int start, unsigned int len);
  145. unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
  146. int get_full_cbm(const char *cache_type, unsigned long *mask);
  147. int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
  148. int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
  149. int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val);
  150. void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
  151. int signal_handler_register(const struct resctrl_test *test);
  152. void signal_handler_unregister(void);
  153. unsigned int count_bits(unsigned long n);
  154. void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
  155. void perf_event_initialize_read_format(struct perf_event_read *pe_read);
  156. int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
  157. int perf_event_reset_enable(int pe_fd);
  158. int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
  159. const char *filename, pid_t bm_pid);
  160. int measure_llc_resctrl(const char *filename, pid_t bm_pid);
  161. void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
  162. /*
  163. * cache_portion_size - Calculate the size of a cache portion
  164. * @cache_size: Total cache size in bytes
  165. * @portion_mask: Cache portion mask
  166. * @full_cache_mask: Full Cache Bit Mask (CBM) for the cache
  167. *
  168. * Return: The size of the cache portion in bytes.
  169. */
  170. static inline unsigned long cache_portion_size(unsigned long cache_size,
  171. unsigned long portion_mask,
  172. unsigned long full_cache_mask)
  173. {
  174. unsigned int bits = count_bits(full_cache_mask);
  175. /*
  176. * With no bits the full CBM, assume cache cannot be split into
  177. * smaller portions. To avoid divide by zero, return cache_size.
  178. */
  179. if (!bits)
  180. return cache_size;
  181. return cache_size * count_bits(portion_mask) / bits;
  182. }
  183. extern struct resctrl_test mbm_test;
  184. extern struct resctrl_test mba_test;
  185. extern struct resctrl_test cmt_test;
  186. extern struct resctrl_test l3_cat_test;
  187. extern struct resctrl_test l3_noncont_cat_test;
  188. extern struct resctrl_test l2_noncont_cat_test;
  189. #endif /* RESCTRL_H */