Skip to content

Commit 74833e3

Browse files
committed
perf jitdump: Fixup in_pidns member when java agent and 'perf record' are not in the same pidns
When running 'perf record' outside a container and the java agent inside a container the jit_repipe_code_load() and friends will emit PERF_RECORD_MMAP2 entries for the jitdump records and will check if we need to fixup the pid/tid: nspid = jr->load.pid; pid = jr_entry_pid(jd, jr); tid = jr_entry_tid(jd, jr); The jr_entry_pid() function looks if we're in the same pidns: static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr) { if (jd->nsi && nsinfo__in_pidns(jd->nsi)) return nsinfo__tgid(jd->nsi); return jr->load.pid; } But since the thread, populated from perf.data records, try to figure out if in the same pidns by actually trying, on the system where 'perf inject' is running to open a procfs file (a bug that remains to be fixed), assuming that if it is not possible that is because that thread terminated and thus we can't get its namespace info and tolerates nsinfo__init() failing, noting only that that namespace can't be entered, so don't even try. But we can kinda get at least that info (thread->nsinfo->in_pidns) from the data in the perf.data file, namely the pid and tid in the PERF_RECORD_MMAP2 for the jit-<PID>.dump file generated from the java agent, if the PERF_RECORD_MMAP2->pid is the same as what is in the jitdump file, then we're in the same namespace, otherwise we need to use the PERF_RECORD_MMAP2->pid. This all has to be revamped for this jitdump + running perf from outside, as the meaning of in_pidns is being abused, the initialization of nsinfo->pid with the value coming from the PERF_RECORD_MMAP2 data is wrong as it is the pid _outside_ the container since perf was running there. The hack in this patch at least produces the expected result in this scenario by following the assumptions in the current codebase for finding maps and for generating the PERF_RECORD_MMAP2 for the ELF files synthesized from the jitdump records in jit_repipe_code_load(), etc.s Reported-by: Francesco Nigro <[email protected]> Reported-by: Ilan Green <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Clark Williams <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Yonatan Goldschmidt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 9c6a585 commit 74833e3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

tools/perf/util/jitdump.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ jit_inject(struct jit_buf_desc *jd, const char *path)
737737
* as captured in the RECORD_MMAP record
738738
*/
739739
static int
740-
jit_detect(const char *mmap_name, pid_t pid, struct nsinfo *nsi)
740+
jit_detect(const char *mmap_name, pid_t pid, struct nsinfo *nsi, bool *in_pidns)
741741
{
742742
char *p;
743743
char *end = NULL;
@@ -773,11 +773,16 @@ jit_detect(const char *mmap_name, pid_t pid, struct nsinfo *nsi)
773773
if (!end)
774774
return -1;
775775

776+
*in_pidns = pid == nsinfo__nstgid(nsi);
776777
/*
777778
* pid does not match mmap pid
778779
* pid==0 in system-wide mode (synthesized)
780+
*
781+
* If the pid in the file name is equal to the nstgid, then
782+
* the agent ran inside a container and perf outside the
783+
* container, so record it for further use in jit_inject().
779784
*/
780-
if (pid && pid2 && pid != nsinfo__nstgid(nsi))
785+
if (pid && !(pid2 == pid || *in_pidns))
781786
return -1;
782787
/*
783788
* validate suffix
@@ -830,6 +835,7 @@ jit_process(struct perf_session *session,
830835
struct nsinfo *nsi;
831836
struct evsel *first;
832837
struct jit_buf_desc jd;
838+
bool in_pidns = false;
833839
int ret;
834840

835841
thread = machine__findnew_thread(machine, pid, tid);
@@ -844,7 +850,7 @@ jit_process(struct perf_session *session,
844850
/*
845851
* first, detect marker mmap (i.e., the jitdump mmap)
846852
*/
847-
if (jit_detect(filename, pid, nsi)) {
853+
if (jit_detect(filename, pid, nsi, &in_pidns)) {
848854
nsinfo__put(nsi);
849855

850856
/*
@@ -866,6 +872,9 @@ jit_process(struct perf_session *session,
866872
jd.machine = machine;
867873
jd.nsi = nsi;
868874

875+
if (in_pidns)
876+
nsinfo__set_in_pidns(nsi);
877+
869878
/*
870879
* track sample_type to compute id_all layout
871880
* perf sets the same sample type to all events as of now

0 commit comments

Comments
 (0)