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 int g_tmp[];
  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 
  65   /**
  66    * Allocate memory but first create a stack trace of a particular depth.
  67    *
  68    * @return list of frames for the allocation.
  69    */
  70   public static List<Frame> allocate(int depth) {
  71     List<Frame> frames = new ArrayList<Frame>();
  72     if (depth > 1) {
  73       createStackDepth(depth - 1, frames);
  74       frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 73));
  75     } else {
  76       actuallyAllocate(frames);
  77       frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 119));
  78       frames.add(new Frame("allocate", "(I)Ljava/util/List;", "HeapMonitor.java", 76));
  79     }
  80     return frames;
  81   }
  82 
  83   /**
  84    * Allocate memory but first create a stack trace.
  85    *
  86    * @return list of frames for the allocation.
  87    */
  88   public static List<Frame> allocate() {
  89     int sum = 0;
  90     List<Frame> frames = new ArrayList<Frame>();
  91     allocate(frames);
  92     frames.add(new Frame("allocate", "()Ljava/util/List;", "HeapMonitor.java", 91));
  93     return frames;
  94   }
  95 
  96   private static void createStackDepth(int depth, List<Frame> frames) {
  97     if (depth > 1) {
  98       createStackDepth(depth - 1, frames);
  99       frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 98));
 100     } else {
 101       allocate(frames);
 102       frames.add(new Frame("createStackDepth", "(ILjava/util/List;)V", "HeapMonitor.java", 101));
 103     }
 104   }
 105 
 106   private static void allocate(List<Frame> frames) {
 107     int sum = 0;
 108     for (int j = 0; j < 1000; j++) {
 109       sum += actuallyAllocate(frames);
 110     }
 111     frames.add(new Frame("actuallyAllocate", "(Ljava/util/List;)I", "HeapMonitor.java", 119));
 112     frames.add(new Frame("allocate", "(Ljava/util/List;)V", "HeapMonitor.java", 109));
 113   }
 114 
 115   private static int actuallyAllocate(List<Frame> frames) {
 116     int sum = 0;
 117     // Let us assume that the array is 24 bytes of memory.
 118     for (int i = 0; i < 127000 / 6; i++) {
 119       int tmp[] = new int[1];
 120       // Force it to be kept.
 121       g_tmp = tmp;
 122       sum += g_tmp[0];
 123     }
 124     return sum;
 125   }
 126 }