hotspot/src/share/vm/gc/shared/taskqueue.cpp
author david
Tue, 29 Sep 2015 11:02:08 +0200
changeset 33105 294e48b4f704
parent 32606 fdaa30d06ada
child 35061 be6025ebffea
permissions -rw-r--r--
8080775: Better argument formatting for assert() and friends Reviewed-by: kbarrett, pliden
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
30585
12f312d694cd 6407976: GC worker number should be unsigned
eistepan
parents: 30584
diff changeset
     2
 * Copyright (c) 2001, 2015, 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: 5076
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5076
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: 5076
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"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 30585
diff changeset
    26
#include "gc/shared/taskqueue.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    27
#include "oops/oop.inline.hpp"
25351
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 24424
diff changeset
    28
#include "runtime/atomic.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    29
#include "runtime/os.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 10565
diff changeset
    30
#include "runtime/thread.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    31
#include "utilities/debug.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6759
diff changeset
    32
#include "utilities/stack.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
    34
#ifdef TRACESPINNING
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
    35
uint ParallelTaskTerminator::_total_yields = 0;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
    36
uint ParallelTaskTerminator::_total_spins = 0;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
    37
uint ParallelTaskTerminator::_total_peeks = 0;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
    38
#endif
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
    39
6067
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    40
#if TASKQUEUE_STATS
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    41
const char * const TaskQueueStats::_names[last_stat_id] = {
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    42
  "qpush", "qpop", "qpop-s", "qattempt", "qsteal", "opush", "omax"
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    43
};
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    44
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    45
TaskQueueStats & TaskQueueStats::operator +=(const TaskQueueStats & addend)
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    46
{
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    47
  for (unsigned int i = 0; i < last_stat_id; ++i) {
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    48
    _stats[i] += addend._stats[i];
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    49
  }
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    50
  return *this;
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    51
}
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    52
6067
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    53
void TaskQueueStats::print_header(unsigned int line, outputStream* const stream,
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    54
                                  unsigned int width)
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    55
{
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    56
  // Use a width w: 1 <= w <= max_width
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    57
  const unsigned int max_width = 40;
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    58
  const unsigned int w = MAX2(MIN2(width, max_width), 1U);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    59
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    60
  if (line == 0) { // spaces equal in width to the header
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    61
    const unsigned int hdr_width = w * last_stat_id + last_stat_id - 1;
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    62
    stream->print("%*s", hdr_width, " ");
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    63
  } else if (line == 1) { // labels
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    64
    stream->print("%*s", w, _names[0]);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    65
    for (unsigned int i = 1; i < last_stat_id; ++i) {
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    66
      stream->print(" %*s", w, _names[i]);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    67
    }
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    68
  } else if (line == 2) { // dashed lines
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    69
    char dashes[max_width + 1];
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    70
    memset(dashes, '-', w);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    71
    dashes[w] = '\0';
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    72
    stream->print("%s", dashes);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    73
    for (unsigned int i = 1; i < last_stat_id; ++i) {
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    74
      stream->print(" %s", dashes);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    75
    }
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    76
  }
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    77
}
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    78
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    79
void TaskQueueStats::print(outputStream* stream, unsigned int width) const
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    80
{
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    81
  #define FMT SIZE_FORMAT_W(*)
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    82
  stream->print(FMT, width, _stats[0]);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    83
  for (unsigned int i = 1; i < last_stat_id; ++i) {
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    84
    stream->print(" " FMT, width, _stats[i]);
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    85
  }
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    86
  #undef FMT
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
    87
}
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    88
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    89
#ifdef ASSERT
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    90
// Invariants which should hold after a TaskQueue has been emptied and is
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    91
// quiescent; they do not hold at arbitrary times.
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    92
void TaskQueueStats::verify() const
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    93
{
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    94
  assert(get(push) == get(pop) + get(steal),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
    95
         "push=" SIZE_FORMAT " pop=" SIZE_FORMAT " steal=" SIZE_FORMAT,
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
    96
         get(push), get(pop), get(steal));
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
    97
  assert(get(pop_slow) <= get(pop),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
    98
         "pop_slow=" SIZE_FORMAT " pop=" SIZE_FORMAT,
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
    99
         get(pop_slow), get(pop));
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
   100
  assert(get(steal) <= get(steal_attempt),
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
   101
         "steal=" SIZE_FORMAT " steal_attempt=" SIZE_FORMAT,
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
   102
         get(steal), get(steal_attempt));
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
   103
  assert(get(overflow) == 0 || get(push) != 0,
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
   104
         "overflow=" SIZE_FORMAT " push=" SIZE_FORMAT,
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
   105
         get(overflow), get(push));
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
   106
  assert(get(overflow_max_len) == 0 || get(overflow) != 0,
33105
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
   107
         "overflow_max_len=" SIZE_FORMAT " overflow=" SIZE_FORMAT,
294e48b4f704 8080775: Better argument formatting for assert() and friends
david
parents: 32606
diff changeset
   108
         get(overflow_max_len), get(overflow));
6251
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
   109
}
90e562b9f1cc 6966222: G1: simplify TaskQueue overflow handling
jcoomes
parents: 6067
diff changeset
   110
