Skip to contentSkip to conversation

tools lib traceevent: Get rid of *die() calls finally!! (v2)

PATCH In review Namhyung Kim 4 participants 11 messages Jan 7, 2014 – Jan 8, 2014

lore
Share & tools
lore
#0 lore raw

cover tools lib traceevent: Get rid of *die() calls finally!! (v2)

Hello,

I've added WARN_ONCE in TRACE_SEQ_CHECK() as Jiri said. Other patches are same as the privous version.

You can also get this on my 'libtraceevent/die-removal-v5' branch in my tree

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung

Namhyung Kim (5):
tools lib traceevent: Add WARN and WARN_ONCE macros
tools lib traceevent: Add state member to struct trace_seq tools lib traceevent: Check return value of realloc() tools lib traceevent: Get rid of malloc_or_die() in trace_seq_init() tools lib traceevent: Get rid of die() finally!!

 tools/lib/traceevent/event-parse.h |  7 +++++
 tools/lib/traceevent/event-utils.h | 26 ++++++++++++++---
 tools/lib/traceevent/parse-utils.c | 44 -----------------------------
 tools/lib/traceevent/trace-seq.c   | 57 +++++++++++++++++++++++++++++++++-----
 4 files changed, 79 insertions(+), 55 deletions(-)
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/5 tools lib traceevent: Add WARN and WARN_ONCE macros

They're copied from the perf code and will be used to print error message during trace_seq_printf() and friends.

---
 tools/lib/traceevent/event-utils.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/lib/traceevent/event-utils.h +22 -0
index e76c9acb92cd..5dc0aec5ab50 100644
@@ -38,6 +38,28 @@ void __vdie(const char *fmt, ...);
 void __vwarning(const char *fmt, ...);
 void __vpr_stat(const char *fmt, ...);
 
+#define likely(x)	__builtin_expect(!!(x), 1)
+#define unlikely(x)	__builtin_expect(!!(x), 0)
+
+#define __WARN_printf(arg...)	do { fprintf(stderr, arg); } while (0)
+
+#define WARN(condition, format...) ({		\
+	int __ret_warn_on = !!(condition);	\
+	if (unlikely(__ret_warn_on))		\
+		__WARN_printf(format);		\
+	unlikely(__ret_warn_on);		\
+})
+
+#define WARN_ONCE(condition, format...)	({	\
+	static int __warned;			\
+	int __ret_warn_once = !!(condition);	\
+						\
+	if (unlikely(__ret_warn_once))		\
+		if (WARN(!__warned, format)) 	\
+			__warned = 1;		\
+	unlikely(__ret_warn_once);		\
+})
+
 #define min(x, y) ({				\
 	typeof(x) _min1 = (x);			\
 	typeof(y) _min2 = (y);			\
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH 1/5] tools lib traceevent: Add WARN and WARN_ONCE macros

Em Tue, Jan 07, 2014 at 12:00:08PM +0900, Namhyung Kim escreveu:

Quoted 5 lines

They're copied from the perf code and will be used to print error message during trace_seq_printf() and friends.

We could take this opportunity and come up with tools/lib/{bug,compiler}.h, matching the files in include/linux/ where those helpers come from, no?

Then make both tools/perf/ and tools/lib/traceevent/ use it, instead of creating the third copy (kernel proper, perf, libtraceevent).

Then, does anybody know why likely/unlikely is guarded inside __KERNEL__ in include/linux/compiler.h? I think the best thing would be for us to just use:

#include <linux/compiler.h>

just like kernel code and be done with it :-\

- Arnaldo

Quoted 39 lines
---
 tools/lib/traceevent/event-utils.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
tools/lib/traceevent/event-utils.h +22 -0
index e76c9acb92cd..5dc0aec5ab50 100644
@@ -38,6 +38,28 @@ void __vdie(const char *fmt, ...);
 void __vwarning(const char *fmt, ...);
 void __vpr_stat(const char *fmt, ...);
 
+#define likely(x)	__builtin_expect(!!(x), 1)
+#define unlikely(x)	__builtin_expect(!!(x), 0)
+
+#define __WARN_printf(arg...)	do { fprintf(stderr, arg); } while (0)
+
+#define WARN(condition, format...) ({		\
+	int __ret_warn_on = !!(condition);	\
+	if (unlikely(__ret_warn_on))		\
+		__WARN_printf(format);		\
+	unlikely(__ret_warn_on);		\
+})
+
+#define WARN_ONCE(condition, format...)	({	\
+	static int __warned;			\
+	int __ret_warn_once = !!(condition);	\
+						\
+	if (unlikely(__ret_warn_once))		\
+		if (WARN(!__warned, format)) 	\
+			__warned = 1;		\
+	unlikely(__ret_warn_once);		\
+})
+
 #define min(x, y) ({				\
 	typeof(x) _min1 = (x);			\
 	typeof(y) _min2 = (y);			\
Signature · 1 line
1.7.11.7
#3 lore raw

