Skip to contentSkip to conversation

perf: limit memory allocation to number cpus online

PATCH In review Tony Jones 2 participants 2 messages Mar 30, 2010 – Apr 26, 2010 perf events

lore
Share & tools
lore

Tony Jones

author
#0 lore raw

PATCH 1/1 perf: limit memory allocation to number cpus online

Subject: perf: limit memory allocation to number cpus online
From: Tony Jones <[email protected]>
Date: Tue, 30 Mar 2010 12:18:08 -0700

Limit the dynamic allocation of fd, event_array and mmap_array to #cpus online rather than MAX_NR_CPUS.

---
 tools/perf/builtin-record.c |   10 +++++++---
 tools/perf/builtin-stat.c   |    4 ++--
 tools/perf/builtin-top.c    |   14 +++++++-------
 3 files changed, 16 insertions(+), 12 deletions(-)
tools/perf/builtin-record.c +7 -3
index 60ecdd3..25b5536 100644
@@ -560,7 +560,6 @@ static int __cmd_record(int argc, const char **argv)
 	if ((!system_wide && !inherit) || profile_cpu != -1) {
 		open_counters(profile_cpu);
 	} else {
-		nr_cpus = read_cpu_map();
 		for (i = 0; i < nr_cpus; i++)
 			open_counters(cpumap[i]);
 	}
@@ -748,7 +747,12 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
 		thread_num = 1;
 	}
 
