linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter@intel.com>
To: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Cc: linux-kernel@vger.kernel.org, David Ahern <dsahern@gmail.com>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>, Mike Galbraith <efault@gmx.de>,
	Namhyung Kim <namhyung@gmail.com>,
	Paul Mackerras <paulus@samba.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Stephane Eranian <eranian@google.com>,
	Ingo Molnar <mingo@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>
Subject: [PATCH V5 12/12] perf tools: add a sample parsing test
Date: Thu, 11 Jul 2013 16:12:21 +0300	[thread overview]
Message-ID: <1373548341-24119-13-git-send-email-adrian.hunter@intel.com> (raw)
In-Reply-To: <1373548341-24119-1-git-send-email-adrian.hunter@intel.com>

Add a test that checks that sample parsing is correctly
implemented.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/Makefile               |   1 +
 tools/perf/tests/builtin-test.c   |   4 +
 tools/perf/tests/sample-parsing.c | 231 ++++++++++++++++++++++++++++++++++++++
 tools/perf/tests/tests.h          |   1 +
 4 files changed, 237 insertions(+)
 create mode 100644 tools/perf/tests/sample-parsing.c

diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 5b7c6db..fe20130 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -389,6 +389,7 @@ LIB_OBJS += $(OUTPUT)tests/bp_signal.o
 LIB_OBJS += $(OUTPUT)tests/bp_signal_overflow.o
 LIB_OBJS += $(OUTPUT)tests/task-exit.o
 LIB_OBJS += $(OUTPUT)tests/sw-clock.o
+LIB_OBJS += $(OUTPUT)tests/sample-parsing.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 35b45f1466..5ee3933 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -94,6 +94,10 @@ static struct test {
 		.func = test__sw_clock_freq,
 	},
 	{
+		.desc = "Test sample parsing",
+		.func = test__sample_parsing,
+	},
+	{
 		.func = NULL,
 	},
 };
