Skip to content

Commit

Permalink
perf namespaces: Add 'in_pidns' to nsinfo struct
Browse files Browse the repository at this point in the history
Provides an accurate mean to determine if the owner thread is in a
different PID namespace.

Signed-off-by: Yonatan Goldschmidt <yonatan.goldschmidt@granulate.io>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20201105015418.1725218-1-yonatan.goldschmidt@granulate.io
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Yonatan Goldschmidt authored and Arnaldo Carvalho de Melo committed Feb 3, 2021
1 parent 473f742 commit 2b51c71
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tools/perf/util/namespaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ int nsinfo__init(struct nsinfo *nsi)
char spath[PATH_MAX];
char *newns = NULL;
char *statln = NULL;
char *nspid;
struct stat old_stat;
struct stat new_stat;
FILE *f = NULL;
Expand Down Expand Up @@ -112,8 +113,12 @@ int nsinfo__init(struct nsinfo *nsi)
}

if (strstr(statln, "NStgid:") != NULL) {
nsi->nstgid = (pid_t)strtol(strrchr(statln, '\t'),
NULL, 10);
nspid = strrchr(statln, '\t');
nsi->nstgid = (pid_t)strtol(nspid, NULL, 10);
/* If innermost tgid is not the first, process is in a different
* PID namespace.
*/
nsi->in_pidns = (statln + sizeof("NStgid:") - 1) != nspid;
break;
}
}
Expand All @@ -140,6 +145,7 @@ struct nsinfo *nsinfo__new(pid_t pid)
nsi->tgid = pid;
nsi->nstgid = pid;
nsi->need_setns = false;
nsi->in_pidns = false;
/* Init may fail if the process exits while we're trying to look
* at its proc information. In that case, save the pid but
* don't try to enter the namespace.
Expand All @@ -166,6 +172,7 @@ struct nsinfo *nsinfo__copy(struct nsinfo *nsi)
nnsi->tgid = nsi->tgid;
nnsi->nstgid = nsi->nstgid;
nnsi->need_setns = nsi->need_setns;
nnsi->in_pidns = nsi->in_pidns;
if (nsi->mntns_path) {
nnsi->mntns_path = strdup(nsi->mntns_path);
if (!nnsi->mntns_path) {
Expand Down
1 change: 1 addition & 0 deletions tools/perf/util/namespaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct nsinfo {
pid_t tgid;
pid_t nstgid;
bool need_setns;
bool in_pidns;
char *mntns_path;
refcount_t refcnt;
};
Expand Down

0 comments on commit 2b51c71

Please sign in to comment.