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 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 /**
  30  * @test
  31  * @summary Verifies that when the VM event is sent, sampled events are also collected.
  32  * @build Frame HeapMonitor
  33  * @compile HeapMonitorVMEventsTest.java
  34  * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorVMEventsTest
  35  */
  36 
  37 public class HeapMonitorVMEventsTest implements Cloneable {
  38   private static native int vmEvents();
  39   private static Object[] array;
  40 
  41   private static void cloneObjects(int iterations) {
  42     HeapMonitorVMEventsTest object = new HeapMonitorVMEventsTest();
  43     for (int i = 0; i < iterations; i++) {
  44       try {
  45         array[i] = object.clone();
  46       } catch (Exception e) {
  47         // Nop.
  48       }
  49     }
  50   }
  51 
  52   public Object clone() throws CloneNotSupportedException {
  53     return super.clone();
  54   }
  55 
  56   private static void checkDifference(int first, int second) {
  57     double diff = Math.abs(first - second) * 100;
  58     diff /= first;
  59 
  60     // Accept a 10% error rate: with objects being allocated: this allows a bit of room in
  61     // case other items are getting allocated during the test.
  62     if (diff > 10) {
  63       throw new RuntimeException("Error rate is over the accepted rate: " + diff
  64           + ": " + first + " , " + second);
  65     }
  66   }
  67 
  68   private static void compareSampledAndVM() {
  69     final int iterations = 1024;
  70     HeapMonitor.resetEventStorage();
  71     cloneObjects(iterations);
  72 
  73     int onlySampleCount = HeapMonitor.sampledEvents();
  74 
  75     HeapMonitor.enableVMEvents();
  76     HeapMonitor.resetEventStorage();
  77     if (!HeapMonitor.eventStorageIsEmpty()) {
  78       throw new RuntimeException("Storage is not empty after reset.");
  79     }
  80 
  81     cloneObjects(iterations);
  82 
  83     int sampleCount = HeapMonitor.sampledEvents();
  84     int vmCount = vmEvents();
  85 
  86     System.err.println("Obtained: " + onlySampleCount + " - " + sampleCount + " - "  + vmCount);
  87     checkDifference(onlySampleCount, sampleCount);
  88     checkDifference(onlySampleCount, vmCount);
  89   }
  90 
  91   public static void main(String[] args) {
  92     final int iterations = 1 << 10;
  93     array = new Object[iterations];
  94 
  95     if (!HeapMonitor.eventStorageIsEmpty()) {
  96       throw new RuntimeException("Storage is not empty at test start...");
  97     }
  98 
  99     HeapMonitor.sampleEverything();
 100     compareSampledAndVM();
 101   }
 102 }