1 /*
   2  * Copyright (c) 2018, Google 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 MyPackage;
  25 
  26 /**
  27  * @test
  28  * @build Frame HeapMonitor
  29  * @summary Verifies the JVMTI Heap Monitor rate when allocating arrays.
  30  * @compile HeapMonitorArrayAllSampledTest.java
  31  * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorArrayAllSampledTest
  32  */
  33 
  34 public class HeapMonitorArrayAllSampledTest {
  35 
  36   // Do 1000 iterations and expect maxIteration samples.
  37   private static final int maxIteration = 1000;
  38   private static int array[];
  39 
  40   private static void allocate(int size) {
  41     for (int j = 0; j < maxIteration; j++) {
  42       array = new int[size];
  43     }
  44   }
  45 
  46   public static void main(String[] args) {
  47     int sizes[] = {1000, 10000, 100000, 1000000};
  48 
  49     HeapMonitor.setSamplingRate(0);
  50     HeapMonitor.enableSamplingEvents();
  51 
  52     for (int currentSize : sizes) {
  53       System.out.println("Testing size " + currentSize);
  54 
  55       HeapMonitor.resetEventStorage();
  56       allocate(currentSize);
  57 
  58       // 10% error ensures a sanity test without becoming flaky.
  59       // Flakiness is due to the fact that this test is dependent on the sampling rate, which is a
  60       // statistical geometric variable around the sampling rate. This means that the test could be
  61       // unlucky and not achieve the mean average fast enough for the test case.
  62       if (!HeapMonitor.statsHaveExpectedNumberSamples(maxIteration, 10)) {
  63         throw new RuntimeException("Statistics should show about " + maxIteration + " samples.");
  64       }
  65     }
  66   }
  67 }