annotate test/hotspot/gtest/oops/test_markOop.cpp @ 54725:bfa52d3068f5

8222893: markOopDesc::print_on() is a bit confused Summary: Add print_on for ObjectMonitor and make markOop printing sensible and add test. Reviewed-by: dcubed, pchilanomate
author coleenp
date Mon, 06 May 2019 15:25:57 -0400
parents
children 0f798f21e8c2
rev   line source
coleenp@54725 1 /*
coleenp@54725 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
coleenp@54725 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
coleenp@54725 4 *
coleenp@54725 5 * This code is free software; you can redistribute it and/or modify it
coleenp@54725 6 * under the terms of the GNU General Public License version 2 only, as
coleenp@54725 7 * published by the Free Software Foundation.
coleenp@54725 8 *
coleenp@54725 9 * This code is distributed in the hope that it will be useful, but WITHOUT
coleenp@54725 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
coleenp@54725 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
coleenp@54725 12 * version 2 for more details (a copy is included in the LICENSE file that
coleenp@54725 13 * accompanied this code).
coleenp@54725 14 *
coleenp@54725 15 * You should have received a copy of the GNU General Public License version
coleenp@54725 16 * 2 along with this work; if not, write to the Free Software Foundation,
coleenp@54725 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
coleenp@54725 18 *
coleenp@54725 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
coleenp@54725 20 * or visit www.oracle.com if you need additional information or have any
coleenp@54725 21 * questions.
coleenp@54725 22 */
coleenp@54725 23
coleenp@54725 24 #include "precompiled.hpp"
coleenp@54725 25 #include "classfile/systemDictionary.hpp"
coleenp@54725 26 #include "memory/resourceArea.hpp"
coleenp@54725 27 #include "memory/universe.hpp"
coleenp@54725 28 #include "oops/oop.inline.hpp"
coleenp@54725 29 #include "runtime/atomic.hpp"
coleenp@54725 30 #include "runtime/interfaceSupport.inline.hpp"
coleenp@54725 31 #include "runtime/orderAccess.hpp"
coleenp@54725 32 #include "runtime/os.hpp"
coleenp@54725 33 #include "runtime/synchronizer.hpp"
coleenp@54725 34 #include "threadHelper.inline.hpp"
coleenp@54725 35 #include "unittest.hpp"
coleenp@54725 36 #include "utilities/globalDefinitions.hpp"
coleenp@54725 37 #include "utilities/ostream.hpp"
coleenp@54725 38
coleenp@54725 39 static bool test_pattern(stringStream* st, const char* pattern) {
coleenp@54725 40 return (strstr(st->as_string(), pattern) != NULL);
coleenp@54725 41 }
coleenp@54725 42
coleenp@54725 43 static void assert_test_pattern(Handle object, const char* pattern) {
coleenp@54725 44 stringStream st;
coleenp@54725 45 object->print_on(&st);
coleenp@54725 46 ASSERT_TRUE(test_pattern(&st, pattern)) << pattern << " not in " << st.as_string();
coleenp@54725 47 }
coleenp@54725 48
coleenp@54725 49 static void assert_not_test_pattern(Handle object, const char* pattern) {
coleenp@54725 50 stringStream st;
coleenp@54725 51 object->print_on(&st);
coleenp@54725 52 ASSERT_FALSE(test_pattern(&st, pattern)) << pattern << " found in " << st.as_string();
coleenp@54725 53 }
coleenp@54725 54
coleenp@54725 55 class LockerThread : public JavaTestThread {
coleenp@54725 56 oop _obj;
coleenp@54725 57 public:
coleenp@54725 58 LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}
coleenp@54725 59 virtual ~LockerThread() {}
coleenp@54725 60
coleenp@54725 61 void main_run() {
coleenp@54725 62 Thread* THREAD = Thread::current();
coleenp@54725 63 HandleMark hm(THREAD);
coleenp@54725 64 Handle h_obj(THREAD, _obj);
coleenp@54725 65 ResourceMark rm(THREAD);
coleenp@54725 66
coleenp@54725 67 // Wait gets the lock inflated.
coleenp@54725 68 // The object will stay locked for the context of 'ol' so the lock will
coleenp@54725 69 // still be inflated after the notify_all() call. Deflation can't happen
coleenp@54725 70 // while an ObjectMonitor is "busy" and being locked is the most "busy"
coleenp@54725 71 // state we have...
coleenp@54725 72 ObjectLocker ol(h_obj, THREAD);
coleenp@54725 73 ol.notify_all(THREAD);
coleenp@54725 74 assert_test_pattern(h_obj, "monitor");
coleenp@54725 75 }
coleenp@54725 76 };
coleenp@54725 77
coleenp@54725 78
coleenp@54725 79 TEST_VM(markOopDesc, printing) {
coleenp@54725 80 JavaThread* THREAD = JavaThread::current();
coleenp@54725 81 ThreadInVMfromNative invm(THREAD);
coleenp@54725 82 ResourceMark rm(THREAD);
coleenp@54725 83
coleenp@54725 84 oop obj = SystemDictionary::Byte_klass()->allocate_instance(THREAD);
coleenp@54725 85
coleenp@54725 86 FlagSetting fs(WizardMode, true);
coleenp@54725 87 FlagSetting bf(UseBiasedLocking, true);
coleenp@54725 88
coleenp@54725 89 HandleMark hm(THREAD);
coleenp@54725 90 Handle h_obj(THREAD, obj);
coleenp@54725 91
coleenp@54725 92 // Biased locking is initially enabled for this java.lang.Byte object.
coleenp@54725 93 assert_test_pattern(h_obj, "is_biased");
coleenp@54725 94
coleenp@54725 95 // Lock using biased locking.
coleenp@54725 96 BasicObjectLock lock;
coleenp@54725 97 lock.set_obj(obj);
coleenp@54725 98 markOop mark = obj->mark()->incr_bias_epoch();
coleenp@54725 99 obj->set_mark(mark);
coleenp@54725 100 ObjectSynchronizer::fast_enter(h_obj, lock.lock(), true, THREAD);
coleenp@54725 101 #ifdef _LP64
coleenp@54725 102 // Look for the biased_locker in markOop, not prototype_header.
coleenp@54725 103 assert_not_test_pattern(h_obj, "mark(is_biased biased_locker=0x0000000000000000");
coleenp@54725 104 #endif
coleenp@54725 105
coleenp@54725 106 // Same thread tries to lock it again.
coleenp@54725 107 {
coleenp@54725 108 ObjectLocker ol(h_obj, THREAD);
coleenp@54725 109 assert_test_pattern(h_obj, "locked");
coleenp@54725 110 }
coleenp@54725 111
coleenp@54725 112 // This is no longer biased, because ObjectLocker revokes the bias.
coleenp@54725 113 assert_test_pattern(h_obj, "is_neutral no_hash");
coleenp@54725 114
coleenp@54725 115 // Wait gets the lock inflated.
coleenp@54725 116 {
coleenp@54725 117 ObjectLocker ol(h_obj, THREAD);
coleenp@54725 118
coleenp@54725 119 Semaphore done(0);
coleenp@54725 120 LockerThread* st;
coleenp@54725 121 st = new LockerThread(&done, h_obj());
coleenp@54725 122 st->doit();
coleenp@54725 123
coleenp@54725 124 ol.wait(THREAD);
coleenp@54725 125 assert_test_pattern(h_obj, "monitor");
coleenp@54725 126 }
coleenp@54725 127
coleenp@54725 128 // Make the object older. Not all GCs use this field.
coleenp@54725 129 Universe::heap()->collect(GCCause::_java_lang_system_gc);
coleenp@54725 130 if (UseParallelGC) {
coleenp@54725 131 assert_test_pattern(h_obj, "is_neutral no_hash age 1");
coleenp@54725 132 }
coleenp@54725 133
coleenp@54725 134 // Hash the object then print it.
coleenp@54725 135 intx hash = h_obj->identity_hash();
coleenp@54725 136 assert_test_pattern(h_obj, "is_neutral hash=0x");
coleenp@54725 137 }