mnaufaldillah

HeadDumper Jmeter

Oct 23rd, 2021
714
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to you under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. package org.apache.jorphan.util;
  19.  
  20. import java.io.File;
  21. import java.lang.management.ManagementFactory;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24.  
  25. import javax.management.InstanceNotFoundException;
  26. import javax.management.MBeanException;
  27. import javax.management.MBeanServer;
  28. import javax.management.MalformedObjectNameException;
  29. import javax.management.ObjectName;
  30. import javax.management.RuntimeMBeanException;
  31.  
  32. /**
  33.  * Class allowing access to Sun's heapDump method (Java 1.6+).
  34.  * Uses Reflection so that the code compiles on Java 1.5.
  35.  * The code will only work on Sun Java 1.6+.
  36.  */
  37. public class HeapDumper {
  38.  
  39.     // SingletonHolder idiom for lazy initialisation
  40.     private static class DumperHolder {
  41.         private static final HeapDumper DUMPER = new HeapDumper();
  42.     }
  43.  
  44.     private static HeapDumper getInstance(){
  45.         return DumperHolder.DUMPER;
  46.     }
  47.  
  48.     // This is the name of the HotSpot Diagnostic platform MBean (Sun Java 1.6)
  49.     // See: http://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/HotSpotDiagnosticMXBean.html
  50.     private static final String HOTSPOT_BEAN_NAME =
  51.          "com.sun.management:type=HotSpotDiagnostic";
  52.  
  53.     // These are needed for invoking the method
  54.     private final MBeanServer server;
  55.     private final ObjectName hotspotDiagnosticBean;
  56.  
  57.     // If we could not find the method, store the exception here
  58.     private final Exception exception;
  59.  
  60.     // Only invoked by IODH class
  61.     private HeapDumper() {
  62.         server = ManagementFactory.getPlatformMBeanServer(); // get the platform beans
  63.         ObjectName on = null;
  64.         Exception ex = null;
  65.         try {
  66.             on = new ObjectName(HOTSPOT_BEAN_NAME); // should never fail
  67.             server.getObjectInstance(on); // See if we can actually find the object
  68.         } catch (MalformedObjectNameException e) { // Should never happen
  69.             throw new AssertionError("Could not establish the HotSpotDiagnostic Bean Name: "+e);
  70.         } catch (InstanceNotFoundException e) {
  71.             ex = e;
  72.             on = null; // Prevent useless dump attempts
  73.         }
  74.         exception = ex;
  75.         hotspotDiagnosticBean = on;
  76.     }
  77.  
  78.     /**
  79.      * Initialise the dumper, and report if there is a problem.
  80.      * This is optional, as the dump methods will initialise if necessary.
  81.      *
  82.      * @throws Exception if there is a problem finding the heapDump MXBean
  83.      */
  84.     public static void init() throws Exception {
  85.         Exception e =getInstance().exception;
  86.         if (e != null) {
  87.             throw e;
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * Dumps the heap to the outputFile file in the same format as the hprof heap dump.
  93.      * <p>
  94.      * Calls the dumpHeap() method of the HotSpotDiagnostic MXBean, if available.
  95.      * <p>
  96.      * See
  97.      * <a href="http://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/HotSpotDiagnosticMXBean.html">
  98.      * HotSpotDiagnosticMXBean
  99.      * </a>
  100.      * @param fileName name of the heap dump file. Must be creatable, i.e. must not exist.
  101.      * @param live if true, dump only the live objects
  102.      * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation
  103.      */
  104.     public static void dumpHeap(String fileName, boolean live) throws Exception{
  105.         getInstance().dumpHeap0(fileName, live);
  106.     }
  107.  
  108.     /**
  109.      * Dumps live objects from the heap to the outputFile file in the same format as the hprof heap dump.
  110.      * <p>
  111.      * @see #dumpHeap(String, boolean)
  112.      * @param fileName name of the heap dump file. Must be creatable, i.e. must not exist.
  113.      * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation
  114.      */
  115.     public static void dumpHeap(String fileName) throws Exception{
  116.         dumpHeap(fileName, true);
  117.     }
  118.  
  119.     /**
  120.      * Dumps live objects from the heap to the outputFile file in the same format as the hprof heap dump.
  121.      * <p>
  122.      * Creates the dump using the file name: dump_yyyyMMdd_hhmmss_SSS.hprof
  123.      * The dump is created in the current directory.
  124.      * <p>
  125.      * @see #dumpHeap(boolean)
  126.      * @return the name of the dump file that was created
  127.      * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation
  128.      */
  129.     public static String dumpHeap() throws Exception{
  130.         return dumpHeap(true);
  131.     }
  132.  
  133.     /**
  134.      * Dumps objects from the heap to the outputFile file in the same format as the hprof heap dump.
  135.      * <p>
  136.      * Creates the dump using the file name: dump_yyyyMMdd_hhmmss_SSS.hprof
  137.      * The dump is created in the current directory.
  138.      * <p>
  139.      * @see #dumpHeap(String, boolean)
  140.      * @param live true id only live objects are to be dumped.
  141.      *
  142.      * @return the name of the dump file that was created
  143.      * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation
  144.      */
  145.     public static String dumpHeap(boolean live) throws Exception {
  146.         return dumpHeap(new File("."), live);
  147.     }
  148.  
  149.     /**
  150.      * Dumps objects from the heap to the outputFile file in the same format as the hprof heap dump.
  151.      * The dump is created in the specified directory.
  152.      * <p>
  153.      * Creates the dump using the file name: dump_yyyyMMdd_hhmmss_SSS.hprof
  154.      * <p>
  155.      * @see #dumpHeap(String, boolean)
  156.      * @param basedir File object for the target base directory.
  157.      * @param live true id only live objects are to be dumped.
  158.      *
  159.      * @return the name of the dump file that was created
  160.      * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation
  161.      */
  162.     @SuppressWarnings("JdkObsolete")
  163.     public static String dumpHeap(File basedir, boolean live) throws Exception {
  164.         SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS");
  165.         String stamp = timestampFormat.format(new Date());
  166.         File temp = new File(basedir,"dump_"+stamp+".hprof");
  167.         final String path = temp.getPath();
  168.         dumpHeap(path, live);
  169.         return path;
  170.     }
  171.  
  172.     /**
  173.      * Perform the dump using the dumpHeap method.
  174.      *
  175.      * @param fileName the file to use
  176.      * @param live true to dump only live objects
  177.      * @throws Exception if the MXBean cannot be found, or if there is a problem during invocation
  178.      */
  179.     private void dumpHeap0(String fileName, boolean live) throws Exception {
  180.         try {
  181.             if (exception == null) {
  182.                 server.invoke(hotspotDiagnosticBean,
  183.                         "dumpHeap",
  184.                         new Object[]{fileName, live},
  185.                         new String[]{"java.lang.String", "boolean"});
  186.             } else {
  187.                 throw exception;
  188.             }
  189.         } catch (RuntimeMBeanException e) {
  190.             Throwable f = e.getCause();
  191.             if (f instanceof Exception){
  192.                 throw (Exception) f;
  193.             }
  194.             throw e;
  195.         } catch (MBeanException e) {
  196.             Throwable f = e.getCause();
  197.             if (f instanceof Exception){
  198.                 throw (Exception) f;
  199.             }
  200.             throw e;
  201.         }
  202.     }
  203. }
RAW Paste Data