Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/mallocWithGC1/mallocWithGC1.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/mallocWithGC1.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 idea here is to see if the vm deadlocks (if it does, it is ofcourse40* a failure ). This test was created because of the following problem41* that the vm used to have :42* "The malloc/GC deadlock problem is that a gc may suspend a thread (in native43* or VM code) that is in the middle of a malloc, so it has the "malloc" lock.44* GC may want to do a malloc, but it can't get the lock, so it deadlocks. "45*46* @library /vmTestbase47* /test/lib48* @run main/othervm/native/timeout=300 gc.gctests.mallocWithGC1.mallocWithGC149*/5051package gc.gctests.mallocWithGC1;5253import nsk.share.test.*;54import nsk.share.gc.*;55import nsk.share.TestFailure;56import java.util.Vector;5758public class mallocWithGC1 implements Test {59private int objectSize = 100;6061static {62System.loadLibrary("mallocWithGC1");63}6465public native void getMallocLock01();6667class javaHeapEater extends Thread {68private Vector v;6970public javaHeapEater(Vector v) {71this.v = v;72}7374public void run() throws OutOfMemoryError {75int gc_count;7677for(int i = 0; i < 5 ; i++)78v.addElement(buildCircularLinkedList());79gc_count = 0;80while( gc_count < 10 ) {8182for(int i = 0; i < 5 ; i++)83v.setElementAt(null, i);8485for(int i = 0; i < 5 ; i++)86v.setElementAt(buildCircularLinkedList(),i);8788gc_count++;89System.out.println("Finished iteration # " + gc_count);90}91}92}9394class cHeapEater extends Thread{95public void run() {96getMallocLock01();97}98}99100public void run() {101Vector v = new Vector(5);102Thread tArray[] = new Thread[2];103104tArray[0] = new javaHeapEater(v);105tArray[1] = new cHeapEater();106107try {108for(int i = 0; i < tArray.length ; i++ )109tArray[i].start();110for(int i = 0; i < tArray.length ; i++ )111tArray[i].join();112} catch (Exception e) {113throw new TestFailure("Test Failed.", e);114}115System.out.println("Test Passed.");116}117118// build a circular linked list of 0.2 Meg119120private CircularLinkedList buildCircularLinkedList() {121CircularLinkedList cl = new CircularLinkedList(objectSize);122for(int i = 0; i < 2000; i++)123cl.grow();124return cl;125}126127public static void main(String args[]){128Tests.runTest(new mallocWithGC1(), args);129}130}131132133