Path: blob/master/src/jdk.internal.jvmstat/share/classes/sun/jvmstat/PlatformSupport.java
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.jvmstat;2526import java.io.File;27import java.lang.reflect.Constructor;28import java.util.List;29import jdk.internal.vm.VMSupport;3031/*32* Support routines handling temp directory locating33* and process ID extraction.34*/35public class PlatformSupport {36private static final String tmpDirName;37static {38/*39* For this to work, the target VM and this code need to use40* the same directory. Instead of guessing which directory the41* VM is using, we will ask.42*/43String tmpdir = VMSupport.getVMTemporaryDirectory();4445/*46* Assure that the string returned has a trailing File.separator47* character. This check was added because the Linux implementation48* changed such that the java.io.tmpdir string no longer terminates49* with a File.separator character.50*/51if (tmpdir.lastIndexOf(File.separator) != (tmpdir.length()-1)) {52tmpdir = tmpdir + File.separator;53}54tmpDirName = tmpdir;55}5657public static PlatformSupport getInstance() {58try {59Class<?> c = Class.forName("sun.jvmstat.PlatformSupportImpl");60@SuppressWarnings("unchecked")61Constructor<PlatformSupport> cntr = (Constructor<PlatformSupport>) c.getConstructor();62return cntr.newInstance();63} catch (ClassNotFoundException e) {64return new PlatformSupport();65} catch (ReflectiveOperationException e) {66throw new InternalError(e);67}68}6970// package-private71PlatformSupport() {}7273/*74* Return the OS specific temporary directory75*/76public static String getTemporaryDirectory() {77return tmpDirName;78}7980/*81* Return a list of the temporary directories that the VM uses82* for the attach and perf data files. This function returns83* the traditional temp directory in addition to any paths84* accessible by the host which map to temp directories used85* by containers. The container functionality is only currently86* supported on Linux platforms.87*88* It is important that this directory is well-known and the89* same for all VM instances. It cannot be affected by configuration90* variables such as java.io.tmpdir.91*/92public List<String> getTemporaryDirectories(int vmid) {93// Add the default temporary directory only94return List.of(tmpDirName);95}9697/*98* Extract the host PID from a file path.99*/100public int getLocalVmId(File file) throws NumberFormatException {101return Integer.parseInt(file.getName());102}103104105/*106* Return the inner most namespaced PID if there is one,107* otherwise return the original PID.108*/109public int getNamespaceVmId(int pid) {110return pid;111}112}113114115