8207139: NMT is not enabled on Windows 2016/10
authormgronlun
Thu, 02 Aug 2018 09:49:04 +0200
changeset 51306 26cca23c165a
parent 51305 331888ea4a78
child 51307 4322ef0c1684
8207139: NMT is not enabled on Windows 2016/10 Reviewed-by: dcubed, zgu, dholmes
src/hotspot/share/services/memTracker.cpp
--- a/src/hotspot/share/services/memTracker.cpp	Tue Jul 31 14:04:29 2018 -0700
+++ b/src/hotspot/share/services/memTracker.cpp	Thu Aug 02 09:49:04 2018 +0200
@@ -35,6 +35,10 @@
 #include "utilities/defaultStream.hpp"
 #include "utilities/vmError.hpp"
 
+#ifdef _WINDOWS
+#include <windows.h>
+#endif
+
 #ifdef SOLARIS
   volatile bool NMT_stack_walkable = false;
 #else
@@ -48,24 +52,31 @@
 Mutex*      MemTracker::_query_lock = NULL;
 bool MemTracker::_is_nmt_env_valid = true;
 
+static const size_t buffer_size = 64;
 
 NMT_TrackingLevel MemTracker::init_tracking_level() {
+  char nmt_env_variable[buffer_size];
+  jio_snprintf(nmt_env_variable, sizeof(nmt_env_variable), "NMT_LEVEL_%d", os::current_process_id());
+  const char* nmt_env_value;
+#ifdef _WINDOWS
+  // Read the NMT environment variable from the PEB instead of the CRT
+  char value[buffer_size];
+  nmt_env_value = GetEnvironmentVariable(nmt_env_variable, value, (DWORD)sizeof(value)) != 0 ? value : NULL;
+#else
+  nmt_env_value = ::getenv(nmt_env_variable);
+#endif
   NMT_TrackingLevel level = NMT_off;
-  char buf[64];
-  jio_snprintf(buf, sizeof(buf), "NMT_LEVEL_%d", os::current_process_id());
-  const char *nmt_option = ::getenv(buf);
-  if (nmt_option != NULL) {
-    if (strcmp(nmt_option, "summary") == 0) {
+  if (nmt_env_value != NULL) {
+    if (strcmp(nmt_env_value, "summary") == 0) {
       level = NMT_summary;
-    } else if (strcmp(nmt_option, "detail") == 0) {
+    } else if (strcmp(nmt_env_value, "detail") == 0) {
       level = NMT_detail;
-    } else if (strcmp(nmt_option, "off") != 0) {
-      // The option value is invalid
+    } else if (strcmp(nmt_env_value, "off") != 0) {
+      // The value of the environment variable is invalid
       _is_nmt_env_valid = false;
     }
-
     // Remove the environment variable to avoid leaking to child processes
-    os::unsetenv(buf);
+    os::unsetenv(nmt_env_variable);
   }
 
   // Construct NativeCallStack::EMPTY_STACK. It may get constructed twice,