8013590: NPG: Add a memory pool MXBean for Metaspace
authorehelin
Wed, 26 Jun 2013 16:58:37 +0200
changeset 18444 8adb4bc92f18
parent 18443 ca4b3427b7c7
child 18445 baaed11c4858
child 18493 752f5cbc2ba6
8013590: NPG: Add a memory pool MXBean for Metaspace Reviewed-by: jmasa, mgerdin
hotspot/src/share/vm/memory/metaspace.cpp
hotspot/src/share/vm/memory/universe.cpp
hotspot/src/share/vm/services/management.cpp
hotspot/src/share/vm/services/memoryManager.cpp
hotspot/src/share/vm/services/memoryManager.hpp
hotspot/src/share/vm/services/memoryPool.cpp
hotspot/src/share/vm/services/memoryPool.hpp
hotspot/src/share/vm/services/memoryService.cpp
hotspot/src/share/vm/services/memoryService.hpp
hotspot/test/gc/metaspace/TestMetaspaceMemoryPool.java
--- a/hotspot/src/share/vm/memory/metaspace.cpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/memory/metaspace.cpp	Wed Jun 26 16:58:37 2013 +0200
@@ -1556,19 +1556,7 @@
 
 // ChunkManager methods
 
-// Verification of _free_chunks_total and _free_chunks_count does not
-// work with the CMS collector because its use of additional locks
-// complicate the mutex deadlock detection but it can still be useful
-// for detecting errors in the chunk accounting with other collectors.
-
 size_t ChunkManager::free_chunks_total() {
-#ifdef ASSERT
-  if (!UseConcMarkSweepGC && !SpaceManager::expand_lock()->is_locked()) {
-    MutexLockerEx cl(SpaceManager::expand_lock(),
-                     Mutex::_no_safepoint_check_flag);
-    slow_locked_verify_free_chunks_total();
-  }
-#endif
   return _free_chunks_total;
 }
 
--- a/hotspot/src/share/vm/memory/universe.cpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/memory/universe.cpp	Wed Jun 26 16:58:37 2013 +0200
@@ -1127,6 +1127,7 @@
 
   // Initialize performance counters for metaspaces
   MetaspaceCounters::initialize_performance_counters();
+  MemoryService::add_metaspace_memory_pools();
 
   GC_locker::unlock();  // allow gc after bootstrapping
 
--- a/hotspot/src/share/vm/services/management.cpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/management.cpp	Wed Jun 26 16:58:37 2013 +0200
@@ -894,12 +894,6 @@
     }
   }
 
-  // In our current implementation, we make sure that all non-heap
-  // pools have defined init and max sizes. Heap pools do not matter,
-  // as we never use total_init and total_max for them.
-  assert(heap || !has_undefined_init_size, "Undefined init size");
-  assert(heap || !has_undefined_max_size,  "Undefined max size");
-
   MemoryUsage usage((heap ? InitialHeapSize : total_init),
                     total_used,
                     total_committed,
--- a/hotspot/src/share/vm/services/memoryManager.cpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/memoryManager.cpp	Wed Jun 26 16:58:37 2013 +0200
@@ -61,6 +61,10 @@
   return (MemoryManager*) new CodeCacheMemoryManager();
 }
 
+MemoryManager* MemoryManager::get_metaspace_memory_manager() {
+  return (MemoryManager*) new MetaspaceMemoryManager();
+}
+
 GCMemoryManager* MemoryManager::get_copy_memory_manager() {
   return (GCMemoryManager*) new CopyMemoryManager();
 }
--- a/hotspot/src/share/vm/services/memoryManager.hpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/memoryManager.hpp	Wed Jun 26 16:58:37 2013 +0200
@@ -56,6 +56,7 @@
   enum Name {
     Abstract,
     CodeCache,
+    Metaspace,
     Copy,
     MarkSweepCompact,
     ParNew,
@@ -88,6 +89,7 @@
 
   // Static factory methods to get a memory manager of a specific type
   static MemoryManager*   get_code_cache_memory_manager();
+  static MemoryManager*   get_metaspace_memory_manager();
   static GCMemoryManager* get_copy_memory_manager();
   static GCMemoryManager* get_msc_memory_manager();
   static GCMemoryManager* get_parnew_memory_manager();
@@ -108,6 +110,14 @@
   const char* name()         { return "CodeCacheManager"; }
 };
 
