resctrl.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <ctype.h>
  15. #include <sys/stat.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/mount.h>
  18. #include <sys/types.h>
  19. #include <sys/wait.h>
  20. #include <sys/select.h>
  21. #include <sys/time.h>
  22. #include <sys/eventfd.h>
  23. #include <asm/unistd.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/compiler.h>
  26. #include <linux/bits.h>
  27. #include "kselftest.h"
  28. #define MB (1024 * 1024)
  29. #define RESCTRL_PATH "/sys/fs/resctrl"
  30. #define PHYS_ID_PATH "/sys/devices/system/cpu/cpu"
  31. #define INFO_PATH "/sys/fs/resctrl/info"
  32. /*
  33. * CPU vendor IDs
  34. *
  35. * Define as bits because they're used for vendor_specific bitmask in
  36. * the struct resctrl_test.
  37. */
  38. #define ARCH_INTEL BIT(0)
  39. #define ARCH_AMD BIT(1)
  40. #define ARCH_HYGON BIT(2)
  41. #define END_OF_TESTS 1
  42. #define BENCHMARK_ARGS 64
  43. #define MINIMUM_SPAN (250 * MB)
  44. /*
  45. * Memory bandwidth (in MiB) below which the bandwidth comparisons
  46. * between iMC and resctrl are considered unreliable. For example RAS
  47. * features or memory performance features that generate memory traffic
  48. * may drive accesses that are counted differently by performance counters
  49. * and MBM respectively, for instance generating "overhead" traffic which
  50. * is not counted against any specific RMID.
  51. */
  52. #define THROTTLE_THRESHOLD 750
  53. /*
  54. * fill_buf_param: "fill_buf" benchmark parameters
  55. * @buf_size: Size (in bytes) of buffer used in benchmark.
  56. * "fill_buf" allocates and initializes buffer of
  57. * @buf_size. User can change value via command line.
  58. * @memflush: If false the buffer will not be flushed after
  59. * allocation and initialization, otherwise the
  60. * buffer will be flushed. User can change value via
  61. * command line (via integers with 0 interpreted as
  62. * false and anything else as true).
  63. */
  64. struct fill_buf_param {
  65. size_t buf_size;
  66. bool memflush;
  67. };
  68. /*
  69. * user_params: User supplied parameters
  70. * @cpu: CPU number to which the benchmark will be bound to
  71. * @bits: Number of bits used for cache allocation size
  72. * @benchmark_cmd: Benchmark command to run during (some of the) tests
  73. * @fill_buf: Pointer to user provided parameters for "fill_buf",
  74. * NULL if user did not provide parameters and test
  75. * specific defaults should be used.
  76. */
  77. struct user_params {
  78. int cpu;
  79. int bits;
  80. const char *benchmark_cmd[BENCHMARK_ARGS];
  81. const struct fill_buf_param *fill_buf;
  82. };
  83. /*
  84. * resctrl_test: resctrl test definition
  85. * @name: Test name
  86. * @group: Test group - a common name for tests that share some characteristic
  87. * (e.g., L3 CAT test belongs to the CAT group). Can be NULL
  88. * @resource: Resource to test (e.g., MB, L3, L2, etc.)
  89. * @vendor_specific: Bitmask for vendor-specific tests (can be 0 for universal tests)
  90. * @disabled: Test is disabled
  91. * @feature_check: Callback to check required resctrl features
  92. * @run_test: Callback to run the test
  93. * @cleanup: Callback to cleanup after the test
  94. */
  95. struct resctrl_test {
  96. const char *name;
  97. const char *group;
  98. const char *resource;
  99. unsigned int vendor_specific;
  100. bool disabled;
  101. bool (*feature_check)(const struct resctrl_test *test);
  102. int (*run_test)(const struct resctrl_test *test,
  103. const struct user_params *uparams);
  104. void (*cleanup)(void);
  105. };
  106. /*
  107. * resctrl_val_param: resctrl test parameters
  108. * @ctrlgrp: Name of the control monitor group (con_mon grp)
  109. * @mongrp: Name of the monitor group (mon grp)
  110. * @filename: Name of file to which the o/p should be written
  111. * @init: Callback function to initialize test environment
  112. * @setup: Callback function to setup per test run environment
  113. * @measure: Callback that performs the measurement (a single test)
  114. * @fill_buf: Parameters for default "fill_buf" benchmark.
  115. * Initialized with user provided parameters, possibly
  116. * adapted to be relevant to the test. If user does
  117. * not provide parameters for "fill_buf" nor a
  118. * replacement benchmark then initialized with defaults
  119. * appropriate for test. NULL if user provided
  120. * benchmark.
  121. */
  122. struct resctrl_val_param {
  123. const char *ctrlgrp;
  124. const char *mongrp;
  125. char filename[64];
  126. unsigned long mask;
  127. int num_of_runs;
  128. int (*init)(const struct resctrl_val_param *param,
  129. int domain_id);
  130. int (*setup)(const struct resctrl_test *test,
  131. const struct user_params *uparams,
  132. struct resctrl_val_param *param);
  133. int (*measure)(const struct user_params *uparams,
  134. struct resctrl_val_param *param,
  135. pid_t bm_pid);
  136. struct fill_buf_param *fill_buf;
  137. };
  138. struct perf_event_read {
  139. __u64 nr; /* The number of events */
  140. struct {
  141. __u64 value; /* The value of the event */
  142. } values[2];
  143. };
  144. /*
  145. * Memory location that consumes values compiler must not optimize away.
  146. * Volatile ensures writes to this location cannot be optimized away by
  147. * compiler.
  148. */
  149. extern volatile int *value_sink;
  150. extern int snc_unreliable;
  151. extern char llc_occup_path[1024];
  152. int snc_nodes_per_l3_cache(void);
  153. unsigned int get_vendor(void);
  154. bool check_resctrlfs_support(void);
  155. int filter_dmesg(void);
  156. int get_domain_id(const char *resource, int cpu_no, int *domain_id);
  157. int mount_resctrlfs(void);
  158. int umount_resctrlfs(void);
  159. bool resctrl_resource_exists(const char *resource);
  160. bool resctrl_mon_feature_exists(const char *resource, const char *feature);
  161. bool resource_info_file_exists(const char *resource, const char *file);
  162. bool test_resource_feature_check(const struct resctrl_test *test);
  163. char *fgrep(FILE *inf, const char *str);
  164. int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity);
  165. int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity);
  166. int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
  167. const char *resource);
  168. int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp);
  169. int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
  170. int group_fd, unsigned long flags);
  171. unsigned char *alloc_buffer(size_t buf_size, bool memflush);
  172. void mem_flush(unsigned char *buf, size_t buf_size);
  173. void fill_cache_read(unsigned char *buf, size_t buf_size, bool once);
  174. ssize_t get_fill_buf_size(int cpu_no, const char *cache_type);
  175. int initialize_read_mem_bw_imc(void);
  176. int measure_read_mem_bw(const struct user_params *uparams,
  177. struct resctrl_val_param *param, pid_t bm_pid);
  178. void initialize_mem_bw_resctrl(const struct resctrl_val_param *param,
  179. int domain_id);
  180. int resctrl_val(const struct resctrl_test *test,
  181. const struct user_params *uparams,
  182. struct resctrl_val_param *param);
  183. unsigned long create_bit_mask(unsigned int start, unsigned int len);
  184. unsigned int count_contiguous_bits(unsigned long val, unsigned int *start);
  185. int get_full_cbm(const char *cache_type, unsigned long *mask);
  186. int get_mask_no_shareable(const char *cache_type, unsigned long *mask);
  187. int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size);
  188. int resource_info_unsigned_get(const char *resource, const char *filename, unsigned int *val);
  189. void ctrlc_handler(int signum, siginfo_t *info, void *ptr);
  190. int signal_handler_register(const struct resctrl_test *test);
  191. void signal_handler_unregister(void);
  192. unsigned int count_bits(unsigned long n);
  193. int snc_kernel_support(void);
  194. void perf_event_attr_initialize(struct perf_event_attr *pea, __u64 config);
  195. void perf_event_initialize_read_format(struct perf_event_read *pe_read);
  196. int perf_open(struct perf_event_attr *pea, pid_t pid, int cpu_no);
  197. int perf_event_reset_enable(int pe_fd);
  198. int perf_event_measure(int pe_fd, struct perf_event_read *pe_read,
  199. const char *filename, pid_t bm_pid);
  200. int measure_llc_resctrl(const char *filename, pid_t bm_pid);
  201. void show_cache_info(int no_of_bits, __u64 avg_llc_val, size_t cache_span, bool lines);
  202. /*
  203. * cache_portion_size - Calculate the size of a cache portion
  204. * @cache_size: Total cache size in bytes
  205. * @portion_mask: Cache portion mask
  206. * @full_cache_mask: Full Cache Bit Mask (CBM) for the cache
  207. *
  208. * Return: The size of the cache portion in bytes.
  209. */
  210. static inline unsigned long cache_portion_size(unsigned long cache_size,
  211. unsigned long portion_mask,
  212. unsigned long full_cache_mask)
  213. {
  214. unsigned int bits = count_bits(full_cache_mask);
  215. /*
  216. * With no bits the full CBM, assume cache cannot be split into
  217. * smaller portions. To avoid divide by zero, return cache_size.
  218. */
  219. if (!bits)
  220. return cache_size;
  221. return cache_size * count_bits(portion_mask) / bits;
  222. }
  223. extern struct resctrl_test mbm_test;
  224. extern struct resctrl_test mba_test;
  225. extern struct resctrl_test cmt_test;
  226. extern struct resctrl_test l3_cat_test;
  227. extern struct resctrl_test l3_noncont_cat_test;
  228. extern struct resctrl_test l2_noncont_cat_test;
  229. #endif /* RESCTRL_H */