001: /*
002: * Created on 08.06.2005
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package de.schlund.pfixxml.perflogging;
008:
009: import java.lang.management.ManagementFactory;
010: import java.util.Map;
011: import java.util.Properties;
012:
013: import javax.management.MBeanServer;
014: import javax.management.Notification;
015: import javax.management.NotificationBroadcasterSupport;
016: import javax.management.ObjectName;
017:
018: import org.apache.log4j.Logger;
019:
020: /**
021: * @author jh
022: *
023: */
024: public class PerfLogging extends NotificationBroadcasterSupport
025: implements PerfLoggingMBean {
026:
027: private static PerfLogging instance = new PerfLogging();
028: private final static Logger LOG = Logger
029: .getLogger(PerfLogging.class);
030: private String PROP_BUFFER_SIZE = "perflogging.buffersize";
031: private String PROP_ENABLED = "perflogging.enabled";
032: private String PROP_AUTOSTART = "perflogging.autostart";
033: private String PROP_OFFER_MAX_WAIT = "perflogging.offermaxwait";
034:
035: private String OFF = "0";
036: private String ON = "1";
037: private int DEFAULT_BUFFER_SIZE = 1000;
038: private int DEFAULT_OFFER_MAX_WAIT = 5;
039: private boolean perfLoggingEnabled = false;
040: private boolean perfActive = false;
041: private BoundedBufferWrapper boundedBuffer;
042: private PerfEventTakeThread perfEventTakeThread;
043: private long seqNo;
044:
045: private PerfLogging() {
046: }
047:
048: public static PerfLogging getInstance() {
049: return instance;
050: }
051:
052: public void init(Properties props) {
053:
054: LOG.info("Perflogging init");
055:
056: String prop_enabled = props.getProperty(PROP_ENABLED, OFF);
057: if (prop_enabled.equals(ON)) {
058: perfLoggingEnabled = true;
059: } else {
060: perfLoggingEnabled = false;
061: }
062: LOG.info("Perflogging enabled: " + perfLoggingEnabled);
063: if (!perfLoggingEnabled)
064: return;
065:
066: String prop_buffs = props.getProperty(PROP_BUFFER_SIZE);
067: int size = 0;
068: if (prop_buffs == null || prop_buffs.length() < 1) {
069: LOG.warn("Property " + PROP_BUFFER_SIZE
070: + " not found. Using default: "
071: + DEFAULT_BUFFER_SIZE);
072: size = DEFAULT_BUFFER_SIZE;
073: } else {
074: size = Integer.parseInt(prop_buffs);
075: }
076:
077: String prop_offerwait = props.getProperty(PROP_OFFER_MAX_WAIT);
078: int wait = 0;
079: if (prop_offerwait == null || prop_offerwait.length() < 1) {
080: LOG.warn("Property " + PROP_OFFER_MAX_WAIT
081: + " not found. Using default: "
082: + DEFAULT_OFFER_MAX_WAIT);
083: wait = DEFAULT_OFFER_MAX_WAIT;
084: } else {
085: wait = Integer.parseInt(prop_offerwait);
086: }
087: boundedBuffer = new BoundedBufferWrapper(size, wait);
088:
089: String prop_autostart = props.getProperty(PROP_AUTOSTART, OFF);
090:
091: if (LOG.isInfoEnabled()) {
092: StringBuffer sb = new StringBuffer();
093: sb.append("After init: \n").append(
094: "Enabled: " + perfLoggingEnabled + "\n").append(
095: "Active: " + perfActive + "\n").append(
096: "Buffersize: " + size + "\n").append(
097: "Bufferwait: " + wait + "\n");
098: LOG.info(sb.toString());
099:
100: }
101:
102: if (prop_autostart.equals(ON)) {
103: activatePerflogging();
104: } else {
105: perfActive = false;
106: }
107:
108: try {
109: MBeanServer mbeanServer = ManagementFactory
110: .getPlatformMBeanServer();
111: ObjectName objectName = new ObjectName(
112: "Pustefix:type=PerfLogging");
113: mbeanServer.registerMBean(this , objectName);
114: } catch (Exception x) {
115: LOG.error("Can't register PerfLogging MBean!", x);
116: }
117:
118: }
119:
120: public boolean isPerfLogggingEnabled() {
121: return perfLoggingEnabled;
122: }
123:
124: public boolean isPerfLoggingActive() {
125: return perfActive;
126: }
127:
128: public synchronized void activatePerflogging() {
129: if (!perfLoggingEnabled) {
130: LOG.warn("Perflogging is disabled");
131: return;
132: }
133: if (!perfActive) {
134: LOG.info("Activating perflogging");
135: boundedBuffer.init();
136: PerfEventPut.getInstance().setBuffer(boundedBuffer);
137: startPerfEventTakeThread();
138: perfActive = true;
139: LOG.info("Perflogging now active");
140: } else {
141: LOG.info("perflogging already active");
142: }
143: }
144:
145: public synchronized String inactivatePerflogging() {
146: if (!perfLoggingEnabled) {
147: LOG.warn("Perflogging is disabled");
148: return null;
149: }
150: if (perfActive) {
151: LOG.info("Inactivating perflogging");
152: perfActive = false;
153: String xml = PerfStatistic.getInstance().toXML();
154: PerfStatistic.getInstance().reset();
155: stopPerfEventTakeThread();
156: boundedBuffer.reset();
157: return xml;
158: } else {
159: LOG.info("perflogging already inactive");
160: return null;
161: }
162: }
163:
164: public synchronized Map<String, Map<String, int[]>> inactivatePerfloggingMap() {
165: if (!perfLoggingEnabled) {
166: LOG.warn("Perflogging is disabled");
167: return null;
168: }
169: if (perfActive) {
170: LOG.info("Inactivating perflogging");
171: perfActive = false;
172: Map<String, Map<String, int[]>> map = PerfStatistic
173: .getInstance().toMap();
174: PerfStatistic.getInstance().reset();
175: stopPerfEventTakeThread();
176: boundedBuffer.reset();
177: return map;
178: } else {
179: LOG.info("perflogging already inactive");
180: return null;
181: }
182: }
183:
184: private void startPerfEventTakeThread() {
185: LOG.info("Starting new Take-Thread for Buffer");
186: perfEventTakeThread = new PerfEventTakeThread(boundedBuffer);
187: perfEventTakeThread.start();
188: }
189:
190: private void stopPerfEventTakeThread() {
191: LOG.info("Interrupting existing Take-Thread for Buffer");
192: perfEventTakeThread.interrupt();
193: }
194:
195: //accessible via JMX:
196:
197: public synchronized boolean isPerfLoggingEnabled() {
198: return isPerfLogggingEnabled();
199: }
200:
201: public synchronized boolean isPerfLoggingRunning() {
202: return isPerfLoggingActive();
203: }
204:
205: public synchronized void startPerfLogging() {
206: if (!isPerfLoggingActive()) {
207: try {
208: activatePerflogging();
209: } finally {
210: if (isPerfLoggingActive()) {
211: Notification n = new Notification(
212: "PerfLogging.started", this , seqNo++,
213: System.currentTimeMillis(),
214: "Started performance logging");
215: sendNotification(n);
216: }
217: }
218: } else
219: throw new IllegalStateException(
220: "Performance logging is already running.");
221: }
222:
223: public synchronized String stopPerfLogging() {
224: if (isPerfLoggingActive()) {
225: try {
226: return inactivatePerflogging();
227: } finally {
228: if (!isPerfLoggingActive()) {
229: Notification n = new Notification(
230: "PerfLogging.stopped", this , seqNo++,
231: System.currentTimeMillis(),
232: "Stopped performance logging");
233: sendNotification(n);
234: }
235: }
236: } else
237: throw new IllegalStateException(
238: "Performance logging isn't running.");
239: }
240:
241: public synchronized Map<String, Map<String, int[]>> stopPerfLoggingMap() {
242: if (isPerfLoggingActive()) {
243: try {
244: return inactivatePerfloggingMap();
245: } finally {
246: if (!isPerfLoggingActive()) {
247: Notification n = new Notification(
248: "PerfLogging.stopped", this , seqNo++,
249: System.currentTimeMillis(),
250: "Stopped performance logging");
251: sendNotification(n);
252: }
253: }
254: } else
255: throw new IllegalStateException(
256: "Performance logging isn't running.");
257: }
258:
259: }
|