Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1263256 > unrolled thread

[PATCH 02/25] perf stat record: Add record command

Started byJiri Olsa <jolsa@kernel.org>
First post2015-11-05 15:50 +0100
Last post2015-11-06 15:20 +0100
Articles 5 — 3 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 02/25] perf stat record: Add record command Jiri Olsa <jolsa@kernel.org> - 2015-11-05 15:50 +0100
    Re: [PATCH 02/25] perf stat record: Add record command Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-11-05 22:00 +0100
      Re: [PATCH 02/25] perf stat record: Add record command Jiri Olsa <jolsa@redhat.com> - 2015-11-06 09:30 +0100
        Re: [PATCH 02/25] perf stat record: Add record command Arnaldo Carvalho de Melo <acme@kernel.org> - 2015-11-06 14:40 +0100
          Re: [PATCH 02/25] perf stat record: Add record command Jiri Olsa <jolsa@redhat.com> - 2015-11-06 15:20 +0100

#1263256 — [PATCH 02/25] perf stat record: Add record command

FromJiri Olsa <jolsa@kernel.org>
Date2015-11-05 15:50 +0100
Subject[PATCH 02/25] perf stat record: Add record command
Message-ID<qru1r-7Yg-3@gated-at.bofh.it>
Add 'perf stat record' command support. It creates simple
(header only) perf.data file ATM.

The record command could be specified anywhere among stat
options. All stat command options are valid for stat record
command with '-o' option exception. If specified for record
command it denotes the perf data file name.

Tested-by: Kan Liang <kan.liang@intel.com>
Link: http://lkml.kernel.org/n/tip-0av5yfkwyywwgoiali88w4hi@git.kernel.org
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 tools/perf/Documentation/perf-stat.txt | 12 ++++++
 tools/perf/builtin-stat.c              | 78 ++++++++++++++++++++++++++++++++--
 2 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
index 4e074a660826..70eee1c2c444 100644
--- a/tools/perf/Documentation/perf-stat.txt
+++ b/tools/perf/Documentation/perf-stat.txt
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'perf stat' [-e <EVENT> | --event=EVENT] [-a] <command>
 'perf stat' [-e <EVENT> | --event=EVENT] [-a] -- <command> [<options>]
+'perf stat' [-e <EVENT> | --event=EVENT] [-a] record [-o file] -- <command> [<options>]
 
 DESCRIPTION
 -----------
@@ -22,6 +23,8 @@ OPTIONS
 <command>...::
 	Any command you can specify in a shell.
 
+record::
+	See STAT RECORD.
 
 -e::
 --event=::
@@ -159,6 +162,15 @@ filter out the startup phase of the program, which is often very different.
 
 Print statistics of transactional execution if supported.
 
+STAT RECORD
+-----------
+Stores stat data into perf data file.
+
+-o file::
+--output file::
+Output file name.
+
+
 EXAMPLES
 --------
 
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index e77880b5094d..04123835fd81 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -59,6 +59,7 @@
 #include "util/thread.h"
 #include "util/thread_map.h"
 #include "util/counts.h"
+#include "util/session.h"
 
 #include <stdlib.h>
 #include <sys/prctl.h>
@@ -126,6 +127,16 @@ static bool			append_file;
 static const char		*output_name;
 static int			output_fd;
 
+struct perf_stat {
+	bool			 record;
+	struct perf_data_file	 file;
+	struct perf_session	*session;
+	u64			 bytes_written;
+};
+
+static struct perf_stat		perf_stat;
+#define STAT_RECORD		perf_stat.record
+
 static volatile int done = 0;
 
 static struct perf_stat_config stat_config = {
@@ -344,6 +355,15 @@ static int __run_perf_stat(int argc, const char **argv)
 		return -1;
 	}
 
+	if (STAT_RECORD) {
+		int err, fd = perf_data_file__fd(&perf_stat.file);
+
+		err = perf_session__write_header(perf_stat.session, evsel_list,
+						 fd, false);
+		if (err < 0)
+			return err;
+	}
+
 	/*
 	 * Enable counters and exec the command:
 	 */
