Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/resctrl: Call kselftest APIs to log test results

Call kselftest APIs instead of using printf() to log test results
for cleaner code and better future extension.

Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
Tested-by: Babu Moger <babu.moger@amd.com>
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Fenghua Yu and committed by
Shuah Khan
ca2f4214 2f320911

+105 -117
+19 -18
tools/testing/selftests/resctrl/cat_test.c
··· 52 52 return ret; 53 53 } 54 54 55 - static void show_cache_info(unsigned long sum_llc_perf_miss, int no_of_bits, 56 - unsigned long span) 55 + static int show_cache_info(unsigned long sum_llc_perf_miss, int no_of_bits, 56 + unsigned long span) 57 57 { 58 58 unsigned long allocated_cache_lines = span / 64; 59 59 unsigned long avg_llc_perf_miss = 0; 60 60 float diff_percent; 61 + int ret; 61 62 62 63 avg_llc_perf_miss = sum_llc_perf_miss / (NUM_OF_RUNS - 1); 63 64 diff_percent = ((float)allocated_cache_lines - avg_llc_perf_miss) / 64 65 allocated_cache_lines * 100; 65 66 66 - printf("%sok CAT: cache miss rate within %d%%\n", 67 - !is_amd && abs((int)diff_percent) > MAX_DIFF_PERCENT ? 68 - "not " : "", MAX_DIFF_PERCENT); 69 - tests_run++; 70 - printf("# Percent diff=%d\n", abs((int)diff_percent)); 71 - printf("# Number of bits: %d\n", no_of_bits); 72 - printf("# Avg_llc_perf_miss: %lu\n", avg_llc_perf_miss); 73 - printf("# Allocated cache lines: %lu\n", allocated_cache_lines); 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; 74 77 } 75 78 76 79 static int check_results(struct resctrl_val_param *param) ··· 83 80 int runs = 0, no_of_bits = 0; 84 81 FILE *fp; 85 82 86 - printf("# Checking for pass/fail\n"); 83 + ksft_print_msg("Checking for pass/fail\n"); 87 84 fp = fopen(param->filename, "r"); 88 85 if (!fp) { 89 86 perror("# Cannot open file"); ··· 111 108 fclose(fp); 112 109 no_of_bits = count_bits(param->mask); 113 110 114 - show_cache_info(sum_llc_perf_miss, no_of_bits, param->span); 115 - 116 - return 0; 111 + return show_cache_info(sum_llc_perf_miss, no_of_bits, param->span); 117 112 } 118 113 119 114 void cat_test_cleanup(void) ··· 147 146 ret = get_cache_size(cpu_no, cache_type, &cache_size); 148 147 if (ret) 149 148 return ret; 150 - printf("cache size :%lu\n", cache_size); 149 + ksft_print_msg("Cache size :%lu\n", cache_size); 151 150 152 151 /* Get max number of bits from default-cabm mask */ 153 152 count_of_bits = count_bits(long_mask); 154 153 155 154 if (n < 1 || n > count_of_bits - 1) { 156 - printf("Invalid input value for no_of_bits n!\n"); 157 - printf("Please Enter value in range 1 to %d\n", 158 - count_of_bits - 1); 155 + ksft_print_msg("Invalid input value for no_of_bits n!\n"); 156 + ksft_print_msg("Please enter value in range 1 to %d\n", 157 + count_of_bits - 1); 159 158 return -1; 160 159 } 161 160
+19 -23
tools/testing/selftests/resctrl/cmt_test.c
··· 39 39 return 0; 40 40 } 41 41 42 - static void show_cache_info(unsigned long sum_llc_occu_resc, int no_of_bits, 43 - unsigned long span) 42 + static int show_cache_info(unsigned long sum_llc_occu_resc, int no_of_bits, 43 + unsigned long span) 44 44 { 45 45 unsigned long avg_llc_occu_resc = 0; 46 46 float diff_percent; 47 47 long avg_diff = 0; 48 - bool res; 48 + int ret; 49 49 50 50 avg_llc_occu_resc = sum_llc_occu_resc / (NUM_OF_RUNS - 1); 51 51 avg_diff = (long)abs(span - avg_llc_occu_resc); 52 52 53 53 diff_percent = (((float)span - avg_llc_occu_resc) / span) * 100; 54 54 55 - if ((abs((int)diff_percent) <= MAX_DIFF_PERCENT) || 56 - (abs(avg_diff) <= MAX_DIFF)) 57 - res = true; 58 - else 59 - res = false; 55 + ret = (abs((int)diff_percent) > MAX_DIFF_PERCENT) && 56 + (abs(avg_diff) > MAX_DIFF); 60 57 61 - printf("%sok CMT: diff within %d, %d\%%\n", res ? "" : "not", 62 - MAX_DIFF, (int)MAX_DIFF_PERCENT); 58 + ksft_print_msg("%s cache miss diff within %d, %d\%%\n", 59 + ret ? "Fail:" : "Pass:", MAX_DIFF, (int)MAX_DIFF_PERCENT); 63 60 64 - printf("# diff: %ld\n", avg_diff); 65 - printf("# percent diff=%d\n", abs((int)diff_percent)); 66 - printf("# Results are displayed in (Bytes)\n"); 67 - printf("# Number of bits: %d\n", no_of_bits); 68 - printf("# Avg_llc_occu_resc: %lu\n", avg_llc_occu_resc); 69 - printf("# llc_occu_exp (span): %lu\n", span); 61 + ksft_print_msg("Diff: %ld\n", avg_diff); 62 + ksft_print_msg("Percent diff=%d\n", abs((int)diff_percent)); 63 + ksft_print_msg("Results are displayed in (Bytes)\n"); 64 + ksft_print_msg("Number of bits: %d\n", no_of_bits); 65 + ksft_print_msg("Avg_llc_occu_resc: %lu\n", avg_llc_occu_resc); 66 + ksft_print_msg("llc_occu_exp (span): %lu\n", span); 70 67 71 - tests_run++; 68 + return ret; 72 69 } 73 70 74 71 static int check_results(struct resctrl_val_param *param, int no_of_bits) ··· 75 78 int runs = 0; 76 79 FILE *fp; 77 80 78 - printf("# checking for pass/fail\n"); 81 + ksft_print_msg("Checking for pass/fail\n"); 79 82 fp = fopen(param->filename, "r"); 80 83 if (!fp) { 81 84 perror("# Error in opening file\n"); ··· 98 101 runs++; 99 102 } 100 103 fclose(fp); 101 - show_cache_info(sum_llc_occu_resc, no_of_bits, param->span); 102 104 103 - return 0; 105 + return show_cache_info(sum_llc_occu_resc, no_of_bits, param->span); 104 106 } 105 107 106 108 void cmt_test_cleanup(void) ··· 130 134 ret = get_cache_size(cpu_no, "L3", &cache_size); 131 135 if (ret) 132 136 return ret; 133 - printf("cache size :%lu\n", cache_size); 137 + ksft_print_msg("Cache size :%lu\n", cache_size); 134 138 135 139 count_of_bits = count_bits(long_mask); 136 140 137 141 if (n < 1 || n > count_of_bits) { 138 - printf("Invalid input value for numbr_of_bits n!\n"); 139 - printf("Please Enter value in range 1 to %d\n", count_of_bits); 142 + ksft_print_msg("Invalid input value for numbr_of_bits n!\n"); 143 + ksft_print_msg("Please enter value in range 1 to %d\n", count_of_bits); 140 144 return -1; 141 145 } 142 146
+12 -12
tools/testing/selftests/resctrl/mba_test.c
··· 56 56 int allocation, runs; 57 57 bool failed = false; 58 58 59 - printf("# Results are displayed in (MB)\n"); 59 + ksft_print_msg("Results are displayed in (MB)\n"); 60 60 /* Memory bandwidth from 100% down to 10% */ 61 61 for (allocation = 0; allocation < ALLOCATION_MAX / ALLOCATION_STEP; 62 62 allocation++) { ··· 78 78 avg_bw_resc = sum_bw_resc / (NUM_OF_RUNS - 1); 79 79 avg_diff = labs((long)(avg_bw_resc - avg_bw_imc)); 80 80 81 - printf("%sok MBA schemata percentage %u smaller than %d %%\n", 82 - avg_diff > MAX_DIFF ? "not " : "", 83 - ALLOCATION_MAX - ALLOCATION_STEP * allocation, 84 - MAX_DIFF); 85 - tests_run++; 86 - printf("# avg_diff: %lu\n", avg_diff); 87 - printf("# avg_bw_imc: %lu\n", avg_bw_imc); 88 - printf("# avg_bw_resc: %lu\n", avg_bw_resc); 81 + ksft_print_msg("%s MBA schemata percentage %u smaller than %d %%\n", 82 + avg_diff > MAX_DIFF ? "Fail:" : "Pass:", 83 + ALLOCATION_MAX - ALLOCATION_STEP * allocation, 84 + MAX_DIFF); 85 + ksft_print_msg("avg_diff: %lu\n", avg_diff); 86 + ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc); 87 + ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc); 89 88 if (avg_diff > MAX_DIFF) 90 89 failed = true; 91 90 } 92 91 93 - printf("%sok schemata change using MBA%s\n", failed ? "not " : "", 94 - failed ? " # at least one test failed" : ""); 95 - tests_run++; 92 + ksft_print_msg("%s schemata change using MBA\n", 93 + failed ? "Fail:" : "Pass:"); 94 + if (failed) 95 + ksft_print_msg("At least one test failed"); 96 96 } 97 97 98 98 static int check_results(void)
+15 -13
tools/testing/selftests/resctrl/mbm_test.c
··· 14 14 #define MAX_DIFF 300 15 15 #define NUM_OF_RUNS 5 16 16 17 - static void 17 + static int 18 18 show_bw_info(unsigned long *bw_imc, unsigned long *bw_resc, int span) 19 19 { 20 20 unsigned long avg_bw_imc = 0, avg_bw_resc = 0; 21 21 unsigned long sum_bw_imc = 0, sum_bw_resc = 0; 22 22 long avg_diff = 0; 23 - int runs; 23 + int runs, ret; 24 24 25 25 /* 26 26 * Discard the first value which is inaccurate due to monitoring setup ··· 35 35 avg_bw_resc = sum_bw_resc / 4; 36 36 avg_diff = avg_bw_resc - avg_bw_imc; 37 37 38 - printf("%sok MBM: diff within %d%%\n", 39 - labs(avg_diff) > MAX_DIFF ? "not " : "", MAX_DIFF); 40 - tests_run++; 41 - printf("# avg_diff: %lu\n", labs(avg_diff)); 42 - printf("# Span (MB): %d\n", span); 43 - printf("# avg_bw_imc: %lu\n", avg_bw_imc); 44 - printf("# avg_bw_resc: %lu\n", avg_bw_resc); 38 + ret = labs(avg_diff) > MAX_DIFF; 39 + ksft_print_msg("%s MBM: diff within %d%%\n", 40 + ret ? "Fail:" : "Pass:", MAX_DIFF); 41 + ksft_print_msg("avg_diff: %lu\n", labs(avg_diff)); 42 + ksft_print_msg("Span (MB): %d\n", span); 43 + ksft_print_msg("avg_bw_imc: %lu\n", avg_bw_imc); 44 + ksft_print_msg("avg_bw_resc: %lu\n", avg_bw_resc); 45 + 46 + return ret; 45 47 } 46 48 47 49 static int check_results(int span) ··· 51 49 unsigned long bw_imc[NUM_OF_RUNS], bw_resc[NUM_OF_RUNS]; 52 50 char temp[1024], *token_array[8]; 53 51 char output[] = RESULT_FILE_NAME; 54 - int runs; 52 + int runs, ret; 55 53 FILE *fp; 56 54 57 - printf("# Checking for pass/fail\n"); 55 + ksft_print_msg("Checking for pass/fail\n"); 58 56 59 57 fp = fopen(output, "r"); 60 58 if (!fp) { ··· 78 76 runs++; 79 77 } 80 78 81 - show_bw_info(bw_imc, bw_resc, span); 79 + ret = show_bw_info(bw_imc, bw_resc, span); 82 80 83 81 fclose(fp); 84 82 85 - return 0; 83 + return ret; 86 84 } 87 85 88 86 static int mbm_setup(int num, ...)
+1 -1
tools/testing/selftests/resctrl/resctrl.h
··· 23 23 #include <sys/eventfd.h> 24 24 #include <asm/unistd.h> 25 25 #include <linux/perf_event.h> 26 + #include "../kselftest.h" 26 27 27 28 #define MB (1024 * 1024) 28 29 #define RESCTRL_PATH "/sys/fs/resctrl" ··· 69 68 #define CAT_STR "cat" 70 69 71 70 extern pid_t bm_pid, ppid; 72 - extern int tests_run; 73 71 74 72 extern char llc_occup_path[1024]; 75 73 extern bool is_amd;
+21 -19
tools/testing/selftests/resctrl/resctrl_tests.c
··· 60 60 int res, c, cpu_no = 1, span = 250, argc_new = argc, i, no_of_bits = 5; 61 61 char *benchmark_cmd[BENCHMARK_ARGS], bw_report[64], bm_type[64]; 62 62 char benchmark_cmd_area[BENCHMARK_ARGS][BENCHMARK_ARG_SIZE]; 63 - int ben_ind, ben_count; 63 + int ben_ind, ben_count, tests = 0; 64 64 bool cat_test = true; 65 65 66 66 for (i = 0; i < argc; i++) { ··· 87 87 while (token) { 88 88 if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) { 89 89 mbm_test = true; 90 + tests++; 90 91 } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) { 91 92 mba_test = true; 93 + tests++; 92 94 } else if (!strncmp(token, CMT_STR, sizeof(CMT_STR))) { 93 95 cmt_test = true; 96 + tests++; 94 97 } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) { 95 98 cat_test = true; 99 + tests++; 96 100 } else { 97 101 printf("invalid argument\n"); 98 102 ··· 122 118 } 123 119 } 124 120 125 - printf("TAP version 13\n"); 121 + ksft_print_header(); 126 122 127 123 /* 128 124 * Typically we need root privileges, because: ··· 130 126 * 2. We execute perf commands 131 127 */ 132 128 if (geteuid() != 0) 133 - printf("# WARNING: not running as root, tests may fail.\n"); 129 + return ksft_exit_fail_msg("Not running as root, abort testing.\n"); 134 130 135 131 /* Detect AMD vendor */ 136 132 detect_amd(); ··· 159 155 sprintf(bw_report, "reads"); 160 156 sprintf(bm_type, "fill_buf"); 161 157 162 - check_resctrlfs_support(); 158 + if (!check_resctrlfs_support()) 159 + return ksft_exit_fail_msg("resctrl FS does not exist\n"); 160 + 163 161 filter_dmesg(); 164 162 163 + ksft_set_plan(tests ? : 4); 164 + 165 165 if (!is_amd && mbm_test) { 166 - printf("# Starting MBM BW change ...\n"); 166 + ksft_print_msg("Starting MBM BW change ...\n"); 167 167 if (!has_ben) 168 168 sprintf(benchmark_cmd[5], "%s", MBA_STR); 169 169 res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd); 170 - printf("%sok MBM: bw change\n", res ? "not " : ""); 170 + ksft_test_result(!res, "MBM: bw change\n"); 171 171 mbm_test_cleanup(); 172 - tests_run++; 173 172 } 174 173 175 174 if (!is_amd && mba_test) { 176 - printf("# Starting MBA Schemata change ...\n"); 175 + ksft_print_msg("Starting MBA Schemata change ...\n"); 177 176 if (!has_ben) 178 177 sprintf(benchmark_cmd[1], "%d", span); 179 178 res = mba_schemata_change(cpu_no, bw_report, benchmark_cmd); 180 - printf("%sok MBA: schemata change\n", res ? "not " : ""); 179 + ksft_test_result(!res, "MBA: schemata change\n"); 181 180 mba_test_cleanup(); 182 - tests_run++; 183 181 } 184 182 185 183 if (cmt_test) { 186 - printf("# Starting CMT test ...\n"); 184 + ksft_print_msg("Starting CMT test ...\n"); 187 185 if (!has_ben) 188 186 sprintf(benchmark_cmd[5], "%s", CMT_STR); 189 187 res = cmt_resctrl_val(cpu_no, no_of_bits, benchmark_cmd); 190 - printf("%sok CMT: test\n", res ? "not " : ""); 188 + ksft_test_result(!res, "CMT: test\n"); 191 189 cmt_test_cleanup(); 192 - tests_run++; 193 190 } 194 191 195 192 if (cat_test) { 196 - printf("# Starting CAT test ...\n"); 193 + ksft_print_msg("Starting CAT test ...\n"); 197 194 res = cat_perf_miss_val(cpu_no, no_of_bits, "L3"); 198 - printf("%sok CAT: test\n", res ? "not " : ""); 199 - tests_run++; 195 + ksft_test_result(!res, "CAT: test\n"); 200 196 cat_test_cleanup(); 201 197 } 202 198 203 - printf("1..%d\n", tests_run); 204 - 205 - return 0; 199 + return ksft_exit_pass(); 206 200 }
+2 -2
tools/testing/selftests/resctrl/resctrl_val.c
··· 449 449 kill(bm_pid, SIGKILL); 450 450 umount_resctrlfs(); 451 451 tests_cleanup(); 452 - printf("Ending\n\n"); 452 + ksft_print_msg("Ending\n\n"); 453 453 454 454 exit(EXIT_SUCCESS); 455 455 } ··· 645 645 PARENT_EXIT("Child is done"); 646 646 } 647 647 648 - printf("# benchmark PID: %d\n", bm_pid); 648 + ksft_print_msg("Benchmark PID: %d\n", bm_pid); 649 649 650 650 /* 651 651 * Register CTRL-C handler for parent, as it has to kill benchmark
+16 -29
tools/testing/selftests/resctrl/resctrlfs.c
··· 10 10 */ 11 11 #include "resctrl.h" 12 12 13 - int tests_run; 14 - 15 13 static int find_resctrl_mount(char *buffer) 16 14 { 17 15 FILE *mounts; ··· 66 68 if (ret) 67 69 strcpy(mountpoint, RESCTRL_PATH); 68 70 69 - if (!ret && mum_resctrlfs && umount(mountpoint)) { 70 - printf("not ok unmounting \"%s\"\n", mountpoint); 71 - perror("# umount"); 72 - tests_run++; 73 - } 71 + if (!ret && mum_resctrlfs && umount(mountpoint)) 72 + ksft_print_msg("Fail: unmounting \"%s\"\n", mountpoint); 74 73 75 74 if (!ret && !mum_resctrlfs) 76 75 return 0; 77 76 77 + ksft_print_msg("Mounting resctrl to \"%s\"\n", RESCTRL_PATH); 78 78 ret = mount("resctrl", RESCTRL_PATH, "resctrl", 0, NULL); 79 - printf("%sok mounting resctrl to \"%s\"\n", ret ? "not " : "", 80 - RESCTRL_PATH); 81 79 if (ret) 82 80 perror("# mount"); 83 - 84 - tests_run++; 85 81 86 82 return ret; 87 83 } ··· 469 477 } 470 478 471 479 out: 472 - printf("%sok writing benchmark parameters to resctrl FS\n", 473 - ret ? "not " : ""); 480 + ksft_print_msg("Writing benchmark parameters to resctrl FS\n"); 474 481 if (ret) 475 482 perror("# writing to resctrlfs"); 476 - 477 - tests_run++; 478 483 479 484 return ret; 480 485 } ··· 500 511 return -ENOENT; 501 512 502 513 if (!schemata) { 503 - printf("# Skipping empty schemata update\n"); 514 + ksft_print_msg("Skipping empty schemata update\n"); 504 515 505 516 return -1; 506 517 } ··· 541 552 fclose(fp); 542 553 543 554 out: 544 - printf("%sok Write schema \"%s\" to resctrl FS%s%s\n", 545 - ret ? "not " : "", schema, ret ? " # " : "", 546 - ret ? reason : ""); 547 - tests_run++; 555 + ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n", 556 + schema, ret ? " # " : "", 557 + ret ? reason : ""); 548 558 549 559 return ret; 550 560 } ··· 567 579 568 580 fclose(inf); 569 581 570 - printf("%sok kernel supports resctrl filesystem\n", ret ? "" : "not "); 571 - tests_run++; 582 + ksft_print_msg("%s kernel supports resctrl filesystem\n", 583 + ret ? "Pass:" : "Fail:"); 572 584 573 585 dp = opendir(RESCTRL_PATH); 574 - printf("%sok resctrl mountpoint \"%s\" exists\n", 575 - dp ? "" : "not ", RESCTRL_PATH); 586 + ksft_print_msg("%s resctrl mountpoint \"%s\" exists\n", 587 + dp ? "Pass:" : "Fail:", RESCTRL_PATH); 576 588 if (dp) 577 589 closedir(dp); 578 - tests_run++; 579 590 580 - printf("# resctrl filesystem %s mounted\n", 581 - find_resctrl_mount(NULL) ? "not" : "is"); 591 + ksft_print_msg("resctrl filesystem %s mounted\n", 592 + find_resctrl_mount(NULL) ? "not" : "is"); 582 593 583 594 return ret; 584 595 } ··· 659 672 660 673 while (fgets(line, 1024, fp)) { 661 674 if (strstr(line, "intel_rdt:")) 662 - printf("# dmesg: %s", line); 675 + ksft_print_msg("dmesg: %s", line); 663 676 if (strstr(line, "resctrl:")) 664 - printf("# dmesg: %s", line); 677 + ksft_print_msg("dmesg: %s", line); 665 678 } 666 679 fclose(fp); 667 680 waitpid(pid, NULL, 0);