diff --git a/tools/perf/tests/sample-parsing.c b/tools/perf/tests/sample-parsing.c
new file mode 100644
index 0000000..b9859f6
--- /dev/null
+++ b/tools/perf/tests/sample-parsing.c
@@ -0,0 +1,231 @@
+#include <stdbool.h>
+#include <inttypes.h>
+
+#include "event.h"
+#include "evsel.h"
+
+#include "tests.h"
+
+#define COMP(m)						\
+	while (s1->m != s2->m) {			\
+		pr_debug("Samples differ at '"#m"'\n");	\
+		return false;				\
+	}
+
+#define MCOMP(m)					\
+	while (memcmp(&s1->m, &s2->m, sizeof(s1->m))) {	\
+		pr_debug("Samples differ at '"#m"'\n");	\
+		return false;				\
+	}
+
+static bool samples_same(const struct perf_sample *s1,
+			 const struct perf_sample *s2, u64 type, u64 regs_user)
+{
+	size_t i;
+
+	if (type & PERF_SAMPLE_IDENTIFIER)
+		COMP(id);
+
+	if (type & PERF_SAMPLE_IP)
+		COMP(ip);
+
+	if (type & PERF_SAMPLE_TID) {
+		COMP(pid);
+		COMP(tid);
+	}
+
+	if (type & PERF_SAMPLE_TIME)
+		COMP(time);
+
+	if (type & PERF_SAMPLE_ADDR)
+		COMP(addr);
+
+	if (type & PERF_SAMPLE_ID)
+		COMP(id);
+
+	if (type & PERF_SAMPLE_STREAM_ID)
+		COMP(stream_id);
+
+	if (type & PERF_SAMPLE_CPU)
+		COMP(cpu);
+
+	if (type & PERF_SAMPLE_PERIOD)
+		COMP(period);
+
+	if (type & PERF_SAMPLE_CALLCHAIN) {
+		COMP(callchain->nr);
+		for (i = 0; i < s1->callchain->nr; i++)
+			COMP(callchain->ips[i]);
+	}
+
+	if (type & PERF_SAMPLE_RAW) {
+		COMP(raw_size);
+		if (memcmp(s1->raw_data, s2->raw_data, s1->raw_size)) {
+			pr_debug("Samples differ at 'raw_data'\n");
+			return false;
+		}
+	}
+
+	if (type & PERF_SAMPLE_BRANCH_STACK) {
+		COMP(branch_stack->nr);
+		for (i = 0; i < s1->branch_stack->nr; i++)
+			MCOMP(branch_stack->entries[i]);
+	}
+
+	if (type & PERF_SAMPLE_REGS_USER) {
+		size_t sz = hweight_long(regs_user) * sizeof(u64);
+
+		if ((sz && (!s1->user_regs.regs || !s2->user_regs.regs)) ||
+		    memcmp(s1->user_regs.regs, s2->user_regs.regs, sz)) {
+			pr_debug("Samples differ at 'user_regs'\n");
+			return false;
+		}
+	}
+
+	if (type & PERF_SAMPLE_STACK_USER) {
+		COMP(user_stack.size);
+		if (memcmp(s1->user_stack.data, s1->user_stack.data, s1->user_stack.size)) {
+			pr_debug("Samples differ at 'user_stack'\n");
+			return false;
+		}
+	}
+
+	if (type & PERF_SAMPLE_WEIGHT)
+		COMP(weight);
+
+	if (type & PERF_SAMPLE_DATA_SRC)
+		COMP(data_src);
+
+	return true;
+}
+
+static int do_test(u64 sample_type, u64 sample_regs_user)
+{
+	struct perf_evsel evsel = {
+		.needs_swap = false,
+		.attr = {
+			.sample_type = sample_type,
+			.sample_regs_user = sample_regs_user,
+		},
+	};
+	union perf_event event = {
+		.header = {
+			.type = PERF_RECORD_SAMPLE,
+			.size = sizeof(union perf_event),
+		},
+	};
+	union {
+		struct ip_callchain callchain;
+		u64 data[64];
+	} callchain = {
+		/* 3 ips */
+		.data = {3, 201, 202, 203},
+	};
+	union {
+		struct branch_stack branch_stack;
+		u64 data[64];
+	} branch_stack = {
+		/* 1 branch_entry */
+		.data = {1, 211, 212, 213},
+	};
+	u64 user_regs[64];
+	const u64 raw_data[] = {0x123456780a0b0c0dULL, 0x1102030405060708ULL};
+	const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
+	const struct perf_sample sample = {
+		.ip		= 101,
+		.pid		= 102,
+		.tid		= 103,
+		.time		= 104,
+		.addr		= 105,
+		.id		= 106,
+		.stream_id	= 107,
+		.period		= 108,
+		.weight		= 109,
+		.cpu		= 110,
+		.raw_size	= sizeof(raw_data),
+		.data_src	= 111,
+		.raw_data	= (void *)raw_data,
+		.callchain	= &callchain.callchain,
+		.branch_stack	= &branch_stack.branch_stack,
+		.user_regs	= {
+			.regs	= user_regs,
+		},
+		.user_stack	= {
+			.size	= sizeof(data),
+			.data	= (void *)data,
+		},
+	};
+	struct perf_sample sample_out;
+	size_t i;
+	int err;
+
+	for (i = 0; i < sizeof(user_regs); i++)
+		*(u8 *)user_regs = i;
+
+	err = perf_event__synthesize_sample(&event, sample_type, sample_regs_user, &sample, false);
+	if (err) {
+		pr_debug("perf_event__synthesize_sample failed for sample_type %#"PRIx64", error %d\n", sample_type, err);
+		return -1;
+	}
+
+	evsel.sample_size = __perf_evsel__sample_size(sample_type);
+
+	err = perf_evsel__parse_sample(&evsel, &event, &sample_out);
+	if (err) {
+		pr_debug("perf_evsel__parse_sample failed for sample_type %#"PRIx64", error %d\n", sample_type, err);
+		return -1;
+	}
+
+	if (!samples_same(&sample, &sample_out, sample_type, sample_regs_user)) {
+		pr_debug("parsing failed for sample_type %#"PRIx64"\n", sample_type);
+		return -1;
+	}
+
+	return 0;
+}
+
+/**
+ * test__sample_parsing - test sample parsing.
+ *
+ * This function implements a test that synthesizes a sample event, parses it
+ * and then checks that the parsed sample matches the original sample.  The test
+ * checks sample format bits separately and together.  If the test passes %0 is
+ * returned, otherwise %-1 is returned.
+ */
+int test__sample_parsing(void)
+{
+	u64 sample_type;
+	u64 sample_regs_user;
+	int err;
+
+	/*
+	 * Fail the test if it has not been updated when new sample format bits
+	 * were added.
+	 */
+	if (PERF_SAMPLE_MAX > PERF_SAMPLE_IDENTIFIER << 1) {
+		pr_debug("sample format has changed - this test needs updating\n");
+		return -1;
+	}
+
+	/* Test each sample format bit separately */
+	for (sample_type = 1; sample_type != PERF_SAMPLE_MAX; sample_type <<= 1) {
+		if (sample_type == PERF_SAMPLE_READ)
+			continue;
+		if (sample_type == PERF_SAMPLE_REGS_USER)
+			sample_regs_user = 0x3fff;
+		else
+			sample_regs_user = 0;
+		err = do_test(sample_type, sample_regs_user);
+		if (err)
+			return err;
+	}
+
+	/* Test all sample format bits together */
+	sample_type = (PERF_SAMPLE_MAX - 1) & (~PERF_SAMPLE_READ);
+	sample_regs_user = 0x3fff;
+	err = do_test(sample_type, sample_regs_user);
+	if (err)
+		return err;
+
+	return 0;
+}
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index 07a92f9..90e3056 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -35,5 +35,6 @@ int test__bp_signal(void);
 int test__bp_signal_overflow(void);
 int test__task_exit(void);
 int test__sw_clock_freq(void);
+int test__sample_parsing(void);
 
 #endif /* TESTS_H */
-- 
1.7.11.7


  parent reply	other threads:[~2013-07-11 13:09 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-11 13:12 [PATCH V5 00/12] perf tools: some fixes and tweaks Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 01/12] perf tools: add debug prints Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 02/12] perf tools: allow non-matching sample types Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 03/12] perf tools: add pid to struct thread Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 04/12] perf tools: change machine__findnew_thread() to set thread pid Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 05/12] perf tools: tidy up sample parsing overflow checking Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 06/12] perf tools: remove unnecessary callchain validation Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 07/12] perf tools: remove references to struct ip_event Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 08/12] perf tools: move " Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 09/12] perf: make events stream always parsable Adrian Hunter
