jdk/test/sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java
author egahlin
Thu, 07 Aug 2014 15:16:35 +0200
changeset 25969 d474691a3e62
parent 25225 f161e8748e8d
child 27189 b90845965ee9
permissions -rw-r--r--
8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out Reviewed-by: jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
     2
 * Copyright (c) 2004, 2014, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    24
import java.io.File;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.IOException;
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    26
import java.net.URISyntaxException;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    27
import java.nio.file.Files;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    28
import java.nio.file.Path;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    29
import java.nio.file.Paths;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    30
import java.nio.file.StandardOpenOption;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    31
import java.util.ArrayList;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    32
import java.util.List;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    33
import java.util.Set;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    34
import java.util.UUID;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    35
import java.util.concurrent.Semaphore;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    37
import jdk.testlibrary.OutputBuffer;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    38
import jdk.testlibrary.ProcessTools;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    39
import sun.jvmstat.monitor.MonitorException;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    40
import sun.jvmstat.monitor.MonitoredHost;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    41
import sun.jvmstat.monitor.MonitoredVm;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    42
import sun.jvmstat.monitor.MonitoredVmUtil;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    43
import sun.jvmstat.monitor.VmIdentifier;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    44
import sun.jvmstat.monitor.event.HostEvent;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    45
import sun.jvmstat.monitor.event.HostListener;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    46
import sun.jvmstat.monitor.event.VmStatusChangeEvent;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    47
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    48
/*
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    49
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    50
 Test starts ten Java processes, each with a unique id.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    52
 Each process creates a file named after the id and then it waits for
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    53
 the test to remove the file, at which the Java process exits.
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    54
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    55
 The processes are monitored by the test to make sure notifications
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    56
 are sent when they are started/terminated.
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    57
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    58
 To avoid Java processes being left behind, in case of an unexpected
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    59
 failure, shutdown hooks are installed that remove files when the test
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    60
 exits. If files are not removed, i.e. due to a JVM crash, the Java
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    61
 processes will exit themselves after 1000 s.
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    62
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    63
*/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    65
/*
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    66
 * @test
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    67
 * @bug 4990825
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    68
 * @summary attach to external but local JVM processes
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    69
 * @library /lib/testlibrary
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    70
 * @build jdk.testlibrary.*
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    71
 * @run main/othervm MonitorVmStartTerminate
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    72
 */
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    73
public final class MonitorVmStartTerminate {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    75
    private static final int PROCESS_COUNT = 10;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    77
    public static void main(String... args) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        MonitoredHost host = MonitoredHost.getMonitoredHost("localhost");
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    80
        host.setInterval(1); // 1 ms
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    81
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    82
        String id = UUID.randomUUID().toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    84
        List<JavaProcess> javaProcesses = new ArrayList<>();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    85
        for (int i = 0; i < PROCESS_COUNT; i++) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    86
            javaProcesses.add(new JavaProcess(id + "_" + i));
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    87
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    89
        Listener listener = new Listener(host, javaProcesses);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    90
        host.addHostListener(listener);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    91
        for (JavaProcess javaProcess : javaProcesses) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    92
            javaProcess.start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    94
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    95
        // Wait for all processes to start before terminating
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    96
        // them, so pids are not reused within a poll interval.
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    97
        System.out.println("Waiting for all processes to get started notification");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    98
        listener.started.acquire(PROCESS_COUNT);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
    99
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   100
        for (JavaProcess javaProcess : javaProcesses) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   101
            javaProcess.terminate();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   102
        }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   103
        System.out.println("Waiting for all processes to get terminated notification");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   104
        listener.terminated.acquire(PROCESS_COUNT);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   105
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   106
        host.removeHostListener(listener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   109
    private static final class Listener implements HostListener {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   110
        private final Semaphore started = new Semaphore(0);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   111
        private final Semaphore terminated = new Semaphore(0);
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   112
        private final MonitoredHost host;
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   113
        private final List<JavaProcess> processes;
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   114
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   115
        public Listener(MonitoredHost host, List<JavaProcess> processes) {
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   116
            this.host = host;
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   117
            this.processes = processes;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   118
            printStatus();
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   119
        }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   120
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   121
        @Override
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   122
        @SuppressWarnings("unchecked")
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   123
        public void vmStatusChanged(VmStatusChangeEvent event) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   124
            releaseStarted(event.getStarted());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   125
            releaseTerminated(event.getTerminated());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   126
            printStatus();
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   127
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   129
        private void printStatus() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   130
            System.out.printf("started=%d, terminated=%d\n",
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   131
                    started.availablePermits(), terminated.availablePermits());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   132
        }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   133
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   134
        @Override
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   135
        public void disconnected(HostEvent arg0) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   136
            // ignore
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   137
        }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   138
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   139
        private void releaseStarted(Set<Integer> ids) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   140
            System.out.println("realeaseStarted(" + ids + ")");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   141
            for (Integer id : ids) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   142
                releaseStarted(id);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   143
            }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   144
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   146
        private void releaseStarted(Integer id) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   147
            for (JavaProcess jp : processes) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   148
                if (hasMainArgs(id, jp.getMainArgsIdentifier())) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   149
                    // store id for terminated identification
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   150
                    jp.setId(id);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   151
                    System.out.println("RELEASED (id=" + jp.getId() + ", args=" + jp.getMainArgsIdentifier() + ")");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   152
                    started.release();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   153
                    return;
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   154
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   158
        private void releaseTerminated(Set<Integer> ids) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   159
            System.out.println("releaseTerminated(" + ids + ")");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   160
            for (Integer id : ids) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   161
                releaseTerminated(id);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   162
            }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   163
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   164
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   165
        private void releaseTerminated(Integer id) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   166
            for (JavaProcess jp : processes) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   167
                if (id.equals(jp.getId())) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   168
                    System.out.println("RELEASED (id=" + jp.getId() + ", args=" + jp.getMainArgsIdentifier() + ")");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   169
                    terminated.release();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   170
                    return;
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   171
                }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   172
            }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   173
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   175
        private boolean hasMainArgs(Integer id, String args) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   176
            try {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   177
                VmIdentifier vmid = new VmIdentifier("//" + id.intValue());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   178
                MonitoredVm target = host.getMonitoredVm(vmid);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   179
                String monitoredArgs = MonitoredVmUtil.mainArgs(target);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   180
                if (monitoredArgs != null && monitoredArgs.contains(args)) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   181
                    return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   183
            } catch (URISyntaxException | MonitorException e) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   184
                // ok. process probably not running
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   185
            }
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   186
            return false;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   187
        }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   188
    }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   189
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   190
    public final static class JavaProcess {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   191
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   192
        private static final class ShutdownHook extends Thread {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   193
            private final JavaProcess javaProcess;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   194
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   195
            public ShutdownHook(JavaProcess javaProcess) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   196
                this.javaProcess = javaProcess;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   197
            }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   198
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   199
            public void run() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   200
                javaProcess.terminate();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   201
            }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   202
        }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   203
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   204
        public static void main(String[] args) throws InterruptedException {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   205
            try {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   206
                Path path = Paths.get(args[0]);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   207
                createFile(path);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   208
                waitForRemoval(path);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   209
            } catch (Throwable t) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   210
                t.printStackTrace();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   211
                System.exit(1);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   212
            }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   213
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   214
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   215
        public Integer getId() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   216
            return id;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   217
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   218
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   219
        public void setId(Integer id) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   220
            this.id = id;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   221
        }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   222
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   223
        private static void createFile(Path path) throws IOException {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   224
            Files.write(path, new byte[0], StandardOpenOption.CREATE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   227
        private static void waitForRemoval(Path path) {
25969
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   228
            String timeoutFactorText = System.getProperty("test.timeout.factor", "1.0");
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   229
            double timeoutFactor = Double.parseDouble(timeoutFactorText);
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   230
            long timeoutNanos = 1000_000_000L*(long)(1000*timeoutFactor);
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   231
            long start = System.nanoTime();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   232
            while (true) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   233
                long now = System.nanoTime();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   234
                long waited = now - start;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   235
                System.out.println("Waiting for " + path + " to be removed, " + waited + " ns");
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   236
                if (!Files.exists(path)) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   237
                    return;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   238
                }
25969
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   239
                if (waited > timeoutNanos) {
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   240
                    System.out.println("Start: " + start);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   241
                    System.out.println("Now: " + now);
25969
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   242
                    System.out.println("Process timed out after " + waited + " ns. Abort.");
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   243
                    System.exit(1);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   244
                }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   245
                takeNap();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   246
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   248
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   249
        private static void takeNap() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   250
            try {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   251
                Thread.sleep(100);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   252
            } catch (InterruptedException e) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   253
                // ignore
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   254
            }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   255
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   256
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   257
        private final String mainArgsIdentifier;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   258
        private final ShutdownHook shutdownHook;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   259
        private volatile Integer id;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   260
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   261
        public JavaProcess(String mainArgsIdentifier) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   262
            this.mainArgsIdentifier = mainArgsIdentifier;
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   263
            this.shutdownHook = new ShutdownHook(this);
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   264
        }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   265
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   266
        /**
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   267
         * Starts a Java process asynchronously.
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   268
         *
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   269
         * The process runs until {@link #stop()} is called. If test exits
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   270
         * unexpectedly the process will be cleaned up by a shutdown hook.
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   271
         *
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   272
         * @throws Exception
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   273
         */
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   274
        public void start() throws Exception {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   275
            Runtime.getRuntime().addShutdownHook(shutdownHook);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   276
            System.out.println("Starting " + getMainArgsIdentifier());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   277
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   278
            Runnable r = new Runnable() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   279
                @Override
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   280
                public void run() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   281
                    try {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   282
                        executeJava();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   283
                    } catch (Throwable t) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   284
                        t.printStackTrace();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   285
                    }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   286
                }
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   287
            };
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   288
            new Thread(r).start();
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   289
        }
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   290
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   291
        public void terminate() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   292
            try {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   293
                System.out.println("Terminating " + mainArgsIdentifier);
25969
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   294
                // File must be created before proceeding,
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   295
                // otherwise Java process may loop forever
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   296
                // waiting for file to be removed.
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   297
                Path path = Paths.get(mainArgsIdentifier);
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   298
                while (!Files.exists(path)) {
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   299
                    takeNap();
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   300
                }
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   301
                Files.delete(path);
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   302
            } catch (IOException e) {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   303
                e.printStackTrace();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   304
            }
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   305
            Runtime.getRuntime().removeShutdownHook(shutdownHook);
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   306
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   307
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   308
        private void executeJava() throws Exception, IOException {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   309
            String className = JavaProcess.class.getName();
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   310
            String classPath = System.getProperty("test.classes");
25969
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   311
            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   312
                "-Dtest.timeout.factor=" + System.getProperty("test.timeout.factor", "1.0"),
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   313
                "-cp", classPath, className, mainArgsIdentifier);
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   314
            OutputBuffer ob = ProcessTools.getOutput(pb.start());
25969
d474691a3e62 8049340: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.java timed out
egahlin
parents: 25225
diff changeset
   315
            System.out.println("Java Process " + getMainArgsIdentifier() + " stderr:"
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   316
                    + ob.getStderr());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   317
            System.err.println("Java Process " + getMainArgsIdentifier() + " stdout:"
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   318
                    + ob.getStdout());
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   319
        }
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   320
25225
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   321
        public String getMainArgsIdentifier() {
f161e8748e8d 8028474: sun/jvmstat/monitor/MonitoredVm/MonitorVmStartTerminate.sh timeout, leaves looping process
egahlin
parents: 23010
diff changeset
   322
            return mainArgsIdentifier;
21652
e935e26bd5ec 6543856: MonitorVmStartTerminate.sh fails intermittently
egahlin
parents: 5506
diff changeset
   323
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}