001: /*
002: * $Id: YourKitProfilerService.java 11234 2008-03-06 23:44:34Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.module.management.mbean;
012:
013: import org.mule.module.management.i18n.ManagementMessages;
014:
015: import com.yourkit.api.Controller;
016:
017: import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: public class YourKitProfilerService implements
023: YourKitProfilerServiceMBean {
024: /**
025: * logger used by this class
026: */
027: protected transient Log logger = LogFactory.getLog(getClass());
028:
029: private final Controller controller;
030: private AtomicBoolean capturing = new AtomicBoolean(false);
031:
032: public YourKitProfilerService() throws Exception {
033: controller = new Controller();
034: }
035:
036: /**
037: * {@inheritDoc}
038: */
039: public String getHost() {
040: return controller.getHost();
041: }
042:
043: /**
044: * {@inheritDoc}
045: */
046: public int getPort() {
047: return controller.getPort();
048: }
049:
050: /**
051: * {@inheritDoc}
052: */
053: public String captureMemorySnapshot() throws Exception {
054: return controller.captureMemorySnapshot();
055: }
056:
057: /**
058: * {@inheritDoc}
059: */
060: public String captureSnapshot(long snapshotFlags) throws Exception {
061: return controller.captureSnapshot(snapshotFlags);
062: }
063:
064: /**
065: * {@inheritDoc}
066: */
067: public void startAllocationRecording(long mode) throws Exception {
068: controller.startAllocationRecording(mode);
069: }
070:
071: /**
072: * {@inheritDoc}
073: */
074: public void stopAllocationRecording() throws Exception {
075: controller.stopAllocationRecording();
076: }
077:
078: /**
079: * {@inheritDoc}
080: */
081: public void startCPUProfiling(long mode, String filters)
082: throws Exception {
083: controller.startCPUProfiling(mode, filters);
084: }
085:
086: /**
087: * {@inheritDoc}
088: */
089: public void stopCPUProfiling() throws Exception {
090: controller.stopCPUProfiling();
091: }
092:
093: /**
094: * {@inheritDoc}
095: */
096: public void startMonitorProfiling() throws Exception {
097: controller.startMonitorProfiling();
098: }
099:
100: /**
101: * {@inheritDoc}
102: */
103: public void stopMonitorProfiling() throws Exception {
104: controller.stopMonitorProfiling();
105: }
106:
107: /**
108: * {@inheritDoc}
109: */
110: public void advanceGeneration(String description) {
111: controller.advanceGeneration(description);
112: }
113:
114: /**
115: * {@inheritDoc}
116: */
117: public String forceGC() throws Exception {
118: long[] heapSizes = controller.forceGC();
119: return ManagementMessages.forceGC(heapSizes).getMessage();
120: }
121:
122: /**
123: * {@inheritDoc}
124: */
125: public void startCapturingMemSnapshot(final int seconds) {
126: if (!this .capturing.compareAndSet(false, true)) {
127: return;
128: }
129:
130: final Thread thread = new Thread(new Runnable() {
131: public void run() {
132: try {
133: while (capturing.get()) {
134: controller.captureMemorySnapshot();
135: Thread
136: .sleep(seconds * 1000 /* millis in second */);
137: }
138: } catch (Exception e) {
139: logger
140: .error("Failed to capture memory snapshot",
141: e);
142: }
143: }
144: });
145: thread.setDaemon(true); // let the application normally terminate
146: thread.start();
147: }
148:
149: /**
150: * {@inheritDoc}
151: */
152: public void stopCapturingMemSnapshot() {
153: this .capturing.set(false);
154: }
155:
156: /**
157: * {@inheritDoc}
158: */
159: public long getStatus() throws java.lang.Exception {
160: return (this.capturing.get()) ? (controller.getStatus() | SNAPSHOT_CAPTURING)
161: : controller.getStatus();
162: }
163:
164: }
|