Re: [PATCH 1/5] tools lib traceevent: Add WARN and WARN_ONCE macros

Hi Arnaldo,

Quoted 10 lines · Arnaldo Carvalho de Melo

Em Tue, Jan 07, 2014 at 12:00:08PM +0900, Namhyung Kim escreveu:

Quoted 5 lines

They're copied from the perf code and will be used to print error message during trace_seq_printf() and friends.

We could take this opportunity and come up with tools/lib/{bug,compiler}.h, matching the files in include/linux/ where those helpers come from, no?

Hmm.. maybe under tools/include/ ?

Quoted 3 lines

Then make both tools/perf/ and tools/lib/traceevent/ use it, instead of creating the third copy (kernel proper, perf, libtraceevent).

Okay.

Quoted 3 lines

Then, does anybody know why likely/unlikely is guarded inside __KERNEL__ in include/linux/compiler.h?

IIUC it's because of BRANCH_PROFILING..

Quoted 5 lines

I think the best thing would be for us to just use:

#include <linux/compiler.h>

just like kernel code and be done with it :-\

Yeah, but it seems to require some modifications.

Thanks,
Namhyung

#4 lore raw

PATCH 2/5 tools lib traceevent: Add state member to struct trace_seq

In reply to Namhyung Kim

The trace_seq->state is for tracking errors during the use of trace_seq APIs and getting rid of die() in it.

---
 tools/lib/traceevent/event-parse.h |  7 +++++++
 tools/lib/traceevent/trace-seq.c   | 42 ++++++++++++++++++++++++++++++++++----
 2 files changed, 45 insertions(+), 4 deletions(-)
tools/lib/traceevent/event-parse.h +7 -0
index cf5db9013f2c..3c890cb28db7 100644
@@ -58,6 +58,12 @@ struct pevent_record {
 #endif
 };
 
