< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page
rev 59757 : Add whitebox support for deflating idle monitors including ObjectSynchronizer::request_deflate_idle_monitors(); drop ObjectSynchronizer::_is_special_deflation_requested flag, functions and uses; switch to ObjectSynchronizer::request_deflate_idle_monitors() as needed; _last_async_deflation_time_ns should be set at the end of async deflation;


 104 
 105 #else //  ndef DTRACE_ENABLED
 106 
 107 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 108 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 109 
 110 #endif // ndef DTRACE_ENABLED
 111 
 112 // This exists only as a workaround of dtrace bug 6254741
 113 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
 114   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 115   return 0;
 116 }
 117 
 118 #define NINFLATIONLOCKS 256
 119 static volatile intptr_t gInflationLocks[NINFLATIONLOCKS];
 120 
 121 // global list of blocks of monitors
 122 PaddedObjectMonitor* ObjectSynchronizer::g_block_list = NULL;
 123 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false;
 124 bool volatile ObjectSynchronizer::_is_special_deflation_requested = false;
 125 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0;
 126 
 127 struct ObjectMonitorListGlobals {
 128   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 129   // These are highly shared list related variables.
 130   // To avoid false-sharing they need to be the sole occupants of a cache line.
 131 
 132   // Global ObjectMonitor free list. Newly allocated and deflated
 133   // ObjectMonitors are prepended here.
 134   ObjectMonitor* _free_list;
 135   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(ObjectMonitor*));
 136 
 137   // Global ObjectMonitor in-use list. When a JavaThread is exiting,
 138   // ObjectMonitors on its per-thread in-use list are prepended here.
 139   ObjectMonitor* _in_use_list;
 140   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(ObjectMonitor*));
 141 
 142   // Global ObjectMonitor wait list. Deflated ObjectMonitors wait on
 143   // this list until after a handshake or a safepoint for platforms
 144   // that don't support handshakes. After the handshake or safepoint,


1292     return monitor_usage > MonitorUsedDeflationThreshold;
1293   }
1294   return false;
1295 }
1296 
1297 bool ObjectSynchronizer::is_async_deflation_needed() {
1298   if (!AsyncDeflateIdleMonitors) {
1299     return false;
1300   }
1301   if (is_async_deflation_requested()) {
1302     // Async deflation request.
1303     return true;
1304   }
1305   if (AsyncDeflationInterval > 0 &&
1306       time_since_last_async_deflation_ms() > AsyncDeflationInterval &&
1307       monitors_used_above_threshold()) {
1308     // It's been longer than our specified deflate interval and there
1309     // are too many monitors in use. We don't deflate more frequently
1310     // than AsyncDeflationInterval (unless is_async_deflation_requested)
1311     // in order to not swamp the ServiceThread.
1312     _last_async_deflation_time_ns = os::javaTimeNanos();
1313     return true;
1314   }
1315   return false;
1316 }
1317 
1318 bool ObjectSynchronizer::is_safepoint_deflation_needed() {
1319   if (!AsyncDeflateIdleMonitors) {
1320     if (monitors_used_above_threshold()) {
1321       // Too many monitors in use.
1322       return true;
1323     }
1324     return false;
1325   }
1326   if (is_special_deflation_requested()) {
1327     // For AsyncDeflateIdleMonitors only do a safepoint deflation
1328     // if there is a special deflation request.
1329     return true;
1330   }
1331   return false;
1332 }
1333 








































