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.DefaultSelectStrategyFactory;
19  import io.netty.channel.EventLoop;
20  import io.netty.channel.EventLoopGroup;
21  import io.netty.channel.MultithreadEventLoopGroup;
22  import io.netty.channel.SelectStrategyFactory;
23  import io.netty.util.concurrent.EventExecutor;
24  import io.netty.util.concurrent.EventExecutorChooserFactory;
25  import io.netty.util.concurrent.RejectedExecutionHandler;
26  import io.netty.util.concurrent.RejectedExecutionHandlers;
27  
28  import java.util.concurrent.Executor;
29  import java.util.concurrent.ThreadFactory;
30  
31  /**
32   * {@link EventLoopGroup} which uses epoll under the covers. Because of this
33   * it only works on linux.
34   */
35  public final class EpollEventLoopGroup extends MultithreadEventLoopGroup {
36      {
37          // Ensure JNI is initialized by the time this class is loaded.
38          Epoll.ensureAvailability();
39      }
40  
41      /**
42       * Create a new instance using the default number of threads and the default {@link ThreadFactory}.
43       */
44      public EpollEventLoopGroup() {
45          this(0);
46      }
47  
48      /**
49       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
50       */
51      public EpollEventLoopGroup(int nThreads) {
52          this(nThreads, (ThreadFactory) null);
53      }
54  
55      /**
56       * Create a new instance using the specified number of threads and the default {@link ThreadFactory}.
57       */
58      @SuppressWarnings("deprecation")
59      public EpollEventLoopGroup(int nThreads, SelectStrategyFactory selectStrategyFactory) {
60          this(nThreads, (ThreadFactory) null, selectStrategyFactory);
61      }
62  
63      /**
64       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
65       */
66      @SuppressWarnings("deprecation")
67      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
68          this(nThreads, threadFactory, 0);
69      }
70  
71      public EpollEventLoopGroup(int nThreads, Executor executor) {
72          this(nThreads, executor, DefaultSelectStrategyFactory.INSTANCE);
73      }
74  
75      /**
76       * Create a new instance using the specified number of threads and the given {@link ThreadFactory}.
77       */
78      @SuppressWarnings("deprecation")
79      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, SelectStrategyFactory selectStrategyFactory) {
80          this(nThreads, threadFactory, 0, selectStrategyFactory);
81      }
82  
83      /**
84       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
85       * maximal amount of epoll events to handle per epollWait(...).
86       *
87       * @deprecated  Use {@link #EpollEventLoopGroup(int)} or {@link #EpollEventLoopGroup(int, ThreadFactory)}
88       */
89      @Deprecated
90      public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce) {
91          this(nThreads, threadFactory, maxEventsAtOnce, DefaultSelectStrategyFactory.INSTANCE);
92      }
93  
94      /**
95       * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the given
96       * maximal amount of epoll events to handle per epollWait(...).
97       *
98       * @deprecated  Use {@link #EpollEventLoopGroup(int)}, {@link #EpollEventLoopGroup(int, ThreadFactory)}, or
99       * {@link #EpollEventLoopGroup(int, SelectStrategyFactory)}
100      */
101     @Deprecated
102     public EpollEventLoopGroup(int nThreads, ThreadFactory threadFactory, int maxEventsAtOnce,
103                                SelectStrategyFactory selectStrategyFactory) {
104         super(nThreads, threadFactory, maxEventsAtOnce, selectStrategyFactory, RejectedExecutionHandlers.reject());
105     }
106 
107     public EpollEventLoopGroup(int nThreads, Executor executor, SelectStrategyFactory selectStrategyFactory) {
108         super(nThreads, executor, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
109     }
110 
111     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
112                                SelectStrategyFactory selectStrategyFactory) {
113         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, RejectedExecutionHandlers.reject());
114     }
115 
116     public EpollEventLoopGroup(int nThreads, Executor executor, EventExecutorChooserFactory chooserFactory,
117                                SelectStrategyFactory selectStrategyFactory,
118                                RejectedExecutionHandler rejectedExecutionHandler) {
119         super(nThreads, executor, chooserFactory, 0, selectStrategyFactory, rejectedExecutionHandler);
120     }
121 
122     /**
123      * Sets the percentage of the desired amount of time spent for I/O in the child event loops.  The default value is
124      * {@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.
125      */
126     public void setIoRatio(int ioRatio) {
127         for (EventExecutor e: this) {
128             ((EpollEventLoop) e).setIoRatio(ioRatio);
129         }
130     }
131 
132     @Override
133     protected EventLoop newChild(Executor executor, Object... args) throws Exception {
134         return new EpollEventLoop(this, executor, (Integer) args[0],
135                 ((SelectStrategyFactory) args[1]).newSelectStrategy(), (RejectedExecutionHandler) args[2]);
136     }
137 }