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.enableSamplingEvents(); 46 47 for (int i = 0 ; i < numThreads; i++) { 48 Thread thread = new Thread(new Allocator(i), "Allocator" + i); 49 thread.start(); 50 list.add(thread); 51 } 52 53 for (Thread elem : list) { 54 try { 55 elem.join(); 56 } catch(InterruptedException e) { 57 throw new RuntimeException("Thread got interrupted..."); 58 } 59 } 60 61 if (!checkSamples(numThreads)) { 62 throw new RuntimeException("Problem with checkSamples..."); 63 } 64 } 65 } 66 67 class Allocator implements Runnable { 68 private int depth; 69 private List<int[]> currentList; 70 71 public Allocator(int depth) { 72 this.depth = depth; 73 } 74 75 private void helper() { 76 List<int[]> newList = new ArrayList<>(); 77 // Let us assume that the array is 24 bytes of memory, by default we sample at 512k, keep in 78 // memory at least 2MB without counting the link-list itself, which adds to this. 79 int iterations = (1 << 21) / 24; 80 for (int i = 0; i < iterations; i++) { 81 int newTmp[] = new int[1]; 82 // Force it to be kept. 83 newList.add(newTmp); 84 } 85 86 // Replace old list with new list, which provokes two things: 87 // Old list will get GC'd at some point. 88 // New list forces that this thread has some allocations still sampled. 89 currentList = newList; 90 } 91 92 private void recursiveWrapper(int depth) { 93 if (depth > 0) { 94 recursiveWrapper(depth - 1); 95 } 96 helper(); 97 } 98 99 public void run() { 100 for (int j = 0; j < 50; j++) { 101 recursiveWrapper(depth); 102 } 103 } 104 }