Merge
authoramurillo
Mon, 16 Mar 2015 14:27:36 -0700
changeset 29501 4532db7f9956
parent 29494 13a73071b538 (current diff)
parent 29500 2c1e94a636b7 (diff)
child 29502 b017930c7b42
Merge
--- a/jdk/test/com/sun/jdi/NativeInstanceFilterTarg.java	Mon Mar 16 13:49:00 2015 -0700
+++ b/jdk/test/com/sun/jdi/NativeInstanceFilterTarg.java	Mon Mar 16 14:27:36 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,36 +21,18 @@
  * questions.
  */
 
-import sun.misc.Version;
-
 public class NativeInstanceFilterTarg {
 
     public static void main(String args[]) {
-        boolean runTest = jvmSupportsJVMTI_12x();
         String s1 = "abc";
         String s2 = "def";
         latch(s1);
         s1.intern();
-        if (runTest) {
-            s2.intern(); // this is the call that generates events that ought
-                         // to be filtered out.
-        } else {
-            System.out.println("Neutering test since JVMTI 1.2 not supported");
-        }
+        s2.intern(); // this is the call that generates events that ought
+                     // to be filtered out.
     }
 
     // Used by debugger to get an instance to filter with
     public static String latch(String s) { return s; }
 
-    public static boolean jvmSupportsJVMTI_12x() {
-        // This fix requires the JVM to support JVMTI 1.2, which doesn't
-        // happen until HSX 20.0, build 05.
-        int major = Version.jvmMajorVersion();
-        int minor = Version.jvmMinorVersion();
-        int micro = Version.jvmMicroVersion();
-        int build = Version.jvmBuildNumber();
-
-        return (major > 20 || major == 20 &&
-                   (minor > 0 || micro > 0 || build >= 5));
-    }
 }
--- a/jdk/test/com/sun/jdi/RunToExit.java	Mon Mar 16 13:49:00 2015 -0700
+++ b/jdk/test/com/sun/jdi/RunToExit.java	Mon Mar 16 14:27:36 2015 -0700
@@ -24,74 +24,29 @@
 /* @test
  * @bug 4997445
  * @summary Test that with server=y, when VM runs to System.exit() no error happens
- *
- * @build VMConnection RunToExit Exit0
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.* VMConnection RunToExit Exit0
  * @run driver RunToExit
  */
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.File;
-import java.io.BufferedInputStream;
 import java.net.ServerSocket;
 import com.sun.jdi.Bootstrap;
 import com.sun.jdi.VirtualMachine;
 import com.sun.jdi.event.*;
 import com.sun.jdi.connect.Connector;
 import com.sun.jdi.connect.AttachingConnector;
