On Thu, 11 Jun 2026 21:32:32 GMT, Coleen Phillimore <[email protected]> wrote:
>> Please review this change to allow XX configuration for specifying a >> different /tmp directory for the JVM to use. In some container >> environments, /tmp and /proc/pid/root/tmp might not be usable and an >> alternate would be used. This requires a release note and CSR. Usage is: >> >> java -XX:AltTempDir=/diags <app> >> jps -J-XX:AltTempDir=/diags >> jcmd -J-XX:AltTempDir=/diags <pid> <cmds> >> >> Tested with a couple of tests and locally, and ran tier1-4. >> >> --------- >> - [x] I confirm that I make this contribution in accordance with the >> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai). > > Coleen Phillimore has updated the pull request incrementally with one > additional commit since the last revision: > > Use shouldMatch for logging message. There is also a problem if a Java process in a container uses AltTempDir, a jps/jcmd process listing from the host cannot see the processes, even if setting the same AltTempDir (jps in the host usually lists Java processes running in containers). "open/src/jdk.internal.jvmstat/linux/classes/sun/jvmstat/PlatformSupportImpl.java" needs to update getLocalVmId() as it contains knowledge of the /tmp path to find hsperdata, which contains host pid and namespace (in-container) pid. Suggestion below which works me for (but is not foolproof...). One more issue: run in a container, with AltTempDir set to a directory mounted (shared) from the host. When the host runs jps with AltTempDir set the same, PlatformSupportImpl.java/getTemporaryDirectories() ignores some directories, because they are the same as its tmp dir (this was intentional, but becomes a problem in this case). $ build/linux-x64/images/jdk/bin/jps -J-XX:AltTempDir=/work 2880104 Jps 111 -- process information unavailable This means a /proc/2878800/root/work/hsperfdata_kwalls/111 path got ignored in preference for /work/hsperfdata_kwalls/111 ..from which it can't deduce both hostpid and nspid. Removing that check for the files being the same in getTemporaryDirectories() works: --- a/src/jdk.internal.jvmstat/linux/classes/sun/jvmstat/PlatformSupportImpl.java +++ b/src/jdk.internal.jvmstat/linux/classes/sun/jvmstat/PlatformSupportImpl.java @@ -159,9 +159,8 @@ public boolean accept(File dir, String name) { File containerFile = new File(containerTmpDir); if (containerFile.exists() && containerFile.isDirectory() && - containerFile.canRead() && - !tempDirectoryEquals(containerFile.toPath())) { - v.add(containerTmpDir); + containerFile.canRead()) { + v.add(containerTmpDir); } } ...although it shows an error in jps: $ build/linux-x64/images/jdk/bin/jps -J-XX:AltTempDir=/work 145 -- process information unavailable 2883758 ThreadsMem 2884405 Jps 2883758 and 145 are the same process. jcmd will ignore this trivial failure to attach (as processes may come and go...) and looks fine. Making jps behave the same as jcmd by ignoring this error is what I came up with: src/jdk.jcmd/share/classes/sun/tools/jps/Jps.java @@ -87,7 +86,6 @@ public static void main(String[] args) { // reasonable message to indicate that the requested // info is not available. - errorString = " -- process information unavailable"; VmIdentifier id = new VmIdentifier(vmidString); vm = monitoredHost.getMonitoredVm(id, 0); Here is a suggested PlatformSupportImpl.java/ getLocalVmId(File file) /* * Extract the VM ID (PID) from a file path. * Specifically the host pid for a container process. * * File path should be in 1 of these 2 forms: * * /proc/{pid}/root/tmp/hsperfdata_{user}/{nspid} * or * /tmp/hsperfdata_{user}/{pid} * * (where /tmp may be substituted due to -XX:AltTempDir) * * In either case we want to return {pid} and NOT {nspid} * * This function filters out host pids which do not have * associated hsperfdata files. This is due to the fact that * getTemporaryDirectories will return /proc/{pid}/root/tmp * paths for all container processes whether they are java * processes or not causing duplicate matches. */ public int getLocalVmId(File file) throws NumberFormatException { String p = file.getAbsolutePath(); String parts[] = p.split("\/"); boolean seenProc = false; boolean seenPerf = false; int hostpid = -1; int nspid = -1; // Step through, as format is flexible given -XX:AltTempDir settings. // (this could be fooled by including "hsperfdata_" in the temp dir path) for (String s : parts) { if (!seenProc && s.equals("proc")) { seenProc = true; continue; } if (seenProc && hostpid == -1) { // host pid immediately follows "proc". hostpid = Integer.parseInt(s); continue; } if (!seenPerf && s.startsWith("hsperfdata_")) { seenPerf = true; continue; } if (seenPerf) { // Parse pid or nspid after seeing "hsperfdata_" if (hostpid == -1) { hostpid = Integer.parseInt(s); } else { nspid = Integer.parseInt(s); } break; } } if (seenPerf) { if (nspid == -1) { return hostpid; } else { // We have both pids. if (nspid == getNamespaceVmId(hostpid)) { return hostpid; } } } return -1; } ------------- PR Comment: https://git.openjdk.org/jdk/pull/31407#issuecomment-4706747768
