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