src/hotspot/share/gc/shared/workgroup.cpp
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58084 cddef3bde924
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 53103
diff changeset
     2
 * Copyright (c) 2001, 2019, 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: 1374
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
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: 1374
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: 6759
diff changeset
    25
#include "precompiled.hpp"
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 33105
diff changeset
    26
#include "gc/shared/gcId.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30584
diff changeset
    27
#include "gc/shared/workgroup.hpp"
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    28
#include "gc/shared/workerManager.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    29
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    30
#include "memory/allocation.inline.hpp"
40655
9f644073d3a0 8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents: 40096
diff changeset
    31
#include "runtime/atomic.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    32
#include "runtime/os.hpp"
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
    33
#include "runtime/semaphore.hpp"
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
    34
#include "runtime/thread.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// Definitions of WorkGang methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    38
// The current implementation will exit if the allocation
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    39
// of any worker fails.
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    40
void  AbstractWorkGang::initialize_workers() {
37225
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
    41
  log_develop_trace(gc, workgang)("Constructing work gang %s with %u threads", name(), total_workers());
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    42
  _workers = NEW_C_HEAP_ARRAY(AbstractGangWorker*, total_workers(), mtInternal);
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    43
  add_workers(true);
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    44
}
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    45
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    46
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    47
AbstractGangWorker* AbstractWorkGang::install_worker(uint worker_id) {
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    48
  AbstractGangWorker* new_worker = allocate_worker(worker_id);
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    49
  set_thread(worker_id, new_worker);
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    50
  return new_worker;
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    51
}
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    52
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    53
void AbstractWorkGang::add_workers(bool initializing) {
39231
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
    54
  add_workers(_active_workers, initializing);
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
    55
}
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
    56
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
    57
