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 "resctrl.h"
12
13int tests_run;
14
15static int find_resctrl_mount(char *buffer)
16{
17	FILE *mounts;
18	char line[256], *fs, *mntpoint;
19
20	mounts = fopen("/proc/mounts", "r");
21	if (!mounts) {
22		perror("/proc/mounts");
23		return -ENXIO;
24	}
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;
39
40		fclose(mounts);
41		if (buffer)
42			strncpy(buffer, mntpoint, 256);
43
44		return 0;
45	}
46
47	fclose(mounts);
48
49	return -ENOENT;
50}
51
52/*
53 * remount_resctrlfs - Remount resctrl FS at /sys/fs/resctrl
54 * @mum_resctrlfs:	Should the resctrl FS be remounted?
55 *
56 * If not mounted, mount it.
57 * If mounted and mum_resctrlfs then remount resctrl FS.
58 * If mounted and !mum_resctrlfs then noop
59 *
60 * Return: 0 on success, non-zero on failure
61 */
62int remount_resctrlfs(bool mum_resctrlfs)
63{
64	char mountpoint[256];
65	int ret;
66
67	ret = find_resctrl_mount(mountpoint);
68	if (ret)
69		strcpy(mountpoint, RESCTRL_PATH);
70
71	if (!ret && mum_resctrlfs && umount(mountpoint)) {
72		printf("not ok unmounting \"%s\"\n", mountpoint);
73		perror("# umount");
74		tests_run++;
75	}
76
77	if (!ret && !mum_resctrlfs)
78		return 0;
79
80	ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL);
81	printf("%sok mounting resctrl to \"%s\"\n", ret ? "not " : "",
82	       RESCTRL_PATH);
83	if (ret)
84		perror("# mount");
85
86	tests_run++;
87
88	return ret;
89}
90
91int umount_resctrlfs(void)
92{
93	if (umount(RESCTRL_PATH)) {
94		perror("# Unable to umount resctrl");
95
96		return errno;
97	}
98
99	return 0;
100}
101
102/*
103 * get_resource_id - Get socket number/l3 id for a specified CPU
104 * @cpu_no:	CPU number
105 * @resource_id: Socket number or l3_id
106 *
107 * Return: >= 0 on success, < 0 on failure.
108 */
109int get_resource_id(int cpu_no, int *resource_id)
110{
111	char phys_pkg_path[1024];
112	FILE *fp;
113
114	if (is_amd)
115		sprintf(phys_pkg_path, "%s%d/cache/index3/id",
116			PHYS_ID_PATH, cpu_no);
117	else
118		sprintf(phys_pkg_path, "%s%d/topology/physical_package_id",
119			PHYS_ID_PATH, cpu_no);
120
121	fp = fopen(phys_pkg_path, "r");
122	if (!fp) {
123		perror("Failed to open physical_package_id");
124
125		return -1;
126	}
127	if (fscanf(fp, "%d", resource_id) <= 0) {
128		perror("Could not get socket number or l3 id");
129		fclose(fp);
130
131		return -1;
132	}
133	fclose(fp);
134
135	return 0;
136}
137
138/*
139 * get_cache_size - Get cache size for a specified CPU
140 * @cpu_no:	CPU number
141 * @cache_type:	Cache level L2/L3
142 * @cache_size:	pointer to cache_size
143 *
144 * Return: = 0 on success, < 0 on failure.
145 */
146int get_cache_size(int cpu_no, char *cache_type, unsigned long *cache_size)
147{
148	char cache_path[1024], cache_str[64];
149	int length, i, cache_num;
150	FILE *fp;
151
152	if (!strcmp(cache_type, "L3")) {
153		cache_num = 3;
154	} else if (!strcmp(cache_type, "L2")) {
155		cache_num = 2;
156	} else {
157		perror("Invalid cache level");
158		return -1;
159	}
160
161	sprintf(cache_path, "/sys/bus/cpu/devices/cpu%d/cache/index%d/size",
162		cpu_no, cache_num);
163	fp = fopen(cache_path, "r");
164	if (!fp) {
165		perror("Failed to open cache size");
166
167		return -1;
168	}
169	if (fscanf(fp, "%s", cache_str) <= 0) {
170		perror("Could not get cache_size");
171		fclose(fp);
172
173		return -1;
174	}
175	fclose(fp);
176
177	length = (int)strlen(cache_str);
178
179	*cache_size = 0;
180
181	for (i = 0; i < length; i++) {
182		if ((cache_str[i] >= '0') && (cache_str[i] <= '9'))
183
184			*cache_size = *cache_size * 10 + (cache_str[i] - '0');
185
186		else if (cache_str[i] == 'K')
187
188			*cache_size = *cache_size * 1024;
189
190		else if (cache_str[i] == 'M')
191
192			*cache_size = *cache_size * 1024 * 1024;
193
194		else
195			break;
196	}
197
198	return 0;
199}
200
201#define CORE_SIBLINGS_PATH	"/sys/bus/cpu/devices/cpu"
202
203/*
204 * get_cbm_mask - Get cbm mask for given cache
205 * @cache_type:	Cache level L2/L3
206 * @cbm_mask:	cbm_mask returned as a string
207 *
208 * Return: = 0 on success, < 0 on failure.
209 */
210int get_cbm_mask(char *cache_type, char *cbm_mask)
211{
212	char cbm_mask_path[1024];
213	FILE *fp;
214
215	if (!cbm_mask)
216		return -1;
217
218	sprintf(cbm_mask_path, "%s/%s/cbm_mask", CBM_MASK_PATH, cache_type);
219
220	fp = fopen(cbm_mask_path, "r");
221	if (!fp) {
222		perror("Failed to open cache level");
223
224		return -1;
225	}
226	if (fscanf(fp, "%s", cbm_mask) <= 0) {
227		perror("Could not get max cbm_mask");
228		fclose(fp);
229
230		return -1;
231	}
232	fclose(fp);
233
234	return 0;
235}
236
237/*
238 * get_core_sibling - Get sibling core id from the same socket for given CPU
239 * @cpu_no:	CPU number
240 *
241 * Return:	> 0 on success, < 0 on failure.
242 */
243int get_core_sibling(int cpu_no)
244{
245	char core_siblings_path[1024], cpu_list_str[64];
246	int sibling_cpu_no = -1;
247	FILE *fp;
248
249	sprintf(core_siblings_path, "%s%d/topology/core_siblings_list",
250		CORE_SIBLINGS_PATH, cpu_no);
251
252	fp = fopen(core_siblings_path, "r");
253	if (!fp) {
254		perror("Failed to open core siblings path");
255
256		return -1;
257	}
258	if (fscanf(fp, "%s", cpu_list_str) <= 0) {
259		perror("Could not get core_siblings list");
260		fclose(fp);
261
262		return -1;
263	}
264	fclose(fp);
265
266	char *token = strtok(cpu_list_str, "-,");
267
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, "-,");
274	}
275
276	return sibling_cpu_no;
277}
278
279/*
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
283 *
284 * Return: 0 on success, non-zero on failure
285 */
286int taskset_benchmark(pid_t bm_pid, int cpu_no)
287{
288	cpu_set_t my_set;
289
290	CPU_ZERO(&my_set);
291	CPU_SET(cpu_no, &my_set);
292
293	if (sched_setaffinity(bm_pid, sizeof(cpu_set_t), &my_set)) {
294		perror("Unable to taskset benchmark");
295
296		return -1;
297	}
298
299	return 0;
300}
301
302/*
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
308 *
309 * Return: void
310 */
311void run_benchmark(int signum, siginfo_t *info, void *ucontext)
312{
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;
318
319	benchmark_cmd = info->si_ptr;
320
321	/*
322	 * Direct stdio of child to /dev/null, so that only parent writes to
323	 * stdio (console)
324	 */
325	fp = freopen("/dev/null", "w", stdout);
326	if (!fp)
327		PARENT_EXIT("Unable to direct benchmark status to /dev/null");
328
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]);
336
337		if (strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
338			buffer_span = span * MB;
339		else
340			buffer_span = span;
341
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");
350	}
351
352	fclose(stdout);
353	PARENT_EXIT("Unable to run specified benchmark");
354}
355
356/*
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
361 *
362 * Return: 0 on success, non-zero on failure
363 */
364static int create_grp(const char *grp_name, char *grp, const char *parent_grp)
365{
366	int found_grp = 0;
367	struct dirent *ep;
368	DIR *dp;
369
370	/*
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
374	 */
375	if (strlen(grp_name) == 0)
376		return 0;
377
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;
384		}
385		closedir(dp);
386	} else {
387		perror("Unable to open resctrl for group");
388
389		return -1;
390	}
391
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");
396
397			return -1;
398		}
399	}
400
401	return 0;
402}
403
404static int write_pid_to_tasks(char *tasks, pid_t pid)
405{
406	FILE *fp;
407
408	fp = fopen(tasks, "w");
409	if (!fp) {
410		perror("Failed to open tasks file");
411
412		return -1;
413	}
414	if (fprintf(fp, "%d\n", pid) < 0) {
415		perror("Failed to wr pid to tasks file");
416		fclose(fp);
417
418		return -1;
419	}
420	fclose(fp);
421
422	return 0;
423}
424
425/*
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)
431 *
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.
437 *
438 * Return: 0 on success, non-zero on failure
439 */
440int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
441			    char *resctrl_val)
442{
443	char controlgroup[128], monitorgroup[512], monitorgroup_p[256];
444	char tasks[1024];
445	int ret = 0;
446
447	if (strlen(ctrlgrp))
448		sprintf(controlgroup, "%s/%s", RESCTRL_PATH, ctrlgrp);
449	else
450		sprintf(controlgroup, "%s", RESCTRL_PATH);
451
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;
460
461	/* Create mon grp and write pid into it for "mbm" and "cqm" test */
462	if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)) ||
463	    !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) {
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;
470
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;
476		}
477	}
478
479out:
480	printf("%sok writing benchmark parameters to resctrl FS\n",
481	       ret ? "not " : "");
482	if (ret)
483		perror("# writing to resctrlfs");
484
485	tests_run++;
486
487	return ret;
488}
489
490/*
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)
496 *
497 * Update schemata of a con_mon grp *only* if requested resctrl feature is
498 * allocation type
499 *
500 * Return: 0 on success, non-zero on failure
501 */
502int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
503{
504	char controlgroup[1024], schema[1024], reason[64];
505	int resource_id, ret = 0;
506	FILE *fp;
507
508	if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
509	    strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) &&
510	    strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
511		return -ENOENT;
512
513	if (!schemata) {
514		printf("# Skipping empty schemata update\n");
515
516		return -1;
517	}
518
519	if (get_resource_id(cpu_no, &resource_id) < 0) {
520		sprintf(reason, "Failed to get resource id");
521		ret = -1;
522
523		goto out;
524	}
525
526	if (strlen(ctrlgrp) != 0)
527		sprintf(controlgroup, "%s/%s/schemata", RESCTRL_PATH, ctrlgrp);
528	else
529		sprintf(controlgroup, "%s/schemata", RESCTRL_PATH);
530
531	if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
532	    !strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)))
533		sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
534	if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)))
535		sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
536
537	fp = fopen(controlgroup, "w");
538	if (!fp) {
539		sprintf(reason, "Failed to open control group");
540		ret = -1;
541
542		goto out;
543	}
544
545	if (fprintf(fp, "%s\n", schema) < 0) {
546		sprintf(reason, "Failed to write schemata in control group");
547		fclose(fp);
548		ret = -1;
549
550		goto out;
551	}
552	fclose(fp);
553
554out:
555	printf("%sok Write schema \"%s\" to resctrl FS%s%s\n",
556	       ret ? "not " : "", schema, ret ? " # " : "",
557	       ret ? reason : "");
558	tests_run++;
559
560	return ret;
561}
562
563bool check_resctrlfs_support(void)
564{
565	FILE *inf = fopen("/proc/filesystems", "r");
566	DIR *dp;
567	char *res;
568	bool ret = false;
569
570	if (!inf)
571		return false;
572
573	res = fgrep(inf, "nodev\tresctrl\n");
574
575	if (res) {
576		ret = true;
577		free(res);
578	}
579
580	fclose(inf);
581
582	printf("%sok kernel supports resctrl filesystem\n", ret ? "" : "not ");
583	tests_run++;
584
585	dp = opendir(RESCTRL_PATH);
586	printf("%sok resctrl mountpoint \"%s\" exists\n",
587	       dp ? "" : "not ", RESCTRL_PATH);
588	if (dp)
589		closedir(dp);
590	tests_run++;
591
592	printf("# resctrl filesystem %s mounted\n",
593	       find_resctrl_mount(NULL) ? "not" : "is");
594
595	return ret;
596}
597
598char *fgrep(FILE *inf, const char *str)
599{
600	char line[256];
601	int slen = strlen(str);
602
603	while (!feof(inf)) {
604		if (!fgets(line, 256, inf))
605			break;
606		if (strncmp(line, str, slen))
607			continue;
608
609		return strdup(line);
610	}
611
612	return NULL;
613}
614
615/*
616 * validate_resctrl_feature_request - Check if requested feature is valid.
617 * @resctrl_val:	Requested feature
618 *
619 * Return: 0 on success, non-zero on failure
620 */
621bool validate_resctrl_feature_request(char *resctrl_val)
622{
623	FILE *inf = fopen("/proc/cpuinfo", "r");
624	bool found = false;
625	char *res;
626
627	if (!inf)
628		return false;
629
630	res = fgrep(inf, "flags");
631
632	if (res) {
633		char *s = strchr(res, ':');
634
635		found = s && !strstr(s, resctrl_val);
636		free(res);
637	}
638	fclose(inf);
639
640	return found;
641}
642
643int filter_dmesg(void)
644{
645	char line[1024];
646	FILE *fp;
647	int pipefds[2];
648	pid_t pid;
649	int ret;
650
651	ret = pipe(pipefds);
652	if (ret) {
653		perror("pipe");
654		return ret;
655	}
656	pid = fork();
657	if (pid == 0) {
658		close(pipefds[0]);
659		dup2(pipefds[1], STDOUT_FILENO);
660		execlp("dmesg", "dmesg", NULL);
661		perror("executing dmesg");
662		exit(1);
663	}
664	close(pipefds[1]);
665	fp = fdopen(pipefds[0], "r");
666	if (!fp) {
667		perror("fdopen(pipe)");
668		kill(pid, SIGTERM);
669
670		return -1;
671	}
672
673	while (fgets(line, 1024, fp)) {
674		if (strstr(line, "intel_rdt:"))
675			printf("# dmesg: %s", line);
676		if (strstr(line, "resctrl:"))
677			printf("# dmesg: %s", line);
678	}
679	fclose(fp);
680	waitpid(pid, NULL, 0);
681
682	return 0;
683}
684
685int validate_bw_report_request(char *bw_report)
686{
687	if (strcmp(bw_report, "reads") == 0)
688		return 0;
689	if (strcmp(bw_report, "writes") == 0)
690		return 0;
691	if (strcmp(bw_report, "nt-writes") == 0) {
692		strcpy(bw_report, "writes");
693		return 0;
694	}
695	if (strcmp(bw_report, "total") == 0)
696		return 0;
697
698	fprintf(stderr, "Requested iMC B/W report type unavailable\n");
699
700	return -1;
701}
702
703int perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu,
704		    int group_fd, unsigned long flags)
705{
706	int ret;
707
708	ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
709		      group_fd, flags);
710	return ret;
711}
712
713unsigned int count_bits(unsigned long n)
714{
715	unsigned int count = 0;
716
717	while (n) {
718		count += n & 1;
719		n >>= 1;
720	}
721
722	return count;
723}
724