Skip to contentSkip to conversation

perf jitdump: Fix PID namespace detection

PATCH In review Ilya Leoshkevich 2 participants 15 messages Nov 5, 2025 – Nov 14, 2025 perf events

lore
Share & tools
lore
#0 lore raw

cover perf jitdump: Fix PID namespace detection

Hi,

I ran into a problem with perf jitdump not working with still running namespaced processes and started (very slowly) working on a series that allows perf to record accurate PID namespace information [1] and use that instead of resorting to guesswork.

However, I realized that at least short-term it's possible to fix the existing heuristic, so I've created this series. Patch 1 is the fix. Patches 2-4 are small changes to the existing test, and patch 5 adds a test that reproduces the situation that I'm facing.

[1] https://github.com/iii-i/linux/commits/iii/wip/nspid/

Best regards,
Ilya

Ilya Leoshkevich (5):
perf jitdump: Fix PID namespace detection
perf test java symbol: Get rid of shellcheck warnings perf test java symbol: Extract LIBJVMTI detection perf test java symbol: Fix a false negative in symbol regex perf test java symbol: Add PID namespace variant

 tools/perf/tests/shell/lib/setup_libjvmti.sh  |  18 +++
 tools/perf/tests/shell/test_java_symbol.sh    |  39 +++--
 .../tests/shell/test_java_symbol_pidns.sh     | 135 ++++++++++++++++++
 tools/perf/util/jitdump.c                     |  27 +++-
 tools/perf/util/namespaces.c                  |   9 ++
 tools/perf/util/namespaces.h                  |   2 +
 6 files changed, 201 insertions(+), 29 deletions(-)
 create mode 100644 tools/perf/tests/shell/lib/setup_libjvmti.sh
 create mode 100755 tools/perf/tests/shell/test_java_symbol_pidns.sh
Signature · 1 line
2.51.1
#1 lore raw

PATCH 1/5 perf jitdump: Fix PID namespace detection

perf inject fails to detect jitdump file produced by a process running in a different PID namespace if this process has not exited yet.

The PID namespace heuristic in jit_detect() compares two PIDs:

* pid: outermost NStgid of mmap(jitdump) caller from perf's PoV.
* nsinfo__nstgid(nsi): innermost NStgid of mmap(jitdump) caller from perf's PoV.

The semantics of the in_pidns variable can be seen in, e.g., nsinfo__get_nspid(): it's true if and only if perf and the profiled process are in different PID namespaces.

The current logic is clearly inverted: if pid and nsinfo__nstgid(nsi) are different, then the profiled process must be in a different PID namespace. This, of course, ignores that fact that they may end up being equal by accident, but that's not the point here.

Fix by flipping the comparison.

Changing just that, however, breaks the case when the process has exited. Add explicit support for that by adding "synthesized" field to nsinfo, which tracks whether NStgid was obtained from a running process (ignoring considerations of PID reuse or running inject on a different machine). When the namespace information is synthesized, assume the process ran in a different PID namespace.

---
 tools/perf/util/jitdump.c    | 27 +++++++++++++++++++++------
 tools/perf/util/namespaces.c |  9 +++++++++
 tools/perf/util/namespaces.h |  2 ++
 3 files changed, 32 insertions(+), 6 deletions(-)
tools/perf/util/jitdump.c +21 -6
index b062b1f234b62..19e4bc139935b 100644
@@ -788,15 +788,30 @@ jit_detect(const char *mmap_name, pid_t pid, struct nsinfo *nsi, bool *in_pidns)
 	if (!end)
 		return -1;
 
-	*in_pidns = pid == nsinfo__nstgid(nsi);
 	/*
-	 * pid does not match mmap pid
-	 * pid==0 in system-wide mode (synthesized)
+	 * Determine whether the process ran inside a container, and whether it
+	 * mapped jit.dump for profiling purposes or by accident. Record this
+	 * for further use in jit_inject(). The kernel does not provide PID
+	 * namespace information, so we have to resort to guesswork.
 	 *
-	 * If the pid in the file name is equal to the nstgid, then
-	 * the agent ran inside a container and perf outside the
-	 * container, so record it for further use in jit_inject().
+	 * If the process exited and perf had to synthesize the namespace
+	 * information, then it's not possible to have any certainty; be
+	 * aggressive and assume that the process ran inside a container. This
+	 * allows the user to proceed with injection at the cost of a small
+	 * probability of injecting irrelevant data.
+	 *
+	 * If the process' NStgid as observed by perf is different from its
+	 * innermost NStgid, then it must have run inside a container. There is
+	 * a very small probability that NStgids randomly happenned to be the
+	 * same; ignore it.
+	 *
+	 * pid == 0 means system-wide mode, just proceed.
+	 *
+	 * Finally, the most straightforward case: if the PID in the file name
+	 * is equal to the process' NStgid as observed by perf, then it must be
+	 * a match.
 	 */
