Skip to content

Commit 384dc56

Browse files
ij-intelgregkh
authored andcommitted
selftests/resctrl: Convert perror() to ksft_perror() or ksft_print_msg()
[ Upstream commit cc8ff7f ] The resctrl selftest code contains a number of perror() calls. Some of them come with hash character and some don't. The kselftest framework provides ksft_perror() that is compatible with test output formatting so it should be used instead of adding custom hash signs. Some perror() calls are too far away from anything that sets error. For those call sites, ksft_print_msg() must be used instead. Convert perror() to ksft_perror() or ksft_print_msg(). Other related changes: - Remove hash signs - Remove trailing stops & newlines from ksft_perror() - Add terminating newlines for converted ksft_print_msg() - Use consistent capitalization - Small fixes/tweaks to typos & grammar of the messages - Extract error printing out of PARENT_EXIT() to be able to differentiate Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org> Stable-dep-of: c44000b ("selftests/resctrl: Fix closing IMC fds on error and open-code R+W instead of loops") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent ec204ab commit 384dc56

File tree

9 files changed

+77
-70
lines changed

9 files changed

+77
-70
lines changed

tools/testing/selftests/resctrl/cache.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static int perf_event_open_llc_miss(pid_t pid, int cpu_no)
4040
fd_lm = perf_event_open(&pea_llc_miss, pid, cpu_no, -1,
4141
PERF_FLAG_FD_CLOEXEC);
4242
if (fd_lm == -1) {
43-
perror("Error opening leader");
43+
ksft_perror("Error opening leader");
4444
ctrlc_handler(0, NULL, NULL);
4545
return -1;
4646
}
@@ -95,7 +95,7 @@ static int get_llc_perf(unsigned long *llc_perf_miss)
9595

9696
ret = read(fd_lm, &rf_cqm, sizeof(struct read_format));
9797
if (ret == -1) {
98-
perror("Could not get llc misses through perf");
98+
ksft_perror("Could not get llc misses through perf");
9999
return -1;
100100
}
101101

@@ -124,12 +124,12 @@ static int get_llc_occu_resctrl(unsigned long *llc_occupancy)
124124

