[tip:perf/core] perf record: Synthesize namespace events for current processes

2017-03-15 Thread tip-bot for Hari Bathini
Commit-ID:  e907caf3a07ee42ef08ba689a436fd1eb99fbf62
Gitweb: http://git.kernel.org/tip/e907caf3a07ee42ef08ba689a436fd1eb99fbf62
Author: Hari Bathini 
AuthorDate: Wed, 8 Mar 2017 02:11:51 +0530
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Tue, 14 Mar 2017 15:16:09 -0300

perf record: Synthesize namespace events for current processes

Synthesize PERF_RECORD_NAMESPACES events for processes that were running prior
to invocation of perf record. The data for this is taken from /proc/$PID/ns.
These changes make way for analyzing events with regard to namespaces.

Committer notes:

Check if 'tool' is NULL in perf_event__synthesize_namespaces(), as in the
test__mmap_thread_lookup case, i.e. 'perf test Lookup mmap thread".

Testing it:

  # ps axH > /tmp/allthreads
  # perf record -a --namespaces usleep 1
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 1.169 MB perf.data (8 samples) ]
  # perf report -D | grep PERF_RECORD_NAMESPACES | wc -l
  602
  # wc -l /tmp/allthreads
  601 /tmp/allthreads
  # tail /tmp/allthreads
  16951 pts/4T  0:00 git rebase -i 
a033bf1bfacdaa25642e6bcc857a7d0f67cc3c92^
  16952 pts/4T  0:00 /bin/sh /usr/libexec/git-core/git-rebase -i 
a033bf1bfacdaa25642e6bcc857a7d0f67cc3c92^
  17176 pts/4T  0:00 git commit --amend --no-post-rewrite
  17204 pts/4T  0:00 vim /home/acme/git/linux/.git/COMMIT_EDITMSG
  18939 ?S  0:00 [kworker/2:1]
  18947 ?S  0:00 [kworker/3:0]
  18974 ?S  0:00 [kworker/1:0]
  19047 ?S  0:00 [kworker/0:1]
  19152 pts/6S+ 0:00 weechat
  19153 pts/7R+ 0:00 ps axH
  # perf report -D | grep PERF_RECORD_NAMESPACES | tail
  0 0 0x125068 [0xa0]: PERF_RECORD_NAMESPACES 17176/17176 - nr_namespaces: 7
  0 0 0x1255b8 [0xa0]: PERF_RECORD_NAMESPACES 17204/17204 - nr_namespaces: 7
  0 0 0x125df0 [0xa0]: PERF_RECORD_NAMESPACES 18939/18939 - nr_namespaces: 7
  0 0 0x125f00 [0xa0]: PERF_RECORD_NAMESPACES 18947/18947 - nr_namespaces: 7
  0 0 0x126010 [0xa0]: PERF_RECORD_NAMESPACES 18974/18974 - nr_namespaces: 7
  0 0 0x126120 [0xa0]: PERF_RECORD_NAMESPACES 19047/19047 - nr_namespaces: 7
  0 0 0x126230 [0xa0]: PERF_RECORD_NAMESPACES 19152/19152 - nr_namespaces: 7
  0 0 0x129330 [0xa0]: PERF_RECORD_NAMESPACES 19154/19154 - nr_namespaces: 7
  0 0 0x12a1f8 [0xa0]: PERF_RECORD_NAMESPACES 19155/19155 - nr_namespaces: 7
  0 0 0x12b0b8 [0xa0]: PERF_RECORD_NAMESPACES 19155/19155 - nr_namespaces: 7
  #

Humm, investigate why we got two record for the 19155 pid/tid...

Signed-off-by: Hari Bathini 
Tested-by: Arnaldo Carvalho de Melo 
Acked-by: Jiri Olsa 
Cc: Alexander Shishkin 
Cc: Alexei Starovoitov 
Cc: Ananth N Mavinakayanahalli 
Cc: Aravinda Prasad 
Cc: Brendan Gregg 
Cc: Daniel Borkmann 
Cc: Eric Biederman 
Cc: Peter Zijlstra 
Cc: Sargun Dhillon 
Cc: Steven Rostedt 
Link: 
http://lkml.kernel.org/r/14889193.25309.11073854609798681633.st...@hbathini.in.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/builtin-record.c | 29 --
 tools/perf/util/event.c | 94 ++---
 tools/perf/util/event.h |  6 +++
 3 files changed, 119 insertions(+), 10 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 99562c7..04faef7 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -986,6 +986,7 @@ static int __cmd_record(struct record *rec, int argc, const 
char **argv)
 */
