View Javadoc

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