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  * @summary Verifies the JVMTI Heap Monitor sampling rate average.
  29  * @build Frame HeapMonitor
  30  * @compile HeapMonitorStatRateTest.java
  31  * @requires vm.compMode != "Xcomp"
  32  * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorStatRateTest
  33  */
  34 
  35 public class HeapMonitorStatRateTest {
  36 
  37   private native static double getAverageRate();
  38 
  39   private static boolean testRateOnce(int rate, boolean throwIfFailure) {
  40     HeapMonitor.resetEventStorage();
  41     HeapMonitor.setSamplingRate(rate);
  42 
  43     HeapMonitor.enableSamplingEvents();
  44 
  45     int allocationTotal = 10 * 1024 * 1024;
  46     HeapMonitor.allocateSize(allocationTotal);
  47 
  48     HeapMonitor.disableSamplingEvents();
  49 
  50     double actualCount = HeapMonitor.getEventStorageElementCount();
  51     double expectedCount = allocationTotal / rate;
  52 
  53     double error = Math.abs(actualCount - expectedCount);
  54     double errorPercentage = error / expectedCount * 100;
  55 
  56     boolean failure = (errorPercentage > 10.0);
  57 
  58     if (failure && throwIfFailure) {
  59       throw new RuntimeException("Rate average over 10% for rate " + rate + " -> " + actualCount
  60           + ", " + expectedCount);
  61     }
  62 
  63     return failure;
  64   }
  65 
  66 
  67   private static void testRate(int rate) {
  68     // Test the rate twice, it can happen that the test is "unlucky" and the rate just goes above
  69     // the 10% mark. So try again to squash flakiness.
  70     // Flakiness is due to the fact that this test is dependent on the sampling rate, which is a
  71     // statistical geometric variable around the sampling rate. This means that the test could be
  72     // unlucky and not achieve the mean average fast enough for the test case.
  73     if (!testRateOnce(rate, false)) {
  74       testRateOnce(rate, true);
  75     }
  76   }
  77 
  78   public static void main(String[] args) {
  79     int[] tab = {1024, 8192};
  80 
  81     for (int rateIdx = 0; rateIdx < tab.length; rateIdx++) {
  82       testRate(tab[rateIdx]);
  83     }
  84   }
  85 }