Skip to contentSkip to conversation

perf tool: save cmdline from user in file header vs what is passed to record

PATCH In review David Ahern 2 participants 3 messages Jul 3, 2012 – Jul 5, 2012 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf tool: save cmdline from user in file header vs what is passed to record

A number of builtin commands process some user args and then pass the rest to cmd_record. cmd_record then saves argc/argv that it receives into the header of the perf data file. But this loses the arguments handled by the first command -- ie., the real command line from the user. This patch saves the command line as typed by the user rather than what was passed to cmd_record.

As an example consider the command:

$ perf kvm --guest --host --guestmount=/tmp/guest-mount record
    -fo /tmp/perf.data -ag -- sleep 10

Currently the command saved to the header is:
cmdline : /tmp/p3.5/perf record -o perf.data.kvm -fo /tmp/perf.data -ag -- sleep 1

(ignore the duplicated -o -- the first would be yet another bug with perf-kvm).

With this patch the command line saved to the header is:
cmdline : /tmp/p3.5/perf kvm --guest --host --guestmount=/tmp/guest-mount
record -fo /tmp/perf.data -ag -- sleep 1

CC Arnaldo Carvalho de Melo <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/perf/builtin-kmem.c      |    2 ++
 tools/perf/builtin-kvm.c       |    2 ++
 tools/perf/builtin-lock.c      |    2 ++
 tools/perf/builtin-sched.c     |    2 ++
 tools/perf/builtin-script.c    |    2 ++
 tools/perf/builtin-timechart.c |    2 ++
 tools/perf/util/header.c       |    9 +++++++++
 7 files changed, 21 insertions(+)
tools/perf/builtin-kmem.c +2 -0
index ce35015..08c8f74 100644
@@ -770,6 +770,8 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_kmem(int argc, const char **argv, const char *prefix __used)
 {
+	perf_header__set_cmdline(argc, argv);
+
 	argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
 
 	if (!argc)
tools/perf/builtin-kvm.c +2 -0
index 9fc6e0f..913d0a8 100644
@@ -107,6 +107,8 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __used)
 	perf_host  = 0;
 	perf_guest = 1;
 
