Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 70096 invoked from network); 6 Jan 2009 15:18:05 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 6 Jan 2009 15:18:05 -0000 Received: (qmail 71852 invoked by uid 500); 6 Jan 2009 15:18:04 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 71759 invoked by uid 500); 6 Jan 2009 15:18:04 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 71750 invoked by uid 99); 6 Jan 2009 15:18:03 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 Jan 2009 07:18:03 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 06 Jan 2009 15:18:02 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id CEB5F23889D8; Tue, 6 Jan 2009 07:17:42 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r731969 - in /commons/sandbox/monitoring/branches/modules/core: ./ src/main/java/org/apache/commons/monitoring/stopwatches/ Date: Tue, 06 Jan 2009 15:17:42 -0000 To: commits@commons.apache.org From: nicolas@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090106151742.CEB5F23889D8@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: nicolas Date: Tue Jan 6 07:17:41 2009 New Revision: 731969 URL: http://svn.apache.org/viewvc?rev=731969&view=rev Log: a simplier stopwatch that only computes elapsed time Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java Modified: commons/sandbox/monitoring/branches/modules/core/ (props changed) commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java Propchange: commons/sandbox/monitoring/branches/modules/core/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Tue Jan 6 07:17:41 2009 @@ -0,0 +1,4 @@ +.settings +target +.classpath +.project Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java?rev=731969&r1=731968&r2=731969&view=diff ============================================================================== --- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java (original) +++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java Tue Jan 6 07:17:41 2009 @@ -46,9 +46,9 @@ } @Override - protected void doStart( Monitor monitor ) + protected void doStart() { - super.doStart( monitor ); + super.doStart(); ThreadMXBean mx = ManagementFactory.getThreadMXBean(); if ( mx.isCurrentThreadCpuTimeSupported() ) { Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java?rev=731969&r1=731968&r2=731969&view=diff ============================================================================== --- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java (original) +++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatch.java Tue Jan 6 07:17:41 2009 @@ -5,32 +5,14 @@ import org.apache.commons.monitoring.Unit; /** - * Simple implementation of StopWatch that estimate monitored element execution time. + * Implementation of StopWatch that maintains a Gauge of concurrent threads accessing the monitored resource. * * @author Nicolas De Loof */ -public class DefaultStopWatch +public class DefaultStopWatch extends SimpleStopWatch implements StopWatch { - /** Time the probe was started */ - protected final long startedAt; - - /** Time the probe was stopped */ - private long stopedAt; - - /** Time the probe was paused */ - private long pauseDelay; - - /** flag for stopped probe */ - private boolean stoped; - - /** flag for paused probe */ - private boolean paused; - - /** Monitor that is notified of process execution state */ - protected final Monitor monitor; - /** * Constructor. *

@@ -40,213 +22,25 @@ */ public DefaultStopWatch( Monitor monitor ) { - super(); - this.monitor = monitor; - startedAt = nanotime(); - doStart( monitor ); + super( monitor ); + doStart(); } - protected void doStart( Monitor monitor ) + protected void doStart() { monitor.getGauge( Monitor.CONCURRENCY ).increment( Unit.UNARY ); } - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#getElapsedTime() - */ - public long getElapsedTime() - { - if ( stoped || paused ) - { - return stopedAt - startedAt - pauseDelay; - } - else - { - // Still running ! - return nanotime() - startedAt - pauseDelay; - } - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#pause() - */ - public void pause() - { - if ( !paused && !stoped ) - { - stopedAt = nanotime(); - paused = true; - } - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#resume() - */ - public void resume() - { - if ( paused && !stoped ) - { - pauseDelay = nanotime() - stopedAt; - paused = false; - stopedAt = 0; - } - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#stop() - */ - public void stop() - { - if ( !stoped ) - { - long t = nanotime(); - if ( paused ) - { - pauseDelay = t - stopedAt; - } - stopedAt = t; - stoped = true; - doStop(); - } - } - protected void doStop() { + super.doStop(); monitor.getGauge( Monitor.CONCURRENCY ).decrement( Unit.UNARY ); - monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), Unit.NANOS ); } - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#stop(boolean) - */ - public void stop( boolean canceled ) - { - if ( canceled ) - { - cancel(); - } - else - { - stop(); - } - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#cancel() - */ - public void cancel() - { - if ( !stoped ) - { - stoped = true; - doCancel(); - } - } protected void doCancel() { monitor.getGauge( Monitor.CONCURRENCY ).decrement( Unit.UNARY ); } - /** - * {@inheritDoc} - *

