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:HeapMonitorTest 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     HeapMonitor.enableSamplingEvents();
  50 
  51     for (int currentSize : sizes) {
  52       System.out.println("Testing size " + currentSize);
  53 
  54       HeapMonitor.resetEventStorage();
  55       if (!HeapMonitor.eventStorageIsEmpty()) {
  56         throw new RuntimeException("Should not have any events stored yet.");
  57       }
  58 
  59       // 111 is as good a number as any.
  60       final int samplingMultiplier = 111;
  61       HeapMonitor.setSamplingRate(samplingMultiplier * currentSize);
  62 
  63       allocate(currentSize);
  64 
  65       // For simplifications, we ignore the array memory usage for array internals (with the array
  66       // sizes requested, it should be a negligible oversight).
  67       //
  68       // That means that with maxIterations, the loop in the method allocate requests:
  69       //    maxIterations * currentSize * 4 bytes (4 for integers)
  70       //
  71       // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes.
  72       //
  73       // Therefore, the expected sample number is:
  74       //   (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize);
  75       double expected = maxIteration;
  76       expected *= 4;
  77       expected /= samplingMultiplier;
  78 
  79       // 10% error ensures a sanity test without becoming flaky.
  80       // Flakiness is due to the fact that this test is dependent on the sampling rate, which is a
  81       // statistical geometric variable around the sampling rate. This means that the test could be
  82       // unlucky and not achieve the mean average fast enough for the test case.
  83       if (!HeapMonitor.statsHaveExpectedNumberSamples((int) expected, 10)) {
  84         throw new RuntimeException("Statistics should show about " + expected + " samples.");
  85       }
  86     }
  87   }
  88 }