< prev index next >

src/share/vm/runtime/synchronizer.cpp

Print this page




   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 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"

  27 #include "memory/resourceArea.hpp"
  28 #include "oops/markOop.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "runtime/biasedLocking.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "runtime/interfaceSupport.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 #include "runtime/objectMonitor.hpp"
  35 #include "runtime/objectMonitor.inline.hpp"
  36 #include "runtime/osThread.hpp"
  37 #include "runtime/stubRoutines.hpp"
  38 #include "runtime/synchronizer.hpp"
  39 #include "runtime/thread.inline.hpp"
  40 #include "utilities/dtrace.hpp"
  41 #include "utilities/events.hpp"
  42 #include "utilities/preserveException.hpp"
  43 #ifdef TARGET_OS_FAMILY_linux
  44 # include "os_linux.inline.hpp"
  45 #endif
  46 #ifdef TARGET_OS_FAMILY_solaris


1161       guarantee (InUseTail != NULL && InUseList != NULL, "invariant");
1162     }
1163 
1164     Thread::muxAcquire (&ListLock, "omFlush") ;
1165     if (Tail != NULL) {
1166       Tail->FreeNext = gFreeList ;
1167       gFreeList = List ;
1168       MonitorFreeCount += Tally;
1169     }
1170 
1171     if (InUseTail != NULL) {
1172       InUseTail->FreeNext = gOmInUseList;
1173       gOmInUseList = InUseList;
1174       gOmInUseCount += InUseTally;
1175     }
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);


1362           m->Recycle() ;
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




   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 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "jfr/jfrEvents.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/markOop.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/biasedLocking.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/interfaceSupport.hpp"
  34 #include "runtime/mutexLocker.hpp"
  35 #include "runtime/objectMonitor.hpp"
  36 #include "runtime/objectMonitor.inline.hpp"
  37 #include "runtime/osThread.hpp"
  38 #include "runtime/stubRoutines.hpp"
  39 #include "runtime/synchronizer.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "utilities/dtrace.hpp"
  42 #include "utilities/events.hpp"
  43 #include "utilities/preserveException.hpp"
  44 #ifdef TARGET_OS_FAMILY_linux
  45 # include "os_linux.inline.hpp"
  46 #endif
  47 #ifdef TARGET_OS_FAMILY_solaris


