--- old/src/hotspot/share/prims/jvmtiTagMap.cpp 2020-02-21 11:14:39.900191309 +0100 +++ new/src/hotspot/share/prims/jvmtiTagMap.cpp 2020-02-21 11:14:39.644187025 +0100 @@ -28,6 +28,7 @@ #include "classfile/symbolTable.hpp" #include "classfile/systemDictionary.hpp" #include "classfile/vmSymbols.hpp" +#include "gc/shared/collectedHeap.hpp" #include "jvmtifiles/jvmtiEnv.hpp" #include "logging/log.hpp" #include "memory/allocation.inline.hpp" @@ -46,7 +47,6 @@ #include "prims/jvmtiExport.hpp" #include "prims/jvmtiImpl.hpp" #include "prims/jvmtiTagMap.hpp" -#include "runtime/biasedLocking.hpp" #include "runtime/frame.inline.hpp" #include "runtime/handles.inline.hpp" #include "runtime/javaCalls.hpp" @@ -1612,137 +1612,21 @@ return collector.result(count_ptr, object_result_ptr, tag_result_ptr); } - -// ObjectMarker is used to support the marking objects when walking the -// heap. -// -// This implementation uses the existing mark bits in an object for -// marking. Objects that are marked must later have their headers restored. -// As most objects are unlocked and don't have their identity hash computed -// we don't have to save their headers. Instead we save the headers that -// are "interesting". Later when the headers are restored this implementation -// restores all headers to their initial value and then restores the few -// objects that had interesting headers. -// -// Future work: This implementation currently uses growable arrays to save -// the oop and header of interesting objects. As an optimization we could -// use the same technique as the GC and make use of the unused area -// between top() and end(). -// - -// An ObjectClosure used to restore the mark bits of an object -class RestoreMarksClosure : public ObjectClosure { - public: - void do_object(oop o) { - if (o != NULL) { - markWord mark = o->mark(); - if (mark.is_marked()) { - o->init_mark(); - } - } - } -}; - -// ObjectMarker provides the mark and visited functions -class ObjectMarker : AllStatic { - private: - // saved headers - static GrowableArray* _saved_oop_stack; - static GrowableArray* _saved_mark_stack; - static bool _needs_reset; // do we need to reset mark bits? - - public: - static void init(); // initialize - static void done(); // clean-up - - static inline void mark(oop o); // mark an object - static inline bool visited(oop o); // check if object has been visited - - static inline bool needs_reset() { return _needs_reset; } - static inline void set_needs_reset(bool v) { _needs_reset = v; } -}; - -GrowableArray* ObjectMarker::_saved_oop_stack = NULL; -GrowableArray* ObjectMarker::_saved_mark_stack = NULL; -bool ObjectMarker::_needs_reset = true; // need to reset mark bits by default - -// initialize ObjectMarker - prepares for object marking -void ObjectMarker::init() { - assert(Thread::current()->is_VM_thread(), "must be VMThread"); - - // prepare heap for iteration - Universe::heap()->ensure_parsability(false); // no need to retire TLABs - - // create stacks for interesting headers - _saved_mark_stack = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(4000, true); - _saved_oop_stack = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(4000, true); - - if (UseBiasedLocking) { - BiasedLocking::preserve_marks(); - } -} - -// Object marking is done so restore object headers -void ObjectMarker::done() { - // iterate over all objects and restore the mark bits to - // their initial value - RestoreMarksClosure blk; - if (needs_reset()) { - Universe::heap()->object_iterate(&blk); - } else { - // We don't need to reset mark bits on this call, but reset the - // flag to the default for the next call. - set_needs_reset(true); - } - - // now restore the interesting headers - for (int i = 0; i < _saved_oop_stack->length(); i++) { - oop o = _saved_oop_stack->at(i); - markWord mark = _saved_mark_stack->at(i); - o->set_mark(mark); - } - - if (UseBiasedLocking) { - BiasedLocking::restore_marks(); - } - - // free the stacks - delete _saved_oop_stack; - delete _saved_mark_stack; -} - -// mark an object -inline void ObjectMarker::mark(oop o) { - assert(Universe::heap()->is_in(o), "sanity check"); - assert(!o->mark().is_marked(), "should only mark an object once"); - - // object's mark word - markWord mark = o->mark(); - - if (o->mark_must_be_preserved(mark)) { - _saved_mark_stack->push(mark); - _saved_oop_stack->push(o); - } - - // mark the object - o->set_mark(markWord::prototype().set_marked()); -} - -// return true if object is marked -inline bool ObjectMarker::visited(oop o) { - return o->mark().is_marked(); -} - // Stack allocated class to help ensure that ObjectMarker is used // correctly. Constructor initializes ObjectMarker, destructor calls // ObjectMarker's done() function to restore object headers. class ObjectMarkerController : public StackObj { - public: - ObjectMarkerController() { - ObjectMarker::init(); - } - ~ObjectMarkerController() { - ObjectMarker::done(); +private: + ObjectMarker* _marker; + +public: + ObjectMarkerController(); + ~ObjectMarkerController(); + + bool init(); + + inline ObjectMarker* object_marker() const { + return _marker; } }; @@ -1855,6 +1739,8 @@ // mode is for the newer FollowReferences function which supports a lot of // additional callbacks. class CallbackInvoker : AllStatic { + friend class ObjectMarkerController; + private: // heap walk styles enum { basic, advanced }; @@ -1880,6 +1766,7 @@ static JvmtiTagMap* _tag_map; static const void* _user_data; static GrowableArray* _visit_stack; + static ObjectMarker* _object_marker; // accessors static JvmtiTagMap* tag_map() { return _tag_map; } @@ -1889,7 +1776,7 @@ // if the object hasn't been visited then push it onto the visit stack // so that it will be visited later static inline bool check_for_visit(oop obj) { - if (!ObjectMarker::visited(obj)) visit_stack()->push(obj); + if (!_object_marker->marked(obj)) visit_stack()->push(obj); return true; } @@ -1959,6 +1846,7 @@ JvmtiTagMap* CallbackInvoker::_tag_map; const void* CallbackInvoker::_user_data; GrowableArray* CallbackInvoker::_visit_stack; +ObjectMarker* CallbackInvoker::_object_marker; // initialize for basic heap walk (IterateOverReachableObjects et al) void CallbackInvoker::initialize_for_basic_heap_walk(JvmtiTagMap* tag_map, @@ -2549,6 +2437,25 @@ } } + +ObjectMarkerController::ObjectMarkerController() { + _marker = Universe::heap()->object_marker(); +} + +bool ObjectMarkerController::init() { + if (_marker->init()) { + CallbackInvoker::_object_marker = _marker; + return true; + } else { + return false; + } +} + +ObjectMarkerController::~ObjectMarkerController() { + CallbackInvoker::_object_marker = NULL; + _marker->done(); +} + // A supporting closure used to process simple roots class SimpleRootsClosure : public OopClosure { private: @@ -3192,10 +3099,6 @@ // the objects referenced by this object). // bool VM_HeapWalkOperation::visit(oop o) { - // mark object as visited - assert(!ObjectMarker::visited(o), "can't visit same object more than once"); - ObjectMarker::mark(o); - // instance if (o->is_instance()) { if (o->klass() == SystemDictionary::Class_klass()) { @@ -3224,16 +3127,20 @@ void VM_HeapWalkOperation::doit() { ResourceMark rm; ObjectMarkerController marker; - ClassFieldMapCacheMark cm; + if (!marker.init()) { + return; + } + ClassFieldMapCacheMark cm; assert(visit_stack()->is_empty(), "visit stack must be empty"); + ObjectMarker* const object_marker = marker.object_marker(); // the heap walk starts with an initial object or the heap roots if (initial_object().is_null()) { // If either collect_stack_roots() or collect_simple_roots() // returns false at this point, then there are no mark bits // to reset. - ObjectMarker::set_needs_reset(false); + object_marker->set_needs_reset(false); // Calling collect_stack_roots() before collect_simple_roots() // can result in a big performance boost for an agent that is @@ -3243,7 +3150,7 @@ if (!collect_simple_roots()) return; // no early return so enable heap traversal to reset the mark bits - ObjectMarker::set_needs_reset(true); + object_marker->set_needs_reset(true); } else { visit_stack()->push(initial_object()()); } @@ -3255,7 +3162,7 @@ // visited or the callback asked to terminate the iteration. while (!visit_stack()->is_empty()) { oop o = visit_stack()->pop(); - if (!ObjectMarker::visited(o)) { + if (object_marker->mark(o)) { if (!visit(o)) { break; }