Skip to content

Question of ioRatio in NioEventLoop and two backed array in SelectedSelectionKeySet #6058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lightningMan opened this issue Nov 23, 2016 · 7 comments
Assignees

Comments

@lightningMan
Copy link

lightningMan commented Nov 23, 2016

Hi,
I was reading netty source code these days, netty is really a great project and the source code is so beautiful!

Now I have two questions to ask for your help:

purpose of ioRatio in NioEventLoop.

Does ioRatio mean the elapsed time of io operation?As the methodprocessSelectedKey above is cpu intennsive(copy bytes from tcp buffer to jvm direct buffer), is ioRatio simply used to control the proportion of copy bytes and business task?Is there any other purpose,like efficiency? ^^

perhaps two backed array in SelectedSelectionKeySet may be reduced to one.

I see this set is backed by two arrays, but I think use one array can also do the same thing as every time after NioEventLoopcall processSelectedKeysOptimized(SelectionKey[] selectedKeys), the two SelectionKey[] is filled with Null point

flip method in SelectedSelectionKeySet

SelectionKey[] flip() {
        if (isA) {
            isA = false;
            keysA[keysASize] = null;
            keysBSize = 0;
            return keysA;
        } else {
            isA = true;
            keysB[keysBSize] = null;
            keysASize = 0;
            return keysB;
        }
    }

perhaps if the flip method is replaced with end method as follows can do the same and also avoid execif (isA) ^^

end method

   SelectionKey[] end() {
        keys[keysSize] = null;
        keysSize = 0;
        return keys;
    }

Thank you for your help!!!

@lightningMan lightningMan changed the title question of ioRatio in NioEventLoop and two backed array in SelectedSelectionKeySet Question of ioRatio in NioEventLoop and two backed array in SelectedSelectionKeySet Nov 23, 2016
@Scottmitch
Copy link
Member

Scottmitch commented Nov 23, 2016

purpose of ioRatio in NioEventLoop.

