FileDocCategorySizeDatePackage
WriteController.javaAPI DocAzureus 3.0.3.411463Fri Dec 29 18:51:30 GMT 2006com.aelitis.azureus.core.networkmanager.impl

WriteController

public class WriteController extends Object implements com.aelitis.azureus.core.stats.AzureusCoreStatsProvider
Processes writes of write-entities and handles the write selector.

Fields Summary
private static int
IDLE_SLEEP_TIME
private static boolean
AGGRESIVE_WRITE
private volatile ArrayList
normal_priority_entities
private volatile ArrayList
high_priority_entities
private final AEMonitor
entities_mon
private int
next_normal_position
private int
next_high_position
private int
aggressive_np_normal_priority_count
private int
aggressive_np_high_priority_count
private long
wait_count
private long
progress_count
private long
non_progress_count
private com.aelitis.azureus.core.networkmanager.EventWaiter
write_waiter
Constructors Summary
public WriteController()
Create a new write controller.

  
          
    
    
    //start write handler processing
    Thread write_processor_thread = new AEThread( "WriteController:WriteProcessor" ) {
      public void runSupport() {
        writeProcessorLoop();
      }
    };
    write_processor_thread.setDaemon( true );
    write_processor_thread.setPriority( Thread.MAX_PRIORITY - 1 );
    write_processor_thread.start();
    
    Set	types = new HashSet();
    
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_WAIT_COUNT );
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_NP_COUNT );
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_P_COUNT );
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_ENTITY_COUNT );
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_CON_COUNT );
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_READY_CON_COUNT );
    types.add( AzureusCoreStats.ST_NET_WRITE_CONTROL_READY_BYTE_COUNT );
       
    AzureusCoreStats.registerProvider(
    	types,
    	this );
    
    AEDiagnostics.addEvidenceGenerator(
    	new AEDiagnosticsEvidenceGenerator()
    	{
    		public void 
    		generate(
    			IndentWriter writer ) 
    		{
				writer.println( "Write Controller" );
					
				try{
					writer.indent();
					
					ArrayList ref = normal_priority_entities;

					writer.println( "normal - " + ref.size());
					    
					for (int i=0;i<ref.size();i++){
						
						RateControlledEntity entity = (RateControlledEntity)ref.get( i );

						writer.println( entity.getString());
					}

					ref = high_priority_entities;

					writer.println( "priority - " + ref.size());
					    
					for (int i=0;i<ref.size();i++){
						
						RateControlledEntity entity = (RateControlledEntity)ref.get( i );

						writer.println( entity.getString());
					}
				}finally{
					
					writer.exdent();
				}
    		}
    	});
  
Methods Summary
public voidaddWriteEntity(RateControlledEntity entity)
Add the given entity to the controller for write processing.

param
entity to process writes for

    try {  entities_mon.enter();
      if( entity.getPriority() == RateControlledEntity.PRIORITY_HIGH ) {
        //copy-on-write
        ArrayList high_new = new ArrayList( high_priority_entities.size() + 1 );
        high_new.addAll( high_priority_entities );
        high_new.add( entity );
        high_priority_entities = high_new;
      }
      else {
        //copy-on-write
        ArrayList norm_new = new ArrayList( normal_priority_entities.size() + 1 );
        norm_new.addAll( normal_priority_entities );
        norm_new.add( entity );
        normal_priority_entities = norm_new;
      }
    }
    finally {  entities_mon.exit();  }
  
private booleandoHighPriorityWrite()

    RateControlledEntity ready_entity = getNextReadyHighPriorityEntity();
    if( ready_entity != null ){
    	if ( ready_entity.doProcessing( write_waiter ) ) {
    
    		progress_count++;
    		
    		return true;
    		
    	}else{
    		
    		non_progress_count++;
    		
    		if ( AGGRESIVE_WRITE ){
    			
    			aggressive_np_high_priority_count++;
    			
    			if ( aggressive_np_high_priority_count < high_priority_entities.size()){
    				
    				return( true );
    				
    			}else{
    				
    				aggressive_np_high_priority_count = 0;
    			}
    		}
    	}
    }
    return false;
  
private booleandoNormalPriorityWrite()

    RateControlledEntity ready_entity = getNextReadyNormalPriorityEntity();
    if( ready_entity != null ){
    	
    	if ( ready_entity.doProcessing( write_waiter ) ) {
    
    		progress_count++;
    		
    		return true;
    	}else{
    		
    		non_progress_count++;
			
    		if ( AGGRESIVE_WRITE ){
    			
     			aggressive_np_normal_priority_count++;
    			     			
    			if ( aggressive_np_normal_priority_count < normal_priority_entities.size()){
    				
    				return( true );
    				
    			}else{
    				
    				aggressive_np_normal_priority_count = 0;
    			}
    		}
    	}
    }
    return false;
  
