0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.util.regex.*;
0N/Aimport java.util.*;
0N/Aimport java.net.URISyntaxException;
0N/Aimport java.io.IOException;
0N/Aimport sun.jvmstat.monitor.*;
0N/Aimport sun.jvmstat.monitor.event.*;
0N/A
0N/Apublic class MonitorVmStartTerminate {
0N/A
0N/A private static final int SLEEPERS = 10;
0N/A private static final int SLEEPTIME = 5000; // sleep time for a sleeper
0N/A private static final int EXECINTERVAL = 3000; // wait time between exec's
0N/A private static final int JOINTIME = (SLEEPERS * EXECINTERVAL)
0N/A + SLEEPTIME * 2;
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A long now = System.currentTimeMillis();
0N/A
0N/A String sleeperArgs = SLEEPTIME + " " + now;
0N/A String sleeperPattern = "Sleeper " + sleeperArgs + " \\d+$";
0N/A
0N/A MonitoredHost host = MonitoredHost.getMonitoredHost("localhost");
0N/A host.setInterval(200);
0N/A
0N/A SleeperListener listener = new SleeperListener(host, sleeperPattern);
0N/A host.addHostListener(listener);
0N/A
0N/A SleeperStarter ss = new SleeperStarter(SLEEPERS, EXECINTERVAL,
0N/A sleeperArgs);
0N/A ss.start();
0N/A
0N/A System.out.println("Waiting for "
0N/A + SLEEPERS + " sleepers to terminate");
0N/A try {
0N/A ss.join(JOINTIME);
0N/A } catch (InterruptedException e) {
0N/A System.err.println("Timed out waiting for sleepers");
0N/A }
0N/A
0N/A if (listener.getStarted() != SLEEPERS) {
0N/A throw new RuntimeException(
0N/A "Too few sleepers started: "
0N/A + " started = " + listener.getStarted()
0N/A + " SLEEPERS = " + SLEEPERS);
0N/A }
0N/A
0N/A if (listener.getStarted() != listener.getTerminated()) {
0N/A throw new RuntimeException(
0N/A "Started count != terminated count: "
0N/A + " started = " + listener.getStarted()
0N/A + " terminated = " + listener.getTerminated());
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass SleeperListener implements HostListener {
0N/A private static final boolean DEBUG = false;
0N/A
0N/A int started;
0N/A int terminated;
0N/A MonitoredHost host;
0N/A Matcher patternMatcher;
0N/A ArrayList targets;
0N/A
0N/A public SleeperListener(MonitoredHost host, String sleeperPattern) {
0N/A this.host = host;
0N/A Pattern pattern = Pattern.compile(sleeperPattern);
0N/A patternMatcher = pattern.matcher("");
0N/A targets = new ArrayList();
0N/A }
0N/A
0N/A private void printList(Iterator i, String msg) {
0N/A System.out.println(msg + ":");
0N/A while (i.hasNext()) {
0N/A Integer lvmid = (Integer)i.next();
0N/A try {
0N/A VmIdentifier vmid = new VmIdentifier("//" + lvmid.intValue());
0N/A MonitoredVm target = host.getMonitoredVm(vmid);
0N/A
0N/A StringMonitor cmdMonitor =
0N/A (StringMonitor)target.findByName("sun.rt.javaCommand");
0N/A String cmd = cmdMonitor.stringValue();
0N/A
0N/A System.out.println("\t" + lvmid.intValue() + ": "
0N/A + "\"" + cmd + "\"" + ": ");
0N/A } catch (URISyntaxException e) {
0N/A System.err.println("Unexpected URISyntaxException: "
0N/A + e.getMessage());
0N/A } catch (MonitorException e) {
0N/A System.out.println("\t" + lvmid.intValue()
0N/A + ": error reading monitoring data: "
0N/A + " target possibly terminated?");
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A private int addStarted(Iterator i) {
0N/A int found = 0;
0N/A while (i.hasNext()) {
0N/A try {
0N/A Integer lvmid = (Integer)i.next();
0N/A VmIdentifier vmid = new VmIdentifier("//" + lvmid.intValue());
0N/A MonitoredVm target = host.getMonitoredVm(vmid);
0N/A
0N/A StringMonitor cmdMonitor =
0N/A (StringMonitor)target.findByName("sun.rt.javaCommand");
0N/A String cmd = cmdMonitor.stringValue();
0N/A
0N/A patternMatcher.reset(cmd);
0N/A System.out.print("Started: " + lvmid.intValue()
0N/A + ": " + "\"" + cmd + "\"" + ": ");
0N/A
0N/A if (patternMatcher.matches()) {
0N/A System.out.println("matches pattern - recorded");
0N/A targets.add(lvmid);
0N/A found++;
0N/A }
0N/A else {
0N/A System.out.println("does not match pattern - ignored");
0N/A }
0N/A } catch (URISyntaxException e) {
0N/A System.err.println("Unexpected URISyntaxException: "
0N/A + e.getMessage());
0N/A } catch (MonitorException e) {
0N/A System.err.println("Unexpected MonitorException: "
0N/A + e.getMessage());
0N/A }
0N/A }
0N/A return found;
0N/A }
0N/A
0N/A private int removeTerminated(Iterator i) {
0N/A int found = 0;
0N/A while (i.hasNext()) {
0N/A Integer lvmid = (Integer)i.next();
0N/A /*
0N/A * we don't attempt to attach to the target here as it's
0N/A * now dead and has no jvmstat share memory file. Just see
0N/A * if the process id is among those that we saved when we
0N/A * started the targets (note - duplicated allowed and somewhat
0N/A * expected on windows);
0N/A */
0N/A System.out.print("Terminated: " + lvmid.intValue() + ": ");
0N/A if (targets.contains(lvmid)) {
0N/A System.out.println("matches pattern - termination recorded");
0N/A targets.remove(lvmid);
0N/A found++;
0N/A }
0N/A else {
0N/A System.out.println("does not match pattern - ignored");
0N/A }
0N/A }
0N/A return found;
0N/A }
0N/A
0N/A public synchronized int getStarted() {
0N/A return started;
0N/A }
0N/A
0N/A public synchronized int getTerminated() {
0N/A return terminated;
0N/A }
0N/A
0N/A public void vmStatusChanged(VmStatusChangeEvent ev) {
0N/A if (DEBUG) {
0N/A printList(ev.getActive().iterator(), "Active");
0N/A printList(ev.getStarted().iterator(), "Started");
0N/A printList(ev.getTerminated().iterator(), "Terminated");
0N/A }
0N/A
0N/A int recentlyStarted = addStarted(ev.getStarted().iterator());
0N/A int recentlyTerminated = removeTerminated(
0N/A ev.getTerminated().iterator());
0N/A
0N/A synchronized (this) {
0N/A started += recentlyStarted;
0N/A terminated += recentlyTerminated;
0N/A }
0N/A }
0N/A
0N/A public void disconnected(HostEvent ev) {
0N/A }
0N/A}
0N/A
0N/Aclass SleeperStarter extends Thread {
0N/A
0N/A JavaProcess[] processes;
0N/A int execInterval;
0N/A String args;
0N/A
0N/A public SleeperStarter(int sleepers, int execInterval, String args) {
0N/A this.execInterval = execInterval;
0N/A this.args = args;
0N/A this.processes = new JavaProcess[sleepers];
0N/A }
0N/A
0N/A private synchronized int active() {
0N/A int active = processes.length;
0N/A for(int i = 0; i < processes.length; i++) {
0N/A try {
0N/A int exitValue = processes[i].exitValue();
0N/A active--;
0N/A } catch (IllegalThreadStateException e) {
0N/A // process hasn't exited yet
0N/A }
0N/A }
0N/A return active;
0N/A }
0N/A
0N/A public void run() {
0N/A System.out.println("Starting " + processes.length + " sleepers");
0N/A
0N/A String[] classpath = {
0N/A "-classpath",
0N/A System.getProperty("java.class.path")
0N/A };
0N/A
0N/A for (int i = 0; i < processes.length; i++) {
0N/A try {
0N/A System.out.println("Starting Sleeper " + i);
0N/A synchronized(this) {
0N/A processes[i] = new JavaProcess("Sleeper", args + " " + i);
0N/A processes[i].addOptions(classpath);
0N/A }
0N/A processes[i].start();
0N/A Thread.sleep(execInterval);
0N/A } catch (InterruptedException ignore) {
0N/A } catch (IOException e) {
0N/A System.err.println(
0N/A "IOException trying to start Sleeper " + i + ": "
0N/A + e.getMessage());
0N/A }
0N/A }
0N/A
0N/A // spin waiting for the processes to terminate
0N/A while (active() > 0) ;
0N/A
0N/A // give final termination event a change to propogate to
0N/A // the HostListener
0N/A try { Thread.sleep(2000); } catch (InterruptedException ignore) { }
0N/A }
0N/A}