Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: joist/java/org/joist/util MemorySentinel.java

Project highlights: Architectural Overview

joist
Discussion topic

Back to topic list

CVS update: joist/java/org/joist/util MemorySentinel.java

Author stack
Full name Michael Stack
Date 2000-11-10 20:34:49 PST
Message User: stack
  Date: 00/11/10 20:34:49

  Added: java/org/joist/util MemorySentinel.java
  Log:
  Class to log memory useage periodically. Singleton so one per heap. Started out of HelmServlet.start( ). Reads properties set in joist/conf/30joist.z​one.properties
  
  Revision Changes Path
  1.1 joist/java/org/joist​/util/MemorySentinel​.java
  
  Index: MemorySentinel.java
  ====================​====================​====================​=======
  /* ====================​====================​====================​====
   * Copyright (c) 2000 Collab.Net. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are
   * met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in the
   * documentation and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   * any, must include the following acknowlegement: "This product includes
   * software developed by Collab.Net (http://www.Collab.Net/)."
   * Alternately, this acknowlegement may appear in the software itself, if
   * and wherever such third-party acknowlegements normally appear.
   *
   * 4. The hosted project names must not be used to endorse or promote
   * products derived from this software without prior written
   * permission. For written permission, please contact info at collab dot net.
   *
   * 5. Products derived from this software may not use the "Tigris" name
   * nor may "Tigris" appear in their names without prior written
   * permission of Collab.Net.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
   * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * ====================​====================​====================​========
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of Collab.Net.
   */
  package org.joist.util;
  
   
  import org.webmacro.util.Log;
  
   
  /** Write state of the memory usage message to the webmacro log periodically.
   *
   * «Immutable -- Read-Only.»
   * <p>
   * Spawn a thread that wakes up periodically to write memory use. Copied
   * the way JoistProperties implements the singleton pattern.
   * <p>
   * Set thread to run in daemon mode. The JVM will not wait on its completion
   * if it's a daemon thread.
   *
   * @author <a href="mailto:stack@c​ollab.net">St.Ack​</a>
   * @version $Id: MemorySentinel.java,v 1.1 2000/11/11 04:34:49 stack Exp $
   * @see JoistProperties
   */
  public class MemorySentinel extends Thread
  {
      /** Class version string
       */
      public static final String versionID
          = "$Id: MemorySentinel.java,v 1.1 2000/11/11 04:34:49 stack Exp $";
              
      
      /** Default sleep period between logging.
      * One minute in milliseconds.
      */
      private final static long DEFAULT_SLEEP = ( ( 1000 ) * ( 60 ) );
      
      
      /** Period to wait between printings of memory usage.
       * In milliseconds.
       */
      private long period = DEFAULT_SLEEP;
      
      
      /** Run in daemon mode.
       * If daemon mode, we can run for ever and JVM won't wait on our completion
       * before exiting.
       */
      private boolean daemon = Boolean.FALSE.booleanValue( );
      
      
      /** Should we log?
       */
      private boolean on = Boolean.FALSE.booleanValue( );
      
          
      /** Keep here the single thisInstance of this class.
       * We only want one of these per heap.
       */
      private static MemorySentinel thisInstance = null;
      
      
      /** This thread's name.
       */
      private final static String THREAD_NAME = "Memory Sentinel";
      
      
      /** Logging object.
       */
      private final org.webmacro.util.Log log = new Log( "memory", THREAD_NAME );
      
      
      /** Key to pull whether or not to log.
       */
      private final static String ON_KEY = "org.joist.util.memo​rysentinel.on";
      
      
      /** Key to pull period between loggings
       */
      private final static String PERIOD_KEY
          = "org.joist.util.memo​rysentinel.period";
          
          
      /** Key to pull whether we should run in daemon mode.
       */
      private final static String DAEMON_KEY
          = "org.joist.util.memo​rysentinel.daemon";
      
      
      /** Constructor.
       * Private. Only want one thisInstance per heap so make this
       * singleton class. Reads properties using JoistProperties class.
       *
       * @exception NullPointerException If we can't get a JoistProperties
       * instance.
       * @exception IllegalArgumentException If the period we get is
       * negative or greater than once every 24 hours.
       */
      private MemorySentinel( )
          throws NullPointerException, IllegalArgumentException
      {
          JoistProperties properties = JoistProperties.getInstance( );
          if( properties == null )
              throw new NullPointerException( "JoistProperties is null" );
              
          Boolean onSetting = new Boolean( properties.getProperty( ON_KEY ) );
          on = onSetting.booleanValue( );
          Boolean daemonSetting
              = new Boolean( properties.getProperty( DAEMON_KEY ) );
          daemon = daemonSetting.booleanValue( );
          
          period = Long.parseLong( properties.getProperty( PERIOD_KEY ) );
          if( ( period < 0 ) || ( period > ( 1000 * 60 * 60 * 24 ) ) )
              throw new IllegalArgumentException( "Period negative or too big: "
                  + Long.toString( period ) );
                  
          log.info( "on: "
                      + ( ( on )
                          ? Boolean.TRUE.toString( )
                          : Boolean.FALSE.toString( ) )
                      + ", daemon: "
                      + ( ( daemon )
                          ? Boolean.TRUE.toString( )
                          : Boolean.FALSE.toString( ) )
                      + ", period (in milliseconds): "
                      + Long.toString( period ) );
      }
      
      
      /** The first call to getInstance starts the memory sentinel thread.
       *
       * Safe to call multiple times. Only one thisInstance possible per heap.
       * Reads properties that tell it interval to run at and whether to run
       * as daemon thread. Same property file may tell us not to start the
       * logging thread.
       *
       * @return The single thisInstance of this class.
       *
       * @exception IllegalThreadStateException
       * @exception SecurityException
       * @exception NullPointerException If we can't get a JoistProperties
       * instance.
       * @exception IllegalArgumentException If the period we get is
       * negative or greater than once every 24 hours.
       */
      public static MemorySentinel getInstance( )
          throws IllegalThreadStateException,
                  SecurityException,
                  NullPointerException,
                  IllegalArgumentException
      {
          if( thisInstance == null )
          {
              synchronized( MemorySentinel.class ) // Get class lock.
              {
                  // Test again for null in case we've been preempted between
                  // the first test for null and obtaining our class lock
                  //
                  if( thisInstance == null )
                  {
                      // Set a thread name and set priority to high. It's
                      // important we run -- especially when JVM is trashing.
                      //
                      thisInstance = new MemorySentinel( );
                      thisInstance.setName( THREAD_NAME );
                      thisInstance.setPriority( Thread.MAX_PRIORITY );
                      if( thisInstance.daemon )
                          thisInstance.setDaemon( Boolean.TRUE.booleanValue( ) );
                      if( thisInstance.on )
                          thisInstance.start( );
                  }
              }
          }
          
          return thisInstance;
      }
      
      
      /** Print total and free memory to webmacro log on an interval.
       */
      public void run( )
      {
          Runtime runtime = Runtime.getRuntime( );
          StringBuffer logBuffer = new StringBuffer( );
          
          while( Boolean.TRUE.booleanValue( ) )
          {
              logBuffer.setLength( 0 );
              logBuffer.append( "Total: " );
              logBuffer.append( runtime.totalMemory( ) );
              logBuffer.append( ", Free: " );
              logBuffer.append( runtime.freeMemory( ) );
              log.info( logBuffer.toString( ) );
              
              try
              {
                  sleep( period );
              }
              
              catch( InterruptedException exception )
              {
                  log.info( exception.toString( ) );
                  break; // Finish
              }
          }
      }
      
      
      /** Main method.
       *
       * Used to test this class.
       *
       * @param args List of command line arguments. Ignored.
       */
      public static void main( String[] args )
      {
         getInstance( );
      }
  }

« Previous message in topic | 1 of 2 | Next message in topic »

Messages

Show all messages in topic

CVS update: joist/java/org/joist/util MemorySentinel.java stack Michael Stack 2000-11-10 20:34:49 PST
     Re: [joist-cvs] CVS update: joist/java/org/joist/util MemorySentinel.java David Pellegrini <davidp at xsitedesign dot com> David Pellegrini <davidp at xsitedesign dot com> 2000-11-11 18:57:31 PST
Messages per page: