1 /*
   2  * Copyright (c) 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 
  24 package nsk.stress.jni;
  25 
  26 class GarbageGenerator extends Thread {
  27     class Garbage {
  28         Garbage() {
  29             this(1024);
  30         }
  31 
  32         Garbage(int m) {
  33             memory = new byte[m];
  34         }
  35 
  36         void setNext(Garbage n) {
  37             next = n;
  38         }
  39 
  40         Garbage getNext() {
  41             return next;
  42         }
  43 
  44         protected void finalize() {
  45         }
  46 
  47         private Garbage next;
  48         private byte[] memory;
  49     }
  50 
  51     class GarbageRing {
  52         GarbageRing() {
  53             attachment = new Garbage(0);
  54         }
  55 
  56         void add(int size) {
  57             Garbage head = attachment.getNext();
  58             Garbage g = new Garbage(size);
  59             if (head != null) {
  60                 Garbage oldNext = head.getNext();
  61                 if (oldNext != null) {
  62                     g.setNext(oldNext);
  63                     head.setNext(g);
  64                     attachment.setNext(g);
  65                 } else {
  66                     g.setNext(head);
  67                     head.setNext(g);
  68                 }
  69             } else
  70                 attachment.setNext(g);
  71         }
  72 
  73         void discard() {
  74             attachment.setNext(null);
  75         }
  76 
  77         private byte[] memory;
  78         private Garbage attachment;
  79     }
  80 
  81     public void run() {
  82         GarbageRing gr = new GarbageRing();
  83         int g = 0;
  84         while (!done) {
  85             for (g = 0; g < ringSize; g++) {
  86                 gr.add(allocSize);
  87                 yield();
  88             }
  89             gr.discard();
  90             try {
  91                 sleep(interval);
  92             } catch (InterruptedException e) {
  93             }
  94         }
  95         if (DEBUG) System.out.println("GarbageRing::run(): done");
  96     }
  97 
  98     public void setAllocSize(int i) {
  99         allocSize = i;
 100     }
 101 
 102     public int getAllocSize() {
 103         return allocSize;
 104     }
 105 
 106     public void setInterval(int i) {
 107         interval = i;
 108     }
 109 
 110     public int getInterval() {
 111         return interval;
 112     }
 113 
 114     public void halt() {
 115         done = true;
 116     }
 117 
 118     private int allocSize = 10000;
 119     private int ringSize = 50;
 120     private int interval = 1000;
 121     private boolean done = false;
 122     final private static boolean DEBUG = false;
 123 }