125125
fp = fopen(llc_occup_path, "r");
126126
if (!fp) {
127-
perror("Failed to open results file");
127+
ksft_perror("Failed to open results file");
128128

129129
return errno;
130130
}
131131
if (fscanf(fp, "%lu", llc_occupancy) <= 0) {
132-
perror("Could not get llc occupancy");
132+
ksft_perror("Could not get llc occupancy");
133133
fclose(fp);
134134

135135
return -1;
@@ -159,7 +159,7 @@ static int print_results_cache(char *filename, int bm_pid,
159159
} else {
160160
fp = fopen(filename, "a");
161161
if (!fp) {
162-
perror("Cannot open results file");
162+
ksft_perror("Cannot open results file");
163163

164164
return errno;
165165
}

tools/testing/selftests/resctrl/cat_test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static int check_results(struct resctrl_val_param *param, size_t span)
5151
ksft_print_msg("Checking for pass/fail\n");
5252
fp = fopen(param->filename, "r");
5353
if (!fp) {
54-
perror("# Cannot open file");
54+
ksft_perror("Cannot open file");
5555

5656
return errno;
5757
}
@@ -149,7 +149,7 @@ int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
149149
param.num_of_runs = 0;
150150

151151
if (pipe(pipefd)) {
152-
perror("# Unable to create pipe");
152+
ksft_perror("Unable to create pipe");
153153
return errno;
154154
}
155155

@@ -185,7 +185,7 @@ int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
185185
* Just print the error message.
186186
* Let while(1) run and wait for itself to be killed.
187187
*/
188-
perror("# failed signaling parent process");
188+
ksft_perror("Failed signaling parent process");
189189

190190
close(pipefd[1]);
191191
while (1)
@@ -197,7 +197,7 @@ int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
197197
while (pipe_message != 1) {
198198
if (read(pipefd[0], &pipe_message,
199199
sizeof(pipe_message)) < sizeof(pipe_message)) {
200-
perror("# failed reading from child process");
200+
ksft_perror("Failed reading from child process");
201201
break;
202202
}
203203
}

tools/testing/selftests/resctrl/cmt_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static int check_results(struct resctrl_val_param *param, size_t span, int no_of
3737
ksft_print_msg("Checking for pass/fail\n");
3838
fp = fopen(param->filename, "r");
3939
if (!fp) {
40-
perror("# Error in opening file\n");
40+
ksft_perror("Error in opening file");
4141

4242
return errno;
4343
}

tools/testing/selftests/resctrl/fill_buf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static int fill_cache_read(unsigned char *buf, size_t buf_size, bool once)
115115
/* Consume read result so that reading memory is not optimized out. */
116116
fp = fopen("/dev/null", "w");
117117
if (!fp) {
118-
perror("Unable to write to /dev/null");
118+
ksft_perror("Unable to write to /dev/null");
119119
return -1;
120120
}
121121
fprintf(fp, "Sum: %d ", ret);

tools/testing/selftests/resctrl/mba_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static int check_results(void)
109109

110110
fp = fopen(output, "r");
111111
if (!fp) {
112-
perror(output);
112+
ksft_perror(output);
113113

114114
return errno;
115115
}

tools/testing/selftests/resctrl/mbm_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int check_results(size_t span)
5959

6060
fp = fopen(output, "r");
6161
if (!fp) {
62-
perror(output);
62+
ksft_perror(output);
6363

6464
return errno;
6565
}

tools/testing/selftests/resctrl/resctrl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@
3737

3838
#define DEFAULT_SPAN (250 * MB)
3939

40-
#define PARENT_EXIT(err_msg) \
40+
#define PARENT_EXIT() \
4141
do { \
42-
perror(err_msg); \
4342
kill(ppid, SIGKILL); \
4443
umount_resctrlfs(); \
4544
exit(EXIT_FAILURE); \

tools/testing/selftests/resctrl/resctrl_val.c

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ static int read_from_imc_dir(char *imc_dir, int count)
156156
sprintf(imc_counter_type, "%s%s", imc_dir, "type");
157157
fp = fopen(imc_counter_type, "r");
158158
if (!fp) {
159-
perror("Failed to open imc counter type file");
159+
ksft_perror("Failed to open iMC counter type file");
160160

161161
return -1;
162162
}
163163
if (fscanf(fp, "%u", &imc_counters_config[count][READ].type) <= 0) {
164-
perror("Could not get imc type");
164+
ksft_perror("Could not get iMC type");
165165
fclose(fp);
166166

167167
return -1;
@@ -175,12 +175,12 @@ static int read_from_imc_dir(char *imc_dir, int count)
175175
sprintf(imc_counter_cfg, "%s%s", imc_dir, READ_FILE_NAME);
176176
fp = fopen(imc_counter_cfg, "r");
177177
if (!fp) {
178-
perror("Failed to open imc config file");
178+
ksft_perror("Failed to open iMC config file");
179179

180180
return -1;
181181
}
182182
if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
183-
perror("Could not get imc cas count read");
183+
ksft_perror("Could not get iMC cas count read");
184184
fclose(fp);
185185

186186
return -1;
@@ -193,12 +193,12 @@ static int read_from_imc_dir(char *imc_dir, int count)
193193
sprintf(imc_counter_cfg, "%s%s", imc_dir, WRITE_FILE_NAME);
194194
fp = fopen(imc_counter_cfg, "r");
195195
if (!fp) {
196-
perror("Failed to open imc config file");
196+
ksft_perror("Failed to open iMC config file");
197197

198198
return -1;
199199
}
200200
if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
201-
perror("Could not get imc cas count write");
201+
ksft_perror("Could not get iMC cas count write");
202202
fclose(fp);
203203

204204
return -1;
@@ -262,12 +262,12 @@ static int num_of_imcs(void)
262262
}
263263
closedir(dp);
264264
if (count == 0) {
265-
perror("Unable find iMC counters!\n");
265+
ksft_print_msg("Unable to find iMC counters\n");
266266

267267
return -1;
268268
}
269269
} else {
270-
perror("Unable to open PMU directory!\n");
270+
ksft_perror("Unable to open PMU directory");
271271

272272
return -1;
273273
}
@@ -339,14 +339,14 @@ static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc)
339339

340340
if (read(r->fd, &r->return_value,
341341
sizeof(struct membw_read_format)) == -1) {
342-
perror("Couldn't get read b/w through iMC");
342+
ksft_perror("Couldn't get read b/w through iMC");
343343

344344
return -1;
345345
}
346346

347347
if (read(w->fd, &w->return_value,
348348
sizeof(struct membw_read_format)) == -1) {
349-
perror("Couldn't get write bw through iMC");
349+
ksft_perror("Couldn't get write bw through iMC");
350350

351351
return -1;
352352
}
@@ -416,7 +416,7 @@ static void initialize_mem_bw_resctrl(const char *ctrlgrp, const char *mongrp,
416416
int resource_id;
417417

418418
if (get_resource_id(cpu_no, &resource_id) < 0) {
419-
perror("Could not get resource_id");
419+
ksft_print_msg("Could not get resource_id\n");
420420
return;
421421
}
422422

@@ -449,12 +449,12 @@ static int get_mem_bw_resctrl(unsigned long *mbm_total)
449449

450450
fp = fopen(mbm_total_path, "r");
451451
if (!fp) {
452-
perror("Failed to open total bw file");
452+
ksft_perror("Failed to open total bw file");
453453

454454
return -1;
455455
}
456456
if (fscanf(fp, "%lu", mbm_total) <= 0) {
457-
perror("Could not get mbm local bytes");
457+
ksft_perror("Could not get mbm local bytes");
458458
fclose(fp);
459459

460460
return -1;
@@ -495,7 +495,7 @@ int signal_handler_register(void)
495495
if (sigaction(SIGINT, &sigact, NULL) ||
496496
sigaction(SIGTERM, &sigact, NULL) ||
497497
sigaction(SIGHUP, &sigact, NULL)) {
498-
perror("# sigaction");
498+
ksft_perror("sigaction");
499499
ret = -1;
500500
}
501501
return ret;
@@ -515,7 +515,7 @@ void signal_handler_unregister(void)
515515
if (sigaction(SIGINT, &sigact, NULL) ||
516516
sigaction(SIGTERM, &sigact, NULL) ||
517517
sigaction(SIGHUP, &sigact, NULL)) {
518-
perror("# sigaction");
518+
ksft_perror("sigaction");
519519
}
520520
}
521521

@@ -540,14 +540,14 @@ static int print_results_bw(char *filename, int bm_pid, float bw_imc,
540540
} else {
541541
fp = fopen(filename, "a");
542542
if (!fp) {
543-
perror("Cannot open results file");
543+
ksft_perror("Cannot open results file");
544544

545545
return errno;
546546
}
547547
if (fprintf(fp, "Pid: %d \t Mem_BW_iMC: %f \t Mem_BW_resc: %lu \t Difference: %lu\n",
548548
bm_pid, bw_imc, bw_resc, diff) <= 0) {
549+
ksft_print_msg("Could not log results\n");
549550
fclose(fp);
550-
perror("Could not log results.");
551551

552552
return errno;
553553
}
@@ -585,7 +585,7 @@ static void initialize_llc_occu_resctrl(const char *ctrlgrp, const char *mongrp,
585585
int resource_id;
586586

587587
if (get_resource_id(cpu_no, &resource_id) < 0) {
588-
perror("# Unable to resource_id");
588+
ksft_print_msg("Could not get resource_id\n");
589589
return;
590590
}
591591

@@ -647,32 +647,37 @@ static void run_benchmark(int signum, siginfo_t *info, void *ucontext)
647647
* stdio (console)
648648
*/
649649
fp = freopen("/dev/null", "w", stdout);
650-
if (!fp)
651-
PARENT_EXIT("Unable to direct benchmark status to /dev/null");
650+
if (!fp) {
651+
ksft_perror("Unable to direct benchmark status to /dev/null");
652+
PARENT_EXIT();
653+
}
652654

653655
if (strcmp(benchmark_cmd[0], "fill_buf") == 0) {
654656
/* Execute default fill_buf benchmark */
655657
span = strtoul(benchmark_cmd[1], NULL, 10);
656658
memflush = atoi(benchmark_cmd[2]);
657659
operation = atoi(benchmark_cmd[3]);
658-
if (!strcmp(benchmark_cmd[4], "true"))
660+
if (!strcmp(benchmark_cmd[4], "true")) {
659661
once = true;
660-
else if (!strcmp(benchmark_cmd[4], "false"))
662+
} else if (!strcmp(benchmark_cmd[4], "false")) {
661663
once = false;
662-
else
663-
PARENT_EXIT("Invalid once parameter");
664+
} else {
665+
ksft_print_msg("Invalid once parameter\n");
666+
PARENT_EXIT();
667+
}
664668

665669
if (run_fill_buf(span, memflush, operation, once))
666670
fprintf(stderr, "Error in running fill buffer\n");
667671
} else {
668672
/* Execute specified benchmark */
669673
ret = execvp(benchmark_cmd[0], benchmark_cmd);
670674
if (ret)
671-
perror("wrong\n");
675+
ksft_perror("execvp");
672676
}
673677

674678
fclose(stdout);
675-
PARENT_EXIT("Unable to run specified benchmark");
679+
ksft_print_msg("Unable to run specified benchmark\n");
680+
PARENT_EXIT();
676681
}
677682

678683
/*
@@ -709,7 +714,7 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
709714
ppid = getpid();
710715

711716
if (pipe(pipefd)) {
712-
perror("# Unable to create pipe");
717+
ksft_perror("Unable to create pipe");
713718

714719
return -1;
715720
}
@@ -721,7 +726,7 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
721726
fflush(stdout);
722727
bm_pid = fork();
723728
if (bm_pid == -1) {
724-
perror("# Unable to fork");
729+
ksft_perror("Unable to fork");
725730

726731
return -1;
727732
}
@@ -738,15 +743,17 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
738743
sigact.sa_flags = SA_SIGINFO;
739744

740745
/* Register for "SIGUSR1" signal from parent */
741-
if (sigaction(SIGUSR1, &sigact, NULL))
742-
PARENT_EXIT("Can't register child for signal");
746+
if (sigaction(SIGUSR1, &sigact, NULL)) {
747+
ksft_perror("Can't register child for signal");
748+
PARENT_EXIT();
749+
}
743750

744751
/* Tell parent that child is ready */
745752
close(pipefd[0]);
746753
pipe_message = 1;
747754
if (write(pipefd[1], &pipe_message, sizeof(pipe_message)) <
748755
sizeof(pipe_message)) {
749-
perror("# failed signaling parent process");
756+
ksft_perror("Failed signaling parent process");
750757
close(pipefd[1]);
751758
return -1;
752759
}
@@ -755,7 +762,8 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
755762
/* Suspend child until delivery of "SIGUSR1" from parent */
756763
sigsuspend(&sigact.sa_mask);
757764

758-
PARENT_EXIT("Child is done");
765+
ksft_perror("Child is done");
766+
PARENT_EXIT();
759767
}
760768

761769
ksft_print_msg("Benchmark PID: %d\n", bm_pid);
@@ -796,7 +804,7 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
796804
while (pipe_message != 1) {
797805
if (read(pipefd[0], &pipe_message, sizeof(pipe_message)) <
798806
sizeof(pipe_message)) {
799-
perror("# failed reading message from child process");
807+
ksft_perror("Failed reading message from child process");
800808
close(pipefd[0]);
801809
goto out;
802810
}
@@ -805,7 +813,7 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
805813

806814
/* Signal child to start benchmark */
807815
if (sigqueue(bm_pid, SIGUSR1, value) == -1) {
808-
perror("# sigqueue SIGUSR1 to child");
816+
ksft_perror("sigqueue SIGUSR1 to child");
809817
ret = errno;
810818
goto out;
811819
}

0 commit comments

Comments
 (0)