changeset 60151:a5649ebf9419

8245226: Clean-up FlagSetting and remove misuse. Reviewed-by: neliasso, dholmes
author phedlin
date Mon, 24 Feb 2020 14:06:01 +0100
parents 03f5eff51e5c
children 6aacfbc575a8
files src/hotspot/share/gc/g1/g1CollectedHeap.cpp src/hotspot/share/gc/shared/genCollectedHeap.cpp src/hotspot/share/memory/universe.cpp src/hotspot/share/runtime/flags/flagSetting.hpp src/hotspot/share/runtime/handshake.hpp src/hotspot/share/utilities/autoRestore.hpp test/hotspot/gtest/gc/shared/test_collectorPolicy.cpp test/hotspot/gtest/runtime/test_globals.cpp test/hotspot/gtest/runtime/test_os_windows.cpp
diffstat 9 files changed, 94 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/gc/g1/g1CollectedHeap.cpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/src/hotspot/share/gc/g1/g1CollectedHeap.cpp	Mon Feb 24 14:06:01 2020 +0100
@@ -95,13 +95,13 @@
 #include "oops/compressedOops.inline.hpp"
 #include "oops/oop.inline.hpp"
 #include "runtime/atomic.hpp"
-#include "runtime/flags/flagSetting.hpp"
 #include "runtime/handles.inline.hpp"
 #include "runtime/init.hpp"
 #include "runtime/orderAccess.hpp"
 #include "runtime/threadSMR.hpp"
 #include "runtime/vmThread.hpp"
 #include "utilities/align.hpp"
+#include "utilities/autoRestore.hpp"
 #include "utilities/bitMap.inline.hpp"
 #include "utilities/globalDefinitions.hpp"
 #include "utilities/stack.inline.hpp"
@@ -1940,7 +1940,7 @@
 
   // _filler_array_max_size is set to humongous object threshold
   // but temporarily change it to use CollectedHeap::fill_with_object().
