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