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_PERCENT	5
15 #define NUM_OF_RUNS		5
16 
17 static int
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 	int runs, ret, avg_diff_per;
23 	float avg_diff = 0;
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 = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
37 	avg_diff_per = (int)(avg_diff * 100);
38 
39 	ret = avg_diff_per > MAX_DIFF_PERCENT;
40 	ksft_print_msg("%s Check MBM diff within %d%%\n",
41 		       ret ? "Fail:" : "Pass:", MAX_DIFF_PERCENT);
42 	ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
43 	ksft_print_msg("Span (MB): %d\n", span);
44 	ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
45 	ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
46 
47 	return ret;
48 }
49 
50 static int check_results(int span)
51 {
52 	unsigned long bw_imc[NUM_OF_RUNS], bw_resc[NUM_OF_RUNS];
53 	char temp[1024], *token_array[8];
54 	char output[] = RESULT_FILE_NAME;
55 	int runs, ret;
56 	FILE *fp;
57 
58 	ksft_print_msg("Checking for pass/fail\n");
59 
60 	fp = fopen(output, "r");
61 	if (!fp) {
62 		perror(output);
63 
64 		return errno;
65 	}
66 
67 	runs = 0;
68 	while (fgets(temp, sizeof(temp), fp)) {
69 		char *token = strtok(temp, ":\t");
70 		int i = 0;
71 
72 		while (token) {
73 			token_array[i++] = token;
74 			token = strtok(NULL, ":\t");
75 		}
76 
77 		bw_resc[runs] = strtoul(token_array[5], NULL, 0);
78 		bw_imc[runs] = strtoul(token_array[3], NULL, 0);
79 		runs++;
80 	}
81 
82 	ret = show_bw_info(bw_imc, bw_resc, span);
83 
84 	fclose(fp);
85 
86 	return ret;
87 }
88 
89 static int mbm_setup(int num, ...)
90 {
91 	struct resctrl_val_param *p;
92 	va_list param;
93 	int ret = 0;
94 
95 	va_start(param, num);
96 	p = va_arg(param, struct resctrl_val_param *);
97 	va_end(param);
98 
99 	/* Run NUM_OF_RUNS times */
100 	if (p->num_of_runs >= NUM_OF_RUNS)
101 		return END_OF_TESTS;
102 
103 	/* Set up shemata with 100% allocation on the first run. */
104 	if (p->num_of_runs == 0)
105 		ret = write_schemata(p->ctrlgrp, "100", p->cpu_no,
106 				     p->resctrl_val);
107 
108 	p->num_of_runs++;
109 
110 	return ret;
111 }
112 
113 void mbm_test_cleanup(void)
114 {
115 	remove(RESULT_FILE_NAME);
116 }
117 
118 int mbm_bw_change(int span, int cpu_no, char *bw_report, char **benchmark_cmd)
119 {
120 	struct resctrl_val_param param = {
121 		.resctrl_val	= MBM_STR,
122 		.ctrlgrp	= "c1",
123 		.mongrp		= "m1",
124 		.span		= span,
125 		.cpu_no		= cpu_no,
126 		.mum_resctrlfs	= true,
127 		.filename	= RESULT_FILE_NAME,
128 		.bw_report	=  bw_report,
129 		.setup		= mbm_setup
130 	};
131 	int ret;
132 
133 	remove(RESULT_FILE_NAME);
134 
135 	ret = resctrl_val(benchmark_cmd, &param);
136 	if (ret)
137 		goto out;
138 
139 	ret = check_results(span);
140 
141 out:
142 	mbm_test_cleanup();
143 
144 	return ret;
145 }
146