8208499: NMT: Missing memory tag for Safepoint polling page
authorzgu
Tue, 31 Jul 2018 07:35:16 -0400
changeset 51264 cf34c71ca27c
parent 51263 b5aac518b097
child 51265 8329bfbe1b30
8208499: NMT: Missing memory tag for Safepoint polling page Summary: Added missing memory tag and cleanup memory type enum Reviewed-by: shade, coleenp
src/hotspot/share/memory/allocation.hpp
src/hotspot/share/runtime/safepointMechanism.cpp
src/hotspot/share/services/memTracker.cpp
src/hotspot/share/services/nmtCommon.cpp
test/hotspot/jtreg/runtime/NMT/SafepointPollingPages.java
--- a/src/hotspot/share/memory/allocation.hpp	Mon Jul 30 16:35:54 2018 -0400
+++ b/src/hotspot/share/memory/allocation.hpp	Tue Jul 31 07:35:16 2018 -0400
@@ -113,29 +113,30 @@
  */
 enum MemoryType {
   // Memory type by sub systems. It occupies lower byte.
-  mtJavaHeap          = 0x00,  // Java heap
-  mtClass             = 0x01,  // memory class for Java classes
-  mtThread            = 0x02,  // memory for thread objects
-  mtThreadStack       = 0x03,
-  mtCode              = 0x04,  // memory for generated code
-  mtGC                = 0x05,  // memory for GC
-  mtCompiler          = 0x06,  // memory for compiler
-  mtInternal          = 0x07,  // memory used by VM, but does not belong to
-                                 // any of above categories, and not used for
-                                 // native memory tracking
-  mtOther             = 0x08,  // memory not used by VM
-  mtSymbol            = 0x09,  // symbol
-  mtNMT               = 0x0A,  // memory used by native memory tracking
-  mtClassShared       = 0x0B,  // class data sharing
-  mtChunk             = 0x0C,  // chunk that holds content of arenas
-  mtTest              = 0x0D,  // Test type for verifying NMT
-  mtTracing           = 0x0E,  // memory used for Tracing
-  mtLogging           = 0x0F,  // memory for logging
-  mtArguments         = 0x10,  // memory for argument processing
-  mtModule            = 0x11,  // memory for module processing
-  mtNone              = 0x12,  // undefined
-  mt_number_of_types  = 0x13   // number of memory types (mtDontTrack
-                                 // is not included as validate type)
+  mtJavaHeap,          // Java heap
+  mtClass,             // memory class for Java classes
+  mtThread,            // memory for thread objects
+  mtThreadStack,
+  mtCode,              // memory for generated code
+  mtGC,                // memory for GC
+  mtCompiler,          // memory for compiler
+  mtInternal,          // memory used by VM, but does not belong to
+                       // any of above categories, and not used for
+                       // native memory tracking
+  mtOther,             // memory not used by VM
+  mtSymbol,            // symbol
+  mtNMT,               // memory used by native memory tracking
+  mtClassShared,       // class data sharing
+  mtChunk,             // chunk that holds content of arenas
+  mtTest,              // Test type for verifying NMT
+  mtTracing,           // memory used for Tracing
+  mtLogging,           // memory for logging
+  mtArguments,         // memory for argument processing
+  mtModule,            // memory for module processing
+  mtSafepoint,         // memory for safepoint support
+  mtNone,              // undefined
+  mt_number_of_types   // number of memory types (mtDontTrack
+                       // is not included as validate type)
 };
 
 typedef MemoryType MEMFLAGS;
--- a/src/hotspot/share/runtime/safepointMechanism.cpp	Mon Jul 30 16:35:54 2018 -0400
+++ b/src/hotspot/share/runtime/safepointMechanism.cpp	Tue Jul 31 07:35:16 2018 -0400
@@ -51,7 +51,7 @@
       const size_t allocation_size = 2 * page_size;
       char* polling_page = os::reserve_memory(allocation_size, NULL, page_size);
       os::commit_memory_or_exit(polling_page, allocation_size, false, "Unable to commit Safepoint polling page");
-      MemTracker::record_virtual_memory_type((address)polling_page, mtInternal);
+      MemTracker::record_virtual_memory_type((address)polling_page, mtSafepoint);
 
       char* bad_page  = polling_page;
       char* good_page = polling_page + page_size;
@@ -76,6 +76,7 @@
     char* polling_page = os::reserve_memory(page_size, NULL, page_size);
     os::commit_memory_or_exit(polling_page, page_size, false, "Unable to commit Safepoint polling page");
     os::protect_memory(polling_page, page_size, os::MEM_PROT_READ);
+    MemTracker::record_virtual_memory_type((address)polling_page, mtSafepoint);
 
     log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
     os::set_polling_page((address)(polling_page));
--- a/src/hotspot/share/services/memTracker.cpp	Mon Jul 30 16:35:54 2018 -0400
+++ b/src/hotspot/share/services/memTracker.cpp	Tue Jul 31 07:35:16 2018 -0400
@@ -32,6 +32,7 @@
 #include "services/memReporter.hpp"
 #include "services/mallocTracker.inline.hpp"
 #include "services/memTracker.hpp"
+#include "utilities/debug.hpp"
 #include "utilities/defaultStream.hpp"
 #include "utilities/vmError.hpp"
 
@@ -50,6 +51,10 @@
 
 
 NMT_TrackingLevel MemTracker::init_tracking_level() {
+  // Memory type is encoded into tracking header as a byte field,
+  // make sure that we don't overflow it.
+  STATIC_ASSERT(mt_number_of_types <= max_jubyte);
+
   NMT_TrackingLevel level = NMT_off;
   char buf[64];
   jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id());
--- a/src/hotspot/share/services/nmtCommon.cpp	Mon Jul 30 16:35:54 2018 -0400
+++ b/src/hotspot/share/services/nmtCommon.cpp	Tue Jul 31 07:35:16 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, 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
@@ -44,6 +44,7 @@
   "Logging",
   "Arguments",
   "Module",
+  "Safepoint",
   "Unknown"
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/hotspot/jtreg/runtime/NMT/SafepointPollingPages.java	Tue Jul 31 07:35:16 2018 -0400
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2018, 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.
+ */
+
+/*
+ * @test
+ * @bug 8208499
+ * @summary NMT should report safepoint polling page(s)
+ * @key nmt jcmd
+ * @library /test/lib
+ * @modules java.base/jdk.internal.misc
+ *          java.management
+ * @run main/othervm -Xbootclasspath/a:. -XX:NativeMemoryTracking=summary -XX:+ThreadLocalHandshakes SafepointPollingPages
+ * @run main/othervm -Xbootclasspath/a:. -XX:NativeMemoryTracking=summary -XX:-ThreadLocalHandshakes SafepointPollingPages
+ */
+
+import jdk.test.lib.process.ProcessTools;
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.JDKToolFinder;
+import jdk.internal.misc.Unsafe;
+
+public class SafepointPollingPages {
+  public static void main(String args[]) throws Exception {
+    OutputAnalyzer output;
+
+    // Grab my own PID
+    String pid = Long.toString(ProcessTools.getProcessId());
+    ProcessBuilder pb = new ProcessBuilder();
+
+    // Run 'jcmd <pid> VM.native_memory summary'
+    pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.native_memory", "summary"});
+    output = new OutputAnalyzer(pb.start());
+    output.shouldContain("Safepoint (reserved=");
+  }
+}
+