hotspot/src/share/vm/utilities/taskqueue.cpp
author coleenp
Wed, 14 Jan 2009 20:14:19 -0500
changeset 1904 7aada8102b30
parent 1623 a0dd9009e992
child 2005 42075507972b
permissions -rw-r--r--
6793825: Missing include dependancies for GCC without predefined headers Summary: With predefined headers off for gcc, some .inline.hpp files aren't included to make definition visible for inline functions Reviewed-by: jcoomes, xlu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
1623
a0dd9009e992 6785258: Update copyright year
xdono
parents: 1407
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  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
 *
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/_taskqueue.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
bool TaskQueueSuper::peek() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
  return _bottom != _age.top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
int TaskQueueSetSuper::randomParkAndMiller(int *seed0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  const int a =      16807;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  const int m = 2147483647;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  const int q =     127773;  /* m div a */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  const int r =       2836;  /* m mod a */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  assert(sizeof(int) == 4, "I think this relies on that");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  int seed = *seed0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  int hi   = seed / q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  int lo   = seed % q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  int test = a * lo - r * hi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  if (test > 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    seed = test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    seed = test + m;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  *seed0 = seed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  return seed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
ParallelTaskTerminator::
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  _n_threads(n_threads),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _queue_set(queue_set),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  _offered_termination(0) {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
bool ParallelTaskTerminator::peek_in_queue_set() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  return _queue_set->peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
void ParallelTaskTerminator::yield() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  os::yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
void ParallelTaskTerminator::sleep(uint millis) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  os::sleep(Thread::current(), millis, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    68
bool
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    69
ParallelTaskTerminator::offer_termination(TerminatorTerminator* terminator) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  Atomic::inc(&_offered_termination);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  juint yield_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  while (true) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    if (_offered_termination == _n_threads) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      //inner_termination_loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      if (yield_count <= WorkStealingYieldsBeforeSleep) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
        yield_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
        yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
        if (PrintGCDetails && Verbose) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
         gclog_or_tty->print_cr("ParallelTaskTerminator::offer_termination() "
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
           "thread %d sleeps after %d yields",
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
           Thread::current(), yield_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
        yield_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
        // A sleep will cause this processor to seek work on another processor's
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
        // runqueue, if it has nothing else to run (as opposed to the yield
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        // which may only move the thread to the end of the this processor's
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        // runqueue).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        sleep(WorkStealingSleepMillis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    95
      if (peek_in_queue_set() ||
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
    96
          (terminator != NULL && terminator->should_exit_termination())) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
        Atomic::dec(&_offered_termination);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
void ParallelTaskTerminator::reset_for_reuse() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  if (_offered_termination != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    assert(_offered_termination == _n_threads,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
           "Terminator may still be in use");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    _offered_termination = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   112
bool RegionTaskQueueWithOverflow::is_empty() {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   113
  return (_region_queue.size() == 0) &&
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
         (_overflow_stack->length() == 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   117
bool RegionTaskQueueWithOverflow::stealable_is_empty() {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   118
  return _region_queue.size() == 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   121
bool RegionTaskQueueWithOverflow::overflow_is_empty() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  return _overflow_stack->length() == 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   125
void RegionTaskQueueWithOverflow::initialize() {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   126
  _region_queue.initialize();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  assert(_overflow_stack == 0, "Creating memory leak");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  _overflow_stack =
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   129
    new (ResourceObj::C_HEAP) GrowableArray<RegionTask>(10, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   132
void RegionTaskQueueWithOverflow::save(RegionTask t) {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   133
  if (TraceRegionTasksQueuing && Verbose) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    gclog_or_tty->print_cr("CTQ: save " PTR_FORMAT, t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  }
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   136
  if(!_region_queue.push(t)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    _overflow_stack->push(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   141
// Note that using this method will retrieve all regions
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
// that have been saved but that it will always check
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
// the overflow stack.  It may be more efficient to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
// check the stealable queue and the overflow stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
// separately.
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   146
bool RegionTaskQueueWithOverflow::retrieve(RegionTask& region_task) {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   147
  bool result = retrieve_from_overflow(region_task);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  if (!result) {
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   149
    result = retrieve_from_stealable_queue(region_task);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  }
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   151
  if (TraceRegionTasksQueuing && Verbose && result) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    gclog_or_tty->print_cr("  CTQ: retrieve " PTR_FORMAT, result);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   157
bool RegionTaskQueueWithOverflow::retrieve_from_stealable_queue(
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   158
                                   RegionTask& region_task) {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   159
  bool result = _region_queue.pop_local(region_task);
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   160
  if (TraceRegionTasksQueuing && Verbose) {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   161
    gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   166
bool
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   167
RegionTaskQueueWithOverflow::retrieve_from_overflow(RegionTask& region_task) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  bool result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  if (!_overflow_stack->is_empty()) {
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   170
    region_task = _overflow_stack->pop();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    result = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  } else {
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   173
    region_task = (RegionTask) NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    result = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   176
  if (TraceRegionTasksQueuing && Verbose) {
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1374
diff changeset
   177
    gclog_or_tty->print_cr("CTQ: retrieve_stealable " PTR_FORMAT, region_task);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
}