hotspot/src/share/vm/utilities/workgroup.cpp
author xdono
Wed, 02 Jul 2008 12:55:16 -0700
changeset 670 ddf3e9583f2f
parent 1 489c9b5090e2
child 1374 4c24294029a9
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_workgroup.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// Definitions of WorkGang methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
AbstractWorkGang::AbstractWorkGang(const char* name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
                                   bool  are_GC_threads) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  _name(name),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  _are_GC_threads(are_GC_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  // Other initialization.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  _monitor = new Monitor(/* priority */       Mutex::leaf,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
                         /* name */           "WorkGroup monitor",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
                         /* allow_vm_block */ are_GC_threads);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  assert(monitor() != NULL, "Failed to allocate monitor");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  _terminate = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  _sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  _started_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
WorkGang::WorkGang(const char* name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
                   int           workers,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
                   bool          are_GC_threads) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  AbstractWorkGang(name, are_GC_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  // Save arguments.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  _total_workers = workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    tty->print_cr("Constructing work gang %s with %d threads", name, workers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  _gang_workers = NEW_C_HEAP_ARRAY(GangWorker*, workers);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  assert(gang_workers() != NULL, "Failed to allocate gang workers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  for (int worker = 0; worker < total_workers(); worker += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    GangWorker* new_worker = new GangWorker(this, worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    assert(new_worker != NULL, "Failed to allocate GangWorker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    _gang_workers[worker] = new_worker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    if (new_worker == NULL || !os::create_thread(new_worker, os::pgc_thread))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
      vm_exit_out_of_memory(0, "Cannot create worker GC thread. Out of system resources.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    if (!DisableStartThread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
      os::start_thread(new_worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
AbstractWorkGang::~AbstractWorkGang() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    tty->print_cr("Destructing work gang %s", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  stop();   // stop all the workers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  for (int worker = 0; worker < total_workers(); worker += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    delete gang_worker(worker);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  delete gang_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  delete monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
GangWorker* AbstractWorkGang::gang_worker(int i) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  // Array index bounds checking.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  GangWorker* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  assert(gang_workers() != NULL, "No workers for indexing");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  assert(((i >= 0) && (i < total_workers())), "Worker index out of bounds");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  result = _gang_workers[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  assert(result != NULL, "Indexing to null worker");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
void WorkGang::run_task(AbstractGangTask* task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // This thread is executed by the VM thread which does not block
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // on ordinary MutexLocker's.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    tty->print_cr("Running work gang %s task %s", name(), task->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // Tell all the workers to run a task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  assert(task != NULL, "Running a null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // Initialize.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  _task = task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  _sequence_number += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  _started_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  _finished_workers = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  // Tell the workers to get to work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  // Wait for them to be finished
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  while (finished_workers() < total_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
      tty->print_cr("Waiting in work gang %s: %d/%d finished sequence %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
                    name(), finished_workers(), total_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
                    _sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    tty->print_cr("/nFinished work gang %s: %d/%d sequence %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
                  name(), finished_workers(), total_workers(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                  _sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
void AbstractWorkGang::stop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // Tell all workers to terminate, then wait for them to become inactive.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    tty->print_cr("Stopping work gang %s task %s", name(), task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  _task = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  _terminate = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  while (finished_workers() < total_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      tty->print_cr("Waiting in work gang %s: %d/%d finished",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
                    name(), finished_workers(), total_workers());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  }
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 AbstractWorkGang::internal_worker_poll(WorkData* data) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  assert(monitor()->owned_by_self(), "worker_poll is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  assert(data != NULL, "worker data is null");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  data->set_terminate(terminate());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  data->set_task(task());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  data->set_sequence_number(sequence_number());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
void AbstractWorkGang::internal_note_start() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  _started_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
void AbstractWorkGang::internal_note_finish() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  assert(monitor()->owned_by_self(), "note_finish is an internal method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  _finished_workers += 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
void AbstractWorkGang::print_worker_threads_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  uint    num_thr = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    gang_worker(i)->print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
void AbstractWorkGang::threads_do(ThreadClosure* tc) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  assert(tc != NULL, "Null ThreadClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  uint num_thr = total_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  for (uint i = 0; i < num_thr; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    tc->do_thread(gang_worker(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
// GangWorker methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
GangWorker::GangWorker(AbstractWorkGang* gang, uint id) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  _gang = gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  set_id(id);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  set_name("Gang worker#%d (%s)", id, gang->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
void GangWorker::run() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
void GangWorker::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  this->initialize_thread_local_storage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  assert(_gang != NULL, "No gang to run in");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  os::set_priority(this, NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    tty->print_cr("Running gang worker for gang %s id %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
                  gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  // The VM thread should not execute here because MutexLocker's are used
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  // as (opposed to MutexLockerEx's).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  assert(!Thread::current()->is_VM_thread(), "VM thread should not be part"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
         " of a work gang");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
void GangWorker::loop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  int previous_sequence_number = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  Monitor* gang_monitor = gang()->monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  for ( ; /* !terminate() */; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    WorkData data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    int part;  // Initialized below.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
      // Grab the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
      MutexLocker ml(gang_monitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      // Wait for something to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
      // Polling outside the while { wait } avoids missed notifies
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
      // in the outer loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
      if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
        tty->print("Polled outside for work in gang %s worker %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
                   gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
        tty->print("  terminate: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
                   data.terminate() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
        tty->print("  sequence: %d (prev: %d)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
                   data.sequence_number(), previous_sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
        if (data.task() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
          tty->print("  task: %s", data.task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
          tty->print("  task: NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
        tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      for ( ; /* break or return */; ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
        // Terminate if requested.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
        if (data.terminate()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
          gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
          gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
          return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
        // Check for new work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
        if ((data.task() != NULL) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
            (data.sequence_number() != previous_sequence_number)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
          gang()->internal_note_start();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
          gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
          part = gang()->started_workers() - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
        // Nothing to do.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
        gang_monitor->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
        gang()->internal_worker_poll(&data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
        if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
          tty->print("Polled inside for work in gang %s worker %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
                     gang()->name(), id());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
          tty->print("  terminate: %s",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
                     data.terminate() ? "true" : "false");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
          tty->print("  sequence: %d (prev: %d)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
                     data.sequence_number(), previous_sequence_number);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
          if (data.task() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
            tty->print("  task: %s", data.task()->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
            tty->print("  task: NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
          tty->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
      // Drop gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
      tty->print("Work for work gang %s id %d task %s part %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
                 gang()->name(), id(), data.task()->name(), part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    assert(data.task() != NULL, "Got null task");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    data.task()->work(part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
      if (TraceWorkGang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
        tty->print("Finish for work gang %s id %d task %s part %d",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
                   gang()->name(), id(), data.task()->name(), part);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
      // Grab the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
      MutexLocker ml(gang_monitor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
      gang()->internal_note_finish();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      // Tell the gang you are done.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      gang_monitor->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
      // Drop the gang mutex.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    previous_sequence_number = data.sequence_number();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
bool GangWorker::is_GC_task_thread() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  return gang()->are_GC_threads();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
void GangWorker::print_on(outputStream* st) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  st->print("\"%s\" ", name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  Thread::print_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  st->cr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
// Printing methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
const char* AbstractWorkGang::name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
const char* AbstractGangTask::name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  return _name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
#endif /* PRODUCT */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
// *** WorkGangBarrierSync
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
WorkGangBarrierSync::WorkGangBarrierSync()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  : _monitor(Mutex::safepoint, "work gang barrier sync", true),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
    _n_workers(0), _n_completed(0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
WorkGangBarrierSync::WorkGangBarrierSync(int n_workers, const char* name)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  : _monitor(Mutex::safepoint, name, true),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    _n_workers(n_workers), _n_completed(0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
void WorkGangBarrierSync::set_n_workers(int n_workers) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  _n_workers   = n_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
  _n_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
void WorkGangBarrierSync::enter() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  MutexLockerEx x(monitor(), Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  inc_completed();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  if (n_completed() == n_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    monitor()->notify_all();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
  else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    while (n_completed() != n_workers()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      monitor()->wait(/* no_safepoint_check */ true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
// SubTasksDone functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
SubTasksDone::SubTasksDone(int n) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  _n_tasks(n), _n_threads(1), _tasks(NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  _tasks = NEW_C_HEAP_ARRAY(jint, n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  guarantee(_tasks != NULL, "alloc failure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
bool SubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  return _tasks != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
void SubTasksDone::set_par_threads(int t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  assert(_claimed == 0 || _threads_completed == _n_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
         "should not be called while tasks are being processed!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  _n_threads = (t == 0 ? 1 : t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
void SubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
  for (int i = 0; i < _n_tasks; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    _tasks[i] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  _threads_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  _claimed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
bool SubTasksDone::is_task_claimed(int t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  assert(0 <= t && t < _n_tasks, "bad task id.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  jint old = _tasks[t];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  if (old == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    old = Atomic::cmpxchg(1, &_tasks[t], 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  assert(_tasks[t] == 1, "What else?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
  bool res = old != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  if (!res) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    assert(_claimed < _n_tasks, "Too many tasks claimed; missing clear?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
    Atomic::inc(&_claimed);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
  return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
void SubTasksDone::all_tasks_completed() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  jint observed = _threads_completed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  jint old;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  do {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
    old = observed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    observed = Atomic::cmpxchg(old+1, &_threads_completed, old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  } while (observed != old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
  // If this was the last thread checking in, clear the tasks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  if (observed+1 == _n_threads) clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
SubTasksDone::~SubTasksDone() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
  if (_tasks != NULL) FREE_C_HEAP_ARRAY(jint, _tasks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
// *** SequentialSubTasksDone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
void SequentialSubTasksDone::clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
  _n_tasks   = _n_claimed   = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
  _n_threads = _n_completed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
bool SequentialSubTasksDone::valid() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  return _n_threads > 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
bool SequentialSubTasksDone::is_task_claimed(int& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  jint* n_claimed_ptr = &_n_claimed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  while (t < _n_tasks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
    jint res = Atomic::cmpxchg(t+1, n_claimed_ptr, t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
    if (res == t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
    t = *n_claimed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
bool SequentialSubTasksDone::all_tasks_completed() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  jint* n_completed_ptr = &_n_completed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  jint  complete        = *n_completed_ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
    jint res = Atomic::cmpxchg(complete+1, n_completed_ptr, complete);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
    if (res == complete) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
    complete = res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  if (complete+1 == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
}