Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/mallocWithGC2/mallocWithGC2.java
41161 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/mallocWithGC2.28* VM Testbase keywords: [gc]29* VM Testbase readme:30* LD_LIBRARY PATH must include "$TESTBASE/src/misc/gc/utils/lib/sparc(or i386)"31* while running these tests. The native code for all the mallocWithGC* tests32* has been bunched up into a single .so.33* In this test, 2 threads are created, one thread(javaHeapEater)34* creates garbage by nulling out the elements of a vector, which formerly35* held points to circular linked lists. These elements are again repopulated36* with new linked lists. The second thread invokes a native function37* that continually mallocs and frees one byte of memory for 3 minutes38* a hold on a malloc lock.39* The difference between mallocWithGC1 mallocWithGC2 is the way the locks40* are held. here the "malloc" lock is more efficiently held, not by41* the expiration of a timer but as long as the java heap devourer thread42* stays alive.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 gc.gctests.mallocWithGC2.mallocWithGC253*/5455package gc.gctests.mallocWithGC2;5657import nsk.share.test.*;58import nsk.share.gc.*;59import nsk.share.TestFailure;60import java.util.Vector;6162public class mallocWithGC2 extends TestBase {63static {64System.loadLibrary("mallocWithGC2");65}6667public native void getMallocLock02();6869private class javaHeapEater extends Thread {70private Vector v;7172javaHeapEater(Vector v) {73this.v = v;74}7576public void run() throws OutOfMemoryError {77int gc_count;7879for(int i = 0; i < 10 ; i++)80v.addElement(buildCircularLinkedList());81gc_count = 0;82while( gc_count < 10 ) {8384for(int i = 0; i < 10 ; i++)85v.setElementAt(null, i);8687for(int i = 0; i < 10 ; i++)88v.setElementAt(buildCircularLinkedList(),i);8990gc_count++;91log.info("Finished iteration # " + gc_count);92}93}94}959697private class cHeapEater extends Thread{98public void run() {99getMallocLock02();100}101}102103public void run() {104Vector v = new Vector(10);105Thread tArray[] = new Thread[2];106107tArray[0] = new javaHeapEater(v);108tArray[1] = new cHeapEater();109110try {111for(int i = 0; i < tArray.length ; i++ )112tArray[i].start();113114tArray[0].join(); // wait for the javaHeapEater Thread to finish115tArray[1].stop(); // Once javaHeapEater is finished, stop the116// the cHeapEater thread.117} catch (Exception e) {118throw new TestFailure("Test Failed.", e);119}120log.info("Test Passed.");121}122123// build a circular linked list of 0.2 Meg124125private CircularLinkedList buildCircularLinkedList() {126CircularLinkedList cl;127128cl = new CircularLinkedList(100);129for(int i = 0; i < 2000; i++)130cl.grow();131return cl;132}133134public static void main(String args[]) {135Tests.runTest(new mallocWithGC2(), args);136}137138}139140141