+import java.net.ConnectException;
 import java.util.Map;
 import java.util.List;
 import java.util.Iterator;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import jdk.testlibrary.ProcessTools;
 
 public class RunToExit {
 
     /* Increment this when ERROR: seen */
-    static int error_seen = 0;
+    static volatile int error_seen = 0;
     static volatile boolean ready = false;
-    /*
-     * Helper class to direct process output to a StringBuffer
-     */
-    static class IOHandler implements Runnable {
-        private String              name;
-        private BufferedInputStream in;
-        private StringBuffer        buffer;
-
-        IOHandler(String name, InputStream in) {
-            this.name = name;
-            this.in = new BufferedInputStream(in);
-            this.buffer = new StringBuffer();
-        }
-
-        static void handle(String name, InputStream in) {
-            IOHandler handler = new IOHandler(name, in);
-            Thread thr = new Thread(handler);
-            thr.setDaemon(true);
-            thr.start();
-        }
-
-        public void run() {
-            try {
-                byte b[] = new byte[100];
-                for (;;) {
-                    int n = in.read(b, 0, 100);
-                    // The first thing that will get read is
-                    //    Listening for transport dt_socket at address: xxxxx
-                    // which shows the debuggee is ready to accept connections.
-                    ready = true;
-                    if (n < 0) {
-                        break;
-                    }
-                    buffer.append(new String(b, 0, n));
-                }
-            } catch (IOException ioe) { }
-
-            String str = buffer.toString();
-            if ( str.contains("ERROR:") ) {
-                error_seen++;
-            }
-            System.out.println(name + ": " + str);
-        }
-
-    }
 
     /*
      * Find a connector by name
@@ -111,24 +66,40 @@
     /*
      * Launch a server debuggee with the given address
      */
-    private static Process launch(String address, String class_name) throws IOException {
-        String exe =   System.getProperty("java.home")
-                     + File.separator + "bin" + File.separator + "java";
-        String cmd = exe + " " + VMConnection.getDebuggeeVMOptions() +
-            " -agentlib:jdwp=transport=dt_socket" +
-            ",server=y" + ",suspend=y" + ",address=" + address +
-            " " + class_name;
+    private static Process launch(String address, String class_name) throws Exception {
+        String args[] = new String[]{
+            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address="
+                + address,
+            class_name
+        };
+        args = VMConnection.insertDebuggeeVMOptions(args);
+
+        ProcessBuilder launcher = ProcessTools.createJavaProcessBuilder(args);
 
-        System.out.println("Starting: " + cmd);
+        System.out.println(launcher.command().stream().collect(Collectors.joining(" ", "Starting: ", "")));
 
-        Process p = Runtime.getRuntime().exec(cmd);
-
-        IOHandler.handle("Input Stream", p.getInputStream());
-        IOHandler.handle("Error Stream", p.getErrorStream());
+        Process p = ProcessTools.startProcess(
+            class_name,
+            launcher,
+            RunToExit::checkForError,
+            RunToExit::isTransportListening,
+            0,
+            TimeUnit.NANOSECONDS
+        );
 
         return p;
     }
 
+    private static boolean isTransportListening(String line) {
+        return line.startsWith("Listening for transport dt_socket");
+    }
+
+    private static void checkForError(String line) {
+        if (line.contains("ERROR:")) {
+            error_seen++;
+        }
+    }
+
     /*
      * - pick a TCP port
      * - Launch a server debuggee: server=y,suspend=y,address=${port}
@@ -146,15 +117,6 @@
         // launch the server debuggee
         Process process = launch(address, "Exit0");
 
-        // wait for the debugge to be ready
-        while (!ready) {
-            try {
-                Thread.sleep(1000);
-            } catch(Exception ee) {
-                throw ee;
-            }
-        }
-
         // attach to server debuggee and resume it so it can exit
         AttachingConnector conn = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach");
         Map conn_args = conn.defaultArguments();
@@ -164,7 +126,16 @@
 
         System.out.println("Connection arguments: " + conn_args);
 
-        VirtualMachine vm = conn.attach(conn_args);
+        VirtualMachine vm = null;
+        while (vm == null) {
+            try {
+                vm = conn.attach(conn_args);
+            } catch (ConnectException e) {
+                e.printStackTrace(System.out);
+                System.out.println("--- Debugee not ready. Retrying in 500ms. ---");
+                Thread.sleep(500);
+            }
+        }
 
         // The first event is always a VMStartEvent, and it is always in
         // an EventSet by itself.  Wait for it.
--- a/jdk/test/java/lang/management/ThreadMXBean/AllThreadIds.java	Mon Mar 16 13:49:00 2015 -0700
+++ b/jdk/test/java/lang/management/ThreadMXBean/AllThreadIds.java	Mon Mar 16 14:27:36 2015 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -27,21 +27,19 @@
  * @summary Basic unit test of ThreadMXBean.getAllThreadIds()
  * @author  Alexei Guibadoulline and Mandy Chung
  *
- * @run build Barrier
  * @run main/othervm AllThreadIds
  */
 
 import java.lang.management.*;
-import java.util.*;
+import java.util.concurrent.Phaser;
 
 public class AllThreadIds {
     final static int DAEMON_THREADS = 20;
     final static int USER_THREADS = 5;
     final static int ALL_THREADS = DAEMON_THREADS + USER_THREADS;
-    private static volatile boolean live[] = new boolean[ALL_THREADS];
-    private static Thread allThreads[] = new Thread[ALL_THREADS];
-    private static ThreadMXBean mbean
-        = ManagementFactory.getThreadMXBean();
+    private static final boolean live[] = new boolean[ALL_THREADS];
+    private static final Thread allThreads[] = new Thread[ALL_THREADS];
+    private static final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
     private static boolean testFailed = false;
     private static boolean trace = false;
 
@@ -52,8 +50,7 @@
     private static int curLiveThreadCount = 0;
     private static int curPeakThreadCount = 0;
 
-    // barrier for threads communication
-    private static Barrier barrier = new Barrier(ALL_THREADS);
+    private static final Phaser startupCheck = new Phaser(ALL_THREADS + 1);
 
     private static void printThreadList() {
         if (!trace) return;
@@ -124,18 +121,15 @@
         curPeakThreadCount = mbean.getPeakThreadCount();
         checkThreadCount(0, 0);
 
-
         // Start all threads and wait to be sure they all are alive
-        barrier.set(ALL_THREADS);
         for (int i = 0; i < ALL_THREADS; i++) {
-            live[i] = true;
+            setLive(i, true);
             allThreads[i] = new MyThread(i);
-            allThreads[i].setDaemon( (i < DAEMON_THREADS) ? true : false);
+            allThreads[i].setDaemon(i < DAEMON_THREADS);
             allThreads[i].start();
         }
         // wait until all threads are started.
-        barrier.await();
-
+        startupCheck.arriveAndAwaitAdvance();
 
         checkThreadCount(ALL_THREADS, 0);
         printThreadList();
@@ -173,15 +167,14 @@
 
         // Stop daemon threads, wait to be sure they all are dead, and check
         // that they disappeared from getAllThreadIds() list
-        barrier.set(DAEMON_THREADS);
         for (int i = 0; i < DAEMON_THREADS; i++) {
-            live[i] = false;
+            setLive(i, false);
         }
-        // wait until daemon threads are terminated.
-        barrier.await();
 
-        // give chance to threads to terminate
-        pause();
+        // make sure the daemon threads are completely dead
+        joinDaemonThreads();
+
+        // and check the reported thread count
         checkThreadCount(0, DAEMON_THREADS);
 
         // Check mbean now
@@ -190,11 +183,11 @@
         for (int i = 0; i < ALL_THREADS; i++) {
             long expectedId = allThreads[i].getId();
             boolean found = false;
-            boolean live = (i >= DAEMON_THREADS);
+            boolean alive = (i >= DAEMON_THREADS);
 
             if (trace) {
                 System.out.print("Looking for thread with id " + expectedId +
-                    (live ? " expected alive." : " expected terminated."));
+                    (alive ? " expected alive." : " expected terminated."));
             }
             for (int j = 0; j < list.length; j++) {
                 if (expectedId == list[j]) {
@@ -203,11 +196,11 @@
                 }
             }
 
-            if (live != found) {
+            if (alive != found) {
                 testFailed = true;
             }
             if (trace) {
-                if (live != found) {
+                if (alive != found) {
                     System.out.println(" TEST FAILED.");
                 } else {
                     System.out.println();
@@ -216,15 +209,14 @@
         }
 
         // Stop all threads and wait to be sure they all are dead
-        barrier.set(ALL_THREADS - DAEMON_THREADS);
         for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
-            live[i] = false;
+            setLive(i, false);
         }
-        // wait until daemon threads are terminated .
-        barrier.await();
 
-        // give chance to threads to terminate
-        pause();
+        // make sure the non-daemon threads are completely dead
+        joinNonDaemonThreads();
+
+        // and check the thread count
         checkThreadCount(0, ALL_THREADS - DAEMON_THREADS);
 
         if (testFailed)
@@ -233,6 +225,30 @@
         System.out.println("Test passed.");
     }
 
+    private static void joinDaemonThreads() throws InterruptedException {
+        for (int i = 0; i < DAEMON_THREADS; i++) {
+            allThreads[i].join();
+        }
+    }
+
+    private static void joinNonDaemonThreads() throws InterruptedException {
+        for (int i = DAEMON_THREADS; i < ALL_THREADS; i++) {
+            allThreads[i].join();
+        }
+    }
+
+    private static void setLive(int i, boolean val) {
+        synchronized(live) {
+            live[i] = val;
+        }
+    }
+
+    private static boolean isLive(int i) {
+        synchronized(live) {
+            return live[i];
+        }
+    }
+
     // The MyThread thread lives as long as correspondent live[i] value is true
     private static class MyThread extends Thread {
         int id;
@@ -243,8 +259,8 @@
 
         public void run() {
             // signal started
-            barrier.signal();
-            while (live[id]) {
+            startupCheck.arrive();
+            while (isLive(id)) {
                 try {
                     sleep(100);
                 } catch (InterruptedException e) {
@@ -253,23 +269,6 @@
                     testFailed = true;
                 }
             }
-            // signal about to exit
-            barrier.signal();
         }
     }
-
-    private static Object pauseObj = new Object();
-    private static void pause() {
-        // Enter lock a without blocking
-        synchronized (pauseObj) {
-            try {
-                // may need to tune this timeout for different platforms
-                pauseObj.wait(50);
-            } catch (Exception e) {
-                System.err.println("Unexpected exception.");
-                e.printStackTrace(System.err);
-            }
-        }
-    }
-
 }
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Mon Mar 16 13:49:00 2015 -0700
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Mon Mar 16 14:27:36 2015 -0700
@@ -88,24 +88,12 @@
                                        ProcessBuilder processBuilder,
                                        Consumer<String> consumer)
     throws IOException {
-        Process p = null;
         try {
-            p = startProcess(
-                name,
-                processBuilder,
-                line -> {
-                    if (consumer != null) {
-                        consumer.accept(line);
-                    }
-                    return false;
-                },
-                -1,
-                TimeUnit.NANOSECONDS
-            );
+            return startProcess(name, processBuilder, consumer, null, -1, TimeUnit.NANOSECONDS);
         } catch (InterruptedException | TimeoutException e) {
-            // can't ever happen
+            // will never happen
+            throw new RuntimeException(e);
         }
-        return p;
     }
 
     /**
@@ -134,6 +122,38 @@
                                        long timeout,
                                        TimeUnit unit)
     throws IOException, InterruptedException, TimeoutException {
+        return startProcess(name, processBuilder, null, linePredicate, timeout, unit);
+    }
+
+    /**
+     * <p>Starts a process from its builder.</p>
+     * <span>The default redirects of STDOUT and STDERR are started</span>
+     * <p>
+     * It is possible to wait for the process to get to a warmed-up state
+     * via {@linkplain Predicate} condition on the STDOUT and monitor the
+     * in-streams via the provided {@linkplain Consumer}
+     * </p>
+     * @param name The process name
+     * @param processBuilder The process builder
+     * @param lineConsumer  The {@linkplain Consumer} the lines will be forwarded to
+     * @param linePredicate The {@linkplain Predicate} to use on the STDOUT
+     *                      Used to determine the moment the target app is
+     *                      properly warmed-up.
+     *                      It can be null - in that case the warmup is skipped.
+     * @param timeout The timeout for the warmup waiting; -1 = no wait; 0 = wait forever
+     * @param unit The timeout {@linkplain TimeUnit}
+     * @return Returns the initialized {@linkplain Process}
+     * @throws IOException
+     * @throws InterruptedException
+     * @throws TimeoutException
+     */
+    public static Process startProcess(String name,
+                                       ProcessBuilder processBuilder,
+                                       final Consumer<String> lineConsumer,
+                                       final Predicate<String> linePredicate,
+                                       long timeout,
+                                       TimeUnit unit)
+    throws IOException, InterruptedException, TimeoutException {
         System.out.println("["+name+"]:" + processBuilder.command().stream().collect(Collectors.joining(" ")));
         Process p = processBuilder.start();
         StreamPumper stdout = new StreamPumper(p.getInputStream());
@@ -141,6 +161,18 @@
 
         stdout.addPump(new LineForwarder(name, System.out));
         stderr.addPump(new LineForwarder(name, System.err));
+        if (lineConsumer != null) {
+            StreamPumper.LinePump pump = new StreamPumper.LinePump() {
+                @Override
+                protected void processLine(String line) {
+                    lineConsumer.accept(line);
+                }
+            };
+            stdout.addPump(pump);
+            stderr.addPump(pump);
+        }
+
+
         CountDownLatch latch = new CountDownLatch(1);
         if (linePredicate != null) {
             StreamPumper.LinePump pump = new StreamPumper.LinePump() {