Skip to content

Commit e942ce1

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 4d2df69 commit e942ce1

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
@@ -3199,14 +3199,44 @@ static void perf_sched__merge_lat(struct perf_sched *sched)
31993199
}
32003200
}
32013201

3202+
static int setup_cpus_switch_event(struct perf_sched *sched)
3203+
{
3204+
unsigned int i;
3205+
3206+
sched->cpu_last_switched = calloc(MAX_CPUS, sizeof(*(sched->cpu_last_switched)));
3207+
if (!sched->cpu_last_switched)
3208+
return -1;
3209+
3210+
sched->curr_pid = malloc(MAX_CPUS * sizeof(*(sched->curr_pid)));
3211+
if (!sched->curr_pid) {
3212+
zfree(&sched->cpu_last_switched);
3213+
return -1;
3214+
}
3215+
3216+
for (i = 0; i < MAX_CPUS; i++)
3217+
sched->curr_pid[i] = -1;
3218+
3219+
return 0;
3220+
}
3221+
3222+
static void free_cpus_switch_event(struct perf_sched *sched)
3223+
{
3224+
zfree(&sched->curr_pid);
3225+
zfree(&sched->cpu_last_switched);
3226+
}
3227+
32023228
static int perf_sched__lat(struct perf_sched *sched)
32033229
{
3230+
int rc = -1;
32043231
struct rb_node *next;
32053232

32063233
setup_pager();
32073234

3235+
if (setup_cpus_switch_event(sched))
3236+
return rc;
3237+
32083238
if (perf_sched__read_events(sched))
3209-
return -1;
3239+
goto out_free_cpus_switch_event;
32103240

32113241
perf_sched__merge_lat(sched);
32123242
perf_sched__sort_lat(sched);
@@ -3235,7 +3265,11 @@ static int perf_sched__lat(struct perf_sched *sched)
32353265
print_bad_events(sched);
32363266
printf("\n");
32373267

3238-
return 0;
3268+
rc = 0;
3269+
3270+
out_free_cpus_switch_event:
3271+
free_cpus_switch_event(sched);
3272+
return rc;
32393273
}
32403274

32413275
static int setup_map_cpus(struct perf_sched *sched)
@@ -3302,9 +3336,12 @@ static int perf_sched__map(struct perf_sched *sched)
33023336
if (!sched->curr_thread)
33033337
return rc;
33043338

3305-
if (setup_map_cpus(sched))
3339+
if (setup_cpus_switch_event(sched))
33063340
goto out_free_curr_thread;
33073341

3342+
if (setup_map_cpus(sched))
3343+
goto out_free_cpus_switch_event;
3344+
33083345
if (setup_color_pids(sched))
33093346
goto out_put_map_cpus;
33103347

@@ -3328,6 +3365,9 @@ static int perf_sched__map(struct perf_sched *sched)
33283365
zfree(&sched->map.comp_cpus);
33293366
perf_cpu_map__put(sched->map.cpus);
33303367

3368+
out_free_cpus_switch_event:
3369+
free_cpus_switch_event(sched);
3370+
33313371
out_free_curr_thread:
33323372
zfree(&sched->curr_thread);
33333373
return rc;
@@ -3341,14 +3381,18 @@ static int perf_sched__replay(struct perf_sched *sched)
33413381
mutex_init(&sched->start_work_mutex);
33423382
mutex_init(&sched->work_done_wait_mutex);
33433383

3384+
ret = setup_cpus_switch_event(sched);
3385+
if (ret)
3386+
goto out_mutex_destroy;
3387+
33443388
calibrate_run_measurement_overhead(sched);
33453389
calibrate_sleep_measurement_overhead(sched);
33463390

33473391
test_calibrations(sched);
33483392

33493393
ret = perf_sched__read_events(sched);
33503394
if (ret)
3351-
goto out_mutex_destroy;
3395+
goto out_free_cpus_switch_event;
33523396

33533397
printf("nr_run_events: %ld\n", sched->nr_run_events);
33543398
printf("nr_sleep_events: %ld\n", sched->nr_sleep_events);
@@ -3374,6 +3418,9 @@ static int perf_sched__replay(struct perf_sched *sched)
33743418
sched->thread_funcs_exit = true;
33753419
destroy_tasks(sched);
33763420