+class MetaspaceMemoryManager : public MemoryManager {
+public:
+  MetaspaceMemoryManager() : MemoryManager() {}
+
+  MemoryManager::Name kind() { return MemoryManager::Metaspace; }
+  const char *name()         { return "Metaspace Manager"; }
+};
+
 class GCStatInfo : public ResourceObj {
 private:
   size_t _index;
--- a/hotspot/src/share/vm/services/memoryPool.cpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/memoryPool.cpp	Wed Jun 26 16:58:37 2013 +0200
@@ -25,6 +25,7 @@
 #include "precompiled.hpp"
 #include "classfile/systemDictionary.hpp"
 #include "classfile/vmSymbols.hpp"
+#include "memory/metaspace.hpp"
 #include "oops/oop.inline.hpp"
 #include "runtime/handles.inline.hpp"
 #include "runtime/javaCalls.hpp"
@@ -33,6 +34,7 @@
 #include "services/memoryManager.hpp"
 #include "services/memoryPool.hpp"
 #include "utilities/macros.hpp"
+#include "utilities/globalDefinitions.hpp"
 
 MemoryPool::MemoryPool(const char* name,
                        PoolType type,
@@ -256,3 +258,39 @@
 
   return MemoryUsage(initial_size(), used, committed, maxSize);
 }
+
+MetaspacePool::MetaspacePool() :
+  MemoryPool("Metaspace", NonHeap, capacity_in_bytes(), calculate_max_size(), true, false) { }
+
+MemoryUsage MetaspacePool::get_memory_usage() {
+  size_t committed = align_size_down_(capacity_in_bytes(), os::vm_page_size());
+  return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());
+}
+
+size_t MetaspacePool::used_in_bytes() {
+  return MetaspaceAux::allocated_used_bytes(Metaspace::NonClassType);
+}
+
+size_t MetaspacePool::capacity_in_bytes() const {
+  return MetaspaceAux::allocated_capacity_bytes(Metaspace::NonClassType);
+}
+
+size_t MetaspacePool::calculate_max_size() const {
+  return FLAG_IS_CMDLINE(MaxMetaspaceSize) ? MaxMetaspaceSize : max_uintx;
+}
+
+CompressedKlassSpacePool::CompressedKlassSpacePool() :
+  MemoryPool("Compressed Class Space", NonHeap, capacity_in_bytes(), ClassMetaspaceSize, true, false) { }
+
+size_t CompressedKlassSpacePool::used_in_bytes() {
+  return MetaspaceAux::allocated_used_bytes(Metaspace::ClassType);
+}
+
+size_t CompressedKlassSpacePool::capacity_in_bytes() const {
+  return MetaspaceAux::allocated_capacity_bytes(Metaspace::ClassType);
+}
+
+MemoryUsage CompressedKlassSpacePool::get_memory_usage() {
+  size_t committed = align_size_down_(capacity_in_bytes(), os::vm_page_size());
+  return MemoryUsage(initial_size(), used_in_bytes(), committed, max_size());
+}
--- a/hotspot/src/share/vm/services/memoryPool.hpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/memoryPool.hpp	Wed Jun 26 16:58:37 2013 +0200
@@ -222,4 +222,21 @@
   size_t used_in_bytes()            { return _codeHeap->allocated_capacity(); }
 };
 
+class MetaspacePool : public MemoryPool {
+  size_t calculate_max_size() const;
+  size_t capacity_in_bytes() const;
+ public:
+  MetaspacePool();
+  MemoryUsage get_memory_usage();
+  size_t used_in_bytes();
+};
+
+class CompressedKlassSpacePool : public MemoryPool {
+  size_t capacity_in_bytes() const;
+ public:
+  CompressedKlassSpacePool();
+  MemoryUsage get_memory_usage();
+  size_t used_in_bytes();
+};
+
 #endif // SHARE_VM_SERVICES_MEMORYPOOL_HPP
--- a/hotspot/src/share/vm/services/memoryService.cpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/memoryService.cpp	Wed Jun 26 16:58:37 2013 +0200
@@ -35,6 +35,7 @@
 #include "memory/memRegion.hpp"
 #include "memory/tenuredGeneration.hpp"
 #include "oops/oop.inline.hpp"
+#include "runtime/globals.hpp"
 #include "runtime/javaCalls.hpp"
 #include "services/classLoadingService.hpp"
 #include "services/lowMemoryDetector.hpp"
@@ -60,9 +61,11 @@
 GrowableArray<MemoryManager*>* MemoryService::_managers_list =
   new (ResourceObj::C_HEAP, mtInternal) GrowableArray<MemoryManager*>(init_managers_list_size, true);
 
-GCMemoryManager* MemoryService::_minor_gc_manager = NULL;
-GCMemoryManager* MemoryService::_major_gc_manager = NULL;
-MemoryPool*      MemoryService::_code_heap_pool   = NULL;
+GCMemoryManager* MemoryService::_minor_gc_manager      = NULL;
+GCMemoryManager* MemoryService::_major_gc_manager      = NULL;
+MemoryPool*      MemoryService::_code_heap_pool        = NULL;
+MemoryPool*      MemoryService::_metaspace_pool        = NULL;
+MemoryPool*      MemoryService::_compressed_class_pool = NULL;
 
 class GcThreadCountClosure: public ThreadClosure {
  private:
@@ -399,6 +402,22 @@
   _managers_list->append(mgr);
 }
 
