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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org
.apache
.hadoop
.hbase
.util
;
20 import java
.lang
.reflect
.Field
;
21 import java
.lang
.reflect
.Method
;
22 import java
.security
.AccessController
;
23 import java
.security
.PrivilegedAction
;
25 import org
.apache
.yetus
.audience
.InterfaceAudience
;
26 import org
.slf4j
.Logger
;
27 import org
.slf4j
.LoggerFactory
;
29 @InterfaceAudience.Private
30 public class UnsafeAvailChecker
{
32 private static final String CLASS_NAME
= "sun.misc.Unsafe";
33 private static final Logger LOG
= LoggerFactory
.getLogger(UnsafeAvailChecker
.class);
34 private static boolean avail
= false;
35 private static boolean unaligned
= false;
38 avail
= AccessController
.doPrivileged(new PrivilegedAction
<Boolean
>() {
40 public Boolean
run() {
42 Class
<?
> clazz
= Class
.forName(CLASS_NAME
);
43 Field f
= clazz
.getDeclaredField("theUnsafe");
44 f
.setAccessible(true);
45 return f
.get(null) != null;
46 } catch (Throwable e
) {
47 LOG
.warn("sun.misc.Unsafe is not available/accessible", e
);
52 // When Unsafe itself is not available/accessible consider unaligned as false.
54 String arch
= System
.getProperty("os.arch");
55 if ("ppc64".equals(arch
) || "ppc64le".equals(arch
) || "aarch64".equals(arch
)) {
56 // java.nio.Bits.unaligned() wrongly returns false on ppc (JDK-8165231),
60 // Using java.nio.Bits#unaligned() to check for unaligned-access capability
61 Class
<?
> clazz
= Class
.forName("java.nio.Bits");
62 Method m
= clazz
.getDeclaredMethod("unaligned");
63 m
.setAccessible(true);
64 unaligned
= (Boolean
) m
.invoke(null);
65 } catch (Exception e
) {
66 LOG
.warn("java.nio.Bits#unaligned() check failed."
67 + "Unsafe based read/write of primitive types won't be used", e
);
74 * @return true when running JVM is having sun's Unsafe package available in it and it is
77 public static boolean isAvailable() {
82 * @return true when running JVM is having sun's Unsafe package available in it and underlying
83 * system having unaligned-access capability.
85 public static boolean unaligned() {
89 private UnsafeAvailChecker() {
90 // private constructor to avoid instantiation