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