jdk/test/java/util/concurrent/locks/Lock/TimedAcquireLeak.java
author hseigel
Fri, 29 Apr 2016 15:17:46 -0400
changeset 38344 81b7cf716427
parent 36233 f85ed703cf7e
child 43522 f9c6f543c4db
permissions -rw-r--r--
8155727: java/util/concurrent/locks/Lock/TimedAcquireLeak.java timeouts. Summary: Fix regex pattern to handle possible (module@version) text Reviewed-by: ctornqvi, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38344
81b7cf716427 8155727: java/util/concurrent/locks/Lock/TimedAcquireLeak.java timeouts.
hseigel
parents: 36233
diff changeset
     2
 * Copyright (c) 2007, 2016, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6460501 6236036 6500694 6490770
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Repeated failed timed waits shouldn't leak memory
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    28
 * @library /lib/testlibrary/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    32
import static java.util.concurrent.TimeUnit.MILLISECONDS;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    33
import static java.util.concurrent.TimeUnit.NANOSECONDS;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    34
import static java.util.concurrent.TimeUnit.SECONDS;
12049
de3e33eb5fa1 7092140: Test: java/util/concurrent/locks/Lock/TimedAcquireLeak.java fails on SE-E due to -XX:-UsePerfData
dholmes
parents: 7668
diff changeset
    35
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    36
import java.io.File;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    37
import java.io.FileOutputStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    38
import java.io.IOException;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    39
import java.io.InputStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    40
import java.io.InputStreamReader;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    41
import java.io.OutputStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    42
import java.io.PrintStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    43
import java.io.Reader;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    44
import java.lang.ref.WeakReference;
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32990
diff changeset
    45
