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 import java.util.ArrayList;
  27 import java.util.List;
  28 
  29 /** API for handling the underlying heap sampling monitoring system. */
  30 public class HeapMonitor {
  31   static {
  32     try {
  33       System.loadLibrary("HeapMonitor");
  34     } catch (UnsatisfiedLinkError ule) {
  35       System.err.println("Could not load HeapMonitor library");
  36       System.err.println("java.library.path: " + System.getProperty("java.library.path"));
  37       throw ule;
  38     }
  39   }
  40 
  41   private static volatile int arrays[][];
  42 
  43   /** Enable heap monitoring sampling given a rate and maximum garbage to keep in memory. */
  44   public native static void enableSampling(int rate, int maximumGarbage);
  45 
  46   /** Enable heap monitoring sampling given a rate. */
  47   public static void enableSampling(int rate) {
  48     enableSampling(rate, 200);
  49   }
  50 
  51   /** Enable heap monitoring sampling with default values for rate and maximum garbage. */
  52   public static void enableSampling() {
  53     enableSampling(1 << 19);
  54   }
  55 
  56   public native static void disableSampling();
  57   public native static boolean areSamplingStatisticsZero();
  58   public native static boolean statsHaveExpectedNumberSamples(int expected, int percentError);
  59 
  60   /** Do the frames provided exist in live, recent garbage, and frequent garbage. */
  61   public native static boolean framesExistEverywhere(Frame[] frames);
  62   /** Do the frames provided not exist in live, recent garbage, and frequent garbage. */
  63   public native static boolean framesExistNowhere(Frame[] frames);
  64   /** Do the frames provided exist in live, recent garbage, or frequent garbage. */
  65   public native static boolean framesExistSomewhere(Frame[] frames);
  66 
  67   /**
  68    * Allocate memory but first create a stack trace of a particular depth.
  69    *
  70    * @return list of frames for the allocation.
  71    */
  72   public static List<Frame> allocate(int depth) {
  73     List<Frame> frames = new ArrayList<Frame>();
  74     if (depth > 1) {
  75       createStackDepth(depth - 1, frames);
  76       frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 75));
  77     } else {
  78       actuallyAllocate(frames);
  79       frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 138));
  80       frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 78));
  81     }
  82     return frames;
  83   }
  84 
  85   /**
  86    * Allocate memory but first create a stack trace.
  87    *
  88    * @return list of frames for the allocation.
  89    */
  90   public static List<Frame> allocate() {
  91     int sum = 0;
  92     List<Frame> frames = new ArrayList<Frame>();
  93     allocate(frames);
  94     frames.add(new Frame("allocate", "()Ljava/util/List;", "HeapMonitor.java", 93));
  95     return frames;
  96   }
  97 
  98   private static void createStackDepth(int depth, List<Frame> frames) {
  99     if (depth > 1) {
 100       createStackDepth(depth - 1, frames);
 101       frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 100));
 102     } else {
 103       allocate(frames);
 104       frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 103));
 105     }
 106   }
 107 
 108   private static void allocate(List<Frame> frames) {
 109     int sum = 0;
 110     for (int j = 0; j < 1000; j++) {
 111       sum += actuallyAllocate(frames);
 112     }
 113     frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 138));
 114     frames.add(new Frame("allocate", "(Ljava/util/List;)V", "HeapMonitor.java", 111));
 115   }
 116 
 117   public static List<Frame> repeatAllocate(int max) {
 118     List<Frame> frames = null;
 119     for (int i = 0; i < max; i++) {
 120       frames = allocate();
 121     }
 122     frames.add(new Frame("repeatAllocate", "(I)Ljava/util/List;", "HeapMonitor.java", 120));
 123     return frames;
 124   }
 125 
 126   private static int actuallyAllocate(List<Frame> frames) {
 127     int sum = 0;
 128 
 129     // Let us assume that a 1-element array is 24 bytes of memory and we want
 130     // 2MB allocated.
 131     int iterations = (1 << 19) / 6;
 132 
 133     if (arrays == null) {
 134       arrays = new int[iterations][];
 135     }
 136 
 137     for (int i = 0; i < iterations; i++) {
 138       int tmp[] = new int[1];
 139       // Force it to be kept and, at the same time, wipe out any previous data.
 140       arrays[i] = tmp;
 141       sum += arrays[0][0];
 142     }
 143     return sum;
 144   }
 145 
 146   /** Remove the reference to the global array to free data at the next GC. */
 147   public static void freeStorage() {
 148     arrays = null;
 149   }
 150 }