1 /*
   2  * Copyright (c) 2002, 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 /*
  25  * @test
  26  * @key stress gc
  27  *
  28  * @summary converted from VM Testbase gc/memory/FillingStation.
  29  * VM Testbase keywords: [gc, stress, nonconcurrent]
  30  *
  31  * @library /vmTestbase
  32  *          /test/lib
  33  * @run driver jdk.test.lib.FileInstaller . .
  34  * @run main/othervm gc.memory.FillingStation.FillingStation
  35  */
  36 
  37 package gc.memory.FillingStation;
  38 
  39 import jdk.test.lib.Utils;
  40 import java.util.Random;
  41 
  42 public class FillingStation {
  43 
  44     public static final long minObjectSize  = 4;
  45     public static final long freeSpaceLimit = 64;
  46     public static final long maxObjectSize  = 32*1024;
  47 
  48     public static final boolean debug        = false;
  49 
  50     public static void main(String[] arg) {
  51         prologue();
  52         fill();
  53         epilogue();
  54     }
  55 
  56     public static void prologue() {
  57         _beforeMillis = System.currentTimeMillis();
  58     }
  59 
  60     public static void epilogue() {
  61         _afterMillis = System.currentTimeMillis();
  62         if (_overflow) {
  63             System.out.println("Overflowed!");
  64         }
  65         final double deltaSecs = (_afterMillis - _beforeMillis) / 1000.0;
  66         final double freeMegs = ((double) _freeBytes) / (1024.0 * 1024.0);
  67         final double totalMegs = ((double) _totalBytes) / (1024.0 * 1024.0);
  68         final double memRatio = freeMegs / totalMegs;
  69         System.out.println("Runtime.freeMemory()/Runtime.totalMemory: " +
  70                            Long.toString(_freeBytes) +
  71                            "/" +
  72                            Long.toString(_totalBytes) +
  73                            " = " +
  74                            Double.toString(memRatio));
  75         System.out.println("That took: " +
  76                            Double.toString(deltaSecs) +
  77                            " seconds");
  78     }
  79 
  80     public static void fill() {
  81         boolean _overflow = false;
  82         Runtime rt = java.lang.Runtime.getRuntime();
  83         Random stream = Utils.getRandomInstance();
  84         Space next = null;
  85         try {
  86             for (long available = rt.freeMemory();
  87                  available > freeSpaceLimit;
  88                  available = rt.freeMemory()) {
  89                 long request   = (available - freeSpaceLimit) / 2;
  90                 int maxRequest = (int) Math.min(maxObjectSize, request);
  91                 int minRequest = (int) Math.max(minObjectSize, maxRequest);
  92                 int size = stream.nextInt(minRequest);
  93                 if (debug) {
  94                     System.err.println("available: " + Long.toString(available) +
  95                                        "  maxRequest: " + Integer.toString(maxRequest) +
  96                                        "  minRequest: " + Integer.toString(minRequest) +
  97                                        "  size: " + Integer.toString(size));
  98                 }
  99                 next = new Space(size, next);
 100             }
 101         } catch (OutOfMemoryError oome) {
 102             _overflow = true;
 103         }
 104         _freeBytes = rt.freeMemory();
 105         _totalBytes = rt.totalMemory();
 106     }
 107 
 108     static long    _beforeMillis = 0L;
 109     static long    _afterMillis  = 0L;
 110     static long    _freeBytes    = 0L;
 111     static long    _totalBytes   = 0L;
 112     static boolean _overflow     = false;
 113 }
 114 
 115 class Space {
 116     public Space(int bytes, Space next) {
 117         _next = next;
 118         _space = new byte[bytes];
 119     }
 120     private Space              _next  = null;
 121     private byte[]             _space = null;
 122 }