private RateControlledEntitygetNextReadyHighPriorityEntity()

    ArrayList ref = high_priority_entities;
    
    int size = ref.size();
    int num_checked = 0;

    while( num_checked < size ) {
      next_high_position = next_high_position >= size ? 0 : next_high_position;  //make circular
      RateControlledEntity entity = (RateControlledEntity)ref.get( next_high_position );
      next_high_position++;
      num_checked++;
      if( entity.canProcess( write_waiter ) ) {  //is ready
        return entity;
      }
    }

    return null;  //none found ready
  
private RateControlledEntitygetNextReadyNormalPriorityEntity()

    ArrayList ref = normal_priority_entities;
    
    int size = ref.size();
    int num_checked = 0;

    while( num_checked < size ) {
      next_normal_position = next_normal_position >= size ? 0 : next_normal_position;  //make circular
      RateControlledEntity entity = (RateControlledEntity)ref.get( next_normal_position );
      next_normal_position++;
      num_checked++;
      if( entity.canProcess( write_waiter ) ) {  //is ready
        return entity;
      }
    }

    return null;  //none found ready
  
public voidremoveWriteEntity(RateControlledEntity entity)
Remove the given entity from the controller.

param
entity to remove from write processing

    try {  entities_mon.enter();
      if( entity.getPriority() == RateControlledEntity.PRIORITY_HIGH ) {
        //copy-on-write
        ArrayList high_new = new ArrayList( high_priority_entities );
        high_new.remove( entity );
        high_priority_entities = high_new;
      }
      else {
        //copy-on-write
        ArrayList norm_new = new ArrayList( normal_priority_entities );
        norm_new.remove( entity );
        normal_priority_entities = norm_new;
      }
    }
    finally {  entities_mon.exit();  }
  
public voidupdateStats(java.util.Set types, java.util.Map values)

	  if ( types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_WAIT_COUNT )){

		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_WAIT_COUNT, new Long( wait_count ));
	  }
	  
	  if ( types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_NP_COUNT )){

		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_NP_COUNT, new Long( non_progress_count ));
	  }
	  
	  if ( types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_P_COUNT )){

		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_P_COUNT, new Long( progress_count ));
	  }

	  if ( types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_ENTITY_COUNT )){

		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_ENTITY_COUNT, new Long( high_priority_entities.size() + normal_priority_entities.size()));
	  }
	  
	  if ( 	types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_CON_COUNT ) ||
			types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_READY_CON_COUNT ) ||
			types.contains( AzureusCoreStats.ST_NET_WRITE_CONTROL_READY_BYTE_COUNT )){
			   
		  long	ready_bytes			= 0;
		  int	ready_connections	= 0;
		  int	connections			= 0;
		  
		  ArrayList[] refs = { normal_priority_entities, high_priority_entities };
		    
		  for (int i=0;i<refs.length;i++){
			  
			  ArrayList	ref = refs[i];
			 
			  for (int j=0;j<ref.size();j++){
				  
			      RateControlledEntity entity = (RateControlledEntity)ref.get( j );
			      
			      connections 		+= entity.getConnectionCount();
			      
			      ready_connections += entity.getReadyConnectionCount( write_waiter );
			      
			      ready_bytes		+= entity.getBytesReadyToWrite();
			  }
		  }
		  
		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_CON_COUNT, new Long( connections ));
		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_READY_CON_COUNT, new Long( ready_connections ));
		  values.put( AzureusCoreStats.ST_NET_WRITE_CONTROL_READY_BYTE_COUNT, new Long( ready_bytes ));
	  }
  
private voidwriteProcessorLoop()

    boolean check_high_first = true;
    
    while( true ) {
      try {
        if( check_high_first ) {
          check_high_first = false;
          if( !doHighPriorityWrite() ) {
            if( !doNormalPriorityWrite() ) {
              if ( write_waiter.waitForEvent( IDLE_SLEEP_TIME )){
            	  wait_count++;
              }
            }
          }
        }
        else {
          check_high_first = true;
          if( !doNormalPriorityWrite() ) {
            if( !doHighPriorityWrite() ) {
            	if ( write_waiter.waitForEvent( IDLE_SLEEP_TIME )){
            		wait_count++;
            	}
            }
          }
        }
      }
      catch( Throwable t ) {
        Debug.out( "writeProcessorLoop() EXCEPTION: ", t );
      }
    }