#endif // ASSERT
6067
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
   111
#endif // TASKQUEUE_STATS
8bfddf73fc04 6962947: shared TaskQueue statistics
jcoomes
parents: 5918
diff changeset
   112
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  const int a =      16807;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  const int m = 2147483647;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  const int q =     127773;  /* m div a */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  const int r =       2836;  /* m mod a */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  assert(sizeof(int) == 4, "I think this relies on that");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  int seed = *seed0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  int hi   = seed / q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  int lo   = seed % q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  int test = a * lo - r * hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  if (test > 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    seed = test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    seed = test + m;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  *seed0 = seed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  return seed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
ParallelTaskTerminator::
30585
12f312d694cd 6407976: GC worker number should be unsigned
eistepan
parents: 30584
diff changeset
   132
ParallelTaskTerminator(uint n_threads, TaskQueueSetSuper* queue_set) :
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  _n_threads(n_threads),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  _queue_set(queue_set),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  _offered_termination(0) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
bool ParallelTaskTerminator::peek_in_queue_set() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  return _queue_set->peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
void ParallelTaskTerminator::yield() {
3262
30d1c247fc25 6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents: 2105
diff changeset
   142
  assert(_offered_termination <= _n_threads, "Invariant");
25477
7dad9f95fd31 8047714: Fix for JDK-6546236 made Solaris os::yield() a no-op
fparain
parents: 25351
diff changeset
   143
  os::naked_yield();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
void ParallelTaskTerminator::sleep(uint millis) {
3262
30d1c247fc25 6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents: 2105
diff changeset
   147
  assert(_offered_termination <= _n_threads, "Invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  os::sleep(Thread::current(), millis, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   151
bool
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   152
ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 6251
diff changeset
   153
  assert(_n_threads > 0, "Initialization is incorrect");
3262
30d1c247fc25 6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents: 2105
diff changeset
   154
  assert(_offered_termination < _n_threads, "Invariant");
30585
12f312d694cd 6407976: GC worker number should be unsigned
eistepan
parents: 30584
diff changeset
   155
  Atomic::inc((int *)&_offered_termination);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1623
diff changeset
   157
  uint yield_count = 0;
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   158
  // Number of hard spin loops done since last yield
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   159
  uint hard_spin_count = 0;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   160
  // Number of iterations in the hard spin loop.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   161
  uint hard_spin_limit = WorkStealingHardSpins;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   162
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   163
  // If WorkStealingSpinToYieldRatio is 0, no hard spinning is done.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   164
  // If it is greater than 0, then start with a small number
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   165
  // of spins and increase number with each turn at spinning until
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   166
  // the count of hard spins exceeds WorkStealingSpinToYieldRatio.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   167
  // Then do a yield() call and start spinning afresh.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   168
  if (WorkStealingSpinToYieldRatio > 0) {
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   169
    hard_spin_limit = WorkStealingHardSpins >> WorkStealingSpinToYieldRatio;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   170
    hard_spin_limit = MAX2(hard_spin_limit, 1U);
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   171
  }
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   172
  // Remember the initial spin limit.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   173
  uint hard_spin_start = hard_spin_limit;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   174
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   175
  // Loop waiting for all threads to offer termination or
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   176
  // more work.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  while (true) {
3262
30d1c247fc25 6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents: 2105
diff changeset
   178
    assert(_offered_termination <= _n_threads, "Invariant");
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   179
    // Are all threads offering termination?
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    if (_offered_termination == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    } else {
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   183
      // Look for more work.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   184
      // Periodically sleep() instead of yield() to give threads
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   185
      // waiting on the cores the chance to grab this code
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      if (yield_count <= WorkStealingYieldsBeforeSleep) {
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   187
        // Do a yield or hardspin.  For purposes of deciding whether
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   188
        // to sleep, count this as a yield.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
        yield_count++;
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   190
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   191
        // Periodically call yield() instead spinning
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   192
        // After WorkStealingSpinToYieldRatio spins, do a yield() call
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   193
        // and reset the counts and starting limit.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   194
        if (hard_spin_count > WorkStealingSpinToYieldRatio) {
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   195
          yield();
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   196
          hard_spin_count = 0;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   197
          hard_spin_limit = hard_spin_start;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   198
#ifdef TRACESPINNING
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   199
          _total_yields++;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   200
#endif
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   201
        } else {
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   202
          // Hard spin this time
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   203
          // Increase the hard spinning period but only up to a limit.
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   204
          hard_spin_limit = MIN2(2*hard_spin_limit,
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   205
                                 (uint) WorkStealingHardSpins);
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   206
          for (uint j = 0; j < hard_spin_limit; j++) {
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   207
            SpinPause();
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   208
          }
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   209
          hard_spin_count++;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   210
#ifdef TRACESPINNING
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   211
          _total_spins++;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   212
#endif
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   213
        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
        if (PrintGCDetails && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
         gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
30584
821c80d31b43 8079337: Format string issues in workgroup.cpp and taskqueue.cpp
david
parents: 25477
diff changeset
   217
           "thread " PTR_FORMAT " sleeps after %u yields",
821c80d31b43 8079337: Format string issues in workgroup.cpp and taskqueue.cpp
david
parents: 25477
diff changeset
   218
           p2i(Thread::current()), yield_count);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
        yield_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
        // A sleep will cause this processor to seek work on another processor's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
        // runqueue, if it has nothing else to run (as opposed to the yield
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
        // which may only move the thread to the end of the this processor's
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        // runqueue).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
        sleep(WorkStealingSleepMillis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   228
#ifdef TRACESPINNING
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   229
      _total_peeks++;
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   230
#endif
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   231
      if (peek_in_queue_set() ||
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   232
          (terminator != NULL && terminator->should_exit_termination())) {
30585
12f312d694cd 6407976: GC worker number should be unsigned
eistepan
parents: 30584
diff changeset
   233
        Atomic::dec((int *)&_offered_termination);
3262
30d1c247fc25 6700789: G1: Enable use of compressed oops with G1 heaps
ysr
parents: 2105
diff changeset
   234
        assert(_offered_termination < _n_threads, "Invariant");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   241
#ifdef TRACESPINNING
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   242
void ParallelTaskTerminator::print_termination_counts() {
23858
dae377f5a7c7 8039244: Don't use UINT32_FORMAT and INT32_FORMAT when printing uints and ints in the GC code
stefank
parents: 15228
diff changeset
   243
  gclog_or_tty->print_cr("ParallelTaskTerminator Total yields: %u"
dae377f5a7c7 8039244: Don't use UINT32_FORMAT and INT32_FORMAT when printing uints and ints in the GC code
stefank
parents: 15228
diff changeset
   244
    " Total spins: %u Total peeks: %u",
2010
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   245
    total_yields(),
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   246
    total_spins(),
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   247
    total_peeks());
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   248
}
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   249
#endif
c13462bbad17 6690928: Use spinning in combination with yields for workstealing termination.
jmasa
parents: 2005
diff changeset
   250
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
void ParallelTaskTerminator::reset_for_reuse() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  if (_offered_termination != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    assert(_offered_termination == _n_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
           "Terminator may still be in use");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    _offered_termination = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
5076
8b74a4b60b31 4396719: Mark Sweep stack overflow on deeply nested Object arrays
jcoomes
parents: 3262
diff changeset
   259
#ifdef ASSERT
8b74a4b60b31 4396719: Mark Sweep stack overflow on deeply nested Object arrays
jcoomes
parents: 3262
diff changeset
   260
bool ObjArrayTask::is_valid() const {
32606
fdaa30d06ada 8129417: Oop iteration clean-up to remove oop_ms_follow_contents
sjohanss
parents: 30764
diff changeset
   261
  return _obj != NULL && _obj->is_objArray() && _index >= 0 &&
fdaa30d06ada 8129417: Oop iteration clean-up to remove oop_ms_follow_contents
sjohanss
parents: 30764
diff changeset
   262
      _index < objArrayOop(_obj)->length();
5076
8b74a4b60b31 4396719: Mark Sweep stack overflow on deeply nested Object arrays
jcoomes
parents: 3262
diff changeset
   263
}
8b74a4b60b31 4396719: Mark Sweep stack overflow on deeply nested Object arrays
jcoomes
parents: 3262
diff changeset
   264
#endif // ASSERT
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 6251
diff changeset
   265
30585
12f312d694cd 6407976: GC worker number should be unsigned
eistepan
parents: 30584
diff changeset
   266
void ParallelTaskTerminator::reset_for_reuse(uint n_threads) {
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 6251
diff changeset
   267
  reset_for_reuse();
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 6251
diff changeset
   268
  _n_threads = n_threads;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 6251
diff changeset
   269
}