+void MemoryService::add_metaspace_memory_pools() {
+  MemoryManager* mgr = MemoryManager::get_metaspace_memory_manager();
+
+  _metaspace_pool = new MetaspacePool();
+  mgr->add_pool(_metaspace_pool);
+  _pools_list->append(_metaspace_pool);
+
+  if (UseCompressedKlassPointers) {
+    _compressed_class_pool = new CompressedKlassSpacePool();
+    mgr->add_pool(_compressed_class_pool);
+    _pools_list->append(_compressed_class_pool);
+  }
+
+  _managers_list->append(mgr);
+}
+
 MemoryManager* MemoryService::get_memory_manager(instanceHandle mh) {
   for (int i = 0; i < _managers_list->length(); i++) {
     MemoryManager* mgr = _managers_list->at(i);
--- a/hotspot/src/share/vm/services/memoryService.hpp	Tue Jun 25 15:17:11 2013 -0700
+++ b/hotspot/src/share/vm/services/memoryService.hpp	Wed Jun 26 16:58:37 2013 +0200
@@ -73,6 +73,9 @@
   // Code heap memory pool
   static MemoryPool*                    _code_heap_pool;
 
+  static MemoryPool*                    _metaspace_pool;
+  static MemoryPool*                    _compressed_class_pool;
+
   static void add_generation_memory_pool(Generation* gen,
                                          MemoryManager* major_mgr,
                                          MemoryManager* minor_mgr);
@@ -121,6 +124,7 @@
 public:
   static void set_universe_heap(CollectedHeap* heap);
   static void add_code_heap_memory_pool(CodeHeap* heap);
+  static void add_metaspace_memory_pools();
 
   static MemoryPool*    get_memory_pool(instanceHandle pool);
   static MemoryManager* get_memory_manager(instanceHandle mgr);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/gc/metaspace/TestMetaspaceMemoryPool.java	Wed Jun 26 16:58:37 2013 +0200
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.util.List;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryManagerMXBean;
+import java.lang.management.MemoryPoolMXBean;
+import java.lang.management.MemoryUsage;
+
+import java.lang.management.RuntimeMXBean;
+import java.lang.management.ManagementFactory;
+
+/* @test TestMetaspaceMemoryPool
+ * @bug 8000754
+ * @summary Tests that a MemoryPoolMXBeans is created for metaspace and that a
+ *          MemoryManagerMXBean is created.
+ * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops TestMetaspaceMemoryPool
+ * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:MaxMetaspaceSize=60m TestMetaspaceMemoryPool
+ * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers TestMetaspaceMemoryPool
+ * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:ClassMetaspaceSize=60m TestMetaspaceMemoryPool
+ */
+public class TestMetaspaceMemoryPool {
+    public static void main(String[] args) {
+        verifyThatMetaspaceMemoryManagerExists();
+        verifyMemoryPool(getMemoryPool("Metaspace"), isFlagDefined("MaxMetaspaceSize"));
+
+        if (runsOn64bit()) {
+            if (usesCompressedOops()) {
+                MemoryPoolMXBean cksPool = getMemoryPool("Compressed Class Space");
+                verifyMemoryPool(cksPool, true);
+            }
+        }
+    }
+
+    private static boolean runsOn64bit() {
+        return !System.getProperty("sun.arch.data.model").equals("32");
+    }
+
+    private static boolean usesCompressedOops() {
+        return isFlagDefined("+UseCompressedOops");
+    }
+
+    private static boolean isFlagDefined(String name) {
+        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
+        List<String> args = runtimeMxBean.getInputArguments();
+        for (String arg : args) {
+            if (arg.startsWith("-XX:" + name)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private static void verifyThatMetaspaceMemoryManagerExists() {
+        List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans();
+        for (MemoryManagerMXBean manager : managers) {
+            if (manager.getName().equals("Metaspace Manager")) {
+                return;
+            }
+        }
+
+        throw new RuntimeException("Expected to find a metaspace memory manager");
+    }
+
+    private static MemoryPoolMXBean getMemoryPool(String name) {
+        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
+        for (MemoryPoolMXBean pool : pools) {
+            if (pool.getName().equals(name)) {
+                return pool;
+            }
+        }
+
+        throw new RuntimeException("Expected to find a memory pool with name " + name);
+    }
+
+    private static void verifyMemoryPool(MemoryPoolMXBean pool, boolean isMaxDefined) {
+        MemoryUsage mu = pool.getUsage();
+        assertDefined(mu.getInit(), "init");
+        assertDefined(mu.getUsed(), "used");
+        assertDefined(mu.getCommitted(), "committed");
+
+        if (isMaxDefined) {
+            assertDefined(mu.getMax(), "max");
+        } else {
+            assertUndefined(mu.getMax(), "max");
+        }
+    }
+
+    private static void assertDefined(long value, String name) {
+        assertTrue(value != -1, "Expected " + name + " to be defined");
+    }
+
+    private static void assertUndefined(long value, String name) {
+        assertEquals(value, -1, "Expected " + name + " to be undefined");
+    }
+
+    private static void assertEquals(long actual, long expected, String msg) {
+        assertTrue(actual == expected, msg);
+    }
+
+    private static void assertTrue(boolean condition, String msg) {
+        if (!condition) {
+            throw new RuntimeException(msg);
+        }
+    }
+}