+	perf_header__set_cmdline(argc, argv);
+
 	argc = parse_options(argc, argv, kvm_options, kvm_usage,
 			PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
tools/perf/builtin-lock.c +2 -0
index b3c4285..89917e1 100644
@@ -980,6 +980,8 @@ int cmd_lock(int argc, const char **argv, const char *prefix __used)
 	for (i = 0; i < LOCKHASH_SIZE; i++)
 		INIT_LIST_HEAD(lockhash_table + i);
 
+	perf_header__set_cmdline(argc, argv);
+
 	argc = parse_options(argc, argv, lock_options, lock_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
tools/perf/builtin-sched.c +2 -0
index 7a9ad2b..25113b2 100644
@@ -1899,6 +1899,8 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_sched(int argc, const char **argv, const char *prefix __used)
 {
+	perf_header__set_cmdline(argc, argv);
+
 	argc = parse_options(argc, argv, sched_options, sched_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 	if (!argc)
tools/perf/builtin-script.c +2 -0
index 1e60ab7..07bc60b 100644
@@ -1206,6 +1206,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __used)
 
 	setup_scripting();
 
+	perf_header__set_cmdline(argc, argv);
+
 	argc = parse_options(argc, argv, options, script_usage,
 			     PARSE_OPT_STOP_AT_NON_OPTION);
 
tools/perf/builtin-timechart.c +2 -0
index 3b75b2e..1e776a6 100644
@@ -1108,6 +1108,8 @@ static const struct option options[] = {
 
 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
 {
+	perf_header__set_cmdline(argc, argv);
+
 	argc = parse_options(argc, argv, options, timechart_usage,
 			PARSE_OPT_STOP_AT_NON_OPTION);
 
tools/perf/util/header.c +9 -0
index 5a47aba..99e410c 100644
@@ -174,6 +174,15 @@ perf_header__set_cmdline(int argc, const char **argv)
 {
 	int i;
 
+	/* 
+	 * If header_argv has already been set, do not override it.
+	 * This allows a command to set the cmdline, parse args and
+	 * then call another builtin function that implements a
+	 * command -- e.g, cmd_kvm calling cmd_record.
+	 */
+	if (header_argv)
+		return 0;
+
 	header_argc = (u32)argc;
 
 	/* do not include NULL termination */
Signature · 1 line
1.7.10.1
#1 lore raw
Quoted 11 lines · David Ahern

A number of builtin commands process some user args and then pass the rest to cmd_record. cmd_record then saves argc/argv that it receives into the header of the perf data file. But this loses the arguments handled by the first command -- ie., the real command line from the user. This patch saves the command line as typed by the user rather than what was passed to cmd_record.

As an example consider the command:

$ perf kvm --guest --host --guestmount=/tmp/guest-mount record
    -fo /tmp/perf.data -ag -- sleep 10
Quoted 9 lines

Currently the command saved to the header is:
cmdline : /tmp/p3.5/perf record -o perf.data.kvm -fo /tmp/perf.data -ag -- sleep 1

(ignore the duplicated -o -- the first would be yet another bug with perf-kvm).

With this patch the command line saved to the header is:
cmdline : /tmp/p3.5/perf kvm --guest --host --guestmount=/tmp/guest-mount
record -fo /tmp/perf.data -ag -- sleep 1

Yes, you need to manually add the pfm_header__set_cmdline(). That begs the question
as to whether or not it would not be better to move this call directly into the parse_option() function given that it must be done BEFORE parsing actually takes place.That way we would be sure all present and future commands would be covered.

Quoted 119 lines
CC Arnaldo Carvalho de Melo <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Stephane Eranian <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 tools/perf/builtin-kmem.c      |    2 ++
 tools/perf/builtin-kvm.c       |    2 ++
 tools/perf/builtin-lock.c      |    2 ++
 tools/perf/builtin-sched.c     |    2 ++
 tools/perf/builtin-script.c    |    2 ++
 tools/perf/builtin-timechart.c |    2 ++
 tools/perf/util/header.c       |    9 +++++++++
 7 files changed, 21 insertions(+)
tools/perf/builtin-kmem.c +2 -0
index ce35015..08c8f74 100644
@@ -770,6 +770,8 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_kmem(int argc, const char **argv, const char *prefix __used)
 {
+       perf_header__set_cmdline(argc, argv);
+
        argc = parse_options(argc, argv, kmem_options, kmem_usage, 0);
 
        if (!argc)
tools/perf/builtin-kvm.c +2 -0
index 9fc6e0f..913d0a8 100644
@@ -107,6 +107,8 @@ int cmd_kvm(int argc, const char **argv, const char *prefix __used)
        perf_host  = 0;
        perf_guest = 1;
 
+       perf_header__set_cmdline(argc, argv);
+
        argc = parse_options(argc, argv, kvm_options, kvm_usage,
                        PARSE_OPT_STOP_AT_NON_OPTION);
        if (!argc)
tools/perf/builtin-lock.c +2 -0
index b3c4285..89917e1 100644
@@ -980,6 +980,8 @@ int cmd_lock(int argc, const char **argv, const char *prefix __used)
        for (i = 0; i < LOCKHASH_SIZE; i++)
                INIT_LIST_HEAD(lockhash_table + i);
 
+       perf_header__set_cmdline(argc, argv);
+
        argc = parse_options(argc, argv, lock_options, lock_usage,
                             PARSE_OPT_STOP_AT_NON_OPTION);
        if (!argc)
tools/perf/builtin-sched.c +2 -0
index 7a9ad2b..25113b2 100644
@@ -1899,6 +1899,8 @@ static int __cmd_record(int argc, const char **argv)
 
 int cmd_sched(int argc, const char **argv, const char *prefix __used)
 {
+       perf_header__set_cmdline(argc, argv);
+
        argc = parse_options(argc, argv, sched_options, sched_usage,
                             PARSE_OPT_STOP_AT_NON_OPTION);
        if (!argc)
tools/perf/builtin-script.c +2 -0
index 1e60ab7..07bc60b 100644
@@ -1206,6 +1206,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __used)
 
        setup_scripting();
 
+       perf_header__set_cmdline(argc, argv);
+
        argc = parse_options(argc, argv, options, script_usage,
                             PARSE_OPT_STOP_AT_NON_OPTION);
 
tools/perf/builtin-timechart.c +2 -0
index 3b75b2e..1e776a6 100644
@@ -1108,6 +1108,8 @@ static const struct option options[] = {
 
 int cmd_timechart(int argc, const char **argv, const char *prefix __used)
 {
+       perf_header__set_cmdline(argc, argv);
+
        argc = parse_options(argc, argv, options, timechart_usage,
                        PARSE_OPT_STOP_AT_NON_OPTION);
 
tools/perf/util/header.c +9 -1
index 5a47aba..99e410c 100644
@@ -174,6 +174,15 @@ perf_header__set_cmdline(int argc, const char **argv)
 {
        int i;
 
+       /*
+        * If header_argv has already been set, do not override it.
+        * This allows a command to set the cmdline, parse args and
+        * then call another builtin function that implements a
+        * command -- e.g, cmd_kvm calling cmd_record.
+        */
+       if (header_argv)
+               return 0;
+
        header_argc = (u32)argc;
 
        /* do not include NULL termination */
--

1.7.10.1

#2 lore raw
Quoted 2 lines · Stephane Eranian

On Tue, Jul 3, 2012 at 6:08 AM, David Ahern <[email protected]> wrote:
Yes, you need to manually add the pfm_header__set_cmdline(). That begs

I take it you meant perf_header__set_cmdline, not pfm_header...

Quoted 5 lines

the question
as to whether or not it would not be better to move this call directly into the parse_option() function given that it must be done BEFORE parsing actually takes place.That way we would be sure all present and future commands would be covered.

Sure, I'll move it to parse_options.

David