PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/azureus-4.7.0.2/com/aelitis/azureus/core/networkmanager/impl/tcp/SelectorGuard.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 161 lines | 83 code | 27 blank | 51 comment | 17 complexity | db752615fef7cea91e5c432fdfe8127d MD5 | raw file
  1. /*
  2. * Created on Jul 28, 2004
  3. * Created by Alon Rohter
  4. * Copyright (C) 2004, 2005, 2006 Aelitis, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * AELITIS, SAS au capital de 46,603.30 euros
  19. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  20. *
  21. */
  22. package com.aelitis.azureus.core.networkmanager.impl.tcp;
  23. import org.gudy.azureus2.core3.logging.*;
  24. import org.gudy.azureus2.core3.util.*;
  25. /**
  26. * Temp class designed to help detect Selector anomalies and cleanly re-open if necessary.
  27. *
  28. * NOTE:
  29. * As of JVM 1.4.2_03, after network connection disconnect/reconnect, usually-blocking
  30. * select() and select(long) calls no longer block, and will instead return immediately.
  31. * This can cause selector spinning and 100% cpu usage.
  32. * See:
  33. * http://forum.java.sun.com/thread.jsp?forum=4&thread=293213
  34. * http://developer.java.sun.com/developer/bugParade/bugs/4850373.html
  35. * http://developer.java.sun.com/developer/bugParade/bugs/4881228.html
  36. * Fixed in JVM 1.4.2_05+ and 1.5b2+
  37. */
  38. public class SelectorGuard {
  39. private static final int SELECTOR_SPIN_THRESHOLD = 200;
  40. private static final int SELECTOR_FAILURE_THRESHOLD = 10000;
  41. private static final int MAX_IGNORES = 5;
  42. private boolean marked = false;
  43. private int consecutiveZeroSelects = 0;
  44. private long beforeSelectTime;
  45. private long select_op_time;
  46. private final String type;
  47. private final GuardListener listener;
  48. private int ignores = 0;
  49. /**
  50. * Create a new SelectorGuard with the given failed count threshold.
  51. */
  52. public SelectorGuard( String _type, GuardListener _listener ) {
  53. this.type = _type;
  54. this.listener = _listener;
  55. }
  56. public String
  57. getType()
  58. {
  59. return( type );
  60. }
  61. /**
  62. * Run this method right before the select() operation to
  63. * mark the start time.
  64. */
  65. public void markPreSelectTime() {
  66. beforeSelectTime = SystemTime.getCurrentTime();
  67. marked = true;
  68. }
  69. /**
  70. * Checks whether selector is still OK, and not spinning.
  71. */
  72. public void verifySelectorIntegrity( int num_keys_ready, long time_threshold ) {
  73. if( num_keys_ready > 0 ) { //non-zero select, so OK
  74. ignores++;
  75. if( ignores > MAX_IGNORES ) { //allow MAX_IGNORES / SELECTOR_SPIN_THRESHOLD to be successful select ops and still trigger a spin alert
  76. ignores = 0;
  77. consecutiveZeroSelects = 0;
  78. }
  79. return;
  80. }
  81. if (marked) marked = false;
  82. else Debug.out("Error: You must run markPreSelectTime() before calling isSelectorOK");
  83. select_op_time = SystemTime.getCurrentTime() - beforeSelectTime;
  84. if( select_op_time > time_threshold || select_op_time < 0 ) {
  85. //zero-select, but over the time threshold, so OK
  86. consecutiveZeroSelects = 0;
  87. return;
  88. }
  89. //if we've gotten here, then we have a potential selector anomalie
  90. consecutiveZeroSelects++;
  91. if( consecutiveZeroSelects % 20 == 0 && Constants.isWindows ) {
  92. // getting triggered with 20 +_ sometimes 40 due to general high CPU usage
  93. if ( consecutiveZeroSelects > 40 ){
  94. Debug.out( "consecutiveZeroSelects=" +consecutiveZeroSelects );
  95. }
  96. }
  97. if( consecutiveZeroSelects > SELECTOR_SPIN_THRESHOLD ) {
  98. if( Constants.isWindows && (Constants.JAVA_VERSION.startsWith("1.4") || Constants.JAVA_VERSION.startsWith("1.5"))) {
  99. //Under windows, it seems that selector spin can sometimes appear when >63 socket channels are registered with a selector
  100. //Should be fixed in later 1.5, but play it safe and assume 1.6 or newer only.
  101. if( !listener.safeModeSelectEnabled() ) {
  102. String msg = "Likely faulty socket selector detected: reverting to safe-mode socket selection. [JRE " +Constants.JAVA_VERSION+"]\n";
  103. msg += "Please see " +Constants.AZUREUS_WIKI+ "LikelyFaultySocketSelector for help.";
  104. Debug.out( msg );
  105. Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
  106. consecutiveZeroSelects = 0;
  107. listener.spinDetected();
  108. return;
  109. }
  110. }
  111. else {
  112. //under linux, it seems that selector spin is somewhat common, but normal??? behavior, so just sleep a bit
  113. consecutiveZeroSelects = 0;
  114. try{ Thread.sleep( 50 ); }catch( Throwable t) {t.printStackTrace();}
  115. return;
  116. }
  117. }
  118. if( consecutiveZeroSelects > SELECTOR_FAILURE_THRESHOLD ) { //should only happen under Windows + JRE 1.4
  119. String msg = "Likely network disconnect/reconnect: Repairing socket channel selector. [JRE " +Constants.JAVA_VERSION+"]\n";
  120. msg += "Please see " +Constants.AZUREUS_WIKI+ "LikelyNetworkDisconnectReconnect for help.";
  121. Debug.out( msg );
  122. Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, msg));
  123. consecutiveZeroSelects = 0;
  124. listener.failureDetected();
  125. return;
  126. }
  127. //not yet over the count threshold
  128. }
  129. public interface GuardListener {
  130. public boolean safeModeSelectEnabled();
  131. public void spinDetected();
  132. public void failureDetected();
  133. }
  134. }