Skip to contentSkip to conversation

perf tools: Don't die in perf_header_attr__new

PATCH In review Arnaldo Carvalho de Melo 2 participants 4 messages Nov 16, 2009 – Nov 17, 2009 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf tools: Don't die in perf_header_attr__new

From: Arnaldo Carvalho de Melo <[email protected]>

We really should propagate such kinds of errors so that users of these library functions decide what to do in such cases instead of exiting in random places like now.

Cc Frederic Weisbecker <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/perf/builtin-record.c |    5 ++++-
 tools/perf/util/header.c    |   22 ++++++++++++----------
 tools/perf/util/header.h    |    4 +---
 3 files changed, 17 insertions(+), 14 deletions(-)
tools/perf/builtin-record.c +4 -1
index 04f335e..4c03bb7 100644
@@ -220,7 +220,8 @@ static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int n
 		h_attr = header->attr[nr];
 	} else {
 		h_attr = perf_header_attr__new(a);
-		perf_header__add_attr(header, h_attr);
+		if (h_attr != NULL)
+			perf_header__add_attr(header, h_attr);
 	}
 
 	return h_attr;
@@ -308,6 +309,8 @@ try_again:
 	}
 
 	h_attr = get_header_attr(attr, counter);
+	if (h_attr == NULL)
+		die("nomem\n");
 
 	if (!file_new) {
 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
tools/perf/util/header.c +12 -10
index d8416f0..2f07a23 100644
@@ -19,16 +19,16 @@ struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
 {
 	struct perf_header_attr *self = malloc(sizeof(*self));
 
-	if (!self)
-		die("nomem");
-
-	self->attr = *attr;
-	self->ids = 0;
-	self->size = 1;
-	self->id = malloc(sizeof(u64));
-
-	if (!self->id)
-		die("nomem");
+	if (self != NULL) {
+		self->attr = *attr;
+		self->ids  = 0;
+		self->size = 1;
+		self->id   = malloc(sizeof(u64));
+		if (self->id == NULL) {
+			free(self);
+			self = NULL;
+		}
+	}
 
 	return self;
 }
@@ -423,6 +423,8 @@ struct perf_header *perf_header__read(int fd)
 		tmp = lseek(fd, 0, SEEK_CUR);
 
 		attr = perf_header_attr__new(&f_attr.attr);
+		if (attr == NULL)
+			 die("nomem");
 
 		nr_ids = f_attr.ids.size / sizeof(u64);
 		lseek(fd, f_attr.ids.offset, SEEK_SET);
tools/perf/util/header.h +1 -3
index f1b3bf7..0cbd4c9 100644
@@ -64,9 +64,7 @@ void perf_header__add_attr(struct perf_header *self,
 void perf_header__push_event(u64 id, const char *name);
 char *perf_header__find_event(u64 id);
 
-
-struct perf_header_attr *
-perf_header_attr__new(struct perf_event_attr *attr);
+struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr);
 void perf_header_attr__add_id(struct perf_header_attr *self, u64 id);
 
 u64 perf_header__sample_type(struct perf_header *header);
Signature · 1 line
1.6.2.5
#1 lore raw

PATCH 2/2 perf top: Use all the lines in the screen

From: Arnaldo Carvalho de Melo <[email protected]>

By querying the current number of rows, if the user specifies the number of entries, use that instead. If the user uses the 'e' command to change the number of lines 0 will mean do it automatically, any other number disables the auto resizing.

Cc Frederic Weisbecker <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/perf/builtin-top.c |   42 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 41 insertions(+), 1 deletions(-)
tools/perf/builtin-top.c +41 -1
index 6613f98..3af9520 100644
@@ -60,7 +60,7 @@ static int			system_wide			=      0;
 static int			default_interval		=      0;
 
 static int			count_filter			=      5;
-static int			print_entries			=     15;
+static int			print_entries;
 
 static int			target_pid			=     -1;
 static int			inherit				=      0;
@@ -115,6 +115,36 @@ struct sym_entry {
  * Source functions
  */
 
+/* most GUI terminals set LINES (although some don't export it) */
+static int term_rows(void)
+{
+	char *lines_string = getenv("LINES");
+	int n_lines;
+
+	if (lines_string && (n_lines = atoi(lines_string)) > 0)
+		return n_lines;
+#ifdef TIOCGWINSZ
+	else {
+		struct winsize ws;
+		if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_row)
+			return ws.ws_row;
+	}
+#endif
+	return 25;
+}
+
+static void update_print_entries(void)
+{
+	print_entries = term_rows();
+	if (print_entries > 9)
+		print_entries -= 9;
+}
+
+static void sig_winch_handler(int sig __used)
+{
+	update_print_entries();
+}
+
 static void parse_source(struct sym_entry *syme)
 {
 	struct symbol *sym;
@@ -668,6 +698,11 @@ static void handle_keypress(int c)
 			break;
 		case 'e':
 			prompt_integer(&print_entries, "Enter display entries (lines)");
+			if (print_entries == 0) {
+				update_print_entries();
+				signal(SIGWINCH, sig_winch_handler);
+			} else
+				signal(SIGWINCH, SIG_DFL);
 			break;
 		case 'E':
 			if (nr_counters > 1) {
@@ -1228,5 +1263,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 	if (target_pid != -1 || profile_cpu != -1)
 		nr_cpus = 1;
 
+	if (print_entries == 0) {
+		update_print_entries();
+		signal(SIGWINCH, sig_winch_handler);
+	}
+
 	return __cmd_top();
 }
Signature · 1 line
1.6.2.5
#2 lore raw

[tip:perf/core] perf top: Use all the lines in the screen

Commit-ID:  3b6ed98895b0fccd8c387f3fc44016fb922c0658
Gitweb:     http://git.kernel.org/tip/3b6ed98895b0fccd8c387f3fc44016fb922c0658
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Mon, 16 Nov 2009 19:30:27 -0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 17 Nov 2009 07:19:53 +0100

perf top: Use all the lines in the screen

By querying the current number of rows, if the user specifies the number of entries, use that instead. If the user uses the 'e' command to change the number of lines 0 will mean do it automatically, any other number disables the auto resizing.

Cc Frederic Weisbecker <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>

LKML-Reference: <[email protected]>

---
 tools/perf/builtin-top.c |   42 +++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 41 insertions(+), 1 deletions(-)
tools/perf/builtin-top.c +41 -1
index 6613f98..3af9520 100644
@@ -60,7 +60,7 @@ static int			system_wide			=      0;
 static int			default_interval		=      0;
 
 static int			count_filter			=      5;
-static int			print_entries			=     15;
+static int			print_entries;
 
 static int			target_pid			=     -1;
 static int			inherit				=      0;
@@ -115,6 +115,36 @@ struct sym_entry {
  * Source functions
  */
 
+/* most GUI terminals set LINES (although some don't export it) */
+static int term_rows(void)
+{
+	char *lines_string = getenv("LINES");
+	int n_lines;
+
+	if (lines_string && (n_lines = atoi(lines_string)) > 0)
+		return n_lines;
+#ifdef TIOCGWINSZ
+	else {
+		struct winsize ws;
+		if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_row)
+			return ws.ws_row;
+	}
+#endif
+	return 25;
+}
+
+static void update_print_entries(void)
+{
+	print_entries = term_rows();
+	if (print_entries > 9)
+		print_entries -= 9;
+}
+
+static void sig_winch_handler(int sig __used)
+{
+	update_print_entries();
+}
+
 static void parse_source(struct sym_entry *syme)
 {
 	struct symbol *sym;
@@ -668,6 +698,11 @@ static void handle_keypress(int c)
 			break;
 		case 'e':
 			prompt_integer(&print_entries, "Enter display entries (lines)");
+			if (print_entries == 0) {
+				update_print_entries();
+				signal(SIGWINCH, sig_winch_handler);
+			} else
+				signal(SIGWINCH, SIG_DFL);
 			break;
 		case 'E':
 			if (nr_counters > 1) {
@@ -1228,5 +1263,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
 	if (target_pid != -1 || profile_cpu != -1)
 		nr_cpus = 1;
 
+	if (print_entries == 0) {
+		update_print_entries();
+		signal(SIGWINCH, sig_winch_handler);
+	}
+
 	return __cmd_top();
 }
#3 lore raw

[tip:perf/core] perf tools: Don't die in perf_header_attr__new()

In reply to Arnaldo Carvalho de Melo
Commit-ID:  dc79c0fc08a94b857aed446bfb47cdfde529400c
Gitweb:     http://git.kernel.org/tip/dc79c0fc08a94b857aed446bfb47cdfde529400c
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Mon, 16 Nov 2009 19:30:26 -0200
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 17 Nov 2009 07:19:52 +0100

perf tools: Don't die in perf_header_attr__new()

We really should propagate such kinds of errors so that users of these library functions decide what to do in such cases instead of exiting in random places like now.

Cc Frederic Weisbecker <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>

LKML-Reference: <[email protected]>

---
 tools/perf/builtin-record.c |    5 ++++-
 tools/perf/util/header.c    |   22 ++++++++++++----------
 tools/perf/util/header.h    |    4 +---
 3 files changed, 17 insertions(+), 14 deletions(-)
tools/perf/builtin-record.c +4 -1
index 04f335e..4c03bb7 100644
@@ -220,7 +220,8 @@ static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int n
 		h_attr = header->attr[nr];
 	} else {
 		h_attr = perf_header_attr__new(a);
-		perf_header__add_attr(header, h_attr);
+		if (h_attr != NULL)
+			perf_header__add_attr(header, h_attr);
 	}
 
 	return h_attr;
@@ -308,6 +309,8 @@ try_again:
 	}
 
 	h_attr = get_header_attr(attr, counter);
+	if (h_attr == NULL)
+		die("nomem\n");
 
 	if (!file_new) {
 		if (memcmp(&h_attr->attr, attr, sizeof(*attr))) {
tools/perf/util/header.c +12 -10
index d8416f0..2f07a23 100644
@@ -19,16 +19,16 @@ struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr)
 {
 	struct perf_header_attr *self = malloc(sizeof(*self));
 
-	if (!self)
-		die("nomem");
-
-	self->attr = *attr;
-	self->ids = 0;
-	self->size = 1;
-	self->id = malloc(sizeof(u64));
-
-	if (!self->id)
-		die("nomem");
+	if (self != NULL) {
+		self->attr = *attr;
+		self->ids  = 0;
+		self->size = 1;
+		self->id   = malloc(sizeof(u64));
+		if (self->id == NULL) {
+			free(self);
+			self = NULL;
+		}
+	}
 
 	return self;
 }
@@ -423,6 +423,8 @@ struct perf_header *perf_header__read(int fd)
 		tmp = lseek(fd, 0, SEEK_CUR);
 
 		attr = perf_header_attr__new(&f_attr.attr);
+		if (attr == NULL)
+			 die("nomem");
 
 		nr_ids = f_attr.ids.size / sizeof(u64);
 		lseek(fd, f_attr.ids.offset, SEEK_SET);
tools/perf/util/header.h +1 -3
index f1b3bf7..0cbd4c9 100644
@@ -64,9 +64,7 @@ void perf_header__add_attr(struct perf_header *self,
 void perf_header__push_event(u64 id, const char *name);
 char *perf_header__find_event(u64 id);
 
-
-struct perf_header_attr *
-perf_header_attr__new(struct perf_event_attr *attr);
+struct perf_header_attr *perf_header_attr__new(struct perf_event_attr *attr);
 void perf_header_attr__add_id(struct perf_header_attr *self, u64 id);
 
 u64 perf_header__sample_type(struct perf_header *header);