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

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>

authored by

Costa Shulyupin and committed by
Tomas Glozar
8cbb25db 34c170ae

+81 -129
+14 -28
tools/tracing/rtla/src/osnoise_hist.c
··· 575 575 break; 576 576 case 'e': 577 577 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"); 582 580 583 581 if (params->common.events) 584 582 tevent->next = params->common.events; ··· 596 598 case 'H': 597 599 params->common.hk_cpus = 1; 598 600 retval = parse_cpu_set(optarg, &params->common.hk_cpu_set); 599 - if (retval) { 600 - err_msg("Error parsing house keeping CPUs\n"); 601 - exit(EXIT_FAILURE); 602 - } 601 + if (retval) 602 + fatal("Error parsing house keeping CPUs"); 603 603 break; 604 604 case 'p': 605 605 params->period = get_llong_from_str(optarg); ··· 650 654 case '4': /* trigger */ 651 655 if (params->common.events) { 652 656 retval = trace_event_add_trigger(params->common.events, optarg); 653 - if (retval) { 654 - err_msg("Error adding trigger %s\n", optarg); 655 - exit(EXIT_FAILURE); 656 - } 657 + if (retval) 658 + fatal("Error adding trigger %s", optarg); 657 659 } else { 658 660 osnoise_hist_usage("--trigger requires a previous -e\n"); 659 661 } ··· 659 665 case '5': /* filter */ 660 666 if (params->common.events) { 661 667 retval = trace_event_add_filter(params->common.events, optarg); 662 - if (retval) { 663 - err_msg("Error adding filter %s\n", optarg); 664 - exit(EXIT_FAILURE); 665 - } 668 + if (retval) 669 + fatal("Error adding filter %s", optarg); 666 670 } else { 667 671 osnoise_hist_usage("--filter requires a previous -e\n"); 668 672 } ··· 674 682 case '8': 675 683 retval = actions_parse(&params->common.threshold_actions, optarg, 676 684 "osnoise_trace.txt"); 677 - if (retval) { 678 - err_msg("Invalid action %s\n", optarg); 679 - exit(EXIT_FAILURE); 680 - } 685 + if (retval) 686 + fatal("Invalid action %s", optarg); 681 687 break; 682 688 case '9': 683 689 retval = actions_parse(&params->common.end_actions, optarg, 684 690 "osnoise_trace.txt"); 685 - if (retval) { 686 - err_msg("Invalid action %s\n", optarg); 687 - exit(EXIT_FAILURE); 688 - } 691 + if (retval) 692 + fatal("Invalid action %s", optarg); 689 693 break; 690 694 default: 691 695 osnoise_hist_usage("Invalid option"); ··· 691 703 if (trace_output) 692 704 actions_add_trace_output(&params->common.threshold_actions, trace_output); 693 705 694 - if (geteuid()) { 695 - err_msg("rtla needs root permission\n"); 696 - exit(EXIT_FAILURE); 697 - } 706 + if (geteuid()) 707 + fatal("rtla needs root permission"); 698 708 699 709 if (params->common.hist.no_index && !params->common.hist.with_zeros) 700 710 osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");
+14 -28
tools/tracing/rtla/src/osnoise_top.c
··· 421 421 break; 422 422 case 'e': 423 423 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"); 428 426 429 427 if (params->common.events) 430 428 tevent->next = params->common.events; ··· 436 438 case 'H': 437 439 params->common.hk_cpus = 1; 438 440 retval = parse_cpu_set(optarg, &params->common.hk_cpu_set); 439 - if (retval) { 440 - err_msg("Error parsing house keeping CPUs\n"); 441 - exit(EXIT_FAILURE); 442 - } 441 + if (retval) 442 + fatal("Error parsing house keeping CPUs"); 443 443 break; 444 444 case 'p': 445 445 params->period = get_llong_from_str(optarg); ··· 481 485 case '0': /* trigger */ 482 486 if (params->common.events) { 483 487 retval = trace_event_add_trigger(params->common.events, optarg); 484 - if (retval) { 485 - err_msg("Error adding trigger %s\n", optarg); 486 - exit(EXIT_FAILURE); 487 - } 488 + if (retval) 489 + fatal("Error adding trigger %s", optarg); 488 490 } else { 489 491 osnoise_top_usage(params, "--trigger requires a previous -e\n"); 490 492 } ··· 490 496 case '1': /* filter */ 491 497 if (params->common.events) { 492 498 retval = trace_event_add_filter(params->common.events, optarg); 493 - if (retval) { 494 - err_msg("Error adding filter %s\n", optarg); 495 - exit(EXIT_FAILURE); 496 - } 499 + if (retval) 500 + fatal("Error adding filter %s", optarg); 497 501 } else { 498 502 osnoise_top_usage(params, "--filter requires a previous -e\n"); 499 503 } ··· 505 513 case '4': 506 514 retval = actions_parse(&params->common.threshold_actions, optarg, 507 515 "osnoise_trace.txt"); 508 - if (retval) { 509 - err_msg("Invalid action %s\n", optarg); 510 - exit(EXIT_FAILURE); 511 - } 516 + if (retval) 517 + fatal("Invalid action %s", optarg); 512 518 break; 513 519 case '5': 514 520 retval = actions_parse(&params->common.end_actions, optarg, 515 521 "osnoise_trace.txt"); 516 - if (retval) { 517 - err_msg("Invalid action %s\n", optarg); 518 - exit(EXIT_FAILURE); 519 - } 522 + if (retval) 523 + fatal("Invalid action %s", optarg); 520 524 break; 521 525 default: 522 526 osnoise_top_usage(params, "Invalid option"); ··· 522 534 if (trace_output) 523 535 actions_add_trace_output(&params->common.threshold_actions, trace_output); 524 536 525 - if (geteuid()) { 526 - err_msg("osnoise needs root permission\n"); 527 - exit(EXIT_FAILURE); 528 - } 537 + if (geteuid()) 538 + fatal("osnoise needs root permission"); 529 539 530 540 return &params->common; 531 541 }
+17 -33
tools/tracing/rtla/src/timerlat_hist.c
··· 894 894 break; 895 895 case 'e': 896 896 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"); 901 899 902 900 if (params->common.events) 903 901 tevent->next = params->common.events; ··· 915 917 case 'H': 916 918 params->common.hk_cpus = 1; 917 919 retval = parse_cpu_set(optarg, &params->common.hk_cpu_set); 918 - if (retval) { 919 - err_msg("Error parsing house keeping CPUs\n"); 920 - exit(EXIT_FAILURE); 921 - } 920 + if (retval) 921 + fatal("Error parsing house keeping CPUs"); 922 922 break; 923 923 case 'i': 924 924 params->common.stop_us = get_llong_from_str(optarg); ··· 982 986 case '6': /* trigger */ 983 987 if (params->common.events) { 984 988 retval = trace_event_add_trigger(params->common.events, optarg); 985 - if (retval) { 986 - err_msg("Error adding trigger %s\n", optarg); 987 - exit(EXIT_FAILURE); 988 - } 989 + if (retval) 990 + fatal("Error adding trigger %s", optarg); 989 991 } else { 990 992 timerlat_hist_usage("--trigger requires a previous -e\n"); 991 993 } ··· 991 997 case '7': /* filter */ 992 998 if (params->common.events) { 993 999 retval = trace_event_add_filter(params->common.events, optarg); 994 - if (retval) { 995 - err_msg("Error adding filter %s\n", optarg); 996 - exit(EXIT_FAILURE); 997 - } 1000 + if (retval) 1001 + fatal("Error adding filter %s", optarg); 998 1002 } else { 999 1003 timerlat_hist_usage("--filter requires a previous -e\n"); 1000 1004 } 1001 1005 break; 1002 1006 case '8': 1003 1007 params->dma_latency = get_llong_from_str(optarg); 1004 - if (params->dma_latency < 0 || params->dma_latency > 10000) { 1005 - err_msg("--dma-latency needs to be >= 0 and < 10000"); 1006 - exit(EXIT_FAILURE); 1007 - } 1008 + if (params->dma_latency < 0 || params->dma_latency > 10000) 1009 + fatal("--dma-latency needs to be >= 0 and < 10000"); 1008 1010 break; 1009 1011 case '9': 1010 1012 params->no_aa = 1; ··· 1020 1030 case '\5': 1021 1031 retval = actions_parse(&params->common.threshold_actions, optarg, 1022 1032 "timerlat_trace.txt"); 1023 - if (retval) { 1024 - err_msg("Invalid action %s\n", optarg); 1025 - exit(EXIT_FAILURE); 1026 - } 1033 + if (retval) 1034 + fatal("Invalid action %s", optarg); 1027 1035 break; 1028 1036 case '\6': 1029 1037 retval = actions_parse(&params->common.end_actions, optarg, 1030 1038 "timerlat_trace.txt"); 1031 - if (retval) { 1032 - err_msg("Invalid action %s\n", optarg); 1033 - exit(EXIT_FAILURE); 1034 - } 1039 + if (retval) 1040 + fatal("Invalid action %s", optarg); 1035 1041 break; 1036 1042 default: 1037 - timerlat_hist_usage("Invalid option"); 1043 + fatal("Invalid option"); 1038 1044 } 1039 1045 } 1040 1046 1041 1047 if (trace_output) 1042 1048 actions_add_trace_output(&params->common.threshold_actions, trace_output); 1043 1049 1044 - if (geteuid()) { 1045 - err_msg("rtla needs root permission\n"); 1046 - exit(EXIT_FAILURE); 1047 - } 1050 + if (geteuid()) 1051 + fatal("rtla needs root permission"); 1048 1052 1049 1053 if (params->common.hist.no_irq && params->common.hist.no_thread) 1050 1054 timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
+16 -32
tools/tracing/rtla/src/timerlat_top.c
··· 666 666 break; 667 667 case 'e': 668 668 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"); 673 671 674 672 if (params->common.events) 675 673 tevent->next = params->common.events; ··· 680 682 case 'H': 681 683 params->common.hk_cpus = 1; 682 684 retval = parse_cpu_set(optarg, &params->common.hk_cpu_set); 683 - if (retval) { 684 - err_msg("Error parsing house keeping CPUs\n"); 685 - exit(EXIT_FAILURE); 686 - } 685 + if (retval) 686 + fatal("Error parsing house keeping CPUs"); 687 687 break; 688 688 case 'i': 689 689 params->common.stop_us = get_llong_from_str(optarg); ··· 732 736 case '0': /* trigger */ 733 737 if (params->common.events) { 734 738 retval = trace_event_add_trigger(params->common.events, optarg); 735 - if (retval) { 736 - err_msg("Error adding trigger %s\n", optarg); 737 - exit(EXIT_FAILURE); 738 - } 739 + if (retval) 740 + fatal("Error adding trigger %s", optarg); 739 741 } else { 740 742 timerlat_top_usage("--trigger requires a previous -e\n"); 741 743 } ··· 741 747 case '1': /* filter */ 742 748 if (params->common.events) { 743 749 retval = trace_event_add_filter(params->common.events, optarg); 744 - if (retval) { 745 - err_msg("Error adding filter %s\n", optarg); 746 - exit(EXIT_FAILURE); 747 - } 750 + if (retval) 751 + fatal("Error adding filter %s", optarg); 748 752 } else { 749 753 timerlat_top_usage("--filter requires a previous -e\n"); 750 754 } 751 755 break; 752 756 case '2': /* dma-latency */ 753 757 params->dma_latency = get_llong_from_str(optarg); 754 - if (params->dma_latency < 0 || params->dma_latency > 10000) { 755 - err_msg("--dma-latency needs to be >= 0 and < 10000"); 756 - exit(EXIT_FAILURE); 757 - } 758 + if (params->dma_latency < 0 || params->dma_latency > 10000) 759 + fatal("--dma-latency needs to be >= 0 and < 10000"); 758 760 break; 759 761 case '3': /* no-aa */ 760 762 params->no_aa = 1; ··· 770 780 case '9': 771 781 retval = actions_parse(&params->common.threshold_actions, optarg, 772 782 "timerlat_trace.txt"); 773 - if (retval) { 774 - err_msg("Invalid action %s\n", optarg); 775 - exit(EXIT_FAILURE); 776 - } 783 + if (retval) 784 + fatal("Invalid action %s", optarg); 777 785 break; 778 786 case '\1': 779 787 retval = actions_parse(&params->common.end_actions, optarg, 780 788 "timerlat_trace.txt"); 781 - if (retval) { 782 - err_msg("Invalid action %s\n", optarg); 783 - exit(EXIT_FAILURE); 784 - } 789 + if (retval) 790 + fatal("Invalid action %s", optarg); 785 791 break; 786 792 default: 787 793 timerlat_top_usage("Invalid option"); ··· 787 801 if (trace_output) 788 802 actions_add_trace_output(&params->common.threshold_actions, trace_output); 789 803 790 - if (geteuid()) { 791 - err_msg("rtla needs root permission\n"); 792 - exit(EXIT_FAILURE); 793 - } 804 + if (geteuid()) 805 + fatal("rtla needs root permission"); 794 806 795 807 /* 796 808 * Auto analysis only happens if stop tracing, thus:
+4 -8
tools/tracing/rtla/src/timerlat_u.c
··· 51 51 52 52 if (!params->sched_param) { 53 53 retval = sched_setscheduler(0, SCHED_FIFO, &sp); 54 - if (retval < 0) { 55 - err_msg("Error setting timerlat u default priority: %s\n", strerror(errno)); 56 - exit(1); 57 - } 54 + if (retval < 0) 55 + fatal("Error setting timerlat u default priority: %s", strerror(errno)); 58 56 } else { 59 57 retval = __set_sched_attr(getpid(), params->sched_param); 60 58 if (retval) { ··· 76 78 snprintf(buffer, sizeof(buffer), "osnoise/per_cpu/cpu%d/timerlat_fd", cpu); 77 79 78 80 timerlat_fd = tracefs_instance_file_open(NULL, buffer, O_RDONLY); 79 - if (timerlat_fd < 0) { 80 - err_msg("Error opening %s:%s\n", buffer, strerror(errno)); 81 - exit(1); 82 - } 81 + if (timerlat_fd < 0) 82 + fatal("Error opening %s:%s", buffer, strerror(errno)); 83 83 84 84 debug_msg("User-space timerlat pid %d on cpu %d\n", gettid(), cpu); 85 85
+15
tools/tracing/rtla/src/utils.c
··· 57 57 } 58 58 59 59 /* 60 + * fatal - print an error message and EOL to stderr and exit with ERROR 61 + */ 62 + void fatal(const char *fmt, ...) 63 + { 64 + va_list ap; 65 + 66 + va_start(ap, fmt); 67 + vfprintf(stderr, fmt, ap); 68 + va_end(ap); 69 + fprintf(stderr, "\n"); 70 + 71 + exit(ERROR); 72 + } 73 + 74 + /* 60 75 * get_llong_from_str - get a long long int from a string 61 76 */ 62 77 long long get_llong_from_str(char *start)
+1
tools/tracing/rtla/src/utils.h
··· 19 19 extern int config_debug; 20 20 void debug_msg(const char *fmt, ...); 21 21 void err_msg(const char *fmt, ...); 22 + void fatal(const char *fmt, ...); 22 23 23 24 long parse_seconds_duration(char *val); 24 25 void get_duration(time_t start_time, char *output, int output_size);