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