resctrlfs.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Basic resctrl file system operations
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. *
  7. * Authors:
  8. * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
  9. * Fenghua Yu <fenghua.yu@intel.com>
  10. */
  11. #include <fcntl.h>
  12. #include <limits.h>
  13. #include "resctrl.h"
  14. int snc_unreliable;
  15. static int find_resctrl_mount(char *buffer)
  16. {
  17. FILE *mounts;
  18. char line[256], *fs, *mntpoint;
  19. mounts = fopen("/proc/mounts", "r");
  20. if (!mounts) {
  21. ksft_perror("/proc/mounts");
  22. return -ENXIO;
  23. }
  24. while (!feof(mounts)) {
  25. if (!fgets(line, 256, mounts))
  26. break;
  27. fs = strtok(line, " \t");
  28. if (!fs)
  29. continue;
  30. mntpoint = strtok(NULL, " \t");
  31. if (!mntpoint)
  32. continue;
  33. fs = strtok(NULL, " \t");
  34. if (!fs)
  35. continue;
  36. if (strcmp(fs, "resctrl"))
  37. continue;
  38. fclose(mounts);
  39. if (buffer)
  40. strncpy(buffer, mntpoint, 256);
  41. return 0;
  42. }
  43. fclose(mounts);
  44. return -ENOENT;
  45. }
  46. /*
  47. * mount_resctrlfs - Mount resctrl FS at /sys/fs/resctrl
  48. *
  49. * Mounts resctrl FS. Fails if resctrl FS is already mounted to avoid
  50. * pre-existing settings interfering with the test results.
  51. *
  52. * Return: 0 on success, < 0 on error.
  53. */
  54. int mount_resctrlfs(void)
  55. {
  56. int ret;
  57. ret = find_resctrl_mount(NULL);
  58. if (ret != -ENOENT)
  59. return -1;
  60. ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH);
  61. ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
  62. if (ret)
  63. ksft_perror("mount");
  64. return ret;
  65. }
  66. int umount_resctrlfs(void)
  67. {
  68. char mountpoint[256];
  69. int ret;
  70. ret = find_resctrl_mount(mountpoint);
  71. if (ret == -ENOENT)
  72. return 0;
  73. if (ret)
  74. return ret;
  75. if (umount(mountpoint)) {
  76. ksft_perror("Unable to umount resctrl");
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. /*
  82. * get_cache_level - Convert cache level from string to integer
  83. * @cache_type: Cache level as string
  84. *
  85. * Return: cache level as integer or -1 if @cache_type is invalid.
  86. */
  87. static int get_cache_level(const char *cache_type)
  88. {
  89. if (!strcmp(cache_type, "L3"))
  90. return 3;
  91. if (!strcmp(cache_type, "L2"))
  92. return 2;
  93. ksft_print_msg("Invalid cache level\n");
  94. return -1;
  95. }
  96. static int get_resource_cache_level(const char *resource)
  97. {
  98. /* "MB" use L3 (LLC) as resource */
  99. if (!strcmp(resource, "MB"))
  100. return 3;
  101. return get_cache_level(resource);
  102. }
  103. /*
  104. * get_domain_id - Get resctrl domain ID for a specified CPU
  105. * @resource: resource name
  106. * @cpu_no: CPU number
  107. * @domain_id: domain ID (cache ID; for MB, L3 cache ID)
  108. *
  109. * Return: >= 0 on success, < 0 on failure.
  110. */
  111. int get_domain_id(const char *resource, int cpu_no, int *domain_id)
  112. {
  113. char phys_pkg_path[1024];
  114. int cache_num;
  115. FILE *fp;
  116. cache_num = get_resource_cache_level(resource);
  117. if (cache_num < 0)
  118. return cache_num;
  119. sprintf(phys_pkg_path, "%s%d/cache/index%d/id", PHYS_ID_PATH, cpu_no, cache_num);
  120. fp = fopen(phys_pkg_path, "r");
  121. if (!fp) {
  122. ksft_perror("Failed to open cache id file");
  123. return -1;
  124. }
  125. if (fscanf(fp, "%d", domain_id) <= 0) {
  126. ksft_perror("Could not get domain ID");
  127. fclose(fp);
  128. return -1;
  129. }
  130. fclose(fp);
  131. return 0;
  132. }
  133. /*
  134. * Count number of CPUs in a /sys bitmap
  135. */
  136. static unsigned int count_sys_bitmap_bits(char *name)
  137. {
  138. FILE *fp = fopen(name, "r");
  139. int count = 0, c;
  140. if (!fp)
  141. return 0;
  142. while ((c = fgetc(fp)) != EOF) {
  143. if (!isxdigit(c))
  144. continue;
  145. switch (c) {
  146. case 'f':
  147. count++;
  148. fallthrough;
  149. case '7': case 'b': case 'd': case 'e':
  150. count++;
  151. fallthrough;
  152. case '3': case '5': case '6': case '9': case 'a': case 'c':
  153. count++;
  154. fallthrough;
  155. case '1': case '2': case '4': case '8':
  156. count++;
  157. break;
  158. }
  159. }
  160. fclose(fp);
  161. return count;
  162. }
  163. static bool cpus_offline_empty(void)
  164. {
  165. char offline_cpus_str[64];
  166. FILE *fp;
  167. fp = fopen("/sys/devices/system/cpu/offline", "r");
  168. if (!fp) {
  169. ksft_perror("Could not open /sys/devices/system/cpu/offline");
  170. return 0;
  171. }
  172. if (fscanf(fp, "%63s", offline_cpus_str) < 0) {
  173. if (!errno) {
  174. fclose(fp);
  175. return 1;
  176. }
  177. ksft_perror("Could not read /sys/devices/system/cpu/offline");
  178. }
  179. fclose(fp);
  180. return 0;
  181. }
  182. /*
  183. * Detect SNC by comparing #CPUs in node0 with #CPUs sharing LLC with CPU0.
  184. * If any CPUs are offline declare the detection as unreliable.
  185. */
  186. int snc_nodes_per_l3_cache(void)
  187. {
  188. int node_cpus, cache_cpus;
  189. static int snc_mode;
  190. if (!snc_mode) {
  191. snc_mode = 1;
  192. if (!cpus_offline_empty()) {
  193. ksft_print_msg("Runtime SNC detection unreliable due to offline CPUs.\n");
  194. ksft_print_msg("Setting SNC mode to disabled.\n");
  195. snc_unreliable = 1;
  196. return snc_mode;
  197. }
  198. node_cpus = count_sys_bitmap_bits("/sys/devices/system/node/node0/cpumap");
  199. cache_cpus = count_sys_bitmap_bits("/sys/devices/system/cpu/cpu0/cache/index3/shared_cpu_map");
  200. if (!node_cpus || !cache_cpus) {
  201. ksft_print_msg("Could not determine Sub-NUMA Cluster mode.\n");
  202. snc_unreliable = 1;
  203. return snc_mode;
  204. }
  205. snc_mode = cache_cpus / node_cpus;
  206. /*
  207. * On some platforms (e.g. Hygon),
  208. * cache_cpus < node_cpus, the calculated snc_mode is 0.
  209. *
  210. * Set snc_mode = 1 to indicate that SNC mode is not
  211. * supported on the platform.
  212. */
  213. if (!snc_mode)
  214. snc_mode = 1;
  215. if (snc_mode > 1)
  216. ksft_print_msg("SNC-%d mode discovered.\n", snc_mode);
  217. }
  218. return snc_mode;
  219. }
  220. /*
  221. * get_cache_size - Get cache size for a specified CPU
  222. * @cpu_no: CPU number
  223. * @cache_type: Cache level L2/L3
  224. * @cache_size: pointer to cache_size
  225. *
  226. * Return: = 0 on success, < 0 on failure.
  227. */
  228. int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size)
  229. {
  230. char cache_path[1024], cache_str[64];
  231. int length, i, cache_num;
  232. FILE *fp;
  233. cache_num = get_cache_level(cache_type);
  234. if (cache_num < 0)
  235. return cache_num;
  236. sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
  237. cpu_no, cache_num);
  238. fp = fopen(cache_path, "r");
  239. if (!fp) {
  240. ksft_perror("Failed to open cache size");
  241. return -1;
  242. }
  243. if (fscanf(fp, "%63s", cache_str) <= 0) {
  244. ksft_perror("Could not get cache_size");
  245. fclose(fp);
  246. return -1;
  247. }
  248. fclose(fp);
  249. length = (int)strlen(cache_str);
  250. *cache_size = 0;
  251. for (i = 0; i < length; i++) {
  252. if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
  253. *cache_size = *cache_size * 10 + (cache_str[i] - '0');
  254. else if (cache_str[i] == 'K')
  255. *cache_size = *cache_size * 1024;
  256. else if (cache_str[i] == 'M')
  257. *cache_size = *cache_size * 1024 * 1024;
  258. else
  259. break;
  260. }
  261. /*
  262. * The amount of cache represented by each bit in the masks
  263. * in the schemata file is reduced by a factor equal to SNC
  264. * nodes per L3 cache.
  265. * E.g. on a SNC-2 system with a 100MB L3 cache a test that
  266. * allocates memory from its local SNC node (default behavior
  267. * without using libnuma) will only see 50 MB llc_occupancy
  268. * with a fully populated L3 mask in the schemata file.
  269. */
  270. if (cache_num == 3)
  271. *cache_size /= snc_nodes_per_l3_cache();
  272. return 0;
  273. }
  274. #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
  275. /*
  276. * get_bit_mask - Get bit mask from given file
  277. * @filename: File containing the mask
  278. * @mask: The bit mask returned as unsigned long
  279. *
  280. * Return: = 0 on success, < 0 on failure.
  281. */
  282. static int get_bit_mask(const char *filename, unsigned long *mask)
  283. {
  284. FILE *fp;
  285. if (!filename || !mask)
  286. return -1;
  287. fp = fopen(filename, "r");
  288. if (!fp) {
  289. ksft_print_msg("Failed to open bit mask file '%s': %s\n",
  290. filename, strerror(errno));
  291. return -1;
  292. }
  293. if (fscanf(fp, "%lx", mask) <= 0) {
  294. ksft_print_msg("Could not read bit mask file '%s': %s\n",
  295. filename, strerror(errno));
  296. fclose(fp);
  297. return -1;
  298. }
  299. fclose(fp);
  300. return 0;
  301. }
  302. /*
  303. * resource_info_unsigned_get - Read an unsigned value from
  304. * /sys/fs/resctrl/info/@resource/@filename
  305. * @resource: Resource name that matches directory name in
  306. * /sys/fs/resctrl/info
  307. * @filename: File in /sys/fs/resctrl/info/@resource
  308. * @val: Contains read value on success.
  309. *
  310. * Return: = 0 on success, < 0 on failure. On success the read
  311. * value is saved into @val.
  312. */
  313. int resource_info_unsigned_get(const char *resource, const char *filename,
  314. unsigned int *val)
  315. {
  316. char file_path[PATH_MAX];
  317. FILE *fp;
  318. snprintf(file_path, sizeof(file_path), "%s/%s/%s", INFO_PATH, resource,
  319. filename);
  320. fp = fopen(file_path, "r");
  321. if (!fp) {
  322. ksft_print_msg("Error opening %s: %m\n", file_path);
  323. return -1;
  324. }
  325. if (fscanf(fp, "%u", val) <= 0) {
  326. ksft_print_msg("Could not get contents of %s: %m\n", file_path);
  327. fclose(fp);
  328. return -1;
  329. }
  330. fclose(fp);
  331. return 0;
  332. }
  333. /*
  334. * create_bit_mask- Create bit mask from start, len pair
  335. * @start: LSB of the mask
  336. * @len Number of bits in the mask
  337. */
  338. unsigned long create_bit_mask(unsigned int start, unsigned int len)
  339. {
  340. return ((1UL << len) - 1UL) << start;
  341. }
  342. /*
  343. * count_contiguous_bits - Returns the longest train of bits in a bit mask
  344. * @val A bit mask
  345. * @start The location of the least-significant bit of the longest train
  346. *
  347. * Return: The length of the contiguous bits in the longest train of bits
  348. */
  349. unsigned int count_contiguous_bits(unsigned long val, unsigned int *start)
  350. {
  351. unsigned long last_val;
  352. unsigned int count = 0;
  353. while (val) {
  354. last_val = val;
  355. val &= (val >> 1);
  356. count++;
  357. }
  358. if (start) {
  359. if (count)
  360. *start = ffsl(last_val) - 1;
  361. else
  362. *start = 0;
  363. }
  364. return count;
  365. }
  366. /*
  367. * get_full_cbm - Get full Cache Bit Mask (CBM)
  368. * @cache_type: Cache type as "L2" or "L3"
  369. * @mask: Full cache bit mask representing the maximal portion of cache
  370. * available for allocation, returned as unsigned long.
  371. *
  372. * Return: = 0 on success, < 0 on failure.
  373. */
  374. int get_full_cbm(const char *cache_type, unsigned long *mask)
  375. {
  376. char cbm_path[PATH_MAX];
  377. int ret;
  378. if (!cache_type)
  379. return -1;
  380. snprintf(cbm_path, sizeof(cbm_path), "%s/%s/cbm_mask",
  381. INFO_PATH, cache_type);
  382. ret = get_bit_mask(cbm_path, mask);
  383. if (ret || !*mask)
  384. return -1;
  385. return 0;
  386. }
  387. /*
  388. * get_shareable_mask - Get shareable mask from shareable_bits
  389. * @cache_type: Cache type as "L2" or "L3"
  390. * @shareable_mask: Shareable mask returned as unsigned long
  391. *
  392. * Return: = 0 on success, < 0 on failure.
  393. */
  394. static int get_shareable_mask(const char *cache_type, unsigned long *shareable_mask)
  395. {
  396. char mask_path[PATH_MAX];
  397. if (!cache_type)
  398. return -1;
  399. snprintf(mask_path, sizeof(mask_path), "%s/%s/shareable_bits",
  400. INFO_PATH, cache_type);
  401. return get_bit_mask(mask_path, shareable_mask);
  402. }
  403. /*
  404. * get_mask_no_shareable - Get Cache Bit Mask (CBM) without shareable bits
  405. * @cache_type: Cache type as "L2" or "L3"
  406. * @mask: The largest exclusive portion of the cache out of the
  407. * full CBM, returned as unsigned long
  408. *
  409. * Parts of a cache may be shared with other devices such as GPU. This function
  410. * calculates the largest exclusive portion of the cache where no other devices
  411. * besides CPU have access to the cache portion.
  412. *
  413. * Return: = 0 on success, < 0 on failure.
  414. */
  415. int get_mask_no_shareable(const char *cache_type, unsigned long *mask)
  416. {
  417. unsigned long full_mask, shareable_mask;
  418. unsigned int start, len;
  419. if (get_full_cbm(cache_type, &full_mask) < 0)
  420. return -1;
  421. if (get_shareable_mask(cache_type, &shareable_mask) < 0)
  422. return -1;
  423. len = count_contiguous_bits(full_mask & ~shareable_mask, &start);
  424. if (!len)
  425. return -1;
  426. *mask = create_bit_mask(start, len);
  427. return 0;
  428. }
  429. /*
  430. * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
  431. * @bm_pid: PID that should be binded
  432. * @cpu_no: CPU number at which the PID would be binded
  433. * @old_affinity: When not NULL, set to old CPU affinity
  434. *
  435. * Return: 0 on success, < 0 on error.
  436. */
  437. int taskset_benchmark(pid_t bm_pid, int cpu_no, cpu_set_t *old_affinity)
  438. {
  439. cpu_set_t my_set;
  440. if (old_affinity) {
  441. CPU_ZERO(old_affinity);
  442. if (sched_getaffinity(bm_pid, sizeof(*old_affinity),
  443. old_affinity)) {
  444. ksft_perror("Unable to read CPU affinity");
  445. return -1;
  446. }
  447. }
  448. CPU_ZERO(&my_set);
  449. CPU_SET(cpu_no, &my_set);
  450. if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
  451. ksft_perror("Unable to taskset benchmark");
  452. return -1;
  453. }
  454. return 0;
  455. }
  456. /*
  457. * taskset_restore - Taskset PID to the earlier CPU affinity
  458. * @bm_pid: PID that should be reset
  459. * @old_affinity: The old CPU affinity to restore
  460. *
  461. * Return: 0 on success, < 0 on error.
  462. */
  463. int taskset_restore(pid_t bm_pid, cpu_set_t *old_affinity)
  464. {
  465. if (sched_setaffinity(bm_pid, sizeof(*old_affinity), old_affinity)) {
  466. ksft_perror("Unable to restore CPU affinity");
  467. return -1;
  468. }
  469. return 0;
  470. }
  471. /*
  472. * create_grp - Create a group only if one doesn't exist
  473. * @grp_name: Name of the group
  474. * @grp: Full path and name of the group
  475. * @parent_grp: Full path and name of the parent group
  476. *
  477. * Creates a group @grp_name if it does not exist yet. If @grp_name is NULL,
  478. * it is interpreted as the root group which always results in success.
  479. *
  480. * Return: 0 on success, < 0 on error.
  481. */
  482. static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
  483. {
  484. int found_grp = 0;
  485. struct dirent *ep;
  486. DIR *dp;
  487. if (!grp_name)
  488. return 0;
  489. /* Check if requested grp exists or not */
  490. dp = opendir(parent_grp);
  491. if (dp) {
  492. while ((ep = readdir(dp)) != NULL) {
  493. if (strcmp(ep->d_name, grp_name) == 0)
  494. found_grp = 1;
  495. }
  496. closedir(dp);
  497. } else {
  498. ksft_perror("Unable to open resctrl for group");
  499. return -1;
  500. }
  501. /* Requested grp doesn't exist, hence create it */
  502. if (found_grp == 0) {
  503. if (mkdir(grp, 0) == -1) {
  504. ksft_perror("Unable to create group");
  505. return -1;
  506. }
  507. }
  508. return 0;
  509. }
  510. static int write_pid_to_tasks(char *tasks, pid_t pid)
  511. {
  512. FILE *fp;
  513. fp = fopen(tasks, "w");
  514. if (!fp) {
  515. ksft_perror("Failed to open tasks file");
  516. return -1;
  517. }
  518. if (fprintf(fp, "%d\n", (int)pid) < 0) {
  519. ksft_print_msg("Failed to write pid to tasks file\n");
  520. fclose(fp);
  521. return -1;
  522. }
  523. fclose(fp);
  524. return 0;
  525. }
  526. /*
  527. * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
  528. * @bm_pid: PID that should be written
  529. * @ctrlgrp: Name of the control monitor group (con_mon grp)
  530. * @mongrp: Name of the monitor group (mon grp)
  531. *
  532. * If a con_mon grp is requested, create it and write pid to it, otherwise
  533. * write pid to root con_mon grp.
  534. * If a mon grp is requested, create it and write pid to it, otherwise
  535. * pid is not written, this means that pid is in con_mon grp and hence
  536. * should consult con_mon grp's mon_data directory for results.
  537. *
  538. * Return: 0 on success, < 0 on error.
  539. */
  540. int write_bm_pid_to_resctrl(pid_t bm_pid, const char *ctrlgrp, const char *mongrp)
  541. {
  542. char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
  543. char tasks[1024];
  544. int ret = 0;
  545. if (ctrlgrp)
  546. sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
  547. else
  548. sprintf(controlgroup, "%s", RESCTRL_PATH);
  549. /* Create control and monitoring group and write pid into it */
  550. ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
  551. if (ret)
  552. goto out;
  553. sprintf(tasks, "%s/tasks", controlgroup);
  554. ret = write_pid_to_tasks(tasks, bm_pid);
  555. if (ret)
  556. goto out;
  557. /* Create monitor group and write pid into if it is used */
  558. if (mongrp) {
  559. sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
  560. sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
  561. ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
  562. if (ret)
  563. goto out;
  564. sprintf(tasks, "%s/mon_groups/%s/tasks",
  565. controlgroup, mongrp);
  566. ret = write_pid_to_tasks(tasks, bm_pid);
  567. if (ret)
  568. goto out;
  569. }
  570. out:
  571. ksft_print_msg("Writing benchmark parameters to resctrl FS\n");
  572. if (ret)
  573. ksft_print_msg("Failed writing to resctrlfs\n");
  574. return ret;
  575. }
  576. /*
  577. * write_schemata - Update schemata of a con_mon grp
  578. * @ctrlgrp: Name of the con_mon grp
  579. * @schemata: Schemata that should be updated to
  580. * @cpu_no: CPU number that the benchmark PID is binded to
  581. * @resource: Resctrl resource (Eg: MB, L3, L2, etc.)
  582. *
  583. * Update schemata of a con_mon grp *only* if requested resctrl resource is
  584. * allocation type
  585. *
  586. * Return: 0 on success, < 0 on error.
  587. */
  588. int write_schemata(const char *ctrlgrp, char *schemata, int cpu_no,
  589. const char *resource)
  590. {
  591. char controlgroup[1024], reason[128], schema[1024] = {};
  592. int domain_id, fd, schema_len, ret = 0;
  593. if (!schemata) {
  594. ksft_print_msg("Skipping empty schemata update\n");
  595. return -1;
  596. }
  597. if (get_domain_id(resource, cpu_no, &domain_id) < 0) {
  598. sprintf(reason, "Failed to get domain ID");
  599. ret = -1;
  600. goto out;
  601. }
  602. if (ctrlgrp)
  603. sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
  604. else
  605. sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
  606. schema_len = snprintf(schema, sizeof(schema), "%s:%d=%s\n",
  607. resource, domain_id, schemata);
  608. if (schema_len < 0 || schema_len >= sizeof(schema)) {
  609. snprintf(reason, sizeof(reason),
  610. "snprintf() failed with return value : %d", schema_len);
  611. ret = -1;
  612. goto out;
  613. }
  614. fd = open(controlgroup, O_WRONLY);
  615. if (fd < 0) {
  616. snprintf(reason, sizeof(reason),
  617. "open() failed : %s", strerror(errno));
  618. ret = -1;
  619. goto err_schema_not_empty;
  620. }
  621. if (write(fd, schema, schema_len) < 0) {
  622. snprintf(reason, sizeof(reason),
  623. "write() failed : %s", strerror(errno));
  624. close(fd);
  625. ret = -1;
  626. goto err_schema_not_empty;
  627. }
  628. close(fd);
  629. err_schema_not_empty:
  630. schema[schema_len - 1] = 0;
  631. out:
  632. ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
  633. schema, ret ? " # " : "",
  634. ret ? reason : "");
  635. return ret;
  636. }
  637. bool check_resctrlfs_support(void)
  638. {
  639. FILE *inf = fopen("/proc/filesystems", "r");
  640. DIR *dp;
  641. char *res;
  642. bool ret = false;
  643. if (!inf)
  644. return false;
  645. res = fgrep(inf, "nodev\tresctrl\n");
  646. if (res) {
  647. ret = true;
  648. free(res);
  649. }
  650. fclose(inf);
  651. ksft_print_msg("%s Check kernel supports resctrl filesystem\n",
  652. ret ? "Pass:" : "Fail:");
  653. if (!ret)
  654. return ret;
  655. dp = opendir(RESCTRL_PATH);
  656. ksft_print_msg("%s Check resctrl mountpoint \"%s\" exists\n",
  657. dp ? "Pass:" : "Fail:", RESCTRL_PATH);
  658. if (dp)
  659. closedir(dp);
  660. ksft_print_msg("resctrl filesystem %s mounted\n",
  661. find_resctrl_mount(NULL) ? "not" : "is");
  662. return ret;
  663. }
  664. char *fgrep(FILE *inf, const char *str)
  665. {
  666. char line[256];
  667. int slen = strlen(str);
  668. while (!feof(inf)) {
  669. if (!fgets(line, 256, inf))
  670. break;
  671. if (strncmp(line, str, slen))
  672. continue;
  673. return strdup(line);
  674. }
  675. return NULL;
  676. }
  677. /*
  678. * resctrl_resource_exists - Check if a resource is supported.
  679. * @resource: Resctrl resource (e.g., MB, L3, L2, L3_MON, etc.)
  680. *
  681. * Return: True if the resource is supported, else false. False is
  682. * also returned if resctrl FS is not mounted.
  683. */
  684. bool resctrl_resource_exists(const char *resource)
  685. {
  686. char res_path[PATH_MAX];
  687. struct stat statbuf;
  688. int ret;
  689. if (!resource)
  690. return false;
  691. ret = find_resctrl_mount(NULL);
  692. if (ret)
  693. return false;
  694. snprintf(res_path, sizeof(res_path), "%s/%s", INFO_PATH, resource);
  695. if (stat(res_path, &statbuf))
  696. return false;
  697. return true;
  698. }
  699. /*
  700. * resctrl_mon_feature_exists - Check if requested monitoring feature is valid.
  701. * @resource: Resource that uses the mon_features file. Currently only L3_MON
  702. * is valid.
  703. * @feature: Required monitor feature (in mon_features file).
  704. *
  705. * Return: True if the feature is supported, else false.
  706. */
  707. bool resctrl_mon_feature_exists(const char *resource, const char *feature)
  708. {
  709. char res_path[PATH_MAX];
  710. char *res;
  711. FILE *inf;
  712. if (!feature || !resource)
  713. return false;
  714. snprintf(res_path, sizeof(res_path), "%s/%s/mon_features", INFO_PATH, resource);
  715. inf = fopen(res_path, "r");
  716. if (!inf)
  717. return false;
  718. res = fgrep(inf, feature);
  719. free(res);
  720. fclose(inf);
  721. return !!res;
  722. }
  723. /*
  724. * resource_info_file_exists - Check if a file is present inside
  725. * /sys/fs/resctrl/info/@resource.
  726. * @resource: Required resource (Eg: MB, L3, L2, etc.)
  727. * @file: Required file.
  728. *
  729. * Return: True if the /sys/fs/resctrl/info/@resource/@file exists, else false.
  730. */
  731. bool resource_info_file_exists(const char *resource, const char *file)
  732. {
  733. char res_path[PATH_MAX];
  734. struct stat statbuf;
  735. if (!file || !resource)
  736. return false;
  737. snprintf(res_path, sizeof(res_path), "%s/%s/%s", INFO_PATH, resource,
  738. file);
  739. if (stat(res_path, &statbuf))
  740. return false;
  741. return true;
  742. }
  743. bool test_resource_feature_check(const struct resctrl_test *test)
  744. {
  745. return resctrl_resource_exists(test->resource);
  746. }
  747. int filter_dmesg(void)
  748. {
  749. char line[1024];
  750. FILE *fp;
  751. int pipefds[2];
  752. pid_t pid;
  753. int ret;
  754. ret = pipe(pipefds);
  755. if (ret) {
  756. ksft_perror("pipe");
  757. return ret;
  758. }
  759. fflush(stdout);
  760. pid = fork();
  761. if (pid == 0) {
  762. close(pipefds[0]);
  763. dup2(pipefds[1], STDOUT_FILENO);
  764. execlp("dmesg", "dmesg", NULL);
  765. ksft_perror("Executing dmesg");
  766. exit(1);
  767. }
  768. close(pipefds[1]);
  769. fp = fdopen(pipefds[0], "r");
  770. if (!fp) {
  771. ksft_perror("fdopen(pipe)");
  772. kill(pid, SIGTERM);
  773. return -1;
  774. }
  775. while (fgets(line, 1024, fp)) {
  776. if (strstr(line, "intel_rdt:"))
  777. ksft_print_msg("dmesg: %s", line);
  778. if (strstr(line, "resctrl:"))
  779. ksft_print_msg("dmesg: %s", line);
  780. }
  781. fclose(fp);
  782. waitpid(pid, NULL, 0);
  783. return 0;
  784. }
  785. int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
  786. int group_fd, unsigned long flags)
  787. {
  788. int ret;
  789. ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
  790. group_fd, flags);
  791. return ret;
  792. }
  793. unsigned int count_bits(unsigned long n)
  794. {
  795. unsigned int count = 0;
  796. while (n) {
  797. count += n & 1;
  798. n >>= 1;
  799. }
  800. return count;
  801. }
  802. /**
  803. * snc_kernel_support - Check for existence of mon_sub_L3_00 file that indicates
  804. * SNC resctrl support on the kernel side.
  805. *
  806. * Return: 0 if not supported, 1 if SNC is disabled or SNC discovery is
  807. * unreliable or SNC is both enabled and supported.
  808. */
  809. int snc_kernel_support(void)
  810. {
  811. char node_path[PATH_MAX];
  812. struct stat statbuf;
  813. int ret;
  814. ret = snc_nodes_per_l3_cache();
  815. /*
  816. * If SNC is disabled then its kernel support isn't important. If SNC
  817. * got disabled because the discovery process was unreliable the
  818. * snc_unreliable variable was set. It can be used to verify the SNC
  819. * discovery reliability elsewhere in the selftest.
  820. */
  821. if (ret == 1)
  822. return ret;
  823. snprintf(node_path, sizeof(node_path), "%s/%s", RESCTRL_PATH,
  824. "mon_data/mon_L3_00/mon_sub_L3_00");
  825. if (!stat(node_path, &statbuf))
  826. return 1;
  827. return 0;
  828. }