< prev index next >

test/hotspot/jtreg/gc/z/TestUncommit.java

Print this page




  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 gc.z;
  25 
  26 /*
  27  * @test TestUncommit
  28  * @requires vm.gc.Z
  29  * @summary Test ZGC uncommit unused memory
  30  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+heap=debug,gc+stats=off -Xms128M -Xmx512M -XX:ZUncommitDelay=10 gc.z.TestUncommit true 2
  31  */
  32 
  33 /*
  34  * @test TestUncommit
  35  * @requires vm.gc.Z
  36  * @summary Test ZGC uncommit unused memory
  37  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+heap=debug,gc+stats=off -Xms512M -Xmx512M -XX:ZUncommitDelay=10 gc.z.TestUncommit false 1
  38  */
  39 
  40 /*
  41  * @test TestUncommit
  42  * @requires vm.gc.Z
  43  * @summary Test ZGC uncommit unused memory
  44  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+heap=debug,gc+stats=off -Xms128M -Xmx512M -XX:ZUncommitDelay=10 -XX:-ZUncommit gc.z.TestUncommit false 1
  45  */
  46 
  47 import java.util.ArrayList;

  48 
  49 public class TestUncommit {
  50     private static final int delay = 10; // seconds
  51     private static final int allocSize = 200 * 1024 * 1024; // 200M
  52     private static final int smallObjectSize = 4 * 1024; // 4K
  53     private static final int mediumObjectSize = 2 * 1024 * 1024; // 2M
  54     private static final int largeObjectSize = allocSize;
  55 
  56     private static volatile ArrayList<byte[]> keepAlive;
  57 







  58     private static long capacity() {
  59         return Runtime.getRuntime().totalMemory();
  60     }
  61 
  62     private static void allocate(int objectSize) {
  63         keepAlive = new ArrayList<>();
  64         for (int i = 0; i < allocSize; i+= objectSize) {
  65             keepAlive.add(new byte[objectSize]);
  66         }
  67     }
  68 
  69     private static void reclaim() {
  70         keepAlive = null;
  71         System.gc();
  72     }
  73 
  74     private static void test(boolean enabled, int objectSize) throws Exception {
  75         final var beforeAlloc = capacity();
  76 
  77         // Allocate memory

  78         allocate(objectSize);
  79 

  80         final var afterAlloc = capacity();
  81 
  82         // Reclaim memory

  83         reclaim();
  84 
  85         // Wait shorter than the uncommit delay
  86         Thread.sleep(delay * 1000 / 2);
  87 
  88         final var beforeUncommit = capacity();
  89 
  90         // Wait longer than the uncommit delay
  91         Thread.sleep(delay * 1000);






  92 

  93         final var afterUncommit = capacity();
  94 
  95         System.out.println("  Uncommit Enabled: " + enabled);
  96         System.out.println("    Uncommit Delay: " + delay);
  97         System.out.println("       Object Size: " + objectSize);
  98         System.out.println("        Alloc Size: " + allocSize);
  99         System.out.println("      Before Alloc: " + beforeAlloc);
 100         System.out.println("       After Alloc: " + afterAlloc);
 101         System.out.println("   Before Uncommit: " + beforeUncommit);
 102         System.out.println("    After Uncommit: " + afterUncommit);
 103         System.out.println();
 104 
 105         // Verify
 106         if (enabled) {
 107             if (beforeUncommit == beforeAlloc) {
 108                 throw new Exception("Uncommitted too fast");
 109             }
 110 
 111             if (afterUncommit >= afterAlloc) {
 112                 throw new Exception("Uncommitted too slow");
 113             }
 114 
 115             if (afterUncommit < beforeAlloc) {
 116                 throw new Exception("Uncommitted too much");
 117             }
 118 
 119             if (afterUncommit > beforeAlloc) {
 120                 throw new Exception("Uncommitted too little");
 121             }
 122         } else {
 123             if (afterAlloc > beforeUncommit ||
 124                 afterAlloc > afterUncommit) {
 125                 throw new Exception("Should not uncommit");
 126             }
 127         }






 128     }
 129 
 130     public static void main(String[] args) throws Exception {
 131         final boolean enabled = Boolean.parseBoolean(args[0]);
 132         final int iterations = Integer.parseInt(args[1]);
 133 
 134         for (int i = 0; i < iterations; i++) {
 135             System.out.println("Iteration " + i);
 136             test(enabled, smallObjectSize);
 137             test(enabled, mediumObjectSize);
 138             test(enabled, largeObjectSize);
 139         }
 140     }
 141 }


  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 gc.z;
  25 
  26 /*
  27  * @test TestUncommit
  28  * @requires vm.gc.Z
  29  * @summary Test ZGC uncommit unused memory
  30  * @library /test/lib
  31  * @run main/othervm -XX:+UseZGC -Xlog:gc*,gc+heap=debug,gc+stats=off -Xms128M -Xmx512M -XX:ZUncommitDelay=10 gc.z.TestUncommit













  32  */
  33 
  34 import java.util.ArrayList;
  35 import jdk.test.lib.Utils;
  36 
  37 public class TestUncommit {
  38     private static final int delay = 10 * 1000; // milliseconds
  39     private static final int allocSize = 200 * 1024 * 1024; // 200M
  40     private static final int smallObjectSize = 4 * 1024; // 4K
  41     private static final int mediumObjectSize = 2 * 1024 * 1024; // 2M
  42     private static final int largeObjectSize = allocSize;
  43 
  44     private static volatile ArrayList<byte[]> keepAlive;
  45 
  46     private static final long startTime = System.nanoTime();
  47 
  48     private static void log(String msg) {
  49         final String elapsedSeconds = String.format("%.3fs", (System.nanoTime() - startTime) / 1_000_000_000.0);
  50         System.out.println("[" + elapsedSeconds + "] (" + Thread.currentThread().getName() + ") " + msg);
  51     }
  52 
  53     private static long capacity() {
  54         return Runtime.getRuntime().totalMemory();
  55     }
  56 
  57     private static void allocate(int objectSize) {
  58         keepAlive = new ArrayList<>();
  59         for (int i = 0; i < allocSize; i+= objectSize) {
  60             keepAlive.add(new byte[objectSize]);
  61         }
  62     }
  63 
  64     private static void reclaim() {
  65         keepAlive = null;
  66         System.gc();
  67     }
  68 
  69     private static void test(int objectSize) throws Exception {
  70         final var beforeAlloc = capacity();
  71 
  72         // Allocate memory
  73         log("Allocating");
  74         allocate(objectSize);
  75 
  76         final var timeAfterAlloc = System.nanoTime();
  77         final var afterAlloc = capacity();
  78 
  79         // Reclaim memory
  80         log("Reclaiming");
  81         reclaim();
  82 
  83         log("Waiting for uncommit to start");
  84         while (capacity() >= afterAlloc) {
  85             Thread.sleep(1000);
  86         }
  87 
  88         log("Uncommit started");
  89         final var timeUncommitStart = System.nanoTime();
  90         final var actualDelay = (timeUncommitStart - timeAfterAlloc) / 1_000_000;
  91 
  92         log("Waiting for uncommit to complete");
  93         while (capacity() > beforeAlloc) {
  94             Thread.sleep(1000);
  95         }
  96 
  97         log("Uncommit completed");
  98         final var afterUncommit = capacity();
  99 
 100         log("        Uncommit Delay: " + delay);
 101         log("           Object Size: " + objectSize);
 102         log("            Alloc Size: " + allocSize);
 103         log("          Before Alloc: " + beforeAlloc);
 104         log("           After Alloc: " + afterAlloc);
 105         log("        After Uncommit: " + afterUncommit);
 106         log(" Actual Uncommit Delay: " + actualDelay);


 107 
 108         // Verify
 109         if (actualDelay < delay) {
 110             throw new Exception("Uncommitted too fast");
 111         }
 112 
 113         if (actualDelay > delay * 2 * Utils.TIMEOUT_FACTOR) {
 114             throw new Exception("Uncommitted too slow");
 115         }
 116 
 117         if (afterUncommit < beforeAlloc) {
 118             throw new Exception("Uncommitted too much");











 119         }
 120 
 121         if (afterUncommit > beforeAlloc) {
 122             throw new Exception("Uncommitted too little");
 123         }
 124 
 125         log("Success");
 126     }
 127 
 128     public static void main(String[] args) throws Exception {
 129         for (int i = 0; i < 2; i++) {
 130             log("Iteration " + i);
 131             test(smallObjectSize);
 132             test(mediumObjectSize);
 133             test(largeObjectSize);



 134         }
 135     }
 136 }
< prev index next >