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.List;
  27 
  28 /**
  29  * @test
  30  * @summary Verifies if turning off the event notification stops events.
  31  * @build Frame HeapMonitor
  32  * @compile HeapMonitorEventOnOffTest.java
  33  * @run main/othervm/native -agentlib:HeapMonitorTest MyPackage.HeapMonitorEventOnOffTest
  34  */
  35 public class HeapMonitorEventOnOffTest {
  36   private static void checkNoEventsAreBeingSent() {
  37     HeapMonitor.resetEventStorage();
  38     HeapMonitor.repeatAllocate(10);
  39 
  40     // Check that the data is not available while heap sampling is disabled.
  41     boolean status = HeapMonitor.eventStorageIsEmpty();
  42     if (!status) {
  43       throw new RuntimeException("Storage is not empty after allocating with disabled events.");
  44     }
  45   }
  46 
  47   private static void checkEventsAreBeingSent() {
  48     List<Frame> frameList = HeapMonitor.repeatAllocate(10);
  49 
  50     frameList.add(new Frame("checkEventsAreBeingSent", "()V", "HeapMonitorEventOnOffTest.java", 48));
  51     Frame[] frames = frameList.toArray(new Frame[0]);
  52 
  53     // Check that the data is available while heap sampling is enabled.
  54     boolean status = HeapMonitor.obtainedEvents(frames);
  55     if (!status) {
  56       throw new RuntimeException("Failed to find the traces after allocating with enabled events.");
  57     }
  58   }
  59 
  60   public static void main(String[] args) {
  61     HeapMonitor.enableSamplingEvents();
  62     checkEventsAreBeingSent();
  63 
  64     // Disabling the notification system should stop events.
  65     HeapMonitor.disableSamplingEvents();
  66     checkNoEventsAreBeingSent();
  67 
  68     // Enabling the notification system should start events again.
  69     HeapMonitor.enableSamplingEvents();
  70     checkEventsAreBeingSent();
  71   }
  72 }