Coverage Report - org.mule.module.management.mbean.YourKitProfilerService
 
Classes in this File Line Coverage Branch Coverage Complexity
YourKitProfilerService
0%
0/39
0%
0/10
1.412
YourKitProfilerService$1
0%
0/8
0%
0/2
1.412
 
 1  
 /*
 2  
  * $Id: YourKitProfilerService.java 19355 2010-09-03 18:21:50Z ddossot $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.module.management.mbean;
 12  
 
 13  
 import org.apache.commons.logging.Log;
 14  
 import org.apache.commons.logging.LogFactory;
 15  
 import org.mule.module.management.i18n.ManagementMessages;
 16  
 
 17  
 import com.yourkit.api.Controller;
 18  
 
 19  
 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
 20  
 
 21  0
 public class YourKitProfilerService implements YourKitProfilerServiceMBean
 22  
 {
 23  
     /**
 24  
      * logger used by this class
 25  
      */
 26  0
     protected transient Log logger = LogFactory.getLog(getClass());
 27  
 
 28  
     private final Controller controller;
 29  0
     private final AtomicBoolean capturing = new AtomicBoolean(false);
 30  
 
 31  
     public YourKitProfilerService() throws Exception
 32  0
     {
 33  0
         controller = new Controller();
 34  0
     }
 35  
 
 36  
     /**
 37  
      * {@inheritDoc}
 38  
      */
 39  
     public String getHost()
 40  
     {
 41  0
         return controller.getHost();
 42  
     }
 43  
 
 44  
     /**
 45  
      * {@inheritDoc}
 46  
      */
 47  
     public int getPort()
 48  
     {
 49  0
         return controller.getPort();
 50  
     }
 51  
 
 52  
     /**
 53  
      * {@inheritDoc}
 54  
      */
 55  
     public String captureMemorySnapshot() throws Exception
 56  
     {
 57  0
         return controller.captureMemorySnapshot();
 58  
     }
 59  
 
 60  
     /**
 61  
      * {@inheritDoc}
 62  
      */
 63  
     public String captureSnapshot(long snapshotFlags) throws Exception
 64  
     {
 65  0
         return controller.captureSnapshot(snapshotFlags);
 66  
     }
 67  
 
 68  
     /**
 69  
      * {@inheritDoc}
 70  
      */
 71  
     public void startAllocationRecording(long mode) throws Exception
 72  
     {
 73  0
         if (ALLOCATION_RECORDING_ADAPTIVE != mode && ALLOCATION_RECORDING_ALL != mode)
 74  
         {
 75  0
             throw new IllegalArgumentException("Invalid allocation recording mode requested: " + mode);
 76  
         }
 77  0
         if (mode == ALLOCATION_RECORDING_ALL)
 78  
         {
 79  0
             controller.startAllocationRecording(true, 1, false, 0);
 80  
         }
 81  
         else
 82  
         {
 83  
             // adaptive, record every 10th object OR above 1MB in size
 84  0
             controller.startAllocationRecording(true, 10, true, 1048576);
 85  
         }
 86  0
     }
 87  
 
 88  
     /**
 89  
      * {@inheritDoc}
 90  
      */
 91  
     public void stopAllocationRecording() throws Exception
 92  
     {
 93  0
         controller.stopAllocationRecording();
 94  0
     }
 95  
 
 96  
     /**
 97  
      * {@inheritDoc}
 98  
      */
 99  
     public void startCPUProfiling(long mode, String filters) throws Exception
 100  
     {
 101  0
         controller.startCPUProfiling(mode, filters);
 102  0
     }
 103  
 
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     public void stopCPUProfiling() throws Exception
 108  
     {
 109  0
         controller.stopCPUProfiling();
 110  0
     }
 111  
 
 112  
     /**
 113  
      * {@inheritDoc}
 114  
      */
 115  
     public void startMonitorProfiling() throws Exception
 116  
     {
 117  0
         controller.startMonitorProfiling();
 118  0
     }
 119  
 
 120  
     /**
 121  
      * {@inheritDoc}
 122  
      */
 123  
     public void stopMonitorProfiling() throws Exception
 124  
     {
 125  0
         controller.stopMonitorProfiling();
 126  0
     }
 127  
 
 128  
     /**
 129  
      * {@inheritDoc}
 130  
      */
 131  
     public void advanceGeneration(String description) throws Exception
 132  
     {
 133  0
         controller.advanceGeneration(description);
 134  0
     }
 135  
 
 136  
     /**
 137  
      * {@inheritDoc}
 138  
      */
 139  
     public String forceGC() throws Exception
 140  
     {
 141  0
         final long[] heapSizes = controller.forceGC();
 142  0
         return ManagementMessages.forceGC(heapSizes).getMessage();
 143  
     }
 144  
 
 145  
     /**
 146  
      * {@inheritDoc}
 147  
      */
 148  
     public void startCapturingMemSnapshot(final int seconds)
 149  
     {
 150  0
         if (!this.capturing.compareAndSet(false, true))
 151  
         {
 152  0
             return;
 153  
         }
 154  
 
 155  0
         final Thread thread = new Thread(new Runnable()
 156  0
         {
 157  
             public void run()
 158  
             {
 159  
                 try
 160  
                 {
 161  0
                     while (capturing.get())
 162  
                     {
 163  0
                         controller.captureMemorySnapshot();
 164  0
                         Thread.sleep(seconds * 1000 /* millis in second */);
 165  
                     }
 166  
                 }
 167  0
                 catch (final Exception e)
 168  
                 {
 169  0
                     logger.error("Failed to capture memory snapshot", e);
 170  0
                 }
 171  0
             }
 172  
         });
 173  0
         thread.setDaemon(true); // let the application normally terminate
 174  0
         thread.start();
 175  0
     }
 176  
 
 177  
     /**
 178  
      * {@inheritDoc}
 179  
      */
 180  
     public void stopCapturingMemSnapshot()
 181  
     {
 182  0
         this.capturing.set(false);
 183  0
     }
 184  
 
 185  
     /**
 186  
      * {@inheritDoc}
 187  
      */
 188  
     public long getStatus() throws java.lang.Exception
 189  
     {
 190  0
         return (this.capturing.get())
 191  
                                      ? (controller.getStatus() | SNAPSHOT_CAPTURING)
 192  
                                      : controller.getStatus();
 193  
     }
 194  
 
 195  
 }