View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.util;
21  
22  import java.lang.management.ManagementFactory;
23  import java.lang.management.RuntimeMXBean;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.nio.ByteBuffer;
27  import java.util.List;
28  
29  import javax.management.JMException;
30  import javax.management.MBeanServer;
31  import javax.management.MalformedObjectNameException;
32  import javax.management.ObjectName;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.hadoop.hbase.classification.InterfaceAudience;
37  import org.apache.hadoop.hbase.classification.InterfaceStability;
38  
39  import com.google.common.base.Preconditions;
40  
41  /**
42   * Utilities for interacting with and monitoring DirectByteBuffer allocations.
43   */
44  @InterfaceAudience.Private
45  @InterfaceStability.Evolving
46  public class DirectMemoryUtils {
47    private static final Log LOG = LogFactory.getLog(DirectMemoryUtils.class);
48    private static final String MEMORY_USED = "MemoryUsed";
49    private static final MBeanServer BEAN_SERVER;
50    private static final ObjectName NIO_DIRECT_POOL;
51    private static final boolean HAS_MEMORY_USED_ATTRIBUTE;
52  
53    static {
54      // initialize singletons. Only maintain a reference to the MBeanServer if
55      // we're able to consume it -- hence convoluted logic.
56      ObjectName n = null;
57      MBeanServer s = null;
58      Object a = null;
59      try {
60        n = new ObjectName("java.nio:type=BufferPool,name=direct");
61      } catch (MalformedObjectNameException e) {
62        LOG.warn("Unable to initialize ObjectName for DirectByteBuffer allocations.");
63      } finally {
64        NIO_DIRECT_POOL = n;
65      }
66      if (NIO_DIRECT_POOL != null) {
67        s = ManagementFactory.getPlatformMBeanServer();
68      }
69      BEAN_SERVER = s;
70      if (BEAN_SERVER != null) {
71        try {
72          a = BEAN_SERVER.getAttribute(NIO_DIRECT_POOL, MEMORY_USED);
73        } catch (JMException e) {
74          LOG.debug("Failed to retrieve nio.BufferPool direct MemoryUsed attribute: " + e);
75        }
76      }
77      HAS_MEMORY_USED_ATTRIBUTE = a != null;
78    }
79  
80    /**
81     * @return the setting of -XX:MaxDirectMemorySize as a long. Returns 0 if
82     *         -XX:MaxDirectMemorySize is not set.
83     */
84    public static long getDirectMemorySize() {
85      RuntimeMXBean runtimemxBean = ManagementFactory.getRuntimeMXBean();
86      List<String> arguments = runtimemxBean.getInputArguments();
87      long multiplier = 1; //for the byte case.
88      for (String s : arguments) {
89        if (s.contains("-XX:MaxDirectMemorySize=")) {
90          String memSize = s.toLowerCase()
91              .replace("-xx:maxdirectmemorysize=", "").trim();
92  
93          if (memSize.contains("k")) {
94            multiplier = 1024;
95          }
96  
97          else if (memSize.contains("m")) {
98            multiplier = 1048576;
99          }
100 
101         else if (memSize.contains("g")) {
102           multiplier = 1073741824;
103         }
104         memSize = memSize.replaceAll("[^\\d]", "");
105 
106         long retValue = Long.parseLong(memSize);
107         return retValue * multiplier;
108       }
109     }
110     return 0;
111   }
112 
113   /**
114    * @return the current amount of direct memory used.
115    */
116   public static long getDirectMemoryUsage() {
117     if (BEAN_SERVER == null || NIO_DIRECT_POOL == null || !HAS_MEMORY_USED_ATTRIBUTE) return 0;
118     try {
119       Long value = (Long) BEAN_SERVER.getAttribute(NIO_DIRECT_POOL, MEMORY_USED);
120       return value == null ? 0 : value;
121     } catch (JMException e) {
122       // should print further diagnostic information?
123       return 0;
124     }
125   }
126 
127   /**
128    * DirectByteBuffers are garbage collected by using a phantom reference and a
129    * reference queue. Every once a while, the JVM checks the reference queue and
130    * cleans the DirectByteBuffers. However, as this doesn't happen
131    * immediately after discarding all references to a DirectByteBuffer, it's
132    * easy to OutOfMemoryError yourself using DirectByteBuffers. This function
133    * explicitly calls the Cleaner method of a DirectByteBuffer.
134    * 
135    * @param toBeDestroyed
136    *          The DirectByteBuffer that will be "cleaned". Utilizes reflection.
137    *          
138    */
139   public static void destroyDirectByteBuffer(ByteBuffer toBeDestroyed)
140       throws IllegalArgumentException, IllegalAccessException,
141       InvocationTargetException, SecurityException, NoSuchMethodException {
142 
143     Preconditions.checkArgument(toBeDestroyed.isDirect(),
144         "toBeDestroyed isn't direct!");
145 
146     Method cleanerMethod = toBeDestroyed.getClass().getMethod("cleaner");
147     cleanerMethod.setAccessible(true);
148     Object cleaner = cleanerMethod.invoke(toBeDestroyed);
149     Method cleanMethod = cleaner.getClass().getMethod("clean");
150     cleanMethod.setAccessible(true);
151     cleanMethod.invoke(cleaner);
152   }
153 }