Skip to contentSkip to conversation

perf bench: fix assert when NDEBUG is defined

PATCH In review Irina Tirdea 4 participants 4 messages Sep 8, 2012 – Sep 9, 2012 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 perf bench: fix assert when NDEBUG is defined

From: Irina Tirdea <[email protected]>

When NDEBUG is defined, the assert macro will be expanded to nothing. Some assert calls used in perf are also including some functionality (e.g. system calls), not only validity checks. Therefore, if NDEBUG is defined, this functionality will be removed along with the assert. Perf also defines BUG_ON based on assert, so it has the same problem.

Define BUG_ON so that the condition will be executed when NDEBUG is defined. Replace the assert statements that have these side effects with BUG_ON.

For defining BUG_ON, use "if (cond) {}" insted of "if (cond) ;" because in the latter case build fails with "error: suggest braces around empty body in an ‘if’ statement [-Werror=empty-body]"

---

Changes in v3:
() ammend commit message with Suggested-by line

Changes in v2:
() redefine BUG_ON for NDEBUG instead of avoiding side-effects () replace assert with BUG_ON

v1 change:
() https://lkml.org/lkml/2012/9/2/189

 tools/perf/bench/sched-pipe.c          |    6 +++---
 tools/perf/util/include/linux/kernel.h |    4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/perf/bench/sched-pipe.c +3 -3
index 0c7454f..15911e9 100644
@@ -56,13 +56,13 @@ int bench_sched_pipe(int argc, const char **argv,
 	 * causes error in building environment for perf
 	 */
 	int __used ret, wait_stat;
-	pid_t pid, retpid;
+	pid_t pid, retpid __used;
 
 	argc = parse_options(argc, argv, options,
 			     bench_sched_pipe_usage, 0);
 
-	assert(!pipe(pipe_1));
-	assert(!pipe(pipe_2));
+	BUG_ON(pipe(pipe_1));
+	BUG_ON(pipe(pipe_2));
 
 	pid = fork();
 	assert(pid >= 0);
tools/perf/util/include/linux/kernel.h +4 -0
index b6842c1..4af9a10 100644
@@ -47,8 +47,12 @@
 #endif
 
 #ifndef BUG_ON
+#ifdef NDEBUG
+#define BUG_ON(cond) do { if (cond) {} } while (0)
+#else
 #define BUG_ON(cond) assert(!(cond))
 #endif
+#endif
 
 /*
  * Both need more care to handle endianness
Signature · 1 line
1.7.9.5
Reviewed-by on v3 1/1
#1 lore raw
Quoted 11 lines · Irina Tirdea

From: Irina Tirdea <[email protected]>

When NDEBUG is defined, the assert macro will be expanded to nothing. Some assert calls used in perf are also including some functionality (e.g. system calls), not only validity checks. Therefore, if NDEBUG is defined, this functionality will be removed along with the assert. Perf also defines BUG_ON based on assert, so it has the same problem.

Define BUG_ON so that the condition will be executed when NDEBUG is defined. Replace the assert statements that have these side effects with BUG_ON.

Quoted 6 lines

For defining BUG_ON, use "if (cond) {}" insted of "if (cond) ;" because in the latter case build fails with "error: suggest braces around empty body in an ‘if’ statement [-Werror=empty-body]"

maintainer Reviewed-by on v3 1/1
#2 lore raw
In reply to Irina Tirdea
Quoted 11 lines · Irina Tirdea

From: Irina Tirdea <[email protected]>

When NDEBUG is defined, the assert macro will be expanded to nothing. Some assert calls used in perf are also including some functionality (e.g. system calls), not only validity checks. Therefore, if NDEBUG is defined, this functionality will be removed along with the assert. Perf also defines BUG_ON based on assert, so it has the same problem.

Define BUG_ON so that the condition will be executed when NDEBUG is defined. Replace the assert statements that have these side effects with BUG_ON.

Quoted 6 lines

For defining BUG_ON, use "if (cond) {}" insted of "if (cond) ;" because in the latter case build fails with "error: suggest braces around empty body in an ‘if’ statement [-Werror=empty-body]"

Thanks,
Namhyung

#3 lore raw
In reply to Irina Tirdea
Commit-ID:  8bf98b89688c3d7ec071bf26d49761e38d846b47
Gitweb:     http://git.kernel.org/tip/8bf98b89688c3d7ec071bf26d49761e38d846b47
Author:     Irina Tirdea <[email protected]>
AuthorDate: Sat, 8 Sep 2012 08:35:51 +0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Sat, 8 Sep 2012 13:18:54 -0300

perf bench: fix assert when NDEBUG is defined

When NDEBUG is defined, the assert macro will be expanded to nothing. Some assert calls used in perf are also including some functionality (e.g. system calls), not only validity checks. Therefore, if NDEBUG is defined, this functionality will be removed along with the assert. Perf also defines BUG_ON based on assert, so it has the same problem.

Define BUG_ON so that the condition will be executed when NDEBUG is defined. Replace the assert statements that have these side effects with BUG_ON.

For defining BUG_ON, use "if (cond) {}" insted of "if (cond) ;" because in the latter case build fails with "error: suggest braces around empty body in an ‘if’ statement [-Werror=empty-body]"

---
 tools/perf/bench/sched-pipe.c          |    6 +++---
 tools/perf/util/include/linux/kernel.h |    4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)
tools/perf/bench/sched-pipe.c +3 -3
index 0c7454f..15911e9 100644
@@ -56,13 +56,13 @@ int bench_sched_pipe(int argc, const char **argv,
 	 * causes error in building environment for perf
 	 */
 	int __used ret, wait_stat;
-	pid_t pid, retpid;
+	pid_t pid, retpid __used;
 
 	argc = parse_options(argc, argv, options,
 			     bench_sched_pipe_usage, 0);
 
-	assert(!pipe(pipe_1));
-	assert(!pipe(pipe_2));
+	BUG_ON(pipe(pipe_1));
+	BUG_ON(pipe(pipe_2));
 
 	pid = fork();
 	assert(pid >= 0);
tools/perf/util/include/linux/kernel.h +4 -0
index b6842c1..4af9a10 100644
@@ -47,8 +47,12 @@
 #endif
 
 #ifndef BUG_ON
+#ifdef NDEBUG
+#define BUG_ON(cond) do { if (cond) {} } while (0)
+#else
 #define BUG_ON(cond) assert(!(cond))
 #endif
+#endif
 
 /*
  * Both need more care to handle endianness