-	for (i = 0; i < MAX_NR_CPUS; i++) {
+	if (system_wide && profile_cpu == -1)
+		nr_cpus = read_cpu_map();
+	else
+		nr_cpus = 1;
+
+	for (i = 0; i < nr_cpus; i++) {
 		for (j = 0; j < MAX_COUNTERS; j++) {
 			fd[i][j] = malloc(sizeof(int)*thread_num);
 			mmap_array[i][j] = zalloc(
@@ -758,7 +762,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
 		}
 	}
 	event_array = malloc(
-		sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
+		sizeof(struct pollfd)*nr_cpus*MAX_COUNTERS*thread_num);
 	if (!event_array)
 		return -ENOMEM;
 
tools/perf/builtin-stat.c +2 -2
index 1036ca7..d9b672e 100644
@@ -540,7 +540,7 @@ static const struct option options[] = {
 int cmd_stat(int argc, const char **argv, const char *prefix __used)
 {
 	int status;
-	int i,j;
+	unsigned int i,j;
 
 	argc = parse_options(argc, argv, options, stat_usage,
 		PARSE_OPT_STOP_AT_NON_OPTION);
@@ -577,7 +577,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
 		thread_num = 1;
 	}
 
-	for (i = 0; i < MAX_NR_CPUS; i++) {
+	for (i = 0; i < nr_cpus; i++) {
 		for (j = 0; j < MAX_COUNTERS; j++) {
 			fd[i][j] = malloc(sizeof(int)*thread_num);
 			if (!fd[i][j])
tools/perf/builtin-top.c +7 -7
index 4abdd9b..f05d270 100644
@@ -1368,7 +1368,12 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 		thread_num = 1;
 	}
 
-	for (i = 0; i < MAX_NR_CPUS; i++) {
+	if (target_tid != -1 || profile_cpu != -1)
+		nr_cpus = 1;
+	else
+		nr_cpus = read_cpu_map();
+
+	for (i = 0; i < nr_cpus; i++) {
 		for (j = 0; j < MAX_COUNTERS; j++) {
 			fd[i][j] = malloc(sizeof(int)*thread_num);
 			mmap_array[i][j] = zalloc(
@@ -1378,7 +1383,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 		}
 	}
 	event_array = malloc(
-		sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
+		sizeof(struct pollfd)*nr_cpus*MAX_COUNTERS*thread_num);
 	if (!event_array)
 		return -ENOMEM;
 
@@ -1424,11 +1429,6 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 		attrs[counter].sample_period = default_interval;
 	}
 
-	if (target_tid != -1 || profile_cpu != -1)
-		nr_cpus = 1;
-	else
-		nr_cpus = read_cpu_map();
-
 	get_term_dimensions(&winsize);
 	if (print_entries == 0) {
 		update_print_entries(&winsize);
Signature · 1 line
1.6.4.2
#1 lore raw
Quoted 6 lines · Tony Jones

Subject: perf: limit memory allocation to number cpus online
From: Tony Jones <[email protected]>
Date: Tue, 30 Mar 2010 12:18:08 -0700

Limit the dynamic allocation of fd, event_array and mmap_array to #cpus online rather than MAX_NR_CPUS.

Acked. Sorry for replying late. There will be a much more valuable patch if it fixes the cpu hotplug issue. For example, if my machine starts 16 cpu and I hot unplug some of them by software method (i.e. echo 0 >/sys/devices/system/cpu/cpuXXX/online) after kernel boots, perf will fail when collecting a system-wide statistics. That's because sys_perf_event_open fails on removed cpu.

Quoted 105 lines
---
 tools/perf/builtin-record.c |   10 +++++++---
 tools/perf/builtin-stat.c   |    4 ++--
 tools/perf/builtin-top.c    |   14 +++++++-------
 3 files changed, 16 insertions(+), 12 deletions(-)
tools/perf/builtin-record.c +7 -3
index 60ecdd3..25b5536 100644
@@ -560,7 +560,6 @@ static int __cmd_record(int argc, const char **argv)
 	if ((!system_wide && !inherit) || profile_cpu != -1) {
 		open_counters(profile_cpu);
 	} else {
-		nr_cpus = read_cpu_map();
 		for (i = 0; i < nr_cpus; i++)
 			open_counters(cpumap[i]);
 	}
@@ -748,7 +747,12 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
 		thread_num = 1;
 	}
 
-	for (i = 0; i < MAX_NR_CPUS; i++) {
+	if (system_wide && profile_cpu == -1)
+		nr_cpus = read_cpu_map();
+	else
+		nr_cpus = 1;
+
+	for (i = 0; i < nr_cpus; i++) {
 		for (j = 0; j < MAX_COUNTERS; j++) {
 			fd[i][j] = malloc(sizeof(int)*thread_num);
 			mmap_array[i][j] = zalloc(
@@ -758,7 +762,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
 		}
 	}
 	event_array = malloc(
-		sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
+		sizeof(struct pollfd)*nr_cpus*MAX_COUNTERS*thread_num);
 	if (!event_array)
 		return -ENOMEM;
 
tools/perf/builtin-stat.c +2 -2
index 1036ca7..d9b672e 100644
@@ -540,7 +540,7 @@ static const struct option options[] = {
 int cmd_stat(int argc, const char **argv, const char *prefix __used)
 {
 	int status;
-	int i,j;
+	unsigned int i,j;
 
 	argc = parse_options(argc, argv, options, stat_usage,
 		PARSE_OPT_STOP_AT_NON_OPTION);
@@ -577,7 +577,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
 		thread_num = 1;
 	}
 
-	for (i = 0; i < MAX_NR_CPUS; i++) {
+	for (i = 0; i < nr_cpus; i++) {
 		for (j = 0; j < MAX_COUNTERS; j++) {
 			fd[i][j] = malloc(sizeof(int)*thread_num);
 			if (!fd[i][j])
tools/perf/builtin-top.c +7 -7
index 4abdd9b..f05d270 100644
@@ -1368,7 +1368,12 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 		thread_num = 1;
 	}
 
-	for (i = 0; i < MAX_NR_CPUS; i++) {
+	if (target_tid != -1 || profile_cpu != -1)
+		nr_cpus = 1;
+	else
+		nr_cpus = read_cpu_map();
+
+	for (i = 0; i < nr_cpus; i++) {
 		for (j = 0; j < MAX_COUNTERS; j++) {
 			fd[i][j] = malloc(sizeof(int)*thread_num);
 			mmap_array[i][j] = zalloc(
@@ -1378,7 +1383,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 		}
 	}
 	event_array = malloc(
-		sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
+		sizeof(struct pollfd)*nr_cpus*MAX_COUNTERS*thread_num);
 	if (!event_array)
 		return -ENOMEM;
 
@@ -1424,11 +1429,6 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 		attrs[counter].sample_period = default_interval;
 	}
 
-	if (target_tid != -1 || profile_cpu != -1)
-		nr_cpus = 1;
-	else
-		nr_cpus = read_cpu_map();
-
 	get_term_dimensions(&winsize);
 	if (print_entries == 0) {
 		update_print_entries(&winsize);