View Javadoc
1   /*
2    * Copyright 2024 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.buffer;
17  
18  import io.netty.util.internal.PlatformDependent;
19  import io.netty.util.internal.SystemPropertyUtil;
20  import io.netty.util.internal.UnstableApi;
21  import io.netty.util.internal.logging.InternalLogger;
22  import io.netty.util.internal.logging.InternalLoggerFactory;
23  
24  import static io.netty.util.internal.PlatformDependent.javaVersion;
25  
26  /**
27   * An auto-tuning pooling {@link ByteBufAllocator}, that follows an anti-generational hypothesis.
28   * <p>
29   * <strong>Note:</strong> this allocator is <strong>experimental</strong>. It is recommended to roll out usage slowly,
30   * and to carefully monitor application performance in the process.
31   * <p>
32   * See the {@link AdaptivePoolingAllocator} class documentation for implementation details.
33   */
34  @UnstableApi
35  public final class AdaptiveByteBufAllocator extends AbstractByteBufAllocator
36          implements ByteBufAllocatorMetricProvider, ByteBufAllocatorMetric {
37      private static final InternalLogger logger = InternalLoggerFactory.getInstance(AdaptiveByteBufAllocator.class);
38      private static final boolean DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS;
39  
40      static {
41          DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS = SystemPropertyUtil.getBoolean(
42                  "io.netty.allocator.useCachedMagazinesForNonEventLoopThreads", false);
43          logger.debug("-Dio.netty.allocator.useCachedMagazinesForNonEventLoopThreads: {}",
44                       DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS);
45      }
46  
47      private final AdaptiveAllocatorApi direct;
48      private final AdaptiveAllocatorApi heap;
49  
50      public AdaptiveByteBufAllocator() {
51          this(!PlatformDependent.isExplicitNoPreferDirect());
52      }
53  
54      public AdaptiveByteBufAllocator(boolean preferDirect) {
55          this(preferDirect, DEFAULT_USE_CACHED_MAGAZINES_FOR_NON_EVENT_LOOP_THREADS);
56      }
57  
58      public AdaptiveByteBufAllocator(boolean preferDirect, boolean useCacheForNonEventLoopThreads) {
59          super(preferDirect);
60          if (javaVersion() < 8) {
61              // The implementation uses StampedLock, which was introduced in Java 8.
62              throw new IllegalStateException("This allocator require Java 8 or newer.");
63          }
64          direct = new AdaptivePoolingAllocator(new DirectChunkAllocator(this), useCacheForNonEventLoopThreads);
65          heap = new AdaptivePoolingAllocator(new HeapChunkAllocator(this), useCacheForNonEventLoopThreads);
66      }
67  
68      @Override
69      protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
70          return toLeakAwareBuffer(heap.allocate(initialCapacity, maxCapacity));
71      }
72  
73      @Override
74      protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
75          return toLeakAwareBuffer(direct.allocate(initialCapacity, maxCapacity));
76      }
77  
78      @Override
79      public boolean isDirectBufferPooled() {
80          return true;
81      }
82  
83      @Override
84      public long usedHeapMemory() {
85          return heap.usedMemory();
86      }
87  
88      @Override
89      public long usedDirectMemory() {
90          return direct.usedMemory();
91      }
92  
93      @Override
94      public ByteBufAllocatorMetric metric() {
95          return this;
96      }
97  
98      interface AdaptiveAllocatorApi {
99          ByteBuf allocate(int initialCapacity, int maxCapacity);
100         long usedMemory();
101     }
102 
103     private static final class HeapChunkAllocator implements AdaptivePoolingAllocator.ChunkAllocator {
104         private final ByteBufAllocator allocator;
105 
106         private HeapChunkAllocator(ByteBufAllocator allocator) {
107             this.allocator = allocator;
108         }
109 
110         @Override
111         public AbstractByteBuf allocate(int initialCapacity, int maxCapacity) {
112             return PlatformDependent.hasUnsafe() ?
113                     new UnpooledUnsafeHeapByteBuf(allocator, initialCapacity, maxCapacity) :
114                     new UnpooledHeapByteBuf(allocator, initialCapacity, maxCapacity);
115         }
116     }
117 
118     private static final class DirectChunkAllocator implements AdaptivePoolingAllocator.ChunkAllocator {
119         private final ByteBufAllocator allocator;
120 
121         private DirectChunkAllocator(ByteBufAllocator allocator) {
122             this.allocator = allocator;
123         }
124 
125         @Override
126         public AbstractByteBuf allocate(int initialCapacity, int maxCapacity) {
127             return PlatformDependent.hasUnsafe() ?
128                     UnsafeByteBufUtil.newUnsafeDirectByteBuf(allocator, initialCapacity, maxCapacity) :
129                     new UnpooledDirectByteBuf(allocator, initialCapacity, maxCapacity);
130         }
131     }
132 }