Skip to content

Commit 8e17ce0

Browse files
committedNov 5, 2021
8275185: Remove dead code and clean up jvmstat LocalVmManager
Reviewed-by: cjplummer, redestad, kevinw
1 parent 396132f commit 8e17ce0

File tree

2 files changed

+41
-287
lines changed

2 files changed

+41
-287
lines changed
 

‎src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalVmManager.java

+38-97
Original file line numberDiff line numberDiff line change
@@ -35,142 +35,83 @@
3535
* Class for managing the LocalMonitoredVm instances on the local system.
3636
* <p>
3737
* This class is responsible for the mechanism that detects the active
38-
* HotSpot Java Virtual Machines on the local host and possibly for a
39-
* specific user. The ability to detect all possible HotSpot Java Virtual
38+
* HotSpot Java Virtual Machines on the local host that can be accessed
39+
* by the current user. The ability to detect all possible HotSpot Java Virtual
4040
* Machines on the local host may be limited by the permissions of the
41-
* principal running this JVM.
41+
* current user running this JVM.
4242
*
4343
* @author Brian Doherty
4444
* @since 1.5
4545
*/
4646
public class LocalVmManager {
47-
private String userName; // user name for monitored jvm
48-
private Pattern userPattern;
49-
private Matcher userMatcher;
50-
private FilenameFilter userFilter;
51-
private Pattern filePattern;
52-
private Matcher fileMatcher;
53-
private FilenameFilter fileFilter;
54-
private Pattern tmpFilePattern;
55-
private Matcher tmpFileMatcher;
56-
private FilenameFilter tmpFileFilter;
47+
private FilenameFilter userDirFilter;
48+
private FilenameFilter userDirFileFilter;
49+
private FilenameFilter oldtmpFileFilter;
5750

5851
/**
5952
* Creates a LocalVmManager instance for the local system.
6053
* <p>
61-
* Manages LocalMonitoredVm instances for which the principal
54+
* Manages LocalMonitoredVm instances for which the current user
6255
* has appropriate permissions.
6356
*/
6457
public LocalVmManager() {
65-
this(null);
66-
}
67-
68-
/**
69-
* Creates a LocalVmManager instance for the given user.
70-
* <p>
71-
* Manages LocalMonitoredVm instances for all JVMs owned by the specified
72-
* user.
73-
*
74-
* @param user the name of the user
75-
*/
76-
public LocalVmManager(String user) {
77-
this.userName = user;
78-
79-
if (userName == null) {
80-
userPattern = Pattern.compile(PerfDataFile.userDirNamePattern);
81-
userMatcher = userPattern.matcher("");
82-
83-
userFilter = new FilenameFilter() {
84-
public boolean accept(File dir, String name) {
85-
userMatcher.reset(name);
86-
return userMatcher.lookingAt();
87-
}
88-
};
89-
}
90-
91-
filePattern = Pattern.compile(PerfDataFile.fileNamePattern);
92-
fileMatcher = filePattern.matcher("");
93-
94-
fileFilter = new FilenameFilter() {
58+
// 1.4.2 and later: The files are in {tmpdir}/hsperfdata_{any_user_name}/[0-9]+
59+
Pattern userDirPattern = Pattern.compile(PerfDataFile.userDirNamePattern);
60+
userDirFilter = new FilenameFilter() {
9561
public boolean accept(File dir, String name) {
96-
fileMatcher.reset(name);
97-
return fileMatcher.matches();
62+
return userDirPattern.matcher(name).lookingAt();
9863
}
9964
};
10065

101-
tmpFilePattern = Pattern.compile(PerfDataFile.tmpFileNamePattern);
102-
tmpFileMatcher = tmpFilePattern.matcher("");
66+
Pattern userDirFilePattern = Pattern.compile(PerfDataFile.fileNamePattern);
67+
userDirFileFilter = new FilenameFilter() {
68+
public boolean accept(File dir, String name) {
69+
return userDirFilePattern.matcher(name).matches();
70+
}
71+
};
10372

104-
tmpFileFilter = new FilenameFilter() {
73+
// 1.4.1 (or earlier?): the files are stored directly under {tmpdir}/ with
74+
// the following pattern.
75+
Pattern oldtmpFilePattern = Pattern.compile(PerfDataFile.tmpFileNamePattern);
76+
oldtmpFileFilter = new FilenameFilter() {
10577
public boolean accept(File dir, String name) {
106-
tmpFileMatcher.reset(name);
107-
return tmpFileMatcher.matches();
78+
return oldtmpFilePattern.matcher(name).matches();
10879
}
10980
};
11081
}
11182

11283
/**
113-
* Return the current set of monitorable Java Virtual Machines.
114-
* <p>
115-
* The set returned by this method depends on the user name passed
116-
* to the constructor. If no user name was specified, then this
117-
* method will return all candidate JVMs on the system. Otherwise,
118-
* only the JVMs for the given user will be returned. This assumes
119-
* that principal associated with this JVM has the appropriate
120-
* permissions to access the target set of JVMs.
84+
* Return the current set of monitorable Java Virtual Machines that
85+
* are accessible by the current user.
12186
*
12287
* @return Set - the Set of monitorable Java Virtual Machines
12388
*/
12489
public synchronized Set<Integer> activeVms() {
12590
/*
126-
* This method is synchronized because the Matcher object used by
127-
* fileFilter is not safe for concurrent use, and this method is
128-
* called by multiple threads. Before this method was synchronized,
129-
* we'd see strange file names being matched by the matcher.
91+
* TODO: this method was synchronized due to its thread-unsafe use of the regexp
92+
* Matcher objects. That is not the case anymore, but I am too afraid to change
93+
* it now. Maybe fix this later in a separate RFE.
13094
*/
13195
Set<Integer> jvmSet = new HashSet<Integer>();
132-
List<String> tmpdirs = PerfDataFile.getTempDirectories(userName, 0);
96+
List<String> tmpdirs = PerfDataFile.getTempDirectories(0);
13397

13498
for (String dir : tmpdirs) {
13599
File tmpdir = new File(dir);
136100
if (! tmpdir.isDirectory()) {
137101
continue;
138102
}
139103

140-
if (userName == null) {
141-
/*
142-
* get a list of all of the user temporary directories and
143-
* iterate over the list to find any files within those directories.
144-
*/
145-
File[] dirs = tmpdir.listFiles(userFilter);
146-
for (int i = 0 ; i < dirs.length; i ++) {
147-
if (!dirs[i].isDirectory()) {
148-
continue;
149-
}
150104

151-
// get a list of files from the directory
152-
File[] files = dirs[i].listFiles(fileFilter);
153-
if (files != null) {
154-
for (int j = 0; j < files.length; j++) {
155-
if (files[j].isFile() && files[j].canRead()) {
156-
int vmid = PerfDataFile.getLocalVmId(files[j]);
157-
if (vmid != -1) {
158-
jvmSet.add(vmid);
159-
}
160-
}
161-
}
162-
}
105+
// 1.4.2 and later: Look for the files {tmpdir}/hsperfdata_{any_user_name}/[0-9]+
106+
// that are readable by the current user.
107+
File[] dirs = tmpdir.listFiles(userDirFilter);
108+
for (int i = 0 ; i < dirs.length; i ++) {
109+
if (!dirs[i].isDirectory()) {
110+
continue;
163111
}
164-
} else {
165-
/*
166-
* Check if the user directory can be accessed. Any of these
167-
* conditions may have asynchronously changed between subsequent
168-
* calls to this method.
169-
*/
170-
171-
// get the list of files from the specified user directory
172-
File[] files = tmpdir.listFiles(fileFilter);
173112

113+
// get a list of files from the directory
114+
File[] files = dirs[i].listFiles(userDirFileFilter);
174115
if (files != null) {
175116
for (int j = 0; j < files.length; j++) {
176117
if (files[j].isFile() && files[j].canRead()) {
@@ -183,8 +124,8 @@ public synchronized Set<Integer> activeVms() {
183124
}
184125
}
185126

186-
// look for any 1.4.1 files
187-
File[] files = tmpdir.listFiles(tmpFileFilter);
127+
// look for any 1.4.1 files that are readable by the current user.
128+
File[] files = tmpdir.listFiles(oldtmpFileFilter);
188129
if (files != null) {
189130
for (int j = 0; j < files.length; j++) {
190131
if (files[j].isFile() && files[j].canRead()) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -84,168 +84,6 @@ public class PerfDataFile {
8484
*/
8585
private static final PlatformSupport platSupport = PlatformSupport.getInstance();
8686

87-
/**
88-
* Get a File object for the instrumentation backing store file
89-
* for the JVM identified by the given local Vm Identifier.
90-
* <p>
91-
* This method looks for the most up to date backing store file for
92-
* the given {@code lvmid}. It will search all the user specific
93-
* directories in the temporary directory for the host operating
94-
* system, which may be influenced by platform specific environment
95-
* variables.
96-
*
97-
* @param lvmid the local Java Virtual Machine Identifier for the target
98-
* @return File - a File object to the backing store file for the named
99-
* shared memory region of the target JVM.
100-
* @see java.io.File
101-
* @see #getTempDirectories()
102-
*/
103-
public static File getFile(int lvmid) {
104-
if (lvmid == 0) {
105-
/*
106-
* lvmid == 0 is used to indicate the current Java Virtual Machine.
107-
* If the SDK provided an API to get a unique Java Virtual Machine
108-
* identifier, then a filename could be constructed with that
109-
* identifier. In absence of such an api, return null.
110-
*/
111-
return null;
112-
}
113-
114-
List<String> tmpDirs = getTempDirectories(null, lvmid);
115-
File newest = null;
116-
117-
for (String dir : tmpDirs) {
118-
/*
119-
* iterate over all files in all directories in this tmpDir that
120-
* match the file name patterns.
121-
*/
122-
File tmpDir = new File(dir);
123-
String[] files = tmpDir.list(new FilenameFilter() {
124-
public boolean accept(File dir, String name) {
125-
if (!name.startsWith(dirNamePrefix)) {
126-
return false;
127-
}
128-
File candidate = new File(dir, name);
129-
return ((candidate.isDirectory() || candidate.isFile())
130-
&& candidate.canRead());
131-
}
132-
});
133-
134-
long newestTime = 0;
135-
136-
for (String file : files) {
137-
File f = new File(dir + file);
138-
File candidate = null;
139-
140-
if (f.exists() && f.isDirectory()) {
141-
/*
142-
* found a directory matching the name patterns. This
143-
* is a 1.4.2 hsperfdata_<user> directory. Check for
144-
* file named <lvmid> in that directory
145-
*/
146-
String name = f.getAbsolutePath() + File.separator + lvmid;
147-
candidate = new File(name);
148-
// Try NameSpace Id if Host Id doesn't exist.
149-
if (!candidate.exists()) {
150-
name = f.getAbsolutePath() + File.separator +
151-
platSupport.getNamespaceVmId(lvmid);
152-
candidate = new File(name);
153-
}
154-
} else if (f.exists() && f.isFile()) {
155-
/*
156-
* found a file matching the name patterns. This
157-
* is a 1.4.1 hsperfdata_<lvmid> file.
158-
*/
159-
candidate = f;
160-
161-
} else {
162-
// unexpected - let conditional below filter this one out
163-
candidate = f;
164-
}
165-
166-
if (candidate.exists() && candidate.isFile()
167-
&& candidate.canRead()) {
168-
long modTime = candidate.lastModified();
169-
if (modTime >= newestTime) {
170-
newestTime = modTime;
171-
newest = candidate;
172-
}
173-
}
174-
}
175-
}
176-
return newest;
177-
}
178-
179-
/**
180-
* Return the File object for the backing store file for the specified Java
181-
* Virtual Machine.
182-
* <p>
183-
* This method looks for the most up to date backing store file for
184-
* the JVM identified by the given user name and lvmid. The directory
185-
* searched is the temporary directory for the host operating system,
186-
* which may be influenced by environment variables.
187-
*
188-
* @param user the user name
189-
* @param lvmid the local Java Virtual Machine Identifier for the target
190-
* @return File - a File object to the backing store file for the named
191-
* shared memory region of the target JVM.
192-
* @see java.io.File
193-
* @see #getTempDirectories()
194-
*/
195-
public static File getFile(String user, int lvmid) {
196-
if (lvmid == 0) {
197-
/*
198-
* lvmid == 0 is used to indicate the current Java Virtual Machine.
199-
* If the SDK provided an API to get a unique Java Virtual Machine
200-
* identifier, then a filename could be constructed with that
201-
* identifier. In absence of such an api, return null.
202-
*/
203-
return null;
204-
}
205-
206-
// first try for 1.4.2 and later JVMs
207-
List<String> tmpDirs = getTempDirectories(user, lvmid);
208-
String basename;
209-
File f;
210-
211-
for (String dir : tmpDirs) {
212-
basename = dir + lvmid;
213-
f = new File(basename);
214-
if (f.exists() && f.isFile() && f.canRead()) {
215-
return f;
216-
}
217-
// Try NameSpace Id if Host Id doesn't exist.
218-
basename = dir + platSupport.getNamespaceVmId(lvmid);
219-
f = new File(basename);
220-
if (f.exists() && f.isFile() && f.canRead()) {
221-
return f;
222-
}
223-
}
224-
225-
// No hit on 1.4.2 JVMs, try 1.4.1 files
226-
long newestTime = 0;
227-
File newest = null;
228-
for (int i = 0; i < 2; i++) {
229-
if (i == 0) {
230-
basename = getTempDirectory() + Integer.toString(lvmid);
231-
} else {
232-
basename = getTempDirectory() + Integer.toString(lvmid)
233-
+ Integer.toString(i);
234-
}
235-
236-
f = new File(basename);
237-
238-
if (f.exists() && f.isFile() && f.canRead()) {
239-
long modTime = f.lastModified();
240-
if (modTime >= newestTime) {
241-
newestTime = modTime;
242-
newest = f;
243-
}
244-
}
245-
}
246-
return newest;
247-
}
248-
24987
/**
25088
* Method to extract a local Java Virtual Machine Identifier from the
25189
* file name of the given File object.
@@ -294,22 +132,6 @@ public static String getTempDirectory() {
294132
return PlatformSupport.getTemporaryDirectory();
295133
}
296134

297-
/**
298-
* Return the name of the temporary directory to be searched
299-
* for HotSpot PerfData backing store files for a given user.
300-
* <p>
301-
* This method generally returns the name of a subdirectory of
302-
* the directory indicated in the java.io.tmpdir property. However,
303-
* on some platforms it may return a different directory, as the
304-
* JVM implementation may store the PerfData backing store files
305-
* in a different directory for performance reasons.
306-
*
307-
* @return String - the name of the temporary directory.
308-
*/
309-
public static String getTempDirectory(String user) {
310-
return getTempDirectory() + dirNamePrefix + user + File.separator;
311-
}
312-
313135
/**
314136
* Return the names of the temporary directories being searched for
315137
* HotSpot PerfData backing store files.
@@ -319,16 +141,7 @@ public static String getTempDirectory(String user) {
319141
*
320142
* @return List<String> - A List of temporary directories to search.
321143
*/
322-
public static List<String> getTempDirectories(String userName, int vmid) {
323-
List<String> list = platSupport.getTemporaryDirectories(vmid);
324-
if (userName == null) {
325-
return list;
326-
}
327-
328-
List<String> nameList = list.stream()
329-
.map(name -> name + dirNamePrefix + userName + File.separator)
330-
.collect(Collectors.toList());
331-
332-
return nameList;
144+
public static List<String> getTempDirectories(int vmid) {
145+
return platSupport.getTemporaryDirectories(vmid);
333146
}
334147
}

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Nov 5, 2021

@openjdk-notifier[bot]
Please sign in to comment.