1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Memory Bandwidth Monitoring (MBM) test
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 
13 #define RESULT_FILE_NAME	"result_mbm"
14 #define MAX_DIFF		300
15 #define NUM_OF_RUNS		5
16 
17 static void
18 show_bw_info(unsigned long *bw_imc, unsigned long *bw_resc, int span)
19 {
20 	unsigned long avg_bw_imc = 0, avg_bw_resc = 0;
21 	unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
22 	long avg_diff = 0;
23 	int runs;
24 
25 	/*
26 	 * Discard the first value which is inaccurate due to monitoring setup
27 	 * transition phase.
28 	 */
29 	for (runs = 1; runs < NUM_OF_RUNS ; runs++) {
30 		sum_bw_imc += bw_imc[runs];
31 		sum_bw_resc += bw_resc[runs];
32 	}
33 
34 	avg_bw_imc = sum_bw_imc / 4;
35 	avg_bw_resc = sum_bw_resc / 4;
36 	avg_diff = avg_bw_resc - avg_bw_imc;
37 
38 	printf("%sok MBM: diff within %d%%\n",
39 	       labs(avg_diff) > MAX_DIFF ? "not " : "", MAX_DIFF);
40 	tests_run++;
41 	printf("# avg_diff: %lu\n", labs(avg_diff));
42 	printf("# Span (MB): %d\n", span);
43 	printf("# avg_bw_imc: %lu\n", avg_bw_imc);
44 	printf("# avg_bw_resc: %lu\n", avg_bw_resc);
45 }
46 
47 static int check_results(int span)
48 {
49 	unsigned long bw_imc[NUM_OF_RUNS], bw_resc[NUM_OF_RUNS];
50 	char temp[1024], *token_array[8];
51 	char output[] = RESULT_FILE_NAME;
52 	int runs;
53 	FILE *fp;
54 
55 	printf("# Checking for pass/fail\n");
56 
57 	fp = fopen(output, "r");
58 	if (!fp) {
59 		perror(output);
60 
61 		return errno;
62 	}
63 
64 	runs = 0;
65 	while (fgets(temp, sizeof(temp), fp)) {
66 		char *token = strtok(temp, ":\t");
67 		int i = 0;
68 
69 		while (token) {
70 			token_array[i++] = token;
71 			token = strtok(NULL, ":\t");
72 		}
73 
74 		bw_resc[runs] = strtoul(token_array[5], NULL, 0);
75 		bw_imc[runs] = strtoul(token_array[3], NULL, 0);
76 		runs++;
77 	}
78 
79 	show_bw_info(bw_imc, bw_resc, span);
80 
81 	fclose(fp);
82 
83 	return 0;
84 }
85 
86 static int mbm_setup(int num, ...)
87 {
88 	struct resctrl_val_param *p;
89 	static int num_of_runs;
90 	va_list param;
91 	int ret = 0;
92 
93 	/* Run NUM_OF_RUNS times */
94 	if (num_of_runs++ >= NUM_OF_RUNS)
95 		return -1;
96 
97 	va_start(param, num);
98 	p = va_arg(param, struct resctrl_val_param *);
99 	va_end(param);
100 
101 	/* Set up shemata with 100% allocation on the first run. */
102 	if (num_of_runs == 0)
103 		ret = write_schemata(p->ctrlgrp, "100", p->cpu_no,
104 				     p->resctrl_val);
105 
106 	return ret;
107 }
108 
109 void mbm_test_cleanup(void)
110 {
111 	remove(RESULT_FILE_NAME);
112 }
113 
114 int mbm_bw_change(int span, int cpu_no, char *bw_report, char **benchmark_cmd)
115 {
116 	struct resctrl_val_param param = {
117 		.resctrl_val	= "mbm",
118 		.ctrlgrp	= "c1",
119 		.mongrp		= "m1",
120 		.span		= span,
121 		.cpu_no		= cpu_no,
122 		.mum_resctrlfs	= 1,
123 		.filename	= RESULT_FILE_NAME,
124 		.bw_report	=  bw_report,
125 		.setup		= mbm_setup
126 	};
127 	int ret;
128 
129 	remove(RESULT_FILE_NAME);
130 
131 	if (!validate_resctrl_feature_request("mbm"))
132 		return -1;
133 
134 	ret = resctrl_val(benchmark_cmd, &param);
135 	if (ret)
136 		return ret;
137 
138 	ret = check_results(span);
139 	if (ret)
140 		return ret;
141 
142 	mbm_test_cleanup();
143 
144 	return 0;
145 }
146