1 /*
   2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 *
   5 * This code is free software; you can redistribute it and/or modify it
   6 * under the terms of the GNU General Public License version 2 only, as
   7 * published by the Free Software Foundation.
   8 *
   9 * This code is distributed in the hope that it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 * version 2 for more details (a copy is included in the LICENSE file that
  13 * accompanied this code).
  14 *
  15 * You should have received a copy of the GNU General Public License version
  16 * 2 along with this work; if not, write to the Free Software Foundation,
  17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18 *
  19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20 * or visit www.oracle.com if you need additional information or have any
  21 * questions.
  22 */
  23 
  24 import java.lang.management.ManagementFactory;
  25 import java.lang.management.MemoryPoolMXBean;
  26 import java.lang.management.MemoryUsage;
  27 
  28 /**
  29  * Utility class used by tests to get heap region usage.
  30  */
  31 public final class HeapRegionUsageTool {
  32 
  33     /**
  34      * Get MemoryUsage from MemoryPoolMXBean which name matches passed string.
  35      *
  36      * @param name
  37      * @return MemoryUsage
  38      */
  39     private static MemoryUsage getUsage(String name){
  40         for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
  41             if (pool.getName().matches(name)) {
  42                 return pool.getUsage();
  43             }
  44         }
  45         return null;
  46     }
  47 
  48     /**
  49      * Get MemoryUsage of Eden space.
  50      *
  51      * @return MemoryUsage
  52      */
  53     public static MemoryUsage getEdenUsage() {
  54         return getUsage(".*Eden.*");
  55     }
  56 
  57     /**
  58      * Get MemoryUsage of Survivor space.
  59      *
  60      * @return MemoryUsage
  61      */
  62     public static MemoryUsage getSurvivorUsage() {
  63         return getUsage(".*Survivor.*");
  64     }
  65 
  66     /**
  67      * Get memory usage of Tenured space
  68      *
  69      * @return MemoryUsage
  70      */
  71     public static MemoryUsage getOldUsage() {
  72         return getUsage(".*(Old|Tenured).*");
  73     }
  74 
  75     /**
  76      * Get heap usage.
  77      *
  78      * @return MemoryUsage
  79      */
  80     public static MemoryUsage getHeapUsage() {
  81         return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
  82     }
  83 
  84     /**
  85      * Helper function to align up.
  86      *
  87      * @param value
  88      * @param alignment
  89      * @return aligned value
  90      */
  91     public static long alignUp(long value, long alignment) {
  92         return (value + alignment - 1) & ~(alignment - 1);
  93     }
  94 
  95     /**
  96      * Helper function to align down.
  97      *
  98      * @param value
  99      * @param alignment
 100      * @return aligned value
 101      */
 102     public static long alignDown(long value, long alignment) {
 103         return value & ~(alignment - 1);
 104     }
 105 }