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