public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Akihiro Nagai <akihiro.nagai.hw@hitachi.com>
To: linux-kernel@vger.kernel.org
Cc: Akihiro Nagai <akihiro.nagai.hw@hitachi.com>,
	Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Paul Mackerras <paulus@samba.org>, Ingo Molnar <mingo@elte.hu>,
	Arnaldo Carvalho de Melo <acme@ghostprotocols.net>,
	linux-kernel@vger.kernel.org
Subject: [PATCH -tip  4/7] perf bts trace: print pid and command
Date: Fri, 03 Dec 2010 12:59:20 +0900	[thread overview]
Message-ID: <20101203035920.7827.30087.stgit@localhost6.localdomain6> (raw)
In-Reply-To: <20101203035832.7827.16528.stgit@localhost6.localdomain6>

Provide the function of printing pid and command name to
'perf bts trace'. Users can select items to print with options.
For example,
    'perf bts -p trace' prints only pid,
    'perf bts -ac trace' prints address and comamnd name.
    'perf bts trace' prints only address (default)

This is output sample (perf bts -ac trace):
command      address
ls           0xffffffff8146fe0e => ls           0x0000003806200b20
ls           0xffffffff8146fe0e => ls           0x0000003806200b20
ls           0x0000003806200b23 => ls           0x0000003806204910
ls           0xffffffff8146fe0e => ls           0x0000003806204910
ls           0xffffffff8146fe0e => ls           0x0000003806204936
ls           0xffffffff8146fe0e => ls           0x000000380620493d
ls           0x0000003806204981 => ls           0x00000038062049a3
ls           0x00000038062049a7 => ls           0x0000003806204988

Signed-off-by: Akihiro Nagai <akihiro.nagai.hw@hitachi.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Cc: linux-kernel@vger.kernel.org
---

 tools/perf/Documentation/perf-bts.txt |   18 ++++-
 tools/perf/builtin-bts.c              |  124 ++++++++++++++++++++++++++++++++-
 2 files changed, 135 insertions(+), 7 deletions(-)

diff --git a/tools/perf/Documentation/perf-bts.txt b/tools/perf/Documentation/perf-bts.txt
index 5920dcc..56ddaa4 100644
--- a/tools/perf/Documentation/perf-bts.txt
+++ b/tools/perf/Documentation/perf-bts.txt
@@ -3,7 +3,7 @@ perf-bts(1)
 
 NAME
 ----
-perf-bts - Record and print branch-trace-store log
+perf-bts - Record and analyze branch-trace-store log
 
 SYNOPSIS
 --------
@@ -12,20 +12,32 @@ SYNOPSIS
 
 DESCRIPTION
 -----------
-This command can record and print branch-trace-store log.
+This command can record and analyze branch-trace-store log.
 Branch-trace-store is a facility of processors. It can record
 address of branch to/from on every branch instruction and interrupt.
 
 'perf bts record <command>' records branch-trace-store log while specified
 command is executing. And, save to the file "perf.data".
 
-'perf bts trace' parses recorded branch-trace-store log and prints it.
+'perf bts trace' analyzes recorded branch-trace-store log and prints it.
+The command can select the item to print. For example,
+  'perf bts -a trace'	: prints only address
+  'perf bts -acp trace'	: prints address, command name and pid
 
 OPTIONS
 -------
 -i::
 --input=::
         Specify input file name to analyze.
+-a::
+--addr::
+	Print address. (default)
+-c::
+--comm::
+	Print command name.
+-p::
+--pid::
+	Print pid.
 
 SEE ALSO
 --------
diff --git a/tools/perf/builtin-bts.c b/tools/perf/builtin-bts.c
index 54e83af..11e491b 100644
--- a/tools/perf/builtin-bts.c
+++ b/tools/perf/builtin-bts.c
@@ -3,6 +3,7 @@
 #include "util/parse-options.h"
 #include "util/session.h"
 #include "util/cache.h"
+#include "util/trace-event.h"
 #include <inttypes.h>
 
 /* format string of specifying min width to print address */