void AbstractWorkGang::add_workers(uint active_workers, bool initializing) {
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    58
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    59
  os::ThreadType worker_type;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    60
  if (are_ConcurrentGC_threads()) {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    61
    worker_type = os::cgc_thread;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    62
  } else {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    63
    worker_type = os::pgc_thread;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    64
  }
39704
157f39705057 8157240: GC task trace logging is incomprehensible
jmasa
parents: 39272
diff changeset
    65
  uint previous_created_workers = _created_workers;
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    66
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    67
  _created_workers = WorkerManager::add_workers(this,
39231
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
    68
                                                active_workers,
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    69
                                                _total_workers,
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    70
                                                _created_workers,
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    71
                                                worker_type,
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    72
                                                initializing);
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    73
  _active_workers = MIN2(_created_workers, _active_workers);
39704
157f39705057 8157240: GC task trace logging is incomprehensible
jmasa
parents: 39272
diff changeset
    74
157f39705057 8157240: GC task trace logging is incomprehensible
jmasa
parents: 39272
diff changeset
    75
  WorkerManager::log_worker_creation(this, previous_created_workers, _active_workers, _created_workers, initializing);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    78
AbstractGangWorker* AbstractWorkGang::worker(uint i) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  // Array index bounds checking.
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    80
  AbstractGangWorker* result = NULL;
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    81
  assert(_workers != NULL, "No workers for indexing");
29580
a67a581cfe11 8073315: Enable gcc -Wtype-limits and fix upcoming issues.
goetz
parents: 29464
diff changeset
    82
  assert(i < total_workers(), "Worker index out of bounds");
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    83
  result = _workers[i];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  assert(result != NULL, "Indexing to null worker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    88
void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    89
  uint workers = created_workers();
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    90
  for (uint i = 0; i < workers; i++) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    91
    worker(i)->print_on(st);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    92
    st->cr();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    93
  }
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    94
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    95
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    96
void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    97
  assert(tc != NULL, "Null ThreadClosure");
38216
250794c6f95f 6858051: Create GC worker threads dynamically
jmasa
parents: 38153
diff changeset
    98
  uint workers = created_workers();
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
    99
  for (uint i = 0; i < workers; i++) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   100
    tc->do_thread(worker(i));
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   101
  }
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   102
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   103
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   104
// WorkGang dispatcher implemented with semaphores.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   105
//
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   106
// Semaphores don't require the worker threads to re-claim the lock when they wake up.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   107
// This helps lowering the latency when starting and stopping the worker threads.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   108
class SemaphoreGangTaskDispatcher : public GangTaskDispatcher {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   109
  // The task currently being dispatched to the GangWorkers.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   110
  AbstractGangTask* _task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   111
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   112
  volatile uint _started;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   113
  volatile uint _not_finished;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   114
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   115
  // Semaphore used to start the GangWorkers.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   116
  Semaphore* _start_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   117
  // Semaphore used to notify the coordinator that all workers are done.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   118
  Semaphore* _end_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   119
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   120
public:
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   121
  SemaphoreGangTaskDispatcher() :
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   122
      _task(NULL),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   123
      _started(0),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   124
      _not_finished(0),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   125
      _start_semaphore(new Semaphore()),
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   126
      _end_semaphore(new Semaphore())
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   127
{ }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   128
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   129
  ~SemaphoreGangTaskDispatcher() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   130
    delete _start_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   131
    delete _end_semaphore;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   132
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   133
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   134
  void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   135
    // No workers are allowed to read the state variables until they have been signaled.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   136
    _task         = task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   137
    _not_finished = num_workers;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   138
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   139
    // Dispatch 'num_workers' number of tasks.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   140
    _start_semaphore->signal(num_workers);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   141
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   142
    // Wait for the last worker to signal the coordinator.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   143
    _end_semaphore->wait();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   144
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   145
    // No workers are allowed to read the state variables after the coordinator has been signaled.
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32361
diff changeset
   146
    assert(_not_finished == 0, "%d not finished workers?", _not_finished);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   147
    _task    = NULL;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   148
    _started = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   149
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   150
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   151
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   152
  WorkData worker_wait_for_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   153
    // Wait for the coordinator to dispatch a task.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   154
    _start_semaphore->wait();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   155
48955
e22914003cf0 8194691: Cleanup unnecessary casts in Atomic/OrderAccess uses
kbarrett
parents: 48123
diff changeset
   156
    uint num_started = Atomic::add(1u, &_started);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   157
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   158
    // Subtract one to get a zero-indexed worker id.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   159
    uint worker_id = num_started - 1;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   160
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   161
    return WorkData(_task, worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   162
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   163
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   164
  void worker_done_with_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   165
    // Mark that the worker is done with the task.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   166
    // The worker is not allowed to read the state variables after this line.
48955
e22914003cf0 8194691: Cleanup unnecessary casts in Atomic/OrderAccess uses
kbarrett
parents: 48123
diff changeset
   167
    uint not_finished = Atomic::sub(1u, &_not_finished);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   168
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   169
    // The last worker signals to the coordinator that all work is completed.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   170
    if (not_finished == 0) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   171
      _end_semaphore->signal();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   172
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   173
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   174
};
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   175
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   176
class MutexGangTaskDispatcher : public GangTaskDispatcher {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   177
  AbstractGangTask* _task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   178
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   179
  volatile uint _started;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   180
  volatile uint _finished;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   181
  volatile uint _num_workers;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   182
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   183
  Monitor* _monitor;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   184
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   185
 public:
51332
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   186
  MutexGangTaskDispatcher() :
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   187
    _task(NULL),
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   188
    _started(0),
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   189
    _finished(0),
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   190
    _num_workers(0),
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   191
    _monitor(new Monitor(Monitor::leaf, "WorkGang dispatcher lock", false, Monitor::_safepoint_check_never)) {
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   192
  }
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   193
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   194
  ~MutexGangTaskDispatcher() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   195
    delete _monitor;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   196
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   197
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   198
  void coordinator_execute_on_workers(AbstractGangTask* task, uint num_workers) {
54645
05aaccf7d558 8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents: 54623
diff changeset
   199
    MonitorLocker ml(_monitor, Mutex::_no_safepoint_check_flag);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   200
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   201
    _task        = task;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   202
    _num_workers = num_workers;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   203
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   204
    // Tell the workers to get to work.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   205
    _monitor->notify_all();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   206
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   207
    // Wait for them to finish.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   208
    while (_finished < _num_workers) {
54645
05aaccf7d558 8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents: 54623
diff changeset
   209
      ml.wait();
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   210
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   211
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   212
    _task        = NULL;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   213
    _num_workers = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   214
    _started     = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   215
    _finished    = 0;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   216
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   217
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   218
  WorkData worker_wait_for_task() {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 53103
diff changeset
   219
    MonitorLocker ml(_monitor, Mutex::_no_safepoint_check_flag);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   220
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   221
    while (_num_workers == 0 || _started == _num_workers) {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 53103
diff changeset
   222
      _monitor->wait();
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   223
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   224
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   225
    _started++;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   226
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   227
    // Subtract one to get a zero-indexed worker id.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   228
    uint worker_id = _started - 1;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   229
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   230
    return WorkData(_task, worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   231
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   232
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   233
  void worker_done_with_task() {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 53103
diff changeset
   234
    MonitorLocker ml(_monitor, Mutex::_no_safepoint_check_flag);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   235
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   236
    _finished++;
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   237
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   238
    if (_finished == _num_workers) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   239
      // This will wake up all workers and not only the coordinator.
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   240
      _monitor->notify_all();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   241
    }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   242
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   243
};
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   244
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   245
static GangTaskDispatcher* create_dispatcher() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   246
  if (UseSemaphoreGCThreadsSynchronization) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   247
    return new SemaphoreGangTaskDispatcher();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   248
  }
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   249
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   250
  return new MutexGangTaskDispatcher();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   251
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   252
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   253
WorkGang::WorkGang(const char* name,
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   254
                   uint  workers,
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   255
                   bool  are_GC_task_threads,
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   256
                   bool  are_ConcurrentGC_threads) :
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   257
    AbstractWorkGang(name, workers, are_GC_task_threads, are_ConcurrentGC_threads),
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   258
    _dispatcher(create_dispatcher())
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   259
{ }
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   260
48123
f93055f440d8 8191864: Provide a public destructor for WorkGang
pliden
parents: 47216
diff changeset
   261
WorkGang::~WorkGang() {
f93055f440d8 8191864: Provide a public destructor for WorkGang
pliden
parents: 47216
diff changeset
   262
  delete _dispatcher;
f93055f440d8 8191864: Provide a public destructor for WorkGang
pliden
parents: 47216
diff changeset
   263
}
f93055f440d8 8191864: Provide a public destructor for WorkGang
pliden
parents: 47216
diff changeset
   264
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   265
AbstractGangWorker* WorkGang::allocate_worker(uint worker_id) {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   266
  return new GangWorker(this, worker_id);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   267
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   268
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
void WorkGang::run_task(AbstractGangTask* task) {
38153
4f13f0b690c3 8155232: Augment Workgang to run task with a given number of threads
tschatzl
parents: 37225
diff changeset
   270
  run_task(task, active_workers());
4f13f0b690c3 8155232: Augment Workgang to run task with a given number of threads
tschatzl
parents: 37225
diff changeset
   271
}
4f13f0b690c3 8155232: Augment Workgang to run task with a given number of threads
tschatzl
parents: 37225
diff changeset
   272
4f13f0b690c3 8155232: Augment Workgang to run task with a given number of threads
tschatzl
parents: 37225
diff changeset
   273
void WorkGang::run_task(AbstractGangTask* task, uint num_workers) {
39231
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
   274
  guarantee(num_workers <= total_workers(),
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
   275
            "Trying to execute task %s with %u workers which is more than the amount of total workers %u.",
e6aa09b4f1b9 8157620: Guarantee in run_task(task, num_workers) fails
jmasa
parents: 38216
diff changeset
   276
            task->name(), num_workers, total_workers());
38153
4f13f0b690c3 8155232: Augment Workgang to run task with a given number of threads
tschatzl
parents: 37225
diff changeset
   277
  guarantee(num_workers > 0, "Trying to execute task %s with zero workers", task->name());
40096
246c62cd9180 8159073: : Error handling incomplete when creating GC threads lazily
jmasa
parents: 39704
diff changeset
   278
  uint old_num_workers = _active_workers;
246c62cd9180 8159073: : Error handling incomplete when creating GC threads lazily
jmasa
parents: 39704
diff changeset
   279
  update_active_workers(num_workers);
38153
4f13f0b690c3 8155232: Augment Workgang to run task with a given number of threads
tschatzl
parents: 37225
diff changeset
   280
  _dispatcher->coordinator_execute_on_workers(task, num_workers);
40096
246c62cd9180 8159073: : Error handling incomplete when creating GC threads lazily
jmasa
parents: 39704
diff changeset
   281
  update_active_workers(old_num_workers);
11174
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   282
}
fccee5238e70 6593758: RFE: Enhance GC ergonomics to dynamically choose ParallelGCThreads
jmasa
parents: 8688
diff changeset
   283
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   284
AbstractGangWorker::AbstractGangWorker(AbstractWorkGang* gang, uint id) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  _gang = gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  set_id(id);
29464
02c245ad3ec9 8073545: Use shorter and more descriptive names for GC worker threads
david
parents: 29326
diff changeset
   287
  set_name("%s#%d", gang->name(), id);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   290
void AbstractGangWorker::run() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   295
void AbstractGangWorker::initialize() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  assert(_gang != NULL, "No gang to run in");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  os::set_priority(this, NearMaxPriority);
37225
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
   298
  log_develop_trace(gc, workgang)("Running gang worker for gang %s id %u", gang()->name(), id());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
         " of a work gang");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
32360
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   303
bool AbstractGangWorker::is_GC_task_thread() const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   304
  return gang()->are_GC_task_threads();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   305
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   306
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   307
bool AbstractGangWorker::is_ConcurrentGC_thread() const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   308
  return gang()->are_ConcurrentGC_threads();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   309
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   310
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   311
void AbstractGangWorker::print_on(outputStream* st) const {
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   312
  st->print("\"%s\" ", name());
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   313
  Thread::print_on(st);
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   314
  st->cr();
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   315
}
86790204fc23 8087323: Unify and split the work gang classes
stefank
parents: 31030
diff changeset
   316
54807
33fe50b6d707 8223626: move print() functions to cpp files
coleenp
parents: 54645
diff changeset
   317
void AbstractGangWorker::print() const { print_on(tty); }
33fe50b6d707 8223626: move print() functions to cpp files
coleenp
parents: 54645
diff changeset
   318
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   319
WorkData GangWorker::wait_for_task() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   320
  return gang()->dispatcher()->worker_wait_for_task();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   321
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   322
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   323
void GangWorker::signal_task_done() {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   324
  gang()->dispatcher()->worker_done_with_task();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   325
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   326
37225
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
   327
void GangWorker::run_task(WorkData data) {
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
   328
  GCIdMark gc_id_mark(data._task->gc_id());
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
   329
  log_develop_trace(gc, workgang)("Running work gang: %s task: %s worker: %u", name(), data._task->name(), data._worker_id);
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   330
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   331
  data._task->work(data._worker_id);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   332
37225
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
   333
  log_develop_trace(gc, workgang)("Finished work gang: %s task: %s worker: %u thread: " PTR_FORMAT,
ac6c704f9a8c 8153187: Convert TraceWorkGang to use unified logging
brutisso
parents: 35465
diff changeset
   334
                                  name(), data._task->name(), data._worker_id, p2i(Thread::current()));
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   335
}
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   336
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
void GangWorker::loop() {
32361
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   338
  while (true) {
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   339
    WorkData data = wait_for_task();
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   340
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   341
    run_task(data);
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   342
3de9a2e1e2d0 8087324: Use semaphores when starting and stopping GC task threads
stefank
parents: 32360
diff changeset
   343
    signal_task_done();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
// *** WorkGangBarrierSync
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
WorkGangBarrierSync::WorkGangBarrierSync()
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
   350
  : _monitor(Mutex::safepoint, "work gang barrier sync", true,
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
   351
             Monitor::_safepoint_check_never),
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   352
    _n_workers(0), _n_completed(0), _should_reset(false), _aborted(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   355
WorkGangBarrierSync::WorkGangBarrierSync(uint n_workers, const char* name)
28163
322d55d167be 8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents: 27880
diff changeset
   356
  : _monitor(Mutex::safepoint, name, true, Monitor::_safepoint_check_never),
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   357
    _n_workers(n_workers), _n_completed(0), _should_reset(false), _aborted(false) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   360
void WorkGangBarrierSync::set_n_workers(uint n_workers) {
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   361
  _n_workers    = n_workers;
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   362
  _n_completed  = 0;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   363
  _should_reset = false;
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   364
  _aborted      = false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   367
bool WorkGangBarrierSync::enter() {
54645
05aaccf7d558 8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents: 54623
diff changeset
   368
  MonitorLocker ml(monitor(), Mutex::_no_safepoint_check_flag);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   369
  if (should_reset()) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   370
    // The should_reset() was set and we are the first worker to enter
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   371
    // the sync barrier. We will zero the n_completed() count which
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   372
    // effectively resets the barrier.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   373
    zero_completed();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   374
    set_should_reset(false);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   375
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  inc_completed();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  if (n_completed() == n_workers()) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   378
    // At this point we would like to reset the barrier to be ready in
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   379
    // case it is used again. However, we cannot set n_completed() to
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   380
    // 0, even after the notify_all(), given that some other workers
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   381
    // might still be waiting for n_completed() to become ==
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   382
    // n_workers(). So, if we set n_completed() to 0, those workers
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   383
    // will get stuck (as they will wake up, see that n_completed() !=
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   384
    // n_workers() and go back to sleep). Instead, we raise the
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   385
    // should_reset() flag and the barrier will be reset the first
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   386
    // time a worker enters it again.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   387
    set_should_reset(true);
54645
05aaccf7d558 8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents: 54623
diff changeset
   388
    ml.notify_all();
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   389
  } else {
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   390
    while (n_completed() != n_workers() && !aborted()) {
54645
05aaccf7d558 8222988: Use MonitorLocker rather than MutexLocker when wait/notify used
coleenp
parents: 54623
diff changeset
   391
      ml.wait();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  }
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   394
  return !aborted();
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   395
}
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   396
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   397
void WorkGangBarrierSync::abort() {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 53103
diff changeset
   398
  MutexLocker x(monitor(), Mutex::_no_safepoint_check_flag);
24468
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   399
  set_aborted();
cb71997b6484 8040803: G1: Concurrent mark hangs when mark stack overflows
pliden
parents: 17376
diff changeset
   400
  monitor()->notify_all();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
// SubTasksDone functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   405
SubTasksDone::SubTasksDone(uint n) :
51332
c25572739e7c 8208669: GC changes to allow enabling -Wreorder
tschatzl
parents: 48955
diff changeset
   406
  _tasks(NULL), _n_tasks(n), _threads_completed(0) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11396
diff changeset
   407
  _tasks = NEW_C_HEAP_ARRAY(uint, n, mtInternal);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
bool SubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  return _tasks != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
void SubTasksDone::clear() {
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   416
  for (uint i = 0; i < _n_tasks; i++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
    _tasks[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  _threads_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  _claimed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
51598
c88019b32bc4 8210119: Rename SubTasksDone::is_task_claimed
kbarrett
parents: 51546
diff changeset
   425
bool SubTasksDone::try_claim_task(uint t) {
29580
a67a581cfe11 8073315: Enable gcc -Wtype-limits and fix upcoming issues.
goetz
parents: 29464
diff changeset
   426
  assert(t < _n_tasks, "bad task id.");
11396
917d8673b5ef 7121618: Change type of number of GC workers to unsigned int.
jmasa
parents: 11174
diff changeset
   427
  uint old = _tasks[t];
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  if (old == 0) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 41279
diff changeset
   429
    old = Atomic::cmpxchg(1u, &_tasks[t], 0u);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  }
51598
c88019b32bc4 8210119: Rename SubTasksDone::is_task_claimed
kbarrett
parents: 51546
diff changeset
   431
  bool res = old == 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
#ifdef ASSERT
51598
c88019b32bc4 8210119: Rename SubTasksDone::is_task_claimed
kbarrett
parents: 51546
diff changeset
   433
  if (res) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
48955
e22914003cf0 8194691: Cleanup unnecessary casts in Atomic/OrderAccess uses
kbarrett
parents: 48123
diff changeset
   435
    Atomic::inc(&_claimed);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
30869
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   441
void SubTasksDone::all_tasks_completed(uint n_threads) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 41279
diff changeset
   442
  uint observed = _threads_completed;
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 41279
diff changeset
   443
  uint old;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
    old = observed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
    observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  } while (observed != old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  // If this was the last thread checking in, clear the tasks.
30869
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   449
  uint adjusted_thread_count = (n_threads == 0 ? 1 : n_threads);
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 41279
diff changeset
   450
  if (observed + 1 == adjusted_thread_count) {
30869
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   451
    clear();
d5cbedffb50b 8080111: Remove SubTaskDone::_n_threads
stefank
parents: 30764
diff changeset
   452
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
SubTasksDone::~SubTasksDone() {
58084
cddef3bde924 8230398: Remove NULL checks before FREE_C_HEAP_ARRAY
lkorinth
parents: 58083
diff changeset
   457
  FREE_C_HEAP_ARRAY(uint, _tasks);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
// *** SequentialSubTasksDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
void SequentialSubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  _n_tasks   = _n_claimed   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  _n_threads = _n_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
bool SequentialSubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  return _n_threads > 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
51598
c88019b32bc4 8210119: Rename SubTasksDone::is_task_claimed
kbarrett
parents: 51546
diff changeset
   471
bool SequentialSubTasksDone::try_claim_task(uint& t) {
41279
5a7c83da4a2d 8165860: WorkGroup classes are missing volatile specifiers for lock-free code
eosterlund
parents: 40655
diff changeset
   472
  t = _n_claimed;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  while (t < _n_tasks) {
46958
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 41279
diff changeset
   474
    uint res = Atomic::cmpxchg(t+1, &_n_claimed, t);
a13bd8c6b7a2 8186166: Generalize Atomic::cmpxchg with templates
eosterlund
parents: 41279
diff changeset
   475
    if (res == t) {
51598
c88019b32bc4 8210119: Rename SubTasksDone::is_task_claimed
kbarrett
parents: 51546
diff changeset
   476
      return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
    }
41279
5a7c83da4a2d 8165860: WorkGroup classes are missing volatile specifiers for lock-free code
eosterlund
parents: 40655
diff changeset
   478
    t = res;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  }
51598
c88019b32bc4 8210119: Rename SubTasksDone::is_task_claimed
kbarrett
parents: 51546
diff changeset
   480
  return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
bool SequentialSubTasksDone::all_tasks_completed() {
41279
5a7c83da4a2d 8165860: WorkGroup classes are missing volatile specifiers for lock-free code
eosterlund
parents: 40655
diff changeset
   484
  uint complete = _n_completed;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  while (true) {
41279
5a7c83da4a2d 8165860: WorkGroup classes are missing volatile specifiers for lock-free code
eosterlund
parents: 40655
diff changeset
   486
    uint res = Atomic::cmpxchg(complete+1, &_n_completed, complete);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    if (res == complete) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
    complete = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  if (complete+1 == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
}