--- /dev/null 2017-09-26 09:07:01.073937575 -0700 +++ new/test/hotspot/jtreg/serviceability/jvmti/HeapMonitor/MyPackage/HeapMonitor.java 2017-10-13 09:58:56.121963001 -0700 @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2017, Google and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package MyPackage; + +import java.util.ArrayList; +import java.util.List; + +/** API for handling the underlying heap sampling monitoring system. */ +public class HeapMonitor { + static { + try { + System.loadLibrary("HeapMonitor"); + } catch (UnsatisfiedLinkError ule) { + System.err.println("Could not load HeapMonitor library"); + System.err.println("java.library.path: " + System.getProperty("java.library.path")); + throw ule; + } + } + + private static int g_tmp[]; + + /** Enable heap monitoring sampling given a rate and maximum garbage to keep in memory. */ + public native static void enableSampling(int rate, int maximumGarbage); + + /** Enable heap monitoring sampling given a rate. */ + public static void enableSampling(int rate) { + enableSampling(rate, 200); + } + + /** Enable heap monitoring sampling with default values for rate and maximum garbage. */ + public static void enableSampling() { + enableSampling(1 << 19); + } + + public native static void disableSampling(); + public native static boolean areSamplingStatisticsZero(); + + /** Do the frames provided exist in live, recent garbage, and frequent garbage. */ + public native static boolean framesExistEverywhere(Frame[] frames); + /** Do the frames provided not exist in live, recent garbage, and frequent garbage. */ + public native static boolean framesExistNowhere(Frame[] frames); + + /** + * Allocate memory but first create a stack trace of a particular depth. + * + * @return list of frames for the allocation. + */ + public static List allocate(int depth) { + List frames = new ArrayList(); + if (depth > 1) { + createStackDepth(depth - 1, frames); + frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 72)); + } else { + actuallyAllocate(frames); + frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 118)); + frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 75)); + } + return frames; + } + + /** + * Allocate memory but first create a stack trace. + * + * @return list of frames for the allocation. + */ + public static List allocate() { + int sum = 0; + List frames = new ArrayList(); + allocate(frames); + frames.add(new Frame("allocate", "()Ljava/util/List;", "HeapMonitor.java", 90)); + return frames; + } + + private static void createStackDepth(int depth, List frames) { + if (depth > 1) { + createStackDepth(depth - 1, frames); + frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 97)); + } else { + allocate(frames); + frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 100)); + } + } + + private static void allocate(List frames) { + int sum = 0; + for (int j = 0; j < 1000; j++) { + sum += actuallyAllocate(frames); + } + frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 118)); + frames.add(new Frame("allocate", "(Ljava/util/List;)V", "HeapMonitor.java", 108)); + } + + private static int actuallyAllocate(List frames) { + int sum = 0; + // Let us assume that the array is 24 bytes of memory. + for (int i = 0; i < 127000 / 6; i++) { + int tmp[] = new int[1]; + // Force it to be kept. + g_tmp = tmp; + sum += g_tmp[0]; + } + return sum; + } +}