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 the JVMTI Heap Monitor API
  32  * @build Frame HeapMonitor
  33  * @compile HeapMonitorObjectTest.java
  34  * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorObjectTest
  35  */
  36 
  37 public class HeapMonitorObjectTest {
  38 
  39   public static HeapMonitorObjectTest rememberIt;
  40 
  41   private static List<Frame> allocateObjects() {
  42     int sum = 0;
  43     List<Frame> frames = new ArrayList<Frame>();
  44     allocateObjects(frames);
  45     frames.add(new Frame("allocateObjects", "()Ljava/util/List;", "HeapMonitorObjectTest.java", 44));
  46     return frames;
  47   }
  48 
  49   private static void allocateObjects(List<Frame> frames) {
  50     for (int j = 0; j < 1000; j++) {
  51       actuallyAllocateObjects();
  52     }
  53     frames.add(new Frame("actuallyAllocateObjects", "()V", "HeapMonitorObjectTest.java", 59));
  54     frames.add(new Frame("allocateObjects", "(Ljava/util/List;)V", "HeapMonitorObjectTest.java", 51));
  55   }
  56 
  57   private static void actuallyAllocateObjects() {
  58     for (int j = 0; j < 512 * 256; j++) {
  59       rememberIt = new HeapMonitorObjectTest();
  60     }
  61   }
  62 
  63   public static void main(String[] args) {
  64     if (!HeapMonitor.eventStorageIsEmpty()) {
  65       throw new RuntimeException("Storage is not empty at test start...");
  66     }
  67 
  68     HeapMonitor.enableSampling();
  69     List<Frame> frameList = allocateObjects();
  70     frameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorObjectTest.java", 69));
  71 
  72     Frame[] frames = frameList.toArray(new Frame[0]);
  73     if (!HeapMonitor.obtainedEvents(frames)) {
  74       throw new RuntimeException("Events not found with the right frames.");
  75     }
  76 
  77     HeapMonitor.disableSampling();
  78     HeapMonitor.resetEventStorage();
  79     if (!HeapMonitor.eventStorageIsEmpty()) {
  80       throw new RuntimeException("Storage is not empty after reset.");
  81     }
  82 
  83     HeapMonitor.allocate();
  84     if (!HeapMonitor.eventStorageIsEmpty()) {
  85       throw new RuntimeException("Storage is not empty after allocation while disabled.");
  86     }
  87   }
  88 }