comparison src/org/eclipse/jetty/io/nio/SelectorManager.java @ 950:a778413aefc0

add SaneSelector
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 12 Oct 2016 14:37:56 -0600
parents e9088af3787f
children e542a9cc75ef
comparison
equal deleted inserted replaced
949:e9088af3787f 950:a778413aefc0
22 import java.nio.channels.CancelledKeyException; 22 import java.nio.channels.CancelledKeyException;
23 import java.nio.channels.Channel; 23 import java.nio.channels.Channel;
24 import java.nio.channels.ClosedSelectorException; 24 import java.nio.channels.ClosedSelectorException;
25 import java.nio.channels.SelectableChannel; 25 import java.nio.channels.SelectableChannel;
26 import java.nio.channels.SelectionKey; 26 import java.nio.channels.SelectionKey;
27 import java.nio.channels.Selector;
28 import java.nio.channels.ServerSocketChannel; 27 import java.nio.channels.ServerSocketChannel;
29 import java.nio.channels.SocketChannel; 28 import java.nio.channels.SocketChannel;
30 import java.util.ArrayList; 29 import java.util.ArrayList;
31 import java.util.List; 30 import java.util.List;
32 import java.util.Collections; 31 import java.util.Collections;
108 SelectSet[] sets = _selectSet; 107 SelectSet[] sets = _selectSet;
109 if (sets!=null) 108 if (sets!=null)
110 { 109 {
111 SelectSet set=sets[s]; 110 SelectSet set=sets[s];
112 set.addChange(channel); 111 set.addChange(channel);
113 set.wakeup();
114 } 112 }
115 } 113 }
116 114
117 /* ------------------------------------------------------------ */ 115 /* ------------------------------------------------------------ */
118 /** 116 /**
232 public class SelectSet implements Dumpable 230 public class SelectSet implements Dumpable
233 { 231 {
234 private final int _setID; 232 private final int _setID;
235 private volatile long _now = System.currentTimeMillis(); 233 private volatile long _now = System.currentTimeMillis();
236 234
237 private volatile Selector _selector; 235 private volatile SaneSelector _selector;
238 236
239 private volatile Thread _selecting; 237 private volatile Thread _selecting;
240 private int _busySelects; 238 private int _busySelects;
241 private long _monitorNext; 239 private long _monitorNext;
242 private boolean _pausing; 240 private boolean _pausing;
249 _setID=acceptorID; 247 _setID=acceptorID;
250 248
251 _idleTick = System.currentTimeMillis(); 249 _idleTick = System.currentTimeMillis();
252 250
253 // create a selector; 251 // create a selector;
254 _selector = Selector.open(); 252 _selector = new SaneSelector();
255 _monitorNext=System.currentTimeMillis()+__MONITOR_PERIOD; 253 _monitorNext=System.currentTimeMillis()+__MONITOR_PERIOD;
256 } 254 }
257 255
258 private void addChange(SocketChannel channel) 256 private void addChange(SocketChannel channel)
259 { 257 {
260 try { 258 try {
261 SelectionKey key = channel.register(_selector,SelectionKey.OP_READ,null); 259 SelectionKey key = _selector.register(channel,SelectionKey.OP_READ,null);
262 SelectChannelEndPoint endpoint = createEndPoint(channel,key); 260 SelectChannelEndPoint endpoint = createEndPoint(channel,key);
263 key.attach(endpoint); 261 key.attach(endpoint);
264 endpoint.schedule(); 262 endpoint.schedule();
265 } catch(IOException e) { 263 } catch(IOException e) {
266 LOG.warn("",e); 264 LOG.warn("",e);
280 public void doSelect() throws IOException 278 public void doSelect() throws IOException
281 { 279 {
282 try 280 try
283 { 281 {
284 _selecting=Thread.currentThread(); 282 _selecting=Thread.currentThread();
285 final Selector selector=_selector; 283 final SaneSelector selector = _selector;
286 // Stopped concurrently ? 284 // Stopped concurrently ?
287 if (selector == null) 285 if (selector == null)
288 return; 286 return;
289 287
290 // Do and instant select to see if any connections can be handled. 288 // Do and instant select to see if any connections can be handled.
291 int selected=selector.selectNow(); 289 // int selected = selector.selectNow();
290 int selected = selector.select();
292 291
293 _now = System.currentTimeMillis(); 292 _now = System.currentTimeMillis();
294 293 /*
295 // if no immediate things to do 294 // if no immediate things to do
296 if (selected==0 && selector.selectedKeys().isEmpty()) 295 if (selected==0 && selector.selectedKeys().isEmpty())
297 { 296 {
297
298 // If we are in pausing mode 298 // If we are in pausing mode
299 if (_pausing) 299 if (_pausing)
300 { 300 {
301 try 301 try
302 { 302 {
315 // If we should wait with a select 315 // If we should wait with a select
316 if (wait>0) 316 if (wait>0)
317 { 317 {
318 long before = _now; 318 long before = _now;
319 selector.select(wait); 319 selector.select(wait);
320 // selector.select(10000L);
320 _now = System.currentTimeMillis(); 321 _now = System.currentTimeMillis();
321 322
322 // If we are monitoring for busy selector 323 // If we are monitoring for busy selector
323 // and this select did not wait more than 1ms 324 // and this select did not wait more than 1ms
324 if (__MONITOR_PERIOD>0 && _now-before <=1) 325 if (__MONITOR_PERIOD>0 && _now-before <=1)
338 } 339 }
339 } 340 }
340 } 341 }
341 } 342 }
342 } 343 }
343 344 */
344 // have we been destroyed while sleeping 345 // have we been destroyed while sleeping
345 if (_selector==null || !selector.isOpen()) 346 if (_selector==null || !selector.isOpen())
346 return; 347 return;
347 348
348 // Look for things to do 349 // Look for things to do
436 437
437 // Everything always handled 438 // Everything always handled
438 selector.selectedKeys().clear(); 439 selector.selectedKeys().clear();
439 440
440 _now = System.currentTimeMillis(); 441 _now = System.currentTimeMillis();
441 442 /*
442 // Idle tick 443 // Idle tick
443 if (_now-_idleTick>__IDLE_TICK) 444 if (_now-_idleTick>__IDLE_TICK)
444 { 445 {
445 _idleTick = _now; 446 _idleTick = _now;
446 447
459 } 460 }
460 public String toString() {return "Idle-"+super.toString();} 461 public String toString() {return "Idle-"+super.toString();}
461 }); 462 });
462 463
463 } 464 }
464 465 */
465 // Reset busy select monitor counts 466 // Reset busy select monitor counts
466 if (__MONITOR_PERIOD>0 && _now>_monitorNext) 467 if (__MONITOR_PERIOD>0 && _now>_monitorNext)
467 { 468 {
468 _busySelects=0; 469 _busySelects=0;
469 _pausing=false; 470 _pausing=false;
495 496
496 public long getNow() 497 public long getNow()
497 { 498 {
498 return _now; 499 return _now;
499 } 500 }
500 501 /*
501 public void wakeup() 502 public void wakeup()
502 { 503 {
503 Selector selector = _selector; 504 SaneSelector selector = _selector;
504 if (selector!=null) 505 if (selector!=null)
505 selector.wakeup(); 506 selector.wakeup();
506 } 507 }
507 508 */
508 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException 509 private SelectChannelEndPoint createEndPoint(SocketChannel channel, SelectionKey sKey) throws IOException
509 { 510 {
510 SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,this,sKey, _maxIdleTime); 511 SelectChannelEndPoint endp = new SelectChannelEndPoint(channel,this,sKey, _maxIdleTime);
511 endp.setConnection(getManager().newConnection(channel,endp, sKey.attachment())); 512 endp.setConnection(getManager().newConnection(channel,endp, sKey.attachment()));
512 LOG.debug("created {}",endp); 513 LOG.debug("created {}",endp);
519 LOG.debug("destroyEndPoint {}",endp); 520 LOG.debug("destroyEndPoint {}",endp);
520 _endPoints.remove(endp); 521 _endPoints.remove(endp);
521 endp.getConnection().onClose(); 522 endp.getConnection().onClose();
522 } 523 }
523 524
524 Selector getSelector() 525 SaneSelector getSelector()
525 { 526 {
526 return _selector; 527 return _selector;
527 } 528 }
528 529
529 void stop() throws Exception 530 void stop() throws Exception
530 { 531 {
531 // Spin for a while waiting for selector to complete 532 // Spin for a while waiting for selector to complete
532 // to avoid unneccessary closed channel exceptions 533 // to avoid unneccessary closed channel exceptions
534 /*
533 try 535 try
534 { 536 {
535 for (int i=0;i<100 && _selecting!=null;i++) 537 for (int i=0;i<100 && _selecting!=null;i++)
536 { 538 {
537 wakeup(); 539 _selector.wakeup();
538 Thread.sleep(10); 540 Thread.sleep(10);
539 } 541 }
540 } 542 }
541 catch(Exception e) 543 catch(Exception e)
542 { 544 {
543 LOG.warn("",e); 545 LOG.warn("",e);
544 } 546 }
545 547 */
546 // close endpoints and selector 548 // close endpoints and selector
547 synchronized (this) 549 synchronized (this)
548 { 550 {
549 Selector selector=_selector; 551 SaneSelector selector=_selector;
550 for (SelectionKey key:selector.keys()) 552 for (SelectionKey key:selector.keys())
551 { 553 {
552 if (key==null) 554 if (key==null)
553 continue; 555 continue;
554 Object att=key.attachment(); 556 Object att=key.attachment();
591 AggregateLifeCycle.dump(out,indent,Collections.emptyList()); 593 AggregateLifeCycle.dump(out,indent,Collections.emptyList());
592 } 594 }
593 595
594 public String toString() 596 public String toString()
595 { 597 {
596 Selector selector=_selector; 598 SaneSelector selector=_selector;
597 return String.format("%s keys=%d selected=%d", 599 return String.format("%s keys=%d selected=%d",
598 super.toString(), 600 super.toString(),
599 selector != null && selector.isOpen() ? selector.keys().size() : -1, 601 selector != null && selector.isOpen() ? selector.keys().size() : -1,
600 selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1); 602 selector != null && selector.isOpen() ? selector.selectedKeys().size() : -1);
601 } 603 }