Skip to content

Commit 8cbb25d

Browse files
makelinuxlenticularis39
authored andcommitted
tools/rtla: Add fatal() and replace error handling pattern
The code contains some technical debt in error handling, which complicates the consolidation of duplicated code. Introduce an fatal() function to replace the common pattern of err_msg() followed by exit(EXIT_FAILURE), reducing the length of an already long function. Further patches using fatal() follow. Signed-off-by: Costa Shulyupin <costa.shul@redhat.com> Reviewed-by: Tomas Glozar <tglozar@redhat.com> Link: https://lore.kernel.org/r/20251011082738.173670-2-costa.shul@redhat.com Signed-off-by: Tomas Glozar <tglozar@redhat.com>
1 parent 34c170a commit 8cbb25d

7 files changed

Lines changed: 81 additions & 129 deletions

File tree

tools/tracing/rtla/src/osnoise_hist.c

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,8 @@ static struct common_params
575575
break;
576576
case 'e':
577577
tevent = trace_event_alloc(optarg);
578-
if (!tevent) {
579-
err_msg("Error alloc trace event");
580-
exit(EXIT_FAILURE);
581-
}
578+
if (!tevent)
579+
fatal("Error alloc trace event");
582580

583581
if (params->common.events)
584582
tevent->next = params->common.events;
@@ -598,10 +596,8 @@ static struct common_params
598596
case 'H':
599597
params->common.hk_cpus = 1;
600598
retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
601-
if (retval) {
602-
err_msg("Error parsing house keeping CPUs\n");
603-
exit(EXIT_FAILURE);
604-
}
599+
if (retval)
600+
fatal("Error parsing house keeping CPUs");
605601
break;
606602
case 'p':
607603
params->period = get_llong_from_str(optarg);
@@ -654,21 +650,17 @@ static struct common_params
654650
case '4': /* trigger */
655651
if (params->common.events) {
656652
retval = trace_event_add_trigger(params->common.events, optarg);
657-
if (retval) {
658-
err_msg("Error adding trigger %s\n", optarg);
659-
exit(EXIT_FAILURE);
660-
}
653+
if (retval)
654+
fatal("Error adding trigger %s", optarg);
661655
} else {
662656
osnoise_hist_usage("--trigger requires a previous -e\n");
663657
}
664658
break;
665659
case '5': /* filter */
666660
if (params->common.events) {
667661
retval = trace_event_add_filter(params->common.events, optarg);
668-
if (retval) {
669-
err_msg("Error adding filter %s\n", optarg);
670-
exit(EXIT_FAILURE);
671-
}
662+
if (retval)
663+
fatal("Error adding filter %s", optarg);
672664
} else {
673665
osnoise_hist_usage("--filter requires a previous -e\n");
674666
}
@@ -682,18 +674,14 @@ static struct common_params
682674
case '8':
683675
retval = actions_parse(&params->common.threshold_actions, optarg,
684676
"osnoise_trace.txt");
685-
if (retval) {
686-
err_msg("Invalid action %s\n", optarg);
687-
exit(EXIT_FAILURE);
688-
}
677+
if (retval)
678+
fatal("Invalid action %s", optarg);
689679
break;
690680
case '9':
691681
retval = actions_parse(&params->common.end_actions, optarg,
692682
"osnoise_trace.txt");
693-
if (retval) {
694-
err_msg("Invalid action %s\n", optarg);
695-
exit(EXIT_FAILURE);
696-
}
683+
if (retval)
684+
fatal("Invalid action %s", optarg);
697685
break;
698686
default:
699687
osnoise_hist_usage("Invalid option");
@@ -703,10 +691,8 @@ static struct common_params
703691
if (trace_output)
704692
actions_add_trace_output(&params->common.threshold_actions, trace_output);
705693

706-
if (geteuid()) {
707-
err_msg("rtla needs root permission\n");
708-
exit(EXIT_FAILURE);
709-
}
694+
if (geteuid())
695+
fatal("rtla needs root permission");
710696

711697
if (params->common.hist.no_index && !params->common.hist.with_zeros)
712698
osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");

tools/tracing/rtla/src/osnoise_top.c

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
421421
break;
422422
case 'e':
423423
tevent = trace_event_alloc(optarg);
424-
if (!tevent) {
425-
err_msg("Error alloc trace event");
426-
exit(EXIT_FAILURE);
427-
}
424+
if (!tevent)
425+
fatal("Error alloc trace event");
428426

