FileDocCategorySizeDatePackage
PeerControlSchedulerImpl.javaAPI DocAzureus 3.0.3.47599Thu Jan 11 11:47:38 GMT 2007com.aelitis.azureus.core.peermanager.control.impl

PeerControlSchedulerImpl

public class PeerControlSchedulerImpl extends Object implements com.aelitis.azureus.core.stats.AzureusCoreStatsProvider, com.aelitis.azureus.core.peermanager.control.PeerControlScheduler

Fields Summary
private static final PeerControlSchedulerImpl
singleton
private Random
random
private Map
instance_map
private List
pending_registrations
private volatile boolean
registrations_changed
private volatile long
latest_time
protected org.gudy.azureus2.core3.util.AEMonitor
this_mon
private long
wait_count
private long
yield_count
private long
total_wait_time
Constructors Summary
protected PeerControlSchedulerImpl()

	
	
	
	
		Set	types = new HashSet();
		
		types.add( AzureusCoreStats.ST_PEER_CONTROL_LOOP_COUNT );
		types.add( AzureusCoreStats.ST_PEER_CONTROL_YIELD_COUNT );
		types.add( AzureusCoreStats.ST_PEER_CONTROL_WAIT_COUNT );
		types.add( AzureusCoreStats.ST_PEER_CONTROL_WAIT_TIME );

		AzureusCoreStats.registerProvider( types, this );
		
		new AEThread( "PeerControlScheduler", true )
		{
			public void
			runSupport()
			{
				schedule();
			}
			
		}.start();
	
Methods Summary
public static com.aelitis.azureus.core.peermanager.control.PeerControlSchedulergetSingleton()

	
	  
	
	
		return( singleton );
	
public voidregister(com.aelitis.azureus.core.peermanager.control.PeerControlInstance instance)

		instanceWrapper wrapper = new instanceWrapper( instance );
		
		wrapper.setNextTick( latest_time + random.nextInt( SCHEDULE_PERIOD_MILLIS ));
		
		try{
			this_mon.enter();
			
			Map	new_map = new HashMap( instance_map );
			
			new_map.put( instance, wrapper );
			
			instance_map = new_map;
			
			pending_registrations.add( wrapper );
			
			registrations_changed = true;
			
		}finally{
			
			this_mon.exit();
		}
	
protected voidschedule()

		latest_time	= SystemTime.getCurrentTime();
		
		SystemTime.registerConsumer(
			new SystemTime.consumer()
			{
				public void
				consume(
					long	time )
				{
					synchronized( PeerControlSchedulerImpl.this ){
						
						latest_time	= time;
						
						PeerControlSchedulerImpl.this.notify();
					}
				}
			});
						
		
		List	instances = new LinkedList();

		long	latest_time_used	= 0;
		
		long	tick_count		= 0;
		long 	last_stats_time	= latest_time;
		
		while( true ){
			
			if ( registrations_changed ){
				
				try{
					this_mon.enter();
					
					Iterator	it = instances.iterator();
					
					while( it.hasNext()){
						
						if (((instanceWrapper)it.next()).isUnregistered()){
							
							it.remove();
						}
					}

					for (int i=0;i<pending_registrations.size();i++){
						
						instances.add( pending_registrations.get(i));
					}
					
					pending_registrations.clear();
					
					registrations_changed	= false;
					
				}finally{
					
					this_mon.exit();
				}	
			}
							
			for (Iterator it=instances.iterator();it.hasNext();){
				
				instanceWrapper	inst = (instanceWrapper)it.next();
									
				long	target = inst.getNextTick();
				
				long	diff = target - latest_time_used;			
				
				if ( diff <= 0 || diff > SCHEDULE_PERIOD_MILLIS ){
					
					tick_count++;
					
					inst.schedule();
					
					long new_target = target + SCHEDULE_PERIOD_MILLIS;
					
					diff = new_target - latest_time_used;
					
					if ( diff <= 0 || diff > SCHEDULE_PERIOD_MILLIS ){
						
						new_target = latest_time_used + SCHEDULE_PERIOD_MILLIS;
					}
					
					inst.setNextTick( new_target );
				}
			}
						
			synchronized( this ){
				
				if ( latest_time == latest_time_used ){
					
					wait_count++;
					
					try{
						long wait_start = SystemTime.getHighPrecisionCounter();
						
						wait();
						
						long wait_time 	= SystemTime.getHighPrecisionCounter() - wait_start;

						total_wait_time += wait_time;
						
					}catch( Throwable e ){
						
						Debug.printStackTrace(e);
					}
					
				}else{
					
					yield_count++;
					
					Thread.yield();
				}
				
				latest_time_used	= latest_time;
			}
			
			long	stats_diff =  latest_time_used - last_stats_time;
			
			if ( stats_diff > 10000 ){
				
				// System.out.println( "stats: time = " + stats_diff + ", ticks = " + tick_count + ", inst = " + instances.size());
				
				last_stats_time	= latest_time_used;
				
				tick_count	= 0;
			}
		}
	
public voidunregister(com.aelitis.azureus.core.peermanager.control.PeerControlInstance instance)

		try{
			this_mon.enter();
			
			Map	new_map = new HashMap( instance_map );
			
			instanceWrapper wrapper = (instanceWrapper)new_map.remove(instance);
			
			if ( wrapper == null ){
				
				Debug.out( "instance wrapper not found" );
				
				return;
			}
				
			wrapper.unregister();
			
			instance_map = new_map;
			
			registrations_changed = true;
			
		}finally{
			
			this_mon.exit();
		}
	
public voidupdateStats(java.util.Set types, java.util.Map values)

			//read
		
		if ( types.contains( AzureusCoreStats.ST_PEER_CONTROL_LOOP_COUNT )){
			
			values.put( AzureusCoreStats.ST_PEER_CONTROL_LOOP_COUNT, new Long( wait_count + yield_count ));
		}
		if ( types.contains( AzureusCoreStats.ST_PEER_CONTROL_YIELD_COUNT )){
			
			values.put( AzureusCoreStats.ST_PEER_CONTROL_YIELD_COUNT, new Long( yield_count ));
		}
		if ( types.contains( AzureusCoreStats.ST_PEER_CONTROL_WAIT_COUNT )){
			
			values.put( AzureusCoreStats.ST_PEER_CONTROL_WAIT_COUNT, new Long( wait_count ));
		}
		if ( types.contains( AzureusCoreStats.ST_PEER_CONTROL_WAIT_TIME )){
			
			values.put( AzureusCoreStats.ST_PEER_CONTROL_WAIT_TIME, new Long( total_wait_time ));
		}