- * Monitored application should use a try/finally block to ensure on of {@link #stop()} or - * {@link #cancel()} method is invoked, even when an exception occurs. To avoid StopWatches to keep running if the - * application didn't follow this recommendation, the finalizer is used to cancel the StopWatch and will log a - * educational warning. - * - * @see java.lang.Object#finalize() - */ - protected void finalize() - { - // This probe is reclaimed by garbage-collector and still running, - // the monitored code "forgot" to stop/cancel it properly. - if ( !stoped && ( monitor != null ) ) - { - System.err.println( "WARNING : Execution for " + monitor.getKey().toString() + - " was not stoped properly. " + "This can result in wrong concurrency monitoring. " + - "Use try/finally blocks to avoid this warning" ); - } - } - - /** - * Returns the current value of the most precise available system timer, in nanoseconds. The real precision depends - * on the JVM and the underlying system. On JRE before java5, backport-util-concurrent provides some - * limited support for equivalent timer. - * - * @see System#nanoTime() - * @return time in nanosecond - */ - protected long nanotime() - { - return System.nanoTime(); - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#isStoped() - */ - public boolean isStoped() - { - return stoped; - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#isPaused() - */ - public boolean isPaused() - { - return paused; - } - - @Override - public String toString() - { - StringBuffer stb = new StringBuffer(); - if ( monitor != null ) - { - stb.append( "Execution for " ).append( monitor.getKey().toString() ).append( " " ); - } - if ( paused ) - { - stb.append( "paused after " ).append( getElapsedTime() ).append( "ns" ); - } - else if ( stoped ) - { - stb.append( "stoped after " ).append( getElapsedTime() ).append( "ns" ); - } - else - { - stb.append( "running for " ).append( getElapsedTime() ).append( "ns" ); - } - return stb.toString(); - - } - - /** - * {@inheritDoc} - * - * @see org.apache.commons.monitoring.StopWatch#getMonitor() - */ - public Monitor getMonitor() - { - return monitor; - } - } \ No newline at end of file Added: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java?rev=731969&view=auto ============================================================================== --- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java (added) +++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java Tue Jan 6 07:17:41 2009 @@ -0,0 +1,250 @@ +package org.apache.commons.monitoring.stopwatches; + +import org.apache.commons.monitoring.Monitor; +import org.apache.commons.monitoring.Unit; + +/** + * Simple implementation of StopWatch that estimate monitored element execution time. + * + * @author Nicolas De Loof + */ +public class SimpleStopWatch +{ + /** Monitor that is notified of process execution state */ + public final Monitor monitor; + + /** Time the probe was started */ + protected final long startedAt; + + /** Time the probe was stopped */ + protected long stopedAt; + + /** Time the probe was paused */ + protected long pauseDelay; + + /** flag for stopped probe */ + protected boolean stoped; + + /** flag for paused probe */ + protected boolean paused; + + /** + * Constructor. + *

+ * The monitor can be set to null to use the StopWatch without the monitoring infrastructure. + * + * @param monitor the monitor associated with the process to be monitored + */ + public SimpleStopWatch( Monitor monitor ) + { + super(); + this.monitor = monitor; + startedAt = nanotime(); + doStart(); + } + + /** + * To be overriden by subclasses to add some features on stopWath start + */ + protected void doStart() + { + + } + + /** + * Returns the current value of the most precise available system timer, in nanoseconds. The real precision depends + * on the JVM and the underlying system. On JRE before java5, backport-util-concurrent provides some + * limited support for equivalent timer. + * + * @see System#nanoTime() + * @return time in nanosecond + */ + protected long nanotime() + { + return System.nanoTime(); + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#getElapsedTime() + */ + public long getElapsedTime() + { + if ( stoped || paused ) + { + return stopedAt - startedAt - pauseDelay; + } + else + { + // Still running ! + return nanotime() - startedAt - pauseDelay; + } + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#pause() + */ + public void pause() + { + if ( !paused && !stoped ) + { + stopedAt = nanotime(); + paused = true; + } + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#resume() + */ + public void resume() + { + if ( paused && !stoped ) + { + pauseDelay = nanotime() - stopedAt; + paused = false; + stopedAt = 0; + } + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#stop() + */ + public void stop() + { + if ( !stoped ) + { + long t = nanotime(); + if ( paused ) + { + pauseDelay = t - stopedAt; + } + stopedAt = t; + stoped = true; + doStop(); + } + } + + protected void doStop() + { + monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), Unit.NANOS ); + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#stop(boolean) + */ + public void stop( boolean canceled ) + { + if ( canceled ) + { + cancel(); + } + else + { + stop(); + } + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#cancel() + */ + public void cancel() + { + if ( !stoped ) + { + stoped = true; + doCancel(); + } + } + + protected void doCancel() + { + + } + + /** + * {@inheritDoc} + *

+ * Monitored application should use a try/finally block to ensure on of {@link #stop()} or + * {@link #cancel()} method is invoked, even when an exception occurs. To avoid StopWatches to keep running if the + * application didn't follow this recommendation, the finalizer is used to cancel the StopWatch and will log a + * educational warning. + * + * @see java.lang.Object#finalize() + */ + protected void finalize() + { + // This probe is reclaimed by garbage-collector and still running, + // the monitored code "forgot" to stop/cancel it properly. + if ( !stoped && ( monitor != null ) ) + { + System.err.println( "WARNING : Execution for " + monitor.getKey().toString() + " was not stoped properly. " + + "This can result in wrong concurrency monitoring. " + "Use try/finally blocks to avoid this warning" ); + } + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#isStoped() + */ + public boolean isStoped() + { + return stoped; + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#isPaused() + */ + public boolean isPaused() + { + return paused; + } + + @Override + public String toString() + { + StringBuffer stb = new StringBuffer(); + if ( monitor != null ) + { + stb.append( "Execution for " ).append( monitor.getKey().toString() ).append( " " ); + } + if ( paused ) + { + stb.append( "paused after " ).append( getElapsedTime() ).append( "ns" ); + } + else if ( stoped ) + { + stb.append( "stoped after " ).append( getElapsedTime() ).append( "ns" ); + } + else + { + stb.append( "running for " ).append( getElapsedTime() ).append( "ns" ); + } + return stb.toString(); + + } + + /** + * {@inheritDoc} + * + * @see org.apache.commons.monitoring.StopWatch#getMonitor() + */ + public Monitor getMonitor() + { + return monitor; + } + +} \ No newline at end of file