1162       guarantee (InUseTail != NULL && InUseList != NULL, "invariant");
1163     }
1164 
1165     Thread::muxAcquire (&ListLock, "omFlush") ;
1166     if (Tail != NULL) {
1167       Tail->FreeNext = gFreeList ;
1168       gFreeList = List ;
1169       MonitorFreeCount += Tally;
1170     }
1171 
1172     if (InUseTail != NULL) {
1173       InUseTail->FreeNext = gOmInUseList;
1174       gOmInUseList = InUseList;
1175       gOmInUseCount += InUseTally;
1176     }
1177 
1178     Thread::muxRelease (&ListLock) ;
1179     TEVENT (omFlush) ;
1180 }
1181 
1182 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1183                                        const oop obj) {
1184   assert(event != NULL, "invariant");
1185   assert(event->should_commit(), "invariant");
1186   event->set_monitorClass(obj->klass());
1187   event->set_address((uintptr_t)(void*)obj);
1188   event->commit();
1189 }
1190 
1191 // Fast path code shared by multiple functions
1192 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) {
1193   markOop mark = obj->mark();
1194   if (mark->has_monitor()) {
1195     assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1196     assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");
1197     return mark->monitor();
1198   }
1199   return ObjectSynchronizer::inflate(Thread::current(), obj);
1200 }
1201 
1202 
1203 // Note that we could encounter some performance loss through false-sharing as
1204 // multiple locks occupy the same $ line.  Padding might be appropriate.
1205 
1206 
1207 ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) {
1208   // Inflate mutates the heap ...
1209   // Relaxing assertion for bug 6320749.
1210   assert (Universe::verify_in_progress() ||
1211           !SafepointSynchronize::is_at_safepoint(), "invariant") ;
1212 
1213   EventJavaMonitorInflate event;
1214 
1215   for (;;) {
1216       const markOop mark = object->mark() ;
1217       assert (!mark->has_bias_pattern(), "invariant") ;
1218 
1219       // The mark can be in one of the following states:
1220       // *  Inflated     - just return
1221       // *  Stack-locked - coerce it to inflated
1222       // *  INFLATING    - busy wait for conversion to complete
1223       // *  Neutral      - aggressively inflate the object.
1224       // *  BIASED       - Illegal.  We should never see this
1225 
1226       // CASE: inflated
1227       if (mark->has_monitor()) {
1228           ObjectMonitor * inf = mark->monitor() ;
1229           assert (inf->header()->is_neutral(), "invariant");
1230           assert (inf->object() == object, "invariant") ;
1231           assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1232           return inf ;
1233       }
1234 


1325           m->set_object(object);
1326           // TODO-FIXME: assert BasicLock->dhw != 0.
1327 
1328           // Must preserve store ordering. The monitor state must
1329           // be stable at the time of publishing the monitor address.
1330           guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ;
1331           object->release_set_mark(markOopDesc::encode(m));
1332 
1333           // Hopefully the performance counters are allocated on distinct cache lines
1334           // to avoid false sharing on MP systems ...
1335           if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1336           TEVENT(Inflate: overwrite stacklock) ;
1337           if (TraceMonitorInflation) {
1338             if (object->is_instance()) {
1339               ResourceMark rm;
1340               tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1341                 (void *) object, (intptr_t) object->mark(),
1342                 object->klass()->external_name());
1343             }
1344           }
1345           if (event.should_commit()) {
1346             post_monitor_inflate_event(&event, object);
1347           }
1348           return m ;
1349       }
1350 
1351       // CASE: neutral
1352       // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1353       // If we know we're inflating for entry it's better to inflate by swinging a
1354       // pre-locked objectMonitor pointer into the object header.   A successful
1355       // CAS inflates the object *and* confers ownership to the inflating thread.
1356       // In the current implementation we use a 2-step mechanism where we CAS()
1357       // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1358       // An inflateTry() method that we could call from fast_enter() and slow_enter()
1359       // would be useful.
1360 
1361       assert (mark->is_neutral(), "invariant");
1362       ObjectMonitor * m = omAlloc (Self) ;
1363       // prepare m for installation - set monitor to initial state
1364       m->Recycle();
1365       m->set_header(mark);
1366       m->set_owner(NULL);
1367       m->set_object(object);


1377           m->Recycle() ;
1378           omRelease (Self, m, true) ;
1379           m = NULL ;
1380           continue ;
1381           // interference - the markword changed - just retry.
1382           // The state-transitions are one-way, so there's no chance of
1383           // live-lock -- "Inflated" is an absorbing state.
1384       }
1385 
1386       // Hopefully the performance counters are allocated on distinct
1387       // cache lines to avoid false sharing on MP systems ...
1388       if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ;
1389       TEVENT(Inflate: overwrite neutral) ;
1390       if (TraceMonitorInflation) {
1391         if (object->is_instance()) {
1392           ResourceMark rm;
1393           tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s",
1394             (void *) object, (intptr_t) object->mark(),
1395             object->klass()->external_name());
1396         }
1397       }
1398       if (event.should_commit()) {
1399         post_monitor_inflate_event(&event, object);
1400       }
1401       return m ;
1402   }
1403 }
1404 
1405 // Note that we could encounter some performance loss through false-sharing as
1406 // multiple locks occupy the same $ line.  Padding might be appropriate.
1407 
1408 
1409 // Deflate_idle_monitors() is called at all safepoints, immediately
1410 // after all mutators are stopped, but before any objects have moved.
1411 // It traverses the list of known monitors, deflating where possible.
1412 // The scavenged monitor are returned to the monitor free list.
1413 //
1414 // Beware that we scavenge at *every* stop-the-world point.
1415 // Having a large number of monitors in-circulation negatively
1416 // impacts the performance of some applications (e.g., PointBase).
1417 // Broadly, we want to minimize the # of monitors in circulation.
1418 //
1419 // We have added a flag, MonitorInUseLists, which creates a list


< prev index next >