Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cache Monitoring Technology (CMT) 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_NAME "result_cmt"
15#define NUM_OF_RUNS 5
16#define MAX_DIFF 2000000
17#define MAX_DIFF_PERCENT 15
18
19#define CON_MON_LCC_OCCUP_PATH \
20 "%s/%s/mon_data/mon_L3_%02d/llc_occupancy"
21
22static int cmt_init(const struct resctrl_val_param *param, int domain_id)
23{
24 sprintf(llc_occup_path, CON_MON_LCC_OCCUP_PATH, RESCTRL_PATH,
25 param->ctrlgrp, domain_id);
26
27 return 0;
28}
29
30static int cmt_setup(const struct resctrl_test *test,
31 const struct user_params *uparams,
32 struct resctrl_val_param *p)
33{
34 /* Run NUM_OF_RUNS times */
35 if (p->num_of_runs >= NUM_OF_RUNS)
36 return END_OF_TESTS;
37
38 p->num_of_runs++;
39
40 return 0;
41}
42
43static int cmt_measure(const struct user_params *uparams,
44 struct resctrl_val_param *param, pid_t bm_pid)
45{
46 sleep(1);
47 return measure_llc_resctrl(param->filename, bm_pid);
48}
49
50static int show_results_info(unsigned long sum_llc_val, int no_of_bits,
51 unsigned long cache_span, unsigned long max_diff,
52 unsigned long max_diff_percent, unsigned long num_of_runs,
53 bool platform)
54{
55 unsigned long avg_llc_val = 0;
56 float diff_percent;
57 long avg_diff = 0;
58 int ret;
59
60 avg_llc_val = sum_llc_val / num_of_runs;
61 avg_diff = (long)(cache_span - avg_llc_val);
62 diff_percent = ((float)cache_span - avg_llc_val) / cache_span * 100;
63
64 ret = platform && abs((int)diff_percent) > max_diff_percent &&
65 labs(avg_diff) > max_diff;
66
67 ksft_print_msg("%s Check cache miss rate within %lu%%\n",
68 ret ? "Fail:" : "Pass:", max_diff_percent);
69
70 ksft_print_msg("Percent diff=%d\n", abs((int)diff_percent));
71
72 show_cache_info(no_of_bits, avg_llc_val, cache_span, false);
73
74 return ret;
75}
76
77static int check_results(struct resctrl_val_param *param, size_t span, int no_of_bits)
78{
79 char *token_array[8], temp[512];
80 unsigned long sum_llc_occu_resc = 0;
81 int runs = 0;
82 FILE *fp;
83
84 ksft_print_msg("Checking for pass/fail\n");
85 fp = fopen(param->filename, "r");
86 if (!fp) {
87 ksft_perror("Error in opening file");
88
89 return -1;
90 }
91
92 while (fgets(temp, sizeof(temp), fp)) {
93 char *token = strtok(temp, ":\t");
94 int fields = 0;
95
96 while (token) {
97 token_array[fields++] = token;
98 token = strtok(NULL, ":\t");
99 }
100
101 /* Field 3 is llc occ resc value */
102 sum_llc_occu_resc += strtoul(token_array[3], NULL, 0);
103 runs++;
104 }
105 fclose(fp);
106
107 return show_results_info(sum_llc_occu_resc, no_of_bits, span,
108 MAX_DIFF, MAX_DIFF_PERCENT, runs, true);
109}
110
111static void cmt_test_cleanup(void)
112{
113 remove(RESULT_FILE_NAME);
114}
115
116static int cmt_run_test(const struct resctrl_test *test, const struct user_params *uparams)
117{
118 struct fill_buf_param fill_buf = {};
119 unsigned long cache_total_size = 0;
120 int n = uparams->bits ? : 5;
121 unsigned long long_mask;
122 int count_of_bits;
123 size_t span;
124 int ret;
125
126 ret = get_full_cbm("L3", &long_mask);
127 if (ret)
128 return ret;
129
130 ret = get_cache_size(uparams->cpu, "L3", &cache_total_size);
131 if (ret)
132 return ret;
133 ksft_print_msg("Cache size :%lu\n", cache_total_size);
134
135 count_of_bits = count_bits(long_mask);
136
137 if (n < 1 || n > count_of_bits) {
138 ksft_print_msg("Invalid input value for numbr_of_bits n!\n");
139 ksft_print_msg("Please enter value in range 1 to %d\n", count_of_bits);
140 return -1;
141 }
142
143 struct resctrl_val_param param = {
144 .ctrlgrp = "c1",
145 .filename = RESULT_FILE_NAME,
146 .mask = ~(long_mask << n) & long_mask,
147 .num_of_runs = 0,
148 .init = cmt_init,
149 .setup = cmt_setup,
150 .measure = cmt_measure,
151 };
152
153 span = cache_portion_size(cache_total_size, param.mask, long_mask);
154
155 if (uparams->fill_buf) {
156 fill_buf.buf_size = span;
157 fill_buf.memflush = uparams->fill_buf->memflush;
158 param.fill_buf = &fill_buf;
159 } else if (!uparams->benchmark_cmd[0]) {
160 fill_buf.buf_size = span;
161 fill_buf.memflush = true;
162 param.fill_buf = &fill_buf;
163 }
164
165 remove(RESULT_FILE_NAME);
166
167 ret = resctrl_val(test, uparams, ¶m);
168 if (ret)
169 return ret;
170
171 ret = check_results(¶m, span, n);
172 if (ret && (get_vendor() == ARCH_INTEL))
173 ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
174
175 return ret;
176}
177
178static bool cmt_feature_check(const struct resctrl_test *test)
179{
180 return test_resource_feature_check(test) &&
181 resctrl_mon_feature_exists("L3_MON", "llc_occupancy");
182}
183
184struct resctrl_test cmt_test = {
185 .name = "CMT",
186 .resource = "L3",
187 .feature_check = cmt_feature_check,
188 .run_test = cmt_run_test,
189 .cleanup = cmt_test_cleanup,
190};
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Cache Monitoring Technology (CMT) 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_NAME "result_cmt"
15#define NUM_OF_RUNS 5
16#define MAX_DIFF 2000000
17#define MAX_DIFF_PERCENT 15
18
19static int count_of_bits;
20static char cbm_mask[256];
21static unsigned long long_mask;
22static unsigned long cache_size;
23
24static int cmt_setup(int num, ...)
25{
26 struct resctrl_val_param *p;
27 va_list param;
28
29 va_start(param, num);
30 p = va_arg(param, struct resctrl_val_param *);
31 va_end(param);
32
33 /* Run NUM_OF_RUNS times */
34 if (p->num_of_runs >= NUM_OF_RUNS)
35 return -1;
36
37 p->num_of_runs++;
38
39 return 0;
40}
41
42static int check_results(struct resctrl_val_param *param, int no_of_bits)
43{
44 char *token_array[8], temp[512];
45 unsigned long sum_llc_occu_resc = 0;
46 int runs = 0;
47 FILE *fp;
48
49 ksft_print_msg("Checking for pass/fail\n");
50 fp = fopen(param->filename, "r");
51 if (!fp) {
52 perror("# Error in opening file\n");
53
54 return errno;
55 }
56
57 while (fgets(temp, sizeof(temp), fp)) {
58 char *token = strtok(temp, ":\t");
59 int fields = 0;
60
61 while (token) {
62 token_array[fields++] = token;
63 token = strtok(NULL, ":\t");
64 }
65
66 /* Field 3 is llc occ resc value */
67 if (runs > 0)
68 sum_llc_occu_resc += strtoul(token_array[3], NULL, 0);
69 runs++;
70 }
71 fclose(fp);
72
73 return show_cache_info(sum_llc_occu_resc, no_of_bits, param->span,
74 MAX_DIFF, MAX_DIFF_PERCENT, NUM_OF_RUNS,
75 true, true);
76}
77
78void cmt_test_cleanup(void)
79{
80 remove(RESULT_FILE_NAME);
81}
82
83int cmt_resctrl_val(int cpu_no, int n, char **benchmark_cmd)
84{
85 int ret, mum_resctrlfs;
86
87 cache_size = 0;
88 mum_resctrlfs = 1;
89
90 ret = remount_resctrlfs(mum_resctrlfs);
91 if (ret)
92 return ret;
93
94 if (!validate_resctrl_feature_request(CMT_STR))
95 return -1;
96
97 ret = get_cbm_mask("L3", cbm_mask);
98 if (ret)
99 return ret;
100
101 long_mask = strtoul(cbm_mask, NULL, 16);
102
103 ret = get_cache_size(cpu_no, "L3", &cache_size);
104 if (ret)
105 return ret;
106 ksft_print_msg("Cache size :%lu\n", cache_size);
107
108 count_of_bits = count_bits(long_mask);
109
110 if (n < 1 || n > count_of_bits) {
111 ksft_print_msg("Invalid input value for numbr_of_bits n!\n");
112 ksft_print_msg("Please enter value in range 1 to %d\n", count_of_bits);
113 return -1;
114 }
115
116 struct resctrl_val_param param = {
117 .resctrl_val = CMT_STR,
118 .ctrlgrp = "c1",
119 .mongrp = "m1",
120 .cpu_no = cpu_no,
121 .mum_resctrlfs = 0,
122 .filename = RESULT_FILE_NAME,
123 .mask = ~(long_mask << n) & long_mask,
124 .span = cache_size * n / count_of_bits,
125 .num_of_runs = 0,
126 .setup = cmt_setup,
127 };
128
129 if (strcmp(benchmark_cmd[0], "fill_buf") == 0)
130 sprintf(benchmark_cmd[1], "%lu", param.span);
131
132 remove(RESULT_FILE_NAME);
133
134 ret = resctrl_val(benchmark_cmd, ¶m);
135 if (ret)
136 return ret;
137
138 ret = check_results(¶m, n);
139 if (ret)
140 return ret;
141
142 cmt_test_cleanup();
143
144 return 0;
145}