1
1
package datadog .trace .util ;
2
2
3
3
import datadog .trace .api .Platform ;
4
- import datadog .trace .bootstrap .instrumentation .api .AgentTracer ;
5
- import datadog .trace .context .TraceScope ;
6
4
import de .thetaphi .forbiddenapis .SuppressForbidden ;
7
- import java .io .BufferedReader ;
8
5
import java .io .IOException ;
9
- import java .io .InputStreamReader ;
10
6
import java .lang .management .ManagementFactory ;
11
7
import java .nio .file .Files ;
12
8
import java .nio .file .Path ;
13
9
import java .nio .file .Paths ;
14
10
import java .util .Collections ;
15
11
import java .util .Set ;
16
- import java .util .concurrent .CompletableFuture ;
17
- import java .util .concurrent .TimeUnit ;
18
12
import java .util .function .Supplier ;
19
13
import java .util .stream .Collectors ;
20
14
import java .util .stream .Stream ;
@@ -39,6 +33,7 @@ public static long getPidAsLong() {
39
33
return PID_AS_LONG ;
40
34
}
41
35
36
+ @ SuppressWarnings ("unchecked" )
42
37
private static String findPid () {
43
38
String pid = "" ;
44
39
if (Platform .isJavaVersionAtLeast (9 )) {
@@ -123,14 +118,6 @@ private static Path getJavaProcessesDir() {
123
118
}
124
119
125
120
public static Set <String > getJavaPids () {
126
- // Attempt to use jvmstat directly, fall through to jps process fork strategy
127
- Set <String > directlyObtainedPids = JPSUtils .getVMPids ();
128
- if (directlyObtainedPids != null ) {
129
- return directlyObtainedPids ;
130
- }
131
-
132
- // Some JDKs don't have jvmstat available as a module, attempt to read from the hsperfdata
133
- // directory instead
134
121
try (Stream <Path > stream = Files .list (getJavaProcessesDir ())) {
135
122
return stream
136
123
.map (Path ::getFileName )
@@ -145,56 +132,22 @@ public static Set<String> getJavaPids() {
145
132
if (name .isEmpty ()) {
146
133
return false ;
147
134
}
148
- for (int i = 0 ; i < name .length (); i ++) {
149
- if (!Character .isDigit (name .charAt (i ))) {
150
- return false ;
151
- }
135
+ char c = name .charAt (0 );
136
+ if (c < '0' || c > '9' ) {
137
+ // Short-circuit - let's not parse as long something that is definitely not a long
138
+ // number
139
+ return false ;
140
+ }
141
+ long pid = -1 ;
142
+ try {
143
+ pid = Long .parseLong (name );
144
+ } catch (NumberFormatException ignored ) {
152
145
}
153
- return true ;
146
+ return pid != - 1 ;
154
147
})
155
148
.collect (Collectors .toSet ());
156
149
} catch (IOException e ) {
157
- log .debug ("Unable to obtain Java PIDs via hsperfdata" , e );
158
- }
159
-
160
- // there is no supported Java API to achieve this
161
- // one could use sun.jvmstat.monitor.MonitoredHost but it is an internal API and can go away at
162
- // any time -
163
- // also, no guarantee it will work with all JVMs
164
- ProcessBuilder pb = new ProcessBuilder ("jps" );
165
- try (TraceScope ignored = AgentTracer .get ().muteTracing ()) {
166
- Process p = pb .start ();
167
- // start draining the subcommand's pipes asynchronously to avoid flooding them
168
- CompletableFuture <Set <String >> collecting =
169
- CompletableFuture .supplyAsync (
170
- () -> {
171
- try (BufferedReader br =
172
- new BufferedReader (new InputStreamReader (p .getInputStream ()))) {
173
- return br .lines ()
174
- .filter (l -> !l .contains ("jps" ))
175
- .map (
176
- l -> {
177
- int idx = l .indexOf (' ' );
178
- return l .substring (0 , idx );
179
- })
180
- .collect (java .util .stream .Collectors .toSet ());
181
- } catch (IOException e ) {
182
- log .debug ("Unable to list java processes via 'jps'" , e );
183
- return Collections .emptySet ();
184
- }
185
- });
186
- if (p .waitFor (1200 , TimeUnit .MILLISECONDS )) {
187
- if (p .exitValue () == 0 ) {
188
- return collecting .get ();
189
- } else {
190
- log .debug ("Execution of 'jps' failed with exit code {}" , p .exitValue ());
191
- }
192
- } else {
193
- p .destroyForcibly ();
194
- log .debug ("Execution of 'jps' timed out" );
195
- }
196
- } catch (Exception e ) {
197
- log .debug ("Unable to list java processes via 'jps'" , e );
150
+ log .debug ("Unable to obtain Java PIDs" , e );
198
151
}
199
152
return Collections .emptySet ();
200
153
}
0 commit comments