35
35
* Class for managing the LocalMonitoredVm instances on the local system.
36
36
* <p>
37
37
* 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
40
40
* Machines on the local host may be limited by the permissions of the
41
- * principal running this JVM.
41
+ * current user running this JVM.
42
42
*
43
43
* @author Brian Doherty
44
44
* @since 1.5
45
45
*/
46
46
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 ;
57
50
58
51
/**
59
52
* Creates a LocalVmManager instance for the local system.
60
53
* <p>
61
- * Manages LocalMonitoredVm instances for which the principal
54
+ * Manages LocalMonitoredVm instances for which the current user
62
55
* has appropriate permissions.
63
56
*/
64
57
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 () {
95
61
public boolean accept (File dir , String name ) {
96
- fileMatcher .reset (name );
97
- return fileMatcher .matches ();
62
+ return userDirPattern .matcher (name ).lookingAt ();
98
63
}
99
64
};
100
65
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
+ };
103
72
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 () {
105
77
public boolean accept (File dir , String name ) {
106
- tmpFileMatcher .reset (name );
107
- return tmpFileMatcher .matches ();
78
+ return oldtmpFilePattern .matcher (name ).matches ();
108
79
}
109
80
};
110
81
}
111
82
112
83
/**
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.
121
86
*
122
87
* @return Set - the Set of monitorable Java Virtual Machines
123
88
*/
124
89
public synchronized Set <Integer > activeVms () {
125
90
/*
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.
130
94
*/
131
95
Set <Integer > jvmSet = new HashSet <Integer >();
132
- List <String > tmpdirs = PerfDataFile .getTempDirectories (userName , 0 );
96
+ List <String > tmpdirs = PerfDataFile .getTempDirectories (0 );
133
97
134
98
for (String dir : tmpdirs ) {
135
99
File tmpdir = new File (dir );
136
100
if (! tmpdir .isDirectory ()) {
137
101
continue ;
138
102
}
139
103
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
- }
150
104
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 ;
163
111
}
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 );
173
112
113
+ // get a list of files from the directory
114
+ File [] files = dirs [i ].listFiles (userDirFileFilter );
174
115
if (files != null ) {
175
116
for (int j = 0 ; j < files .length ; j ++) {
176
117
if (files [j ].isFile () && files [j ].canRead ()) {
@@ -183,8 +124,8 @@ public synchronized Set<Integer> activeVms() {
183
124
}
184
125
}
185
126
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 );
188
129
if (files != null ) {
189
130
for (int j = 0 ; j < files .length ; j ++) {
190
131
if (files [j ].isFile () && files [j ].canRead ()) {
1 commit comments
openjdk-notifier[bot] commentedon Nov 5, 2021
Review
Issues