View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.epoll;
17  
18  import io.netty.channel.EventLoop;
19  import io.netty.channel.EventLoopGroup;
20  import io.netty.channel.MultithreadEventLoopGroup;
21  import io.netty.util.concurrent.EventExecutor;
22  import io.netty.util.concurrent.ExecutorServiceFactory;
23  
24  import java.util.concurrent.Executor;
25  
26  
27  /**
28   * A {@link MultithreadEventLoopGroup} which uses <a href="http://en.wikipedia.org/wiki/Epoll">epoll</a> under the
29   * covers. This {@link EventLoopGroup} works only on Linux systems!
30   */
31  public final class EpollEventLoopGroup extends MultithreadEventLoopGroup {
32  
33      /**
34       * Create a new instance that uses twice as many {@link EventLoop}s as there processors/cores
35       * available, as well as the default {@link Executor}.
36       *
37       * @see io.netty.util.concurrent.DefaultExecutorServiceFactory
38       */
39      public EpollEventLoopGroup() {
40          this(0);
41      }
42  
43      /**
44       * Create a new instance that uses the default {@link Executor}.
45       *
46       * @see io.netty.util.concurrent.DefaultExecutorServiceFactory
47       *
48       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
49       *                      If {@code executor} is {@code null} this number will also be the parallelism
50       *                      requested from the default executor. It is generally advised for the number
51       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
52       */
53      public EpollEventLoopGroup(int nEventLoops) {
54          this(nEventLoops, (Executor) null);
55      }
56  
57      /**
58       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
59       *                      If {@code executor} is {@code null} this number will also be the parallelism
60       *                      requested from the default executor. It is generally advised for the number
61       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
62       *                      {@code executor} to lie very close together.
63       * @param executor  the {@link Executor} to use, or {@code null} if the default should be used.
64       */
65      @SuppressWarnings("deprecation")
66      public EpollEventLoopGroup(int nEventLoops, Executor executor) {
67          this(nEventLoops, executor, 0);
68      }
69  
70      /**
71       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
72       *                      If {@code executor} is {@code null} this number will also be the parallelism
73       *                      requested from the default executor. It is generally advised for the number
74       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
75       *                      {@code executor} to lie very close together.
76       * @param executorServiceFactory   the {@link ExecutorServiceFactory} to use, or {@code null} if the
77       *                                 default should be used.
78       */
79      @SuppressWarnings("deprecation")
80      public EpollEventLoopGroup(int nEventLoops, ExecutorServiceFactory executorServiceFactory) {
81          this(nEventLoops, executorServiceFactory, 0);
82      }
83  
84      /**
85       * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
86       *                      If {@code executor} is {@code null} this number will also be the parallelism
87       *                      requested from the default executor. It is generally advised for the number
88       *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
89       *                      {@code executor} to lie very close together.
90       * @param executor   the {@link Executor} to use, or {@code null} if the default should be used.
91       * @param maxEventsAtOnce   the maximum number of epoll events to handle per epollWait(...).
92       *
93       * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int)} or
94       *              {@link #EpollEventLoopGroup(int, Executor)}
95       */
96      @Deprecated
97      public EpollEventLoopGroup(int nEventLoops, Executor executor, int maxEventsAtOnce) {
98          super(nEventLoops, executor, maxEventsAtOnce);
99      }
100 
101     /**
102      * @param nEventLoops   the number of {@link EventLoop}s that will be used by this instance.
103      *                      If {@code executor} is {@code null} this number will also be the parallelism
104      *                      requested from the default executor. It is generally advised for the number
105      *                      of {@link EventLoop}s and the number of {@link Thread}s used by the
106      *                      {@code executor} to lie very close together.
107      * @param executorServiceFactory   the {@link ExecutorServiceFactory} to use, or {@code null} if the default
108      *                                 should be used.
109      * @param maxEventsAtOnce   the maximum number of epoll events to handle per epollWait(...).
110      *
111      * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int)} or
112      *              {@link #EpollEventLoopGroup(int, ExecutorServiceFactory)}
113      */
114     @Deprecated
115     public EpollEventLoopGroup(int nEventLoops, ExecutorServiceFactory executorServiceFactory, int maxEventsAtOnce) {
116         super(nEventLoops, executorServiceFactory, maxEventsAtOnce);
117     }
118 
119     /**
120      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
121      * {@code 50}, which means the event loop will try to spend the same amount of time for I/O as for non-I/O tasks.
122      */
123     public void setIoRatio(int ioRatio) {
124         for (EventExecutor e: children()) {
125             ((EpollEventLoop) e).setIoRatio(ioRatio);
126         }
127     }
128 
129     @Override
130     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
131         return new EpollEventLoop(this, executor, (Integer) args[0]);
132     }
133 }