test/jdk/java/util/concurrent/locks/Lock/TimedAcquireLeak.java
author iignatyev
Mon, 17 Sep 2018 11:50:39 -0700
changeset 51769 0ae80830256e
parent 51754 594919232b8f
child 58134 51cd29502ea9
permissions -rw-r--r--
8210779: 8182404 and 8210732 haven't updated copyright years Reviewed-by: bchristi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51769
0ae80830256e 8210779: 8182404 and 8210732 haven't updated copyright years
iignatyev
parents: 51754
diff changeset
     2
 * Copyright (c) 2007, 2018, 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
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 50229
diff changeset
    28
 * @library /test/lib
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;
12049
de3e33eb5fa1 7092140: Test: java/util/concurrent/locks/Lock/TimedAcquireLeak.java fails on SE-E due to -XX:-UsePerfData
dholmes
parents: 7668
diff changeset
    34
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    35
import java.io.File;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    36
import java.io.FileOutputStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    37
import java.io.IOException;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    38
import java.io.InputStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    39
import java.io.InputStreamReader;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    40
import java.io.OutputStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    41
import java.io.PrintStream;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    42
import java.io.Reader;
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
    43
import java.lang.ref.ReferenceQueue;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    44
import java.lang.ref.WeakReference;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    45
import java.util.concurrent.BlockingQueue;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    46
import java.util.concurrent.Callable;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    47
import java.util.concurrent.CountDownLatch;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    48
import java.util.concurrent.CyclicBarrier;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    49
import java.util.concurrent.ExecutorService;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    50
import java.util.concurrent.Executors;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    51
import java.util.concurrent.Future;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    52
import java.util.concurrent.LinkedBlockingQueue;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
    53
import java.util.concurrent.Semaphore;
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 38344
diff changeset
    54
import java.util.concurrent.ThreadLocalRandom;
32990
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;
51754
594919232b8f 8210732: remove jdk.testlibrary.Utils
iignatyev
parents: 50229
diff changeset
    59
