|  | 
| 37 | 37 | import org.slf4j.LoggerFactory; | 
| 38 | 38 | 
 | 
| 39 | 39 | import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; | 
|  | 40 | +import org.apache.hbase.thirdparty.com.google.common.collect.Sets; | 
| 40 | 41 | 
 | 
| 41 | 42 | /** | 
| 42 | 43 |  * ByteBuffAllocator is used for allocating/freeing the ByteBuffers from/to NIO ByteBuffer pool, and | 
| @@ -159,7 +160,10 @@ public interface Recycler { | 
| 159 | 160 |    * reservoir is enabled and the reservoir has enough buffers, otherwise the allocator will just | 
| 160 | 161 |    * allocate the insufficient buffers from on-heap to meet the requirement. | 
| 161 | 162 |    * @param conf which get the arguments to initialize the allocator. | 
| 162 |  | -   * @param reservoirEnabled indicate whether the reservoir is enabled or disabled. | 
|  | 163 | +   * @param reservoirEnabled indicate whether the reservoir is enabled or disabled. NOTICE: if | 
|  | 164 | +   *          reservoir is enabled, then we will use the pool allocator to allocate off-heap | 
|  | 165 | +   *          ByteBuffers and use the HEAP allocator to allocate heap ByteBuffers. Otherwise if | 
|  | 166 | +   *          reservoir is disabled then all allocations will happen in HEAP instance. | 
| 163 | 167 |    * @return ByteBuffAllocator to manage the byte buffers. | 
| 164 | 168 |    */ | 
| 165 | 169 |   public static ByteBuffAllocator create(Configuration conf, boolean reservoirEnabled) { | 
| @@ -192,7 +196,7 @@ public static ByteBuffAllocator create(Configuration conf, boolean reservoirEnab | 
| 192 | 196 |       int minSizeForReservoirUse = conf.getInt(MIN_ALLOCATE_SIZE_KEY, poolBufSize / 6); | 
| 193 | 197 |       return new ByteBuffAllocator(true, maxBuffCount, poolBufSize, minSizeForReservoirUse); | 
| 194 | 198 |     } else { | 
| 195 |  | -      return new ByteBuffAllocator(false, 0, poolBufSize, Integer.MAX_VALUE); | 
|  | 199 | +      return HEAP; | 
| 196 | 200 |     } | 
| 197 | 201 |   } | 
| 198 | 202 | 
 | 
| @@ -247,12 +251,22 @@ public int getTotalBufferCount() { | 
| 247 | 251 |     return maxBufCount; | 
| 248 | 252 |   } | 
| 249 | 253 | 
 | 
| 250 |  | -  public double getHeapAllocationRatio() { | 
| 251 |  | -    long heapAllocBytes = heapAllocationBytes.sum(), poolAllocBytes = poolAllocationBytes.sum(); | 
| 252 |  | -    double heapDelta = heapAllocBytes - lastHeapAllocationBytes; | 
| 253 |  | -    double poolDelta = poolAllocBytes - lastPoolAllocationBytes; | 
| 254 |  | -    lastHeapAllocationBytes = heapAllocBytes; | 
| 255 |  | -    lastPoolAllocationBytes = poolAllocBytes; | 
|  | 254 | +  public static double getHeapAllocationRatio(ByteBuffAllocator... allocators) { | 
|  | 255 | +    double heapDelta = 0.0, poolDelta = 0.0; | 
|  | 256 | +    long heapAllocBytes, poolAllocBytes; | 
|  | 257 | +    // If disabled the pool allocator, then we use the global HEAP allocator. otherwise we use | 
|  | 258 | +    // the pool allocator to allocate offheap ByteBuffers and use the HEAP to allocate heap | 
|  | 259 | +    // ByteBuffers. So here we use a HashSet to remove the duplicated allocator object in disable | 
|  | 260 | +    // case. | 
|  | 261 | +    for (ByteBuffAllocator alloc : Sets.newHashSet(allocators)) { | 
|  | 262 | +      heapAllocBytes = alloc.heapAllocationBytes.sum(); | 
|  | 263 | +      poolAllocBytes = alloc.poolAllocationBytes.sum(); | 
|  | 264 | +      heapDelta += (heapAllocBytes - alloc.lastHeapAllocationBytes); | 
|  | 265 | +      poolDelta += (poolAllocBytes - alloc.lastPoolAllocationBytes); | 
|  | 266 | +      alloc.lastHeapAllocationBytes = heapAllocBytes; | 
|  | 267 | +      alloc.lastPoolAllocationBytes = poolAllocBytes; | 
|  | 268 | +    } | 
|  | 269 | +    // Calculate the heap allocation ratio. | 
| 256 | 270 |     if (Math.abs(heapDelta + poolDelta) < 1e-3) { | 
| 257 | 271 |       return 0.0; | 
| 258 | 272 |     } | 
|  | 
0 commit comments