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