View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.util;
22  
23  import java.lang.reflect.Field;
24  import java.lang.reflect.Modifier;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.ConcurrentSkipListMap;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  
32  /**
33   * Class for determining the "size" of a class, an attempt to calculate the
34   * actual bytes that an object of this class will occupy in memory
35   *
36   * The core of this class is taken from the Derby project
37   */
38  @InterfaceAudience.Private
39  public class ClassSize {
40    private static final Log LOG = LogFactory.getLog(ClassSize.class);
41  
42    /** Array overhead */
43    public static final int ARRAY;
44  
45    /** Overhead for ArrayList(0) */
46    public static final int ARRAYLIST;
47  
48    /** Overhead for ByteBuffer */
49    public static final int BYTE_BUFFER;
50  
51    /** Overhead for an Integer */
52    public static final int INTEGER;
53  
54    /** Overhead for entry in map */
55    public static final int MAP_ENTRY;
56  
57    /** Object overhead is minimum 2 * reference size (8 bytes on 64-bit) */
58    public static final int OBJECT;
59  
60    /** Reference size is 8 bytes on 64-bit, 4 bytes on 32-bit */
61    public static final int REFERENCE;
62  
63    /** String overhead */
64    public static final int STRING;
65  
66    /** Overhead for TreeMap */
67    public static final int TREEMAP;
68  
69    /** Overhead for ConcurrentHashMap */
70    public static final int CONCURRENT_HASHMAP;
71  
72    /** Overhead for ConcurrentHashMap.Entry */
73    public static final int CONCURRENT_HASHMAP_ENTRY;
74  
75    /** Overhead for ConcurrentHashMap.Segment */
76    public static final int CONCURRENT_HASHMAP_SEGMENT;
77  
78    /** Overhead for ConcurrentSkipListMap */
79    public static final int CONCURRENT_SKIPLISTMAP;
80  
81    /** Overhead for ConcurrentSkipListMap Entry */
82    public static final int CONCURRENT_SKIPLISTMAP_ENTRY;
83  
84    /** Overhead for ReentrantReadWriteLock */
85    public static final int REENTRANT_LOCK;
86  
87    /** Overhead for AtomicLong */
88    public static final int ATOMIC_LONG;
89  
90    /** Overhead for AtomicInteger */
91    public static final int ATOMIC_INTEGER;
92  
93    /** Overhead for AtomicBoolean */
94    public static final int ATOMIC_BOOLEAN;
95  
96    /** Overhead for CopyOnWriteArraySet */
97    public static final int COPYONWRITE_ARRAYSET;
98  
99    /** Overhead for CopyOnWriteArrayList */
100   public static final int COPYONWRITE_ARRAYLIST;
101 
102   /** Overhead for timerange */
103   public static final int TIMERANGE;
104 
105   /** Overhead for TimeRangeTracker */
106   public static final int TIMERANGE_TRACKER;
107 
108   /** Overhead for CellSkipListSet */
109   public static final int CELL_SKIPLIST_SET;
110 
111   /**
112    * MemoryLayout abstracts details about the JVM object layout. Default implementation is used in
113    * case Unsafe is not available.
114    */
115   private static class MemoryLayout {
116     int headerSize() {
117       return 2 * oopSize();
118     }
119 
120     int arrayHeaderSize() {
121       return (int) align(3 * oopSize());
122     }
123 
124     /**
125      * Return the size of an "ordinary object pointer". Either 4 or 8, depending on 32/64 bit,
126      * and CompressedOops
127      */
128     int oopSize() {
129       return is32BitJVM() ? 4 : 8;
130     }
131 
132     /**
133      * Aligns a number to 8.
134      * @param num number to align to 8
135      * @return smallest number >= input that is a multiple of 8
136      */
137     public long align(long num) {
138       //The 7 comes from that the alignSize is 8 which is the number of bytes
139       //stored and sent together
140       return  ((num + 7) >> 3) << 3;
141     }
142 
143     long sizeOf(byte[] b, int len) {
144       return align(ARRAY + len);
145     }
146   }
147 
148   /**
149    * UnsafeLayout uses Unsafe to guesstimate the object-layout related parameters like object header
150    * sizes and oop sizes
151    * See HBASE-15950.
152    */
153   private static class UnsafeLayout extends MemoryLayout {
154     @SuppressWarnings("unused")
155     private static final class HeaderSize {
156       private byte a;
157     }
158 
159     public UnsafeLayout() {
160     }
161 
162     @Override
163     int headerSize() {
164       try {
165         return (int) UnsafeAccess.theUnsafe.objectFieldOffset(
166           HeaderSize.class.getDeclaredField("a"));
167       } catch (NoSuchFieldException | SecurityException e) {
168         LOG.error(e);
169       }
170       return super.headerSize();
171     }
172 
173     @Override
174     int arrayHeaderSize() {
175       return UnsafeAccess.theUnsafe.arrayBaseOffset(byte[].class);
176     }
177 
178     @Override
179     @SuppressWarnings("static-access")
180     int oopSize() {
181       // Unsafe.addressSize() returns 8, even with CompressedOops. This is how many bytes each
182       // element is allocated in an Object[].
183       return UnsafeAccess.theUnsafe.ARRAY_OBJECT_INDEX_SCALE;
184     }
185 
186     @Override
187     @SuppressWarnings("static-access")
188     long sizeOf(byte[] b, int len) {
189       return align(ARRAY + len * UnsafeAccess.theUnsafe.ARRAY_BYTE_INDEX_SCALE);
190     }
191   }
192 
193   private static MemoryLayout getMemoryLayout() {
194     // Have a safeguard in case Unsafe estimate is wrong. This is static context, there is
195     // no configuration, so we look at System property.
196     String enabled = System.getProperty("hbase.memorylayout.use.unsafe");
197     if (UnsafeAvailChecker.isAvailable() && (enabled == null || Boolean.parseBoolean(enabled))) {
198       LOG.debug("Using Unsafe to estimate memory layout");
199       return new UnsafeLayout();
200     }
201     LOG.debug("Not using Unsafe to estimate memory layout");
202     return new MemoryLayout();
203   }
204 
205   private static final MemoryLayout memoryLayout = getMemoryLayout();
206 
207   /**
208    * Method for reading the arc settings and setting overheads according
209    * to 32-bit or 64-bit architecture.
210    */
211   static {
212     REFERENCE = memoryLayout.oopSize();
213 
214     OBJECT = memoryLayout.headerSize();
215 
216     ARRAY = memoryLayout.arrayHeaderSize();
217 
218     ARRAYLIST = align(OBJECT + REFERENCE + (2 * Bytes.SIZEOF_INT)) + align(ARRAY);
219 
220     //noinspection PointlessArithmeticExpression
221     BYTE_BUFFER = align(OBJECT + REFERENCE +
222         (5 * Bytes.SIZEOF_INT) +
223         (3 * Bytes.SIZEOF_BOOLEAN) + Bytes.SIZEOF_LONG) + align(ARRAY);
224 
225     INTEGER = align(OBJECT + Bytes.SIZEOF_INT);
226 
227     MAP_ENTRY = align(OBJECT + 5 * REFERENCE + Bytes.SIZEOF_BOOLEAN);
228 
229     TREEMAP = align(OBJECT + (2 * Bytes.SIZEOF_INT) + 7 * REFERENCE);
230 
231     // STRING is different size in jdk6 and jdk7. Just use what we estimate as size rather than
232     // have a conditional on whether jdk7.
233     STRING = (int) estimateBase(String.class, false);
234 
235     // CONCURRENT_HASHMAP is different size in jdk6 and jdk7; it looks like its different between
236     // 23.6-b03 and 23.0-b21. Just use what we estimate as size rather than have a conditional on
237     // whether jdk7.
238     CONCURRENT_HASHMAP = (int) estimateBase(ConcurrentHashMap.class, false);
239 
240     CONCURRENT_HASHMAP_ENTRY = align(REFERENCE + OBJECT + (3 * REFERENCE) +
241         (2 * Bytes.SIZEOF_INT));
242 
243     CONCURRENT_HASHMAP_SEGMENT = align(REFERENCE + OBJECT +
244         (3 * Bytes.SIZEOF_INT) + Bytes.SIZEOF_FLOAT + ARRAY);
245 
246     // The size changes from jdk7 to jdk8, estimate the size rather than use a conditional
247     CONCURRENT_SKIPLISTMAP = (int) estimateBase(ConcurrentSkipListMap.class, false);
248 
249     CONCURRENT_SKIPLISTMAP_ENTRY =
250         align(OBJECT + (3 * REFERENCE)) + /* one node per entry */
251         align((OBJECT + (3 * REFERENCE))/2); /* one index per two entries */
252 
253     REENTRANT_LOCK = align(OBJECT + (3 * REFERENCE));
254 
255     ATOMIC_LONG = align(OBJECT + Bytes.SIZEOF_LONG);
256 
257     ATOMIC_INTEGER = align(OBJECT + Bytes.SIZEOF_INT);
258 
259     ATOMIC_BOOLEAN = align(OBJECT + Bytes.SIZEOF_BOOLEAN);
260 
261     COPYONWRITE_ARRAYSET = align(OBJECT + REFERENCE);
262 
263     COPYONWRITE_ARRAYLIST = align(OBJECT + (2 * REFERENCE) + ARRAY);
264 
265     TIMERANGE = align(ClassSize.OBJECT + Bytes.SIZEOF_LONG * 2 + Bytes.SIZEOF_BOOLEAN);
266 
267     TIMERANGE_TRACKER = align(ClassSize.OBJECT + 2 * REFERENCE);
268 
269     CELL_SKIPLIST_SET = align(OBJECT + REFERENCE);
270   }
271 
272   /**
273    * The estimate of the size of a class instance depends on whether the JVM
274    * uses 32 or 64 bit addresses, that is it depends on the size of an object
275    * reference. It is a linear function of the size of a reference, e.g.
276    * 24 + 5*r where r is the size of a reference (usually 4 or 8 bytes).
277    *
278    * This method returns the coefficients of the linear function, e.g. {24, 5}
279    * in the above example.
280    *
281    * @param cl A class whose instance size is to be estimated
282    * @param debug debug flag
283    * @return an array of 3 integers. The first integer is the size of the
284    * primitives, the second the number of arrays and the third the number of
285    * references.
286    */
287   @SuppressWarnings("unchecked")
288   private static int [] getSizeCoefficients(Class cl, boolean debug) {
289     int primitives = 0;
290     int arrays = 0;
291     int references = 0;
292     int index = 0;
293 
294     for ( ; null != cl; cl = cl.getSuperclass()) {
295       Field[] field = cl.getDeclaredFields();
296       if (null != field) {
297         for (Field aField : field) {
298           if (Modifier.isStatic(aField.getModifiers())) continue;
299           Class fieldClass = aField.getType();
300           if (fieldClass.isArray()) {
301             arrays++;
302             references++;
303           } else if (!fieldClass.isPrimitive()) {
304             references++;
305           } else {// Is simple primitive
306             String name = fieldClass.getName();
307 
308             if (name.equals("int") || name.equals("I"))
309               primitives += Bytes.SIZEOF_INT;
310             else if (name.equals("long") || name.equals("J"))
311               primitives += Bytes.SIZEOF_LONG;
312             else if (name.equals("boolean") || name.equals("Z"))
313               primitives += Bytes.SIZEOF_BOOLEAN;
314             else if (name.equals("short") || name.equals("S"))
315               primitives += Bytes.SIZEOF_SHORT;
316             else if (name.equals("byte") || name.equals("B"))
317               primitives += Bytes.SIZEOF_BYTE;
318             else if (name.equals("char") || name.equals("C"))
319               primitives += Bytes.SIZEOF_CHAR;
320             else if (name.equals("float") || name.equals("F"))
321               primitives += Bytes.SIZEOF_FLOAT;
322             else if (name.equals("double") || name.equals("D"))
323               primitives += Bytes.SIZEOF_DOUBLE;
324           }
325           if (debug) {
326             if (LOG.isDebugEnabled()) {
327               LOG.debug("" + index + " " + aField.getName() + " " + aField.getType());
328             }
329           }
330           index++;
331         }
332       }
333     }
334     return new int [] {primitives, arrays, references};
335   }
336 
337   /**
338    * Estimate the static space taken up by a class instance given the
339    * coefficients returned by getSizeCoefficients.
340    *
341    * @param coeff the coefficients
342    *
343    * @param debug debug flag
344    * @return the size estimate, in bytes
345    */
346   private static long estimateBaseFromCoefficients(int [] coeff, boolean debug) {
347     long prealign_size = (long) OBJECT + coeff[0] + coeff[2] * REFERENCE;
348 
349     // Round up to a multiple of 8
350     long size = align(prealign_size) + align(coeff[1] * ARRAY);
351     if (debug) {
352       if (LOG.isDebugEnabled()) {
353         LOG.debug("Primitives=" + coeff[0] + ", arrays=" + coeff[1] +
354             ", references=" + coeff[2] + ", refSize " + REFERENCE +
355             ", size=" + size + ", prealign_size=" + prealign_size);
356       }
357     }
358     return size;
359   }
360 
361   /**
362    * Estimate the static space taken up by the fields of a class. This includes
363    * the space taken up by by references (the pointer) but not by the referenced
364    * object. So the estimated size of an array field does not depend on the size
365    * of the array. Similarly the size of an object (reference) field does not
366    * depend on the object.
367    *
368    * @param cl class
369    * @param debug debug flag
370    * @return the size estimate in bytes.
371    */
372   @SuppressWarnings("unchecked")
373   public static long estimateBase(Class cl, boolean debug) {
374     return estimateBaseFromCoefficients( getSizeCoefficients(cl, debug), debug);
375   }
376 
377   /**
378    * Aligns a number to 8.
379    * @param num number to align to 8
380    * @return smallest number &gt;= input that is a multiple of 8
381    */
382   public static int align(int num) {
383     return (int)(align((long)num));
384   }
385 
386   /**
387    * Aligns a number to 8.
388    * @param num number to align to 8
389    * @return smallest number &gt;= input that is a multiple of 8
390    */
391   public static long align(long num) {
392     return memoryLayout.align(num);
393   }
394 
395   /**
396    * Determines if we are running in a 32-bit JVM. Some unit tests need to
397    * know this too.
398    */
399   public static boolean is32BitJVM() {
400     final String model = System.getProperty("sun.arch.data.model");
401     return model != null && model.equals("32");
402   }
403 
404   public static long sizeOf(byte[] b, int len) {
405     return memoryLayout.sizeOf(b, len);
406   }
407 
408 }
409