avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l...@apache.org
Subject cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui ProfilerFrame.java
Date Tue, 05 Mar 2002 12:34:32 GMT
leif        02/03/05 04:34:32

  Modified:    src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler
                        AbstractProfileSample.java
                        AbstractValueProfileSample.java
                        AverageValueProfileSample.java
                        CounterProfileSample.java
                        DefaultProfilerManager.java
                        MaximumValueProfileSample.java
                        MinimumValueProfileSample.java ProfilableProxy.java
                        ProfilePointProxy.java ProfileSample.java
               src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui
                        ProfilerFrame.java
  Log:
  Add the ability to save profiler sample data across JVM invocations.
  
  Revision  Changes    Path
  1.3       +120 -1    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/AbstractProfileSample.java
  
  Index: AbstractProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/AbstractProfileSample.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractProfileSample.java	4 Mar 2002 03:52:16 -0000	1.2
  +++ AbstractProfileSample.java	5 Mar 2002 12:34:32 -0000	1.3
  @@ -10,6 +10,9 @@
   import java.util.ArrayList;
   import java.util.Arrays;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   
   /**
  @@ -17,7 +20,7 @@
    *  ProfileSamples.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.2 $ $Date: 2002/03/04 03:52:16 $
  + * @version CVS $Revision: 1.3 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   abstract class AbstractProfileSample
  @@ -363,6 +366,96 @@
           }
       }
       
  +    /**
  +     * Saves the current state into a Configuration.
  +     *
  +     * @return The state as a Configuration.
  +     */
  +    public final Configuration saveState()
  +    {
  +        synchronized(this)
  +        {
  +            DefaultConfiguration state = new DefaultConfiguration( "profile-sample", "-"
);
  +            state.addAttribute( "name", m_name );
  +            state.addAttribute( "time", Long.toString( m_time ) );
  +            
  +            // Save the history samples so that the newest is first.
  +            DefaultConfiguration samples = new DefaultConfiguration( "history", "-" );
  +            int[] history = getHistorySnapshot();
  +            for ( int i = history.length - 1; i >= 0; i-- )
  +            {
  +                DefaultConfiguration sample = new DefaultConfiguration( "sample", "-" );
  +                sample.setValue( Integer.toString( history[i] ) );
  +                samples.addChild( sample );
  +            }
  +            state.addChild( samples );
  +            
  +            saveState( state );
  +            
  +            return state;
  +        }
  +    }
  +    
  +    /**
  +     * Loads the state into the ProfileSample.
  +     *
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    public final void loadState( Configuration state ) throws ConfigurationException
  +    {
  +        synchronized( this )
  +        {
  +            // Set the time
  +            long savedTime = m_time = state.getAttributeAsLong( "time" );
  +            
  +            // Set the history index.
  +            m_historyIndex = 0;
  +            
  +            // Read in the samples, don't trust that the number will be correct.
  +            //  First sample is the current value, following sames go back in
  +            //   time from newest to oldest.
  +            Configuration history = state.getChild( "history" );
  +            Configuration samples[] = history.getChildren( "sample" );
  +            
  +            
  +            // Get the current value
  +            int value;
  +            if ( samples.length > 0 )
  +            {
  +                value = samples[0].getValueAsInteger();
  +                
  +                for ( int i = 0; i < m_size - 1; i++ )
  +                {
  +                    if ( i < samples.length - 1 )
  +                    {
  +                        m_historyOld[ m_size - 2 - i ] = samples[ i + 1 ].getValueAsInteger();
  +                    }
  +                    else
  +                    {
  +                        m_historyOld[ m_size - 2 - i ] = 0;
  +                    }
  +                }
  +            }
  +            else
  +            {
  +                value = 0;
  +            }
  +            
  +            loadState( value, state );
  +
  +            if ( calculateSampleTime( System.currentTimeMillis() ) > savedTime )
  +            {
  +                // The sample period changed since the save.
  +                //  This will usually happen, but not always for long
  +                //  intervals.
  +                postSaveNeedsReset();
  +            }
  +        }
  +    }
  +    
       /*---------------------------------------------------------------
        * Methods
        *-------------------------------------------------------------*/
  @@ -386,6 +479,32 @@
           m_historyNew = new int[ m_size - 1 ];
       }
       
  +    /**
  +     * Allow subclasses to add information into the saved state.
  +     *
  +     * @param state State configuration.
  +     */
  +    protected void saveState( DefaultConfiguration state ) {}
  +    
  +    /**
  +     * Used to load the state, called from AbstractProfileSample.loadState();
  +     * <p>
  +     * Should only be called when synchronized.
  +     *
  +     * @param value Current value loaded from the state.
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    protected abstract void loadState( int value, Configuration state )
  +        throws ConfigurationException;
  +    
  +    /**
  +     * Called after a state is loaded if the sample period is not the same
  +     *  as the last period saved.
  +     */
  +    protected abstract void postSaveNeedsReset();
       
       /**
        * Calculates the time of the sample which contains the specified time.
  
  
  
  1.2       +48 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/AbstractValueProfileSample.java
  
  Index: AbstractValueProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/AbstractValueProfileSample.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractValueProfileSample.java	3 Mar 2002 15:59:43 -0000	1.1
  +++ AbstractValueProfileSample.java	5 Mar 2002 12:34:32 -0000	1.2
  @@ -7,12 +7,16 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
  +
   /**
    * An AbstractValueProfileSample contains all of the functionality common
    *  to all ProfileSamples which represent a fixed value.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/03/03 15:59:43 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   abstract class AbstractValueProfileSample
  @@ -72,6 +76,49 @@
       public int getValueInner()
       {
           return m_value;
  +    }
  +
  +    /*---------------------------------------------------------------
  +     * AbstractProfileSample Methods
  +     *-------------------------------------------------------------*/
  +    /**
  +     * Allow subclasses to add information into the saved state.
  +     *
  +     * @param state State configuration.
  +     */
  +    protected void saveState( DefaultConfiguration state )
  +    {
  +        super.saveState( state );
  +        
  +        state.setAttribute( "value-count", Integer.toString( m_valueCount ) );
  +    }
  +    
  +    /**
  +     * Used to load the state, called from AbstractProfileSample.loadState();
  +     * <p>
  +     * Should only be called when synchronized.
  +     *
  +     * @param value Current value loaded from the state.
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    protected void loadState( int value, Configuration state )
  +        throws ConfigurationException
  +    {
  +        m_value = value;
  +        m_valueCount = state.getAttributeAsInteger( "value-count" );
  +    }
  +    
  +    /**
  +     * Called after a state is loaded if the sample period is not the same
  +     *  as the last period saved.
  +     */
  +    protected void postSaveNeedsReset()
  +    {
  +        m_value = 0;
  +        m_valueCount = 0;
       }
       
       /*---------------------------------------------------------------
  
  
  
  1.2       +47 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/AverageValueProfileSample.java
  
  Index: AverageValueProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/AverageValueProfileSample.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AverageValueProfileSample.java	3 Mar 2002 15:59:43 -0000	1.1
  +++ AverageValueProfileSample.java	5 Mar 2002 12:34:32 -0000	1.2
  @@ -7,12 +7,16 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
  +
   /**
    * A ProfileSample which stores the average value set during the sample
    *  period.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/03/03 15:59:43 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   class AverageValueProfileSample
  @@ -52,6 +56,48 @@
           //  if needed.  But reset the value count so that new values will not
           //  be affected by the old.
           m_valueCount = 0;
  +    }
  +    
  +    /**
  +     * Allow subclasses to add information into the saved state.
  +     *
  +     * @param state State configuration.
  +     */
  +    protected void saveState( DefaultConfiguration state )
  +    {
  +        super.saveState( state );
  +        
  +        state.setAttribute( "value-total", Long.toString( m_valueTotal ) );
  +    }
  +    
  +    /**
  +     * Used to load the state, called from AbstractProfileSample.loadState();
  +     * <p>
  +     * Should only be called when synchronized.
  +     *
  +     * @param value Current value loaded from the state.
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    protected void loadState( int value, Configuration state )
  +        throws ConfigurationException
  +    {
  +        super.loadState( value, state );
  +        
  +        m_valueTotal = state.getAttributeAsLong( "value-total" );
  +    }
  +    
  +    /**
  +     * Called after a state is loaded if the sample period is not the same
  +     *  as the last period saved.
  +     */
  +    protected void postSaveNeedsReset()
  +    {
  +        super.postSaveNeedsReset();
  +        
  +        m_valueTotal = 0;
       }
       
       /*---------------------------------------------------------------
  
  
  
  1.2       +30 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/CounterProfileSample.java
  
  Index: CounterProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/CounterProfileSample.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CounterProfileSample.java	3 Mar 2002 15:59:43 -0000	1.1
  +++ CounterProfileSample.java	5 Mar 2002 12:34:32 -0000	1.2
  @@ -7,12 +7,15 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +
   /**
    * A ProfileSample which stores the number of times that increment has been
    *  called during the sample period.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/03/03 15:59:43 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   class CounterProfileSample
  @@ -81,6 +84,32 @@
       protected void advanceToNextSample()
       {
           // Counts do not propagate, so always reset the count to 0.
  +        m_count = 0;
  +    }
  +    
  +    /**
  +     * Used to load the state, called from AbstractProfileSample.loadState();
  +     * <p>
  +     * Should only be called when synchronized.
  +     *
  +     * @param value Current value loaded from the state.
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    protected void loadState( int value, Configuration state )
  +        throws ConfigurationException
  +    {
  +        m_count = value;
  +    }
  +    
  +    /**
  +     * Called after a state is loaded if the sample period is not the same
  +     *  as the last period saved.
  +     */
  +    protected void postSaveNeedsReset()
  +    {
           m_count = 0;
       }
       
  
  
  
  1.3       +247 -2    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/DefaultProfilerManager.java
  
  Index: DefaultProfilerManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/DefaultProfilerManager.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultProfilerManager.java	5 Mar 2002 09:31:03 -0000	1.2
  +++ DefaultProfilerManager.java	5 Mar 2002 12:34:32 -0000	1.3
  @@ -7,6 +7,11 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.io.FileOutputStream;
  +import java.io.InputStream;
  +import java.io.OutputStream;
   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.Iterator;
  @@ -23,12 +28,15 @@
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
  +import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
  +import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   
   /**
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 09:31:03 $
  + * @version CVS $Revision: 1.3 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   public class DefaultProfilerManager
  @@ -42,6 +50,15 @@
       /** Configuration for the ProfileManager */
       private Configuration m_configuration;
       
  +    /** State file. */
  +    private File m_stateFile;
  +    
  +    /** Save state interval. */
  +    private long m_stateInterval;
  +    
  +    /** Last time that the state was saved. */
  +    private long m_lastStateSave;
  +    
       /** Semaphore for actions which must be synchronized */
       private Object m_semaphore = new Object();
       
  @@ -117,6 +134,28 @@
                   m_profilableProxyArray = null;
                   m_profilableDescriptorArray = null;
               }
  +            
  +            // Configure the state file.
  +            Configuration stateFileConf = configuration.getChild( "state-file" );
  +            m_stateInterval = stateFileConf.getAttributeAsLong( "interval", 60000 );
  +            String stateFile = stateFileConf.getValue( null );
  +            if ( stateFile != null )
  +            {
  +                m_stateFile = new File( stateFile );
  +                if ( m_stateFile.exists() )
  +                {
  +                    try
  +                    {
  +                        loadProfilerStateFromFile( m_stateFile );
  +                    }
  +                    catch ( Exception e )
  +                    {
  +                        getLogger().error( "Unable to load the profiler state.  The configuration
" +
  +                            "may have been corruptped.  A backup may have been made in
the same " +
  +                            "directory when it was saved.", e );
  +                    }
  +                }
  +            }
           }
       }
       
  @@ -153,6 +192,8 @@
           {
               m_runner = null;
           }
  +        
  +        saveState();
       }
       
       /*---------------------------------------------------------------
  @@ -223,6 +264,149 @@
           registerProfilableCommon( profilable, profilableName );
       }
       
  +    /**
  +     * Loads the profiler state from the specified file.
  +     *
  +     * @param stateFile File to read the profiler's state from.
  +     *
  +     * @throws Exception if there are any problems loading the state.
  +     */
  +    public void loadProfilerStateFromFile( File stateFile )
  +        throws Exception
  +    {
  +        long now = System.currentTimeMillis();
  +        getLogger().debug( "Loading profiler state from: " + stateFile );
  +        
  +        FileInputStream is = new FileInputStream( stateFile);
  +        try
  +        {
  +            loadProfilerStateFromStream( is );
  +        }
  +        finally
  +        {
  +            is.close();
  +        }
  +        
  +        getLogger().debug( "Loading profiler state took " +
  +            ( System.currentTimeMillis() - now ) + "ms." );
  +    }
  +    
  +    /**
  +     * Loads the profiler state from the specified stream.
  +     *
  +     * @param is Stream to read the profiler's state from.
  +     *
  +     * @throws Exception if there are any problems loading the state.
  +     */
  +    public void loadProfilerStateFromStream( InputStream is )
  +        throws Exception
  +    {
  +        // Ride on top of the Configuration classes to load the state.
  +        DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
  +        Configuration stateConfig = builder.build( is );
  +        
  +        loadProfilerStateFromConfiguration( stateConfig );
  +    }
  +    
  +    /**
  +     * Loads the profiler state from the specified Configuration.
  +     *
  +     * @param state Configuration object to load the state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    public void loadProfilerStateFromConfiguration( Configuration state )
  +        throws ConfigurationException
  +    {
  +        Configuration[] profilableConfs = state.getChildren( "profilable" );
  +        for ( int i = 0; i < profilableConfs.length; i++ )
  +        {
  +            Configuration profilableConf = profilableConfs[i];
  +            String name = profilableConf.getAttribute( "name" );
  +            ProfilableProxy proxy = getProfilableProxy( name );
  +            if ( proxy == null )
  +            {
  +                getLogger().warn( "Profilable entry ignored while loading state because
the " +
  +                    "profilable does not exist: " + name );
  +            }
  +            else
  +            {
  +                proxy.loadState( profilableConf );
  +            }
  +        }
  +    }
  +    
  +    /**
  +     * Saves the Profiler's state to the specified file.
  +     *
  +     * @param stateFile File to write the profiler's state to.
  +     *
  +     * @throws Exception if there are any problems saving the state.
  +     */
  +    public void saveProfilerStateToFile( File stateFile )
  +        throws Exception
  +    {
  +        long now = System.currentTimeMillis();
  +        getLogger().debug( "Saving profiler state to: " + stateFile );
  +        
  +        FileOutputStream os = new FileOutputStream( stateFile );
  +        try
  +        {
  +            saveProfilerStateToStream( os );
  +        }
  +        finally
  +        {
  +            os.close();
  +        }
  +        
  +        getLogger().debug( "Saving profiler state took " +
  +            ( System.currentTimeMillis() - now ) + "ms." );
  +    }
  +    
  +    /**
  +     * Saves the Profiler's state to the specified output stream.
  +     *
  +     * @param os Stream to write the profiler's state to.
  +     *
  +     * @throws Exception if there are any problems saving the state.
  +     */
  +    public void saveProfilerStateToStream( OutputStream os )
  +        throws Exception
  +    {
  +        Configuration stateConfig = saveProfilerStateToConfiguration();
  +        
  +        // Ride on top of the Configuration classes to save the state.
  +        DefaultConfigurationSerializer serializer = new DefaultConfigurationSerializer();
  +        serializer.setIndent( true );
  +        serializer.serialize( os, stateConfig );
  +    }
  +    
  +    /**
  +     * Returns the desktop state as a Configuration object.
  +     *
  +     * @return The desktop state as a Configuration object.
  +     */
  +    public Configuration saveProfilerStateToConfiguration()
  +    {
  +        
  +        DefaultConfiguration state = new DefaultConfiguration( "profiler-state", "-" );
  +            
  +        ProfilableProxy[] profilableProxies = m_profilableProxyArray;
  +        if ( profilableProxies == null )
  +        {
  +            profilableProxies = updateProfilableProxyArray();
  +        }
  +        
  +        for ( int i = 0; i < profilableProxies.length; i++ )
  +        {
  +            state.addChild( profilableProxies[i].saveState() );
  +        }
  +        
  +        return state;
  +    }
  +    
  +    
       /*---------------------------------------------------------------
        * Profilable Methods
        *-------------------------------------------------------------*/
  @@ -305,6 +489,13 @@
                   Thread.sleep( 1000 );
                   
                   memoryProfilePoints();
  +                
  +                // Handle the state file if it is set
  +                long now = System.currentTimeMillis();
  +                if ( now - m_lastStateSave >= m_stateInterval )
  +                {
  +                    saveState();
  +                }
               }
               catch ( Throwable t )
               {
  @@ -317,12 +508,66 @@
        * Methods
        *-------------------------------------------------------------*/
       /**
  +     * Saves the state to the current state file if configured.
  +     */
  +    private void saveState()
  +    {
  +        long now = System.currentTimeMillis();
  +        
  +        // Always set the time even if the save fails so that we don't thrash
  +        m_lastStateSave = now;
  +        
  +        if ( m_stateFile == null )
  +        {
  +            return;
  +        }
  +        
  +        // Rename the old file in case there is a problem
  +        File renameFile = null;
  +        boolean success = false;
  +        if ( m_stateFile.exists() )
  +        {
  +            renameFile = new File( m_stateFile.getAbsolutePath() + "." + now + ".backup"
);
  +            m_stateFile.renameTo( renameFile );
  +        }
  +        
  +        try
  +        {
  +            try
  +            {
  +                saveProfilerStateToFile( m_stateFile );
  +                success = true;
  +            }
  +            catch ( Exception e )
  +            {
  +                getLogger().error( "Unable to save the profiler state.", e );
  +            }
  +        }
  +        finally
  +        {
  +            // Clean up after the renamed file.
  +            if ( renameFile != null )
  +            {
  +                if ( success )
  +                {
  +                    renameFile.delete();
  +                }
  +                else
  +                {
  +                    m_stateFile.delete();
  +                    renameFile.renameTo( m_stateFile );
  +                }
  +            }
  +        }
  +    }
  +    
  +    /**
        * Returns a ProfilableDescriptor based on its name or the name of any
        *  of its children.
        *
        * @param profilableName Name of the Profilable being requested.
        *
  -     * @return A Descriptor of the requested Profilable or null if not found.
  +     * @return A Proxy of the requested Profilable or null if not found.
        */
       private ProfilableProxy getProfilableProxy( String profilableName )
       {
  
  
  
  1.2       +47 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/MaximumValueProfileSample.java
  
  Index: MaximumValueProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/MaximumValueProfileSample.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MaximumValueProfileSample.java	3 Mar 2002 15:59:43 -0000	1.1
  +++ MaximumValueProfileSample.java	5 Mar 2002 12:34:32 -0000	1.2
  @@ -7,12 +7,16 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
  +
   /**
    * A ProfileSample which stores the maximum value set during the sample
    *  period.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/03/03 15:59:43 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   class MaximumValueProfileSample
  @@ -51,6 +55,48 @@
           // Reset the value count and set the value to the last known value.
           m_value = m_lastValue;
           m_valueCount = 0;
  +    }
  +    
  +    /**
  +     * Allow subclasses to add information into the saved state.
  +     *
  +     * @param state State configuration.
  +     */
  +    protected void saveState( DefaultConfiguration state )
  +    {
  +        super.saveState( state );
  +        
  +        state.setAttribute( "last-value", Integer.toString( m_lastValue ) );
  +    }
  +    
  +    /**
  +     * Used to load the state, called from AbstractProfileSample.loadState();
  +     * <p>
  +     * Should only be called when synchronized.
  +     *
  +     * @param value Current value loaded from the state.
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    protected void loadState( int value, Configuration state )
  +        throws ConfigurationException
  +    {
  +        super.loadState( value, state );
  +        
  +        m_lastValue = state.getAttributeAsInteger( "last-value" );
  +    }
  +    
  +    /**
  +     * Called after a state is loaded if the sample period is not the same
  +     *  as the last period saved.
  +     */
  +    protected void postSaveNeedsReset()
  +    {
  +        super.postSaveNeedsReset();
  +        
  +        m_lastValue = 0;
       }
       
       /*---------------------------------------------------------------
  
  
  
  1.2       +47 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/MinimumValueProfileSample.java
  
  Index: MinimumValueProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/MinimumValueProfileSample.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MinimumValueProfileSample.java	3 Mar 2002 15:59:43 -0000	1.1
  +++ MinimumValueProfileSample.java	5 Mar 2002 12:34:32 -0000	1.2
  @@ -7,12 +7,16 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
  +
   /**
    * A ProfileSample which stores the minimum value set during the sample
    *  period.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/03/03 15:59:43 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   class MinimumValueProfileSample
  @@ -51,6 +55,48 @@
           // Reset the value count and set the value to the last known value.
           m_value = m_lastValue;
           m_valueCount = 0;
  +    }
  +    
  +    /**
  +     * Allow subclasses to add information into the saved state.
  +     *
  +     * @param state State configuration.
  +     */
  +    protected void saveState( DefaultConfiguration state )
  +    {
  +        super.saveState( state );
  +        
  +        state.setAttribute( "last-value", Integer.toString( m_lastValue ) );
  +    }
  +    
  +    /**
  +     * Used to load the state, called from AbstractProfileSample.loadState();
  +     * <p>
  +     * Should only be called when synchronized.
  +     *
  +     * @param value Current value loaded from the state.
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    protected void loadState( int value, Configuration state )
  +        throws ConfigurationException
  +    {
  +        super.loadState( value, state );
  +        
  +        m_lastValue = state.getAttributeAsInteger( "last-value" );
  +    }
  +    
  +    /**
  +     * Called after a state is loaded if the sample period is not the same
  +     *  as the last period saved.
  +     */
  +    protected void postSaveNeedsReset()
  +    {
  +        super.postSaveNeedsReset();
  +        
  +        m_lastValue = 0;
       }
       
       /*---------------------------------------------------------------
  
  
  
  1.3       +52 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/ProfilableProxy.java
  
  Index: ProfilableProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/ProfilableProxy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ProfilableProxy.java	5 Mar 2002 09:31:03 -0000	1.2
  +++ ProfilableProxy.java	5 Mar 2002 12:34:32 -0000	1.3
  @@ -12,6 +12,7 @@
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   
   /**
  @@ -21,7 +22,7 @@
    * Not Synchronized.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 09:31:03 $
  + * @version CVS $Revision: 1.3 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   class ProfilableProxy
  @@ -301,6 +302,56 @@
               }
               
               return m_profilePointDescriptorArray;
  +        }
  +    }
  +    
  +    /**
  +     * Saves the current state into a Configuration.
  +     *
  +     * @return The state as a Configuration.
  +     */
  +    Configuration saveState()
  +    {
  +        DefaultConfiguration state = new DefaultConfiguration( "profilable", "-" );
  +        state.addAttribute( "name", m_name );
  +        
  +        ProfilePointProxy[] proxies = getProfilePointProxies();
  +        for ( int i = 0; i < proxies.length; i++ )
  +        {
  +            state.addChild( proxies[i].saveState() );
  +        }
  +        
  +        return state;
  +    }
  +    
  +    /**
  +     * Loads the state into the Profilable.
  +     *
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    void loadState( Configuration state ) throws ConfigurationException
  +    {
  +        synchronized( this )
  +        {
  +            Configuration[] profilePointConfs = state.getChildren( "profile-point" );
  +            for ( int i = 0; i < profilePointConfs.length; i++ )
  +            {
  +                Configuration profilePointConf = profilePointConfs[i];
  +                String name = profilePointConf.getAttribute( "name" );
  +                ProfilePointProxy proxy = getProfilePointProxy( name );
  +                if ( proxy == null )
  +                {
  +                    getLogger().warn( "ProfilePoint entry ignored while loading state because
the " +
  +                        "profile point does not exist: " + name );
  +                }
  +                else
  +                {
  +                    proxy.loadState( profilePointConf );
  +                }
  +            }
           }
       }
   }
  
  
  
  1.3       +52 -1     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/ProfilePointProxy.java
  
  Index: ProfilePointProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/ProfilePointProxy.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ProfilePointProxy.java	4 Mar 2002 03:52:16 -0000	1.2
  +++ ProfilePointProxy.java	5 Mar 2002 12:34:32 -0000	1.3
  @@ -14,6 +14,7 @@
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  +import org.apache.avalon.framework.configuration.DefaultConfiguration;
   import org.apache.avalon.framework.logger.AbstractLogEnabled;
   import org.apache.avalon.framework.logger.Logger;
   
  @@ -28,7 +29,7 @@
    *  It is resolved when the Profilable actually registers the ProfilePoint.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.2 $ $Date: 2002/03/04 03:52:16 $
  + * @version CVS $Revision: 1.3 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   public class ProfilePointProxy
  @@ -701,6 +702,56 @@
               }
               
               return m_sampleDescriptorArray;
  +        }
  +    }
  +    
  +    /**
  +     * Saves the current state into a Configuration.
  +     *
  +     * @return The state as a Configuration.
  +     */
  +    Configuration saveState()
  +    {
  +        DefaultConfiguration state = new DefaultConfiguration( "profile-point", "-" );
  +        state.addAttribute( "name", m_name );
  +        
  +        ProfileSample[] samples = getProfileSamples();
  +        for ( int i = 0; i < samples.length; i++ )
  +        {
  +            state.addChild( samples[i].saveState() );
  +        }
  +        
  +        return state;
  +    }
  +    
  +    /**
  +     * Loads the state into the ProfilePoint.
  +     *
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    void loadState( Configuration state ) throws ConfigurationException
  +    {
  +        synchronized( this )
  +        {
  +            Configuration[] profileSampleConfs = state.getChildren( "profile-sample" );
  +            for ( int i = 0; i < profileSampleConfs.length; i++ )
  +            {
  +                Configuration profileSampleConf = profileSampleConfs[i];
  +                String name = profileSampleConf.getAttribute( "name" );
  +                ProfileSample sample = getProfileSample( name );
  +                if ( sample == null )
  +                {
  +                    getLogger().warn( "ProfileSample entry ignored while loading state
because the " +
  +                        "profile sample does not exist: " + name );
  +                }
  +                else
  +                {
  +                    sample.loadState( profileSampleConf );
  +                }
  +            }
           }
       }
       
  
  
  
  1.2       +77 -58    jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/ProfileSample.java
  
  Index: ProfileSample.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/ProfileSample.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ProfileSample.java	3 Mar 2002 15:59:43 -0000	1.1
  +++ ProfileSample.java	5 Mar 2002 12:34:32 -0000	1.2
  @@ -7,6 +7,8 @@
    */
   package org.apache.avalon.excalibur.altprofile.profiler;
   
  +import org.apache.avalon.framework.configuration.Configuration;
  +import org.apache.avalon.framework.configuration.ConfigurationException;
   import org.apache.avalon.framework.logger.LogEnabled;
   
   /**
  @@ -14,94 +16,111 @@
    * Access to ProfileSamples are synchronized through the ProfileDataSet.
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.1 $ $Date: 2002/03/03 15:59:43 $
  + * @version CVS $Revision: 1.2 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   public interface ProfileSample
  -	extends LogEnabled
  +    extends LogEnabled
   {
       /**
  -	 * Returns the name of the sample.
  -	 *
  -	 * @return The name of the sample.
  -	 */
  +     * Returns the name of the sample.
  +     *
  +     * @return The name of the sample.
  +     */
       String getName();
       
       /**
  -	 * Returns the sample interval.  The period of each sample in millisends.
  -	 *
  -	 * @return The sample interval.
  -	 */
  +     * Returns the sample interval.  The period of each sample in millisends.
  +     *
  +     * @return The sample interval.
  +     */
       long getInterval();
       
       /**
  -	 * Returns the number of samples in the sample history.
  -	 *
  -	 * @return The size of the sample history.
  -	 */
  +     * Returns the number of samples in the sample history.
  +     *
  +     * @return The size of the sample history.
  +     */
       int getSize();
       
       /**
  -	 * Returns the description of the sample.
  -	 *
  -	 * @return The description of the sample.
  -	 */
  +     * Returns the description of the sample.
  +     *
  +     * @return The description of the sample.
  +     */
       String getDescription();
       
  -	/**
  -	 * Returns a Descriptor for the ProfileSample.
  -	 *
  -	 * @return A Descriptor for the ProfileSample.
  -	 */
  -	ProfileSampleDescriptor getDescriptor();
  -	
  -	/**
  -	 * Obtain the value of the sample.  All samples are integers, so the profiled
  -	 * objects must measure quantity (numbers of items), rate (items/period), time in
  -	 * milliseconds, etc.
  -	 *
  -	 * @return The sample value.
  -	 */
  +    /**
  +     * Returns a Descriptor for the ProfileSample.
  +     *
  +     * @return A Descriptor for the ProfileSample.
  +     */
  +    ProfileSampleDescriptor getDescriptor();
  +    
  +    /**
  +     * Obtain the value of the sample.  All samples are integers, so the profiled
  +     * objects must measure quantity (numbers of items), rate (items/period), time in
  +     * milliseconds, etc.
  +     *
  +     * @return The sample value.
  +     */
       int getValue();
       
       /**
  -	 * Obtain the UNIX time of the beginning of the sample.
  -	 *
  -	 * @return The UNIX time of the beginning of the sample.
  -	 */
  +     * Obtain the UNIX time of the beginning of the sample.
  +     *
  +     * @return The UNIX time of the beginning of the sample.
  +     */
       long getTime();
       
       /**
  -	 * Returns the Type of the ProfilePoint which can use the sample.  This
  -	 *  should be the same for all instances of a class.
  -	 * <p>
  -	 * Should be one of the following: ProfilerManager.PROFILE_POINT_TYPE_COUNTER
  -	 *  or ProfilerManager.PROFILE_POINT_TYPE_VALUE
  -	 *
  -	 * @return The Type of the ProfilePoint which can use the sample.
  -	 */
  +     * Returns the Type of the ProfilePoint which can use the sample.  This
  +     *  should be the same for all instances of a class.
  +     * <p>
  +     * Should be one of the following: ProfilerManager.PROFILE_POINT_TYPE_COUNTER
  +     *  or ProfilerManager.PROFILE_POINT_TYPE_VALUE
  +     *
  +     * @return The Type of the ProfilePoint which can use the sample.
  +     */
       int getProfilePointType();
       
       /**
  -	 * Obtains a static snapshot of the ProfileSample.
  -	 *
  -	 * @return A static snapshot of the ProfileSample.
  -	 */
  +     * Obtains a static snapshot of the ProfileSample.
  +     *
  +     * @return A static snapshot of the ProfileSample.
  +     */
       ProfileSampleSnapshot getSnapshot();
       
       /**
  -	 * Registers a ProfileSampleListener with a ProfileSample given a name.
  -	 *
  -	 * @param listener The listener which should start receiving updates from the
  -	 *                 ProfileSample.
  -	 */
  +     * Registers a ProfileSampleListener with a ProfileSample given a name.
  +     *
  +     * @param listener The listener which should start receiving updates from the
  +     *                 ProfileSample.
  +     */
       void addProfileSampleListener( ProfileSampleListener listener );
       
       /**
  -	 * Unregisters a ProfileSampleListener from a ProfileSample given a name.
  -	 *
  -	 * @param listener The listener which should stop receiving updates from the
  -	 *                 ProfileSample.
  -	 */
  +     * Unregisters a ProfileSampleListener from a ProfileSample given a name.
  +     *
  +     * @param listener The listener which should stop receiving updates from the
  +     *                 ProfileSample.
  +     */
       void removeProfileSampleListener( ProfileSampleListener listener );
  +    
  +    /**
  +     * Saves the current state into a Configuration.
  +     *
  +     * @return The state as a Configuration.
  +     */
  +    Configuration saveState();
  +    
  +    /**
  +     * Loads the state into the ProfileSample.
  +     *
  +     * @param state Configuration object to load state from.
  +     *
  +     * @throws ConfigurationException If there were any problems loading the
  +     *                                state.
  +     */
  +    void loadState( Configuration state ) throws ConfigurationException;
   }
  
  
  
  1.4       +4 -4      jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui/ProfilerFrame.java
  
  Index: ProfilerFrame.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/altprofile/profiler/gui/ProfilerFrame.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ProfilerFrame.java	5 Mar 2002 09:40:52 -0000	1.3
  +++ ProfilerFrame.java	5 Mar 2002 12:34:32 -0000	1.4
  @@ -42,7 +42,7 @@
   /**
    *
    * @author <a href="mailto:leif@silveregg.co.jp">Leif Mortenson</a>
  - * @version CVS $Revision: 1.3 $ $Date: 2002/03/05 09:40:52 $
  + * @version CVS $Revision: 1.4 $ $Date: 2002/03/05 12:34:32 $
    * @since 4.1
    */
   public class ProfilerFrame
  @@ -116,7 +116,7 @@
       }
       
       /**
  -     * Loads the desktop state from the specified input stream.
  +     * Loads the desktop state from the specified Configuration.
        */
       public void loadDesktopStateFromConfiguration( Configuration stateConfig,
                                                      boolean showErrorDialog )
  @@ -167,7 +167,7 @@
       }
       
       /**
  -     * Loads the desktop state to the specified file.
  +     * Saves the desktop state to the specified file.
        */
       public void saveDesktopStateToFile( File file )
           throws SAXException, IOException, ConfigurationException
  @@ -202,7 +202,7 @@
       }
       
       /**
  -     * Loads the desktop state from the specified input stream.
  +     * Returns the desktop state as a Configuration object.
        */
       public Configuration saveDesktopStateToConfiguration()
       {
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message