429427
if (params->common.events)
430428
tevent->next = params->common.events;
@@ -438,10 +436,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
438436
case 'H':
439437
params->common.hk_cpus = 1;
440438
retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
441-
if (retval) {
442-
err_msg("Error parsing house keeping CPUs\n");
443-
exit(EXIT_FAILURE);
444-
}
439+
if (retval)
440+
fatal("Error parsing house keeping CPUs");
445441
break;
446442
case 'p':
447443
params->period = get_llong_from_str(optarg);
@@ -485,21 +481,17 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
485481
case '0': /* trigger */
486482
if (params->common.events) {
487483
retval = trace_event_add_trigger(params->common.events, optarg);
488-
if (retval) {
489-
err_msg("Error adding trigger %s\n", optarg);
490-
exit(EXIT_FAILURE);
491-
}
484+
if (retval)
485+
fatal("Error adding trigger %s", optarg);
492486
} else {
493487
osnoise_top_usage(params, "--trigger requires a previous -e\n");
494488
}
495489
break;
496490
case '1': /* filter */
497491
if (params->common.events) {
498492
retval = trace_event_add_filter(params->common.events, optarg);
499-
if (retval) {
500-
err_msg("Error adding filter %s\n", optarg);
501-
exit(EXIT_FAILURE);
502-
}
493+
if (retval)
494+
fatal("Error adding filter %s", optarg);
503495
} else {
504496
osnoise_top_usage(params, "--filter requires a previous -e\n");
505497
}
@@ -513,18 +505,14 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
513505
case '4':
514506
retval = actions_parse(&params->common.threshold_actions, optarg,
515507
"osnoise_trace.txt");
516-
if (retval) {
517-
err_msg("Invalid action %s\n", optarg);
518-
exit(EXIT_FAILURE);
519-
}
508+
if (retval)
509+
fatal("Invalid action %s", optarg);
520510
break;
521511
case '5':
522512
retval = actions_parse(&params->common.end_actions, optarg,
523513
"osnoise_trace.txt");
524-
if (retval) {
525-
err_msg("Invalid action %s\n", optarg);
526-
exit(EXIT_FAILURE);
527-
}
514+
if (retval)
515+
fatal("Invalid action %s", optarg);
528516
break;
529517
default:
530518
osnoise_top_usage(params, "Invalid option");
@@ -534,10 +522,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
534522
if (trace_output)
535523
actions_add_trace_output(&params->common.threshold_actions, trace_output);
536524

537-
if (geteuid()) {
538-
err_msg("osnoise needs root permission\n");
539-
exit(EXIT_FAILURE);
540-
}
525+
if (geteuid())
526+
fatal("osnoise needs root permission");
541527

542528
return &params->common;
543529
}

tools/tracing/rtla/src/timerlat_hist.c

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -894,10 +894,8 @@ static struct common_params
894894
break;
895895
case 'e':
896896
tevent = trace_event_alloc(optarg);
897-
if (!tevent) {
898-
err_msg("Error alloc trace event");
899-
exit(EXIT_FAILURE);
900-
}
897+
if (!tevent)
898+
fatal("Error alloc trace event");
901899

902900
if (params->common.events)
903901
tevent->next = params->common.events;
@@ -917,10 +915,8 @@ static struct common_params
917915
case 'H':
918916
params->common.hk_cpus = 1;
919917
retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
920-
if (retval) {
921-
err_msg("Error parsing house keeping CPUs\n");
922-
exit(EXIT_FAILURE);
923-
}
918+
if (retval)
919+
fatal("Error parsing house keeping CPUs");
924920
break;
925921
case 'i':
926922
params->common.stop_us = get_llong_from_str(optarg);
@@ -986,31 +982,25 @@ static struct common_params
986982
case '6': /* trigger */
987983
if (params->common.events) {
988984
retval = trace_event_add_trigger(params->common.events, optarg);
989-
if (retval) {
990-
err_msg("Error adding trigger %s\n", optarg);
991-
exit(EXIT_FAILURE);
992-
}
985+
if (retval)
986+
fatal("Error adding trigger %s", optarg);
993987
} else {
994988
timerlat_hist_usage("--trigger requires a previous -e\n");
995989
}
996990
break;
997991
case '7': /* filter */
998992
if (params->common.events) {
999993
retval = trace_event_add_filter(params->common.events, optarg);
1000-
if (retval) {
1001-
err_msg("Error adding filter %s\n", optarg);
1002-
exit(EXIT_FAILURE);
1003-
}
994+
if (retval)
995+
fatal("Error adding filter %s", optarg);
1004996
} else {
1005997
timerlat_hist_usage("--filter requires a previous -e\n");
1006998
}
1007999
break;
10081000
case '8':
10091001
params->dma_latency = get_llong_from_str(optarg);
1010-
if (params->dma_latency < 0 || params->dma_latency > 10000) {
1011-
err_msg("--dma-latency needs to be >= 0 and < 10000");
1012-
exit(EXIT_FAILURE);
1013-
}
1002+
if (params->dma_latency < 0 || params->dma_latency > 10000)
1003+
fatal("--dma-latency needs to be >= 0 and < 10000");
10141004
break;
10151005
case '9':
10161006
params->no_aa = 1;
@@ -1030,31 +1020,25 @@ static struct common_params
10301020
case '\5':
10311021
retval = actions_parse(&params->common.threshold_actions, optarg,
10321022
"timerlat_trace.txt");
1033-
if (retval) {
1034-
err_msg("Invalid action %s\n", optarg);
1035-
exit(EXIT_FAILURE);
1036-
}
1023+
if (retval)
1024+
fatal("Invalid action %s", optarg);
10371025
break;
10381026
case '\6':
10391027
retval = actions_parse(&params->common.end_actions, optarg,
10401028
"timerlat_trace.txt");
1041-
if (retval) {
1042-
err_msg("Invalid action %s\n", optarg);
1043-
exit(EXIT_FAILURE);
1044-
}
1029+
if (retval)
1030+
fatal("Invalid action %s", optarg);
10451031
break;
10461032
default:
1047-
timerlat_hist_usage("Invalid option");
1033+
fatal("Invalid option");
10481034
}
10491035
}
10501036