+	*in_pidns = nsinfo__synthesized(nsi) || pid != nsinfo__nstgid(nsi);
 	if (pid && !(pid2 == pid || *in_pidns))
 		return -1;
 	/*
tools/perf/util/namespaces.c +9 -0
index 01502570b32d0..7de5d62e271c4 100644
@@ -132,6 +132,8 @@ int nsinfo__init(struct nsinfo *nsi)
 
 	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
 			       &RC_CHK_ACCESS(nsi)->in_pidns, spath);
+	if (rv == 0)
+		RC_CHK_ACCESS(nsi)->synthesized = false;
 
 out:
 	free(newns);
@@ -166,6 +168,7 @@ struct nsinfo *nsinfo__new(pid_t pid)
 	RC_CHK_ACCESS(nsi)->nstgid = pid;
 	nsinfo__clear_need_setns(nsi);
 	RC_CHK_ACCESS(nsi)->in_pidns = false;
+	RC_CHK_ACCESS(nsi)->synthesized = true;
 	/* 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.
@@ -197,6 +200,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	RC_CHK_ACCESS(nnsi)->nstgid = nsinfo__nstgid(nsi);
 	RC_CHK_ACCESS(nnsi)->need_setns = nsinfo__need_setns(nsi);
 	RC_CHK_ACCESS(nnsi)->in_pidns = nsinfo__in_pidns(nsi);
+	RC_CHK_ACCESS(nnsi)->synthesized = nsinfo__synthesized(nsi);
 	if (nsinfo__mntns_path(nsi)) {
 		RC_CHK_ACCESS(nnsi)->mntns_path = strdup(nsinfo__mntns_path(nsi));
 		if (!RC_CHK_ACCESS(nnsi)->mntns_path) {
@@ -275,6 +279,11 @@ void nsinfo__set_in_pidns(struct nsinfo *nsi)
 	RC_CHK_ACCESS(nsi)->in_pidns = true;
 }
 
+bool nsinfo__synthesized(const struct nsinfo *nsi)
+{
+	return RC_CHK_ACCESS(nsi)->synthesized;
+}
+
 void nsinfo__mountns_enter(struct nsinfo *nsi,
 				  struct nscookie *nc)
 {
tools/perf/util/namespaces.h +2 -0
index e95c79b80e27c..41ba2ea8137e5 100644
@@ -38,6 +38,7 @@ DECLARE_RC_STRUCT(nsinfo) {
 	bool			in_pidns;
 	char			*mntns_path;
 	refcount_t		refcnt;
+	bool			synthesized;
 };
 
 struct nscookie {
@@ -60,6 +61,7 @@ pid_t nsinfo__nstgid(const struct nsinfo  *nsi);
 pid_t nsinfo__pid(const struct nsinfo  *nsi);
 bool nsinfo__in_pidns(const struct nsinfo  *nsi);
 void nsinfo__set_in_pidns(struct nsinfo *nsi);
+bool nsinfo__synthesized(const struct nsinfo  *nsi);
 
 void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc);
 void nsinfo__mountns_exit(struct nscookie *nc);
Signature · 1 line
2.51.1
maintainer
#2 lore raw
Quoted 19 lines · Ilya Leoshkevich

perf inject fails to detect jitdump file produced by a process running in a different PID namespace if this process has not exited yet.

The PID namespace heuristic in jit_detect() compares two PIDs:

* pid: outermost NStgid of mmap(jitdump) caller from perf's PoV.
* nsinfo__nstgid(nsi): innermost NStgid of mmap(jitdump) caller from perf's PoV.

The semantics of the in_pidns variable can be seen in, e.g., nsinfo__get_nspid(): it's true if and only if perf and the profiled process are in different PID namespaces.

The current logic is clearly inverted: if pid and nsinfo__nstgid(nsi) are different, then the profiled process must be in a different PID namespace. This, of course, ignores that fact that they may end up being equal by accident, but that's not the point here.

Quoted 8 lines

Fix by flipping the comparison.

Changing just that, however, breaks the case when the process has exited. Add explicit support for that by adding "synthesized" field to nsinfo, which tracks whether NStgid was obtained from a running process (ignoring considerations of PID reuse or running inject on a different machine). When the namespace information is synthesized, assume the process ran in a different PID namespace.

I'm not sure I'm following. It'd be great if anyone understand the topic well could review.

Quoted 95 lines
---
 tools/perf/util/jitdump.c    | 27 +++++++++++++++++++++------
 tools/perf/util/namespaces.c |  9 +++++++++
 tools/perf/util/namespaces.h |  2 ++
 3 files changed, 32 insertions(+), 6 deletions(-)
tools/perf/util/jitdump.c +21 -6
index b062b1f234b62..19e4bc139935b 100644
@@ -788,15 +788,30 @@ jit_detect(const char *mmap_name, pid_t pid, struct nsinfo *nsi, bool *in_pidns)
 	if (!end)
 		return -1;
 
-	*in_pidns = pid == nsinfo__nstgid(nsi);
 	/*
-	 * pid does not match mmap pid
-	 * pid==0 in system-wide mode (synthesized)
+	 * Determine whether the process ran inside a container, and whether it
+	 * mapped jit.dump for profiling purposes or by accident. Record this
+	 * for further use in jit_inject(). The kernel does not provide PID
+	 * namespace information, so we have to resort to guesswork.
 	 *
-	 * If the pid in the file name is equal to the nstgid, then
-	 * the agent ran inside a container and perf outside the
-	 * container, so record it for further use in jit_inject().
+	 * If the process exited and perf had to synthesize the namespace
+	 * information, then it's not possible to have any certainty; be
+	 * aggressive and assume that the process ran inside a container. This
+	 * allows the user to proceed with injection at the cost of a small
+	 * probability of injecting irrelevant data.
+	 *
+	 * If the process' NStgid as observed by perf is different from its
+	 * innermost NStgid, then it must have run inside a container. There is
+	 * a very small probability that NStgids randomly happenned to be the
+	 * same; ignore it.
+	 *
+	 * pid == 0 means system-wide mode, just proceed.
+	 *
+	 * Finally, the most straightforward case: if the PID in the file name
+	 * is equal to the process' NStgid as observed by perf, then it must be
+	 * a match.
 	 */
