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  * @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[] threads);
  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.enableSampling(1 << 19, 10000);
  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     int[] threads = new int[numThreads];
  62     if (!checkSamples(threads)) {
  63       throw new RuntimeException("Problem with checkSamples...");
  64     }
  65 
  66     for (int elem : threads) {
  67       if (elem == 0) {
  68         throw new RuntimeException("Missing at least one thread in the array...");
  69       }
  70     }
  71   }
  72 }
  73 
  74 class Allocator implements Runnable {
  75   private int depth;
  76   private volatile int tmp[];
  77 
  78   public Allocator(int depth) {
  79     this.depth = depth;
  80   }
  81 
  82   private int helper() {
  83     int sum = 0;
  84     // Let us assume that the array is 24 bytes of memory.
  85     for (int i = 0; i < 127000 / 6; i++) {
  86       int newTmp[] = new int[1];
  87       // Force it to be kept.
  88       tmp = newTmp;
  89       sum += tmp[0];
  90     }
  91     return sum;
  92   }
  93 
  94   private int recursiveWrapper(int depth) {
  95     if (depth > 0) {
  96       return recursiveWrapper(depth - 1);
  97     }
  98     return helper();
  99   }
 100 
 101   public void run() {
 102     int sum = 0;
 103     for (int j = 0; j < 50; j++) {
 104       sum += recursiveWrapper(depth);
 105     }
 106     System.out.println(sum);
 107   }
 108 }