Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/gctests/gctest04/reqgen.java
41155 views
/*1* Copyright (c) 2002, 2018, 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*/2223package gc.gctests.gctest04;2425import nsk.share.test.*;26import nsk.share.gc.*;2728//reqgen.java2930/* stress testing31reqgen is a subtype of Thread which generates32request to allocate small objects ( 8 ~ 32k), short live time ( 5 ~ 10 ms)33reqdisp is a subtype of Thread which dispatches request34and create a livethread object to allocate the requested memory35and simulate its life time.36*/3738class bufreq39{40int bufsz; // memory size41int life; // live time of the object42bufreq next;4344bufreq(int bufsz, int t)45{46this.bufsz = bufsz;47this.life = t;48this.next = null;49}5051public void setnext(bufreq b)52{53next = b;54}5556public bufreq getnext()57{58return next;59}6061public int getsize()62{63return bufsz;64}6566public int livetime()67{68return life;69}70}7172class queue73{74bufreq head;75bufreq tail;76int limit;77int count;7879queue(int newLimit)80{81head = null;82tail = null;83limit = newLimit;84count = 0;85}8687public boolean okToContinue()88{89return (count < limit);90}9192public synchronized void append(bufreq b)93{94count++;95if ( tail == null ) // head must be null too96{97head = tail = b;98return;99}100tail.setnext(b);101tail = b;102}103104public synchronized bufreq remove()105{106if ( head == null ) return null;107bufreq buf = head;108head = head.getnext();109if ( head == null ) // only one element in the queue110{111tail = head = null;112}113return buf;114}115}116117class reqgen extends Thread {118queue req;119int maxsz;120int minsz;121int maxlive;122int minlive;123int amda;124125reqgen(queue req, int t)126{127this.req = req;128amda = t;129}130131public void setsize(int s1, int s2)132{133maxsz = s2;134minsz = s1;135}136137public void setlive(int t1, int t2)138{139maxlive = t2;140minlive = t1;141}142143public void run()144{145bufreq buf;146int sz;147int t;148149sz = minsz;150t = minlive;151while ( req.okToContinue() )152{153buf = new bufreq(sz, t);154155sz = ( 2*sz);156157if ( sz > maxsz)158{159sz = minsz;160}161162t = ( 2 * t );163if ( t > maxlive)164{165t = minlive;166}167168req.append(buf);169170try171{172sleep(amda);173}174catch(InterruptedException e) {}175}176}177178public bufreq nextreq()179{180return req.remove();181}182183}184185// buffer request dispatcher and allocator186class reqdisp extends Thread {187queue req;188189reqdisp(queue q )190{191req = q;192}193194public void run()195{196bufreq r;197livethread lt;198199while ( req.okToContinue() )200{201r = req.remove();202if ( r != null )203{204lt = new livethread(r);205lt.start();206}207// simulate the interarrival time208try209{210sleep((int)(LocalRandom.random() * 20));211}212catch (InterruptedException e) {}213}214}215216217}218219class livethread extends Thread {220bufreq req;221222livethread(bufreq r)223{224req = r;225}226227public void run()228{229int buf[];230231buf = new int[req.getsize()];232233// simulate the life time of the created object234// if live time is 0, that means forever235if ( req.livetime() == 0 )236{237while ( true )238{239try240{241sleep(10000);242}243catch (InterruptedException e) {}244}245}246else247{248try249{250sleep(req.livetime());251}252catch (InterruptedException e) {}253254}255// live object is outdated, should be GC'ed256}257}258259260