1334 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() {
1335   return (os::javaTimeNanos() - _last_async_deflation_time_ns) / (NANOUNITS / MILLIUNITS);
1336 }
1337 
1338 void ObjectSynchronizer::oops_do(OopClosure* f) {
1339   // We only scan the global used list here (for moribund threads), and
1340   // the thread-local monitors in Thread::oops_do().
1341   global_used_oops_do(f);
1342 }
1343 
1344 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
1345   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1346   list_oops_do(Atomic::load(&om_list_globals._in_use_list), f);
1347 }
1348 
1349 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
1350   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1351   list_oops_do(thread->om_in_use_list, f);
1352 }
1353 
1354 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, OopClosure* f) {
1355   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");


2000 // number of monitors in-use could negatively impact performance. We also want
2001 // to minimize the total # of monitors in circulation, as they incur a small
2002 // footprint penalty.
2003 //
2004 // Perversely, the heap size -- and thus the STW safepoint rate --
2005 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
2006 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
2007 // This is an unfortunate aspect of this design.
2008 //
2009 // For async deflation:
2010 // If a special deflation request is made, then the safepoint based
2011 // deflation mechanism is used. Otherwise, an async deflation request
2012 // is registered with the ServiceThread and it is notified.
2013 
2014 void ObjectSynchronizer::do_safepoint_work(DeflateMonitorCounters* counters) {
2015   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2016 
2017   // The per-thread in-use lists are handled in
2018   // ParallelSPCleanupThreadClosure::do_thread().
2019 
2020   if (!AsyncDeflateIdleMonitors || is_special_deflation_requested()) {
2021     // Use the older mechanism for the global in-use list or if a
2022     // special deflation has been requested before the safepoint.
2023     ObjectSynchronizer::deflate_idle_monitors(counters);
2024     return;
2025   }
2026 
2027   log_debug(monitorinflation)("requesting async deflation of idle monitors.");
2028   // Request deflation of idle monitors by the ServiceThread:
2029   set_is_async_deflation_requested(true);
2030   MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
2031   ml.notify_all();
2032 
2033   if (log_is_enabled(Debug, monitorinflation)) {
2034     // exit_globals()'s call to audit_and_print_stats() is done
2035     // at the Info level and not at a safepoint.
2036     // For safepoint based deflation, audit_and_print_stats() is called
2037     // in ObjectSynchronizer::finish_deflate_idle_monitors() at the
2038     // Debug level at a safepoint.
2039     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2040   }
2041 }
2042 


2421   }
2422   // We finished the list without a safepoint starting so there's
2423   // no need to save state.
2424   *saved_mid_in_use_p = NULL;
2425   return deflated_count;
2426 }
2427 
2428 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2429   counters->n_in_use = 0;              // currently associated with objects
2430   counters->n_in_circulation = 0;      // extant
2431   counters->n_scavenged = 0;           // reclaimed (global and per-thread)
2432   counters->per_thread_scavenged = 0;  // per-thread scavenge total
2433   counters->per_thread_times = 0.0;    // per-thread scavenge times
2434 }
2435 
2436 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
2437   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2438 
2439   if (AsyncDeflateIdleMonitors) {
2440     // Nothing to do when global idle ObjectMonitors are deflated using
2441     // a JavaThread unless a special deflation has been requested.
2442     if (!is_special_deflation_requested()) {
2443       return;
2444     }
2445   }
2446 
2447   bool deflated = false;
2448 
2449   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
2450   ObjectMonitor* free_tail_p = NULL;
2451   elapsedTimer timer;
2452 
2453   if (log_is_enabled(Info, monitorinflation)) {
2454     timer.start();
2455   }
2456 
2457   // Note: the thread-local monitors lists get deflated in
2458   // a separate pass. See deflate_thread_local_monitors().
2459 
2460   // For moribund threads, scan om_list_globals._in_use_list
2461   int deflated_count = 0;
2462   if (Atomic::load(&om_list_globals._in_use_list) != NULL) {
2463     // Update n_in_circulation before om_list_globals._in_use_count is
2464     // updated by deflation.
2465     Atomic::add(&counters->n_in_circulation,


2517     if (Atomic::load(&jt->om_in_use_count) > 0 && !jt->is_exiting()) {
2518       // This JavaThread is using ObjectMonitors so deflate any that
2519       // are idle unless this JavaThread is exiting; do not race with
2520       // ObjectSynchronizer::om_flush().
2521       deflate_per_thread_idle_monitors_using_JT(jt);
2522       count++;
2523     }
2524   }
2525   if (count > 0) {
2526     log_debug(monitorinflation)("did async deflation of idle monitors for %d thread(s).", count);
2527   }
2528 
2529   log_info(monitorinflation)("async global_population=%d, global_in_use_count=%d, "
2530                              "global_free_count=%d, global_wait_count=%d",
2531                              Atomic::load(&om_list_globals._population),
2532                              Atomic::load(&om_list_globals._in_use_count),
2533                              Atomic::load(&om_list_globals._free_count),
2534                              Atomic::load(&om_list_globals._wait_count));
2535 
2536   // The ServiceThread's async deflation request has been processed.

2537   set_is_async_deflation_requested(false);
2538 
2539   if (Atomic::load(&om_list_globals._wait_count) > 0) {
2540     // There are deflated ObjectMonitors waiting for a handshake
2541     // (or a safepoint) for safety.
2542 
2543     ObjectMonitor* list = Atomic::load(&om_list_globals._wait_list);
2544     ADIM_guarantee(list != NULL, "om_list_globals._wait_list must not be NULL");
2545     int count = Atomic::load(&om_list_globals._wait_count);
2546     Atomic::store(&om_list_globals._wait_count, 0);
2547     Atomic::store(&om_list_globals._wait_list, (ObjectMonitor*)NULL);
2548 
2549     // Find the tail for prepend_list_to_common(). No need to mark
2550     // ObjectMonitors for this list walk since only the deflater
2551     // thread manages the wait list.
2552     int l_count = 0;
2553     ObjectMonitor* tail = NULL;
2554     for (ObjectMonitor* n = list; n != NULL; n = unmarked_next(n)) {
2555       tail = n;
2556       l_count++;


2592 void ObjectSynchronizer::deflate_common_idle_monitors_using_JT(bool is_global, JavaThread* target) {
2593   JavaThread* self = JavaThread::current();
2594 
2595   int deflated_count = 0;
2596   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged ObjectMonitors
2597   ObjectMonitor* free_tail_p = NULL;
2598   ObjectMonitor* saved_mid_in_use_p = NULL;
2599   elapsedTimer timer;
2600 
2601   if (log_is_enabled(Info, monitorinflation)) {
2602     timer.start();
2603   }
2604 
2605   if (is_global) {
2606     OM_PERFDATA_OP(MonExtant, set_value(Atomic::load(&om_list_globals._in_use_count)));
2607   } else {
2608     OM_PERFDATA_OP(MonExtant, inc(Atomic::load(&target->om_in_use_count)));
2609   }
2610 
2611   do {
2612     if (saved_mid_in_use_p != NULL) {
2613       // We looped around because deflate_monitor_list_using_JT()
2614       // detected a pending safepoint. Honoring the safepoint is good,
2615       // but as long as is_special_deflation_requested() is supported,
2616       // we can't safely restart using saved_mid_in_use_p. That saved
2617       // ObjectMonitor could have been deflated by safepoint based
2618       // deflation and would no longer be on the in-use list where we
2619       // originally found it.
2620       saved_mid_in_use_p = NULL;
2621     }
2622     int local_deflated_count;
2623     if (is_global) {
2624       local_deflated_count =
2625           deflate_monitor_list_using_JT(&om_list_globals._in_use_list,
2626                                         &om_list_globals._in_use_count,
2627                                         &free_head_p, &free_tail_p,
2628                                         &saved_mid_in_use_p);
2629     } else {
2630       local_deflated_count =
2631           deflate_monitor_list_using_JT(&target->om_in_use_list,
2632                                         &target->om_in_use_count, &free_head_p,
2633                                         &free_tail_p, &saved_mid_in_use_p);
2634     }
2635     deflated_count += local_deflated_count;
2636 
2637     if (free_head_p != NULL) {
2638       // Move the deflated ObjectMonitors to the global free list.
2639       guarantee(free_tail_p != NULL && local_deflated_count > 0, "free_tail_p=" INTPTR_FORMAT ", local_deflated_count=%d", p2i(free_tail_p), local_deflated_count);
2640       // Note: The target thread can be doing an om_alloc() that
2641       // is trying to prepend an ObjectMonitor on its in-use list


2684     ls = &lsh_debug;
2685   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2686     ls = &lsh_info;
2687   }
2688   if (ls != NULL) {
2689     if (is_global) {
2690       ls->print_cr("async-deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2691     } else {
2692       ls->print_cr("jt=" INTPTR_FORMAT ": async-deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(target), timer.seconds(), deflated_count);
2693     }
2694   }
2695 }
2696 
2697 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2698   // Report the cumulative time for deflating each thread's idle
2699   // monitors. Note: if the work is split among more than one
2700   // worker thread, then the reported time will likely be more
2701   // than a beginning to end measurement of the phase.
2702   log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->per_thread_times, counters->per_thread_scavenged);
2703 
2704   bool needs_special_deflation = is_special_deflation_requested();
2705   if (AsyncDeflateIdleMonitors && !needs_special_deflation) {
2706     // Nothing to do when idle ObjectMonitors are deflated using
2707     // a JavaThread unless a special deflation has been requested.
2708     return;
2709   }
2710 
2711   if (log_is_enabled(Debug, monitorinflation)) {
2712     // exit_globals()'s call to audit_and_print_stats() is done
2713     // at the Info level and not at a safepoint.
2714     // For async deflation, audit_and_print_stats() is called in
2715     // ObjectSynchronizer::do_safepoint_work() at the Debug level
2716     // at a safepoint.
2717     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2718   } else if (log_is_enabled(Info, monitorinflation)) {
2719     log_info(monitorinflation)("global_population=%d, global_in_use_count=%d, "
2720                                "global_free_count=%d, global_wait_count=%d",
2721                                Atomic::load(&om_list_globals._population),
2722                                Atomic::load(&om_list_globals._in_use_count),
2723                                Atomic::load(&om_list_globals._free_count),
2724                                Atomic::load(&om_list_globals._wait_count));
2725   }
2726 
2727   OM_PERFDATA_OP(Deflations, inc(counters->n_scavenged));
2728   OM_PERFDATA_OP(MonExtant, set_value(counters->n_in_circulation));
2729 
2730   GVars.stw_random = os::random();
2731   GVars.stw_cycle++;
2732 
2733   if (needs_special_deflation) {
2734     set_is_special_deflation_requested(false);  // special deflation is done
2735   }
2736 }
2737 
2738 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
2739   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2740 
2741   if (AsyncDeflateIdleMonitors && !is_special_deflation_requested()) {
2742     // Nothing to do if a special deflation has NOT been requested.

2743     return;
2744   }
2745 
2746   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
2747   ObjectMonitor* free_tail_p = NULL;
2748   elapsedTimer timer;
2749 
2750   if (log_is_enabled(Info, safepoint, cleanup) ||
2751       log_is_enabled(Info, monitorinflation)) {
2752     timer.start();
2753   }
2754 
2755   // Update n_in_circulation before om_in_use_count is updated by deflation.
2756   Atomic::add(&counters->n_in_circulation, Atomic::load(&thread->om_in_use_count));
2757 
2758   int deflated_count = deflate_monitor_list(&thread->om_in_use_list, &thread->om_in_use_count, &free_head_p, &free_tail_p);
2759   Atomic::add(&counters->n_in_use, Atomic::load(&thread->om_in_use_count));
2760 
2761   if (free_head_p != NULL) {
2762     // Move the deflated ObjectMonitors back to the global free list.




 104 
 105 #else //  ndef DTRACE_ENABLED
 106 
 107 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 108 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 109 
 110 #endif // ndef DTRACE_ENABLED
 111 
 112 // This exists only as a workaround of dtrace bug 6254741
 113 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
 114   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 115   return 0;
 116 }
 117 
 118 #define NINFLATIONLOCKS 256
 119 static volatile intptr_t gInflationLocks[NINFLATIONLOCKS];
 120 
 121 // global list of blocks of monitors
 122 PaddedObjectMonitor* ObjectSynchronizer::g_block_list = NULL;
 123 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false;

 124 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0;
 125 
 126 struct ObjectMonitorListGlobals {
 127   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 128   // These are highly shared list related variables.
 129   // To avoid false-sharing they need to be the sole occupants of a cache line.
 130 
 131   // Global ObjectMonitor free list. Newly allocated and deflated
 132   // ObjectMonitors are prepended here.
 133   ObjectMonitor* _free_list;
 134   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(ObjectMonitor*));
 135 
 136   // Global ObjectMonitor in-use list. When a JavaThread is exiting,
 137   // ObjectMonitors on its per-thread in-use list are prepended here.
 138   ObjectMonitor* _in_use_list;
 139   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(ObjectMonitor*));
 140 
 141   // Global ObjectMonitor wait list. Deflated ObjectMonitors wait on
 142   // this list until after a handshake or a safepoint for platforms
 143   // that don't support handshakes. After the handshake or safepoint,


1291     return monitor_usage > MonitorUsedDeflationThreshold;
1292   }
1293   return false;
1294 }
1295 
1296 bool ObjectSynchronizer::is_async_deflation_needed() {
1297   if (!AsyncDeflateIdleMonitors) {
1298     return false;
1299   }
1300   if (is_async_deflation_requested()) {
1301     // Async deflation request.
1302     return true;
1303   }
1304   if (AsyncDeflationInterval > 0 &&
1305       time_since_last_async_deflation_ms() > AsyncDeflationInterval &&
1306       monitors_used_above_threshold()) {
1307     // It's been longer than our specified deflate interval and there
1308     // are too many monitors in use. We don't deflate more frequently
1309     // than AsyncDeflationInterval (unless is_async_deflation_requested)
1310     // in order to not swamp the ServiceThread.

1311     return true;
1312   }
1313   return false;
1314 }
1315 
1316 bool ObjectSynchronizer::is_safepoint_deflation_needed() {
1317   if (!AsyncDeflateIdleMonitors) {
1318     if (monitors_used_above_threshold()) {
1319       // Too many monitors in use.
1320       return true;
1321     }
1322     return false;
1323   }





1324   return false;
1325 }
1326 
1327 bool ObjectSynchronizer::request_deflate_idle_monitors() {
1328   bool is_JavaThread = Thread::current()->is_Java_thread();
1329   bool ret_code = false;
1330 
1331   if (AsyncDeflateIdleMonitors) {
1332     jlong last_time = last_async_deflation_time_ns();
1333     set_is_async_deflation_requested(true);
1334     {
1335       MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
1336       ml.notify_all();
1337     }
1338     const int N_CHECKS = 5;
1339     for (int i = 0; i < N_CHECKS; i++) {  // sleep for at most 5 seconds
1340       if (last_async_deflation_time_ns() > last_time) {
1341         log_info(monitorinflation)("Async Deflation happened after %d check(s).", i);
1342         ret_code = true;
1343         break;
1344       }
1345       if (is_JavaThread) {
1346         // JavaThread has to honor the blocking protocol.
1347         ThreadBlockInVM tbivm(JavaThread::current());
1348         os::naked_short_sleep(999);  // sleep for almost 1 second
1349       } else {
1350         os::naked_short_sleep(999);  // sleep for almost 1 second
1351       }
1352     }
1353     if (ret_code == false) {
1354       log_info(monitorinflation)("Async Deflation DID NOT happen after %d checks.", N_CHECKS);
1355     }
1356   } else if (!Thread::current()->is_VM_thread()) {
1357     // The VMThread only calls this at shutdown time before the final
1358     // safepoint so it should not need to force this safepoint.
1359     VM_ForceSafepoint force_safepoint_op;
1360     VMThread::execute(&force_safepoint_op);
1361     ret_code = true;
1362   }
1363 
1364   return ret_code;
1365 }
1366 
1367 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() {
1368   return (os::javaTimeNanos() - last_async_deflation_time_ns()) / (NANOUNITS / MILLIUNITS);
1369 }
1370 
1371 void ObjectSynchronizer::oops_do(OopClosure* f) {
1372   // We only scan the global used list here (for moribund threads), and
1373   // the thread-local monitors in Thread::oops_do().
1374   global_used_oops_do(f);
1375 }
1376 
1377 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
1378   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1379   list_oops_do(Atomic::load(&om_list_globals._in_use_list), f);
1380 }
1381 
1382 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
1383   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1384   list_oops_do(thread->om_in_use_list, f);
1385 }
1386 
1387 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, OopClosure* f) {
1388   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");


2033 // number of monitors in-use could negatively impact performance. We also want
2034 // to minimize the total # of monitors in circulation, as they incur a small
2035 // footprint penalty.
2036 //
2037 // Perversely, the heap size -- and thus the STW safepoint rate --
2038 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
2039 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
2040 // This is an unfortunate aspect of this design.
2041 //
2042 // For async deflation:
2043 // If a special deflation request is made, then the safepoint based
2044 // deflation mechanism is used. Otherwise, an async deflation request
2045 // is registered with the ServiceThread and it is notified.
2046 
2047 void ObjectSynchronizer::do_safepoint_work(DeflateMonitorCounters* counters) {
2048   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2049 
2050   // The per-thread in-use lists are handled in
2051   // ParallelSPCleanupThreadClosure::do_thread().
2052 
2053   if (!AsyncDeflateIdleMonitors) {
2054     // Use the older mechanism for the global in-use list.

2055     ObjectSynchronizer::deflate_idle_monitors(counters);
2056     return;
2057   }
2058 
2059   log_debug(monitorinflation)("requesting async deflation of idle monitors.");
2060   // Request deflation of idle monitors by the ServiceThread:
2061   set_is_async_deflation_requested(true);
2062   MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
2063   ml.notify_all();
2064 
2065   if (log_is_enabled(Debug, monitorinflation)) {
2066     // exit_globals()'s call to audit_and_print_stats() is done
2067     // at the Info level and not at a safepoint.
2068     // For safepoint based deflation, audit_and_print_stats() is called
2069     // in ObjectSynchronizer::finish_deflate_idle_monitors() at the
2070     // Debug level at a safepoint.
2071     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2072   }
2073 }
2074 


2453   }
2454   // We finished the list without a safepoint starting so there's
2455   // no need to save state.
2456   *saved_mid_in_use_p = NULL;
2457   return deflated_count;
2458 }
2459 
2460 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2461   counters->n_in_use = 0;              // currently associated with objects
2462   counters->n_in_circulation = 0;      // extant
2463   counters->n_scavenged = 0;           // reclaimed (global and per-thread)
2464   counters->per_thread_scavenged = 0;  // per-thread scavenge total
2465   counters->per_thread_times = 0.0;    // per-thread scavenge times
2466 }
2467 
2468 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
2469   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2470 
2471   if (AsyncDeflateIdleMonitors) {
2472     // Nothing to do when global idle ObjectMonitors are deflated using
2473     // a JavaThread.

2474     return;
2475   }