-  SizeTFlagSetting fs(_filler_array_max_size, word_size);
+  AutoModifyRestore<size_t> temporarily(_filler_array_max_size, word_size);
 
   for (uintx i = 0; i < G1DummyRegionsPerGC; ++i) {
     // Let's use the existing mechanism for the allocation
--- a/src/hotspot/share/gc/shared/genCollectedHeap.cpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/src/hotspot/share/gc/shared/genCollectedHeap.cpp	Mon Feb 24 14:06:01 2020 +0100
@@ -63,13 +63,13 @@
 #include "memory/universe.hpp"
 #include "oops/oop.inline.hpp"
 #include "runtime/biasedLocking.hpp"
-#include "runtime/flags/flagSetting.hpp"
 #include "runtime/handles.hpp"
 #include "runtime/handles.inline.hpp"
 #include "runtime/java.hpp"
 #include "runtime/vmThread.hpp"
 #include "services/management.hpp"
 #include "services/memoryService.hpp"
+#include "utilities/autoRestore.hpp"
 #include "utilities/debug.hpp"
 #include "utilities/formatBuffer.hpp"
 #include "utilities/macros.hpp"
@@ -566,7 +566,7 @@
 
   ClearedAllSoftRefs casr(do_clear_all_soft_refs, soft_ref_policy());
 
-  FlagSetting fl(_is_gc_active, true);
+  AutoModifyRestore<bool> temporarily(_is_gc_active, true);
 
   bool complete = full && (max_generation == OldGen);
   bool old_collects_young = complete && !ScavengeBeforeFullGC;
--- a/src/hotspot/share/memory/universe.cpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/src/hotspot/share/memory/universe.cpp	Mon Feb 24 14:06:01 2020 +0100
@@ -64,7 +64,6 @@
 #include "runtime/arguments.hpp"
 #include "runtime/atomic.hpp"
 #include "runtime/deoptimization.hpp"
-#include "runtime/flags/flagSetting.hpp"
 #include "runtime/flags/jvmFlagConstraintList.hpp"
 #include "runtime/handles.inline.hpp"
 #include "runtime/init.hpp"
@@ -77,6 +76,7 @@
 #include "runtime/vmOperations.hpp"
 #include "services/memoryService.hpp"
 #include "utilities/align.hpp"
+#include "utilities/autoRestore.hpp"
 #include "utilities/copy.hpp"
 #include "utilities/debug.hpp"
 #include "utilities/events.hpp"
@@ -298,7 +298,7 @@
 void Universe::genesis(TRAPS) {
   ResourceMark rm(THREAD);
 
-  { FlagSetting fs(_bootstrapping, true);
+  { AutoModifyRestore<bool> temporarily(_bootstrapping, true);
 
     { MutexLocker mc(THREAD, Compile_lock);
 
--- a/src/hotspot/share/runtime/flags/flagSetting.hpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/src/hotspot/share/runtime/flags/flagSetting.hpp	Mon Feb 24 14:06:01 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2020, 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
@@ -25,47 +25,25 @@
 #ifndef SHARE_RUNTIME_FLAGS_FLAGSETTING_HPP
 #define SHARE_RUNTIME_FLAGS_FLAGSETTING_HPP
 
-#include "memory/allocation.hpp"
-
-// debug flags control various aspects of the VM and are global accessible
-
-// use FlagSetting to temporarily change some debug flag
-// e.g. FlagSetting fs(DebugThisAndThat, true);
-// restored to previous value upon leaving scope
-class FlagSetting : public StackObj {
-  bool val;
-  bool* flag;
-public:
-  FlagSetting(bool& fl, bool newValue) { flag = &fl; val = fl; fl = newValue; }
-  ~FlagSetting()                       { *flag = val; }
-};
+#include "utilities/autoRestore.hpp"
 
-class UIntFlagSetting : public StackObj {
-  uint val;
-  uint* flag;
-public:
-  UIntFlagSetting(uint& fl, uint newValue) { flag = &fl; val = fl; fl = newValue; }
-  ~UIntFlagSetting()                       { *flag = val; }
-};
+// Legacy use of FlagSetting and UIntFlagSetting to temporarily change a debug
+// flag/option in the current (local) scope.
+//
+// Example:
+// {
+//   FlagSetting temporarily(DebugThisAndThat, true);
+//   . . .
+// }
+//
+// The previous/original value is restored when leaving the scope.
 
-class SizeTFlagSetting : public StackObj {
-  size_t val;
-  size_t* flag;
-public:
-  SizeTFlagSetting(size_t& fl, size_t newValue) { flag = &fl; val = fl; fl = newValue; }
-  ~SizeTFlagSetting()                           { *flag = val; }
-};
+typedef AutoModifyRestore<bool> FlagSetting;
+typedef AutoModifyRestore<uint> UIntFlagSetting;
 
-// Helper class for temporarily saving the value of a flag during a scope.
-template <size_t SIZE>
-class FlagGuard {
-  unsigned char _value[SIZE];
-  void* const _addr;
-public:
-  FlagGuard(void* flag_addr) : _addr(flag_addr) { memcpy(_value, _addr, SIZE); }
-  ~FlagGuard()                                  { memcpy(_addr, _value, SIZE); }
-};
+// Legacy use of FLAG_GUARD. Retained in the code to help identify use-cases
+// that should be addressed when this file is removed.
 
-#define FLAG_GUARD(f) FlagGuard<sizeof(f)> f ## _guard(&f)
+#define FLAG_GUARD(f) f ## _guard(f)
 
 #endif // SHARE_RUNTIME_FLAGS_FLAGSETTING_HPP
--- a/src/hotspot/share/runtime/handshake.hpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/src/hotspot/share/runtime/handshake.hpp	Mon Feb 24 14:06:01 2020 +0100
@@ -27,8 +27,8 @@
 
 #include "memory/allocation.hpp"
 #include "memory/iterator.hpp"
-#include "runtime/flags/flagSetting.hpp"
 #include "runtime/semaphore.hpp"
+#include "utilities/autoRestore.hpp"
 
 class HandshakeOperation;
 class JavaThread;
@@ -92,7 +92,7 @@
 
   void process_by_self() {
     if (!_thread_in_process_handshake) {
-      FlagSetting fs(_thread_in_process_handshake, true);
+      AutoModifyRestore<bool> temporarily(_thread_in_process_handshake, true);
       process_self_inner();
     }
   }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hotspot/share/utilities/autoRestore.hpp	Mon Feb 24 14:06:01 2020 +0100
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2020, 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.
+ *
+ */
+
+#ifndef SHARE_UTILITIES_AUTORESTORE_HPP
+#define SHARE_UTILITIES_AUTORESTORE_HPP
+
+#include "memory/allocation.hpp"
+
+// A simplistic template providing a general save-restore pattern through a
+// local auto/stack object (scope).
+//
+template<typename T> class AutoSaveRestore : public StackObj {
+public:
+  AutoSaveRestore(T &loc) : _loc(loc) {
+    _value = loc;
+  }
+  ~AutoSaveRestore() {
+    _loc = _value;
+  }
+private:
+  T &_loc;
+  T _value;
+};
+
+// A simplistic template providing a general modify-restore pattern through a
+// local auto/stack object (scope).
+//
+template<typename T> class AutoModifyRestore : private AutoSaveRestore<T> {
+public:
+  AutoModifyRestore(T &loc, T value) : AutoSaveRestore<T>(loc) {
+    loc = value;
+  }
+};
+
+#endif // SHARE_UTILITIES_AUTORESTORE_HPP
--- a/test/hotspot/gtest/gc/shared/test_collectorPolicy.cpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/test/hotspot/gtest/gc/shared/test_collectorPolicy.cpp	Mon Feb 24 14:06:01 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2020, 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
@@ -57,13 +57,13 @@
   class TestWrapper {
    public:
     static void test(Executor* setter1, Executor* setter2, Executor* checker) {
-      FLAG_GUARD(MinHeapSize);
-      FLAG_GUARD(InitialHeapSize);
-      FLAG_GUARD(MaxHeapSize);
-      FLAG_GUARD(MaxNewSize);
-      FLAG_GUARD(MinHeapDeltaBytes);
-      FLAG_GUARD(NewSize);
-      FLAG_GUARD(OldSize);
+      AutoSaveRestore<size_t> FLAG_GUARD(MinHeapSize);
+      AutoSaveRestore<size_t> FLAG_GUARD(InitialHeapSize);
+      AutoSaveRestore<size_t> FLAG_GUARD(MaxHeapSize);
+      AutoSaveRestore<size_t> FLAG_GUARD(MaxNewSize);
+      AutoSaveRestore<size_t> FLAG_GUARD(MinHeapDeltaBytes);
+      AutoSaveRestore<size_t> FLAG_GUARD(NewSize);
+      AutoSaveRestore<size_t> FLAG_GUARD(OldSize);
 
       MinHeapSize = 40 * M;
       FLAG_SET_ERGO(InitialHeapSize, 100 * M);
--- a/test/hotspot/gtest/runtime/test_globals.cpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/test/hotspot/gtest/runtime/test_globals.cpp	Mon Feb 24 14:06:01 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2020, 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
@@ -32,7 +32,7 @@
     ASSERT_TRUE(JVMFlag::find_flag(#f)->is_ ## type());          \
     type original_value = f;                                     \
     {                                                            \
-      FLAG_GUARD(f);                                             \
+      AutoSaveRestore<type> FLAG_GUARD(f);                       \
       f = value;                                                 \
     }                                                            \
     ASSERT_EQ(original_value, f);                                \
--- a/test/hotspot/gtest/runtime/test_os_windows.cpp	Tue Jul 14 05:10:46 2020 +0200
+++ b/test/hotspot/gtest/runtime/test_os_windows.cpp	Mon Feb 24 14:06:01 2020 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2020, 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
@@ -57,8 +57,8 @@
   }
 
   // set globals to make sure we hit the correct code path
-  FLAG_GUARD(UseLargePagesIndividualAllocation);
-  FLAG_GUARD(UseNUMAInterleaving);
+  AutoSaveRestore<bool> FLAG_GUARD(UseLargePagesIndividualAllocation);
+  AutoSaveRestore<bool> FLAG_GUARD(UseNUMAInterleaving);
   FLAG_SET_CMDLINE(UseLargePagesIndividualAllocation, false);
   FLAG_SET_CMDLINE(UseNUMAInterleaving, false);