最近打算用kilim做一个rpc框架, kilim有自己的nio框架 而在业界有强劲的netty和mina。
所以问了一下kilim的作者,他的回答说 因为底层用的都是java nio的api,所以留给nio框架最主要的问题是这2点:
(i) 为了处理很多socket连接和优化吞吐量,会导致了大量的线程切换。
Amount of thread switching done to handle n numbers of sockets and optimizing for throughput.
(ii) 有很多次的selector的中断和调用,唤醒seletor是很费资源的操作。
所以能做的优化有以下几点:
1、对read writer process操作分别作轻量级的scheduler,基于actor。
2、有个trick,就是read或者write操作时候,但没有读满或者写完的情况下,并不是立即返回并再次注册channel到selector,而是再尝试若干次(3次),再返回并注册到selector。在mina中也有同样的处理。不同之处在于kilim会yield的当前task,而mina为了避免线程切换,只是做了简单的while循环。但目的都是减少线程切换和避免多次注册 selector。
mina 处理代码
- for (int i = WRITE_SPIN_COUNT; i > 0; i --)
- {
- localWrittenBytes = ch.write(buf.buf());
- if (localWrittenBytes != 0 || !buf.hasRemaining())
- {
- break;
- }
- }
kilim nio的处理
- while (remaining > 0)
- {
- if (n == 0)
- {
- yieldCount++;
- if (yieldCount < YIELD_COUNT)
- {
- Task.yield(); // don't go back to selector yet.
- } else
- {
- pauseUntilWritable();
- yieldCount = 0;
- }
- }
- n = ch.write(buf);
- remaining -= n;
- }
除了上面说的2个因素以外,还有有哪些因素会影响nio的性能?
原文如下:
I have not tested netty, but here's my experience. All NIO frameworks, Kilim included, are comparable because they use the same underlying NIO API. The difference in performance _may_ stem from the following two sources:
(i) Amount of thread switching done to handle n numbers of sockets and optimizing for throughput.
(ii) Number of times the selector is interrupted and invoked. Waking up the selector is an expensive operation.
The first one is entirely up to the user of the Kilim NIO library. A typical server request consists of one or more reads to accumulate the frame, processing the packet, and one or more writes to write a packet to the socket. One can split up this work between multiple schedulers if you wish. By default, all reading and writing is done outside of the selector's thread. Which brings me to the next point.
I have optimized the access to the selector, by avoiding using it as much as possible. If a socket read or write is unable to transfer any bytes, then the task simply yields. Other runnable tasks get a chance to run. When the task is subsequently resumed, it retries the operation. This goes on for a fixed number of times (3), and if it still fails, it sends a message to the selector thread to wake it up whenever the socket is ready. See the comments on kilim.nio.EndPoint.
Please keep us posted about your netty experiments.
原文地址:http://wangscu.iteye.com/blog/664688
【编辑推荐】