View Javadoc

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