PageRenderTime 61ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/PerfDataFile.java

https://bitbucket.org/psandoz/lambda-jdk-pipeline-patches
Java | 327 lines | 115 code | 31 blank | 181 comment | 37 complexity | a8ccf20105beebbc59c7e216c9d8a1c1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0
  1. /*
  2. * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package sun.jvmstat.perfdata.monitor.protocol.local;
  26. import java.io.File;
  27. import java.io.FilenameFilter;
  28. /**
  29. * Class to provide translations from the local Vm Identifier
  30. * name space into the file system name space and vice-versa.
  31. * <p>
  32. * Provides a factory for creating a File object to the backing
  33. * store file for instrumentation shared memory region for a JVM
  34. * identified by its Local Java Virtual Machine Identifier, or
  35. * <em>lvmid</em>.
  36. *
  37. * @author Brian Doherty
  38. * @since 1.5
  39. * @see java.io.File
  40. */
  41. public class PerfDataFile {
  42. private PerfDataFile() { };
  43. /**
  44. * The name of the of the system dependent temporary directory
  45. */
  46. public static final String tmpDirName;
  47. /**
  48. * The file name prefix for PerfData shared memory files.
  49. * <p>
  50. * This prefix must be kept in sync with the prefix used by the JVM.
  51. */
  52. public static final String dirNamePrefix = "hsperfdata_";
  53. /**
  54. * The directory name pattern for the user directories.
  55. */
  56. public static final String userDirNamePattern = "hsperfdata_\\S*";
  57. /**
  58. * The file name pattern for PerfData shared memory files.
  59. * <p>
  60. * This pattern must be kept in synch with the file name pattern
  61. * used by the 1.4.2 and later HotSpot JVM.
  62. */
  63. public static final String fileNamePattern = "^[0-9]+$";
  64. /**
  65. * The file name pattern for 1.4.1 PerfData shared memory files.
  66. * <p>
  67. * This pattern must be kept in synch with the file name pattern
  68. * used by the 1.4.1 HotSpot JVM.
  69. */
  70. public static final String tmpFileNamePattern =
  71. "^hsperfdata_[0-9]+(_[1-2]+)?$";
  72. /**
  73. * Get a File object for the instrumentation backing store file
  74. * for the JVM identified by the given local Vm Identifier.
  75. * <p>
  76. * This method looks for the most up to date backing store file for
  77. * the given <tt>lvmid</tt>. It will search all the user specific
  78. * directories in the temporary directory for the host operating
  79. * system, which may be influenced by platform specific environment
  80. * variables.
  81. *
  82. * @param lvmid the local Java Virtual Machine Identifier for the target
  83. * @return File - a File object to the backing store file for the named
  84. * shared memory region of the target JVM.
  85. * @see java.io.File
  86. * @see #getTempDirectory()
  87. */
  88. public static File getFile(int lvmid) {
  89. if (lvmid == 0) {
  90. /*
  91. * lvmid == 0 is used to indicate the current Java Virtual Machine.
  92. * If the SDK provided an API to get a unique Java Virtual Machine
  93. * identifier, then a filename could be constructed with that
  94. * identifier. In absence of such an api, return null.
  95. */
  96. return null;
  97. }
  98. /*
  99. * iterate over all files in all directories in tmpDirName that
  100. * match the file name patterns.
  101. */
  102. File tmpDir = new File(tmpDirName);
  103. String[] files = tmpDir.list(new FilenameFilter() {
  104. public boolean accept(File dir, String name) {
  105. if (!name.startsWith(dirNamePrefix)) {
  106. return false;
  107. }
  108. File candidate = new File(dir, name);
  109. return ((candidate.isDirectory() || candidate.isFile())
  110. && candidate.canRead());
  111. }
  112. });
  113. long newestTime = 0;
  114. File newest = null;
  115. for (int i = 0; i < files.length; i++) {
  116. File f = new File(tmpDirName + files[i]);
  117. File candidate = null;
  118. if (f.exists() && f.isDirectory()) {
  119. /*
  120. * found a directory matching the name patterns. This
  121. * is a 1.4.2 hsperfdata_<user> directory. Check for
  122. * file named <lvmid> in that directory
  123. */
  124. String name = Integer.toString(lvmid);
  125. candidate = new File(f.getName(), name);
  126. } else if (f.exists() && f.isFile()) {
  127. /*
  128. * found a file matching the name patterns. This
  129. * is a 1.4.1 hsperfdata_<lvmid> file.
  130. */
  131. candidate = f;
  132. } else {
  133. // unexpected - let conditional below filter this one out
  134. candidate = f;
  135. }
  136. if (candidate.exists() && candidate.isFile()
  137. && candidate.canRead()) {
  138. long modTime = candidate.lastModified();
  139. if (modTime >= newestTime) {
  140. newestTime = modTime;
  141. newest = candidate;
  142. }
  143. }
  144. }
  145. return newest;
  146. }
  147. /**
  148. * Return the File object for the backing store file for the specified Java
  149. * Virtual Machine.
  150. * <p>
  151. * This method looks for the most up to date backing store file for
  152. * the JVM identified by the given user name and lvmid. The directory
  153. * searched is the temporary directory for the host operating system,
  154. * which may be influenced by environment variables.
  155. *
  156. * @param user the user name
  157. * @param lvmid the local Java Virtual Machine Identifier for the target
  158. * @return File - a File object to the backing store file for the named
  159. * shared memory region of the target JVM.
  160. * @see java.io.File
  161. * @see #getTempDirectory()
  162. */
  163. public static File getFile(String user, int lvmid) {
  164. if (lvmid == 0) {
  165. /*
  166. * lvmid == 0 is used to indicate the current Java Virtual Machine.
  167. * If the SDK provided an API to get a unique Java Virtual Machine
  168. * identifier, then a filename could be constructed with that
  169. * identifier. In absence of such an api, return null.
  170. */
  171. return null;
  172. }
  173. // first try for 1.4.2 and later JVMs
  174. String basename = getTempDirectory(user) + Integer.toString(lvmid);
  175. File f = new File(basename);
  176. if (f.exists() && f.isFile() && f.canRead()) {
  177. return f;
  178. }
  179. // No hit on 1.4.2 JVMs, try 1.4.1 files
  180. long newestTime = 0;
  181. File newest = null;
  182. for (int i = 0; i < 2; i++) {
  183. if (i == 0) {
  184. basename = getTempDirectory() + Integer.toString(lvmid);
  185. } else {
  186. basename = getTempDirectory() + Integer.toString(lvmid)
  187. + Integer.toString(i);
  188. }
  189. f = new File(basename);
  190. if (f.exists() && f.isFile() && f.canRead()) {
  191. long modTime = f.lastModified();
  192. if (modTime >= newestTime) {
  193. newestTime = modTime;
  194. newest = f;
  195. }
  196. }
  197. }
  198. return newest;
  199. }
  200. /**
  201. * Method to extract a local Java Virtual Machine Identifier from the
  202. * file name of the given File object.
  203. *
  204. * @param file A File object representing the name of a
  205. * shared memory region for a target JVM
  206. * @return int - the local Java Virtual Machine Identifier for the target
  207. * associated with the file
  208. * @throws java.lang.IllegalArgumentException Thrown if the file name
  209. * does not conform to the expected pattern
  210. */
  211. public static int getLocalVmId(File file) {
  212. int lvmid = 0;
  213. try {
  214. // try 1.4.2 and later format first
  215. return Integer.parseInt(file.getName());
  216. } catch (NumberFormatException e) { }
  217. // now try the 1.4.1 format
  218. String name = file.getName();
  219. if (name.startsWith(dirNamePrefix)) {
  220. int first = name.indexOf('_');
  221. int last = name.lastIndexOf('_');
  222. try {
  223. if (first == last) {
  224. return Integer.parseInt(name.substring(first + 1));
  225. } else {
  226. return Integer.parseInt(name.substring(first + 1, last));
  227. }
  228. } catch (NumberFormatException e) { }
  229. }
  230. throw new IllegalArgumentException("file name does not match pattern");
  231. }
  232. /**
  233. * Return the name of the temporary directory being searched for
  234. * HotSpot PerfData backing store files.
  235. * <p>
  236. * This method generally returns the value of the java.io.tmpdir
  237. * property. However, on some platforms it may return a different
  238. * directory, as the JVM implementation may store the PerfData backing
  239. * store files in a different directory for performance reasons.
  240. *
  241. * @return String - the name of the temporary directory.
  242. */
  243. public static String getTempDirectory() {
  244. return tmpDirName;
  245. }
  246. /**
  247. * Return the name of the temporary directory to be searched
  248. * for HotSpot PerfData backing store files for a given user.
  249. * <p>
  250. * This method generally returns the name of a subdirectory of
  251. * the directory indicated in the java.io.tmpdir property. However,
  252. * on some platforms it may return a different directory, as the
  253. * JVM implementation may store the PerfData backing store files
  254. * in a different directory for performance reasons.
  255. *
  256. * @return String - the name of the temporary directory.
  257. */
  258. public static String getTempDirectory(String user) {
  259. return tmpDirName + dirNamePrefix + user + File.separator;
  260. }
  261. /*
  262. * this static initializer would not be necessary if the
  263. * Solaris java.io.tmpdir property were set to /tmp by default
  264. */
  265. static {
  266. /*
  267. * Why is java.io.tmpdir on Solaris set to "/var/tmp/" when the
  268. * HotSpot JVM os:get_temp_path() method returns "/tmp/"
  269. *
  270. * Why do Solaris and Windows return a string with a trailing
  271. * file separator character where as Linix does not? (this change
  272. * seems to have occurred sometime during hopper beta)
  273. */
  274. String tmpdir = System.getProperty("java.io.tmpdir");
  275. if (tmpdir.compareTo("/var/tmp/") == 0) {
  276. /*
  277. * shared memory files are created in /tmp. Interestingly,
  278. * java.io.tmpdir is set to "/var/tmp/" on Solaris and Linux,
  279. * but os::get_temp_directory() is set to "/tmp/" on these
  280. * platforms. the java.io.logging packages also makes reference
  281. * to java.io.tmpdir.
  282. */
  283. tmpdir = "/tmp/";
  284. }
  285. /*
  286. * Assure that the string returned has a trailing File.separator
  287. * character. This check was added because the Linux implementation
  288. * changed such that the java.io.tmpdir string no longer terminates
  289. * with a File.separator character.
  290. */
  291. if (tmpdir.lastIndexOf(File.separator) != (tmpdir.length()-1)) {
  292. tmpdir = tmpdir + File.separator;
  293. }
  294. tmpDirName = tmpdir;
  295. }
  296. }