Skip to content

Commit 03cec19

Browse files
Yang Jihonggregkh
Yang Jihong
authored andcommitted
perf sched: Move curr_pid and cpu_last_switched initialization to perf_sched__{lat|map|replay}()
[ Upstream commit bd2cdf2 ] The curr_pid and cpu_last_switched are used only for the 'perf sched replay/latency/map'. Put their initialization in perf_sched__{lat|map|replay () to reduce unnecessary actions in other commands. Simple functional testing: # perf sched record perf bench sched messaging # Running 'sched/messaging' benchmark: # 20 sender and receiver processes per group # 10 groups == 400 processes run Total time: 0.209 [sec] [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 16.456 MB perf.data (147907 samples) ] # perf sched lat ------------------------------------------------------------------------------------------------------------------------------------------- Task | Runtime ms | Switches | Avg delay ms | Max delay ms | Max delay start | Max delay end | ------------------------------------------------------------------------------------------------------------------------------------------- sched-messaging:(401) | 2990.699 ms | 38705 | avg: 0.661 ms | max: 67.046 ms | max start: 456532.624830 s | max end: 456532.691876 s qemu-system-x86:(7) | 179.764 ms | 2191 | avg: 0.152 ms | max: 21.857 ms | max start: 456532.576434 s | max end: 456532.598291 s sshd:48125 | 0.522 ms | 2 | avg: 0.037 ms | max: 0.046 ms | max start: 456532.514610 s | max end: 456532.514656 s <SNIP> ksoftirqd/11:82 | 0.063 ms | 1 | avg: 0.005 ms | max: 0.005 ms | max start: 456532.769366 s | max end: 456532.769371 s kworker/9:0-mm_:34624 | 0.233 ms | 20 | avg: 0.004 ms | max: 0.007 ms | max start: 456532.690804 s | max end: 456532.690812 s migration/13:93 | 0.000 ms | 1 | avg: 0.004 ms | max: 0.004 ms | max start: 456532.512669 s | max end: 456532.512674 s ----------------------------------------------------------------------------------------------------------------- TOTAL: | 3180.750 ms | 41368 | --------------------------------------------------- # echo $? 0 # perf sched map *A0 456532.510141 secs A0 => migration/0:15 *. 456532.510171 secs . => swapper:0 . *B0 456532.510261 secs B0 => migration/1:21 . *. 456532.510279 secs <SNIP> L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 *L7 . . . . 456532.785979 secs L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 *L7 . . . 456532.786054 secs L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 *L7 . . 456532.786127 secs L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 *L7 . 456532.786197 secs L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 L7 *L7 456532.786270 secs # echo $? 0 # perf sched replay run measurement overhead: 108 nsecs sleep measurement overhead: 66473 nsecs the run test took 1000002 nsecs the sleep test took 1082686 nsecs nr_run_events: 49334 nr_sleep_events: 50054 nr_wakeup_events: 34701 target-less wakeups: 165 multi-target wakeups: 766 task 0 ( swapper: 0), nr_events: 15419 task 1 ( swapper: 1), nr_events: 1 task 2 ( swapper: 2), nr_events: 1 <SNIP> task 715 ( sched-messaging: 110248), nr_events: 1438 task 716 ( sched-messaging: 110249), nr_events: 512 task 717 ( sched-messaging: 110250), nr_events: 500 task 718 ( sched-messaging: 110251), nr_events: 537 task 719 ( sched-messaging: 110252), nr_events: 823 ------------------------------------------------------------ #1 : 1325.288, ravg: 1325.29, cpu: 7823.35 / 7823.35 #2 : 1363.606, ravg: 1329.12, cpu: 7655.53 / 7806.56 #3 : 1349.494, ravg: 1331.16, cpu: 7544.80 / 7780.39 #4 : 1311.488, ravg: 1329.19, cpu: 7495.13 / 7751.86 #5 : 1309.902, ravg: 1327.26, cpu: 7266.65 / 7703.34 #6 : 1309.535, ravg: 1325.49, cpu: 7843.86 / 7717.39 #7 : 1316.482, ravg: 1324.59, cpu: 7854.41 / 7731.09 #8 : 1366.604, ravg: 1328.79, cpu: 7955.81 / 7753.57 #9 : 1326.286, ravg: 1328.54, cpu: 7466.86 / 7724.90 #10 : 1356.653, ravg: 1331.35, cpu: 7566.60 / 7709.07 # echo $? 0 Signed-off-by: Yang Jihong <[email protected]> Signed-off-by: Namhyung Kim <[email protected]> Link: https://lore.kernel.org/r/[email protected] Stable-dep-of: 1a5efc9 ("libsubcmd: Don't free the usage string") Signed-off-by: Sasha Levin <[email protected]>
1 parent 4078f1d commit 03cec19

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

tools/perf/builtin-sched.c

+61-33
Original file line numberDiff line numberDiff line change
@@ -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+
32143240
static 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

32533287
static 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+
33433383
out_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+
33893436
out_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

Comments
 (0)