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