2476 
2477   bool deflated = false;
2478 
2479   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
2480   ObjectMonitor* free_tail_p = NULL;
2481   elapsedTimer timer;
2482 
2483   if (log_is_enabled(Info, monitorinflation)) {
2484     timer.start();
2485   }
2486 
2487   // Note: the thread-local monitors lists get deflated in
2488   // a separate pass. See deflate_thread_local_monitors().
2489 
2490   // For moribund threads, scan om_list_globals._in_use_list
2491   int deflated_count = 0;
2492   if (Atomic::load(&om_list_globals._in_use_list) != NULL) {
2493     // Update n_in_circulation before om_list_globals._in_use_count is
2494     // updated by deflation.
2495     Atomic::add(&counters->n_in_circulation,


2547     if (Atomic::load(&jt->om_in_use_count) > 0 && !jt->is_exiting()) {
2548       // This JavaThread is using ObjectMonitors so deflate any that
2549       // are idle unless this JavaThread is exiting; do not race with
2550       // ObjectSynchronizer::om_flush().
2551       deflate_per_thread_idle_monitors_using_JT(jt);
2552       count++;
2553     }
2554   }
2555   if (count > 0) {
2556     log_debug(monitorinflation)("did async deflation of idle monitors for %d thread(s).", count);
2557   }
2558 
2559   log_info(monitorinflation)("async global_population=%d, global_in_use_count=%d, "
2560                              "global_free_count=%d, global_wait_count=%d",
2561                              Atomic::load(&om_list_globals._population),
2562                              Atomic::load(&om_list_globals._in_use_count),
2563                              Atomic::load(&om_list_globals._free_count),
2564                              Atomic::load(&om_list_globals._wait_count));
2565 
2566   // The ServiceThread's async deflation request has been processed.
2567   _last_async_deflation_time_ns = os::javaTimeNanos();
2568   set_is_async_deflation_requested(false);
2569 
2570   if (Atomic::load(&om_list_globals._wait_count) > 0) {
2571     // There are deflated ObjectMonitors waiting for a handshake
2572     // (or a safepoint) for safety.
2573 
2574     ObjectMonitor* list = Atomic::load(&om_list_globals._wait_list);
2575     ADIM_guarantee(list != NULL, "om_list_globals._wait_list must not be NULL");
2576     int count = Atomic::load(&om_list_globals._wait_count);
2577     Atomic::store(&om_list_globals._wait_count, 0);
2578     Atomic::store(&om_list_globals._wait_list, (ObjectMonitor*)NULL);
2579 
2580     // Find the tail for prepend_list_to_common(). No need to mark
2581     // ObjectMonitors for this list walk since only the deflater
2582     // thread manages the wait list.
2583     int l_count = 0;
2584     ObjectMonitor* tail = NULL;
2585     for (ObjectMonitor* n = list; n != NULL; n = unmarked_next(n)) {
2586       tail = n;
2587       l_count++;


2623 void ObjectSynchronizer::deflate_common_idle_monitors_using_JT(bool is_global, JavaThread* target) {
2624   JavaThread* self = JavaThread::current();
2625 
2626   int deflated_count = 0;
2627   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged ObjectMonitors
2628   ObjectMonitor* free_tail_p = NULL;
2629   ObjectMonitor* saved_mid_in_use_p = NULL;
2630   elapsedTimer timer;
2631 
2632   if (log_is_enabled(Info, monitorinflation)) {
2633     timer.start();
2634   }
2635 
2636   if (is_global) {
2637     OM_PERFDATA_OP(MonExtant, set_value(Atomic::load(&om_list_globals._in_use_count)));
2638   } else {
2639     OM_PERFDATA_OP(MonExtant, inc(Atomic::load(&target->om_in_use_count)));
2640   }
2641 
2642   do {










2643     int local_deflated_count;
2644     if (is_global) {
2645       local_deflated_count =
2646           deflate_monitor_list_using_JT(&om_list_globals._in_use_list,
2647                                         &om_list_globals._in_use_count,
2648                                         &free_head_p, &free_tail_p,
2649                                         &saved_mid_in_use_p);
2650     } else {
2651       local_deflated_count =
2652           deflate_monitor_list_using_JT(&target->om_in_use_list,
2653                                         &target->om_in_use_count, &free_head_p,
2654                                         &free_tail_p, &saved_mid_in_use_p);
2655     }
2656     deflated_count += local_deflated_count;
2657 
2658     if (free_head_p != NULL) {
2659       // Move the deflated ObjectMonitors to the global free list.
2660       guarantee(free_tail_p != NULL && local_deflated_count > 0, "free_tail_p=" INTPTR_FORMAT ", local_deflated_count=%d", p2i(free_tail_p), local_deflated_count);
2661       // Note: The target thread can be doing an om_alloc() that
2662       // is trying to prepend an ObjectMonitor on its in-use list


2705     ls = &lsh_debug;
2706   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2707     ls = &lsh_info;
2708   }
2709   if (ls != NULL) {
2710     if (is_global) {
2711       ls->print_cr("async-deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2712     } else {
2713       ls->print_cr("jt=" INTPTR_FORMAT ": async-deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(target), timer.seconds(), deflated_count);
2714     }
2715   }
2716 }
2717 
2718 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2719   // Report the cumulative time for deflating each thread's idle
2720   // monitors. Note: if the work is split among more than one
2721   // worker thread, then the reported time will likely be more
2722   // than a beginning to end measurement of the phase.
2723   log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->per_thread_times, counters->per_thread_scavenged);
2724 
2725   if (AsyncDeflateIdleMonitors) {

2726     // Nothing to do when idle ObjectMonitors are deflated using
2727     // a JavaThread.
2728     return;
2729   }
2730 
2731   if (log_is_enabled(Debug, monitorinflation)) {
2732     // exit_globals()'s call to audit_and_print_stats() is done
2733     // at the Info level and not at a safepoint.
2734     // For async deflation, audit_and_print_stats() is called in
2735     // ObjectSynchronizer::do_safepoint_work() at the Debug level
2736     // at a safepoint.
2737     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2738   } else if (log_is_enabled(Info, monitorinflation)) {
2739     log_info(monitorinflation)("global_population=%d, global_in_use_count=%d, "
2740                                "global_free_count=%d, global_wait_count=%d",
2741                                Atomic::load(&om_list_globals._population),
2742                                Atomic::load(&om_list_globals._in_use_count),
2743                                Atomic::load(&om_list_globals._free_count),
2744                                Atomic::load(&om_list_globals._wait_count));
2745   }
2746 
2747   OM_PERFDATA_OP(Deflations, inc(counters->n_scavenged));
2748   OM_PERFDATA_OP(MonExtant, set_value(counters->n_in_circulation));
2749 
2750   GVars.stw_random = os::random();
2751   GVars.stw_cycle++;




2752 }
2753 
2754 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
2755   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2756 
2757   if (AsyncDeflateIdleMonitors) {
2758     // Nothing to do when per-thread idle ObjectMonitors are deflated
2759     // using a JavaThread.
2760     return;
2761   }
2762 
2763   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
2764   ObjectMonitor* free_tail_p = NULL;
2765   elapsedTimer timer;
2766 
2767   if (log_is_enabled(Info, safepoint, cleanup) ||
2768       log_is_enabled(Info, monitorinflation)) {
2769     timer.start();
2770   }
2771 
2772   // Update n_in_circulation before om_in_use_count is updated by deflation.
2773   Atomic::add(&counters->n_in_circulation, Atomic::load(&thread->om_in_use_count));
2774 
2775   int deflated_count = deflate_monitor_list(&thread->om_in_use_list, &thread->om_in_use_count, &free_head_p, &free_tail_p);
2776   Atomic::add(&counters->n_in_use, Atomic::load(&thread->om_in_use_count));
2777 
2778   if (free_head_p != NULL) {
2779     // Move the deflated ObjectMonitors back to the global free list.


< prev index next >