test/hotspot/jtreg/gc/cslocker/TestCSLocker.java
author jiangli
Fri, 02 Nov 2018 19:30:31 -0400
changeset 52397 1322829d1501
parent 47216 71c04702a3d5
child 53523 4c5184c56dc2
permissions -rw-r--r--
8209564: runtime/appcds/CDSandJFR.java timeout on tier6 on sparc Summary: Set timeout 500 in CDSandJFR.java Reviewed-by: dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46607
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     1
/*
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     2
 * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     4
 *
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     7
 * published by the Free Software Foundation.
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     8
 *
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    13
 * accompanied this code).
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    14
 *
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    18
 *
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    21
 * questions.
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    22
 */
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    23
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    24
/*
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    25
 * @test TestCSLocker
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    26
 * @key gc
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    27
 * @bug 6186200
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    28
 * @summary This short test check RFE 6186200 changes. One thread locked
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    29
 * @summary completely in JNI CS, while other is trying to allocate memory
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    30
 * @summary provoking GC. OOM means FAIL, deadlock means PASS.
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    31
 * @run main/native/othervm -Xmx256m TestCSLocker
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    32
 */
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    33
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    34
public class TestCSLocker extends Thread
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    35
{
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    36
    static int timeout = 5000;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    37
    public static void main(String args[]) throws Exception {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    38
        long startTime = System.currentTimeMillis();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    39
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    40
        // start garbage producer thread
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    41
        GarbageProducer garbageProducer = new GarbageProducer(1000000, 10);
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    42
        garbageProducer.start();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    43
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    44
        // start CS locker thread
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    45
        CSLocker csLocker = new CSLocker();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    46
        csLocker.start();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    47
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    48
        // check timeout to success deadlocking
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    49
        while(System.currentTimeMillis() < startTime + timeout) {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    50
            System.out.println("sleeping...");
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    51
            sleep(1000);
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    52
        }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    53
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    54
        csLocker.unlock();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    55
        garbageProducer.interrupt();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    56
    }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    57
}
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    58
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    59
class GarbageProducer extends Thread
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    60
{
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    61
    private int size;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    62
    private int sleepTime;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    63
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    64
    GarbageProducer(int size, int sleepTime) {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    65
        this.size = size;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    66
        this.sleepTime = sleepTime;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    67
    }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    68
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    69
    public void run() {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    70
        boolean isRunning = true;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    71
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    72
        while (isRunning) {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    73
            try {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    74
                int[] arr = null;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    75
                arr = new int[size];
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    76
                sleep(sleepTime);
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    77
            } catch (InterruptedException e) {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    78
                isRunning = false;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    79
            }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    80
        }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    81
    }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    82
}
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    83
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    84
class CSLocker extends Thread
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    85
{
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    86
    static { System.loadLibrary("TestCSLocker"); }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    87
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    88
    public void run() {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    89
        int[] a = new int[10];
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    90
        a[0] = 1;
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    91
        if (!lock(a)) {
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    92
            throw new RuntimeException("failed to acquire CSLock");
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    93
        }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    94
    }
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    95
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    96
    native boolean lock(int[] array);
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    97
    native void unlock();
cbe334268a4c 8178507: co-locate nsk.regression.gc tests
aharlap
parents:
diff changeset
    98
}