/* * Copyright (c) 2018, 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; /** * @test * @build Frame HeapMonitor * @summary Verifies the JVMTI Heap Monitor Thread information sanity * @compile HeapMonitorThreadTest.java * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorThreadTest */ import java.util.ArrayList; import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class HeapMonitorThreadTest { private native static boolean checkSamples(int numThreads); public static void main(String[] args) { final int numThreads = 24; ArrayList threadList = new ArrayList<>(); HeapMonitor.enableSamplingEvents(); // The test creates numThreads threads, each allocate a lot. Then they inform the main thread // they are done allocating. The test looks if all threads are available in the collected event // information and then proceeds to tell each thread to stop running before leaving the test. for (int i = 0 ; i < numThreads; i++) { BlockingQueue queue = new LinkedBlockingQueue<>(); Allocator allocator = new Allocator(i, queue); Thread thread = new Thread(allocator, "Allocator" + i); thread.start(); ThreadInformation info = new ThreadInformation(thread, queue, allocator); threadList.add(info); } // Wait until all threads have put an object in the queue. for (ThreadInformation info : threadList) { info.waitForAllocations(); } if (!checkSamples(numThreads)) { throw new RuntimeException("Problem with checkSamples..."); } // Now inform each thread we are done and wait for them to be done. for (ThreadInformation info : threadList) { info.stop(); } } } class ThreadInformation { private Thread thread; private BlockingQueue finishedAllocationQueue; private Allocator allocator; public ThreadInformation(Thread thread, BlockingQueue finishedAllocationQueue, Allocator allocator) { this.thread = thread; this.finishedAllocationQueue = finishedAllocationQueue; this.allocator = allocator; } public void waitForAllocations() { try { finishedAllocationQueue.take(); } catch(InterruptedException e) { throw new RuntimeException("Thread got interrupted..."); } } public void stop() { try { allocator.stopRun(); thread.join(); if (!allocator.endedNormally()) { throw new RuntimeException("Thread did not end normally..."); } } catch(InterruptedException e) { throw new RuntimeException("Thread got interrupted..."); } } } class Allocator implements Runnable { private int depth; private List currentList; private BlockingQueue finishedAllocationQueue; private BlockingQueue jobCanStop; private boolean failed; public Allocator(int depth, BlockingQueue finishedAllocationQueue) { jobCanStop = new LinkedBlockingQueue<>(); this.finishedAllocationQueue = finishedAllocationQueue; this.depth = depth; } public boolean endedNormally() { return !failed; } private void helper() { List newList = new ArrayList<>(); // Let us assume that the array is 24 bytes of memory, by default we sample at 512k, keep in // memory at least 2MB without counting the link-list itself, which adds to this. int iterations = (1 << 21) / 24; for (int i = 0; i < iterations; i++) { int newTmp[] = new int[1]; // Force it to be kept. newList.add(newTmp); } // Replace old list with new list, which provokes two things: // Old list will get GC'd at some point. // New list forces that this thread has some allocations still sampled. currentList = newList; } private void recursiveWrapper(int depth) { if (depth > 0) { recursiveWrapper(depth - 1); } helper(); } public void stopRun() throws InterruptedException { jobCanStop.put(new Object()); } public void run() { for (int j = 0; j < 50; j++) { recursiveWrapper(depth); } try { // Tell the main thread we are done. finishedAllocationQueue.put(new Object()); // Wait until the main thread says we can stop. jobCanStop.take(); } catch (InterruptedException e) { failed = true; } } }