1 /*
2  * Copyright (c) 2015, 2019, 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 package gc.arguments;
25 
26 import java.lang.management.ManagementFactory;
27 import java.lang.management.MemoryPoolMXBean;
28 import java.lang.management.MemoryUsage;
29 
30 /**
31  * Utility class used by tests to get heap region usage.
32  */
33 public final class HeapRegionUsageTool {
34 
35     /**
36      * Get MemoryUsage from MemoryPoolMXBean which name matches passed string.
37      *
38      * @param name
39      * @return MemoryUsage
40      */
getUsage(String name)41     private static MemoryUsage getUsage(String name){
42         for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
43             if (pool.getName().matches(name)) {
44                 return pool.getUsage();
45             }
46         }
47         return null;
48     }
49 
50     /**
51      * Get MemoryUsage of Eden space.
52      *
53      * @return MemoryUsage
54      */
getEdenUsage()55     public static MemoryUsage getEdenUsage() {
56         return getUsage(".*Eden.*");
57     }
58 
59     /**
60      * Get MemoryUsage of Survivor space.
61      *
62      * @return MemoryUsage
63      */
getSurvivorUsage()64     public static MemoryUsage getSurvivorUsage() {
65         return getUsage(".*Survivor.*");
66     }
67 
68     /**
69      * Get memory usage of Tenured space
70      *
71      * @return MemoryUsage
72      */
getOldUsage()73     public static MemoryUsage getOldUsage() {
74         return getUsage(".*(Old|Tenured).*");
75     }
76 
77     /**
78      * Get heap usage.
79      *
80      * @return MemoryUsage
81      */
getHeapUsage()82     public static MemoryUsage getHeapUsage() {
83         return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
84     }
85 
86     /**
87      * Helper function to align up.
88      *
89      * @param value
90      * @param alignment
91      * @return aligned value
92      */
alignUp(long value, long alignment)93     public static long alignUp(long value, long alignment) {
94         return (value + alignment - 1) & ~(alignment - 1);
95     }
96 
97     /**
98      * Helper function to align down.
99      *
100      * @param value
101      * @param alignment
102      * @return aligned value
103      */
alignDown(long value, long alignment)104     public static long alignDown(long value, long alignment) {
105         return value & ~(alignment - 1);
106     }
107 }
108