SelectorGuardpublic class SelectorGuard extends Object Temp class designed to help detect Selector anomalies and cleanly re-open if necessary.
NOTE:
As of JVM 1.4.2_03, after network connection disconnect/reconnect, usually-blocking
select() and select(long) calls no longer block, and will instead return immediately.
This can cause selector spinning and 100% cpu usage.
See:
http://forum.java.sun.com/thread.jsp?forum=4&thread=293213
http://developer.java.sun.com/developer/bugParade/bugs/4850373.html
http://developer.java.sun.com/developer/bugParade/bugs/4881228.html
Fixed in JVM 1.4.2_05+ and 1.5b2+ |
Fields Summary |
---|
private static final int | SELECTOR_SPIN_THRESHOLD | private static final int | SELECTOR_FAILURE_THRESHOLD | private static final int | MAX_IGNORES | private boolean | marked | private int | consecutiveZeroSelects | private long | beforeSelectTime | private long | select_op_time | private final String | type | private final GuardListener | listener | private int | ignores |
Constructors Summary |
---|
public SelectorGuard(String _type, GuardListener _listener)Create a new SelectorGuard with the given failed count threshold.
this.type = _type;
this.listener = _listener;
|
Methods Summary |
---|
public java.lang.String | getType()
return( type );
| public void | markPreSelectTime()Run this method right before the select() operation to
mark the start time.
beforeSelectTime = SystemTime.getCurrentTime();
marked = true;
| public void | verifySelectorIntegrity(int num_keys_ready, long time_threshold)Checks whether selector is still OK, and not spinning.
if( num_keys_ready > 0 ) { //non-zero select, so OK
ignores++;
if( ignores > MAX_IGNORES ) { //allow MAX_IGNORES / SELECTOR_SPIN_THRESHOLD to be successful select ops and still trigger a spin alert
ignores = 0;
consecutiveZeroSelects = 0;
}
return;
}
if (marked) marked = false;
else Debug.out("Error: You must run markPreSelectTime() before calling isSelectorOK");
select_op_time = SystemTime.getCurrentTime() - beforeSelectTime;
if( select_op_time > time_threshold || select_op_time < 0 ) {
//zero-select, but over the time threshold, so OK
consecutiveZeroSelects = 0;
return;
}
//if we've gotten here, then we have a potential selector anomalie
consecutiveZeroSelects++;
if( consecutiveZeroSelects % 20 == 0 && Constants.isWindows ) {
// getting triggered with 20 +_ sometimes 40 due to general high CPU usage
if ( consecutiveZeroSelects > 40 ){
Debug.out( "consecutiveZeroSelects=" +consecutiveZeroSelects );
}
}
if( consecutiveZeroSelects > SELECTOR_SPIN_THRESHOLD ) {
if( Constants.isWindows && (Constants.JAVA_VERSION.startsWith("1.4") || Constants.JAVA_VERSION.startsWith("1.5"))) {
//Under windows, it seems that selector spin can sometimes appear when >63 socket channels are registered with a selector
//Should be fixed in later 1.5, but play it safe and assume 1.6 or newer only.
if( !listener.safeModeSelectEnabled() ) {
String msg = "Likely faulty socket selector detected: reverting to safe-mode socket selection. [JRE " +Constants.JAVA_VERSION+"]\n";
msg += "Please see " +Constants.AZUREUS_WIKI+ "LikelyFaultySocketSelector for help.";
Debug.out( msg );
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
consecutiveZeroSelects = 0;
listener.spinDetected();
return;
}
}
else {
//under linux, it seems that selector spin is somewhat common, but normal??? behavior, so just sleep a bit
consecutiveZeroSelects = 0;
try{ Thread.sleep( 50 ); }catch( Throwable t) {t.printStackTrace();}
return;
}
}
if( consecutiveZeroSelects > SELECTOR_FAILURE_THRESHOLD ) { //should only happen under Windows + JRE 1.4
String msg = "Likely network disconnect/reconnect: Repairing socket channel selector. [JRE " +Constants.JAVA_VERSION+"]\n";
msg += "Please see " +Constants.AZUREUS_WIKI+ "LikelyNetworkDisconnectReconnect for help.";
Debug.out( msg );
Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
consecutiveZeroSelects = 0;
listener.failureDetected();
return;
}
//not yet over the count threshold
|
|