Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / tools / testing / selftests / resctrl / resctrlfs.c
blob19c0ec4045a40b242d2186a5e724022642cb1d6b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Basic resctrl file system operations
5 * Copyright (C) 2018 Intel Corporation
7 * Authors:
8 * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
9 * Fenghua Yu <fenghua.yu@intel.com>
11 #include "resctrl.h"
13 int tests_run;
15 static int find_resctrl_mount(char *buffer)
17 FILE *mounts;
18 char line[256], *fs, *mntpoint;
20 mounts = fopen("/proc/mounts", "r");
21 if (!mounts) {
22 perror("/proc/mounts");
23 return -ENXIO;
25 while (!feof(mounts)) {
26 if (!fgets(line, 256, mounts))
27 break;
28 fs = strtok(line, " \t");
29 if (!fs)
30 continue;
31 mntpoint = strtok(NULL, " \t");
32 if (!mntpoint)
33 continue;
34 fs = strtok(NULL, " \t");
35 if (!fs)
36 continue;
37 if (strcmp(fs, "resctrl"))
38 continue;
40 fclose(mounts);
41 if (buffer)
42 strncpy(buffer, mntpoint, 256);
44 return 0;
47 fclose(mounts);
49 return -ENOENT;
52 char cbm_mask[256];
55 * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl
56 * @mum_resctrlfs: Should the resctrl FS be remounted?
58 * If not mounted, mount it.
59 * If mounted and mum_resctrlfs then remount resctrl FS.
60 * If mounted and !mum_resctrlfs then noop
62 * Return: 0 on success, non-zero on failure
64 int remount_resctrlfs(bool mum_resctrlfs)
66 char mountpoint[256];
67 int ret;
69 ret = find_resctrl_mount(mountpoint);
70 if (ret)
71 strcpy(mountpoint, RESCTRL_PATH);
73 if (!ret && mum_resctrlfs && umount(mountpoint)) {
74 printf("not ok unmounting \"%s\"\n", mountpoint);
75 perror("# umount");
76 tests_run++;
79 if (!ret && !mum_resctrlfs)
80 return 0;
82 ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
83 printf("%sok mounting resctrl to \"%s\"\n", ret ? "not " : "",
84 RESCTRL_PATH);
85 if (ret)
86 perror("# mount");
88 tests_run++;
90 return ret;
93 int umount_resctrlfs(void)
95 if (umount(RESCTRL_PATH)) {
96 perror("# Unable to umount resctrl");
98 return errno;
101 return 0;
105 * get_resource_id - Get socket number/l3 id for a specified CPU
106 * @cpu_no: CPU number
107 * @resource_id: Socket number or l3_id
109 * Return: >= 0 on success, < 0 on failure.
111 int get_resource_id(int cpu_no, int *resource_id)
113 char phys_pkg_path[1024];
114 FILE *fp;
116 if (is_amd)
117 sprintf(phys_pkg_path, "%s%d/cache/index3/id",
118 PHYS_ID_PATH, cpu_no);
119 else
120 sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
121 PHYS_ID_PATH, cpu_no);
123 fp = fopen(phys_pkg_path, "r");
124 if (!fp) {
125 perror("Failed to open physical_package_id");
127 return -1;
129 if (fscanf(fp, "%d", resource_id) <= 0) {
130 perror("Could not get socket number or l3 id");
131 fclose(fp);
133 return -1;
135 fclose(fp);
137 return 0;
141 * get_cache_size - Get cache size for a specified CPU
142 * @cpu_no: CPU number
143 * @cache_type: Cache level L2/L3
144 * @cache_size: pointer to cache_size
146 * Return: = 0 on success, < 0 on failure.
148 int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
150 char cache_path[1024], cache_str[64];
151 int length, i, cache_num;
152 FILE *fp;
154 if (!strcmp(cache_type, "L3")) {
155 cache_num = 3;
156 } else if (!strcmp(cache_type, "L2")) {
157 cache_num = 2;
158 } else {
159 perror("Invalid cache level");
160 return -1;
163 sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
164 cpu_no, cache_num);
165 fp = fopen(cache_path, "r");
166 if (!fp) {
167 perror("Failed to open cache size");
169 return -1;
171 if (fscanf(fp, "%s", cache_str) <= 0) {
172 perror("Could not get cache_size");
173 fclose(fp);
175 return -1;
177 fclose(fp);
179 length = (int)strlen(cache_str);
181 *cache_size = 0;
183 for (i = 0; i < length; i++) {
184 if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
186 *cache_size = *cache_size * 10 + (cache_str[i] - '0');
188 else if (cache_str[i] == 'K')
190 *cache_size = *cache_size * 1024;
192 else if (cache_str[i] == 'M')
194 *cache_size = *cache_size * 1024 * 1024;
196 else
197 break;
200 return 0;
203 #define CORE_SIBLINGS_PATH "/sys/bus/cpu/devices/cpu"
206 * get_cbm_mask - Get cbm mask for given cache
207 * @cache_type: Cache level L2/L3
209 * Mask is stored in cbm_mask which is global variable.
211 * Return: = 0 on success, < 0 on failure.
213 int get_cbm_mask(char *cache_type)
215 char cbm_mask_path[1024];
216 FILE *fp;
218 sprintf(cbm_mask_path, "%s/%s/cbm_mask", CBM_MASK_PATH, cache_type);
220 fp = fopen(cbm_mask_path, "r");
221 if (!fp) {
222 perror("Failed to open cache level");
224 return -1;
226 if (fscanf(fp, "%s", cbm_mask) <= 0) {
227 perror("Could not get max cbm_mask");
228 fclose(fp);
230 return -1;
232 fclose(fp);
234 return 0;
238 * get_core_sibling - Get sibling core id from the same socket for given CPU
239 * @cpu_no: CPU number
241 * Return: > 0 on success, < 0 on failure.
243 int get_core_sibling(int cpu_no)
245 char core_siblings_path[1024], cpu_list_str[64];
246 int sibling_cpu_no = -1;
247 FILE *fp;
249 sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
250 CORE_SIBLINGS_PATH, cpu_no);
252 fp = fopen(core_siblings_path, "r");
253 if (!fp) {
254 perror("Failed to open core siblings path");
256 return -1;
258 if (fscanf(fp, "%s", cpu_list_str) <= 0) {
259 perror("Could not get core_siblings list");
260 fclose(fp);
262 return -1;
264 fclose(fp);
266 char *token = strtok(cpu_list_str, "-,");
268 while (token) {
269 sibling_cpu_no = atoi(token);
270 /* Skipping core 0 as we don't want to run test on core 0 */
271 if (sibling_cpu_no != 0)
272 break;
273 token = strtok(NULL, "-,");
276 return sibling_cpu_no;
280 * taskset_benchmark - Taskset PID (i.e. benchmark) to a specified cpu
281 * @bm_pid: PID that should be binded
282 * @cpu_no: CPU number at which the PID would be binded
284 * Return: 0 on success, non-zero on failure
286 int taskset_benchmark(pid_t bm_pid, int cpu_no)
288 cpu_set_t my_set;
290 CPU_ZERO(&my_set);
291 CPU_SET(cpu_no, &my_set);
293 if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
294 perror("Unable to taskset benchmark");
296 return -1;
299 return 0;
303 * run_benchmark - Run a specified benchmark or fill_buf (default benchmark)
304 * in specified signal. Direct benchmark stdio to /dev/null.
305 * @signum: signal number
306 * @info: signal info
307 * @ucontext: user context in signal handling
309 * Return: void
311 void run_benchmark(int signum, siginfo_t *info, void *ucontext)
313 int operation, ret, malloc_and_init_memory, memflush;
314 unsigned long span, buffer_span;
315 char **benchmark_cmd;
316 char resctrl_val[64];
317 FILE *fp;
319 benchmark_cmd = info->si_ptr;
322 * Direct stdio of child to /dev/null, so that only parent writes to
323 * stdio (console)
325 fp = freopen("/dev/null", "w", stdout);
326 if (!fp)
327 PARENT_EXIT("Unable to direct benchmark status to /dev/null");
329 if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
330 /* Execute default fill_buf benchmark */
331 span = strtoul(benchmark_cmd[1], NULL, 10);
332 malloc_and_init_memory = atoi(benchmark_cmd[2]);
333 memflush = atoi(benchmark_cmd[3]);
334 operation = atoi(benchmark_cmd[4]);
335 sprintf(resctrl_val, "%s", benchmark_cmd[5]);
337 if (strcmp(resctrl_val, "cqm") != 0)
338 buffer_span = span * MB;
339 else
340 buffer_span = span;
342 if (run_fill_buf(buffer_span, malloc_and_init_memory, memflush,
343 operation, resctrl_val))
344 fprintf(stderr, "Error in running fill buffer\n");
345 } else {
346 /* Execute specified benchmark */
347 ret = execvp(benchmark_cmd[0], benchmark_cmd);
348 if (ret)
349 perror("wrong\n");
352 fclose(stdout);
353 PARENT_EXIT("Unable to run specified benchmark");
357 * create_grp - Create a group only if one doesn't exist
358 * @grp_name: Name of the group
359 * @grp: Full path and name of the group
360 * @parent_grp: Full path and name of the parent group
362 * Return: 0 on success, non-zero on failure
364 static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
366 int found_grp = 0;
367 struct dirent *ep;
368 DIR *dp;
371 * At this point, we are guaranteed to have resctrl FS mounted and if
372 * length of grp_name == 0, it means, user wants to use root con_mon
373 * grp, so do nothing
375 if (strlen(grp_name) == 0)
376 return 0;
378 /* Check if requested grp exists or not */
379 dp = opendir(parent_grp);
380 if (dp) {
381 while ((ep = readdir(dp)) != NULL) {
382 if (strcmp(ep->d_name, grp_name) == 0)
383 found_grp = 1;
385 closedir(dp);
386 } else {
387 perror("Unable to open resctrl for group");
389 return -1;
392 /* Requested grp doesn't exist, hence create it */
393 if (found_grp == 0) {
394 if (mkdir(grp, 0) == -1) {
395 perror("Unable to create group");
397 return -1;
401 return 0;
404 static int write_pid_to_tasks(char *tasks, pid_t pid)
406 FILE *fp;
408 fp = fopen(tasks, "w");
409 if (!fp) {
410 perror("Failed to open tasks file");
412 return -1;
414 if (fprintf(fp, "%d\n", pid) < 0) {
415 perror("Failed to wr pid to tasks file");
416 fclose(fp);
418 return -1;
420 fclose(fp);
422 return 0;
426 * write_bm_pid_to_resctrl - Write a PID (i.e. benchmark) to resctrl FS
427 * @bm_pid: PID that should be written
428 * @ctrlgrp: Name of the control monitor group (con_mon grp)
429 * @mongrp: Name of the monitor group (mon grp)
430 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
432 * If a con_mon grp is requested, create it and write pid to it, otherwise
433 * write pid to root con_mon grp.
434 * If a mon grp is requested, create it and write pid to it, otherwise
435 * pid is not written, this means that pid is in con_mon grp and hence
436 * should consult con_mon grp's mon_data directory for results.
438 * Return: 0 on success, non-zero on failure
440 int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
441 char *resctrl_val)
443 char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
444 char tasks[1024];
445 int ret = 0;
447 if (strlen(ctrlgrp))
448 sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
449 else
450 sprintf(controlgroup, "%s", RESCTRL_PATH);
452 /* Create control and monitoring group and write pid into it */
453 ret = create_grp(ctrlgrp, controlgroup, RESCTRL_PATH);
454 if (ret)
455 goto out;
456 sprintf(tasks, "%s/tasks", controlgroup);
457 ret = write_pid_to_tasks(tasks, bm_pid);
458 if (ret)
459 goto out;
461 /* Create mon grp and write pid into it for "mbm" and "cqm" test */
462 if ((strcmp(resctrl_val, "cqm") == 0) ||
463 (strcmp(resctrl_val, "mbm") == 0)) {
464 if (strlen(mongrp)) {
465 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup);
466 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp);
467 ret = create_grp(mongrp, monitorgroup, monitorgroup_p);
468 if (ret)
469 goto out;
471 sprintf(tasks, "%s/mon_groups/%s/tasks",
472 controlgroup, mongrp);
473 ret = write_pid_to_tasks(tasks, bm_pid);
474 if (ret)
475 goto out;
479 out:
480 printf("%sok writing benchmark parameters to resctrl FS\n",
481 ret ? "not " : "");
482 if (ret)
483 perror("# writing to resctrlfs");
485 tests_run++;
487 return ret;
491 * write_schemata - Update schemata of a con_mon grp
492 * @ctrlgrp: Name of the con_mon grp
493 * @schemata: Schemata that should be updated to
494 * @cpu_no: CPU number that the benchmark PID is binded to
495 * @resctrl_val: Resctrl feature (Eg: mbm, mba.. etc)
497 * Update schemata of a con_mon grp *only* if requested resctrl feature is
498 * allocation type
500 * Return: 0 on success, non-zero on failure
502 int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
504 char controlgroup[1024], schema[1024], reason[64];
505 int resource_id, ret = 0;
506 FILE *fp;
508 if ((strcmp(resctrl_val, "mba") != 0) &&
509 (strcmp(resctrl_val, "cat") != 0) &&
510 (strcmp(resctrl_val, "cqm") != 0))
511 return -ENOENT;
513 if (!schemata) {
514 printf("# Skipping empty schemata update\n");
516 return -1;
519 if (get_resource_id(cpu_no, &resource_id) < 0) {
520 sprintf(reason, "Failed to get resource id");
521 ret = -1;
523 goto out;
526 if (strlen(ctrlgrp) != 0)
527 sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
528 else
529 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
531 if (!strcmp(resctrl_val, "cat") || !strcmp(resctrl_val, "cqm"))
532 sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
533 if (strcmp(resctrl_val, "mba") == 0)
534 sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
536 fp = fopen(controlgroup, "w");
537 if (!fp) {
538 sprintf(reason, "Failed to open control group");
539 ret = -1;
541 goto out;
544 if (fprintf(fp, "%s\n", schema) < 0) {
545 sprintf(reason, "Failed to write schemata in control group");
546 fclose(fp);
547 ret = -1;
549 goto out;
551 fclose(fp);
553 out:
554 printf("%sok Write schema \"%s\" to resctrl FS%s%s\n",
555 ret ? "not " : "", schema, ret ? " # " : "",
556 ret ? reason : "");
557 tests_run++;
559 return ret;
562 bool check_resctrlfs_support(void)
564 FILE *inf = fopen("/proc/filesystems", "r");
565 DIR *dp;
566 char *res;
567 bool ret = false;
569 if (!inf)
570 return false;
572 res = fgrep(inf, "nodev\tresctrl\n");
574 if (res) {
575 ret = true;
576 free(res);
579 fclose(inf);
581 printf("%sok kernel supports resctrl filesystem\n", ret ? "" : "not ");
582 tests_run++;
584 dp = opendir(RESCTRL_PATH);
585 printf("%sok resctrl mountpoint \"%s\" exists\n",
586 dp ? "" : "not ", RESCTRL_PATH);
587 if (dp)
588 closedir(dp);
589 tests_run++;
591 printf("# resctrl filesystem %s mounted\n",
592 find_resctrl_mount(NULL) ? "not" : "is");
594 return ret;
597 char *fgrep(FILE *inf, const char *str)
599 char line[256];
600 int slen = strlen(str);
602 while (!feof(inf)) {
603 if (!fgets(line, 256, inf))
604 break;
605 if (strncmp(line, str, slen))
606 continue;
608 return strdup(line);
611 return NULL;
615 * validate_resctrl_feature_request - Check if requested feature is valid.
616 * @resctrl_val: Requested feature
618 * Return: 0 on success, non-zero on failure
620 bool validate_resctrl_feature_request(char *resctrl_val)
622 FILE *inf = fopen("/proc/cpuinfo", "r");
623 bool found = false;
624 char *res;
626 if (!inf)
627 return false;
629 res = fgrep(inf, "flags");
631 if (res) {
632 char *s = strchr(res, ':');
634 found = s && !strstr(s, resctrl_val);
635 free(res);
637 fclose(inf);
639 return found;
642 int filter_dmesg(void)
644 char line[1024];
645 FILE *fp;
646 int pipefds[2];
647 pid_t pid;
648 int ret;
650 ret = pipe(pipefds);
651 if (ret) {
652 perror("pipe");
653 return ret;
655 pid = fork();
656 if (pid == 0) {
657 close(pipefds[0]);
658 dup2(pipefds[1], STDOUT_FILENO);
659 execlp("dmesg", "dmesg", NULL);
660 perror("executing dmesg");
661 exit(1);
663 close(pipefds[1]);
664 fp = fdopen(pipefds[0], "r");
665 if (!fp) {
666 perror("fdopen(pipe)");
667 kill(pid, SIGTERM);
669 return -1;
672 while (fgets(line, 1024, fp)) {
673 if (strstr(line, "intel_rdt:"))
674 printf("# dmesg: %s", line);
675 if (strstr(line, "resctrl:"))
676 printf("# dmesg: %s", line);
678 fclose(fp);
679 waitpid(pid, NULL, 0);
681 return 0;
684 int validate_bw_report_request(char *bw_report)
686 if (strcmp(bw_report, "reads") == 0)
687 return 0;
688 if (strcmp(bw_report, "writes") == 0)
689 return 0;
690 if (strcmp(bw_report, "nt-writes") == 0) {
691 strcpy(bw_report, "writes");
692 return 0;
694 if (strcmp(bw_report, "total") == 0)
695 return 0;
697 fprintf(stderr, "Requested iMC B/W report type unavailable\n");
699 return -1;
702 int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
703 int group_fd, unsigned long flags)
705 int ret;
707 ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
708 group_fd, flags);
709 return ret;
712 unsigned int count_bits(unsigned long n)
714 unsigned int count = 0;
716 while (n) {
717 count += n & 1;
718 n >>= 1;
721 return count;