hotspot/src/share/vm/utilities/taskqueue.hpp
author ysr
Fri, 30 Jan 2009 14:17:52 -0800
changeset 2005 42075507972b
parent 1407 9006b01ba3fd
child 2010 c13462bbad17
permissions -rw-r--r--
6787254: Work queue capacity can be increased substantially on some platforms Summary: Increased the default and maximum size of the CMS marking stack and the size of the parallel workers' work queues in 64-bit mode. The latter was accomplished by an increase in the width of the Taskqueue's Age struct and its Tag field in 64-bit mode. Reviewed-by: jmasa, tonyp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
670
ddf3e9583f2f 6719955: Update copyright year
xdono
parents: 360
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
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    25
#ifdef LP64
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    26
typedef juint TAG_TYPE;
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    27
// for a taskqueue size of 4M
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    28
#define LOG_TASKQ_SIZE 22
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    29
#else
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    30
typedef jushort TAG_TYPE;
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    31
// for a taskqueue size of 16K
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    32
#define LOG_TASKQ_SIZE 14
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    33
#endif
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    34
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
class TaskQueueSuper: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  // The first free element after the last one pushed (mod _n).
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    38
  volatile uint _bottom;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  // log2 of the size of the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  enum SomeProtectedConstants {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    42
    Log_n = LOG_TASKQ_SIZE
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  };
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    44
#undef LOG_TASKQ_SIZE
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  // Size of the queue.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    47
  uint n() { return (1 << Log_n); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  // For computing "x mod n" efficiently.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    49
  uint n_mod_mask() { return n() - 1; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  struct Age {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    52
    TAG_TYPE _top;
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    53
    TAG_TYPE _tag;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    55
    TAG_TYPE tag() const { return _tag; }
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    56
    TAG_TYPE top() const { return _top; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    Age() { _tag = 0; _top = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    friend bool operator ==(const Age& a1, const Age& a2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
      return a1.tag() == a2.tag() && a1.top() == a2.top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  Age _age;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  // These make sure we do single atomic reads and writes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  Age get_age() {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    67
    uint res = *(volatile uint*)(&_age);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    return *(Age*)(&res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  void set_age(Age a) {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    71
    *(volatile uint*)(&_age) = *(uint*)(&a);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    74
  TAG_TYPE get_top() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    return get_age().top();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  // These both operate mod _n.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    79
  uint increment_index(uint ind) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    return (ind + 1) & n_mod_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  }
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    82
  uint decrement_index(uint ind) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    return (ind - 1) & n_mod_mask();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  // Returns a number in the range [0.._n).  If the result is "n-1", it
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // should be interpreted as 0.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    88
  uint dirty_size(uint bot, uint top) {
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    89
    return ((int)bot - (int)top) & n_mod_mask();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // Returns the size corresponding to the given "bot" and "top".
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    93
  uint size(uint bot, uint top) {
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
    94
    uint sz = dirty_size(bot, top);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    // Has the queue "wrapped", so that bottom is less than top?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    // There's a complicated special case here.  A pair of threads could
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    // perform pop_local and pop_global operations concurrently, starting
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    // from a state in which _bottom == _top+1.  The pop_local could
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    // succeed in decrementing _bottom, and the pop_global in incrementing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    // _top (in which case the pop_global will be awarded the contested
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    // queue element.)  The resulting state must be interpreted as an empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    // queue.  (We only need to worry about one such event: only the queue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    // owner performs pop_local's, and several concurrent threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    // attempting to perform the pop_global will all perform the same CAS,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    // and only one can succeed.  Any stealing thread that reads after
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   106
    // either the increment or decrement will see an empty queue, and will
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    // not join the competitors.  The "sz == -1 || sz == _n-1" state will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    // not be modified  by concurrent queues, so the owner thread can reset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    // the state to _bottom == top so subsequent pushes will be performed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    // normally.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    if (sz == (n()-1)) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    else return sz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  TaskQueueSuper() : _bottom(0), _age() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // Return "true" if the TaskQueue contains any tasks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  bool peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  // Return an estimate of the number of elements in the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  // The "careful" version admits the possibility of pop_local/pop_global
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // races.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   124
  uint size() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    return size(_bottom, get_top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   128
  uint dirty_size() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    return dirty_size(_bottom, get_top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   132
  void set_empty() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   133
    _bottom = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   134
    _age = Age();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   135
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   136
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  // Maximum number of elements allowed in the queue.  This is two less
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // than the actual queue size, for somewhat complicated reasons.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   139
  uint max_elems() { return n() - 2; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
template<class E> class GenericTaskQueue: public TaskQueueSuper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  // Slow paths for push, pop_local.  (pop_global has no fast path.)
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   146
  bool push_slow(E t, uint dirty_n_elems);
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   147
  bool pop_local_slow(uint localBot, Age oldAge);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  // Initializes the queue to empty.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  GenericTaskQueue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  // Push the task "t" on the queue.  Returns "false" iff the queue is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  // full.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  inline bool push(E t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  // If succeeds in claiming a task (from the 'local' end, that is, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  // most recently pushed task), returns "true" and sets "t" to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  // Otherwise, the queue is empty and returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  inline bool pop_local(E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  // If succeeds in claiming a task (from the 'global' end, that is, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  // least recently pushed task), returns "true" and sets "t" to that task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // Otherwise, the queue is empty and returns false.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  bool pop_global(E& t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  // Delete any resource associated with the queue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  ~GenericTaskQueue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   172
  // apply the closure to all elements in the task queue
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   173
  void oops_do(OopClosure* f);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   174
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  // Element array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  volatile E* _elems;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
GenericTaskQueue<E>::GenericTaskQueue():TaskQueueSuper() {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   182
  assert(sizeof(Age) == sizeof(int), "Depends on this.");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
void GenericTaskQueue<E>::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  _elems = NEW_C_HEAP_ARRAY(E, n());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  guarantee(_elems != NULL, "Allocation failed.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
template<class E>
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   192
void GenericTaskQueue<E>::oops_do(OopClosure* f) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   193
  // tty->print_cr("START OopTaskQueue::oops_do");
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   194
  uint iters = size();
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   195
  uint index = _bottom;
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   196
  for (uint i = 0; i < iters; ++i) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   197
    index = decrement_index(index);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   198
    // tty->print_cr("  doing entry %d," INTPTR_T " -> " INTPTR_T,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   199
    //            index, &_elems[index], _elems[index]);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   200
    E* t = (E*)&_elems[index];      // cast away volatility
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   201
    oop* p = (oop*)t;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   202
    assert((*t)->is_oop_or_null(), "Not an oop or null");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   203
    f->do_oop(p);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   204
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   205
  // tty->print_cr("END OopTaskQueue::oops_do");
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   206
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   207
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   208
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   209
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   210
bool GenericTaskQueue<E>::push_slow(E t, uint dirty_n_elems) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  if (dirty_n_elems == n() - 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
    // Actually means 0, so do the push.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   213
    uint localBot = _bottom;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    _bottom = increment_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  } else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
bool GenericTaskQueue<E>::
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   223
pop_local_slow(uint localBot, Age oldAge) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  // This queue was observed to contain exactly one element; either this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  // thread will claim it, or a competing "pop_global".  In either case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  // the queue will be logically empty afterwards.  Create a new Age value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  // that represents the empty queue for the given value of "_bottom".  (We
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  // must also increment "tag" because of the case where "bottom == 1",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  // "top == 0".  A pop_global could read the queue element in that case,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  // then have the owner thread do a pop followed by another push.  Without
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  // the incrementing of "tag", the pop_global's CAS could succeed,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  // allowing it to believe it has claimed the stale element.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  Age newAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  newAge._top = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  newAge._tag = oldAge.tag() + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  // Perhaps a competing pop_global has already incremented "top", in which
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  // case it wins the element.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  if (localBot == oldAge.top()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    Age tempAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    // No competing pop_global has yet incremented "top"; we'll try to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    // install new_age, thus claiming the element.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   242
    assert(sizeof(Age) == sizeof(int), "Assumption about CAS unit.");
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   243
    *(uint*)&tempAge = Atomic::cmpxchg(*(uint*)&newAge, (volatile uint*)&_age, *(uint*)&oldAge);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    if (tempAge == oldAge) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      // We win.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
      assert(dirty_size(localBot, get_top()) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
             "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  // We fail; a completing pop_global gets the element.  But the queue is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  // empty (and top is greater than bottom.)  Fix this representation of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  // the empty queue to become the canonical one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  set_age(newAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  assert(dirty_size(localBot, get_top()) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
         "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
bool GenericTaskQueue<E>::pop_global(E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  Age newAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  Age oldAge = get_age();
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   264
  uint localBot = _bottom;
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   265
  uint n_elems = size(localBot, oldAge.top());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
  if (n_elems == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  t = _elems[oldAge.top()];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  newAge = oldAge;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  newAge._top = increment_index(newAge.top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  if ( newAge._top == 0 ) newAge._tag++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  Age resAge;
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   274
  *(uint*)&resAge = Atomic::cmpxchg(*(uint*)&newAge, (volatile uint*)&_age, *(uint*)&oldAge);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  // Note that using "_bottom" here might fail, since a pop_local might
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  // have decremented it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  assert(dirty_size(localBot, newAge._top) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
         "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  return (resAge == oldAge);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
GenericTaskQueue<E>::~GenericTaskQueue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  FREE_C_HEAP_ARRAY(E, _elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
// Inherits the typedef of "Task" from above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
class TaskQueueSetSuper: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  static int randomParkAndMiller(int* seed0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  // Returns "true" if some TaskQueue in the set contains a task.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  virtual bool peek() = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
template<class E> class GenericTaskQueueSet: public TaskQueueSetSuper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
private:
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   298
  uint _n;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  GenericTaskQueue<E>** _queues;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  GenericTaskQueueSet(int n) : _n(n) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    typedef GenericTaskQueue<E>* GenericTaskQueuePtr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    _queues = NEW_C_HEAP_ARRAY(GenericTaskQueuePtr, n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    guarantee(_queues != NULL, "Allocation failure.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    for (int i = 0; i < n; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      _queues[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   311
  bool steal_1_random(uint queue_num, int* seed, E& t);
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   312
  bool steal_best_of_2(uint queue_num, int* seed, E& t);
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   313
  bool steal_best_of_all(uint queue_num, int* seed, E& t);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   315
  void register_queue(uint i, GenericTaskQueue<E>* q);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   317
  GenericTaskQueue<E>* queue(uint n);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  // The thread with queue number "queue_num" (and whose random number seed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  // is at "seed") is trying to steal a task from some other queue.  (It
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  // may try several queues, according to some configuration parameter.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  // If some steal succeeds, returns "true" and sets "t" the stolen task,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  // otherwise returns false.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   324
  bool steal(uint queue_num, int* seed, E& t);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  bool peek();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   330
void GenericTaskQueueSet<E>::register_queue(uint i, GenericTaskQueue<E>* q) {
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   331
  assert(i < _n, "index out of range.");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  _queues[i] = q;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   336
GenericTaskQueue<E>* GenericTaskQueueSet<E>::queue(uint i) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  return _queues[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   341
bool GenericTaskQueueSet<E>::steal(uint queue_num, int* seed, E& t) {
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   342
  for (uint i = 0; i < 2 * _n; i++)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    if (steal_best_of_2(queue_num, seed, t))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   349
bool GenericTaskQueueSet<E>::steal_best_of_all(uint queue_num, int* seed, E& t) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
  if (_n > 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    int best_k;
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   352
    uint best_sz = 0;
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   353
    for (uint k = 0; k < _n; k++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
      if (k == queue_num) continue;
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   355
      uint sz = _queues[k]->size();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
      if (sz > best_sz) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
        best_sz = sz;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
        best_k = k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
    return best_sz > 0 && _queues[best_k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   373
bool GenericTaskQueueSet<E>::steal_1_random(uint queue_num, int* seed, E& t) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  if (_n > 2) {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   375
    uint k = queue_num;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    while (k == queue_num) k = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    return _queues[2]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    // Just try the other one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    int k = (queue_num + 1) % 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
template<class E>
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   389
bool GenericTaskQueueSet<E>::steal_best_of_2(uint queue_num, int* seed, E& t) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  if (_n > 2) {
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   391
    uint k1 = queue_num;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    while (k1 == queue_num) k1 = randomParkAndMiller(seed) % _n;
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   393
    uint k2 = queue_num;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    while (k2 == queue_num || k2 == k1) k2 = randomParkAndMiller(seed) % _n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    // Sample both and try the larger.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   396
    uint sz1 = _queues[k1]->size();
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   397
    uint sz2 = _queues[k2]->size();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    if (sz2 > sz1) return _queues[k2]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    else return _queues[k1]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
  } else if (_n == 2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
    // Just try the other one.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   402
    uint k = (queue_num + 1) % 2;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
    return _queues[k]->pop_global(t);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
    assert(_n == 1, "can't be zero.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
template<class E>
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
bool GenericTaskQueueSet<E>::peek() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
  // Try all the queues.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   413
  for (uint j = 0; j < _n; j++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
    if (_queues[j]->peek())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   420
// When to terminate from the termination protocol.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   421
class TerminatorTerminator: public CHeapObj {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   422
public:
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   423
  virtual bool should_exit_termination() = 0;
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   424
};
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   425
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
// A class to aid in the termination of a set of parallel tasks using
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
// TaskQueueSet's for work stealing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
class ParallelTaskTerminator: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  int _n_threads;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  TaskQueueSetSuper* _queue_set;
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   433
  int _offered_termination;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  bool peek_in_queue_set();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  virtual void yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  void sleep(uint millis);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  // "n_threads" is the number of threads to be terminated.  "queue_set" is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
  // queue sets of work queues of other threads.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  ParallelTaskTerminator(int n_threads, TaskQueueSetSuper* queue_set);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
  // The current thread has no work, and is ready to terminate if everyone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  // else is.  If returns "true", all threads are terminated.  If returns
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
  // "false", available work has been observed in one of the task queues,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  // so the global task is not complete.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   450
  bool offer_termination() {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   451
    return offer_termination(NULL);
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   452
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   453
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   454
  // As above, but it also terminates of the should_exit_termination()
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   455
  // method of the terminator parameter returns true. If terminator is
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   456
  // NULL, then it is ignored.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 360
diff changeset
   457
  bool offer_termination(TerminatorTerminator* terminator);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
  // Reset the terminator, so that it may be reused again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  // The caller is responsible for ensuring that this is done
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  // in an MT-safe manner, once the previous round of use of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  // the terminator is finished.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
  void reset_for_reuse();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
#define SIMPLE_STACK 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
template<class E> inline bool GenericTaskQueue<E>::push(E t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
#if SIMPLE_STACK
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   471
  uint localBot = _bottom;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  if (_bottom < max_elems()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
    _bottom = localBot + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
#else
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   480
  uint localBot = _bottom;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  assert((localBot >= 0) && (localBot < n()), "_bottom out of range.");
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   482
  TAG_TYPE top = get_top();
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   483
  uint dirty_n_elems = dirty_size(localBot, top);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  assert((dirty_n_elems >= 0) && (dirty_n_elems < n()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
         "n_elems out of range.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  if (dirty_n_elems < max_elems()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    _elems[localBot] = t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
    _bottom = increment_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
    return push_slow(t, dirty_n_elems);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
template<class E> inline bool GenericTaskQueue<E>::pop_local(E& t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
#if SIMPLE_STACK
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   498
  uint localBot = _bottom;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  assert(localBot > 0, "precondition.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  localBot--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
  t = _elems[localBot];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  _bottom = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
#else
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   505
  uint localBot = _bottom;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  // This value cannot be n-1.  That can only occur as a result of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  // the assignment to bottom in this method.  If it does, this method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
  // resets the size( to 0 before the next call (which is sequential,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  // since this is pop_local.)
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   510
  uint dirty_n_elems = dirty_size(localBot, get_top());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  assert(dirty_n_elems != n() - 1, "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  if (dirty_n_elems == 0) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  localBot = decrement_index(localBot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  _bottom = localBot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
  // This is necessary to prevent any read below from being reordered
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  // before the store just above.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  OrderAccess::fence();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
  t = _elems[localBot];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  // This is a second read of "age"; the "size()" above is the first.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  // If there's still at least one element in the queue, based on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
  // "_bottom" and "age" we've read, then there can be no interference with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
  // a "pop_global" operation, and we're done.
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   523
  TAG_TYPE tp = get_top();    // XXX
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  if (size(localBot, tp) > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
    assert(dirty_size(localBot, tp) != n() - 1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
           "Shouldn't be possible...");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    // Otherwise, the queue contained exactly one element; we take the slow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    // path.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
    return pop_local_slow(localBot, get_age());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
typedef oop Task;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
typedef GenericTaskQueue<Task>         OopTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
typedef GenericTaskQueueSet<Task>      OopTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   540
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   541
#define COMPRESSED_OOP_MASK  1
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   542
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   543
// This is a container class for either an oop* or a narrowOop*.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   544
// Both are pushed onto a task queue and the consumer will test is_narrow()
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   545
// to determine which should be processed.
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   546
class StarTask {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   547
  void*  _holder;        // either union oop* or narrowOop*
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   548
 public:
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   549
  StarTask(narrowOop *p) { _holder = (void *)((uintptr_t)p | COMPRESSED_OOP_MASK); }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   550
  StarTask(oop *p)       { _holder = (void*)p; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   551
  StarTask()             { _holder = NULL; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   552
  operator oop*()        { return (oop*)_holder; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   553
  operator narrowOop*()  {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   554
    return (narrowOop*)((uintptr_t)_holder & ~COMPRESSED_OOP_MASK);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   555
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   556
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   557
  // Operators to preserve const/volatile in assignments required by gcc
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   558
  void operator=(const volatile StarTask& t) volatile { _holder = t._holder; }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   559
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   560
  bool is_narrow() const {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   561
    return (((uintptr_t)_holder & COMPRESSED_OOP_MASK) != 0);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   562
  }
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   563
};
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   564
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
typedef GenericTaskQueue<StarTask>     OopStarTaskQueue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
typedef GenericTaskQueueSet<StarTask>  OopStarTaskQueueSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   568
typedef size_t RegionTask;  // index for region
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   569
typedef GenericTaskQueue<RegionTask>    RegionTaskQueue;
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   570
typedef GenericTaskQueueSet<RegionTask> RegionTaskQueueSet;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   572
class RegionTaskQueueWithOverflow: public CHeapObj {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
 protected:
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   574
  RegionTaskQueue              _region_queue;
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   575
  GrowableArray<RegionTask>*   _overflow_stack;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
 public:
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   578
  RegionTaskQueueWithOverflow() : _overflow_stack(NULL) {}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  // Initialize both stealable queue and overflow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  void initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  // Save first to stealable queue and then to overflow
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   582
  void save(RegionTask t);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  // Retrieve first from overflow and then from stealable queue
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   584
  bool retrieve(RegionTask& region_index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  // Retrieve from stealable queue
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   586
  bool retrieve_from_stealable_queue(RegionTask& region_index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
  // Retrieve from overflow
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   588
  bool retrieve_from_overflow(RegionTask& region_index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
  bool is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  bool stealable_is_empty();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  bool overflow_is_empty();
2005
42075507972b 6787254: Work queue capacity can be increased substantially on some platforms
ysr
parents: 1407
diff changeset
   592
  uint stealable_size() { return _region_queue.size(); }
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   593
  RegionTaskQueue* task_queue() { return &_region_queue; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
1407
9006b01ba3fd 6725697: par compact - rename class ChunkData to RegionData
jcoomes
parents: 1388
diff changeset
   596
#define USE_RegionTaskQueueWithOverflow