10511037
if (trace_output)
10521038
actions_add_trace_output(&params->common.threshold_actions, trace_output);
10531039

1054-
if (geteuid()) {
1055-
err_msg("rtla needs root permission\n");
1056-
exit(EXIT_FAILURE);
1057-
}
1040+
if (geteuid())
1041+
fatal("rtla needs root permission");
10581042

10591043
if (params->common.hist.no_irq && params->common.hist.no_thread)
10601044
timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");

tools/tracing/rtla/src/timerlat_top.c

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -666,10 +666,8 @@ static struct common_params
666666
break;
667667
case 'e':
668668
tevent = trace_event_alloc(optarg);
669-
if (!tevent) {
670-
err_msg("Error alloc trace event");
671-
exit(EXIT_FAILURE);
672-
}
669+
if (!tevent)
670+
fatal("Error alloc trace event");
673671

674672
if (params->common.events)
675673
tevent->next = params->common.events;
@@ -682,10 +680,8 @@ static struct common_params
682680
case 'H':
683681
params->common.hk_cpus = 1;
684682
retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
685-
if (retval) {
686-
err_msg("Error parsing house keeping CPUs\n");
687-
exit(EXIT_FAILURE);
688-
}
683+
if (retval)
684+
fatal("Error parsing house keeping CPUs");
689685
break;
690686
case 'i':
691687
params->common.stop_us = get_llong_from_str(optarg);
@@ -736,31 +732,25 @@ static struct common_params
736732
case '0': /* trigger */
737733
if (params->common.events) {
738734
retval = trace_event_add_trigger(params->common.events, optarg);
739-
if (retval) {
740-
err_msg("Error adding trigger %s\n", optarg);
741-
exit(EXIT_FAILURE);
742-
}
735+
if (retval)
736+
fatal("Error adding trigger %s", optarg);
743737
} else {
744738
timerlat_top_usage("--trigger requires a previous -e\n");
745739
}
746740
break;
747741
case '1': /* filter */
748742
if (params->common.events) {
749743
retval = trace_event_add_filter(params->common.events, optarg);
750-
if (retval) {
751-
err_msg("Error adding filter %s\n", optarg);
752-
exit(EXIT_FAILURE);
753-
}
744+
if (retval)
745+
fatal("Error adding filter %s", optarg);
754746
} else {
755747
timerlat_top_usage("--filter requires a previous -e\n");
756748
}
757749
break;
758750
case '2': /* dma-latency */
759751
params->dma_latency = get_llong_from_str(optarg);
760-
if (params->dma_latency < 0 || params->dma_latency > 10000) {
761-
err_msg("--dma-latency needs to be >= 0 and < 10000");
762-
exit(EXIT_FAILURE);
763-
}
752+
if (params->dma_latency < 0 || params->dma_latency > 10000)
753+
fatal("--dma-latency needs to be >= 0 and < 10000");
764754
break;
765755
case '3': /* no-aa */
766756
params->no_aa = 1;
@@ -780,18 +770,14 @@ static struct common_params
780770
case '9':
781771
retval = actions_parse(&params->common.threshold_actions, optarg,
782772
"timerlat_trace.txt");
783-
if (retval) {
784-
err_msg("Invalid action %s\n", optarg);
785-
exit(EXIT_FAILURE);
786-
}
773+
if (retval)
774+
fatal("Invalid action %s", optarg);
787775
break;
788776
case '\1':
789777
retval = actions_parse(&params->common.end_actions, optarg,
790778
"timerlat_trace.txt");
791-
if (retval) {
792-
err_msg("Invalid action %s\n", optarg);
793-
exit(EXIT_FAILURE);
794-
}
779+
if (retval)
780+
fatal("Invalid action %s", optarg);
795781
break;
796782
default:
797783
timerlat_top_usage("Invalid option");
@@ -801,10 +787,8 @@ static struct common_params
801787
if (trace_output)
802788
actions_add_trace_output(&params->common.threshold_actions, trace_output);
803789

804-
if (geteuid()) {
805-
err_msg("rtla needs root permission\n");
806-
exit(EXIT_FAILURE);
807-
}
790+
if (geteuid())
791+
fatal("rtla needs root permission");
808792

809793
/*
810794
* Auto analysis only happens if stop tracing, thus:

0 commit comments

Comments
 (0)