This provides a rough mechanism to control how much time is spent processing IO related tasks (socket reads, connects, closed, hangups, etc..) vs "non-IO" related tasks. The "non-IO" tasks are Runnable objects queued in the EventLoopGroup due to using the Executor interface (e.g. Executor#execute(..). The lower the number the more time will be spent on "non-IO" tasks, and currently 100 disables the timeout all together and runs all pending "non-IO" tasks.

perhaps two backed array in SelectedSelectionKeySet may be reduced to one.

Some of the methods may return "invalid values" if we are operating on the internal array (e.g. size) ... however this may be OK if the interface is only inconsistent while we are using it and we take care to make sure the interface is consistent when we use it externally ... also need to see if we are concurrently using 2 arrays at 1 time or not ... @normanmaurer @trustin - WDYT?

Also I wonder if the iteration to bounds in processSelectedKeysOptimized can be handled better:

https://github.com/netty/netty/blob/4.1/transport/src/main/java/io/netty/channel/nio/NioEventLoop.java#L582

// to call the updated method:
int size = selectedKeys.size();
processSelectedKeysOptimized(selectedKeys.flip(), size);

// alternatively if we make the changes suggested in this issue:
// processSelectedKeysOptimized(selectedKeys.array(), selectedKeys.size());


private void processSelectedKeysOptimized(SelectionKey[] selectedKeys, int size) {
  for (int i = 0; i < size; ++i) {
   final SelectionKey k = selectedKeys[i];
   // null out entry in the array to allow to have it GC'ed once the Channel close
   // See https://github.com/netty/netty/issues/2363
   selectedKeys[i] = null;

   ...

    if (needsToSelectAgain) {
      for (int x = i + 1; x < size; ++x) {
        selectedKeys[x] = null;
      }
      ...
      // Note we would have to be careful to always call end() before we select
      selectedKeys = selectedKeys.end()
      selectAgain();
      size = selectedKeys.size();
      i = -1;
    }
  }
}

@Scottmitch Scottmitch self-assigned this Nov 23, 2016
@Scottmitch
Copy link
Member

let me take a stab at this...

@lightningMan
Copy link
Author

@Scottmitch thank you for eliminating my doubts, this is my first time to create an issue in github. I am so happy for receiving your reply^^

Scottmitch added a commit to Scottmitch/netty that referenced this issue Dec 1, 2016
Motivation:
SelectedSelectionKeySet currently uses 2 arrays internally and users are expected to call flip() to access the underlying array and switch the active array. However we do not concurrently use 2 arrays at the same time and we can get away with using a single array if we are careful about when we reset the elements of the array.

Modifications:
- Introduce SelectedSelectionKeySetSelector which wraps a Selector and ensures we reset the underlying SelectedSelectionKeySet data structures before we select
- The loop bounds in NioEventLoop#processSelectedKeysOptimized can be defined more precisely because we know the real size of the underlying array

Result:
Fixes netty#6058
Scottmitch added a commit to Scottmitch/netty that referenced this issue Feb 16, 2017
Motivation:
SelectedSelectionKeySet currently uses 2 arrays internally and users are expected to call flip() to access the underlying array and switch the active array. However we do not concurrently use 2 arrays at the same time and we can get away with using a single array if we are careful about when we reset the elements of the array.

Modifications:
- Introduce SelectedSelectionKeySetSelector which wraps a Selector and ensures we reset the underlying SelectedSelectionKeySet data structures before we select
- The loop bounds in NioEventLoop#processSelectedKeysOptimized can be defined more precisely because we know the real size of the underlying array

Result:
Fixes netty#6058
Scottmitch added a commit that referenced this issue Feb 16, 2017
Motivation:
SelectedSelectionKeySet currently uses 2 arrays internally and users are expected to call flip() to access the underlying array and switch the active array. However we do not concurrently use 2 arrays at the same time and we can get away with using a single array if we are careful about when we reset the elements of the array.

Modifications:
- Introduce SelectedSelectionKeySetSelector which wraps a Selector and ensures we reset the underlying SelectedSelectionKeySet data structures before we select
- The loop bounds in NioEventLoop#processSelectedKeysOptimized can be defined more precisely because we know the real size of the underlying array

Result:
Fixes #6058
liuzhengyang pushed a commit to liuzhengyang/netty that referenced this issue Sep 10, 2017
Motivation:
SelectedSelectionKeySet currently uses 2 arrays internally and users are expected to call flip() to access the underlying array and switch the active array. However we do not concurrently use 2 arrays at the same time and we can get away with using a single array if we are careful about when we reset the elements of the array.

Modifications:
- Introduce SelectedSelectionKeySetSelector which wraps a Selector and ensures we reset the underlying SelectedSelectionKeySet data structures before we select
- The loop bounds in NioEventLoop#processSelectedKeysOptimized can be defined more precisely because we know the real size of the underlying array

Result:
Fixes netty#6058
@lxchinesszz
Copy link

还是不太明白ioRatio 到底是什么作用,能给解释下吗

@Scottmitch
Copy link
Member

@lxchinesszz - can you translate to English please?

@jiakme
Copy link

jiakme commented Nov 19, 2017

@Scottmitch He means that he does not understand the purpose of this parameter ioRatio. And I just read about what you wrote before, so I translate it into Chinese and replay to him.

@lxchinesszz

This provides a rough mechanism to control how much time is spent processing IO related tasks (socket reads, connects, closed, hangups, etc..) vs "non-IO" related tasks. The "non-IO" tasks are Runnable objects queued in the EventLoopGroup due to using the Executor interface (e.g. Executor#execute(..). The lower the number the more time will be spent on "non-IO" tasks, and currently 100 disables the timeout all together and runs all pending "non-IO" tasks.

这个参数提供了一个粗略的机制,用来大致控制处理IO相关(socket 读,链接,关闭,挂起等)和非IO相关任务的时间分配比.非IO任务是,由于使用Executor接口,例如Executor#execute(..),而在EventLoopGroup队列中的Runnable对象.参数值越小,越多的时间将消耗在非IO任务上.当前,100将禁止所有超时时间(详见源码runAllTasks(long timeoutNanos))并运行所有等待着的非IO任务.

@Scottmitch
Copy link
Member

thanks @jiakme

kiril-me pushed a commit to kiril-me/netty that referenced this issue Feb 28, 2018
Motivation:
SelectedSelectionKeySet currently uses 2 arrays internally and users are expected to call flip() to access the underlying array and switch the active array. However we do not concurrently use 2 arrays at the same time and we can get away with using a single array if we are careful about when we reset the elements of the array.

Modifications:
- Introduce SelectedSelectionKeySetSelector which wraps a Selector and ensures we reset the underlying SelectedSelectionKeySet data structures before we select
- The loop bounds in NioEventLoop#processSelectedKeysOptimized can be defined more precisely because we know the real size of the underlying array

Result:
Fixes netty#6058
pulllock pushed a commit to pulllock/netty that referenced this issue Oct 19, 2023
Motivation:
SelectedSelectionKeySet currently uses 2 arrays internally and users are expected to call flip() to access the underlying array and switch the active array. However we do not concurrently use 2 arrays at the same time and we can get away with using a single array if we are careful about when we reset the elements of the array.

Modifications:
- Introduce SelectedSelectionKeySetSelector which wraps a Selector and ensures we reset the underlying SelectedSelectionKeySet data structures before we select
- The loop bounds in NioEventLoop#processSelectedKeysOptimized can be defined more precisely because we know the real size of the underlying array

Result:
Fixes netty#6058
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants