selftests/resctrl: Call kselftest APIs to log test results
[linux-2.6-microblaze.git] / tools / testing / selftests / resctrl / cat_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cache Allocation Technology (CAT) 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 #include <unistd.h>
13
14 #define RESULT_FILE_NAME1       "result_cat1"
15 #define RESULT_FILE_NAME2       "result_cat2"
16 #define NUM_OF_RUNS             5
17 #define MAX_DIFF_PERCENT        4
18 #define MAX_DIFF                1000000
19
20 static int count_of_bits;
21 static char cbm_mask[256];
22 static unsigned long long_mask;
23 static unsigned long cache_size;
24
25 /*
26  * Change schemata. Write schemata to specified
27  * con_mon grp, mon_grp in resctrl FS.
28  * Run 5 times in order to get average values.
29  */
30 static int cat_setup(int num, ...)
31 {
32         struct resctrl_val_param *p;
33         char schemata[64];
34         va_list param;
35         int ret = 0;
36
37         va_start(param, num);
38         p = va_arg(param, struct resctrl_val_param *);
39         va_end(param);
40
41         /* Run NUM_OF_RUNS times */
42         if (p->num_of_runs >= NUM_OF_RUNS)
43                 return -1;
44
45         if (p->num_of_runs == 0) {
46                 sprintf(schemata, "%lx", p->mask);
47                 ret = write_schemata(p->ctrlgrp, schemata, p->cpu_no,
48                                      p->resctrl_val);
49         }
50         p->num_of_runs++;
51
52         return ret;
53 }
54
55 static int show_cache_info(unsigned long sum_llc_perf_miss, int no_of_bits,
56                            unsigned long span)
57 {
58         unsigned long allocated_cache_lines = span / 64;
59         unsigned long avg_llc_perf_miss = 0;
60         float diff_percent;
61         int ret;
62
63         avg_llc_perf_miss = sum_llc_perf_miss / (NUM_OF_RUNS - 1);
64         diff_percent = ((float)allocated_cache_lines - avg_llc_perf_miss) /
65                                 allocated_cache_lines * 100;
66
67         ret = !is_amd && abs((int)diff_percent) > MAX_DIFF_PERCENT;
68         ksft_print_msg("Cache miss rate %swithin %d%%\n",
69                        ret ? "not " : "", MAX_DIFF_PERCENT);
70
71         ksft_print_msg("Percent diff=%d\n", abs((int)diff_percent));
72         ksft_print_msg("Number of bits: %d\n", no_of_bits);
73         ksft_print_msg("Avg_llc_perf_miss: %lu\n", avg_llc_perf_miss);
74         ksft_print_msg("Allocated cache lines: %lu\n", allocated_cache_lines);
75
76         return ret;
77 }
78
79 static int check_results(struct resctrl_val_param *param)
80 {
81         char *token_array[8], temp[512];
82         unsigned long sum_llc_perf_miss = 0;
83         int runs = 0, no_of_bits = 0;
84         FILE *fp;
85
86         ksft_print_msg("Checking for pass/fail\n");
87         fp = fopen(param->filename, "r");
88         if (!fp) {
89                 perror("# Cannot open file");
90
91                 return errno;
92         }
93
94         while (fgets(temp, sizeof(temp), fp)) {
95                 char *token = strtok(temp, ":\t");
96                 int fields = 0;
97
98                 while (token) {
99                         token_array[fields++] = token;
100                         token = strtok(NULL, ":\t");
101                 }
102                 /*
103                  * Discard the first value which is inaccurate due to monitoring
104                  * setup transition phase.
105                  */
106                 if (runs > 0)
107                         sum_llc_perf_miss += strtoul(token_array[3], NULL, 0);
108                 runs++;
109         }
110
111         fclose(fp);
112         no_of_bits = count_bits(param->mask);
113
114         return show_cache_info(sum_llc_perf_miss, no_of_bits, param->span);
115 }
116
117 void cat_test_cleanup(void)
118 {
119         remove(RESULT_FILE_NAME1);
120         remove(RESULT_FILE_NAME2);
121 }
122
123 int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
124 {
125         unsigned long l_mask, l_mask_1;
126         int ret, pipefd[2], sibling_cpu_no;
127         char pipe_message;
128         pid_t bm_pid;
129
130         cache_size = 0;
131
132         ret = remount_resctrlfs(true);
133         if (ret)
134                 return ret;
135
136         if (!validate_resctrl_feature_request("cat"))
137                 return -1;
138
139         /* Get default cbm mask for L3/L2 cache */
140         ret = get_cbm_mask(cache_type, cbm_mask);
141         if (ret)
142                 return ret;
143
144         long_mask = strtoul(cbm_mask, NULL, 16);
145
146         /* Get L3/L2 cache size */
147         ret = get_cache_size(cpu_no, cache_type, &cache_size);
148         if (ret)
149                 return ret;
150         ksft_print_msg("Cache size :%lu\n", cache_size);
151
152         /* Get max number of bits from default-cabm mask */
153         count_of_bits = count_bits(long_mask);
154
155         if (n < 1 || n > count_of_bits - 1) {
156                 ksft_print_msg("Invalid input value for no_of_bits n!\n");
157                 ksft_print_msg("Please enter value in range 1 to %d\n",
158                                count_of_bits - 1);
159                 return -1;
160         }
161
162         /* Get core id from same socket for running another thread */
163         sibling_cpu_no = get_core_sibling(cpu_no);
164         if (sibling_cpu_no < 0)
165                 return -1;
166
167         struct resctrl_val_param param = {
168                 .resctrl_val    = CAT_STR,
169                 .cpu_no         = cpu_no,
170                 .mum_resctrlfs  = 0,
171                 .setup          = cat_setup,
172         };
173
174         l_mask = long_mask >> n;
175         l_mask_1 = ~l_mask & long_mask;
176
177         /* Set param values for parent thread which will be allocated bitmask
178          * with (max_bits - n) bits
179          */
180         param.span = cache_size * (count_of_bits - n) / count_of_bits;
181         strcpy(param.ctrlgrp, "c2");
182         strcpy(param.mongrp, "m2");
183         strcpy(param.filename, RESULT_FILE_NAME2);
184         param.mask = l_mask;
185         param.num_of_runs = 0;
186
187         if (pipe(pipefd)) {
188                 perror("# Unable to create pipe");
189                 return errno;
190         }
191
192         bm_pid = fork();
193
194         /* Set param values for child thread which will be allocated bitmask
195          * with n bits
196          */
197         if (bm_pid == 0) {
198                 param.mask = l_mask_1;
199                 strcpy(param.ctrlgrp, "c1");
200                 strcpy(param.mongrp, "m1");
201                 param.span = cache_size * n / count_of_bits;
202                 strcpy(param.filename, RESULT_FILE_NAME1);
203                 param.num_of_runs = 0;
204                 param.cpu_no = sibling_cpu_no;
205         }
206
207         remove(param.filename);
208
209         ret = cat_val(&param);
210         if (ret)
211                 return ret;
212
213         ret = check_results(&param);
214         if (ret)
215                 return ret;
216
217         if (bm_pid == 0) {
218                 /* Tell parent that child is ready */
219                 close(pipefd[0]);
220                 pipe_message = 1;
221                 if (write(pipefd[1], &pipe_message, sizeof(pipe_message)) <
222                     sizeof(pipe_message)) {
223                         close(pipefd[1]);
224                         perror("# failed signaling parent process");
225                         return errno;
226                 }
227
228                 close(pipefd[1]);
229                 while (1)
230                         ;
231         } else {
232                 /* Parent waits for child to be ready. */
233                 close(pipefd[1]);
234                 pipe_message = 0;
235                 while (pipe_message != 1) {
236                         if (read(pipefd[0], &pipe_message,
237                                  sizeof(pipe_message)) < sizeof(pipe_message)) {
238                                 perror("# failed reading from child process");
239                                 break;
240                         }
241                 }
242                 close(pipefd[0]);
243                 kill(bm_pid, SIGKILL);
244         }
245
246         cat_test_cleanup();
247         if (bm_pid)
248                 umount_resctrlfs();
249
250         return 0;
251 }