import jdk.test.lib.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() {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   128
        long timeoutMillis = 1000L;
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   129
        CountDownLatch finalized = new CountDownLatch(1);
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   130
        ReferenceQueue<Object> queue = new ReferenceQueue<>();
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   131
        WeakReference<Object> ref = new WeakReference<>(
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   132
            new Object() { protected void finalize() { finalized.countDown(); }},
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   133
            queue);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   134
        try {
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   135
            for (int tries = 3; tries--> 0; ) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   136
                System.gc();
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   137
                if (finalized.await(timeoutMillis, MILLISECONDS)
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   138
                    && queue.remove(timeoutMillis) != null
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   139
                    && ref.get() == null) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   140
                    System.runFinalization(); // try to pick up stragglers
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   141
                    return;
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   142
                }
50229
6b29ef846c5c 8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents: 47216
diff changeset
   143
                timeoutMillis *= 4;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   144
            }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   145
        } catch (InterruptedException unexpected) {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   146
            throw new AssertionError("unexpected InterruptedException");
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   147
        }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   148
        throw new AssertionError("failed to do a \"full\" gc");
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   149
    }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   150
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // To be called exactly twice by the child process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static void rendezvousChild() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        try {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   154
            forceFullGc();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   155
            sendByte(System.out);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    static String match(String s, String regex, int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Matcher matcher = Pattern.compile(regex).matcher(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        matcher.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return matcher.group(group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   166
    /** It's all about sending a message! */
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   167
    static void sendByte(OutputStream s) throws IOException {
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   168
        s.write('!');
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   169
        s.flush();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   170
    }
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   171
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static int objectsInUse(final Process child,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            final String childPid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                            final String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        final String regex =
38344
81b7cf716427 8155727: java/util/concurrent/locks/Lock/TimedAcquireLeak.java timeouts.
hseigel
parents: 36233
diff changeset
   176
            "(?m)^ *[0-9]+: +([0-9]+) +[0-9]+ +\\Q"+className+"\\E(?:$| )";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        final Callable<Integer> objectsInUse =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            new Callable<Integer>() { public Integer call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                Integer i = Integer.parseInt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    match(commandOutputOf(jmap, "-histo:live", childPid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                          regex, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                if (i > 100)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    System.out.print(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        commandOutputOf(jmap,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                        "-dump:file=dump,format=b",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                        childPid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        try { return rendezvousParent(child, objectsInUse); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        catch (Throwable t) { unexpected(t); return -1; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // jmap doesn't work on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (System.getProperty("os.name").startsWith("Windows"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        final String childClassName = Job.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        final String classToCheckForLeaks = Job.classToCheckForLeaks();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        final String uniqueID =
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 38344
diff changeset
   201
            String.valueOf(ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        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
   204
            java, "-Xmx8m", "-XX:+UsePerfData",
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   205
            "-classpath", System.getProperty("test.class.path"),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            childClassName, uniqueID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        final Process p = new ProcessBuilder(jobCmd).start();
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   209
        // 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
   210
        p.getInputStream().read();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   211
        sendByte(p.getOutputStream());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        final String childPid =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            match(commandOutputOf(jps, "-m"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                  "(?m)^ *([0-9]+) +\\Q"+childClassName+"\\E *"+uniqueID+"$", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        final int n0 = objectsInUse(p, childPid, classToCheckForLeaks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        final int n1 = objectsInUse(p, childPid, classToCheckForLeaks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        equal(p.waitFor(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        equal(p.exitValue(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        failed += p.exitValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Check that no objects were leaked.
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   224
        //
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   225
        // 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
   226
        // implementation, and needing occasional adjustment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        System.out.printf("%d -> %d%n", n0, n1);
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   228
        // Almost always n0 == n1
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   229
        // Maximum jitter observed in practice is 10 -> 17
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   230
        check(Math.abs(n1 - n0) < 10);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   231
        check(n1 < 25);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        drainers.shutdown();
36233
f85ed703cf7e 8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents: 34347
diff changeset
   233
        if (!drainers.awaitTermination(LONG_DELAY_MS, MILLISECONDS)) {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   234
            drainers.shutdownNow(); // last resort
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   235
            throw new AssertionError("thread pool did not terminate");
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   236
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // The main class of the child process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // Job's job is to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    // - provide the name of a class to check for leaks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // - call rendezvousChild exactly twice, while quiescent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    // - in between calls to rendezvousChild, run code that may leak.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    //----------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public static class Job {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        static String classToCheckForLeaks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                "java.util.concurrent.locks.AbstractQueuedSynchronizer$Node";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        public static void main(String[] args) throws Throwable {
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   253
            // 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
   254
            sendByte(System.out);
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   255
            System.in.read();
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 14342
diff changeset
   256
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            final ReentrantReadWriteLock rwlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                = new ReentrantReadWriteLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            final ReentrantReadWriteLock.ReadLock readLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                = rwlock.readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            final ReentrantReadWriteLock.WriteLock writeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                = rwlock.writeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            rwlock.writeLock().lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 38344
diff changeset
   268
            final BlockingQueue<Object> q = new LinkedBlockingQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            final Semaphore fairSem = new Semaphore(0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            final Semaphore unfairSem = new Semaphore(0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            //final int threads =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            //rnd.nextInt(Runtime.getRuntime().availableProcessors() + 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            final int threads = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // On Linux, this test runs very slowly for some reason,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // so use a smaller number of iterations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            // Solaris can handle 1 << 18.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            // On the other hand, jmap is much slower on Solaris...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            final int iterations = 1 << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            final CyclicBarrier cb = new CyclicBarrier(threads+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            for (int i = 0; i < threads; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                new Thread() { public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    try {
43522
f9c6f543c4db 8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents: 38344
diff changeset
   284
                        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        for (int j = 0; j < iterations; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            if (j == iterations/10 || j == iterations - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                cb.await(); // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                cb.await(); // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            //int t = rnd.nextInt(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                            int t = rnd.nextInt(900);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            check(! lock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            check(! readLock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            check(! writeLock.tryLock(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            equal(null, q.poll(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            check(! fairSem.tryAcquire(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            check(! unfairSem.tryAcquire(t, NANOSECONDS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    } catch (Throwable t) { unexpected(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }}.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            cb.await();         // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            rendezvousChild();  // Measure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            cb.await();         // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            cb.await();         // Quiesce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            rendezvousChild();  // Measure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            cb.await();         // Resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            System.exit(failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // If something goes wrong, we might never see it, since IO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        // streams are connected to the parent.  So we need a special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // purpose print method to debug Jobs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        static void debugPrintf(String format, Object... args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                new PrintStream(new FileOutputStream("/dev/tty"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    .printf(format, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            } catch (Throwable t) { throw new Error(t); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}