< prev index next >

src/hotspot/share/prims/jvmtiEnvBase.cpp

Print this page

1366 jvmtiError
1367 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1368   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1369   Thread* current_thread = VMThread::vm_thread();
1370   assert(current_thread == Thread::current(), "must be");
1371 
1372   HandleMark hm(current_thread);
1373   Handle hobj;
1374 
1375   // Check arguments
1376   {
1377     oop mirror = JNIHandles::resolve_external_guard(object);
1378     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1379     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1380 
1381     hobj = Handle(current_thread, mirror);
1382   }
1383 
1384   ThreadsListHandle tlh(current_thread);
1385   JavaThread *owning_thread = NULL;
1386   ObjectMonitor *mon = NULL;
1387   jvmtiMonitorUsage ret = {
1388       NULL, 0, 0, NULL, 0, NULL
1389   };
1390 
1391   uint32_t debug_bits = 0;
1392   // first derive the object's owner and entry_count (if any)
1393   {
1394     address owner = NULL;
1395     {
1396       markWord mark = hobj()->mark();
1397 
1398       if (!mark.has_monitor()) {
1399         // this object has a lightweight monitor
1400 
1401         if (mark.has_locker()) {
1402           owner = (address)mark.locker(); // save the address of the Lock word
1403         }
1404         // implied else: no owner
1405       } else {
1406         // this object has a heavyweight monitor
1407         mon = mark.monitor();
1408 
1409         // The owner field of a heavyweight monitor may be NULL for no
1410         // owner, a JavaThread * or it may still be the address of the
1411         // Lock word in a JavaThread's stack. A monitor can be inflated
1412         // by a non-owning JavaThread, but only the owning JavaThread
1413         // can change the owner field from the Lock word to the
1414         // JavaThread * and it may not have done that yet.
1415         owner = (address)mon->owner();
1416       }
1417     }
1418 
1419     if (owner != NULL) {
1420       // This monitor is owned so we have to find the owning JavaThread.
1421       owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner);
1422       assert(owning_thread != NULL, "owning JavaThread must not be NULL");
1423       Handle th(current_thread, get_vthread_or_thread_oop(owning_thread));
1424       ret.owner = (jthread)jni_reference(calling_thread, th);
1425     }
1426 
1427     if (owning_thread != NULL) {  // monitor is owned
1428       // The recursions field of a monitor does not reflect recursions
1429       // as lightweight locks before inflating the monitor are not included.
1430       // We have to count the number of recursive monitor entries the hard way.
1431       // We pass a handle to survive any GCs along the way.
1432       ret.entry_count = count_locked_objects(owning_thread, hobj);
1433     }
1434     // implied else: entry_count == 0
1435   }
1436 
1437   jint nWant = 0, nWait = 0;
1438   if (mon != NULL) {
1439     // this object has a heavyweight monitor
1440     nWant = mon->contentions(); // # of threads contending for monitor
1441     nWait = mon->waiters();     // # of threads in Object.wait()
1442     ret.waiter_count = nWant + nWait;
1443     ret.notify_waiter_count = nWait;
1444   } else {
1445     // this object has a lightweight monitor
1446     ret.waiter_count = 0;
1447     ret.notify_waiter_count = 0;
1448   }
1449 
1450   // Allocate memory for heavyweight and lightweight monitor.
1451   jvmtiError err;
1452   err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
1453   if (err != JVMTI_ERROR_NONE) {
1454     return err;
1455   }

1366 jvmtiError
1367 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1368   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1369   Thread* current_thread = VMThread::vm_thread();
1370   assert(current_thread == Thread::current(), "must be");
1371 
1372   HandleMark hm(current_thread);
1373   Handle hobj;
1374 
1375   // Check arguments
1376   {
1377     oop mirror = JNIHandles::resolve_external_guard(object);
1378     NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1379     NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1380 
1381     hobj = Handle(current_thread, mirror);
1382   }
1383 
1384   ThreadsListHandle tlh(current_thread);
1385   JavaThread *owning_thread = NULL;
1386   ObjectMonitor* mon = NULL;
1387   jvmtiMonitorUsage ret = {
1388       NULL, 0, 0, NULL, 0, NULL
1389   };
1390 
1391   uint32_t debug_bits = 0;
1392   // first derive the object's owner and entry_count (if any)
1393   owning_thread = Threads::owning_thread_from_object(tlh.list(), hobj(), &mon);
1394   if (owning_thread != NULL) {
1395     Handle th(current_thread, get_vthread_or_thread_oop(owning_thread));
1396     ret.owner = (jthread)jni_reference(calling_thread, th);
1397     // The recursions field of a monitor does not reflect recursions
1398     // as lightweight locks before inflating the monitor are not included.
1399     // We have to count the number of recursive monitor entries the hard way.
1400     // We pass a handle to survive any GCs along the way.
1401     ret.entry_count = count_locked_objects(owning_thread, hobj);
1402   }
1403   // implied else: entry_count == 0
































1404 
1405   jint nWant = 0, nWait = 0;
1406   if (mon != NULL) {
1407     // this object has a heavyweight monitor
1408     nWant = mon->contentions(); // # of threads contending for monitor
1409     nWait = mon->waiters();     // # of threads in Object.wait()
1410     ret.waiter_count = nWant + nWait;
1411     ret.notify_waiter_count = nWait;
1412   } else {
1413     // this object has a lightweight monitor
1414     ret.waiter_count = 0;
1415     ret.notify_waiter_count = 0;
1416   }
1417 
1418   // Allocate memory for heavyweight and lightweight monitor.
1419   jvmtiError err;
1420   err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
1421   if (err != JVMTI_ERROR_NONE) {
1422     return err;
1423   }
< prev index next >