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 HeapMonitorThreadOnOffTest.java
  31  * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorThreadOnOffTest
  32  */
  33 
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 
  37 public class HeapMonitorThreadOnOffTest {
  38   public static void main(String[] args) {
  39     final int numThreads = 24;
  40     ArrayList<Thread> list = new ArrayList<>();
  41 
  42     // Add one thread that consistently turns on/off the sampler to ensure correctness with
  43     // potential resets.
  44     Switch switchPlayer = new Switch();
  45     Thread switchThread = new Thread(switchPlayer, "Switch Player");
  46     switchThread.start();
  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     switchPlayer.stop();
  63     try {
  64       switchThread.join();
  65     } catch(InterruptedException e) {
  66       throw new RuntimeException("Thread got interrupted while waiting for the switch player...");
  67     }
  68 
  69     // We don't check here for correctness of data. If we made it here, the test succeeded:
  70     //  Threads can allocate like crazy
  71     //  Other threads can turn on/off the system
  72   }
  73 }
  74 
  75 class Allocator implements Runnable {
  76   private int depth;
  77   private volatile int tmp[];
  78 
  79   public Allocator(int depth) {
  80     this.depth = depth;
  81   }
  82 
  83   private int helper() {
  84     int sum = 0;
  85     // Let us assume that the array is 24 bytes of memory.
  86     for (int i = 0; i < 127000 / 6; i++) {
  87       int newTmp[] = new int[1];
  88       // Force it to be kept.
  89       tmp = newTmp;
  90       sum += tmp[0];
  91     }
  92     return sum;
  93   }
  94 
  95   private int recursiveWrapper(int depth) {
  96     if (depth > 0) {
  97       return recursiveWrapper(depth - 1);
  98     }
  99     return helper();
 100   }
 101 
 102   public void run() {
 103     int sum = 0;
 104     for (int j = 0; j < 500; j++) {
 105       sum += recursiveWrapper(depth);
 106     }
 107   }
 108 }
 109 
 110 class Switch implements Runnable {
 111   private volatile boolean keepGoing;
 112 
 113   public Switch() {
 114     keepGoing = true;
 115   }
 116 
 117   public void stop() {
 118     keepGoing = false;
 119   }
 120 
 121   public void run() {
 122     while (keepGoing) {
 123       HeapMonitor.disableSamplingRate();
 124       HeapMonitor.resetEventStorage();
 125       HeapMonitor.enableSamplingRate();
 126     }
 127   }
 128 }