import java.util.SplittableRandom;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    46
import java.util.concurrent.BlockingQueue;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    47
import java.util.concurrent.Callable;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    48
import java.util.concurrent.CountDownLatch;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    49
import java.util.concurrent.CyclicBarrier;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    50
import java.util.concurrent.ExecutorService;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    51
import java.util.concurrent.Executors;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    52
import java.util.concurrent.Future;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    53
import java.util.concurrent.LinkedBlockingQueue;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    54
import java.util.concurrent.Semaphore;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    55
import java.util.concurrent.locks.ReentrantLock;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    56
import java.util.concurrent.locks.ReentrantReadWriteLock;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    57
import java.util.regex.Matcher;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    58
import java.util.regex.Pattern;
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    59
import jdk.testlibrary.Utils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class TimedAcquireLeak {
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    62
    static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
    63
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static String javahome() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String jh = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (jh.endsWith("jre")) ? jh.substring(0, jh.length() - 4) : jh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final File bin = new File(javahome(), "bin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static String javaProgramPath(String programName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return new File(bin, programName).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final String java = javaProgramPath("java");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static final String jmap = javaProgramPath("jmap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static final String jps  = javaProgramPath("jps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static String outputOf(Reader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        final StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        final char[] buf = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while ((n = r.read(buf)) > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            sb.append(buf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static String outputOf(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return outputOf(new InputStreamReader(is, "UTF-8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    92
    static final ExecutorService drainers = Executors.newFixedThreadPool(12);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static Future<String> futureOutputOf(final InputStream is) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return drainers.submit(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            new Callable<String>() { public String call() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    return outputOf(is); }});}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static String outputOf(final Process p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            Future<String> outputFuture = futureOutputOf(p.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            Future<String> errorFuture = futureOutputOf(p.getErrorStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            final String output = outputFuture.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            final String error = errorFuture.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // Check for successful process completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            equal(error, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            equal(p.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (Throwable t) { unexpected(t); throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static String commandOutputOf(String... cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try { return outputOf(new ProcessBuilder(cmd).start()); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        catch (Throwable t) { unexpected(t); throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    // To be called exactly twice by the parent process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static <T> T rendezvousParent(Process p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                  Callable<T> callable) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        p.getInputStream().read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        T result = callable.call();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   122
        sendByte(p.getOutputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   126
    /** No guarantees, but effective in practice. */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   127
    private static void forceFullGc() {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   128
        CountDownLatch finalizeDone = new CountDownLatch(1);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   129
        WeakReference<?> ref = new WeakReference<Object>(new Object() {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   130
            protected void finalize() { finalizeDone.countDown(); }});
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   131
        try {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   132
            for (int i = 0; i < 10; i++) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   133
                System.gc();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   134
                if (finalizeDone.await(1L, SECONDS) && ref.get() == null) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   135
                    System.runFinalization(); // try to pick up stragglers
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   136
                    return;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   137
                }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   138
            }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   139
        } catch (InterruptedException unexpected) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   140
            throw new AssertionError("unexpected InterruptedException");
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   141
        }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   142
        throw new AssertionError("failed to do a \"full\" gc");
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   143
    }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   144
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    // To be called exactly twice by the child process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static void rendezvousChild() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   148
            forceFullGc();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   149
            sendByte(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    static String match(String s, String regex, int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Matcher matcher = Pattern.compile(regex).matcher(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        matcher.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return matcher.group(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   160
    /** It's all about sending a message! */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   161
    static void sendByte(OutputStream s) throws IOException {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   162
        s.write('!');
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   163
        s.flush();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   164
    }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   165
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static int objectsInUse(final Process child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            final String childPid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                            final String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        final String regex =
38344
81b7cf716427 8155727: java/util/concurrent/locks/Lock/TimedAcquireLeak.java timeouts.
hseigel
parents: 36233
diff changeset
   170
            "(?m)^ *[0-9]+: +([0-9]+) +[0-9]+ +\\Q"+className+"\\E(?:$| )";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        final Callable<Integer> objectsInUse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            new Callable<Integer>() { public Integer call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                Integer i = Integer.parseInt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    match(commandOutputOf(jmap, "-histo:live", childPid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                          regex, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                if (i > 100)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    System.out.print(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        commandOutputOf(jmap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                        "-dump:file=dump,format=b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                        childPid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try { return rendezvousParent(child, objectsInUse); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        catch (Throwable t) { unexpected(t); return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // jmap doesn't work on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (System.getProperty("os.name").startsWith("Windows"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        final String childClassName = Job.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        final String classToCheckForLeaks = Job.classToCheckForLeaks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        final String uniqueID =
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32990
diff changeset
   195
            String.valueOf(new SplittableRandom().nextInt(Integer.MAX_VALUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        final String[] jobCmd = {
12049
de3e33eb5fa1 7092140: Test: java/util/concurrent/locks/Lock/TimedAcquireLeak.java fails on SE-E due to -XX:-UsePerfData
dholmes
parents: 7668
diff changeset
   198
            java, "-Xmx8m", "-XX:+UsePerfData",
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   199
            "-classpath", System.getProperty("test.class.path"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            childClassName, uniqueID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        final Process p = new ProcessBuilder(jobCmd).start();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   203
        // Ensure subprocess jvm has started, so that jps can find it
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   204
        p.getInputStream().read();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   205
        sendByte(p.getOutputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        final String childPid =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            match(commandOutputOf(jps, "-m"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                  "(?m)^ *([0-9]+) +\\Q"+childClassName+"\\E *"+uniqueID+"$", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        final int n0 = objectsInUse(p, childPid, classToCheckForLeaks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        final int n1 = objectsInUse(p, childPid, classToCheckForLeaks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        equal(p.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        failed += p.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // Check that no objects were leaked.
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   218
        //
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   219
        // TODO: This test is very brittle, depending on current JDK
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   220
        // implementation, and needing occasional adjustment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        System.out.printf("%d -> %d%n", n0, n1);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   222
        // Almost always n0 == n1
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   223
        // Maximum jitter observed in practice is 10 -> 17
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   224
        check(Math.abs(n1 - n0) < 10);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   225
        check(n1 < 25);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        drainers.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   227
        if (!drainers.awaitTermination(LONG_DELAY_MS, MILLISECONDS)) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   228
            drainers.shutdownNow(); // last resort
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   229
            throw new AssertionError("thread pool did not terminate");
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   230
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // The main class of the child process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // Job's job is to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // - provide the name of a class to check for leaks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // - call rendezvousChild exactly twice, while quiescent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // - in between calls to rendezvousChild, run code that may leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public static class Job {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        static String classToCheckForLeaks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                "java.util.concurrent.locks.AbstractQueuedSynchronizer$Node";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public static void main(String[] args) throws Throwable {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   247
            // Synchronize with parent process, so that jps can find us
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   248
            sendByte(System.out);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   249
            System.in.read();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   250
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            final ReentrantReadWriteLock rwlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                = new ReentrantReadWriteLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            final ReentrantReadWriteLock.ReadLock readLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                = rwlock.readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            final ReentrantReadWriteLock.WriteLock writeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                = rwlock.writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            rwlock.writeLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            final BlockingQueue<Object> q = new LinkedBlockingQueue<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            final Semaphore fairSem = new Semaphore(0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            final Semaphore unfairSem = new Semaphore(0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            //final int threads =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            //rnd.nextInt(Runtime.getRuntime().availableProcessors() + 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            final int threads = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // On Linux, this test runs very slowly for some reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            // so use a smaller number of iterations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // Solaris can handle 1 << 18.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // On the other hand, jmap is much slower on Solaris...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            final int iterations = 1 << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            final CyclicBarrier cb = new CyclicBarrier(threads+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            for (int i = 0; i < threads; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                new Thread() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    try {
34347
4a17f9e90a0f 8142441: Improve jtreg tests for java.util.concurrent
dl
parents: 32990
diff changeset
   278
                        final SplittableRandom rnd = new SplittableRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        for (int j = 0; j < iterations; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            if (j == iterations/10 || j == iterations - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                cb.await(); // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                cb.await(); // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                            //int t = rnd.nextInt(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                            int t = rnd.nextInt(900);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            check(! lock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            check(! readLock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            check(! writeLock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            equal(null, q.poll(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            check(! fairSem.tryAcquire(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            check(! unfairSem.tryAcquire(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                }}.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            cb.await();         // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            rendezvousChild();  // Measure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            cb.await();         // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            cb.await();         // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            rendezvousChild();  // Measure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            cb.await();         // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            System.exit(failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // If something goes wrong, we might never see it, since IO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        // streams are connected to the parent.  So we need a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        // purpose print method to debug Jobs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        static void debugPrintf(String format, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                new PrintStream(new FileOutputStream("/dev/tty"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    .printf(format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}