23
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/interfaceSupport.inline.hpp"
31 #include "runtime/orderAccess.hpp"
32 #include "runtime/os.hpp"
33 #include "runtime/semaphore.inline.hpp"
34 #include "runtime/synchronizer.hpp"
35 #include "threadHelper.inline.hpp"
36 #include "unittest.hpp"
37 #include "utilities/globalDefinitions.hpp"
38 #include "utilities/ostream.hpp"
39
40 // The test doesn't work for PRODUCT because it needs WizardMode
41 #ifndef PRODUCT
42
43 static void assert_test_pattern(Handle object, const char* pattern) {
44 stringStream st;
45 object->print_on(&st);
46 ASSERT_THAT(st.base(), testing::HasSubstr(pattern));
47 }
48
49 class LockerThread : public JavaTestThread {
50 oop _obj;
51 public:
52 LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}
53 virtual ~LockerThread() {}
54
55 void main_run() {
56 JavaThread* THREAD = JavaThread::current();
57 HandleMark hm(THREAD);
58 Handle h_obj(THREAD, _obj);
59 ResourceMark rm(THREAD);
60
61 // Wait gets the lock inflated.
62 // The object will stay locked for the context of 'ol' so the lock will
63 // still be inflated after the notify_all() call. Deflation can't happen
64 // while an ObjectMonitor is "busy" and being locked is the most "busy"
65 // state we have...
66 ObjectLocker ol(h_obj, THREAD);
67 ol.notify_all(THREAD);
68 assert_test_pattern(h_obj, "monitor");
69 }
70 };
71
72
73 TEST_VM(markWord, printing) {
74 JavaThread* THREAD = JavaThread::current();
75 ThreadInVMfromNative invm(THREAD);
76 ResourceMark rm(THREAD);
77
78 oop obj = vmClasses::Byte_klass()->allocate_instance(THREAD);
79
80 FlagSetting fs(WizardMode, true);
81
82 HandleMark hm(THREAD);
83 Handle h_obj(THREAD, obj);
84
85 // Thread tries to lock it.
86 {
87 ObjectLocker ol(h_obj, THREAD);
88 assert_test_pattern(h_obj, "locked");
89 }
90 assert_test_pattern(h_obj, "is_unlocked no_hash");
91
92 // Hash the object then print it.
93 intx hash = h_obj->identity_hash();
94 assert_test_pattern(h_obj, "is_unlocked hash=0x");
95
96 // Wait gets the lock inflated.
97 {
98 ObjectLocker ol(h_obj, THREAD);
99
100 Semaphore done(0);
101 LockerThread* st;
102 st = new LockerThread(&done, h_obj());
103 st->doit();
104
105 ol.wait(THREAD);
106 assert_test_pattern(h_obj, "monitor");
107 done.wait_with_safepoint_check(THREAD); // wait till the thread is done.
108 }
109 }
110 #endif // PRODUCT
|
23
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/interfaceSupport.inline.hpp"
31 #include "runtime/orderAccess.hpp"
32 #include "runtime/os.hpp"
33 #include "runtime/semaphore.inline.hpp"
34 #include "runtime/synchronizer.hpp"
35 #include "threadHelper.inline.hpp"
36 #include "unittest.hpp"
37 #include "utilities/globalDefinitions.hpp"
38 #include "utilities/ostream.hpp"
39
40 // The test doesn't work for PRODUCT because it needs WizardMode
41 #ifndef PRODUCT
42
43 template<typename Printable>
44 static void assert_test_pattern(Printable object, const char* pattern) {
45 stringStream st;
46 object->print_on(&st);
47 ASSERT_THAT(st.base(), testing::HasSubstr(pattern));
48 }
49
50 template<typename Printable>
51 static void assert_mark_word_print_pattern(Printable object, const char* pattern) {
52 if (LockingMode == LM_MONITOR) {
53 // With heavy monitors, we do not use the mark word. Printing the oop only shows "monitor" regardless of the
54 // locking state.
55 assert_test_pattern(object, "monitor");
56 } else {
57 assert_test_pattern(object, pattern);
58 }
59 }
60
61 class LockerThread : public JavaTestThread {
62 oop _obj;
63 public:
64 LockerThread(Semaphore* post, oop obj) : JavaTestThread(post), _obj(obj) {}
65 virtual ~LockerThread() {}
66
67 void main_run() {
68 JavaThread* THREAD = JavaThread::current();
69 HandleMark hm(THREAD);
70 Handle h_obj(THREAD, _obj);
71 ResourceMark rm(THREAD);
72
73 // Wait gets the lock inflated.
74 // The object will stay locked for the context of 'ol' so the lock will
75 // still be inflated after the notify_all() call. Deflation can't happen
76 // while an ObjectMonitor is "busy" and being locked is the most "busy"
77 // state we have...
78 ObjectLocker ol(h_obj, THREAD);
79 ol.notify_all(THREAD);
80 assert_test_pattern(h_obj, "monitor");
81 }
82 };
83
84
85 TEST_VM(markWord, printing) {
86 JavaThread* THREAD = JavaThread::current();
87 ThreadInVMfromNative invm(THREAD);
88 ResourceMark rm(THREAD);
89
90 oop obj = vmClasses::Byte_klass()->allocate_instance(THREAD);
91
92 FlagSetting fs(WizardMode, true);
93
94 HandleMark hm(THREAD);
95 Handle h_obj(THREAD, obj);
96
97 // Thread tries to lock it.
98 {
99 ObjectLocker ol(h_obj, THREAD);
100 assert_mark_word_print_pattern(h_obj, "locked");
101 }
102 assert_mark_word_print_pattern(h_obj, "is_unlocked no_hash");
103
104 // Hash the object then print it.
105 intx hash = h_obj->identity_hash();
106 assert_mark_word_print_pattern(h_obj, "is_unlocked hash=0x");
107
108 // Wait gets the lock inflated.
109 {
110 ObjectLocker ol(h_obj, THREAD);
111
112 Semaphore done(0);
113 LockerThread* st;
114 st = new LockerThread(&done, h_obj());
115 st->doit();
116
117 ol.wait(THREAD);
118 assert_test_pattern(h_obj, "monitor");
119 done.wait_with_safepoint_check(THREAD); // wait till the thread is done.
120 }
121 }
122
123 static void assert_unlocked_state(markWord mark) {
124 EXPECT_FALSE(mark.has_displaced_mark_helper());
125 if (LockingMode == LM_LEGACY) {
126 EXPECT_FALSE(mark.has_locker());
127 } else if (LockingMode == LM_LIGHTWEIGHT) {
128 EXPECT_FALSE(mark.is_fast_locked());
129 }
130 EXPECT_FALSE(mark.has_monitor());
131 EXPECT_FALSE(mark.is_being_inflated());
132 EXPECT_FALSE(mark.is_locked());
133 EXPECT_TRUE(mark.is_unlocked());
134 }
135
136 static void assert_copy_set_hash(markWord mark) {
137 const intptr_t hash = 4711;
138 EXPECT_TRUE(mark.has_no_hash());
139 markWord copy = mark.copy_set_hash(hash);
140 EXPECT_EQ(hash, copy.hash());
141 EXPECT_FALSE(copy.has_no_hash());
142 }
143
144 static void assert_type(markWord mark) {
145 EXPECT_FALSE(mark.is_flat_array());
146 EXPECT_FALSE(mark.is_inline_type());
147 EXPECT_FALSE(mark.is_larval_state());
148 EXPECT_FALSE(mark.is_null_free_array());
149 }
150
151 TEST_VM(markWord, prototype) {
152 markWord mark = markWord::prototype();
153 assert_unlocked_state(mark);
154 EXPECT_TRUE(mark.is_neutral());
155
156 assert_type(mark);
157
158 EXPECT_TRUE(mark.has_no_hash());
159 EXPECT_FALSE(mark.is_marked());
160 EXPECT_TRUE(mark.decode_pointer() == nullptr);
161
162 assert_copy_set_hash(mark);
163 assert_type(mark);
164 }
165
166 static void assert_inline_type(markWord mark) {
167 EXPECT_FALSE(mark.is_flat_array());
168 EXPECT_TRUE(mark.is_inline_type());
169 EXPECT_FALSE(mark.is_null_free_array());
170 }
171
172 TEST_VM(markWord, inline_type_prototype) {
173 markWord mark = markWord::inline_type_prototype();
174 assert_unlocked_state(mark);
175 EXPECT_FALSE(mark.is_neutral());
176 assert_test_pattern(&mark, " inline_type");
177
178 assert_inline_type(mark);
179 EXPECT_FALSE(mark.is_larval_state());
180
181 EXPECT_TRUE(mark.has_no_hash());
182 EXPECT_FALSE(mark.is_marked());
183 EXPECT_TRUE(mark.decode_pointer() == nullptr);
184
185 markWord larval = mark.enter_larval_state();
186 EXPECT_TRUE(larval.is_larval_state());
187 assert_inline_type(larval);
188 assert_test_pattern(&larval, " inline_type=larval");
189
190 mark = larval.exit_larval_state();
191 EXPECT_FALSE(mark.is_larval_state());
192 assert_inline_type(mark);
193
194 EXPECT_TRUE(mark.has_no_hash());
195 EXPECT_FALSE(mark.is_marked());
196 EXPECT_TRUE(mark.decode_pointer() == nullptr);
197 }
198
199 #if _LP64
200
201 static void assert_flat_array_type(markWord mark) {
202 EXPECT_TRUE(mark.is_flat_array());
203 EXPECT_FALSE(mark.is_inline_type());
204 EXPECT_FALSE(mark.is_larval_state());
205 }
206
207 TEST_VM(markWord, null_free_flat_array_prototype) {
208 markWord mark = markWord::flat_array_prototype(LayoutKind::NON_ATOMIC_FLAT);
209 assert_unlocked_state(mark);
210 EXPECT_TRUE(mark.is_neutral());
211
212 assert_flat_array_type(mark);
213 EXPECT_TRUE(mark.is_null_free_array());
214
215 EXPECT_TRUE(mark.has_no_hash());
216 EXPECT_FALSE(mark.is_marked());
217 EXPECT_TRUE(mark.decode_pointer() == nullptr);
218
219 assert_copy_set_hash(mark);
220 assert_flat_array_type(mark);
221 EXPECT_TRUE(mark.is_null_free_array());
222
223 assert_test_pattern(&mark, " flat_null_free_array");
224 }
225
226 TEST_VM(markWord, nullable_flat_array_prototype) {
227 markWord mark = markWord::flat_array_prototype(LayoutKind::NULLABLE_ATOMIC_FLAT);
228 assert_unlocked_state(mark);
229 EXPECT_TRUE(mark.is_neutral());
230
231 assert_flat_array_type(mark);
232 EXPECT_FALSE(mark.is_null_free_array());
233
234 EXPECT_TRUE(mark.has_no_hash());
235 EXPECT_FALSE(mark.is_marked());
236 EXPECT_TRUE(mark.decode_pointer() == nullptr);
237
238 assert_copy_set_hash(mark);
239 assert_flat_array_type(mark);
240 EXPECT_FALSE(mark.is_null_free_array());
241
242 assert_test_pattern(&mark, " flat_array");
243 }
244
245 static void assert_null_free_array_type(markWord mark) {
246 EXPECT_FALSE(mark.is_flat_array());
247 EXPECT_FALSE(mark.is_inline_type());
248 EXPECT_FALSE(mark.is_larval_state());
249 EXPECT_TRUE(mark.is_null_free_array());
250 }
251
252 TEST_VM(markWord, null_free_array_prototype) {
253 markWord mark = markWord::null_free_array_prototype();
254 assert_unlocked_state(mark);
255 EXPECT_TRUE(mark.is_neutral());
256
257 assert_null_free_array_type(mark);
258
259 EXPECT_TRUE(mark.has_no_hash());
260 EXPECT_FALSE(mark.is_marked());
261 EXPECT_TRUE(mark.decode_pointer() == nullptr);
262
263 assert_copy_set_hash(mark);
264 assert_null_free_array_type(mark);
265
266 assert_test_pattern(&mark, " null_free_array");
267 }
268 #endif // _LP64
269
270 #endif // PRODUCT
|