8275185: Remove dead code and clean up jvmstat LocalVmManager

Reviewed-by: cjplummer, redestad, kevinw
This commit is contained in:
Ioi Lam 2021-11-05 04:37:01 +00:00
parent 396132ff1e
commit 8e17ce0031
2 changed files with 42 additions and 288 deletions
src/jdk.internal.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local

@ -35,101 +35,65 @@ import java.io.*;
* Class for managing the LocalMonitoredVm instances on the local system.
* <p>
* This class is responsible for the mechanism that detects the active
* HotSpot Java Virtual Machines on the local host and possibly for a
* specific user. The ability to detect all possible HotSpot Java Virtual
* HotSpot Java Virtual Machines on the local host that can be accessed
* by the current user. The ability to detect all possible HotSpot Java Virtual
* Machines on the local host may be limited by the permissions of the
* principal running this JVM.
* current user running this JVM.
*
* @author Brian Doherty
* @since 1.5
*/
public class LocalVmManager {
private String userName; // user name for monitored jvm
private Pattern userPattern;
private Matcher userMatcher;
private FilenameFilter userFilter;
private Pattern filePattern;
private Matcher fileMatcher;
private FilenameFilter fileFilter;
private Pattern tmpFilePattern;
private Matcher tmpFileMatcher;
private FilenameFilter tmpFileFilter;
private FilenameFilter userDirFilter;
private FilenameFilter userDirFileFilter;
private FilenameFilter oldtmpFileFilter;
/**
* Creates a LocalVmManager instance for the local system.
* <p>
* Manages LocalMonitoredVm instances for which the principal
* Manages LocalMonitoredVm instances for which the current user
* has appropriate permissions.
*/
public LocalVmManager() {
this(null);
}
/**
* Creates a LocalVmManager instance for the given user.
* <p>
* Manages LocalMonitoredVm instances for all JVMs owned by the specified
* user.
*
* @param user the name of the user
*/
public LocalVmManager(String user) {
this.userName = user;
if (userName == null) {
userPattern = Pattern.compile(PerfDataFile.userDirNamePattern);
userMatcher = userPattern.matcher("");
userFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
userMatcher.reset(name);
return userMatcher.lookingAt();
}
};
}
filePattern = Pattern.compile(PerfDataFile.fileNamePattern);
fileMatcher = filePattern.matcher("");
fileFilter = new FilenameFilter() {
// 1.4.2 and later: The files are in {tmpdir}/hsperfdata_{any_user_name}/[0-9]+
Pattern userDirPattern = Pattern.compile(PerfDataFile.userDirNamePattern);
userDirFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
fileMatcher.reset(name);
return fileMatcher.matches();
return userDirPattern.matcher(name).lookingAt();
}
};
tmpFilePattern = Pattern.compile(PerfDataFile.tmpFileNamePattern);
tmpFileMatcher = tmpFilePattern.matcher("");
tmpFileFilter = new FilenameFilter() {
Pattern userDirFilePattern = Pattern.compile(PerfDataFile.fileNamePattern);
userDirFileFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
tmpFileMatcher.reset(name);
return tmpFileMatcher.matches();
return userDirFilePattern.matcher(name).matches();
}
};
// 1.4.1 (or earlier?): the files are stored directly under {tmpdir}/ with
// the following pattern.
Pattern oldtmpFilePattern = Pattern.compile(PerfDataFile.tmpFileNamePattern);
oldtmpFileFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return oldtmpFilePattern.matcher(name).matches();
}
};
}
/**
* Return the current set of monitorable Java Virtual Machines.
* <p>
* The set returned by this method depends on the user name passed
* to the constructor. If no user name was specified, then this
* method will return all candidate JVMs on the system. Otherwise,
* only the JVMs for the given user will be returned. This assumes
* that principal associated with this JVM has the appropriate
* permissions to access the target set of JVMs.
* Return the current set of monitorable Java Virtual Machines that
* are accessible by the current user.
*
* @return Set - the Set of monitorable Java Virtual Machines
*/
public synchronized Set<Integer> activeVms() {
/*
* This method is synchronized because the Matcher object used by
* fileFilter is not safe for concurrent use, and this method is
* called by multiple threads. Before this method was synchronized,
* we'd see strange file names being matched by the matcher.
* TODO: this method was synchronized due to its thread-unsafe use of the regexp
* Matcher objects. That is not the case anymore, but I am too afraid to change
* it now. Maybe fix this later in a separate RFE.
*/
Set<Integer> jvmSet = new HashSet<Integer>();
List<String> tmpdirs = PerfDataFile.getTempDirectories(userName, 0);
List<String> tmpdirs = PerfDataFile.getTempDirectories(0);
for (String dir : tmpdirs) {
File tmpdir = new File(dir);
@ -137,40 +101,17 @@ public class LocalVmManager {
continue;
}
if (userName == null) {
/*
* get a list of all of the user temporary directories and
* iterate over the list to find any files within those directories.
*/
File[] dirs = tmpdir.listFiles(userFilter);
for (int i = 0 ; i < dirs.length; i ++) {
if (!dirs[i].isDirectory()) {
continue;
}
// get a list of files from the directory
File[] files = dirs[i].listFiles(fileFilter);
if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].isFile() && files[j].canRead()) {
int vmid = PerfDataFile.getLocalVmId(files[j]);
if (vmid != -1) {
jvmSet.add(vmid);
}
}
}
}
// 1.4.2 and later: Look for the files {tmpdir}/hsperfdata_{any_user_name}/[0-9]+
// that are readable by the current user.
File[] dirs = tmpdir.listFiles(userDirFilter);
for (int i = 0 ; i < dirs.length; i ++) {
if (!dirs[i].isDirectory()) {
continue;
}
} else {
/*
* Check if the user directory can be accessed. Any of these
* conditions may have asynchronously changed between subsequent
* calls to this method.
*/
// get the list of files from the specified user directory
File[] files = tmpdir.listFiles(fileFilter);
// get a list of files from the directory
File[] files = dirs[i].listFiles(userDirFileFilter);
if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].isFile() && files[j].canRead()) {
@ -183,8 +124,8 @@ public class LocalVmManager {
}
}
// look for any 1.4.1 files
File[] files = tmpdir.listFiles(tmpFileFilter);
// look for any 1.4.1 files that are readable by the current user.
File[] files = tmpdir.listFiles(oldtmpFileFilter);
if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].isFile() && files[j].canRead()) {

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -84,168 +84,6 @@ public class PerfDataFile {
*/
private static final PlatformSupport platSupport = PlatformSupport.getInstance();
/**
* Get a File object for the instrumentation backing store file
* for the JVM identified by the given local Vm Identifier.
* <p>
* This method looks for the most up to date backing store file for
* the given {@code lvmid}. It will search all the user specific
* directories in the temporary directory for the host operating
* system, which may be influenced by platform specific environment
* variables.
*
* @param lvmid the local Java Virtual Machine Identifier for the target
* @return File - a File object to the backing store file for the named
* shared memory region of the target JVM.
* @see java.io.File
* @see #getTempDirectories()
*/
public static File getFile(int lvmid) {
if (lvmid == 0) {
/*
* lvmid == 0 is used to indicate the current Java Virtual Machine.
* If the SDK provided an API to get a unique Java Virtual Machine
* identifier, then a filename could be constructed with that
* identifier. In absence of such an api, return null.
*/
return null;
}
List<String> tmpDirs = getTempDirectories(null, lvmid);
File newest = null;
for (String dir : tmpDirs) {
/*
* iterate over all files in all directories in this tmpDir that
* match the file name patterns.
*/
File tmpDir = new File(dir);
String[] files = tmpDir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (!name.startsWith(dirNamePrefix)) {
return false;
}
File candidate = new File(dir, name);
return ((candidate.isDirectory() || candidate.isFile())
&& candidate.canRead());
}
});
long newestTime = 0;
for (String file : files) {
File f = new File(dir + file);
File candidate = null;
if (f.exists() && f.isDirectory()) {
/*
* found a directory matching the name patterns. This
* is a 1.4.2 hsperfdata_<user> directory. Check for
* file named <lvmid> in that directory
*/
String name = f.getAbsolutePath() + File.separator + lvmid;
candidate = new File(name);
// Try NameSpace Id if Host Id doesn't exist.
if (!candidate.exists()) {
name = f.getAbsolutePath() + File.separator +
platSupport.getNamespaceVmId(lvmid);
candidate = new File(name);
}
} else if (f.exists() && f.isFile()) {
/*
* found a file matching the name patterns. This
* is a 1.4.1 hsperfdata_<lvmid> file.
*/
candidate = f;
} else {
// unexpected - let conditional below filter this one out
candidate = f;
}
if (candidate.exists() && candidate.isFile()
&& candidate.canRead()) {
long modTime = candidate.lastModified();
if (modTime >= newestTime) {
newestTime = modTime;
newest = candidate;
}
}
}
}
return newest;
}
/**
* Return the File object for the backing store file for the specified Java
* Virtual Machine.
* <p>
* This method looks for the most up to date backing store file for
* the JVM identified by the given user name and lvmid. The directory
* searched is the temporary directory for the host operating system,
* which may be influenced by environment variables.
*
* @param user the user name
* @param lvmid the local Java Virtual Machine Identifier for the target
* @return File - a File object to the backing store file for the named
* shared memory region of the target JVM.
* @see java.io.File
* @see #getTempDirectories()
*/
public static File getFile(String user, int lvmid) {
if (lvmid == 0) {
/*
* lvmid == 0 is used to indicate the current Java Virtual Machine.
* If the SDK provided an API to get a unique Java Virtual Machine
* identifier, then a filename could be constructed with that
* identifier. In absence of such an api, return null.
*/
return null;
}
// first try for 1.4.2 and later JVMs
List<String> tmpDirs = getTempDirectories(user, lvmid);
String basename;
File f;
for (String dir : tmpDirs) {
basename = dir + lvmid;
f = new File(basename);
if (f.exists() && f.isFile() && f.canRead()) {
return f;
}
// Try NameSpace Id if Host Id doesn't exist.
basename = dir + platSupport.getNamespaceVmId(lvmid);
f = new File(basename);
if (f.exists() && f.isFile() && f.canRead()) {
return f;
}
}
// No hit on 1.4.2 JVMs, try 1.4.1 files
long newestTime = 0;
File newest = null;
for (int i = 0; i < 2; i++) {
if (i == 0) {
basename = getTempDirectory() + Integer.toString(lvmid);
} else {
basename = getTempDirectory() + Integer.toString(lvmid)
+ Integer.toString(i);
}
f = new File(basename);
if (f.exists() && f.isFile() && f.canRead()) {
long modTime = f.lastModified();
if (modTime >= newestTime) {
newestTime = modTime;
newest = f;
}
}
}
return newest;
}
/**
* Method to extract a local Java Virtual Machine Identifier from the
* file name of the given File object.
@ -294,22 +132,6 @@ public class PerfDataFile {
return PlatformSupport.getTemporaryDirectory();
}
/**
* Return the name of the temporary directory to be searched
* for HotSpot PerfData backing store files for a given user.
* <p>
* This method generally returns the name of a subdirectory of
* the directory indicated in the java.io.tmpdir property. However,
* on some platforms it may return a different directory, as the
* JVM implementation may store the PerfData backing store files
* in a different directory for performance reasons.
*
* @return String - the name of the temporary directory.
*/
public static String getTempDirectory(String user) {
return getTempDirectory() + dirNamePrefix + user + File.separator;
}
/**
* Return the names of the temporary directories being searched for
* HotSpot PerfData backing store files.
@ -319,16 +141,7 @@ public class PerfDataFile {
*
* @return List<String> - A List of temporary directories to search.
*/
public static List<String> getTempDirectories(String userName, int vmid) {
List<String> list = platSupport.getTemporaryDirectories(vmid);
if (userName == null) {
return list;
}
List<String> nameList = list.stream()
.map(name -> name + dirNamePrefix + userName + File.separator)
.collect(Collectors.toList());
return nameList;
public static List<String> getTempDirectories(int vmid) {
return platSupport.getTemporaryDirectories(vmid);
}
}