gcTaskManager.cpp revision 9077:17cfe2c6dc00
1/*
2 * Copyright (c) 2002, 2015, 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 *
23 */
24
25#include "precompiled.hpp"
26#include "gc/parallel/gcTaskManager.hpp"
27#include "gc/parallel/gcTaskThread.hpp"
28#include "gc/shared/adaptiveSizePolicy.hpp"
29#include "gc/shared/gcId.hpp"
30#include "memory/allocation.hpp"
31#include "memory/allocation.inline.hpp"
32#include "runtime/mutex.hpp"
33#include "runtime/mutexLocker.hpp"
34#include "runtime/orderAccess.inline.hpp"
35
36//
37// GCTask
38//
39
40const char* GCTask::Kind::to_string(kind value) {
41  const char* result = "unknown GCTask kind";
42  switch (value) {
43  default:
44    result = "unknown GCTask kind";
45    break;
46  case unknown_task:
47    result = "unknown task";
48    break;
49  case ordinary_task:
50    result = "ordinary task";
51    break;
52  case wait_for_barrier_task:
53    result = "wait for barrier task";
54    break;
55  case noop_task:
56    result = "noop task";
57    break;
58  case idle_task:
59    result = "idle task";
60    break;
61  }
62  return result;
63};
64
65GCTask::GCTask() {
66  initialize(Kind::ordinary_task, GCId::current());
67}
68
69GCTask::GCTask(Kind::kind kind) {
70  initialize(kind, GCId::current());
71}
72
73GCTask::GCTask(Kind::kind kind, uint gc_id) {
74  initialize(kind, gc_id);
75}
76
77void GCTask::initialize(Kind::kind kind, uint gc_id) {
78  _kind = kind;
79  _affinity = GCTaskManager::sentinel_worker();
80  _older = NULL;
81  _newer = NULL;
82  _gc_id = gc_id;
83}
84
85void GCTask::destruct() {
86  assert(older() == NULL, "shouldn't have an older task");
87  assert(newer() == NULL, "shouldn't have a newer task");
88  // Nothing to do.
89}
90
91NOT_PRODUCT(
92void GCTask::print(const char* message) const {
93  tty->print(INTPTR_FORMAT " <- " INTPTR_FORMAT "(%u) -> " INTPTR_FORMAT,
94             p2i(newer()), p2i(this), affinity(), p2i(older()));
95}
96)
97
98//
99// GCTaskQueue
100//
101
102GCTaskQueue* GCTaskQueue::create() {
103  GCTaskQueue* result = new GCTaskQueue(false);
104  if (TraceGCTaskQueue) {
105    tty->print_cr("GCTaskQueue::create()"
106                  " returns " INTPTR_FORMAT, p2i(result));
107  }
108  return result;
109}
110
111GCTaskQueue* GCTaskQueue::create_on_c_heap() {
112  GCTaskQueue* result = new(ResourceObj::C_HEAP, mtGC) GCTaskQueue(true);
113  if (TraceGCTaskQueue) {
114    tty->print_cr("GCTaskQueue::create_on_c_heap()"
115                  " returns " INTPTR_FORMAT,
116                  p2i(result));
117  }
118  return result;
119}
120
121GCTaskQueue::GCTaskQueue(bool on_c_heap) :
122  _is_c_heap_obj(on_c_heap) {
123  initialize();
124  if (TraceGCTaskQueue) {
125    tty->print_cr("[" INTPTR_FORMAT "]"
126                  " GCTaskQueue::GCTaskQueue() constructor",
127                  p2i(this));
128  }
129}
130
131void GCTaskQueue::destruct() {
132  // Nothing to do.
133}
134
135void GCTaskQueue::destroy(GCTaskQueue* that) {
136  if (TraceGCTaskQueue) {
137    tty->print_cr("[" INTPTR_FORMAT "]"
138                  " GCTaskQueue::destroy()"
139                  "  is_c_heap_obj:  %s",
140                  p2i(that),
141                  that->is_c_heap_obj() ? "true" : "false");
142  }
143  // That instance may have been allocated as a CHeapObj,
144  // in which case we have to free it explicitly.
145  if (that != NULL) {
146    that->destruct();
147    assert(that->is_empty(), "should be empty");
148    if (that->is_c_heap_obj()) {
149      FreeHeap(that);
150    }
151  }
152}
153
154void GCTaskQueue::initialize() {
155  set_insert_end(NULL);
156  set_remove_end(NULL);
157  set_length(0);
158}
159
160// Enqueue one task.
161void GCTaskQueue::enqueue(GCTask* task) {
162  if (TraceGCTaskQueue) {
163    tty->print_cr("[" INTPTR_FORMAT "]"
164                  " GCTaskQueue::enqueue(task: "
165                  INTPTR_FORMAT ")",
166                  p2i(this), p2i(task));
167    print("before:");
168  }
169  assert(task != NULL, "shouldn't have null task");
170  assert(task->older() == NULL, "shouldn't be on queue");
171  assert(task->newer() == NULL, "shouldn't be on queue");
172  task->set_newer(NULL);
173  task->set_older(insert_end());
174  if (is_empty()) {
175    set_remove_end(task);
176  } else {
177    insert_end()->set_newer(task);
178  }
179  set_insert_end(task);
180  increment_length();
181  verify_length();
182  if (TraceGCTaskQueue) {
183    print("after:");
184  }
185}
186
187// Enqueue a whole list of tasks.  Empties the argument list.
188void GCTaskQueue::enqueue(GCTaskQueue* list) {
189  if (TraceGCTaskQueue) {
190    tty->print_cr("[" INTPTR_FORMAT "]"
191                  " GCTaskQueue::enqueue(list: "
192                  INTPTR_FORMAT ")",
193                  p2i(this), p2i(list));
194    print("before:");
195    list->print("list:");
196  }
197  if (list->is_empty()) {
198    // Enqueueing the empty list: nothing to do.
199    return;
200  }
201  uint list_length = list->length();
202  if (is_empty()) {
203    // Enqueueing to empty list: just acquire elements.
204    set_insert_end(list->insert_end());
205    set_remove_end(list->remove_end());
206    set_length(list_length);
207  } else {
208    // Prepend argument list to our queue.
209    list->remove_end()->set_older(insert_end());
210    insert_end()->set_newer(list->remove_end());
211    set_insert_end(list->insert_end());
212    set_length(length() + list_length);
213    // empty the argument list.
214  }
215  list->initialize();
216  if (TraceGCTaskQueue) {
217    print("after:");
218    list->print("list:");
219  }
220  verify_length();
221}
222
223// Dequeue one task.
224GCTask* GCTaskQueue::dequeue() {
225  if (TraceGCTaskQueue) {
226    tty->print_cr("[" INTPTR_FORMAT "]"
227                  " GCTaskQueue::dequeue()", p2i(this));
228    print("before:");
229  }
230  assert(!is_empty(), "shouldn't dequeue from empty list");
231  GCTask* result = remove();
232  assert(result != NULL, "shouldn't have NULL task");
233  if (TraceGCTaskQueue) {
234    tty->print_cr("    return: " INTPTR_FORMAT, p2i(result));
235    print("after:");
236  }
237  return result;
238}
239
240// Dequeue one task, preferring one with affinity.
241GCTask* GCTaskQueue::dequeue(uint affinity) {
242  if (TraceGCTaskQueue) {
243    tty->print_cr("[" INTPTR_FORMAT "]"
244                  " GCTaskQueue::dequeue(%u)", p2i(this), affinity);
245    print("before:");
246  }
247  assert(!is_empty(), "shouldn't dequeue from empty list");
248  // Look down to the next barrier for a task with this affinity.
249  GCTask* result = NULL;
250  for (GCTask* element = remove_end();
251       element != NULL;
252       element = element->newer()) {
253    if (element->is_barrier_task()) {
254      // Don't consider barrier tasks, nor past them.
255      result = NULL;
256      break;
257    }
258    if (element->affinity() == affinity) {
259      result = remove(element);
260      break;
261    }
262  }
263  // If we didn't find anything with affinity, just take the next task.
264  if (result == NULL) {
265    result = remove();
266  }
267  if (TraceGCTaskQueue) {
268    tty->print_cr("    return: " INTPTR_FORMAT, p2i(result));
269    print("after:");
270  }
271  return result;
272}
273
274GCTask* GCTaskQueue::remove() {
275  // Dequeue from remove end.
276  GCTask* result = remove_end();
277  assert(result != NULL, "shouldn't have null task");
278  assert(result->older() == NULL, "not the remove_end");
279  set_remove_end(result->newer());
280  if (remove_end() == NULL) {
281    assert(insert_end() == result, "not a singleton");
282    set_insert_end(NULL);
283  } else {
284    remove_end()->set_older(NULL);
285  }
286  result->set_newer(NULL);
287  decrement_length();
288  assert(result->newer() == NULL, "shouldn't be on queue");
289  assert(result->older() == NULL, "shouldn't be on queue");
290  verify_length();
291  return result;
292}
293
294GCTask* GCTaskQueue::remove(GCTask* task) {
295  // This is slightly more work, and has slightly fewer asserts
296  // than removing from the remove end.
297  assert(task != NULL, "shouldn't have null task");
298  GCTask* result = task;
299  if (result->newer() != NULL) {
300    result->newer()->set_older(result->older());
301  } else {
302    assert(insert_end() == result, "not youngest");
303    set_insert_end(result->older());
304  }
305  if (result->older() != NULL) {
306    result->older()->set_newer(result->newer());
307  } else {
308    assert(remove_end() == result, "not oldest");
309    set_remove_end(result->newer());
310  }
311  result->set_newer(NULL);
312  result->set_older(NULL);
313  decrement_length();
314  verify_length();
315  return result;
316}
317
318NOT_PRODUCT(
319// Count the elements in the queue and verify the length against
320// that count.
321void GCTaskQueue::verify_length() const {
322  uint count = 0;
323  for (GCTask* element = insert_end();
324       element != NULL;
325       element = element->older()) {
326
327    count++;
328  }
329  assert(count == length(), "Length does not match queue");
330}
331
332void GCTaskQueue::print(const char* message) const {
333  tty->print_cr("[" INTPTR_FORMAT "] GCTaskQueue:"
334                "  insert_end: " INTPTR_FORMAT
335                "  remove_end: " INTPTR_FORMAT
336                "  length:       %d"
337                "  %s",
338                p2i(this), p2i(insert_end()), p2i(remove_end()), length(), message);
339  uint count = 0;
340  for (GCTask* element = insert_end();
341       element != NULL;
342       element = element->older()) {
343    element->print("    ");
344    count++;
345    tty->cr();
346  }
347  tty->print("Total tasks: %d", count);
348}
349)
350
351//
352// SynchronizedGCTaskQueue
353//
354
355SynchronizedGCTaskQueue::SynchronizedGCTaskQueue(GCTaskQueue* queue_arg,
356                                                 Monitor *       lock_arg) :
357  _unsynchronized_queue(queue_arg),
358  _lock(lock_arg) {
359  assert(unsynchronized_queue() != NULL, "null queue");
360  assert(lock() != NULL, "null lock");
361}
362
363SynchronizedGCTaskQueue::~SynchronizedGCTaskQueue() {
364  // Nothing to do.
365}
366
367//
368// GCTaskManager
369//
370GCTaskManager::GCTaskManager(uint workers) :
371  _workers(workers),
372  _active_workers(0),
373  _idle_workers(0) {
374  initialize();
375}
376
377void GCTaskManager::initialize() {
378  if (TraceGCTaskManager) {
379    tty->print_cr("GCTaskManager::initialize: workers: %u", workers());
380  }
381  assert(workers() != 0, "no workers");
382  _monitor = new Monitor(Mutex::barrier,                // rank
383                         "GCTaskManager monitor",       // name
384                         Mutex::_allow_vm_block_flag,   // allow_vm_block
385                         Monitor::_safepoint_check_never);
386  // The queue for the GCTaskManager must be a CHeapObj.
387  GCTaskQueue* unsynchronized_queue = GCTaskQueue::create_on_c_heap();
388  _queue = SynchronizedGCTaskQueue::create(unsynchronized_queue, lock());
389  _noop_task = NoopGCTask::create_on_c_heap();
390  _resource_flag = NEW_C_HEAP_ARRAY(bool, workers(), mtGC);
391  {
392    // Set up worker threads.
393    //     Distribute the workers among the available processors,
394    //     unless we were told not to, or if the os doesn't want to.
395    uint* processor_assignment = NEW_C_HEAP_ARRAY(uint, workers(), mtGC);
396    if (!BindGCTaskThreadsToCPUs ||
397        !os::distribute_processes(workers(), processor_assignment)) {
398      for (uint a = 0; a < workers(); a += 1) {
399        processor_assignment[a] = sentinel_worker();
400      }
401    }
402    _thread = NEW_C_HEAP_ARRAY(GCTaskThread*, workers(), mtGC);
403    for (uint t = 0; t < workers(); t += 1) {
404      set_thread(t, GCTaskThread::create(this, t, processor_assignment[t]));
405    }
406    if (TraceGCTaskThread) {
407      tty->print("GCTaskManager::initialize: distribution:");
408      for (uint t = 0; t < workers(); t += 1) {
409        tty->print("  %u", processor_assignment[t]);
410      }
411      tty->cr();
412    }
413    FREE_C_HEAP_ARRAY(uint, processor_assignment);
414  }
415  reset_busy_workers();
416  set_unblocked();
417  for (uint w = 0; w < workers(); w += 1) {
418    set_resource_flag(w, false);
419  }
420  reset_delivered_tasks();
421  reset_completed_tasks();
422  reset_barriers();
423  reset_emptied_queue();
424  for (uint s = 0; s < workers(); s += 1) {
425    thread(s)->start();
426  }
427}
428
429GCTaskManager::~GCTaskManager() {
430  assert(busy_workers() == 0, "still have busy workers");
431  assert(queue()->is_empty(), "still have queued work");
432  NoopGCTask::destroy(_noop_task);
433  _noop_task = NULL;
434  if (_thread != NULL) {
435    for (uint i = 0; i < workers(); i += 1) {
436      GCTaskThread::destroy(thread(i));
437      set_thread(i, NULL);
438    }
439    FREE_C_HEAP_ARRAY(GCTaskThread*, _thread);
440    _thread = NULL;
441  }
442  if (_resource_flag != NULL) {
443    FREE_C_HEAP_ARRAY(bool, _resource_flag);
444    _resource_flag = NULL;
445  }
446  if (queue() != NULL) {
447    GCTaskQueue* unsynchronized_queue = queue()->unsynchronized_queue();
448    GCTaskQueue::destroy(unsynchronized_queue);
449    SynchronizedGCTaskQueue::destroy(queue());
450    _queue = NULL;
451  }
452  if (monitor() != NULL) {
453    delete monitor();
454    _monitor = NULL;
455  }
456}
457
458void GCTaskManager::set_active_gang() {
459  _active_workers =
460    AdaptiveSizePolicy::calc_active_workers(workers(),
461                                 active_workers(),
462                                 Threads::number_of_non_daemon_threads());
463
464  assert(!all_workers_active() || active_workers() == ParallelGCThreads,
465         "all_workers_active() is  incorrect: "
466         "active %d  ParallelGCThreads %u", active_workers(),
467         ParallelGCThreads);
468  if (TraceDynamicGCThreads) {
469    gclog_or_tty->print_cr("GCTaskManager::set_active_gang(): "
470                           "all_workers_active()  %d  workers %d  "
471                           "active  %d  ParallelGCThreads %u",
472                           all_workers_active(), workers(),  active_workers(),
473                           ParallelGCThreads);
474  }
475}
476
477// Create IdleGCTasks for inactive workers.
478// Creates tasks in a ResourceArea and assumes
479// an appropriate ResourceMark.
480void GCTaskManager::task_idle_workers() {
481  {
482    int more_inactive_workers = 0;
483    {
484      // Stop any idle tasks from exiting their IdleGCTask's
485      // and get the count for additional IdleGCTask's under
486      // the GCTaskManager's monitor so that the "more_inactive_workers"
487      // count is correct.
488      MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489      _wait_helper.set_should_wait(true);
490      // active_workers are a number being requested.  idle_workers
491      // are the number currently idle.  If all the workers are being
492      // requested to be active but some are already idle, reduce
493      // the number of active_workers to be consistent with the
494      // number of idle_workers.  The idle_workers are stuck in
495      // idle tasks and will no longer be release (since a new GC
496      // is starting).  Try later to release enough idle_workers
497      // to allow the desired number of active_workers.
498      more_inactive_workers =
499        workers() - active_workers() - idle_workers();
500      if (more_inactive_workers < 0) {
501        int reduced_active_workers = active_workers() + more_inactive_workers;
502        set_active_workers(reduced_active_workers);
503        more_inactive_workers = 0;
504      }
505      if (TraceDynamicGCThreads) {
506        gclog_or_tty->print_cr("JT: %d  workers %d  active  %d  "
507                                "idle %d  more %d",
508                                Threads::number_of_non_daemon_threads(),
509                                workers(),
510                                active_workers(),
511                                idle_workers(),
512                                more_inactive_workers);
513      }
514    }
515    GCTaskQueue* q = GCTaskQueue::create();
516    for(uint i = 0; i < (uint) more_inactive_workers; i++) {
517      q->enqueue(IdleGCTask::create_on_c_heap());
518      increment_idle_workers();
519    }
520    assert(workers() == active_workers() + idle_workers(),
521      "total workers should equal active + inactive");
522    add_list(q);
523    // GCTaskQueue* q was created in a ResourceArea so a
524    // destroy() call is not needed.
525  }
526}
527
528void  GCTaskManager::release_idle_workers() {
529  {
530    MutexLockerEx ml(monitor(),
531      Mutex::_no_safepoint_check_flag);
532    _wait_helper.set_should_wait(false);
533    monitor()->notify_all();
534  // Release monitor
535  }
536}
537
538void GCTaskManager::print_task_time_stamps() {
539  for(uint i=0; i<ParallelGCThreads; i++) {
540    GCTaskThread* t = thread(i);
541    t->print_task_time_stamps();
542  }
543}
544
545void GCTaskManager::print_threads_on(outputStream* st) {
546  uint num_thr = workers();
547  for (uint i = 0; i < num_thr; i++) {
548    thread(i)->print_on(st);
549    st->cr();
550  }
551}
552
553void GCTaskManager::threads_do(ThreadClosure* tc) {
554  assert(tc != NULL, "Null ThreadClosure");
555  uint num_thr = workers();
556  for (uint i = 0; i < num_thr; i++) {
557    tc->do_thread(thread(i));
558  }
559}
560
561GCTaskThread* GCTaskManager::thread(uint which) {
562  assert(which < workers(), "index out of bounds");
563  assert(_thread[which] != NULL, "shouldn't have null thread");
564  return _thread[which];
565}
566
567void GCTaskManager::set_thread(uint which, GCTaskThread* value) {
568  assert(which < workers(), "index out of bounds");
569  assert(value != NULL, "shouldn't have null thread");
570  _thread[which] = value;
571}
572
573void GCTaskManager::add_task(GCTask* task) {
574  assert(task != NULL, "shouldn't have null task");
575  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
576  if (TraceGCTaskManager) {
577    tty->print_cr("GCTaskManager::add_task(" INTPTR_FORMAT " [%s])",
578                  p2i(task), GCTask::Kind::to_string(task->kind()));
579  }
580  queue()->enqueue(task);
581  // Notify with the lock held to avoid missed notifies.
582  if (TraceGCTaskManager) {
583    tty->print_cr("    GCTaskManager::add_task (%s)->notify_all",
584                  monitor()->name());
585  }
586  (void) monitor()->notify_all();
587  // Release monitor().
588}
589
590void GCTaskManager::add_list(GCTaskQueue* list) {
591  assert(list != NULL, "shouldn't have null task");
592  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
593  if (TraceGCTaskManager) {
594    tty->print_cr("GCTaskManager::add_list(%u)", list->length());
595  }
596  queue()->enqueue(list);
597  // Notify with the lock held to avoid missed notifies.
598  if (TraceGCTaskManager) {
599    tty->print_cr("    GCTaskManager::add_list (%s)->notify_all",
600                  monitor()->name());
601  }
602  (void) monitor()->notify_all();
603  // Release monitor().
604}
605
606// GC workers wait in get_task() for new work to be added
607// to the GCTaskManager's queue.  When new work is added,
608// a notify is sent to the waiting GC workers which then
609// compete to get tasks.  If a GC worker wakes up and there
610// is no work on the queue, it is given a noop_task to execute
611// and then loops to find more work.
612
613GCTask* GCTaskManager::get_task(uint which) {
614  GCTask* result = NULL;
615  // Grab the queue lock.
616  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
617  // Wait while the queue is block or
618  // there is nothing to do, except maybe release resources.
619  while (is_blocked() ||
620         (queue()->is_empty() && !should_release_resources(which))) {
621    if (TraceGCTaskManager) {
622      tty->print_cr("GCTaskManager::get_task(%u)"
623                    "  blocked: %s"
624                    "  empty: %s"
625                    "  release: %s",
626                    which,
627                    is_blocked() ? "true" : "false",
628                    queue()->is_empty() ? "true" : "false",
629                    should_release_resources(which) ? "true" : "false");
630      tty->print_cr("    => (%s)->wait()",
631                    monitor()->name());
632    }
633    monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
634  }
635  // We've reacquired the queue lock here.
636  // Figure out which condition caused us to exit the loop above.
637  if (!queue()->is_empty()) {
638    if (UseGCTaskAffinity) {
639      result = queue()->dequeue(which);
640    } else {
641      result = queue()->dequeue();
642    }
643    if (result->is_barrier_task()) {
644      assert(which != sentinel_worker(),
645             "blocker shouldn't be bogus");
646      set_blocking_worker(which);
647    }
648  } else {
649    // The queue is empty, but we were woken up.
650    // Just hand back a Noop task,
651    // in case someone wanted us to release resources, or whatever.
652    result = noop_task();
653  }
654  assert(result != NULL, "shouldn't have null task");
655  if (TraceGCTaskManager) {
656    tty->print_cr("GCTaskManager::get_task(%u) => " INTPTR_FORMAT " [%s]",
657                  which, p2i(result), GCTask::Kind::to_string(result->kind()));
658    tty->print_cr("     %s", result->name());
659  }
660  if (!result->is_idle_task()) {
661    increment_busy_workers();
662    increment_delivered_tasks();
663  }
664  return result;
665  // Release monitor().
666}
667
668void GCTaskManager::note_completion(uint which) {
669  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
670  if (TraceGCTaskManager) {
671    tty->print_cr("GCTaskManager::note_completion(%u)", which);
672  }
673  // If we are blocked, check if the completing thread is the blocker.
674  if (blocking_worker() == which) {
675    assert(blocking_worker() != sentinel_worker(),
676           "blocker shouldn't be bogus");
677    increment_barriers();
678    set_unblocked();
679  }
680  increment_completed_tasks();
681  uint active = decrement_busy_workers();
682  if ((active == 0) && (queue()->is_empty())) {
683    increment_emptied_queue();
684    if (TraceGCTaskManager) {
685      tty->print_cr("    GCTaskManager::note_completion(%u) done", which);
686    }
687  }
688  if (TraceGCTaskManager) {
689    tty->print_cr("    GCTaskManager::note_completion(%u) (%s)->notify_all",
690                  which, monitor()->name());
691    tty->print_cr("  "
692                  "  blocked: %s"
693                  "  empty: %s"
694                  "  release: %s",
695                  is_blocked() ? "true" : "false",
696                  queue()->is_empty() ? "true" : "false",
697                  should_release_resources(which) ? "true" : "false");
698    tty->print_cr("  "
699                  "  delivered: %u"
700                  "  completed: %u"
701                  "  barriers: %u"
702                  "  emptied: %u",
703                  delivered_tasks(),
704                  completed_tasks(),
705                  barriers(),
706                  emptied_queue());
707  }
708  // Tell everyone that a task has completed.
709  (void) monitor()->notify_all();
710  // Release monitor().
711}
712
713uint GCTaskManager::increment_busy_workers() {
714  assert(queue()->own_lock(), "don't own the lock");
715  _busy_workers += 1;
716  return _busy_workers;
717}
718
719uint GCTaskManager::decrement_busy_workers() {
720  assert(queue()->own_lock(), "don't own the lock");
721  assert(_busy_workers > 0, "About to make a mistake");
722  _busy_workers -= 1;
723  return _busy_workers;
724}
725
726void GCTaskManager::release_all_resources() {
727  // If you want this to be done atomically, do it in a WaitForBarrierGCTask.
728  for (uint i = 0; i < workers(); i += 1) {
729    set_resource_flag(i, true);
730  }
731}
732
733bool GCTaskManager::should_release_resources(uint which) {
734  // This can be done without a lock because each thread reads one element.
735  return resource_flag(which);
736}
737
738void GCTaskManager::note_release(uint which) {
739  // This can be done without a lock because each thread writes one element.
740  set_resource_flag(which, false);
741}
742
743// "list" contains tasks that are ready to execute.  Those
744// tasks are added to the GCTaskManager's queue of tasks and
745// then the GC workers are notified that there is new work to
746// do.
747//
748// Typically different types of tasks can be added to the "list".
749// For example in PSScavenge OldToYoungRootsTask, SerialOldToYoungRootsTask,
750// ScavengeRootsTask, and StealTask tasks are all added to the list
751// and then the GC workers are notified of new work.  The tasks are
752// handed out in the order in which they are added to the list
753// (although execution is not necessarily in that order).  As long
754// as any tasks are running the GCTaskManager will wait for execution
755// to complete.  GC workers that execute a stealing task remain in
756// the stealing task until all stealing tasks have completed.  The load
757// balancing afforded by the stealing tasks work best if the stealing
758// tasks are added last to the list.
759
760void GCTaskManager::execute_and_wait(GCTaskQueue* list) {
761  WaitForBarrierGCTask* fin = WaitForBarrierGCTask::create();
762  list->enqueue(fin);
763  // The barrier task will be read by one of the GC
764  // workers once it is added to the list of tasks.
765  // Be sure that is globally visible before the
766  // GC worker reads it (which is after the task is added
767  // to the list of tasks below).
768  OrderAccess::storestore();
769  add_list(list);
770  fin->wait_for(true /* reset */);
771  // We have to release the barrier tasks!
772  WaitForBarrierGCTask::destroy(fin);
773}
774
775bool GCTaskManager::resource_flag(uint which) {
776  assert(which < workers(), "index out of bounds");
777  return _resource_flag[which];
778}
779
780void GCTaskManager::set_resource_flag(uint which, bool value) {
781  assert(which < workers(), "index out of bounds");
782  _resource_flag[which] = value;
783}
784
785//
786// NoopGCTask
787//
788
789NoopGCTask* NoopGCTask::create_on_c_heap() {
790  NoopGCTask* result = new(ResourceObj::C_HEAP, mtGC) NoopGCTask();
791  return result;
792}
793
794void NoopGCTask::destroy(NoopGCTask* that) {
795  if (that != NULL) {
796    that->destruct();
797    FreeHeap(that);
798  }
799}
800
801// This task should never be performing GC work that require
802// a valid GC id.
803NoopGCTask::NoopGCTask() : GCTask(GCTask::Kind::noop_task, GCId::undefined()) { }
804
805void NoopGCTask::destruct() {
806  // This has to know it's superclass structure, just like the constructor.
807  this->GCTask::destruct();
808  // Nothing else to do.
809}
810
811//
812// IdleGCTask
813//
814
815IdleGCTask* IdleGCTask::create() {
816  IdleGCTask* result = new IdleGCTask(false);
817  assert(UseDynamicNumberOfGCThreads,
818    "Should only be used with dynamic GC thread");
819  return result;
820}
821
822IdleGCTask* IdleGCTask::create_on_c_heap() {
823  IdleGCTask* result = new(ResourceObj::C_HEAP, mtGC) IdleGCTask(true);
824  assert(UseDynamicNumberOfGCThreads,
825    "Should only be used with dynamic GC thread");
826  return result;
827}
828
829void IdleGCTask::do_it(GCTaskManager* manager, uint which) {
830  WaitHelper* wait_helper = manager->wait_helper();
831  if (TraceGCTaskManager) {
832    tty->print_cr("[" INTPTR_FORMAT "]"
833                  " IdleGCTask:::do_it()"
834      "  should_wait: %s",
835      p2i(this), wait_helper->should_wait() ? "true" : "false");
836  }
837  MutexLockerEx ml(manager->monitor(), Mutex::_no_safepoint_check_flag);
838  if (TraceDynamicGCThreads) {
839    gclog_or_tty->print_cr("--- idle %d", which);
840  }
841  // Increment has to be done when the idle tasks are created.
842  // manager->increment_idle_workers();
843  manager->monitor()->notify_all();
844  while (wait_helper->should_wait()) {
845    if (TraceGCTaskManager) {
846      tty->print_cr("[" INTPTR_FORMAT "]"
847                    " IdleGCTask::do_it()"
848        "  [" INTPTR_FORMAT "] (%s)->wait()",
849        p2i(this), p2i(manager->monitor()), manager->monitor()->name());
850    }
851    manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
852  }
853  manager->decrement_idle_workers();
854  if (TraceDynamicGCThreads) {
855    gclog_or_tty->print_cr("--- release %d", which);
856  }
857  if (TraceGCTaskManager) {
858    tty->print_cr("[" INTPTR_FORMAT "]"
859                  " IdleGCTask::do_it() returns"
860      "  should_wait: %s",
861      p2i(this), wait_helper->should_wait() ? "true" : "false");
862  }
863  // Release monitor().
864}
865
866void IdleGCTask::destroy(IdleGCTask* that) {
867  if (that != NULL) {
868    that->destruct();
869    if (that->is_c_heap_obj()) {
870      FreeHeap(that);
871    }
872  }
873}
874
875void IdleGCTask::destruct() {
876  // This has to know it's superclass structure, just like the constructor.
877  this->GCTask::destruct();
878  // Nothing else to do.
879}
880
881//
882// WaitForBarrierGCTask
883//
884WaitForBarrierGCTask* WaitForBarrierGCTask::create() {
885  WaitForBarrierGCTask* result = new WaitForBarrierGCTask();
886  return result;
887}
888
889WaitForBarrierGCTask::WaitForBarrierGCTask() : GCTask(GCTask::Kind::wait_for_barrier_task) { }
890
891void WaitForBarrierGCTask::destroy(WaitForBarrierGCTask* that) {
892  if (that != NULL) {
893    if (TraceGCTaskManager) {
894      tty->print_cr("[" INTPTR_FORMAT "] WaitForBarrierGCTask::destroy()", p2i(that));
895    }
896    that->destruct();
897  }
898}
899
900void WaitForBarrierGCTask::destruct() {
901  if (TraceGCTaskManager) {
902    tty->print_cr("[" INTPTR_FORMAT "] WaitForBarrierGCTask::destruct()", p2i(this));
903  }
904  this->GCTask::destruct();
905  // Clean up that should be in the destructor,
906  // except that ResourceMarks don't call destructors.
907  _wait_helper.release_monitor();
908}
909
910void WaitForBarrierGCTask::do_it_internal(GCTaskManager* manager, uint which) {
911  // Wait for this to be the only busy worker.
912  assert(manager->monitor()->owned_by_self(), "don't own the lock");
913  assert(manager->is_blocked(), "manager isn't blocked");
914  while (manager->busy_workers() > 1) {
915    if (TraceGCTaskManager) {
916      tty->print_cr("WaitForBarrierGCTask::do_it(%u) waiting on %u workers",
917                    which, manager->busy_workers());
918    }
919    manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
920  }
921}
922
923void WaitForBarrierGCTask::do_it(GCTaskManager* manager, uint which) {
924  if (TraceGCTaskManager) {
925    tty->print_cr("[" INTPTR_FORMAT "]"
926                  " WaitForBarrierGCTask::do_it() waiting for idle",
927                  p2i(this));
928  }
929  {
930    // First, wait for the barrier to arrive.
931    MutexLockerEx ml(manager->lock(), Mutex::_no_safepoint_check_flag);
932    do_it_internal(manager, which);
933    // Release manager->lock().
934  }
935  // Then notify the waiter.
936  _wait_helper.notify();
937}
938
939WaitHelper::WaitHelper() : _should_wait(true), _monitor(MonitorSupply::reserve()) {
940  if (TraceGCTaskManager) {
941    tty->print_cr("[" INTPTR_FORMAT "]"
942                  " WaitHelper::WaitHelper()"
943                  "  monitor: " INTPTR_FORMAT,
944                  p2i(this), p2i(monitor()));
945  }
946}
947
948void WaitHelper::release_monitor() {
949  assert(_monitor != NULL, "");
950  MonitorSupply::release(_monitor);
951  _monitor = NULL;
952}
953
954WaitHelper::~WaitHelper() {
955  release_monitor();
956}
957
958void WaitHelper::wait_for(bool reset) {
959  if (TraceGCTaskManager) {
960    tty->print_cr("[" INTPTR_FORMAT "]"
961                  " WaitForBarrierGCTask::wait_for()"
962      "  should_wait: %s",
963      p2i(this), should_wait() ? "true" : "false");
964  }
965  {
966    // Grab the lock and check again.
967    MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
968    while (should_wait()) {
969      if (TraceGCTaskManager) {
970        tty->print_cr("[" INTPTR_FORMAT "]"
971                      " WaitForBarrierGCTask::wait_for()"
972          "  [" INTPTR_FORMAT "] (%s)->wait()",
973          p2i(this), p2i(monitor()), monitor()->name());
974      }
975      monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
976    }
977    // Reset the flag in case someone reuses this task.
978    if (reset) {
979      set_should_wait(true);
980    }
981    if (TraceGCTaskManager) {
982      tty->print_cr("[" INTPTR_FORMAT "]"
983                    " WaitForBarrierGCTask::wait_for() returns"
984        "  should_wait: %s",
985        p2i(this), should_wait() ? "true" : "false");
986    }
987    // Release monitor().
988  }
989}
990
991void WaitHelper::notify() {
992  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
993  set_should_wait(false);
994  // Waiter doesn't miss the notify in the wait_for method
995  // since it checks the flag after grabbing the monitor.
996  if (TraceGCTaskManager) {
997    tty->print_cr("[" INTPTR_FORMAT "]"
998                  " WaitForBarrierGCTask::do_it()"
999                  "  [" INTPTR_FORMAT "] (%s)->notify_all()",
1000                  p2i(this), p2i(monitor()), monitor()->name());
1001  }
1002  monitor()->notify_all();
1003}
1004
1005Mutex*                   MonitorSupply::_lock     = NULL;
1006GrowableArray<Monitor*>* MonitorSupply::_freelist = NULL;
1007
1008Monitor* MonitorSupply::reserve() {
1009  Monitor* result = NULL;
1010  // Lazy initialization: possible race.
1011  if (lock() == NULL) {
1012    _lock = new Mutex(Mutex::barrier,                  // rank
1013                      "MonitorSupply mutex",           // name
1014                      Mutex::_allow_vm_block_flag);    // allow_vm_block
1015  }
1016  {
1017    MutexLockerEx ml(lock());
1018    // Lazy initialization.
1019    if (freelist() == NULL) {
1020      _freelist =
1021        new(ResourceObj::C_HEAP, mtGC) GrowableArray<Monitor*>(ParallelGCThreads,
1022                                                         true);
1023    }
1024    if (! freelist()->is_empty()) {
1025      result = freelist()->pop();
1026    } else {
1027      result = new Monitor(Mutex::barrier,                  // rank
1028                           "MonitorSupply monitor",         // name
1029                           Mutex::_allow_vm_block_flag,     // allow_vm_block
1030                           Monitor::_safepoint_check_never);
1031    }
1032    guarantee(result != NULL, "shouldn't return NULL");
1033    assert(!result->is_locked(), "shouldn't be locked");
1034    // release lock().
1035  }
1036  return result;
1037}
1038
1039void MonitorSupply::release(Monitor* instance) {
1040  assert(instance != NULL, "shouldn't release NULL");
1041  assert(!instance->is_locked(), "shouldn't be locked");
1042  {
1043    MutexLockerEx ml(lock());
1044    freelist()->push(instance);
1045    // release lock().
1046  }
1047}
1048