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 HeapMonitorStatArrayCorrectnessTest.java
  31  * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatArrayCorrectnessTest
  32  */
  33 
  34 public class HeapMonitorStatArrayCorrectnessTest {
  35 
  36   // Do 100000 iterations and expect maxIteration / multiplier samples.
  37   private static final int maxIteration = 100000;
  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};
  48 
  49     for (int currentSize : sizes) {
  50       System.out.println("Testing size " + currentSize);
  51 
  52       if (!HeapMonitor.eventStorageIsEmpty()) {
  53         throw new RuntimeException("Should not have any events stored yet.");
  54       }
  55 
  56       // 111 is as good a number as any.
  57       final int samplingMultiplier = 111;
  58       HeapMonitor.setSamplingRate(samplingMultiplier * currentSize);
  59 
  60       allocate(currentSize);
  61 
  62       // For simplifications, we ignore the array memory usage for array internals (with the array
  63       // sizes requested, it should be a negligible oversight).
  64       //
  65       // That means that with maxIterations, the loop in the method allocate requests:
  66       //    maxIterations * currentSize * 4 bytes (4 for integers)
  67       //
  68       // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes.
  69       //
  70       // Therefore, the expected sample number is:
  71       //   (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize);
  72       double expected = maxIteration;
  73       expected *= 4;
  74       expected /= samplingMultiplier;
  75 
  76       // 10% error ensures a sanity test without becoming flaky.
  77       if (!HeapMonitor.statsHaveExpectedNumberSamples((int) expected, 10)) {
  78         throw new RuntimeException("Statistics should show about " + expected + " samples.");
  79       }
  80 
  81       HeapMonitor.resetEventStorage();
  82     }
  83   }
  84 }