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, no sampled events are 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 boolean enableVMEvents();
  39   private static native boolean vmStorageContained(Frame[] frames);
  40   private static native int sampledEvents();
  41   private static native int vmEvents();
  42   private static Object[] array;
  43 
  44   private static void cloneObjects(int iterations) {
  45     HeapMonitorVMEventsTest object = new HeapMonitorVMEventsTest();
  46     for (int i = 0; i < iterations; i++) {
  47       try {
  48         array[i] = object.clone();
  49       } catch (Exception e) {
  50         // Nop.
  51       }
  52     }
  53   }
  54 
  55   public Object clone() throws CloneNotSupportedException {
  56     return super.clone();
  57   }
  58 
  59   public static void main(String[] args) {
  60     final int iterations = 1 << 10;
  61     array = new Object[iterations];
  62 
  63     if (!HeapMonitor.eventStorageIsEmpty()) {
  64       throw new RuntimeException("Storage is not empty at test start...");
  65     }
  66 
  67     HeapMonitor.enableSamplingEvents();
  68     HeapMonitor.setSamplingRate(0);
  69     // Heat up the tlab to start sampling at the right rate.
  70     for (int i = 0; i < 100; i++) {
  71       cloneObjects(iterations);
  72     }
  73 
  74     HeapMonitor.resetEventStorage();
  75     cloneObjects(iterations);
  76 
  77     int onlySampleCount = sampledEvents();
  78 
  79     HeapMonitor.disableSamplingEvents();
  80     enableVMEvents();
  81     HeapMonitor.resetEventStorage();
  82     if (!HeapMonitor.eventStorageIsEmpty()) {
  83       throw new RuntimeException("Storage is not empty after reset.");
  84     }
  85 
  86     HeapMonitor.enableSamplingEvents();
  87     cloneObjects(iterations);
  88 
  89     int sampleCount = sampledEvents();
  90     int vmCount = vmEvents();
  91 
  92     double diff = Math.abs(onlySampleCount - sampleCount - vmCount) * 100;
  93     diff /= onlySampleCount;
  94 
  95     // Accept a 10% error rate.
  96     if (diff > 10) {
  97       throw new RuntimeException("Error rate is over the accepted rate.");
  98     }
  99   }
 100 }