1 /* 2 * Copyright (c) 2017, 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 * @summary Verifies the JVMTI Heap Monitor Thread sanity 29 * @compile HeapMonitorThreadOnOffTest.java 30 * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorThreadOnOffTest 31 */ 32 33 import java.io.PrintStream; 34 import java.util.ArrayList; 35 import java.util.List; 36 37 public class HeapMonitorThreadOnOffTest { 38 static { 39 try { 40 System.loadLibrary("HeapMonitor"); 41 } catch (UnsatisfiedLinkError ule) { 42 System.err.println("Could not load HeapMonitor library"); 43 System.err.println("java.library.path: " 44 + System.getProperty("java.library.path")); 45 throw ule; 46 } 47 } 48 49 public static void main(String[] args) { 50 final int numThreads = 24; 51 ArrayList<Thread> list = new ArrayList<>(); 52 53 // Add one thread that consistently turns on/off the sampler to ensure correctness with 54 // potential resets. 55 Switch switchPlayer = new Switch(); 56 Thread switchThread = new Thread(switchPlayer, "Switch Player"); 57 switchThread.start(); 58 59 for (int i = 0 ; i < numThreads; i++) { 60 Thread thread = new Thread(new Allocator(i), "Allocator" + i); 61 thread.start(); 62 list.add(thread); 63 } 64 65 for (Thread elem : list) { 66 try { 67 elem.join(); 68 } catch(InterruptedException e) { 69 throw new RuntimeException("Thread got interrupted..."); 70 } 71 } 72 73 switchPlayer.stop(); 74 try { 75 switchThread.join(); 76 } catch(InterruptedException e) { 77 throw new RuntimeException("Thread got interrupted while waiting for the switch player..."); 78 } 79 80 // We don't check here for correctness of data. If we made it here, the test succeeded: 81 // Threads can allocate like crazy 82 // Other threads can turn on/off the system 83 } 84 } 85 86 class Allocator implements Runnable { 87 private int depth; 88 private int g_tmp[]; 89 90 public Allocator(int depth) { 91 this.depth = depth; 92 } 93 94 private int helper() { 95 int sum = 0; 96 // Let us assume that the array is 24 bytes of memory. 97 for (int i = 0; i < 127000 / 6; i++) { 98 int tmp[] = new int[1]; 99 // Force it to be kept. 100 g_tmp = tmp; 101 sum += g_tmp[0]; 102 } 103 return sum; 104 } 105 106 private int recursiveWrapper(int depth) { 107 if (depth > 0) { 108 return recursiveWrapper(depth - 1); 109 } 110 return helper(); 111 } 112 113 public void run() { 114 int sum = 0; 115 System.err.println("Runner with " + depth + " depth."); 116 for (int j = 0; j < 500; j++) { 117 sum += recursiveWrapper(depth); 118 } 119 System.err.println("Ran with " + depth + " depth and sum is " + sum); 120 } 121 } 122 123 class Switch implements Runnable { 124 private volatile boolean keepGoing; 125 126 private native static void enableSampling(); 127 private native static void disableSampling(); 128 129 public Switch() { 130 keepGoing = true; 131 } 132 133 public void stop() { 134 keepGoing = false; 135 } 136 137 public void run() { 138 int cnt = 0; 139 System.err.println("Going to turned on/off sampling."); 140 while (keepGoing) { 141 disableSampling(); 142 enableSampling(); 143 cnt++; 144 } 145 System.err.println("Turned on/off " + cnt + " times."); 146 } 147 }