1 /*
2 * Copyright (c) 1998, 2014, 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 *
1176
1177 Thread::muxRelease (&ListLock) ;
1178 TEVENT (omFlush) ;
1179 }
1180
1181 // Fast path code shared by multiple functions
1182 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1183 markOop mark = obj->mark();
1184 if (mark->has_monitor()) {
1185 assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1186 assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1187 return mark->monitor();
1188 }
1189 return ObjectSynchronizer::inflate(Thread::current(), obj);
1190 }
1191
1192
1193 // Note that we could encounter some performance loss through false-sharing as
1194 // multiple locks occupy the same $ line. Padding might be appropriate.
1195
1196
1197 ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
1198 // Inflate mutates the heap ...
1199 // Relaxing assertion for bug 6320749.
1200 assert (Universe::verify_in_progress() ||
1201 !SafepointSynchronize::is_at_safepoint(), "invariant") ;
1202
1203 for (;;) {
1204 const markOop mark = object->mark() ;
1205 assert (!mark->has_bias_pattern(), "invariant") ;
1206
1207 // The mark can be in one of the following states:
1208 // * Inflated - just return
1209 // * Stack-locked - coerce it to inflated
1210 // * INFLATING - busy wait for conversion to complete
1211 // * Neutral - aggressively inflate the object.
1212 // * BIASED - Illegal. We should never see this
1213
1214 // CASE: inflated
1215 if (mark->has_monitor()) {
1216 ObjectMonitor * inf = mark->monitor() ;
1217 assert (inf->header()->is_neutral(), "invariant");
1218 assert (inf->object() == object, "invariant") ;
1219 assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1220 return inf ;
1221 }
1222
1313 m->set_object(object);
1314 // TODO-FIXME: assert BasicLock->dhw != 0.
1315
1316 // Must preserve store ordering. The monitor state must
1317 // be stable at the time of publishing the monitor address.
1318 guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
1319 object->release_set_mark(markOopDesc::encode(m));
1320
1321 // Hopefully the performance counters are allocated on distinct cache lines
1322 // to avoid false sharing on MP systems ...
1323 if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1324 TEVENT(Inflate: overwrite stacklock) ;
1325 if (TraceMonitorInflation) {
1326 if (object->is_instance()) {
1327 ResourceMark rm;
1328 tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1329 (void *) object, (intptr_t) object->mark(),
1330 object->klass()->external_name());
1331 }
1332 }
1333 return m ;
1334 }
1335
1336 // CASE: neutral
1337 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1338 // If we know we're inflating for entry it's better to inflate by swinging a
1339 // pre-locked objectMonitor pointer into the object header. A successful
1340 // CAS inflates the object *and* confers ownership to the inflating thread.
1341 // In the current implementation we use a 2-step mechanism where we CAS()
1342 // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1343 // An inflateTry() method that we could call from fast_enter() and slow_enter()
1344 // would be useful.
1345
1346 assert (mark->is_neutral(), "invariant");
1347 ObjectMonitor * m = omAlloc (Self) ;
1348 // prepare m for installation - set monitor to initial state
1349 m->Recycle();
1350 m->set_header(mark);
1351 m->set_owner(NULL);
1352 m->set_object(object);
1363 omRelease (Self, m, true) ;
1364 m = NULL ;
1365 continue ;
1366 // interference - the markword changed - just retry.
1367 // The state-transitions are one-way, so there's no chance of
1368 // live-lock -- "Inflated" is an absorbing state.
1369 }
1370
1371 // Hopefully the performance counters are allocated on distinct
1372 // cache lines to avoid false sharing on MP systems ...
1373 if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1374 TEVENT(Inflate: overwrite neutral) ;
1375 if (TraceMonitorInflation) {
1376 if (object->is_instance()) {
1377 ResourceMark rm;
1378 tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1379 (void *) object, (intptr_t) object->mark(),
1380 object->klass()->external_name());
1381 }
1382 }
1383 return m ;
1384 }
1385 }
1386
1387 // Note that we could encounter some performance loss through false-sharing as
1388 // multiple locks occupy the same $ line. Padding might be appropriate.
1389
1390
1391 // Deflate_idle_monitors() is called at all safepoints, immediately
1392 // after all mutators are stopped, but before any objects have moved.
1393 // It traverses the list of known monitors, deflating where possible.
1394 // The scavenged monitor are returned to the monitor free list.
1395 //
1396 // Beware that we scavenge at *every* stop-the-world point.
1397 // Having a large number of monitors in-circulation negatively
1398 // impacts the performance of some applications (e.g., PointBase).
1399 // Broadly, we want to minimize the # of monitors in circulation.
1400 //
1401 // We have added a flag, MonitorInUseLists, which creates a list
1402 // of active monitors for each thread. deflate_idle_monitors()
|
1 /*
2 * Copyright (c) 1998, 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 *
1176
1177 Thread::muxRelease (&ListLock) ;
1178 TEVENT (omFlush) ;
1179 }
1180
1181 // Fast path code shared by multiple functions
1182 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1183 markOop mark = obj->mark();
1184 if (mark->has_monitor()) {
1185 assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1186 assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1187 return mark->monitor();
1188 }
1189 return ObjectSynchronizer::inflate(Thread::current(), obj);
1190 }
1191
1192
1193 // Note that we could encounter some performance loss through false-sharing as
1194 // multiple locks occupy the same $ line. Padding might be appropriate.
1195
1196 static void post_monitor_inflate_event(EventJavaMonitorInflate& event,
1197 const oop obj,
1198 //const ObjectSynchronizer::InflateCause cause) {
1199 // unsupport cause
1200 const u1 cause) {
1201 #if INCLUDE_TRACE
1202 assert(event.should_commit(), "check outside");
1203 event.set_monitorClass(obj->klass());
1204 event.set_address((TYPE_ADDRESS)(uintptr_t)(void*)obj);
1205 event.set_cause((u1)cause);
1206 event.commit();
1207 #endif
1208 }
1209
1210 ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
1211 // Inflate mutates the heap ...
1212 // Relaxing assertion for bug 6320749.
1213 assert (Universe::verify_in_progress() ||
1214 !SafepointSynchronize::is_at_safepoint(), "invariant") ;
1215
1216 EventJavaMonitorInflate event;
1217
1218 for (;;) {
1219 const markOop mark = object->mark() ;
1220 assert (!mark->has_bias_pattern(), "invariant") ;
1221
1222 // The mark can be in one of the following states:
1223 // * Inflated - just return
1224 // * Stack-locked - coerce it to inflated
1225 // * INFLATING - busy wait for conversion to complete
1226 // * Neutral - aggressively inflate the object.
1227 // * BIASED - Illegal. We should never see this
1228
1229 // CASE: inflated
1230 if (mark->has_monitor()) {
1231 ObjectMonitor * inf = mark->monitor() ;
1232 assert (inf->header()->is_neutral(), "invariant");
1233 assert (inf->object() == object, "invariant") ;
1234 assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1235 return inf ;
1236 }
1237
1328 m->set_object(object);
1329 // TODO-FIXME: assert BasicLock->dhw != 0.
1330
1331 // Must preserve store ordering. The monitor state must
1332 // be stable at the time of publishing the monitor address.
1333 guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
1334 object->release_set_mark(markOopDesc::encode(m));
1335
1336 // Hopefully the performance counters are allocated on distinct cache lines
1337 // to avoid false sharing on MP systems ...
1338 if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1339 TEVENT(Inflate: overwrite stacklock) ;
1340 if (TraceMonitorInflation) {
1341 if (object->is_instance()) {
1342 ResourceMark rm;
1343 tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1344 (void *) object, (intptr_t) object->mark(),
1345 object->klass()->external_name());
1346 }
1347 }
1348 if (event.should_commit()) {
1349 post_monitor_inflate_event(event, object, (u1)0);
1350 }
1351 return m ;
1352 }
1353
1354 // CASE: neutral
1355 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1356 // If we know we're inflating for entry it's better to inflate by swinging a
1357 // pre-locked objectMonitor pointer into the object header. A successful
1358 // CAS inflates the object *and* confers ownership to the inflating thread.
1359 // In the current implementation we use a 2-step mechanism where we CAS()
1360 // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1361 // An inflateTry() method that we could call from fast_enter() and slow_enter()
1362 // would be useful.
1363
1364 assert (mark->is_neutral(), "invariant");
1365 ObjectMonitor * m = omAlloc (Self) ;
1366 // prepare m for installation - set monitor to initial state
1367 m->Recycle();
1368 m->set_header(mark);
1369 m->set_owner(NULL);
1370 m->set_object(object);
1381 omRelease (Self, m, true) ;
1382 m = NULL ;
1383 continue ;
1384 // interference - the markword changed - just retry.
1385 // The state-transitions are one-way, so there's no chance of
1386 // live-lock -- "Inflated" is an absorbing state.
1387 }
1388
1389 // Hopefully the performance counters are allocated on distinct
1390 // cache lines to avoid false sharing on MP systems ...
1391 if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1392 TEVENT(Inflate: overwrite neutral) ;
1393 if (TraceMonitorInflation) {
1394 if (object->is_instance()) {
1395 ResourceMark rm;
1396 tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1397 (void *) object, (intptr_t) object->mark(),
1398 object->klass()->external_name());
1399 }
1400 }
1401 if (event.should_commit()) {
1402 post_monitor_inflate_event(event, object, (u1)0);
1403 }
1404 return m ;
1405 }
1406 }
1407
1408 // Note that we could encounter some performance loss through false-sharing as
1409 // multiple locks occupy the same $ line. Padding might be appropriate.
1410
1411
1412 // Deflate_idle_monitors() is called at all safepoints, immediately
1413 // after all mutators are stopped, but before any objects have moved.
1414 // It traverses the list of known monitors, deflating where possible.
1415 // The scavenged monitor are returned to the monitor free list.
1416 //
1417 // Beware that we scavenge at *every* stop-the-world point.
1418 // Having a large number of monitors in-circulation negatively
1419 // impacts the performance of some applications (e.g., PointBase).
1420 // Broadly, we want to minimize the # of monitors in circulation.
1421 //
1422 // We have added a flag, MonitorInUseLists, which creates a list
1423 // of active monitors for each thread. deflate_idle_monitors()
|