+enum trace_seq_fail {
+	TRACE_SEQ__GOOD,
+	TRACE_SEQ__BUFFER_POISONED,
+	TRACE_SEQ__MEM_ALLOC_FAILED,
+};
+
 /*
  * Trace sequences are used to allow a function to call several other functions
  * to create a string of data to use (up to a max of PAGE_SIZE).
@@ -68,6 +74,7 @@ struct trace_seq {
 	unsigned int		buffer_size;
 	unsigned int		len;
 	unsigned int		readpos;
+	enum trace_seq_fail	state;
 };
 
 void trace_seq_init(struct trace_seq *s);
tools/lib/traceevent/trace-seq.c +38 -4

102 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#5 lore raw

Re: [PATCH 2/5] tools lib traceevent: Add state member to struct trace_seq

On Tue, Jan 07, 2014 at 12:00:09PM +0900, Namhyung Kim wrote:

SNIP

Quoted 26 lines

void trace_seq_init(struct trace_seq *s);

tools/lib/traceevent/trace-seq.c +4 -2
index d7f2e68bc5b9..1747aa50ecb0 100644
@@ -32,8 +32,9 @@
 #define TRACE_SEQ_POISON	((void *)0xdeadbeef)
 #define TRACE_SEQ_CHECK(s)						\
 do {									\
-	if ((s)->buffer == TRACE_SEQ_POISON)			\
-		die("Usage of trace_seq after it was destroyed");	\
+	if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON,			\
+		      "Usage of trace_seq after it was destroyed"))	\
+		(s)->state = TRACE_SEQ__BUFFER_POISONED;		\
 } while (0)
 
 /**
@@ -46,6 +47,7 @@ void trace_seq_init(struct trace_seq *s)
 	s->readpos = 0;
 	s->buffer_size = TRACE_SEQ_BUF_SIZE;
 	s->buffer = malloc_or_die(s->buffer_size);
+	s->state = TRACE_SEQ__GOOD;
 }
 
 /**

tools/lib/traceevent/trace-seq.c static void expand_buffer(struct trace_seq *s) · line 86

Quoted 5 lines
tools/lib/traceevent/trace-seq.c +1 -1
@@ -81,7 +83,7 @@ static void expand_buffer(struct trace_seq *s)
 	s->buffer_size += TRACE_SEQ_BUF_SIZE;
 	s->buffer = realloc(s->buffer, s->buffer_size);
 	if (!s->buffer)
-		die("Can't allocate trace_seq buffer memory");
+		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;

and also here?

	if (WARN_ONCE(!s->buffer))
		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;

otherwise the whole series seems ok,
jirka

#6 lore raw

Re: [PATCH 2/5] tools lib traceevent: Add state member to struct trace_seq

Hi Jiri,

Quoted 38 lines · Jiri Olsa

On Tue, Jan 07, 2014 at 12:00:09PM +0900, Namhyung Kim wrote:

SNIP

Quoted 26 lines

void trace_seq_init(struct trace_seq *s);

tools/lib/traceevent/trace-seq.c +4 -2
index d7f2e68bc5b9..1747aa50ecb0 100644
@@ -32,8 +32,9 @@
 #define TRACE_SEQ_POISON	((void *)0xdeadbeef)
 #define TRACE_SEQ_CHECK(s)						\
 do {									\
-	if ((s)->buffer == TRACE_SEQ_POISON)			\
-		die("Usage of trace_seq after it was destroyed");	\
+	if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON,			\
+		      "Usage of trace_seq after it was destroyed"))	\
+		(s)->state = TRACE_SEQ__BUFFER_POISONED;		\
 } while (0)
 
 /**
@@ -46,6 +47,7 @@ void trace_seq_init(struct trace_seq *s)
 	s->readpos = 0;
 	s->buffer_size = TRACE_SEQ_BUF_SIZE;
 	s->buffer = malloc_or_die(s->buffer_size);
+	s->state = TRACE_SEQ__GOOD;
 }
 
 /**

tools/lib/traceevent/trace-seq.c static void expand_buffer(struct trace_seq *s) · line 86

Quoted 5 lines
tools/lib/traceevent/trace-seq.c +1 -1
@@ -81,7 +83,7 @@ static void expand_buffer(struct trace_seq *s)
 	s->buffer_size += TRACE_SEQ_BUF_SIZE;
 	s->buffer = realloc(s->buffer, s->buffer_size);
 	if (!s->buffer)
-		die("Can't allocate trace_seq buffer memory");
+		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;

and also here?

Quoted 2 lines
	if (WARN_ONCE(!s->buffer))
		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;

Ah, will fix.

Quoted 2 lines

otherwise the whole series seems ok,

Thanks for your review!
Namhyung

#7 lore raw

PATCH 3/5 tools lib traceevent: Check return value of realloc()

In reply to Namhyung Kim

If realloc() fails, it'll leak the buffer. Also increate buffer size only if the allocation succeeded.

---
 tools/lib/traceevent/trace-seq.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
tools/lib/traceevent/trace-seq.c +9 -3
index 1747aa50ecb0..b139d55a6bf9 100644
@@ -80,10 +80,16 @@ void trace_seq_destroy(struct trace_seq *s)
 
 static void expand_buffer(struct trace_seq *s)
 {
-	s->buffer_size += TRACE_SEQ_BUF_SIZE;
-	s->buffer = realloc(s->buffer, s->buffer_size);
-	if (!s->buffer)
+	char *buf;
+
+	buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE);
+	if (!buf) {
 		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
+		return;
+	}
+
+	s->buffer = buf;
+	s->buffer_size += TRACE_SEQ_BUF_SIZE;
 }
 
 /**
Signature · 1 line
1.7.11.7
#8 lore raw

PATCH 4/5 tools lib traceevent: Get rid of malloc_or_die() in trace_seq_init()

In reply to Namhyung Kim

Use plain malloc() and check its return value.

---
 tools/lib/traceevent/trace-seq.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/lib/traceevent/trace-seq.c +5 -2
index b139d55a6bf9..e858bfd3b104 100644
@@ -46,8 +46,11 @@ void trace_seq_init(struct trace_seq *s)
 	s->len = 0;
 	s->readpos = 0;
 	s->buffer_size = TRACE_SEQ_BUF_SIZE;
-	s->buffer = malloc_or_die(s->buffer_size);
-	s->state = TRACE_SEQ__GOOD;
+	s->buffer = malloc(s->buffer_size);
+	if (s->buffer != NULL)
+		s->state = TRACE_SEQ__GOOD;
+	else
+		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;
 }
 
 /**
Signature · 1 line
1.7.11.7
#9 lore raw

PATCH 5/5 tools lib traceevent: Get rid of die() finally!!

In reply to Namhyung Kim

Now all of its users were gone. :)

---
 tools/lib/traceevent/event-utils.h |  4 ----
 tools/lib/traceevent/parse-utils.c | 44 --------------------------------------
 2 files changed, 48 deletions(-)
tools/lib/traceevent/event-utils.h +0 -4
index 5dc0aec5ab50..496dba9fe984 100644
@@ -23,18 +23,14 @@
 #include <ctype.h>
 
 /* Can be overridden */
-void die(const char *fmt, ...);
-void *malloc_or_die(unsigned int size);
 void warning(const char *fmt, ...);
 void pr_stat(const char *fmt, ...);
 void vpr_stat(const char *fmt, va_list ap);
 
 /* Always available */
-void __die(const char *fmt, ...);
 void __warning(const char *fmt, ...);
 void __pr_stat(const char *fmt, ...);
 
-void __vdie(const char *fmt, ...);
 void __vwarning(const char *fmt, ...);
 void __vpr_stat(const char *fmt, ...);
 
tools/lib/traceevent/parse-utils.c +0 -44

53 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#10 lore raw

Re: [PATCH 5/5] tools lib traceevent: Get rid of die() finally!!

Quoted 2 lines · On Tue, 7 Jan 2014 12:00:12 +0900 Namhyung Kim <[email protected]> wrote:

Now all of its users were gone. :)

Die die!

;-)

-- Steve