Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/mallocWithGC3/mallocWithGC3.java
41159 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26*27* @summary converted from VM Testbase gc/gctests/mallocWithGC3.28* VM Testbase keywords: [gc]29* VM Testbase readme:30* ********************************************************31* LD_LIBRARY PATH must include "$TESTBASE/src/misc/gc/utils/lib/sparc(or i386)"32* while running these tests. The native code for all the mallocWithGC* tests33* has been bunched up into a single .so.34* ********************************************************35* In this test, 2 threads are created, one thread(javaHeapEater)36* creates garbage by nulling out the elements of a vector, which formerly37* held points to circular linked lists. These elements are again repopulated38* with new linked lists. The second thread invokes a native function39* that continually mallocs and frees one byte of memory for 3 minutes40* a hold on a malloc lock.41* In this test, the GC is called synchronously while another42* thread contines to malloc/free in a loop.43* The idea here is to see if the vm deadlocks (if it does, it is ofcourse44* a failure ). This test was created because of the following problem45* that the vm used to have :46* "The malloc/GC deadlock problem is that a gc may suspend a thread (in native47* or VM code) that is in the middle of a malloc, so it has the "malloc" lock.48* GC may want to do a malloc, but it can't get the lock, so it deadlocks. "49*50* @library /vmTestbase51* /test/lib52* @run main/othervm/native/timeout=300 gc.gctests.mallocWithGC3.mallocWithGC353*/5455package gc.gctests.mallocWithGC3;5657import nsk.share.test.*;58import nsk.share.gc.*;59import java.util.Vector;6061public class mallocWithGC3 extends TestBase {62static {63System.loadLibrary("mallocWithGC3");64}6566public native void getMallocLock03();6768private class javaHeapEater extends Thread {69private Vector v;7071public javaHeapEater(Vector v) {72this.v = v;73}7475public void run() throws OutOfMemoryError {76int gc_count;7778for(int i = 0; i < 5 ; i++)79v.addElement(buildCircularLinkedList());80gc_count = 0;8182while( gc_count < 10 ) {83for (int i = 0; i < 5 ; i++)84v.setElementAt(null, i);8586System.gc(); // Forcibly call GC.8788for (int i = 0; i < 5 ; i++)89v.setElementAt(buildCircularLinkedList(),i);9091gc_count++;92log.info("Finished iteration # " + gc_count);93}94}95}9697private class cHeapEater extends Thread {98public void run() {99getMallocLock03();100}101}102103public void run() {104Vector v = new Vector(5);105Thread tArray[] = new Thread[2];106107tArray[0] = new javaHeapEater(v);108tArray[1] = new cHeapEater();109110111try {112for(int i = 0; i < tArray.length ; i++ )113tArray[i].start();114for(int i = 0; i < tArray.length ; i++ )115tArray[i].join();116} catch (Exception e) {117setFailed(true);118}119}120121// build a circular linked list of 0.4 Meg122private CircularLinkedList buildCircularLinkedList() {123CircularLinkedList cl;124125cl = new CircularLinkedList(100);126for(int i = 0; i < 2000; i++)127cl.grow();128return cl;129}130131public static void main(String args[]){132Tests.runTest(new mallocWithGC3(), args);133}134135}136137138