@@ -3211,14 +3211,44 @@ static void perf_sched__merge_lat(struct perf_sched *sched)
32113211 }
32123212}
32133213
3214+ static int setup_cpus_switch_event (struct perf_sched * sched )
3215+ {
3216+ unsigned int i ;
3217+
3218+ sched -> cpu_last_switched = calloc (MAX_CPUS , sizeof (* (sched -> cpu_last_switched )));
3219+ if (!sched -> cpu_last_switched )
3220+ return -1 ;
3221+
3222+ sched -> curr_pid = malloc (MAX_CPUS * sizeof (* (sched -> curr_pid )));
3223+ if (!sched -> curr_pid ) {
3224+ zfree (& sched -> cpu_last_switched );
3225+ return -1 ;
3226+ }
3227+
3228+ for (i = 0 ; i < MAX_CPUS ; i ++ )
3229+ sched -> curr_pid [i ] = -1 ;
3230+
3231+ return 0 ;
3232+ }
3233+
3234+ static void free_cpus_switch_event (struct perf_sched * sched )
3235+ {
3236+ zfree (& sched -> curr_pid );
3237+ zfree (& sched -> cpu_last_switched );
3238+ }
3239+
32143240static int perf_sched__lat (struct perf_sched * sched )
32153241{
3242+ int rc = -1 ;
32163243 struct rb_node * next ;
32173244
32183245 setup_pager ();
32193246
3247+ if (setup_cpus_switch_event (sched ))
3248+ return rc ;
3249+
32203250 if (perf_sched__read_events (sched ))
3221- return -1 ;
3251+ goto out_free_cpus_switch_event ;
32223252
32233253 perf_sched__merge_lat (sched );
32243254 perf_sched__sort_lat (sched );
@@ -3247,7 +3277,11 @@ static int perf_sched__lat(struct perf_sched *sched)
32473277 print_bad_events (sched );
32483278 printf ("\n" );
32493279
3250- return 0 ;
3280+ rc = 0 ;
3281+
3282+ out_free_cpus_switch_event :
3283+ free_cpus_switch_event (sched );
3284+ return rc ;
32513285}
32523286
32533287static int setup_map_cpus (struct perf_sched * sched )
@@ -3314,9 +3348,12 @@ static int perf_sched__map(struct perf_sched *sched)
33143348 if (!sched -> curr_thread )
33153349 return rc ;
33163350
3317- if (setup_map_cpus (sched ))
3351+ if (setup_cpus_switch_event (sched ))
33183352 goto out_free_curr_thread ;
33193353
3354+ if (setup_map_cpus (sched ))
3355+ goto out_free_cpus_switch_event ;
3356+
33203357 if (setup_color_pids (sched ))
33213358 goto out_put_map_cpus ;
33223359
@@ -3340,6 +3377,9 @@ static int perf_sched__map(struct perf_sched *sched)
33403377 zfree (& sched -> map .comp_cpus );
33413378 perf_cpu_map__put (sched -> map .cpus );
33423379
3380+ out_free_cpus_switch_event :
3381+ free_cpus_switch_event (sched );
3382+
33433383out_free_curr_thread :
33443384 zfree (& sched -> curr_thread );
33453385 return rc ;
@@ -3353,14 +3393,18 @@ static int perf_sched__replay(struct perf_sched *sched)
33533393 mutex_init (& sched -> start_work_mutex );
33543394 mutex_init (& sched -> work_done_wait_mutex );
33553395
3396+ ret = setup_cpus_switch_event (sched );
3397+ if (ret )
3398+ goto out_mutex_destroy ;
3399+
33563400 calibrate_run_measurement_overhead (sched );
33573401 calibrate_sleep_measurement_overhead (sched );
33583402
33593403 test_calibrations (sched );
33603404
33613405 ret = perf_sched__read_events (sched );
33623406 if (ret )
3363- goto out_mutex_destroy ;
3407+ goto out_free_cpus_switch_event ;
33643408
33653409 printf ("nr_run_events: %ld\n" , sched -> nr_run_events );
33663410 printf ("nr_sleep_events: %ld\n" , sched -> nr_sleep_events );
@@ -3386,6 +3430,9 @@ static int perf_sched__replay(struct perf_sched *sched)
33863430 sched -> thread_funcs_exit = true;
33873431 destroy_tasks (sched );
33883432
3433+ out_free_cpus_switch_event :
3434+ free_cpus_switch_event (sched );
3435+
33893436out_mutex_destroy :
33903437 mutex_destroy (& sched -> start_work_mutex );
33913438 mutex_destroy (& sched -> work_done_wait_mutex );
@@ -3624,21 +3671,7 @@ int cmd_sched(int argc, const char **argv)
36243671 .switch_event = replay_switch_event ,
36253672 .fork_event = replay_fork_event ,
36263673 };
3627- unsigned int i ;
3628- int ret = 0 ;
3629-
3630- sched .cpu_last_switched = calloc (MAX_CPUS , sizeof (* sched .cpu_last_switched ));
3631- if (!sched .cpu_last_switched ) {
3632- ret = - ENOMEM ;
3633- goto out ;
3634- }
3635- sched .curr_pid = malloc (MAX_CPUS * sizeof (* sched .curr_pid ));
3636- if (!sched .curr_pid ) {
3637- ret = - ENOMEM ;
3638- goto out ;
3639- }
3640- for (i = 0 ; i < MAX_CPUS ; i ++ )
3641- sched .curr_pid [i ] = -1 ;
3674+ int ret ;
36423675
36433676 argc = parse_options_subcommand (argc , argv , sched_options , sched_subcommands ,
36443677 sched_usage , PARSE_OPT_STOP_AT_NON_OPTION );
@@ -3649,9 +3682,9 @@ int cmd_sched(int argc, const char **argv)
36493682 * Aliased to 'perf script' for now:
36503683 */
36513684 if (!strcmp (argv [0 ], "script" )) {
3652- ret = cmd_script (argc , argv );
3685+ return cmd_script (argc , argv );
36533686 } else if (strlen (argv [0 ]) > 2 && strstarts ("record" , argv [0 ])) {
3654- ret = __cmd_record (argc , argv );
3687+ return __cmd_record (argc , argv );
36553688 } else if (strlen (argv [0 ]) > 2 && strstarts ("latency" , argv [0 ])) {
36563689 sched .tp_handler = & lat_ops ;
36573690 if (argc > 1 ) {
@@ -3660,7 +3693,7 @@ int cmd_sched(int argc, const char **argv)
36603693 usage_with_options (latency_usage , latency_options );
36613694 }
36623695 setup_sorting (& sched , latency_options , latency_usage );
3663- ret = perf_sched__lat (& sched );
3696+ return perf_sched__lat (& sched );
36643697 } else if (!strcmp (argv [0 ], "map" )) {
36653698 if (argc ) {
36663699 argc = parse_options (argc , argv , map_options , map_usage , 0 );
@@ -3669,15 +3702,15 @@ int cmd_sched(int argc, const char **argv)
36693702 }
36703703 sched .tp_handler = & map_ops ;
36713704 setup_sorting (& sched , latency_options , latency_usage );
3672- ret = perf_sched__map (& sched );
3705+ return perf_sched__map (& sched );
36733706 } else if (strlen (argv [0 ]) > 2 && strstarts ("replay" , argv [0 ])) {
36743707 sched .tp_handler = & replay_ops ;
36753708 if (argc ) {
36763709 argc = parse_options (argc , argv , replay_options , replay_usage , 0 );
36773710 if (argc )
36783711 usage_with_options (replay_usage , replay_options );
36793712 }
3680- ret = perf_sched__replay (& sched );
3713+ return perf_sched__replay (& sched );
36813714 } else if (!strcmp (argv [0 ], "timehist" )) {
36823715 if (argc ) {
36833716 argc = parse_options (argc , argv , timehist_options ,
@@ -3693,21 +3726,16 @@ int cmd_sched(int argc, const char **argv)
36933726 parse_options_usage (NULL , timehist_options , "w" , true);
36943727 if (sched .show_next )
36953728 parse_options_usage (NULL , timehist_options , "n" , true);
3696- ret = - EINVAL ;
3697- goto out ;
3729+ return - EINVAL ;
36983730 }
36993731 ret = symbol__validate_sym_arguments ();
37003732 if (ret )
3701- goto out ;
3733+ return ret ;
37023734
3703- ret = perf_sched__timehist (& sched );
3735+ return perf_sched__timehist (& sched );
37043736 } else {
37053737 usage_with_options (sched_usage , sched_options );
37063738 }
37073739
3708- out :
3709- free (sched .curr_pid );
3710- free (sched .cpu_last_switched );
3711-
3712- return ret ;
3740+ return 0 ;
37133741}
0 commit comments