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 /** 27 * @test 28 * @build Frame HeapMonitor 29 * @summary Verifies the JVMTI Heap Monitor Thread sanity 30 * @compile HeapMonitorThreadTest.java 31 * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorThreadTest 32 */ 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 public class HeapMonitorThreadTest { 38 private native static boolean checkSamples(int numThreads); 39 40 public static void main(String[] args) { 41 final int numThreads = 24; 42 ArrayList<Thread> list = new ArrayList<>(); 43 44 // Remember a lot of garbage to have space for all thread samples. 45 HeapMonitor.enableSamplingRate(); 46 HeapMonitor.setGarbageHistory(10000); 47 48 for (int i = 0 ; i < numThreads; i++) { 49 Thread thread = new Thread(new Allocator(i), "Allocator" + i); 50 thread.start(); 51 list.add(thread); 52 } 53 54 for (Thread elem : list) { 55 try { 56 elem.join(); 57 } catch(InterruptedException e) { 58 throw new RuntimeException("Thread got interrupted..."); 59 } 60 } 61 62 if (!checkSamples(numThreads)) { 63 throw new RuntimeException("Problem with checkSamples..."); 64 } 65 } 66 } 67 68 class Allocator implements Runnable { 69 private int depth; 70 private volatile int tmp[]; 71 72 public Allocator(int depth) { 73 this.depth = depth; 74 } 75 76 private int helper() { 77 int sum = 0; 78 // Let us assume that the array is 24 bytes of memory. 79 for (int i = 0; i < 127000 / 6; i++) { 80 int newTmp[] = new int[1]; 81 // Force it to be kept. 82 tmp = newTmp; 83 sum += tmp[0]; 84 } 85 return sum; 86 } 87 88 private int recursiveWrapper(int depth) { 89 if (depth > 0) { 90 return recursiveWrapper(depth - 1); 91 } 92 return helper(); 93 } 94 95 public void run() { 96 int sum = 0; 97 for (int j = 0; j < 50; j++) { 98 sum += recursiveWrapper(depth); 99 } 100 System.out.println(sum); 101 } 102 }