Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Memory Bandwidth Allocation (MBA) test
0004  *
0005  * Copyright (C) 2018 Intel Corporation
0006  *
0007  * Authors:
0008  *    Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
0009  *    Fenghua Yu <fenghua.yu@intel.com>
0010  */
0011 #include "resctrl.h"
0012 
0013 #define RESULT_FILE_NAME    "result_mba"
0014 #define NUM_OF_RUNS     5
0015 #define MAX_DIFF_PERCENT    5
0016 #define ALLOCATION_MAX      100
0017 #define ALLOCATION_MIN      10
0018 #define ALLOCATION_STEP     10
0019 
0020 /*
0021  * Change schemata percentage from 100 to 10%. Write schemata to specified
0022  * con_mon grp, mon_grp in resctrl FS.
0023  * For each allocation, run 5 times in order to get average values.
0024  */
0025 static int mba_setup(int num, ...)
0026 {
0027     static int runs_per_allocation, allocation = 100;
0028     struct resctrl_val_param *p;
0029     char allocation_str[64];
0030     va_list param;
0031 
0032     va_start(param, num);
0033     p = va_arg(param, struct resctrl_val_param *);
0034     va_end(param);
0035 
0036     if (runs_per_allocation >= NUM_OF_RUNS)
0037         runs_per_allocation = 0;
0038 
0039     /* Only set up schemata once every NUM_OF_RUNS of allocations */
0040     if (runs_per_allocation++ != 0)
0041         return 0;
0042 
0043     if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
0044         return -1;
0045 
0046     sprintf(allocation_str, "%d", allocation);
0047 
0048     write_schemata(p->ctrlgrp, allocation_str, p->cpu_no, p->resctrl_val);
0049     allocation -= ALLOCATION_STEP;
0050 
0051     return 0;
0052 }
0053 
0054 static void show_mba_info(unsigned long *bw_imc, unsigned long *bw_resc)
0055 {
0056     int allocation, runs;
0057     bool failed = false;
0058 
0059     ksft_print_msg("Results are displayed in (MB)\n");
0060     /* Memory bandwidth from 100% down to 10% */
0061     for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP;
0062          allocation++) {
0063         unsigned long avg_bw_imc, avg_bw_resc;
0064         unsigned long sum_bw_imc = 0, sum_bw_resc = 0;
0065         int avg_diff_per;
0066         float avg_diff;
0067 
0068         /*
0069          * The first run is discarded due to inaccurate value from
0070          * phase transition.
0071          */
0072         for (runs = NUM_OF_RUNS * allocation + 1;
0073              runs < NUM_OF_RUNS * allocation + NUM_OF_RUNS ; runs++) {
0074             sum_bw_imc += bw_imc[runs];
0075             sum_bw_resc += bw_resc[runs];
0076         }
0077 
0078         avg_bw_imc = sum_bw_imc / (NUM_OF_RUNS - 1);
0079         avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1);
0080         avg_diff = (float)labs(avg_bw_resc - avg_bw_imc) / avg_bw_imc;
0081         avg_diff_per = (int)(avg_diff * 100);
0082 
0083         ksft_print_msg("%s Check MBA diff within %d%% for schemata %u\n",
0084                    avg_diff_per > MAX_DIFF_PERCENT ?
0085                    "Fail:" : "Pass:",
0086                    MAX_DIFF_PERCENT,
0087                    ALLOCATION_MAX - ALLOCATION_STEP * allocation);
0088 
0089         ksft_print_msg("avg_diff_per: %d%%\n", avg_diff_per);
0090         ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc);
0091         ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc);
0092         if (avg_diff_per > MAX_DIFF_PERCENT)
0093             failed = true;
0094     }
0095 
0096     ksft_print_msg("%s Check schemata change using MBA\n",
0097                failed ? "Fail:" : "Pass:");
0098     if (failed)
0099         ksft_print_msg("At least one test failed\n");
0100 }
0101 
0102 static int check_results(void)
0103 {
0104     char *token_array[8], output[] = RESULT_FILE_NAME, temp[512];
0105     unsigned long bw_imc[1024], bw_resc[1024];
0106     int runs;
0107     FILE *fp;
0108 
0109     fp = fopen(output, "r");
0110     if (!fp) {
0111         perror(output);
0112 
0113         return errno;
0114     }
0115 
0116     runs = 0;
0117     while (fgets(temp, sizeof(temp), fp)) {
0118         char *token = strtok(temp, ":\t");
0119         int fields = 0;
0120 
0121         while (token) {
0122             token_array[fields++] = token;
0123             token = strtok(NULL, ":\t");
0124         }
0125 
0126         /* Field 3 is perf imc value */
0127         bw_imc[runs] = strtoul(token_array[3], NULL, 0);
0128         /* Field 5 is resctrl value */
0129         bw_resc[runs] = strtoul(token_array[5], NULL, 0);
0130         runs++;
0131     }
0132 
0133     fclose(fp);
0134 
0135     show_mba_info(bw_imc, bw_resc);
0136 
0137     return 0;
0138 }
0139 
0140 void mba_test_cleanup(void)
0141 {
0142     remove(RESULT_FILE_NAME);
0143 }
0144 
0145 int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd)
0146 {
0147     struct resctrl_val_param param = {
0148         .resctrl_val    = MBA_STR,
0149         .ctrlgrp    = "c1",
0150         .mongrp     = "m1",
0151         .cpu_no     = cpu_no,
0152         .mum_resctrlfs  = 1,
0153         .filename   = RESULT_FILE_NAME,
0154         .bw_report  = bw_report,
0155         .setup      = mba_setup
0156     };
0157     int ret;
0158 
0159     remove(RESULT_FILE_NAME);
0160 
0161     ret = resctrl_val(benchmark_cmd, &param);
0162     if (ret)
0163         return ret;
0164 
0165     ret = check_results();
0166     if (ret)
0167         return ret;
0168 
0169     mba_test_cleanup();
0170 
0171     return 0;
0172 }