+	*in_pidns = nsinfo__synthesized(nsi) || pid != nsinfo__nstgid(nsi);
 	if (pid && !(pid2 == pid || *in_pidns))
 		return -1;
 	/*
tools/perf/util/namespaces.c +9 -0
index 01502570b32d0..7de5d62e271c4 100644
@@ -132,6 +132,8 @@ int nsinfo__init(struct nsinfo *nsi)
 
 	rv = nsinfo__get_nspid(&RC_CHK_ACCESS(nsi)->tgid, &RC_CHK_ACCESS(nsi)->nstgid,
 			       &RC_CHK_ACCESS(nsi)->in_pidns, spath);
+	if (rv == 0)
+		RC_CHK_ACCESS(nsi)->synthesized = false;
 
 out:
 	free(newns);
@@ -166,6 +168,7 @@ struct nsinfo *nsinfo__new(pid_t pid)
 	RC_CHK_ACCESS(nsi)->nstgid = pid;
 	nsinfo__clear_need_setns(nsi);
 	RC_CHK_ACCESS(nsi)->in_pidns = false;
+	RC_CHK_ACCESS(nsi)->synthesized = true;
 	/* 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.
@@ -197,6 +200,7 @@ struct nsinfo *nsinfo__copy(const struct nsinfo *nsi)
 	RC_CHK_ACCESS(nnsi)->nstgid = nsinfo__nstgid(nsi);
 	RC_CHK_ACCESS(nnsi)->need_setns = nsinfo__need_setns(nsi);
 	RC_CHK_ACCESS(nnsi)->in_pidns = nsinfo__in_pidns(nsi);
+	RC_CHK_ACCESS(nnsi)->synthesized = nsinfo__synthesized(nsi);
 	if (nsinfo__mntns_path(nsi)) {
 		RC_CHK_ACCESS(nnsi)->mntns_path = strdup(nsinfo__mntns_path(nsi));
 		if (!RC_CHK_ACCESS(nnsi)->mntns_path) {
@@ -275,6 +279,11 @@ void nsinfo__set_in_pidns(struct nsinfo *nsi)
 	RC_CHK_ACCESS(nsi)->in_pidns = true;
 }
 
+bool nsinfo__synthesized(const struct nsinfo *nsi)
+{
+	return RC_CHK_ACCESS(nsi)->synthesized;
+}
+
 void nsinfo__mountns_enter(struct nsinfo *nsi,
 				  struct nscookie *nc)
 {
tools/perf/util/namespaces.h +0 -0
index e95c79b80e27c..41ba2ea8137e5 100644

tools/perf/util/namespaces.h DECLARE_RC_STRUCT(nsinfo) { · line 41

Quoted 4 lines
tools/perf/util/namespaces.h +1 -0
@@ -38,6 +38,7 @@ DECLARE_RC_STRUCT(nsinfo) {
 	bool			in_pidns;
 	char			*mntns_path;
 	refcount_t		refcnt;
+	bool			synthesized;

It'd be nice if you can put this along with other bool fields.

Thanks,
Namhyung

Quoted 14 lines
tools/perf/util/namespaces.h +1 -0
 };
 
 struct nscookie {
@@ -60,6 +61,7 @@ pid_t nsinfo__nstgid(const struct nsinfo  *nsi);
 pid_t nsinfo__pid(const struct nsinfo  *nsi);
 bool nsinfo__in_pidns(const struct nsinfo  *nsi);
 void nsinfo__set_in_pidns(struct nsinfo *nsi);
+bool nsinfo__synthesized(const struct nsinfo  *nsi);
 
 void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc);
 void nsinfo__mountns_exit(struct nscookie *nc);
Signature · 1 line
2.51.1
#3 lore raw
Quoted 33 lines · Namhyung Kim
Quoted 23 lines · Ilya Leoshkevich

perf inject fails to detect jitdump file produced by a process running in a different PID namespace if this process has not exited yet.

The PID namespace heuristic in jit_detect() compares two PIDs:

* pid: outermost NStgid of mmap(jitdump) caller from perf's PoV.
* nsinfo__nstgid(nsi): innermost NStgid of mmap(jitdump) caller from
                       perf's PoV.

The semantics of the in_pidns variable can be seen in, e.g., nsinfo__get_nspid(): it's true if and only if perf and the profiled process are in different PID namespaces.

The current logic is clearly inverted: if pid and nsinfo__nstgid(nsi)
are different, then the profiled process must be in a different PID namespace. This, of course, ignores that fact that they may end up being equal by accident, but that's not the point here.

Fix by flipping the comparison.

Quoted 8 lines

Changing just that, however, breaks the case when the process has exited. Add explicit support for that by adding "synthesized" field to
nsinfo, which tracks whether NStgid was obtained from a running process (ignoring considerations of PID reuse or running inject on a different machine). When the namespace information is synthesized,
assume the process ran in a different PID namespace.

Quoted 2 lines

I'm not sure I'm following.  It'd be great if anyone understand the topic well could review.

Perhaps some data from the testcase from [5/5] can make it more clear. Here are the PIDs that exist in reality:

unshare[a] perf-record unshare[b] jshell
Host PIDNS: 1000 1001 1002 1003

PIDNS[a]:       -          1           2          3
PIDNS[b]:       -          -           -          1

In jit_detect() we deal with 2 of them.

- pid is jshell@PIDNS[a].
It is taken from the MMAP2 event, this is how perf sees jshell.

- pid2 is jshell@PIDNS[b].
It is taken from "jit-1.dump", this is how jshell sees itself.

- nsinfo__nstgid(nsi) ideally should be jshell@PIDNS[b]. This is jshell's innermost NStgid.
But perf can see it differently. This is the core of the problem this series deals with.

Why does nsinfo__nstgid(nsi) vary? Because the kernel does not record it, and perf has to guess it. I have a WIP patch to fix that [1], but it needs a lot more work at this point.

How does perf guess it? It looks into /proc/$PID/status. This is quite unreliable, but this is the best perf can do under circumstances. As a result we have 3 possibilities:

- The original process is still around. This is the buggy case. In this case nsinfo__nstgid(nsi) == jshell@PIDNS[b]. IMHO this is a very clear indication of namespacing, and that's why the condition should be flipped.

- The original process has exited and PID was not reused. I believe this is the use case the current code has been extensively tested with. In this case perf assumes there was no namespacing and nsinfo__nstgid(nsi) == pid. That's why I need the "synthesized"
field: to indicate that NStgid is just an assumption and didn't come from any real data source.

- The original process has exited ans PID was reused. I am not trying to deal with this here, because this is rare and absolutely anything is possible. The only concern is running perf inject on a different machine, but I'm also not sure whether we should care about this.

[1]
https://github.com/iii-i/linux/commit/4b519b774eed850abe0757df39be13a704d2748e

[...]

Quoted 4 lines
Quoted 4 lines
tools/perf/util/namespaces.h +0 -1
index e95c79b80e27c..41ba2ea8137e5 100644
--- a/tools/perf/util/namespaces.h
Quoted 8 lines

tools/perf/util/namespaces.h DECLARE_RC_STRUCT(nsinfo) { · line 41

Quoted 6 lines

+++ b/tools/perf/util/namespaces.h

tools/perf/util/namespaces.h +1 -0
@@ -38,6 +38,7 @@ DECLARE_RC_STRUCT(nsinfo) {
  	bool			in_pidns;
  	char			*mntns_path;
  	refcount_t		refcnt;
+	bool			synthesized;

It'd be nice if you can put this along with other bool fields.

Sure, will do.

[...]

Quoted 1 line
maintainer
#4 lore raw

Hello,

Sorry for the delay.

Quoted 50 lines · Ilya Leoshkevich
Quoted 33 lines · Namhyung Kim
Quoted 23 lines · Ilya Leoshkevich

perf inject fails to detect jitdump file produced by a process running in a different PID namespace if this process has not exited yet.

The PID namespace heuristic in jit_detect() compares two PIDs:

* pid: outermost NStgid of mmap(jitdump) caller from perf's PoV.
* nsinfo__nstgid(nsi): innermost NStgid of mmap(jitdump) caller from
                       perf's PoV.

The semantics of the in_pidns variable can be seen in, e.g., nsinfo__get_nspid(): it's true if and only if perf and the profiled process are in different PID namespaces.

The current logic is clearly inverted: if pid and nsinfo__nstgid(nsi)
are different, then the profiled process must be in a different PID namespace. This, of course, ignores that fact that they may end up being equal by accident, but that's not the point here.

Fix by flipping the comparison.

Quoted 8 lines

Changing just that, however, breaks the case when the process has exited. Add explicit support for that by adding "synthesized" field to
nsinfo, which tracks whether NStgid was obtained from a running process (ignoring considerations of PID reuse or running inject on a different machine). When the namespace information is synthesized,
assume the process ran in a different PID namespace.

Quoted 2 lines

I'm not sure I'm following.  It'd be great if anyone understand the topic well could review.

Perhaps some data from the testcase from [5/5] can make it more clear. Here are the PIDs that exist in reality:

unshare[a] perf-record unshare[b] jshell
Host PIDNS: 1000 1001 1002 1003

PIDNS[a]:       -          1           2          3
PIDNS[b]:       -          -           -          1

In jit_detect() we deal with 2 of them.

- pid is jshell@PIDNS[a].
It is taken from the MMAP2 event, this is how perf sees jshell.

Quoted 7 lines

- pid2 is jshell@PIDNS[b].
It is taken from "jit-1.dump", this is how jshell sees itself.

- nsinfo__nstgid(nsi) ideally should be jshell@PIDNS[b]. This is jshell's innermost NStgid.
But perf can see it differently. This is the core of the problem this series deals with.

Thanks a lot for the example and explanation! I'm trying to understand. :)

Quoted 5 lines

Why does nsinfo__nstgid(nsi) vary? Because the kernel does not record it, and perf has to guess it. I have a WIP patch to fix that [1], but it needs a lot more work at this point.

Quoted 8 lines

How does perf guess it? It looks into /proc/$PID/status. This is quite unreliable, but this is the best perf can do under circumstances. As a result we have 3 possibilities:

- The original process is still around. This is the buggy case. In this case nsinfo__nstgid(nsi) == jshell@PIDNS[b]. IMHO this is a very clear indication of namespacing, and that's why the condition should be flipped.

So perf would look at /proc/3/status and the file would have the below

NStgid: 1003 3 1

and *in_pidns should be true, right?

Quoted 7 lines

- The original process has exited and PID was not reused. I believe this is the use case the current code has been extensively tested with. In this case perf assumes there was no namespacing and nsinfo__nstgid(nsi) == pid. That's why I need the "synthesized"
field: to indicate that NStgid is just an assumption and didn't come from any real data source.

Ok, can you please put short comments on each boolean field so that we can see the meaning of them clearly?

Quoted 5 lines

- The original process has exited ans PID was reused. I am not trying to deal with this here, because this is rare and absolutely anything is possible. The only concern is running perf inject on a different machine, but I'm also not sure whether we should care about this.

Fair enough.

Thanks,
Namhyung

Quoted 23 lines

[1]
https://github.com/iii-i/linux/commit/4b519b774eed850abe0757df39be13a704d2748e

[...]

Quoted 4 lines
Quoted 4 lines
tools/perf/util/namespaces.h +0 -1
index e95c79b80e27c..41ba2ea8137e5 100644
--- a/tools/perf/util/namespaces.h
Quoted 8 lines

tools/perf/util/namespaces.h DECLARE_RC_STRUCT(nsinfo) { · line 41

Quoted 6 lines

+++ b/tools/perf/util/namespaces.h

tools/perf/util/namespaces.h +1 -0
@@ -38,6 +38,7 @@ DECLARE_RC_STRUCT(nsinfo) {
  	bool			in_pidns;
  	char			*mntns_path;
  	refcount_t		refcnt;
+	bool			synthesized;

It'd be nice if you can put this along with other bool fields.

Sure, will do.

[...]

Quoted 1 line
#5 lore raw
Quoted 74 lines · Namhyung Kim

Hello,

Sorry for the delay.

Quoted 60 lines · Ilya Leoshkevich
Quoted 41 lines · Namhyung Kim
Quoted 28 lines · Ilya Leoshkevich

perf inject fails to detect jitdump file produced by a process running in a different PID namespace if this process has not exited
yet.

The PID namespace heuristic in jit_detect() compares two PIDs:

* pid: outermost NStgid of mmap(jitdump) caller from perf's PoV.
* nsinfo__nstgid(nsi): innermost NStgid of mmap(jitdump) caller from
                       perf's PoV.

The semantics of the in_pidns variable can be seen in, e.g., nsinfo__get_nspid(): it's true if and only if perf and the profiled
process are in different PID namespaces.

The current logic is clearly inverted: if pid and nsinfo__nstgid(nsi)
are different, then the profiled process must be in a different PID
namespace. This, of course, ignores that fact that they may end up
being equal by accident, but that's not the point here.

Fix by flipping the comparison.

Quoted 11 lines

Changing just that, however, breaks the case when the process has
exited. Add explicit support for that by adding "synthesized" field
to
nsinfo, which tracks whether NStgid was obtained from a running process (ignoring considerations of PID reuse or running inject on
a different machine). When the namespace information is synthesized,
assume the process ran in a different PID namespace.

Quoted 3 lines

I'm not sure I'm following.  It'd be great if anyone understand the
topic well could review.

Perhaps some data from the testcase from [5/5] can make it more clear.
Here are the PIDs that exist in reality:

             unshare[a] perf-record unshare[b] jshell Host PIDNS:  1000       1001        1002       1003 PIDNS[a]:       -          1           2          3 PIDNS[b]:       -          -           -          1

In jit_detect() we deal with 2 of them.

- pid is jshell@PIDNS[a].
  It is taken from the MMAP2 event, this is how perf sees jshell.

Quoted 8 lines

- pid2 is jshell@PIDNS[b].
  It is taken from "jit-1.dump", this is how jshell sees itself.

- nsinfo__nstgid(nsi) ideally should be jshell@PIDNS[b]. This is jshell's innermost NStgid.
  But perf can see it differently. This is the core of the problem this
  series deals with.

Quoted 3 lines

Thanks a lot for the example and explanation!  I'm trying to understand.
:)

Thank you for the patience!

Quoted 19 lines
Quoted 12 lines

Why does nsinfo__nstgid(nsi) vary? Because the kernel does not record
it, and perf has to guess it. I have a WIP patch to fix that [1], but
it needs a lot more work at this point.

How does perf guess it? It looks into /proc/$PID/status. This is quite
unreliable, but this is the best perf can do under circumstances. As a
result we have 3 possibilities:

Quoted 6 lines

- The original process is still around. This is the buggy case. In this
  case nsinfo__nstgid(nsi) == jshell@PIDNS[b]. IMHO this is a very clear indication of namespacing, and that's why the condition should
  be flipped.

Quoted 6 lines

So perf would look at /proc/3/status and the file would have the below

  NStgid:  1003  3  1

and *in_pidns should be true, right?

It should be

NStgid: 3 1

because perf itself is namespaced too - in this testcase nobody sees the root PID namespace. I wrote it this way to make sure there are no accidental leaks from the root PID namespace, but it's not very important and perhaps obscures things somewhat unnecessarily.

But regardless, I believe that in this case setting *in_pidns to true is the right thing.

Quoted 11 lines
Quoted 7 lines

- The original process has exited and PID was not reused. I believe this is the use case the current code has been extensively tested with. In this case perf assumes there was no namespacing and nsinfo__nstgid(nsi) == pid. That's why I need the "synthesized"
  field: to indicate that NStgid is just an assumption and didn't come
  from any real data source.

Ok, can you please put short comments on each boolean field so that we
can see the meaning of them clearly?

Sure, I can add this in v2.

[...]

maintainer
#6 lore raw
Quoted 107 lines · Ilya Leoshkevich
Quoted 74 lines · Namhyung Kim

Hello,

Sorry for the delay.

Quoted 60 lines · Ilya Leoshkevich
Quoted 41 lines · Namhyung Kim
Quoted 28 lines · Ilya Leoshkevich

perf inject fails to detect jitdump file produced by a process running in a different PID namespace if this process has not exited
yet.

The PID namespace heuristic in jit_detect() compares two PIDs:

* pid: outermost NStgid of mmap(jitdump) caller from perf's PoV.
* nsinfo__nstgid(nsi): innermost NStgid of mmap(jitdump) caller from
                       perf's PoV.

The semantics of the in_pidns variable can be seen in, e.g., nsinfo__get_nspid(): it's true if and only if perf and the profiled
process are in different PID namespaces.

The current logic is clearly inverted: if pid and nsinfo__nstgid(nsi)
are different, then the profiled process must be in a different PID
namespace. This, of course, ignores that fact that they may end up
being equal by accident, but that's not the point here.

Fix by flipping the comparison.

Quoted 11 lines

Changing just that, however, breaks the case when the process has
exited. Add explicit support for that by adding "synthesized" field
to
nsinfo, which tracks whether NStgid was obtained from a running process (ignoring considerations of PID reuse or running inject on
a different machine). When the namespace information is synthesized,
assume the process ran in a different PID namespace.

Quoted 3 lines

I'm not sure I'm following.  It'd be great if anyone understand the
topic well could review.

Perhaps some data from the testcase from [5/5] can make it more clear.
Here are the PIDs that exist in reality:

             unshare[a] perf-record unshare[b] jshell Host PIDNS:  1000       1001        1002       1003 PIDNS[a]:       -          1           2          3 PIDNS[b]:       -          -           -          1

In jit_detect() we deal with 2 of them.

- pid is jshell@PIDNS[a].
  It is taken from the MMAP2 event, this is how perf sees jshell.

Quoted 8 lines

- pid2 is jshell@PIDNS[b].
  It is taken from "jit-1.dump", this is how jshell sees itself.

- nsinfo__nstgid(nsi) ideally should be jshell@PIDNS[b]. This is jshell's innermost NStgid.
  But perf can see it differently. This is the core of the problem this
  series deals with.

Quoted 3 lines

Thanks a lot for the example and explanation!  I'm trying to understand.
:)

Thank you for the patience!

Quoted 19 lines
Quoted 12 lines

Why does nsinfo__nstgid(nsi) vary? Because the kernel does not record
it, and perf has to guess it. I have a WIP patch to fix that [1], but
it needs a lot more work at this point.

How does perf guess it? It looks into /proc/$PID/status. This is quite
unreliable, but this is the best perf can do under circumstances. As a
result we have 3 possibilities:

Quoted 6 lines

- The original process is still around. This is the buggy case. In this
  case nsinfo__nstgid(nsi) == jshell@PIDNS[b]. IMHO this is a very clear indication of namespacing, and that's why the condition should
  be flipped.

Quoted 6 lines

So perf would look at /proc/3/status and the file would have the below

  NStgid:  1003  3  1

and *in_pidns should be true, right?

Quoted 8 lines

It should be

NStgid: 3 1

because perf itself is namespaced too - in this testcase nobody sees the root PID namespace. I wrote it this way to make sure there are no accidental leaks from the root PID namespace, but it's not very important and perhaps obscures things somewhat unnecessarily.

Ah, ok. Thanks for the correction.

Quoted 3 lines

But regardless, I believe that in this case setting *in_pidns to true is the right thing.

Yep, I agree.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

- The original process has exited and PID was not reused. I believe this is the use case the current code has been extensively tested with. In this case perf assumes there was no namespacing and nsinfo__nstgid(nsi) == pid. That's why I need the "synthesized"
  field: to indicate that NStgid is just an assumption and didn't come
  from any real data source.

Quoted 5 lines
Quoted 3 lines

Ok, can you please put short comments on each boolean field so that we
can see the meaning of them clearly?

Sure, I can add this in v2.

Perhaps it's better to split the part adding "synthesize" field to nsinfo in a separate commit and this one focuses on setting in_pidns.

Thanks,
Namhyung

#7 lore raw

PATCH 2/5 perf test java symbol: Get rid of shellcheck warnings

In reply to Ilya Leoshkevich

Add missing quotes and suppress the $? warnings.

---
 tools/perf/tests/shell/test_java_symbol.sh | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
tools/perf/tests/shell/test_java_symbol.sh +12 -8
index 499539d1c4794..b1d7cd43af01a 100755
@@ -4,6 +4,9 @@
 # SPDX-License-Identifier: GPL-2.0
 # Leo Yan <[email protected]>, 2022
 
+# Allow [ $? -ne 0 ], because long commands look ugly in if statements.
+# shellcheck disable=SC2181
+
 # skip if there's no jshell
 if ! [ -x "$(command -v jshell)" ]; then
 	echo "skip: no jshell, install JDK"
@@ -13,11 +16,12 @@ fi
 PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
 PERF_INJ_DATA=$(mktemp /tmp/__perf_test.perf.data.inj.XXXXX)
 
-cleanup_files()
-{
+# Shellcheck does not understand that this function is used by a trap.
+# shellcheck disable=SC2317
+cleanup_files() {
 	echo "Cleaning up files..."
-	rm -f ${PERF_DATA}
-	rm -f ${PERF_INJ_DATA}
+	rm -f "$PERF_DATA"
+	rm -f "$PERF_INJ_DATA"
 }
 
 trap cleanup_files exit term int
@@ -38,7 +42,7 @@ else
 	exit 2
 fi
 
-cat <<EOF | perf record -k 1 -o $PERF_DATA jshell -s -J-agentpath:$LIBJVMTI
+cat <<EOF | perf record -k 1 -o "$PERF_DATA" jshell -s -J"-agentpath:$LIBJVMTI"
 int fib(int x) {
 	return x > 1 ? fib(x - 2) + fib(x - 1) : 1;
 }
@@ -56,7 +60,7 @@ if [ $? -ne 0 ]; then
 	exit 1
 fi
 
-if ! DEBUGINFOD_URLS='' perf inject -i $PERF_DATA -o $PERF_INJ_DATA -j; then
+if ! DEBUGINFOD_URLS='' perf inject -i "$PERF_DATA" -o "$PERF_INJ_DATA" -j; then
 	echo "Fail to inject samples"
 	exit 1
 fi
@@ -64,8 +68,8 @@ fi
 # Below is an example of the instruction samples reporting:
 #   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
 #   0.75%  Thread-1         jitted-83602-1670.so  [.] jdk.internal.jimage.BasicImageReader.getString(int)
-perf report --stdio -i ${PERF_INJ_DATA} 2>&1 | \
-	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" > /dev/null 2>&1
+perf report --stdio -i "$PERF_INJ_DATA" 2>&1 |
+	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" >/dev/null 2>&1
 
 if [ $? -ne 0 ]; then
 	echo "Fail to find java symbols"
Signature · 1 line
2.51.1
maintainer
#8 lore raw

Re: [PATCH 2/5] perf test java symbol: Get rid of shellcheck warnings

Hello,

Quoted 1 line · Ilya Leoshkevich

Add missing quotes and suppress the $? warnings.

Can you please share the actual shellcheck warnings you see?

Quoted 21 lines
---
 tools/perf/tests/shell/test_java_symbol.sh | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
tools/perf/tests/shell/test_java_symbol.sh +3 -0
index 499539d1c4794..b1d7cd43af01a 100755
@@ -4,6 +4,9 @@
 # SPDX-License-Identifier: GPL-2.0
 # Leo Yan <[email protected]>, 2022
 
+# Allow [ $? -ne 0 ], because long commands look ugly in if statements.
+# shellcheck disable=SC2181
+
 # skip if there's no jshell
 if ! [ -x "$(command -v jshell)" ]; then
 	echo "skip: no jshell, install JDK"

tools/perf/tests/shell/test_java_symbol.sh fi · line 21

Quoted 8 lines
tools/perf/tests/shell/test_java_symbol.sh +3 -2
@@ -13,11 +16,12 @@ fi
 PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
 PERF_INJ_DATA=$(mktemp /tmp/__perf_test.perf.data.inj.XXXXX)
 
-cleanup_files()
-{
+# Shellcheck does not understand that this function is used by a trap.
+# shellcheck disable=SC2317
+cleanup_files() {

Please minimize unnecessary changes. It seems you don't need to change the function declaration.

Thanks,
Namhyung

Quoted 40 lines
tools/perf/tests/shell/test_java_symbol.sh +6 -6
 	echo "Cleaning up files..."
-	rm -f ${PERF_DATA}
-	rm -f ${PERF_INJ_DATA}
+	rm -f "$PERF_DATA"
+	rm -f "$PERF_INJ_DATA"
 }
 
 trap cleanup_files exit term int
@@ -38,7 +42,7 @@ else
 	exit 2
 fi
 
-cat <<EOF | perf record -k 1 -o $PERF_DATA jshell -s -J-agentpath:$LIBJVMTI
+cat <<EOF | perf record -k 1 -o "$PERF_DATA" jshell -s -J"-agentpath:$LIBJVMTI"
 int fib(int x) {
 	return x > 1 ? fib(x - 2) + fib(x - 1) : 1;
 }
@@ -56,7 +60,7 @@ if [ $? -ne 0 ]; then
 	exit 1
 fi
 
-if ! DEBUGINFOD_URLS='' perf inject -i $PERF_DATA -o $PERF_INJ_DATA -j; then
+if ! DEBUGINFOD_URLS='' perf inject -i "$PERF_DATA" -o "$PERF_INJ_DATA" -j; then
 	echo "Fail to inject samples"
 	exit 1
 fi
@@ -64,8 +68,8 @@ fi
 # Below is an example of the instruction samples reporting:
 #   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
 #   0.75%  Thread-1         jitted-83602-1670.so  [.] jdk.internal.jimage.BasicImageReader.getString(int)
-perf report --stdio -i ${PERF_INJ_DATA} 2>&1 | \
-	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" > /dev/null 2>&1
+perf report --stdio -i "$PERF_INJ_DATA" 2>&1 |
+	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" >/dev/null 2>&1
 
 if [ $? -ne 0 ]; then
 	echo "Fail to find java symbols"
Signature · 1 line
2.51.1
#9 lore raw

Re: [PATCH 2/5] perf test java symbol: Get rid of shellcheck warnings

Quoted 6 lines · Namhyung Kim

Hello,

Quoted 1 line · Ilya Leoshkevich

Add missing quotes and suppress the $? warnings.

Can you please share the actual shellcheck warnings you see?

There are a lot of them, I have cut similar ones from the output:

linux/tools/perf/tests/shell$ shellcheck -x test_java_symbol.sh

[...]

In test_java_symbol.sh line 19:

        rm -f ${PERF_DATA}
        ^----------------^ SC2317 (info): Command appears to be
unreachable. Check usage (or ignore if invoked indirectly).
              ^----------^ SC2086 (info): Double quote to prevent

globbing and word splitting.

[...]

In test_java_symbol.sh line 54:
if [ $? -ne 0 ]; then
^-- SC2181 (style): Check exit code directly with e.g. 'if !
mycmd;', not indirectly with $?.

[...]

Quoted 28 lines
Quoted 28 lines

---
 tools/perf/tests/shell/test_java_symbol.sh | 20 ++++++++++++------ --

 1 file changed, 12 insertions(+), 8 deletions(-)
tools/perf/tests/shell/test_java_symbol.sh +0 -0
index 499539d1c4794..b1d7cd43af01a 100755
@@ -4,6 +4,9 @@
  # SPDX-License-Identifier: GPL-2.0
  # Leo Yan <[email protected]>, 2022


+# Allow [ $? -ne 0 ], because long commands look ugly in if statements.

tools/perf/tests/shell/test_java_symbol.sh +2 -0
+# shellcheck disable=SC2181
+
 # skip if there's no jshell
 if ! [ -x "$(command -v jshell)" ]; then
 	echo "skip: no jshell, install JDK"
tools/perf/tests/shell/test_java_symbol.sh +0 -0
@@ -13,11 +16,12 @@ fi
  PERF_DATA=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
  PERF_INJ_DATA=$(mktemp /tmp/__perf_test.perf.data.inj.XXXXX)

tools/perf/tests/shell/test_java_symbol.sh +0 -2
-cleanup_files()
-{
Quoted 8 lines

tools/perf/tests/shell/test_java_symbol.sh fi · line 21

Quoted 4 lines

+# Shellcheck does not understand that this function is used by a trap.

tools/perf/tests/shell/test_java_symbol.sh +2 -0
+# shellcheck disable=SC2317
+cleanup_files() {

Please minimize unnecessary changes.  It seems you don't need to change
the function declaration.

My bad, I ran shfmt as well and forgot about this. Interestingly enough, there appears to be no consensus regarding style, but { on the same line is a bit more popular:

linux/tools/perf/tests/shell$ grep -R '^[a-zA-Z0-9_]*()$' | wc -l 128

linux/tools/perf/tests/shell$ grep -R '^[a-zA-Z0-9_]*() {$' | wc -l 185

[...]

Quoted 1 line
#10 lore raw

PATCH 3/5 perf test java symbol: Extract LIBJVMTI detection

In reply to Ilya Leoshkevich

Extract LIBJVMTI into lib/setup_libjvmti.sh for reuse by future tests.

---
 tools/perf/tests/shell/lib/setup_libjvmti.sh | 18 ++++++++++++++++++
 tools/perf/tests/shell/test_java_symbol.sh   | 17 ++---------------
 2 files changed, 20 insertions(+), 15 deletions(-)
 create mode 100644 tools/perf/tests/shell/lib/setup_libjvmti.sh
tools/perf/tests/shell/lib/setup_libjvmti.sh new file +18 -0
new file mode 100644
index 0000000000000..fab4a44f8ecb2
@@ -0,0 +1,18 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+if [ -e "$PWD/tools/perf/libperf-jvmti.so" ]; then
+	LIBJVMTI=$PWD/tools/perf/libperf-jvmti.so
+elif [ -e "$PWD/libperf-jvmti.so" ]; then
+	LIBJVMTI=$PWD/libperf-jvmti.so
+elif [ -e "$PREFIX/lib64/libperf-jvmti.so" ]; then
+	LIBJVMTI=$PREFIX/lib64/libperf-jvmti.so
+elif [ -e "$PREFIX/lib/libperf-jvmti.so" ]; then
+	LIBJVMTI=$PREFIX/lib/libperf-jvmti.so
+elif [ -e "/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so" ]; then
+	LIBJVMTI=/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so
+else
+	echo "Fail to find libperf-jvmti.so"
+	# JVMTI is a build option, skip the test if fail to find lib
+	exit 2
+fi
tools/perf/tests/shell/test_java_symbol.sh +2 -15
index b1d7cd43af01a..f36c9321568c5 100755
@@ -26,21 +26,8 @@ cleanup_files() {
 
 trap cleanup_files exit term int
 
-if [ -e "$PWD/tools/perf/libperf-jvmti.so" ]; then
-	LIBJVMTI=$PWD/tools/perf/libperf-jvmti.so
-elif [ -e "$PWD/libperf-jvmti.so" ]; then
-	LIBJVMTI=$PWD/libperf-jvmti.so
-elif [ -e "$PREFIX/lib64/libperf-jvmti.so" ]; then
-	LIBJVMTI=$PREFIX/lib64/libperf-jvmti.so
-elif [ -e "$PREFIX/lib/libperf-jvmti.so" ]; then
-	LIBJVMTI=$PREFIX/lib/libperf-jvmti.so
-elif [ -e "/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so" ]; then
-	LIBJVMTI=/usr/lib/linux-tools-$(uname -a | awk '{ print $3 }' | sed -r 's/-generic//')/libperf-jvmti.so
-else
-	echo "Fail to find libperf-jvmti.so"
-	# JVMTI is a build option, skip the test if fail to find lib
-	exit 2
-fi
+# shellcheck source=lib/setup_libjvmti.sh
+. "$(dirname "$0")/lib/setup_libjvmti.sh"
 
 cat <<EOF | perf record -k 1 -o "$PERF_DATA" jshell -s -J"-agentpath:$LIBJVMTI"
 int fib(int x) {
Signature · 1 line
2.51.1
#11 lore raw

PATCH 4/5 perf test java symbol: Fix a false negative in symbol regex

In reply to Ilya Leoshkevich

There are a lot of symbols like InterpreterRuntime::resolve_get_put() in the perf report output, so the existing regex unfortunately always matches something. Replace it with a more precise one.

---
 tools/perf/tests/shell/test_java_symbol.sh | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/tests/shell/test_java_symbol.sh +3 -1
index f36c9321568c5..4c6bc57b87181 100755
@@ -55,8 +55,10 @@ fi
 # Below is an example of the instruction samples reporting:
 #   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
 #   0.75%  Thread-1         jitted-83602-1670.so  [.] jdk.internal.jimage.BasicImageReader.getString(int)
+# Look for them, while avoiding false positives from lines like this:
+#   0.03%  jshell           libjvm.so             [.] InterpreterRuntime::resolve_get_put(JavaThread*, Bytecodes::Code)
 perf report --stdio -i "$PERF_INJ_DATA" 2>&1 |
-	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" >/dev/null 2>&1
+	grep ' jshell .* jitted-.*\.so .* \(Interpreter$\|jdk\.internal\)' &>/dev/null
 
 if [ $? -ne 0 ]; then
 	echo "Fail to find java symbols"
Signature · 1 line
2.51.1
maintainer
#12 lore raw

Re: [PATCH 4/5] perf test java symbol: Fix a false negative in symbol regex

Quoted 14 lines · Ilya Leoshkevich

There are a lot of symbols like InterpreterRuntime::resolve_get_put() in the perf report output, so the existing regex unfortunately always matches something. Replace it with a more precise one.

---
 tools/perf/tests/shell/test_java_symbol.sh | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/tests/shell/test_java_symbol.sh +0 -0
index f36c9321568c5..4c6bc57b87181 100755

tools/perf/tests/shell/test_java_symbol.sh fi · line 61

Quoted 8 lines
tools/perf/tests/shell/test_java_symbol.sh +3 -1
@@ -55,8 +55,10 @@ fi
 # Below is an example of the instruction samples reporting:
 #   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
 #   0.75%  Thread-1         jitted-83602-1670.so  [.] jdk.internal.jimage.BasicImageReader.getString(int)
+# Look for them, while avoiding false positives from lines like this:
+#   0.03%  jshell           libjvm.so             [.] InterpreterRuntime::resolve_get_put(JavaThread*, Bytecodes::Code)
 perf report --stdio -i "$PERF_INJ_DATA" 2>&1 |
-	grep -E " +[0-9]+\.[0-9]+% .* (Interpreter|jdk\.internal).*" >/dev/null 2>&1
+	grep ' jshell .* jitted-.*\.so .* \(Interpreter$\|jdk\.internal\)' &>/dev/null

Maybe 'jshell' part can go away as well.. but it's up to you. :)

Thanks,
Namhyung

Quoted 6 lines
tools/perf/tests/shell/test_java_symbol.sh +0 -0
 if [ $? -ne 0 ]; then
 	echo "Fail to find java symbols"
Signature · 1 line
2.51.1
#13 lore raw

Re: [PATCH 4/5] perf test java symbol: Fix a false negative in symbol regex

Quoted 34 lines · Namhyung Kim
Quoted 29 lines · Ilya Leoshkevich

There are a lot of symbols like InterpreterRuntime::resolve_get_put()
in the perf report output, so the existing regex unfortunately always
matches something. Replace it with a more precise one.

---
 tools/perf/tests/shell/test_java_symbol.sh | 4 +++-

 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/tests/shell/test_java_symbol.sh +1 -0
index f36c9321568c5..4c6bc57b87181 100755
@@ -55,8 +55,10 @@ fi
  # Below is an example of the instruction samples reporting:
  #   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
  #   0.75%  Thread-1         jitted-83602-1670.so  [.]
 jdk.internal.jimage.BasicImageReader.getString(int)
+# Look for them, while avoiding false positives from lines like

this:
+#   0.03%  jshell           libjvm.so             [.] InterpreterRuntime::resolve_get_put(JavaThread*, Bytecodes::Code) perf report --stdio -i "$PERF_INJ_DATA" 2>&1 |

tools/perf/tests/shell/test_java_symbol.sh +0 -1
-	grep -E " +[0-9]+\.[0-9]+% .*

(Interpreter|jdk\.internal).*" >/dev/null 2>&1

tools/perf/tests/shell/test_java_symbol.sh +1 -0
+	grep ' jshell .* jitted-.*\.so .*
Quoted 1 line

\(Interpreter$\|jdk\.internal\)' &>/dev/null

Maybe 'jshell' part can go away as well.. but it's up to you. :)

Quoted 2 lines

Thanks,
Namhyung

You are right, jdk.internal stuff may appear in Thread-1, as one can see in the existing example. I will drop jshell from the regex.

[...]

Quoted 1 line
#14 lore raw

PATCH 5/5 perf test java symbol: Add PID namespace variant

In reply to Ilya Leoshkevich

Add a PID namespace variant of test_java_symbol.sh. Compared to the existing test, we must jump through two extra hoops here.

First, we need to check whether unshare is there an is usable, which may not be the case either due to kernel config or permissions.

Second, we need to keep jshell running after perf record exits in order to reproduce a real issue with this setup. This means we have to attach perf after starting jshell, use ctl-fifo and ack-fifo, and work around perf's inability to attach to existing children.

---
 .../tests/shell/test_java_symbol_pidns.sh     | 135 ++++++++++++++++++
 1 file changed, 135 insertions(+)
 create mode 100755 tools/perf/tests/shell/test_java_symbol_pidns.sh
tools/perf/tests/shell/test_java_symbol_pidns.sh new file +135 -0
new file mode 100755
index 0000000000000..afee6d055ad90
@@ -0,0 +1,135 @@
+#!/bin/bash
+# Test symbol resolution for java running inside a PID namespace
+# SPDX-License-Identifier: GPL-2.0
+
+set -u -o pipefail
+
+# shellcheck source=lib/setup_libjvmti.sh
+. "$(dirname "$0")/lib/setup_libjvmti.sh"
+
+JSHELL=(jshell -s -J"-agentpath:$LIBJVMTI")
+if ! command -v jshell &>/dev/null; then
+	echo "skip: no jshell, install JDK"
+	exit 2
+fi
+
+UNSHARE=(
+	unshare
+	--user --map-user="$(id -u)" --map-group="$(id -g)"
+	--fork --pid --mount-proc
+)
+if ! "${UNSHARE[@]}" true; then
+	echo "skip: unshare not functional"
+	exit 2
+fi
+
+if [ "$#" -eq 0 ]; then
+	# Verify isolation by running the test inside a separate PID namespace.
+	exec "${UNSHARE[@]}" "$0" "$@" stage2
+fi
+if [ "$1" != stage2 ]; then
+	echo "$0 must be started without arguments"
+	exit 1
+fi
+shift
+
+WORKDIR=$(mktemp -d /tmp/__perf_test_java_symbol_pidns.XXXXX)
+trap 'rm -r "$WORKDIR"' EXIT
+
+test() {
+	TEST_ID=jshell-exit-$JSHELL_EXIT
+
+	# Verify that perf inject can deal with JIT dump produced by jshell
+	# running in PID namespace that is different from its own one.
+	# Make sure jshell is still around when perf inject runs, so attach and
+	# detach perf manually.
+	# jshell is a shell script that runs java as a child process, and perf
+	# cannot attach to existing children. Therefore create a stopped
+	# process, which will exec jshell after it's resumed.
+	JSHELL_STDIN=$WORKDIR/jshell-stdin-$TEST_ID
+	JSHELL_STDOUT=$WORKDIR/jshell-stdout-$TEST_ID
+	mkfifo "$JSHELL_STDIN"
+	mkfifo "$JSHELL_STDOUT"
+	exec {JSHELL_STDIN_FD}<>"$JSHELL_STDIN"
+	exec {JSHELL_STDOUT_FD}<>"$JSHELL_STDOUT"
+	JITDUMPDIR="$WORKDIR" bash -c 'kill -STOP "$$" && exec "$0" "$@"' \
+		"${UNSHARE[@]}" "${JSHELL[@]}" \
+		<&"$JSHELL_STDIN_FD" >&"$JSHELL_STDOUT_FD" &
+	JSHELL_PID=$!
+
+	# Start perf record and wait until it attaches to jshell.
+	PERF_CTL=$WORKDIR/perf-ctl-$TEST_ID
+	PERF_ACK=$WORKDIR/perf-ack-$TEST_ID
+	mkfifo "$PERF_CTL"
+	mkfifo "$PERF_ACK"
+	exec {PERF_CTL_FD}<>"$PERF_CTL"
+	exec {PERF_ACK_FD}<>"$PERF_ACK"
+	PERF_DATA=$WORKDIR/perf-$TEST_ID.data
+	perf record --clockid=1 --delay=-1 \
+		--control="fd:$PERF_CTL_FD,$PERF_ACK_FD" \
+		--output="$PERF_DATA" --pid="$JSHELL_PID" &
+	PERF_PID=$!
+	echo "enable" >&"$PERF_CTL_FD"
+	while IFS= read -r LINE <&"$PERF_ACK_FD"; do
+		[ "$LINE" != "ack" ] || break
+	done
+
+	# Spawn jshell and ask it to run some CPU-intensive code.
+	kill -CONT "$JSHELL_PID"
+	cat >&"$JSHELL_STDIN_FD" <<EOF
+int fib(int x) { return x > 1 ? fib(x - 2) + fib(x - 1) : 1; }
+System.out.println(fib(44));
+EOF
+	while IFS= read -r LINE <&"$JSHELL_STDOUT_FD"; do
+		[ "$LINE" != 1134903170 ] || break
+	done
+
+	# Terminate perf record.
+	echo "stop" >"$PERF_CTL"
+	if ! wait "$PERF_PID"; then
+		echo "Fail to record for java program"
+		exit 1
+	fi
+
+	# Terminate jshell before perf inject.
+	if [ "$JSHELL_EXIT" = "early" ]; then
+		echo "/exit" >&"$JSHELL_STDIN_FD"
+		if ! wait "$JSHELL_PID"; then
+			echo "Fail to run java program"
+			exit 1
+		fi
+	fi
+
+	# Inject the JIT data.
+	PERF_INJ_DATA=$WORKDIR/perf-$TEST_ID.data.inj
+	if ! DEBUGINFOD_URLS="" perf inject --input="$PERF_DATA" \
+		--output="$PERF_INJ_DATA" --jit; then
+		echo "Fail to inject samples"
+		exit 1
+	fi
+
+	# Below is an example of the instruction samples reporting:
+	#   8.18%  jshell           jitted-50116-29.so    [.] Interpreter
+	#   0.75%  Thread-1         jitted-83602-1670.so  [.] jdk.internal.jimage.BasicImageReader.getString(int)
+	# Look for them, while avoiding false positives from lines like this:
+	#   0.03%  jshell           libjvm.so             [.] InterpreterRuntime::resolve_get_put(JavaThread*, Bytecodes::Code)
+	if ! perf report --input="$PERF_INJ_DATA" --stdio |
+		grep ' jshell .* jitted-.*\.so .* \(Interpreter$\|jdk\.internal\)' \
+			&>/dev/null; then
+		echo "Fail to find java symbols"
+		exit 1
+	fi
+
+	# Terminate jshell after perf inject.
+	if [ "$JSHELL_EXIT" = "late" ]; then
+		echo "/exit" >&"$JSHELL_STDIN_FD"
+		if ! wait "$JSHELL_PID"; then
+			echo "Fail to run java program"
+			exit 1
+		fi
+	fi
+}
+
+for JSHELL_EXIT in early late; do
+	test
+done
Signature · 1 line
2.51.1