if (forks) {
union perf_event *event;
+   pid_t tgid;
 
event = malloc(sizeof(event->comm) + machine->id_hdr_size);
if (event == NULL) {
@@ -999,10 +1000,30 @@ static int __cmd_record(struct record *rec, int argc, 
const char **argv)
 * cannot see a correct process name for those events.
 * Synthesize COMM event to prevent it.
 */
-   perf_event__synthesize_comm(tool, event,
-   rec->evlist->workload.pid,
-   process_synthesized_event,
-   machine);
+   tgid = perf_event__synthesize_comm(tool, event,
+  rec->evlist->workload.pid,
+  process_synthesized_event,
+  machine);
+   free(event);
+
+   if (tgid == -1)
+   goto out_child;
+
+   event = 

[tip:perf/core] perf record: Synthesize namespace events for current processes

2017-03-15 Thread tip-bot for Hari Bathini
Commit-ID:  e907caf3a07ee42ef08ba689a436fd1eb99fbf62
Gitweb: http://git.kernel.org/tip/e907caf3a07ee42ef08ba689a436fd1eb99fbf62
Author: Hari Bathini 
AuthorDate: Wed, 8 Mar 2017 02:11:51 +0530
Committer:  Arnaldo Carvalho de Melo 
CommitDate: Tue, 14 Mar 2017 15:16:09 -0300

perf record: Synthesize namespace events for current processes

Synthesize PERF_RECORD_NAMESPACES events for processes that were running prior
to invocation of perf record. The data for this is taken from /proc/$PID/ns.
These changes make way for analyzing events with regard to namespaces.

Committer notes:

Check if 'tool' is NULL in perf_event__synthesize_namespaces(), as in the
test__mmap_thread_lookup case, i.e. 'perf test Lookup mmap thread".

Testing it:

  # ps axH > /tmp/allthreads
  # perf record -a --namespaces usleep 1
  [ perf record: Woken up 1 times to write data ]
  [ perf record: Captured and wrote 1.169 MB perf.data (8 samples) ]
  # perf report -D | grep PERF_RECORD_NAMESPACES | wc -l
  602
  # wc -l /tmp/allthreads
  601 /tmp/allthreads
  # tail /tmp/allthreads
  16951 pts/4T  0:00 git rebase -i 
a033bf1bfacdaa25642e6bcc857a7d0f67cc3c92^
  16952 pts/4T  0:00 /bin/sh /usr/libexec/git-core/git-rebase -i 
a033bf1bfacdaa25642e6bcc857a7d0f67cc3c92^
  17176 pts/4T  0:00 git commit --amend --no-post-rewrite
  17204 pts/4T  0:00 vim /home/acme/git/linux/.git/COMMIT_EDITMSG
  18939 ?S  0:00 [kworker/2:1]
  18947 ?S  0:00 [kworker/3:0]
  18974 ?S  0:00 [kworker/1:0]
  19047 ?S  0:00 [kworker/0:1]
  19152 pts/6S+ 0:00 weechat
  19153 pts/7R+ 0:00 ps axH
  # perf report -D | grep PERF_RECORD_NAMESPACES | tail
  0 0 0x125068 [0xa0]: PERF_RECORD_NAMESPACES 17176/17176 - nr_namespaces: 7
  0 0 0x1255b8 [0xa0]: PERF_RECORD_NAMESPACES 17204/17204 - nr_namespaces: 7
  0 0 0x125df0 [0xa0]: PERF_RECORD_NAMESPACES 18939/18939 - nr_namespaces: 7
  0 0 0x125f00 [0xa0]: PERF_RECORD_NAMESPACES 18947/18947 - nr_namespaces: 7
  0 0 0x126010 [0xa0]: PERF_RECORD_NAMESPACES 18974/18974 - nr_namespaces: 7
  0 0 0x126120 [0xa0]: PERF_RECORD_NAMESPACES 19047/19047 - nr_namespaces: 7
  0 0 0x126230 [0xa0]: PERF_RECORD_NAMESPACES 19152/19152 - nr_namespaces: 7
  0 0 0x129330 [0xa0]: PERF_RECORD_NAMESPACES 19154/19154 - nr_namespaces: 7
  0 0 0x12a1f8 [0xa0]: PERF_RECORD_NAMESPACES 19155/19155 - nr_namespaces: 7
  0 0 0x12b0b8 [0xa0]: PERF_RECORD_NAMESPACES 19155/19155 - nr_namespaces: 7
  #

Humm, investigate why we got two record for the 19155 pid/tid...

Signed-off-by: Hari Bathini 
Tested-by: Arnaldo Carvalho de Melo 
Acked-by: Jiri Olsa 
Cc: Alexander Shishkin 
Cc: Alexei Starovoitov 
Cc: Ananth N Mavinakayanahalli 
Cc: Aravinda Prasad 
Cc: Brendan Gregg 
Cc: Daniel Borkmann 
Cc: Eric Biederman 
Cc: Peter Zijlstra 
Cc: Sargun Dhillon 
Cc: Steven Rostedt 
Link: 
http://lkml.kernel.org/r/14889193.25309.11073854609798681633.st...@hbathini.in.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/perf/builtin-record.c | 29 --
 tools/perf/util/event.c | 94 ++---
 tools/perf/util/event.h |  6 +++
 3 files changed, 119 insertions(+), 10 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 99562c7..04faef7 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -986,6 +986,7 @@ static int __cmd_record(struct record *rec, int argc, const 
char **argv)
 */
if (forks) {
union perf_event *event;
+   pid_t tgid;
 
event = malloc(sizeof(event->comm) + machine->id_hdr_size);
if (event == NULL) {
@@ -999,10 +1000,30 @@ static int __cmd_record(struct record *rec, int argc, 
const char **argv)
 * cannot see a correct process name for those events.
 * Synthesize COMM event to prevent it.
 */
-   perf_event__synthesize_comm(tool, event,
-   rec->evlist->workload.pid,
-   process_synthesized_event,
-   machine);
+   tgid = perf_event__synthesize_comm(tool, event,
+  rec->evlist->workload.pid,
+  process_synthesized_event,
+  machine);
+   free(event);
+
+   if (tgid == -1)
+   goto out_child;
+
+   event = malloc(sizeof(event->namespaces) +
+  (NR_NAMESPACES * sizeof(struct 
perf_ns_link_info)) +
+  machine->id_hdr_size);
+   if (event == NULL) {
+   err = -ENOMEM;
+   goto out_child;
+   }
+
+   /*
+* Synthesize NAMESPACES event for