@@ -14,6 +15,32 @@
 /* format string to print address */
 #define FMT_ADDR	"%#0" FMT_ADDR_WIDTH "llx"
 
+/* printable items */
+struct exec_info {
+	u64		addr;		/* recorded address by bts */
+	pid_t		pid;		/* tracee process pid */
+	const char	*comm;		/* command name */
+};
+
+#define EI_PID_UNSET	-1
+
+/* flags which item print */
+#define EI_FLAG_PRINT_ADDR		(1 << 0)
+#define EI_FLAG_PRINT_PID		(1 << 1)
+#define EI_FLAG_PRINT_COMM		(1 << 2)
+
+/* it's used when no print item specified */
+#define EI_FLAG_PRINT_DEFAULT		EI_FLAG_PRINT_ADDR
+
+/* print item flags */
+static unsigned long print_flags;
+
+#define is_flags_unset(flags)		(flags == 0)
+
+/* print it when we cannnot analyze and get the information */
+#define EI_UNKNOWN_TEXT			"(unknown)"
+#define EI_UNKNOWN_TEXT_LEN		(sizeof(EI_UNKNOWN_TEXT))
+
 /* default input file name to analyze */
 static const char *input_name = "perf.data";
 
@@ -31,26 +58,111 @@ static const char * const record_args[] = {
 	"-d",
 };
 
+/* set print flags call from parse_options() */
+static int set_print_flags(const struct option *opt, const char *str __unused,
+							int unset __unused)
+{
+	print_flags |= (unsigned long)opt->defval;
+	return 0;
+}
+
 static const struct option bts_options[] = {
 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
+	OPT_CALLBACK_DEFAULT_NOOPT('a', "addr", NULL, NULL,
+				   "print address (default)", set_print_flags,
+				   (void *)EI_FLAG_PRINT_ADDR),
+	OPT_CALLBACK_DEFAULT_NOOPT('p', "pid", NULL, NULL,
+				   "print pid", set_print_flags,
+				   (void *)EI_FLAG_PRINT_PID),
+	OPT_CALLBACK_DEFAULT_NOOPT('c', "comm", NULL, NULL,
+				   "print command name", set_print_flags,
+				   (void *)EI_FLAG_PRINT_COMM),
 	OPT_END()
 };
 
+static void init_exec_info(struct exec_info *ei)
+{
+	memset(ei, 0, sizeof(*ei));
+	ei->pid = EI_PID_UNSET;
+}
+
+/* collect printable items to struct exec_info */
+static void fill_exec_info(struct exec_info *ei, struct perf_session *session,
+						event_t *event, u64 addr)
+{
+	struct thread *thread;
+
+	ei->addr = addr;
+	ei->pid = event->ip.pid;
+
+	thread = perf_session__findnew(session, event->ip.pid);
+	if (!thread)
+		return;
+	ei->comm = thread->comm;
+}
+
+static void __print_exec_info(struct exec_info *ei)
+{
+	char pid[16];
+	const char *comm;
+
+	if (print_flags & EI_FLAG_PRINT_PID) {
+		if (ei->pid == EI_PID_UNSET)
+			strncpy(pid, EI_UNKNOWN_TEXT, EI_UNKNOWN_TEXT_LEN);
+		else
+			snprintf(pid, 16, "%d", ei->pid);
+		printf("%5s ", pid);
+	}
+	if (print_flags & EI_FLAG_PRINT_COMM) {
+		comm = ei->comm ? : EI_UNKNOWN_TEXT;
+		printf("%-12s ", comm);
+	}
+	if (print_flags & EI_FLAG_PRINT_ADDR)
+		printf(FMT_ADDR " ", ei->addr);
+}
+
+static void print_exec_info(struct exec_info *ei_from, struct exec_info *ei_to)
+{
+	__print_exec_info(ei_from);
+	printf("=> ");
+	__print_exec_info(ei_to);
+	printf("\n");
+}
+
+static void print_exec_info_header(void)
+{
+	if (print_flags & EI_FLAG_PRINT_PID)
+		printf("%5s ", "pid");
+	if (print_flags & EI_FLAG_PRINT_COMM)
+		printf("%-12s ", "command");
+	if (print_flags & EI_FLAG_PRINT_ADDR)
+		printf("%-" FMT_ADDR_WIDTH "s ", "address");
+	printf("\n");
+}
+
 static int process_sample_event(event_t *event, struct perf_session *session)
 {
 	struct sample_data data;
+	struct exec_info ei_from, ei_to;
 
 	memset(&data, 0, sizeof(data));
 	event__parse_sample(event, session->sample_type, &data);
 
+	init_exec_info(&ei_from);
+	init_exec_info(&ei_to);
+
 	/* data.ip is 'from address', data.addr is 'to address' */
-	printf(FMT_ADDR " => " FMT_ADDR "\n", data.ip, data.addr);
+	fill_exec_info(&ei_from, session, event, data.ip);
+	fill_exec_info(&ei_to, session, event, data.addr);
+
+	print_exec_info(&ei_from, &ei_to);
 
 	return 0;
 }
 
 static struct perf_event_ops event_ops = {
 	.sample			= process_sample_event,
+	.comm			= event__process_comm,
 	.ordered_samples	= false,
 };
 