@@ -1236,6 +1256,38 @@ static int add_default_attributes(void)
 	return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
 }
 
+static const char * const recort_usage[] = {
+	"perf stat record [<options>]",
+	NULL,
+};
+
+static int __cmd_record(int argc, const char **argv)
+{
+	struct perf_session *session;
+	struct perf_data_file *file = &perf_stat.file;
+
+	argc = parse_options(argc, argv, stat_options, record_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (output_name)
+		file->path = output_name;
+
+	session = perf_session__new(file, false, NULL);
+	if (session == NULL) {
+		pr_err("Perf session creation failed.\n");
+		return -1;
+	}
+
+	/* No pipe support ATM */
+	if (perf_stat.file.is_pipe)
+		return -EINVAL;
+
+	session->evlist   = evsel_list;
+	perf_stat.session = session;
+	perf_stat.record  = true;
+	return argc;
+}
+
 int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	const char * const stat_usage[] = {
@@ -1246,6 +1298,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 	const char *mode;
 	FILE *output = stderr;
 	unsigned int interval;
+	const char * const stat_subcommands[] = { "record" };
 
 	setlocale(LC_ALL, "");
 
@@ -1253,12 +1306,22 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 	if (evsel_list == NULL)
 		return -ENOMEM;
 
-	argc = parse_options(argc, argv, stat_options, stat_usage,
-		PARSE_OPT_STOP_AT_NON_OPTION);
+	argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
+					(const char **) stat_usage,
+					PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (argc && !strncmp(argv[0], "rec", 3)) {
+		argc = __cmd_record(argc, argv);
+		if (argc < 0)
+			return -1;
+	}
 
 	interval = stat_config.interval;
 
-	if (output_name && strcmp(output_name, "-"))
+	/*
+	 * For record command the -o is already taken care of.
+	 */
+	if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
 		output = NULL;
 
 	if (output_name && output_fd) {
@@ -1425,6 +1488,15 @@ int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
 	if (!forever && status != -1 && !interval)
 		print_counters(NULL, argc, argv);
 
+	if (STAT_RECORD) {
+		int fd = perf_data_file__fd(&perf_stat.file);
+
+		perf_stat.session->header.data_size += perf_stat.bytes_written;
+		perf_session__write_header(perf_stat.session, evsel_list, fd, true);
+
+		perf_session__delete(perf_stat.session);
+	}
+
 	perf_evlist__free_stats(evsel_list);
 out:
 	perf_evlist__delete(evsel_list);
-- 
2.4.3

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

[toc] | [next] | [standalone]


#1263549

FromArnaldo Carvalho de Melo <acme@kernel.org>
Date2015-11-05 22:00 +0100
Message-ID<qrzNx-39o-19@gated-at.bofh.it>
In reply to#1263256
Em Thu, Nov 05, 2015 at 03:40:46PM +0100, Jiri Olsa escreveu:
> Add 'perf stat record' command support. It creates simple
> (header only) perf.data file ATM.
> 
> The record command could be specified anywhere among stat
> options. All stat command options are valid for stat record
> command with '-o' option exception. If specified for record
> command it denotes the perf data file name.
> 
> Tested-by: Kan Liang <kan.liang@intel.com>
> Link: http://lkml.kernel.org/n/tip-0av5yfkwyywwgoiali88w4hi@git.kernel.org
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>

Still stopping here:

[acme@zoo linux]$ rm -f perf.data
[acme@zoo linux]$ perf stat record usleep 1

 Performance counter stats for 'usleep 1':

          0.621181      task-clock (msec)         #    0.455 CPUs utilized          
                 1      context-switches          #    0.002 M/sec                  
                 0      cpu-migrations            #    0.000 K/sec                  
                54      page-faults               #    0.087 M/sec                  
           917,006      cycles                    #    1.476 GHz                    
           611,746      stalled-cycles-frontend   #   66.71% frontend cycles idle   
   <not supported>      stalled-cycles-backend   
           654,410      instructions              #    0.71  insns per cycle        
                                                  #    0.93  stalled cycles per insn
           132,653      branches                  #  213.550 M/sec                  
             7,432      branch-misses             #    5.60% of all branches        

       0.001365369 seconds time elapsed

[acme@zoo linux]$ ls -la perf.data
-rw-------. 1 acme acme 1384 Nov  5 17:42 perf.data
[acme@zoo linux]$ perf evlist
WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?
non matching sample_type[acme@zoo linux]$ 

--------------------------------

When we pass one event it gets a bit better:

[acme@zoo linux]$ rm -f perf.data
[acme@zoo linux]$ perf stat -e cycles record usleep 1

 Performance counter stats for 'usleep 1':

         1,056,818      cycles                                                      

       0.000715850 seconds time elapsed

[acme@zoo linux]$ ls -la perf.data
-rw-------. 1 acme acme 232 Nov  5 17:44 perf.data
[acme@zoo linux]$ perf evlist
WARNING: The perf.data file's data size field is 0 which is unexpected.
Was the 'perf record' command properly terminated?
cycles
[acme@zoo linux]$

-----

In the second case it almost works, modulo that warning.

I think that what we need to achieve is for older tools to be able to, with a
file produced by 'perf stat record', to show this:

[root@zoo ~]# perf report --no-header --stdio
Error:
The perf.data file has no samples!
# To display the perf.data header info, please use --header/--header-only options.
#
[root@zoo ~]# 


I.e. the file should look like one that is produced by this command, purposely
to not create any sample:

# perf record -e syscalls:sys_enter_accept usleep 1
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.018 MB perf.data ]


I applied the first patch and added it to that perf/stat branch.

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

[toc] | [prev] | [next] | [standalone]


#1263832

FromJiri Olsa <jolsa@redhat.com>
Date2015-11-06 09:30 +0100
Message-ID<qrKzg-235-1@gated-at.bofh.it>
In reply to#1263549
On Thu, Nov 05, 2015 at 05:51:01PM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

> In the second case it almost works, modulo that warning.
> 
> I think that what we need to achieve is for older tools to be able to, with a
> file produced by 'perf stat record', to show this:
> 
> [root@zoo ~]# perf report --no-header --stdio
> Error:
> The perf.data file has no samples!
> # To display the perf.data header info, please use --header/--header-only options.
> #
> [root@zoo ~]# 
> 
> 
> I.e. the file should look like one that is produced by this command, purposely
> to not create any sample:
> 
> # perf record -e syscalls:sys_enter_accept usleep 1
> [ perf record: Woken up 1 times to write data ]
> [ perf record: Captured and wrote 0.018 MB perf.data ]
> 
> 
> I applied the first patch and added it to that perf/stat branch.

well.. it's either simple patches and step by step
functionality or one big with everything..

[PATCH 02/25] perf stat record: Add record command
   - adds record command that creates empty perf.data

[PATCH 03/25] perf stat record: Initialize record features
   - adds FEATURES initialization for stat data

[PATCH 04/25] perf stat record: Synthesize stat record data
   - adds meta data

[PATCH 05/25] perf stat record: Store events IDs in perf data file
   - adds event IDs
...


you get proper warning right after patch 3/25, where
we store STAT feature bit and properly check it when
opening perf.data

I can merge patch 2 and 3 to get the proper warning
from begining.. but that'd be bigger patch ;-)

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

[toc] | [prev] | [next] | [standalone]


#1264021

FromArnaldo Carvalho de Melo <acme@kernel.org>
Date2015-11-06 14:40 +0100
Message-ID<qrPpg-55Y-17@gated-at.bofh.it>
In reply to#1263832
Em Fri, Nov 06, 2015 at 09:24:00AM +0100, Jiri Olsa escreveu:
> On Thu, Nov 05, 2015 at 05:51:01PM -0300, Arnaldo Carvalho de Melo wrote:
> 
> SNIP
> 
> > In the second case it almost works, modulo that warning.
> > 
> > I think that what we need to achieve is for older tools to be able to, with a
> > file produced by 'perf stat record', to show this:
> > 
> > [root@zoo ~]# perf report --no-header --stdio
> > Error:
> > The perf.data file has no samples!
> > # To display the perf.data header info, please use --header/--header-only options.
> > #
> > [root@zoo ~]# 
> > 
> > 
> > I.e. the file should look like one that is produced by this command, purposely
> > to not create any sample:
> > 
> > # perf record -e syscalls:sys_enter_accept usleep 1
> > [ perf record: Woken up 1 times to write data ]
> > [ perf record: Captured and wrote 0.018 MB perf.data ]
> > 
> > 
> > I applied the first patch and added it to that perf/stat branch.
> 
> well.. it's either simple patches and step by step
> functionality or one big with everything..

Humm, no, there are several things we should strive for, and
bisectability is one of the first, it requires smaller, self contained
patches, sure, but it also requires that at after applying each patch we
have sane output from the tools.

So, after applying the patch above we get a message that says the file
is corrupted, and more than that, it even forgets to put a newline,
further breaking the output.
 
> [PATCH 02/25] perf stat record: Add record command
>    - adds record command that creates empty perf.data
> 
> [PATCH 03/25] perf stat record: Initialize record features
>    - adds FEATURES initialization for stat data
> 
> [PATCH 04/25] perf stat record: Synthesize stat record data
>    - adds meta data
> 
> [PATCH 05/25] perf stat record: Store events IDs in perf data file
>    - adds event IDs
> ...
> 
> 
> you get proper warning right after patch 3/25, where
> we store STAT feature bit and properly check it when
> opening perf.data

But that will be will _new_ tools, right? I'm talking about getting sane
output from _older_, unmodified, tools, like I demonstrated.

Anyway, I'll take the time to fix the broken missing newline and will
check those first few patches to see if I have a suggestion for you on
how to group them.

- Arnaldo

> I can merge patch 2 and 3 to get the proper warning
> from begining.. but that'd be bigger patch ;-)
> 
> thanks,
> jirka
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [prev] | [next] | [standalone]


#1264052

FromJiri Olsa <jolsa@redhat.com>
Date2015-11-06 15:20 +0100
Message-ID<qrQ1Y-5z3-13@gated-at.bofh.it>
In reply to#1264021
On Fri, Nov 06, 2015 at 10:33:03AM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

> 
> Humm, no, there are several things we should strive for, and
> bisectability is one of the first, it requires smaller, self contained
> patches, sure, but it also requires that at after applying each patch we
> have sane output from the tools.
> 
> So, after applying the patch above we get a message that says the file
> is corrupted, and more than that, it even forgets to put a newline,
> further breaking the output.

well, because the 'perf stat record' creates just minimal
perf.data and I'm adding data itself in later commits

>  
> > [PATCH 02/25] perf stat record: Add record command
> >    - adds record command that creates empty perf.data
> > 
> > [PATCH 03/25] perf stat record: Initialize record features
> >    - adds FEATURES initialization for stat data
> > 
> > [PATCH 04/25] perf stat record: Synthesize stat record data
> >    - adds meta data
> > 
> > [PATCH 05/25] perf stat record: Store events IDs in perf data file
> >    - adds event IDs
> > ...
> > 
> > 
> > you get proper warning right after patch 3/25, where
> > we store STAT feature bit and properly check it when
> > opening perf.data
> 
> But that will be will _new_ tools, right? I'm talking about getting sane
> output from _older_, unmodified, tools, like I demonstrated.

new and old.. there's change to react on STAT feature
during opening perf data file in 03/25 that fixes the
issue.. I moved it from 'report' command patch earlier
in the patchset

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

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web