author | rriggs |
Fri, 16 Mar 2018 16:17:17 -0400 | |
changeset 49260 | 55c0de67f375 |
parent 48801 | 099f18e10557 |
permissions | -rw-r--r-- |
48801
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
1 |
/* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
4 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
10 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
15 |
* accompanied this code). |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
16 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
20 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
23 |
* questions. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
24 |
*/ |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
25 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
26 |
package sun.jvmstat; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
27 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
28 |
import java.io.*; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
29 |
import java.util.*; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
30 |
import java.util.regex.*; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
31 |
import java.nio.file.Path; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
32 |
import java.nio.file.Paths; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
33 |
import java.nio.file.Files; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
34 |
import java.nio.charset.*; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
35 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
36 |
/* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
37 |
* Linux specific implementation of the PlatformSupport routines |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
38 |
* providing process ID and temp directory support for host and |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
39 |
* cgroup container processes. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
40 |
*/ |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
41 |
public class PlatformSupportImpl extends PlatformSupport { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
42 |
private static final String containerTmpPath = "/root" + getTemporaryDirectory(); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
43 |
private static final String pidPatternStr = "^[0-9]+$"; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
44 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
45 |
/* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
46 |
* Return the temporary directories that the VM uses for the attach |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
47 |
* and perf data files. This function returns the traditional |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
48 |
* /tmp directory in addition to paths within the /proc file system |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
49 |
* allowing access to container tmp directories such as /proc/{pid}/root/tmp. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
50 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
51 |
* It is important that this directory is well-known and the |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
52 |
* same for all VM instances. It cannot be affected by configuration |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
53 |
* variables such as java.io.tmpdir. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
54 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
55 |
* Implementation Details: |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
56 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
57 |
* Java processes that run in docker containers are typically running |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
58 |
* under cgroups with separate pid namespaces which means that pids |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
59 |
* within the container are different that the pid which is visible |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
60 |
* from the host. The container pids typically start with 1 and |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
61 |
* increase. The java process running in the container will use these |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
62 |
* pids when creating the hsperfdata files. In order to locate java |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
63 |
* processes that are running in containers, we take advantage of |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
64 |
* the Linux proc file system which maps the containers tmp directory |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
65 |
* to the hosts under /proc/{hostpid}/root/tmp. We use the /proc status |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
66 |
* file /proc/{hostpid}/status to determine the containers pid and |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
67 |
* then access the hsperfdata file. The status file contains an |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
68 |
* entry "NSPid:" which shows the mapping from the hostpid to the |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
69 |
* containers pid. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
70 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
71 |
* Example: |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
72 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
73 |
* NSPid: 24345 11 |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
74 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
75 |
* In this example process 24345 is visible from the host, |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
76 |
* is running under the PID namespace and has a container specific |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
77 |
* pid of 11. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
78 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
79 |
* The search for Java processes is done by first looking in the |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
80 |
* traditional /tmp for host process hsperfdata files and then |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
81 |
* the search will container in every /proc/{pid}/root/tmp directory. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
82 |
* There are of course added complications to this search that |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
83 |
* need to be taken into account. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
84 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
85 |
* 1. duplication of tmp directories |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
86 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
87 |
* /proc/{hostpid}/root/tmp directories exist for many processes |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
88 |
* that are running on a Linux kernel that has cgroups enabled even |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
89 |
* if they are not running in a container. To avoid this duplication, |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
90 |
* we compare the inode of the /proc tmp directories to /tmp and |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
91 |
* skip these duplicated directories. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
92 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
93 |
* 2. Containerized processes without PID namespaces being enabled. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
94 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
95 |
* If a container is running a Java process without namespaces being |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
96 |
* enabled, an hsperfdata file will only be located at |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
97 |
* /proc/{hostpid}/root/tmp/{hostpid}. This is handled by |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
98 |
* checking the last component in the path for both the hostpid |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
99 |
* and potential namespacepids (if one exists). |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
100 |
*/ |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
101 |
public List<String> getTemporaryDirectories(int pid) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
102 |
FilenameFilter pidFilter; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
103 |
Matcher pidMatcher; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
104 |
Pattern pidPattern = Pattern.compile(pidPatternStr); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
105 |
long tmpInode = 0; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
106 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
107 |
File procdir = new File("/proc"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
108 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
109 |
if (pid != 0) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
110 |
pidPattern = Pattern.compile(Integer.toString(pid)); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
111 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
112 |
else { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
113 |
pidPattern = Pattern.compile(pidPatternStr); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
114 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
115 |
pidMatcher = pidPattern.matcher(""); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
116 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
117 |
// Add the default temporary directory first |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
118 |
List<String> v = new ArrayList<>(); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
119 |
v.add(getTemporaryDirectory()); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
120 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
121 |
try { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
122 |
File f = new File(getTemporaryDirectory()); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
123 |
tmpInode = (Long)Files.getAttribute(f.toPath(), "unix:ino"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
124 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
125 |
catch (IOException e) {} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
126 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
127 |
pidFilter = new FilenameFilter() { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
128 |
public boolean accept(File dir, String name) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
129 |
if (!dir.isDirectory()) |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
130 |
return false; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
131 |
pidMatcher.reset(name); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
132 |
return pidMatcher.matches(); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
133 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
134 |
}; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
135 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
136 |
File[] dirs = procdir.listFiles(pidFilter); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
137 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
138 |
// Add all unique /proc/{pid}/root/tmp dirs that are not mapped to /tmp |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
139 |
for (File dir : dirs) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
140 |
String containerTmpDir = dir.getAbsolutePath() + containerTmpPath; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
141 |
File containerFile = new File(containerTmpDir); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
142 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
143 |
try { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
144 |
long procInode = (Long)Files.getAttribute(containerFile.toPath(), "unix:ino"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
145 |
if (containerFile.exists() && containerFile.isDirectory() && |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
146 |
containerFile.canRead() && procInode != tmpInode) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
147 |
v.add(containerTmpDir); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
148 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
149 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
150 |
catch (IOException e) {} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
151 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
152 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
153 |
return v; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
154 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
155 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
156 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
157 |
/* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
158 |
* Extract either the host PID or the NameSpace PID |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
159 |
* from a file path. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
160 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
161 |
* File path should be in 1 of these 2 forms: |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
162 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
163 |
* /proc/{pid}/root/tmp/hsperfdata_{user}/{nspid} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
164 |
* or |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
165 |
* /tmp/hsperfdata_{user}/{pid} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
166 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
167 |
* In either case we want to return {pid} and NOT {nspid} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
168 |
* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
169 |
* This function filters out host pids which do not have |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
170 |
* associated hsperfdata files. This is due to the fact that |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
171 |
* getTemporaryDirectories will return /proc/{pid}/root/tmp |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
172 |
* paths for all container processes whether they are java |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
173 |
* processes or not causing duplicate matches. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
174 |
*/ |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
175 |
public int getLocalVmId(File file) throws NumberFormatException { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
176 |
String p = file.getAbsolutePath(); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
177 |
String s[] = p.split("\\/"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
178 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
179 |
// Determine if this file is from a container |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
180 |
if (s.length == 7 && s[1].equals("proc")) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
181 |
int hostpid = Integer.parseInt(s[2]); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
182 |
int nspid = Integer.parseInt(s[6]); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
183 |
if (nspid == hostpid || nspid == getNamespaceVmId(hostpid)) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
184 |
return hostpid; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
185 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
186 |
else { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
187 |
return -1; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
188 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
189 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
190 |
else { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
191 |
return Integer.parseInt(file.getName()); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
192 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
193 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
194 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
195 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
196 |
/* |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
197 |
* Return the inner most namespaced PID if there is one, |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
198 |
* otherwise return the original PID. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
199 |
*/ |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
200 |
public int getNamespaceVmId(int pid) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
201 |
// Assuming a real procfs sits beneath, reading this doesn't block |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
202 |
// nor will it consume a lot of memory. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
203 |
Path statusPath = Paths.get("/proc", Integer.toString(pid), "status"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
204 |
if (Files.notExists(statusPath)) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
205 |
return pid; // Likely a bad pid, but this is properly handled later. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
206 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
207 |
|
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
208 |
try { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
209 |
for (String line : Files.readAllLines(statusPath, StandardCharsets.UTF_8)) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
210 |
String[] parts = line.split(":"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
211 |
if (parts.length == 2 && parts[0].trim().equals("NSpid")) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
212 |
parts = parts[1].trim().split("\\s+"); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
213 |
// The last entry represents the PID the JVM "thinks" it is. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
214 |
// Even in non-namespaced pids these entries should be |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
215 |
// valid. You could refer to it as the inner most pid. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
216 |
int ns_pid = Integer.parseInt(parts[parts.length - 1]); |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
217 |
return ns_pid; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
218 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
219 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
220 |
// Old kernels may not have NSpid field (i.e. 3.10). |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
221 |
// Fallback to original pid in the event we cannot deduce. |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
222 |
return pid; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
223 |
} catch (NumberFormatException | IOException x) { |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
224 |
return pid; |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
225 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
226 |
} |
099f18e10557
8193710: jcmd -l and jps commands do not list Java processes running in Docker containers
bobv
parents:
diff
changeset
|
227 |
} |