Skip to content

Commit 6ec7796

Browse files
captain5050gregkh
authored andcommitted
perf sched: Avoid large stack allocations
[ Upstream commit 232418a ] Commit 5ded57a ("perf inject: Remove static variables") moved static variables to local, however, in this case 3 MAX_CPUS (4096) sized arrays were moved onto the stack making the stack frame quite large. Avoid the stack usage by dynamically allocating the arrays. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]> Stable-dep-of: 1a5efc9 ("libsubcmd: Don't free the usage string") Signed-off-by: Sasha Levin <[email protected]>
1 parent 7e33362 commit 6ec7796

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

tools/perf/builtin-sched.c

+22-4
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ struct perf_sched {
193193
* weird events, such as a task being switched away that is not current.
194194
*/
195195
struct perf_cpu max_cpu;
196-
u32 curr_pid[MAX_CPUS];
197-
struct thread *curr_thread[MAX_CPUS];
196+
u32 *curr_pid;
197+
struct thread **curr_thread;
198198
char next_shortname1;
199199
char next_shortname2;
200200
unsigned int replay_repeat;
@@ -224,7 +224,7 @@ struct perf_sched {
224224
u64 run_avg;
225225
u64 all_runtime;
226226
u64 all_count;
227-
u64 cpu_last_switched[MAX_CPUS];
227+
u64 *cpu_last_switched;
228228
struct rb_root_cached atom_root, sorted_atom_root, merged_atom_root;
229229
struct list_head sort_list, cmp_pid;
230230
bool force;
@@ -3590,7 +3590,22 @@ int cmd_sched(int argc, const char **argv)
35903590

35913591
mutex_init(&sched.start_work_mutex);
35923592
mutex_init(&sched.work_done_wait_mutex);
3593-
for (i = 0; i < ARRAY_SIZE(sched.curr_pid); i++)
3593+
sched.curr_thread = calloc(MAX_CPUS, sizeof(*sched.curr_thread));
3594+
if (!sched.curr_thread) {
3595+
ret = -ENOMEM;
3596+
goto out;
3597+
}
3598+
sched.cpu_last_switched = calloc(MAX_CPUS, sizeof(*sched.cpu_last_switched));
3599+
if (!sched.cpu_last_switched) {
3600+
ret = -ENOMEM;
3601+
goto out;
3602+
}
3603+
sched.curr_pid = malloc(MAX_CPUS * sizeof(*sched.curr_pid));
3604+
if (!sched.curr_pid) {
3605+
ret = -ENOMEM;
3606+
goto out;
3607+
}
3608+
for (i = 0; i < MAX_CPUS; i++)
35943609
sched.curr_pid[i] = -1;
35953610

35963611
argc = parse_options_subcommand(argc, argv, sched_options, sched_subcommands,
@@ -3659,6 +3674,9 @@ int cmd_sched(int argc, const char **argv)
36593674
}
36603675

36613676
out:
3677+
free(sched.curr_pid);
3678+
free(sched.cpu_last_switched);
3679+
free(sched.curr_thread);
36623680
mutex_destroy(&sched.start_work_mutex);
36633681
mutex_destroy(&sched.work_done_wait_mutex);
36643682

0 commit comments

Comments
 (0)