From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751809AbdCCI5y (ORCPT ); Fri, 3 Mar 2017 03:57:54 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:41838 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751648AbdCCI5v (ORCPT ); Fri, 3 Mar 2017 03:57:51 -0500 Subject: Re: [PATCH v7 4/8] perf tool: synthesize namespace events for current processes To: Arnaldo Carvalho de Melo References: <148768564246.30285.16450228018975882950.stgit@hbathini.in.ibm.com> <148768570431.30285.4321000215785619369.stgit@hbathini.in.ibm.com> <20170301210558.GK15145@kernel.org> Cc: ast@fb.com, peterz@infradead.org, lkml , alexander.shishkin@linux.intel.com, mingo@redhat.com, daniel@iogearbox.net, rostedt@goodmis.org, Ananth N Mavinakayanahalli , ebiederm@xmission.com, sargun@sargun.me, Aravinda Prasad , brendan.d.gregg@gmail.com, jolsa@redhat.com From: Hari Bathini Date: Fri, 3 Mar 2017 14:27:09 +0530 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: <20170301210558.GK15145@kernel.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17030308-0008-0000-0000-000005371603 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17030308-0009-0000-0000-000013406AA3 Message-Id: X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-03-03_06:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=2 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1702020001 definitions=main-1703030086 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thursday 02 March 2017 02:35 AM, Arnaldo Carvalho de Melo wrote: > Em Tue, Feb 21, 2017 at 07:31:44PM +0530, Hari Bathini escreveu: >> Synthesize PERF_RECORD_NAMESPACES events for processes that were >> running prior to invocation of perf record, the data for which is >> taken from /proc/$PID/ns. These changes make way for analyzing >> events with regard to namespaces. >> >> Signed-off-by: Hari Bathini >> --- >> tools/perf/builtin-record.c | 27 +++++++++-- >> tools/perf/util/event.c | 107 +++++++++++++++++++++++++++++++++++++++++-- >> tools/perf/util/event.h | 6 ++ >> 3 files changed, 130 insertions(+), 10 deletions(-) >> >> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c >> index a8b9a78..f4bf6a6 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,28 @@ 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) + machine->id_hdr_size); >> + if (event == NULL) { >> + err = -ENOMEM; >> + goto out_child; >> + } >> + >> + /* >> + * Synthesize NAMESPACES event for the command specified. >> + */ >> + perf_event__synthesize_namespaces(tool, event, >> + rec->evlist->workload.pid, >> + tgid, process_synthesized_event, >> + machine); >> free(event); >> >> perf_evlist__start_workload(rec->evlist); >> diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c >> index f118eac..c8c112a 100644 >> --- a/tools/perf/util/event.c >> +++ b/tools/perf/util/event.c >> @@ -50,6 +50,16 @@ static const char *perf_event__names[] = { >> [PERF_RECORD_TIME_CONV] = "TIME_CONV", >> }; >> >> +static const char *perf_ns__names[] = { >> + [NET_NS_INDEX] = "net", >> + [UTS_NS_INDEX] = "uts", >> + [IPC_NS_INDEX] = "ipc", >> + [PID_NS_INDEX] = "pid", >> + [USER_NS_INDEX] = "user", >> + [MNT_NS_INDEX] = "mnt", >> + [CGROUP_NS_INDEX] = "cgroup", >> +}; >> + >> const char *perf_event__name(unsigned int id) >> { >> if (id >= ARRAY_SIZE(perf_event__names)) >> @@ -59,6 +69,13 @@ const char *perf_event__name(unsigned int id) >> return perf_event__names[id]; >> } >> >> +static const char *perf_ns__name(unsigned int id) >> +{ >> + if (id >= ARRAY_SIZE(perf_ns__names)) >> + return "UNKNOWN"; >> + return perf_ns__names[id]; >> +} >> + >> static int perf_tool__process_synth_event(struct perf_tool *tool, >> union perf_event *event, >> struct machine *machine, >> @@ -204,6 +221,56 @@ pid_t perf_event__synthesize_comm(struct perf_tool *tool, >> return tgid; >> } >> >> +static void perf_event__get_ns_link_info(pid_t pid, const char *ns, >> + struct perf_ns_link_info *ns_link_info) >> +{ >> + struct stat64 st; >> + char proc_ns[128]; >> + >> + sprintf(proc_ns, "/proc/%u/ns/%s", pid, ns); >> + if (stat64(proc_ns, &st) == 0) { >> + ns_link_info->dev = st.st_dev; >> + ns_link_info->ino = st.st_ino; >> + } >> +} >> + >> +int perf_event__synthesize_namespaces(struct perf_tool *tool, >> + union perf_event *event, >> + pid_t pid, pid_t tgid, >> + perf_event__handler_t process, >> + struct machine *machine) >> +{ >> + u32 idx; >> + struct perf_ns_link_info *ns_link_info; >> + >> + if (!tool->namespace_events) >> + return 0; >> + >> + memset(&event->namespaces, 0, >> + sizeof(event->namespaces) + machine->id_hdr_size); >> + >> + event->namespaces.pid = tgid; >> + event->namespaces.tid = pid; >> + >> + event->namespaces.nr_namespaces = NR_NAMESPACES; > Huh? Don't you have to first figure out how many namespaces a process is > in to then set this field? > NR_NAMESPACES is the total number of namespaces. For synthesized namespace events, data is obtained from /proc//ns/ dir. Looking at this dir, it is difficult to arrive at the total number of namespaces in kernel, as some namespaces can be compiled in/out of the kernel. NR_NAMESPACES is used instead. Its value is most likely the same for kernel and perf tool unless a new namespace is introduced - the warning in previous patch is intended to alert the user about such scenario. Thanks Hari