[PATCH 3/3] perf tests: Add test to hit wrong event sched out

From: Jiri Olsa
Date: Tue Jun 24 2014 - 04:20:55 EST


We create single breakpoint event with inherit = true and
create many workload children.

With some luck (which is directly proportional to number of
children) we hit event optimized sched out moving original
(parent) event into one of the child, closing it and causing
wrong measured numbers.

Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Corey Ashford <cjashfor@xxxxxxxxxxxxxxxxxx>
Cc: David Ahern <dsahern@xxxxxxxxx>
Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Paul Mackerras <paulus@xxxxxxxxx>
Cc: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/Makefile.perf | 1 +
tools/perf/tests/builtin-test.c | 4 ++
tools/perf/tests/optimized-sched-out.c | 90 ++++++++++++++++++++++++++++++++++
tools/perf/tests/tests.h | 1 +
4 files changed, 96 insertions(+)
create mode 100644 tools/perf/tests/optimized-sched-out.c

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 9670a16..29ff74e 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -419,6 +419,7 @@ endif
endif
LIB_OBJS += $(OUTPUT)tests/mmap-thread-lookup.o
LIB_OBJS += $(OUTPUT)tests/thread-mg-share.o
+LIB_OBJS += $(OUTPUT)tests/optimized-sched-out.o

BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 6f8b01b..754dc2a 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -154,6 +154,10 @@ static struct test {
.func = test__hists_cumulate,
},
{
+ .desc = "Test optimized event schedule out",
+ .func = test__optimized_sched_out,
+ },
+ {
.func = NULL,
},
};
diff --git a/tools/perf/tests/optimized-sched-out.c b/tools/perf/tests/optimized-sched-out.c
new file mode 100644
index 0000000..dfbb1b9
--- /dev/null
+++ b/tools/perf/tests/optimized-sched-out.c
@@ -0,0 +1,90 @@
+#include <linux/perf_event.h>
+#include <linux/hw_breakpoint.h>
+#include <string.h>
+#include <unistd.h>
+#include "tests.h"
+#include "perf.h"
+#include "debug.h"
+
+static void __attribute__ ((noinline)) bp_func(void)
+{
+ asm("" : : : "memory");
+}
+
+static int bp_event(void *fn)
+{
+ struct perf_event_attr pe;
+ int fd;
+
+ memset(&pe, 0, sizeof(struct perf_event_attr));
+ pe.type = PERF_TYPE_BREAKPOINT;
+ pe.size = sizeof(struct perf_event_attr);
+
+ pe.config = 0;
+ pe.bp_type = HW_BREAKPOINT_X;
+ pe.bp_addr = (unsigned long) fn;
+ pe.bp_len = sizeof(long);
+
+ pe.read_format = PERF_FORMAT_ID;
+ pe.inherit = 1;
+ pe.exclude_kernel = 1;
+ pe.exclude_hv = 1;
+ pe.disabled = 1;
+
+ fd = sys_perf_event_open(&pe, 0, -1, -1, 0);
+ if (fd < 0) {
+ pr_debug("failed opening event %llx\n", pe.config);
+ return TEST_FAIL;
+ }
+
+ return fd;
+}
+
+#define CHILDREN 100
+
+int test__optimized_sched_out(void)
+{
+ struct read_data_t {
+ __u64 val;
+ __u64 id;
+ } read_data = { 0 };
+ int fd, err, i;
+ __u64 id;
+
+ fd = bp_event(bp_func);
+ TEST_ASSERT_VAL("create event", fd >= 0);
+
+ err = ioctl(fd, PERF_EVENT_IOC_ID, &id);
+ TEST_ASSERT_VAL("id event", !err);
+
+ err = ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
+ TEST_ASSERT_VAL("enable event", !err);
+
+ for (i = 0; i < CHILDREN; i++) {
+ err = fork();
+ TEST_ASSERT_VAL("failed to fork", err != -1);
+ if (!err) {
+ pr_debug("child %d created\n", getpid());
+ bp_func();
+ exit(0);
+ }
+ }
+
+ while (wait(&err) > 0)
+ ;
+
+ bp_func();
+
+ /* read */
+ err = read(fd, &read_data, sizeof(read_data));
+ TEST_ASSERT_VAL("read values", sizeof(read_data) == err);
+ TEST_ASSERT_VAL("event id", read_data.id == id);
+
+ pr_debug("read_data.val %llu, expected %u\n",
+ read_data.val, CHILDREN + 1);
+
+ TEST_ASSERT_VAL("event count", read_data.val == CHILDREN + 1);
+
+ close(fd);
+ return 0;
+}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index ed64790..a62d58d 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -48,6 +48,7 @@ int test__mmap_thread_lookup(void);
int test__thread_mg_share(void);
int test__hists_output(void);
int test__hists_cumulate(void);
+int test__optimized_sched_out(void);

#if defined(__x86_64__) || defined(__i386__) || defined(__arm__)
#ifdef HAVE_DWARF_UNWIND_SUPPORT
--
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/