jdk/src/jdk.jvmstat/share/classes/sun/jvmstat/perfdata/monitor/protocol/local/LocalVmManager.java
author chegar
Sun, 03 Apr 2016 16:28:41 +0100
changeset 36854 84179cb88469
parent 25859 3317bb8137f4
permissions -rw-r--r--
8153181: Move sun.misc.VMSupport to an internal package Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.jvmstat.perfdata.monitor.protocol.local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.jvmstat.monitor.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Class for managing the LocalMonitoredVm instances on the local system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is responsible for the mechanism that detects the active
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * HotSpot Java Virtual Machines on the local host and possibly for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * specific user. The ability to detect all possible HotSpot Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Machines on the local host may be limited by the permissions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * principal running this JVM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class LocalVmManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private String userName;                 // user name for monitored jvm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private File tmpdir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Pattern userPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private Matcher userMatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private FilenameFilter userFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private Pattern filePattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private Matcher fileMatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private FilenameFilter fileFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Pattern tmpFilePattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Matcher tmpFileMatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private FilenameFilter tmpFileFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates a LocalVmManager instance for the local system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Manages LocalMonitoredVm instances for which the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * has appropriate permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public LocalVmManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a LocalVmManager instance for the given user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Manages LocalMonitoredVm instances for all JVMs owned by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param user the name of the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public LocalVmManager(String user) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.userName = user;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (userName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            tmpdir = new File(PerfDataFile.getTempDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            userPattern = Pattern.compile(PerfDataFile.userDirNamePattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            userMatcher = userPattern.matcher("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            userFilter = new FilenameFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                public boolean accept(File dir, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    userMatcher.reset(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    return userMatcher.lookingAt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            tmpdir = new File(PerfDataFile.getTempDirectory(userName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        filePattern = Pattern.compile(PerfDataFile.fileNamePattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        fileMatcher = filePattern.matcher("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        fileFilter = new FilenameFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            public boolean accept(File dir, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                fileMatcher.reset(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                return fileMatcher.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        tmpFilePattern = Pattern.compile(PerfDataFile.tmpFileNamePattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        tmpFileMatcher = tmpFilePattern.matcher("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        tmpFileFilter = new FilenameFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            public boolean accept(File dir, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                tmpFileMatcher.reset(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return tmpFileMatcher.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Return the current set of monitorable Java Virtual Machines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The set returned by this method depends on the user name passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * to the constructor. If no user name was specified, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * method will return all candidate JVMs on the system. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * only the JVMs for the given user will be returned. This assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * that principal associated with this JVM has the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * permissions to access the target set of JVMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @return Set - the Set of monitorable Java Virtual Machines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public synchronized Set<Integer> activeVms() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * This method is synchronized because the Matcher object used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * fileFilter is not safe for concurrent use, and this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * called by multiple threads. Before this method was synchronized,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * we'd see strange file names being matched by the matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Set<Integer> jvmSet = new HashSet<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (! tmpdir.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return jvmSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (userName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
             * get a list of all of the user temporary directories and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
             * iterate over the list to find any files within those directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            File[] dirs = tmpdir.listFiles(userFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            for (int i = 0 ; i < dirs.length; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (!dirs[i].isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // get a list of files from the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                File[] files = dirs[i].listFiles(fileFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (files != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    for (int j = 0; j < files.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        if (files[j].isFile() && files[j].canRead()) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   159
                            jvmSet.add(
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   160
                                    PerfDataFile.getLocalVmId(files[j]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
             * Check if the user directory can be accessed. Any of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
             * conditions may have asynchronously changed between subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
             * calls to this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // get the list of files from the specified user directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            File[] files = tmpdir.listFiles(fileFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (files != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                for (int j = 0; j < files.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    if (files[j].isFile() && files[j].canRead()) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   178
                        jvmSet.add(
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   179
                                PerfDataFile.getLocalVmId(files[j]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // look for any 1.4.1 files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        File[] files = tmpdir.listFiles(tmpFileFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (files != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            for (int j = 0; j < files.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (files[j].isFile() && files[j].canRead()) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   190
                    jvmSet.add(
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 5506
diff changeset
   191
                            PerfDataFile.getLocalVmId(files[j]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return jvmSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}