3421+
out_free_cpus_switch_event:
3422+
free_cpus_switch_event(sched);
3423+
33773424
out_mutex_destroy:
33783425
mutex_destroy(&sched->start_work_mutex);
33793426
mutex_destroy(&sched->work_done_wait_mutex);
@@ -3612,21 +3659,7 @@ int cmd_sched(int argc, const char **argv)
36123659
.switch_event = replay_switch_event,
36133660
.fork_event = replay_fork_event,
36143661
};
3615-
unsigned int i;
3616-
int ret = 0;
3617-
3618-
sched.cpu_last_switched = calloc(MAX_CPUS, sizeof(*sched.cpu_last_switched));
3619-
if (!sched.cpu_last_switched) {
3620-
ret = -ENOMEM;
3621-
goto out;
3622-
}
3623-
sched.curr_pid = malloc(MAX_CPUS * sizeof(*sched.curr_pid));
3624-
if (!sched.curr_pid) {
3625-
ret = -ENOMEM;
3626-
goto out;
3627-
}
3628-
for (i = 0; i < MAX_CPUS; i++)
3629-
sched.curr_pid[i] = -1;
3662+
int ret;
36303663

36313664
argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
36323665
sched_usage, PARSE_OPT_STOP_AT_NON_OPTION);
@@ -3637,9 +3670,9 @@ int cmd_sched(int argc, const char **argv)
36373670
* Aliased to 'perf script' for now:
36383671
*/
36393672
if (!strcmp(argv[0], "script")) {
3640-
ret = cmd_script(argc, argv);
3673+
return cmd_script(argc, argv);
36413674
} else if (strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
3642-
ret = __cmd_record(argc, argv);
3675+
return __cmd_record(argc, argv);
36433676
} else if (strlen(argv[0]) > 2 && strstarts("latency", argv[0])) {
36443677
sched.tp_handler = &lat_ops;
36453678
if (argc > 1) {
@@ -3648,7 +3681,7 @@ int cmd_sched(int argc, const char **argv)
36483681
usage_with_options(latency_usage, latency_options);
36493682
}
36503683
setup_sorting(&sched, latency_options, latency_usage);
3651-
ret = perf_sched__lat(&sched);
3684+
return perf_sched__lat(&sched);
36523685
} else if (!strcmp(argv[0], "map")) {
36533686
if (argc) {
36543687
argc = parse_options(argc, argv, map_options, map_usage, 0);
@@ -3657,15 +3690,15 @@ int cmd_sched(int argc, const char **argv)
36573690
}
36583691
sched.tp_handler = &map_ops;
36593692
setup_sorting(&sched, latency_options, latency_usage);
3660-
ret = perf_sched__map(&sched);
3693+
return perf_sched__map(&sched);
36613694
} else if (strlen(argv[0]) > 2 && strstarts("replay", argv[0])) {
36623695
sched.tp_handler = &replay_ops;
36633696
if (argc) {
36643697
argc = parse_options(argc, argv, replay_options, replay_usage, 0);
36653698
if (argc)
36663699
usage_with_options(replay_usage, replay_options);
36673700
}
3668-
ret = perf_sched__replay(&sched);
3701+
return perf_sched__replay(&sched);
36693702
} else if (!strcmp(argv[0], "timehist")) {
36703703
if (argc) {
36713704
argc = parse_options(argc, argv, timehist_options,
@@ -3681,21 +3714,16 @@ int cmd_sched(int argc, const char **argv)
36813714
parse_options_usage(NULL, timehist_options, "w", true);
36823715
if (sched.show_next)
36833716
parse_options_usage(NULL, timehist_options, "n", true);
3684-
ret = -EINVAL;
3685-
goto out;
3717+
return -EINVAL;
36863718
}
36873719
ret = symbol__validate_sym_arguments();
36883720
if (ret)
3689-
goto out;
3721+
return ret;
36903722

3691-
ret = perf_sched__timehist(&sched);
3723+
return perf_sched__timehist(&sched);
36923724
} else {
36933725
usage_with_options(sched_usage, sched_options);
36943726
}
36953727

3696-
out:
3697-
free(sched.curr_pid);
3698-
free(sched.cpu_last_switched);
3699-
3700-
return ret;
3728+
return 0;
37013729
}

0 commit comments

Comments
 (0)