View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import java.lang.reflect.Field;
21  import java.lang.reflect.Method;
22  import java.nio.ByteOrder;
23  import java.security.AccessController;
24  import java.security.PrivilegedAction;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.classification.InterfaceStability;
30  
31  import sun.misc.Unsafe;
32  
33  @InterfaceAudience.Private
34  @InterfaceStability.Evolving
35  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="REC_CATCH_EXCEPTION",
36    justification="If exception, presume unaligned")
37  public final class UnsafeAccess {
38  
39    private static final Log LOG = LogFactory.getLog(UnsafeAccess.class);
40  
41    public static final Unsafe theUnsafe;
42  
43    /** The offset to the first element in a byte array. */
44    public static final int BYTE_ARRAY_BASE_OFFSET;
45    private static boolean unaligned = false;
46  
47    static {
48      theUnsafe = (Unsafe) AccessController.doPrivileged(new PrivilegedAction<Object>() {
49        @Override
50        public Object run() {
51          try {
52            Field f = Unsafe.class.getDeclaredField("theUnsafe");
53            f.setAccessible(true);
54            return f.get(null);
55          } catch (Throwable e) {
56            LOG.warn("sun.misc.Unsafe is not accessible", e);
57          }
58          return null;
59        }
60      });
61  
62      if(theUnsafe != null){
63        BYTE_ARRAY_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class);
64        try {
65          // Using java.nio.Bits#unaligned() to check for unaligned-access capability
66          Class<?> clazz = Class.forName("java.nio.Bits");
67          Method m = clazz.getDeclaredMethod("unaligned");
68          m.setAccessible(true);
69          unaligned = (boolean) m.invoke(null);
70        } catch (Exception e) {
71          unaligned = false; // FindBugs: Causes REC_CATCH_EXCEPTION. Suppressed.
72        }
73      } else{
74        BYTE_ARRAY_BASE_OFFSET = -1;
75      }
76    }
77  
78    private UnsafeAccess(){}
79    
80    public static boolean isAvailable() {
81      return theUnsafe != null;
82    }
83  
84    /**
85     * @return true when running JVM is having sun's Unsafe package available in it and underlying
86     *         system having unaligned-access capability.
87     */
88    public static boolean unaligned() {
89      return unaligned;
90    }
91  
92    public static final boolean littleEndian = ByteOrder.nativeOrder()
93        .equals(ByteOrder.LITTLE_ENDIAN);
94  }