Path: blob/master/test/hotspot/gtest/oops/test_markWord.cpp
41144 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "classfile/vmClasses.hpp"25#include "memory/resourceArea.hpp"26#include "memory/universe.hpp"27#include "oops/instanceKlass.hpp"28#include "oops/oop.inline.hpp"29#include "runtime/atomic.hpp"30#include "runtime/biasedLocking.hpp"31#include "runtime/interfaceSupport.inline.hpp"32#include "runtime/orderAccess.hpp"33#include "runtime/os.hpp"34#include "runtime/synchronizer.hpp"35#include "runtime/semaphore.inline.hpp"36#include "threadHelper.inline.hpp"37#include "unittest.hpp"38#include "utilities/globalDefinitions.hpp"39#include "utilities/ostream.hpp"4041// The test doesn't work for PRODUCT because it needs WizardMode42#ifndef PRODUCT43static bool test_pattern(stringStream* st, const char* pattern) {44return (strstr(st->as_string(), pattern) != NULL);45}4647static void assert_test_pattern(Handle object, const char* pattern) {48stringStream st;49object->print_on(&st);50ASSERT_TRUE(test_pattern(&st, pattern)) << pattern << " not in " << st.as_string();51}5253static void assert_not_test_pattern(Handle object, const char* pattern) {54stringStream st;55object->print_on(&st);56ASSERT_FALSE(test_pattern(&st, pattern)) << pattern << " found in " << st.as_string();57}5859class LockerThread : public JavaTestThread {60oop _obj;61public:62LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}63virtual ~LockerThread() {}6465void main_run() {66JavaThread* THREAD = JavaThread::current();67HandleMark hm(THREAD);68Handle h_obj(THREAD, _obj);69ResourceMark rm(THREAD);7071// Wait gets the lock inflated.72// The object will stay locked for the context of 'ol' so the lock will73// still be inflated after the notify_all() call. Deflation can't happen74// while an ObjectMonitor is "busy" and being locked is the most "busy"75// state we have...76ObjectLocker ol(h_obj, THREAD);77ol.notify_all(THREAD);78assert_test_pattern(h_obj, "monitor");79}80};818283TEST_VM(markWord, printing) {84JavaThread* THREAD = JavaThread::current();85ThreadInVMfromNative invm(THREAD);86ResourceMark rm(THREAD);8788oop obj = vmClasses::Byte_klass()->allocate_instance(THREAD);8990FlagSetting fs(WizardMode, true);9192HandleMark hm(THREAD);93Handle h_obj(THREAD, obj);9495if (UseBiasedLocking && BiasedLocking::enabled()) {96// Can't test this with biased locking disabled.97// Biased locking is initially enabled for this java.lang.Byte object.98assert_test_pattern(h_obj, "is_biased");99100// Lock using biased locking.101BasicObjectLock lock;102lock.set_obj(obj);103markWord prototype_header = obj->klass()->prototype_header();104markWord mark = obj->mark();105markWord biased_mark = markWord::encode((JavaThread*) THREAD, mark.age(), prototype_header.bias_epoch());106obj->set_mark(biased_mark);107// Look for the biased_locker in markWord, not prototype_header.108#ifdef _LP64109assert_not_test_pattern(h_obj, "mark(is_biased biased_locker=0x0000000000000000");110#else111assert_not_test_pattern(h_obj, "mark(is_biased biased_locker=0x00000000");112#endif113}114115// Same thread tries to lock it again.116{117ObjectLocker ol(h_obj, THREAD);118assert_test_pattern(h_obj, "locked");119}120121// This is no longer biased, because ObjectLocker revokes the bias.122assert_test_pattern(h_obj, "is_neutral no_hash");123124// Hash the object then print it.125intx hash = h_obj->identity_hash();126assert_test_pattern(h_obj, "is_neutral hash=0x");127128// Wait gets the lock inflated.129{130ObjectLocker ol(h_obj, THREAD);131132Semaphore done(0);133LockerThread* st;134st = new LockerThread(&done, h_obj());135st->doit();136137ol.wait(THREAD);138assert_test_pattern(h_obj, "monitor");139done.wait_with_safepoint_check(THREAD); // wait till the thread is done.140}141}142#endif // PRODUCT143144145