101fee6b4SFenghua Yu // SPDX-License-Identifier: GPL-2.0
201fee6b4SFenghua Yu /*
301fee6b4SFenghua Yu * Memory Bandwidth Allocation (MBA) test
401fee6b4SFenghua Yu *
501fee6b4SFenghua Yu * Copyright (C) 2018 Intel Corporation
601fee6b4SFenghua Yu *
701fee6b4SFenghua Yu * Authors:
801fee6b4SFenghua Yu * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
901fee6b4SFenghua Yu * Fenghua Yu <fenghua.yu@intel.com>
1001fee6b4SFenghua Yu */
1101fee6b4SFenghua Yu #include "resctrl.h"
1201fee6b4SFenghua Yu
1301fee6b4SFenghua Yu #define RESULT_FILE_NAME "result_mba"
1401fee6b4SFenghua Yu #define NUM_OF_RUNS 5
15ef43c308SIlpo Järvinen #define MAX_DIFF_PERCENT 8
1601fee6b4SFenghua Yu #define ALLOCATION_MAX 100
1701fee6b4SFenghua Yu #define ALLOCATION_MIN 10
1801fee6b4SFenghua Yu #define ALLOCATION_STEP 10
1901fee6b4SFenghua Yu
mba_init(const struct resctrl_val_param * param,int domain_id)20aef5efa6SIlpo Järvinen static int mba_init(const struct resctrl_val_param *param, int domain_id)
21aef5efa6SIlpo Järvinen {
22aef5efa6SIlpo Järvinen int ret;
23aef5efa6SIlpo Järvinen
24aef5efa6SIlpo Järvinen ret = initialize_mem_bw_imc();
25aef5efa6SIlpo Järvinen if (ret)
26aef5efa6SIlpo Järvinen return ret;
27aef5efa6SIlpo Järvinen
28aef5efa6SIlpo Järvinen initialize_mem_bw_resctrl(param, domain_id);
29aef5efa6SIlpo Järvinen
30aef5efa6SIlpo Järvinen return 0;
31aef5efa6SIlpo Järvinen }
32aef5efa6SIlpo Järvinen
3301fee6b4SFenghua Yu /*
3401fee6b4SFenghua Yu * Change schemata percentage from 100 to 10%. Write schemata to specified
3501fee6b4SFenghua Yu * con_mon grp, mon_grp in resctrl FS.
3601fee6b4SFenghua Yu * For each allocation, run 5 times in order to get average values.
3701fee6b4SFenghua Yu */
mba_setup(const struct resctrl_test * test,const struct user_params * uparams,struct resctrl_val_param * p)38ca160887SIlpo Järvinen static int mba_setup(const struct resctrl_test *test,
39ca160887SIlpo Järvinen const struct user_params *uparams,
40ca160887SIlpo Järvinen struct resctrl_val_param *p)
4101fee6b4SFenghua Yu {
4201fee6b4SFenghua Yu static int runs_per_allocation, allocation = 100;
4301fee6b4SFenghua Yu char allocation_str[64];
440d45c83bSIlpo Järvinen int ret;
4501fee6b4SFenghua Yu
4601fee6b4SFenghua Yu if (runs_per_allocation >= NUM_OF_RUNS)
4701fee6b4SFenghua Yu runs_per_allocation = 0;
4801fee6b4SFenghua Yu
4901fee6b4SFenghua Yu /* Only set up schemata once every NUM_OF_RUNS of allocations */
5001fee6b4SFenghua Yu if (runs_per_allocation++ != 0)
5101fee6b4SFenghua Yu return 0;
5201fee6b4SFenghua Yu
5301fee6b4SFenghua Yu if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
54fa10366cSIlpo Järvinen return END_OF_TESTS;
5501fee6b4SFenghua Yu
5601fee6b4SFenghua Yu sprintf(allocation_str, "%d", allocation);
5701fee6b4SFenghua Yu
58ca160887SIlpo Järvinen ret = write_schemata(p->ctrlgrp, allocation_str, uparams->cpu, test->resource);
590d45c83bSIlpo Järvinen if (ret < 0)
600d45c83bSIlpo Järvinen return ret;
610d45c83bSIlpo Järvinen
6201fee6b4SFenghua Yu allocation -= ALLOCATION_STEP;
6301fee6b4SFenghua Yu
6401fee6b4SFenghua Yu return 0;
6501fee6b4SFenghua Yu }
6601fee6b4SFenghua Yu
mba_measure(const struct user_params * uparams,struct resctrl_val_param * param,pid_t bm_pid)670e251816SIlpo Järvinen static int mba_measure(const struct user_params *uparams,
680e251816SIlpo Järvinen struct resctrl_val_param *param, pid_t bm_pid)
690e251816SIlpo Järvinen {
70*fa1116d0SIlpo Järvinen return measure_mem_bw(uparams, param, bm_pid, "reads");
710e251816SIlpo Järvinen }
720e251816SIlpo Järvinen
show_mba_info(unsigned long * bw_imc,unsigned long * bw_resc)731e359b6aSShaopeng Tan static bool show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
7401fee6b4SFenghua Yu {
7501fee6b4SFenghua Yu int allocation, runs;
761e359b6aSShaopeng Tan bool ret = false;
7701fee6b4SFenghua Yu
78ca2f4214SFenghua Yu ksft_print_msg("Results are displayed in (MB)\n");
7901fee6b4SFenghua Yu /* Memory bandwidth from 100% down to 10% */
8001fee6b4SFenghua Yu for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
8101fee6b4SFenghua Yu allocation++) {
8201fee6b4SFenghua Yu unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
8314d28ec6SJohn Hubbard long avg_bw_imc, avg_bw_resc;
8406bd03a5SFenghua Yu int avg_diff_per;
8506bd03a5SFenghua Yu float avg_diff;
8601fee6b4SFenghua Yu
8701fee6b4SFenghua Yu /*
8801fee6b4SFenghua Yu * The first run is discarded due to inaccurate value from
8901fee6b4SFenghua Yu * phase transition.
9001fee6b4SFenghua Yu */
9101fee6b4SFenghua Yu for (runs = NUM_OF_RUNS * allocation + 1;
9201fee6b4SFenghua Yu runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
9301fee6b4SFenghua Yu sum_bw_imc += bw_imc[runs];
9401fee6b4SFenghua Yu sum_bw_resc += bw_resc[runs];
9501fee6b4SFenghua Yu }
9601fee6b4SFenghua Yu
9701fee6b4SFenghua Yu avg_bw_imc = sum_bw_imc / (NUM_OF_RUNS - 1);
9801fee6b4SFenghua Yu avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1);
9906bd03a5SFenghua Yu avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
10006bd03a5SFenghua Yu avg_diff_per = (int)(avg_diff * 100);
10101fee6b4SFenghua Yu
102e7507478SFenghua Yu ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
10306bd03a5SFenghua Yu avg_diff_per > MAX_DIFF_PERCENT ?
10406bd03a5SFenghua Yu "Fail:" : "Pass:",
10506bd03a5SFenghua Yu MAX_DIFF_PERCENT,
10606bd03a5SFenghua Yu ALLOCATION_MAX - ALLOCATION_STEP * allocation);
10706bd03a5SFenghua Yu
10806bd03a5SFenghua Yu ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
109ca2f4214SFenghua Yu ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
110ca2f4214SFenghua Yu ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
11106bd03a5SFenghua Yu if (avg_diff_per > MAX_DIFF_PERCENT)
1121e359b6aSShaopeng Tan ret = true;
11301fee6b4SFenghua Yu }
11401fee6b4SFenghua Yu
115e7507478SFenghua Yu ksft_print_msg("%s Check schemata change using MBA\n",
1161e359b6aSShaopeng Tan ret ? "Fail:" : "Pass:");
1171e359b6aSShaopeng Tan if (ret)
118e7507478SFenghua Yu ksft_print_msg("At least one test failed\n");
1191e359b6aSShaopeng Tan
1201e359b6aSShaopeng Tan return ret;
12101fee6b4SFenghua Yu }
12201fee6b4SFenghua Yu
check_results(void)12301fee6b4SFenghua Yu static int check_results(void)
12401fee6b4SFenghua Yu {
12501fee6b4SFenghua Yu char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
12601fee6b4SFenghua Yu unsigned long bw_imc[1024], bw_resc[1024];
12701fee6b4SFenghua Yu int runs;
12801fee6b4SFenghua Yu FILE *fp;
12901fee6b4SFenghua Yu
13001fee6b4SFenghua Yu fp = fopen(output, "r");
13101fee6b4SFenghua Yu if (!fp) {
132cc8ff7f5SIlpo Järvinen ksft_perror(output);
13301fee6b4SFenghua Yu
134c90fba60SIlpo Järvinen return -1;
13501fee6b4SFenghua Yu }
13601fee6b4SFenghua Yu
13701fee6b4SFenghua Yu runs = 0;
13801fee6b4SFenghua Yu while (fgets(temp, sizeof(temp), fp)) {
13901fee6b4SFenghua Yu char *token = strtok(temp, ":\t");
14001fee6b4SFenghua Yu int fields = 0;
14101fee6b4SFenghua Yu
14201fee6b4SFenghua Yu while (token) {
14301fee6b4SFenghua Yu token_array[fields++] = token;
14401fee6b4SFenghua Yu token = strtok(NULL, ":\t");
14501fee6b4SFenghua Yu }
14601fee6b4SFenghua Yu
14701fee6b4SFenghua Yu /* Field 3 is perf imc value */
14801fee6b4SFenghua Yu bw_imc[runs] = strtoul(token_array[3], NULL, 0);
14901fee6b4SFenghua Yu /* Field 5 is resctrl value */
15001fee6b4SFenghua Yu bw_resc[runs] = strtoul(token_array[5], NULL, 0);
15101fee6b4SFenghua Yu runs++;
15201fee6b4SFenghua Yu }
15301fee6b4SFenghua Yu
15401fee6b4SFenghua Yu fclose(fp);
15501fee6b4SFenghua Yu
1561e359b6aSShaopeng Tan return show_mba_info(bw_imc, bw_resc);
15701fee6b4SFenghua Yu }
15801fee6b4SFenghua Yu
mba_test_cleanup(void)1596cd36898SMaciej Wieczor-Retman static void mba_test_cleanup(void)
16001fee6b4SFenghua Yu {
16101fee6b4SFenghua Yu remove(RESULT_FILE_NAME);
16201fee6b4SFenghua Yu }
16301fee6b4SFenghua Yu
mba_run_test(const struct resctrl_test * test,const struct user_params * uparams)164c603ff5bSIlpo Järvinen static int mba_run_test(const struct resctrl_test *test, const struct user_params *uparams)
16501fee6b4SFenghua Yu {
16601fee6b4SFenghua Yu struct resctrl_val_param param = {
16701fee6b4SFenghua Yu .ctrlgrp = "c1",
16801fee6b4SFenghua Yu .filename = RESULT_FILE_NAME,
169aef5efa6SIlpo Järvinen .init = mba_init,
1700e251816SIlpo Järvinen .setup = mba_setup,
1710e251816SIlpo Järvinen .measure = mba_measure,
17201fee6b4SFenghua Yu };
17301fee6b4SFenghua Yu int ret;
17401fee6b4SFenghua Yu
17501fee6b4SFenghua Yu remove(RESULT_FILE_NAME);
17601fee6b4SFenghua Yu
177ca160887SIlpo Järvinen ret = resctrl_val(test, uparams, uparams->benchmark_cmd, ¶m);
17801fee6b4SFenghua Yu if (ret)
1796cd36898SMaciej Wieczor-Retman return ret;
18001fee6b4SFenghua Yu
18101fee6b4SFenghua Yu ret = check_results();
18201fee6b4SFenghua Yu
18391db4fd9SShaopeng Tan return ret;
18401fee6b4SFenghua Yu }
185c603ff5bSIlpo Järvinen
mba_feature_check(const struct resctrl_test * test)186c603ff5bSIlpo Järvinen static bool mba_feature_check(const struct resctrl_test *test)
187c603ff5bSIlpo Järvinen {
188c603ff5bSIlpo Järvinen return test_resource_feature_check(test) &&
18900616416SMaciej Wieczor-Retman resctrl_mon_feature_exists("L3_MON", "mbm_local_bytes");
190c603ff5bSIlpo Järvinen }
191c603ff5bSIlpo Järvinen
192c603ff5bSIlpo Järvinen struct resctrl_test mba_test = {
193c603ff5bSIlpo Järvinen .name = "MBA",
194c603ff5bSIlpo Järvinen .resource = "MB",
195c603ff5bSIlpo Järvinen .vendor_specific = ARCH_INTEL,
196c603ff5bSIlpo Järvinen .feature_check = mba_feature_check,
197c603ff5bSIlpo Järvinen .run_test = mba_run_test,
1988780bc88SMaciej Wieczor-Retman .cleanup = mba_test_cleanup,
199c603ff5bSIlpo Järvinen };
200