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