1 /*
   2  * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package gc.lock;
  24 
  25 import nsk.share.runner.*;
  26 import nsk.share.gc.*;
  27 import nsk.share.gc.gp.*;
  28 import nsk.share.gc.lock.*;
  29 import nsk.share.test.ExecutionController;
  30 
  31 /**
  32  * Test how GC is affected by locking.
  33  *
  34  * A number of threads is started. Each one locks and eats memory.
  35  */
  36 public class LockerTest extends ThreadedGCTest implements GarbageProducerAware, GarbageProducer1Aware, LockersAware {
  37 
  38     private GarbageProducer garbageProducer;
  39     private GarbageProducer garbageProducer1;
  40     private Lockers lockers;
  41     private long objectSize = 1000;
  42 
  43     private class Worker implements Runnable {
  44 
  45         byte[] rezerve = new byte[1024 * 1024];
  46         private Locker locker = lockers.createLocker(garbageProducer1.create(objectSize));
  47 
  48         public Worker() {
  49             locker.enable();
  50         }
  51 
  52         public void run() {
  53             locker.lock();
  54             GarbageUtils.eatMemory(getExecutionController(), garbageProducer);
  55             locker.unlock();
  56         }
  57     }
  58 
  59     protected Runnable createRunnable(int i) {
  60         return new Worker();
  61     }
  62 
  63     public void setGarbageProducer(GarbageProducer garbageProducer) {
  64         this.garbageProducer = garbageProducer;
  65     }
  66 
  67     public void setGarbageProducer1(GarbageProducer garbageProducer1) {
  68         this.garbageProducer1 = garbageProducer1;
  69     }
  70 
  71     public void setLockers(Lockers lockers) {
  72         this.lockers = lockers;
  73     }
  74 
  75     public static void main(String[] args) {
  76         RunParams.getInstance().setRunMemDiagThread(false);
  77         GC.runTest(new LockerTest(), args);
  78     }
  79 }