@@ -64,6 +176,12 @@ static int __cmd_trace(void)
 		return -ENOMEM;
 	}
 
+	/* if print flags are unset, we use default flags */
+	if (is_flags_unset(print_flags))
+		print_flags = EI_FLAG_PRINT_DEFAULT;
+
+	setup_pager();
+	print_exec_info_header();
 	perf_session__process_events(session, &event_ops);
 	perf_session__delete(session);
 
@@ -104,10 +222,8 @@ int cmd_bts(int argc, const char **argv, const char *prefix __used)
 
 	if (!strncmp(argv[0], "record", 6))
 		return __cmd_record(argc, argv);
-	else if (!strncmp(argv[0], "trace", 5)) {
-		setup_pager();
+	else if (!strncmp(argv[0], "trace", 5))
 		return __cmd_trace();
-	}
 	else
 		usage_with_options(bts_usage, bts_options);
 


  parent reply	other threads:[~2010-12-03  4:01 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-03  3:58 [PATCH -tip 0/7] perf: Introduce bts sub commands Akihiro Nagai
2010-12-03  3:58 ` [PATCH -tip 1/7] perf: add OPT_CALLBACK_DEFAULT_NOOPT Akihiro Nagai
2010-12-06  2:23   ` Masami Hiramatsu
2010-12-07  6:56   ` [tip:perf/core] perf options: " tip-bot for Akihiro Nagai
2010-12-03  3:59 ` [PATCH -tip 2/7] perf: Introduce perf sub command 'bts record' Akihiro Nagai
2010-12-06  2:26   ` Masami Hiramatsu
2010-12-03  3:59 ` [PATCH -tip 3/7] perf bts: Introduce new sub command 'perf bts trace' Akihiro Nagai
2010-12-06  3:10   ` Masami Hiramatsu
2010-12-03  3:59 ` Akihiro Nagai [this message]
2010-12-06  3:16   ` [PATCH -tip 4/7] perf bts trace: print pid and command Masami Hiramatsu
2010-12-03  3:59 ` [PATCH -tip 5/7] perf bts trace: print file path of the executed elf Akihiro Nagai
2010-12-06  3:17   ` Masami Hiramatsu
2010-12-03  4:00 ` [PATCH -tip 6/7] perf bts trace: print function+offset Akihiro Nagai
2010-12-03 13:00   ` Peter Zijlstra
2010-12-06 10:08     ` Akihiro Nagai
2010-12-06  2:42   ` Frederic Weisbecker
2010-12-06 10:28     ` Akihiro Nagai
2010-12-06  3:20   ` Masami Hiramatsu
2010-12-03  4:00 ` [PATCH -tip 7/7] perf bts trace: add print all option Akihiro Nagai
2010-12-06  3:20   ` Masami Hiramatsu

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=20101203035920.7827.30087.stgit@localhost6.localdomain6 \
    --to=akihiro.nagai.hw@hitachi.com \
    --cc=acme@ghostprotocols.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@elte.hu \
    --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