1 /*
   2  * Copyright (c) 2017, 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  * @summary Verifies the JVMTI Heap Monitor Statistics
  29  * @compile HeapMonitorStatCorrectnessTest.java
  30  * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorStatCorrectnessTest
  31  */
  32 
  33 public class HeapMonitorStatCorrectnessTest {
  34 
  35   static {
  36     try {
  37       System.loadLibrary("HeapMonitor");
  38     } catch (UnsatisfiedLinkError ule) {
  39       System.err.println("Could not load HeapMonitor library");
  40       System.err.println("java.library.path: "
  41           + System.getProperty("java.library.path"));
  42       throw ule;
  43     }
  44   }
  45 
  46   // Do 100000 iterations and expect maxIteration / multiplier samples.
  47   private static final int maxIteration = 100000;
  48   private static int array[];
  49 
  50   private native static int statsNull();
  51   private native static int statsHaveSamples(int expected, int percentError);
  52   private native static int enableSampling(int rate);
  53   private native static int disableSampling();
  54 
  55   private static void allocate(int size) {
  56     System.out.println("With a size of " + size + ", execute " + maxIteration + " iterations");
  57     for (int j = 0; j < maxIteration; j++) {
  58       array = new int[size];
  59     }
  60   }
  61 
  62   public static void main(String[] args) {
  63     int sizes[] = {1000, 10000, 100000};
  64 
  65     for (int i = 0; i < sizes.length; i++) {
  66       int currentSize = sizes[i];
  67       System.out.println("Testing size " + currentSize);
  68 
  69       // 111 is as good a number as any.
  70       final int samplingMultiplier = 111;
  71       enableSampling(samplingMultiplier * currentSize);
  72 
  73       if (statsNull() == 0) {
  74         throw new RuntimeException("Statistics should be null to begin with.");
  75       }
  76 
  77       allocate(currentSize);
  78 
  79       // For simplifications, we ignore the array memory usage for array internals (with the array
  80       // sizes requested, it should be a negligible oversight).
  81       //
  82       // That means that with maxIterations, the loop in the method allocate requests:
  83       //    maxIterations * currentSize * 4 bytes (4 for integers)
  84       //
  85       // Via the enable sampling, the code requests a sample every samplingMultiplier * currentSize bytes.
  86       //
  87       // Therefore, the expected sample number is:
  88       //   (maxIterations * currentSize * 4) / (samplingMultiplier * currentSize);
  89       double expected = maxIteration;
  90       expected *= 4;
  91       expected /= samplingMultiplier;
  92 
  93       // 10% error ensures a sanity test without becoming flaky.
  94       if (statsHaveSamples((int) expected, 10) != 0) {
  95         throw new RuntimeException("Statistics should show about " + expected + " samples.");
  96       }
  97 
  98       disableSampling();
  99     }
 100   }
 101 }