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    *   https://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.DefaultSelectStrategyFactory;
19  import io.netty.channel.EventLoop;
20  import io.netty.channel.EventLoopGroup;
21  import io.netty.channel.EventLoopTaskQueueFactory;
22  import io.netty.channel.MultithreadEventLoopGroup;
23  import io.netty.channel.SelectStrategyFactory;
24  import io.netty.channel.SingleThreadEventLoop;
25  import io.netty.util.concurrent.EventExecutor;
26  import io.netty.util.concurrent.EventExecutorChooserFactory;
27  import io.netty.util.concurrent.RejectedExecutionHandler;
28  import io.netty.util.concurrent.RejectedExecutionHandlers;
29  
30  import java.util.concurrent.Executor;
31  import java.util.concurrent.ThreadFactory;
32  
33  /**
34   * {@link EventLoopGroup} which uses epoll under the covers. Because of this
35   * it only works on linux.
36   */
37  public final class EpollEventLoopGroup extends MultithreadEventLoopGroup {
38      {
39          // Ensure JNI is initialized by the time this class is loaded.
40          Epoll.ensureAvailability();
41      }
42  
43      /**
44       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
45       */
46      public EpollEventLoopGroup() {
47          this(0);
48      }
49  
50      /**
51       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
52       */
53      public EpollEventLoopGroup(int nThreads) {
54          this(nThreads, (ThreadFactory) null);
55      }
56  
57      /**
58       * Create a new instance using the default number of threads and the given {@link ThreadFactory}.
59       */
60      @SuppressWarnings("deprecation")
61      public EpollEventLoopGroup(ThreadFactory threadFactory) {
62          this(0, threadFactory, 0);
63      }
64  
65      /**
66       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
67       */
68      @SuppressWarnings("deprecation")
69      public EpollEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
70          this(nThreads, (ThreadFactory) null, selectStrategyFactory);
71      }
72  
73      /**
74       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
75       */
76      @SuppressWarnings("deprecation")
77      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
78          this(nThreads, threadFactory, 0);
79      }
80  
81      public EpollEventLoopGroup(int nThreads, Executor executor) {
82          this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
83      }
84  
85      /**
86       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
87       */
88      @SuppressWarnings("deprecation")
89      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, SelectStrategyFactory selectStrategyFactory) {
90          this(nThreads, threadFactory, 0, selectStrategyFactory);
91      }
92  
93      /**
94       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
95       * maximal amount of epoll events to handle per epollWait(...).
96       *
97       * @deprecated  Use {@link #EpollEventLoopGroup(int)} or {@link #EpollEventLoopGroup(int, ThreadFactory)}
98       */
99      @Deprecated
100     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
101         this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
102     }
103 
104     /**
105      * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
106      * maximal amount of epoll events to handle per epollWait(...).
107      *
108      * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int, ThreadFactory)}, or
109      * {@link #EpollEventLoopGroup(int, SelectStrategyFactory)}
110      */
111     @Deprecated
112     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
113                                SelectStrategyFactory selectStrategyFactory) {
114         super(nThreads, threadFactory, maxEventsAtOnce, selectStrategyFactory, RejectedExecutionHandlers.reject());
115     }
116 
117     public EpollEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
118         super(nThreads, executor, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
119     }
120 
121     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
122                                SelectStrategyFactory selectStrategyFactory) {
123         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
124     }
125 
126     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
127                                SelectStrategyFactory selectStrategyFactory,
128                                RejectedExecutionHandler rejectedExecutionHandler) {
129         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler);
130     }
131 
132     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
133                                SelectStrategyFactory selectStrategyFactory,
134                                RejectedExecutionHandler rejectedExecutionHandler,
135                                EventLoopTaskQueueFactory queueFactory) {
136         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler, queueFactory);
137     }
138 
139     /**
140      * @param nThreads the number of threads that will be used by this instance.
141      * @param executor the Executor to use, or {@code null} if default one should be used.
142      * @param chooserFactory the {@link EventExecutorChooserFactory} to use.
143      * @param selectStrategyFactory the {@link SelectStrategyFactory} to use.
144      * @param rejectedExecutionHandler the {@link RejectedExecutionHandler} to use.
145      * @param taskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
146      *                         {@link SingleThreadEventLoop#execute(Runnable)},
147      *                         or {@code null} if default one should be used.
148      * @param tailTaskQueueFactory the {@link EventLoopTaskQueueFactory} to use for
149      *                             {@link SingleThreadEventLoop#executeAfterEventLoopIteration(Runnable)},
150      *                             or {@code null} if default one should be used.
151      */
152     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
153                                SelectStrategyFactory selectStrategyFactory,
154                                RejectedExecutionHandler rejectedExecutionHandler,
155                                EventLoopTaskQueueFactory taskQueueFactory,
156                                EventLoopTaskQueueFactory tailTaskQueueFactory) {
157         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler, taskQueueFactory,
158                 tailTaskQueueFactory);
159     }
160 
161     /**
162      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
163      * {@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.
164      */
165     public void setIoRatio(int ioRatio) {
166         for (EventExecutor e: this) {
167             ((EpollEventLoop) e).setIoRatio(ioRatio);
168         }
169     }
170 
171     @Override
172     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
173         Integer maxEvents = (Integer) args[0];
174         SelectStrategyFactory selectStrategyFactory = (SelectStrategyFactory) args[1];
175         RejectedExecutionHandler rejectedExecutionHandler = (RejectedExecutionHandler) args[2];
176         EventLoopTaskQueueFactory taskQueueFactory = null;
177         EventLoopTaskQueueFactory tailTaskQueueFactory = null;
178 
179         int argsLength = args.length;
180         if (argsLength > 3) {
181             taskQueueFactory = (EventLoopTaskQueueFactory) args[3];
182         }
183         if (argsLength > 4) {
184             tailTaskQueueFactory = (EventLoopTaskQueueFactory) args[4];
185         }
186         return new EpollEventLoop(this, executor, maxEvents,
187                 selectStrategyFactory.newSelectStrategy(),
188                 rejectedExecutionHandler, taskQueueFactory, tailTaskQueueFactory);
189     }
190 }