2013-07-11 15:43   ` David Ahern
2013-07-11 17:16     ` David Ahern
2013-07-12  6:42     ` Adrian Hunter
2013-07-12  9:56   ` Peter Zijlstra
2013-07-12 12:56     ` Adrian Hunter
2013-07-12 14:55       ` Peter Zijlstra
2013-07-15  6:14         ` Adrian Hunter
2013-07-15 11:53           ` Stephane Eranian
2013-07-15 12:09             ` Adrian Hunter
2013-07-16  6:49           ` Adrian Hunter
2013-07-16 15:09             ` Peter Zijlstra
2013-07-17  4:10               ` Stephane Eranian
2013-07-17 12:44               ` Adrian Hunter
2013-07-24  3:55               ` [tip:perf/core] perf: Update perf_event_type documentation tip-bot for Peter Zijlstra
2013-07-24 17:54                 ` Vince Weaver
2013-07-25  6:22                   ` Adrian Hunter
2013-07-25 12:34                     ` Vince Weaver
2013-07-25 16:27                       ` Peter Zijlstra
2013-07-26  3:24                         ` Vince Weaver
2013-07-26  8:55                           ` Peter Zijlstra
2013-07-26  3:20                     ` Vince Weaver
2013-07-26  8:57                       ` Peter Zijlstra
2013-07-27  3:20                         ` Vince Weaver
2013-07-25 10:04                   ` Peter Zijlstra
2013-07-25 12:31                     ` Vince Weaver
2013-07-25 12:39                       ` David Ahern
2013-09-13 21:31                 ` Vince Weaver
2013-09-13 21:42                   ` Peter Zijlstra
2013-07-11 13:12 ` [PATCH V5 10/12] perf tools: add support for PERF_SAMPLE_IDENTFIER Adrian Hunter
2013-07-11 13:12 ` [PATCH V5 11/12] perf tools: expand perf_event__synthesize_sample() Adrian Hunter
2013-07-11 13:12 ` Adrian Hunter [this message]
2013-07-16 12:48   ` [PATCH V5 12/12] perf tools: add a sample parsing test Jiri Olsa
2013-07-17 11:02     ` Adrian Hunter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1373548341-24119-13-git-send-email-adrian.hunter@intel.com \
    --to=adrian.hunter@intel.com \
    --cc=acme@ghostprotocols.net \
    --cc=dsahern@gmail.com \
    --cc=efault@gmx.de \
    --cc=eranian@google.com \
    --cc=fweisbec@gmail.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@gmail.com \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).