hotspot/src/share/vm/utilities/yieldingWorkgroup.hpp
author jmasa
Mon, 20 Sep 2010 14:38:38 -0700
changeset 6759 67b1a69ef5aa
parent 5547 f4b087cbb361
child 6763 711b8a44c4dd
permissions -rw-r--r--
6984287: Regularize how GC parallel workers are specified. Summary: Associate number of GC workers with the workgang as opposed to the task. Reviewed-by: johnc, ysr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
     2
 * Copyright (c) 2005, 2010 Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1374
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// Forward declarations
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
class YieldingFlexibleWorkGang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
// Status of tasks
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
enum Status {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
    INACTIVE,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
    ACTIVE,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    YIELDING,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    YIELDED,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    ABORTING,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    ABORTED,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    COMPLETING,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    COMPLETED
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// Class YieldingFlexibleGangWorker:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//   Several instances of this class run in parallel as workers for a gang.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
class YieldingFlexibleGangWorker: public GangWorker {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // Ctor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  YieldingFlexibleGangWorker(AbstractWorkGang* gang, int id) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    GangWorker(gang, id) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  YieldingFlexibleWorkGang* yf_gang() const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    { return (YieldingFlexibleWorkGang*)gang(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
protected: // Override from parent class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  virtual void loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    57
class FlexibleGangTask: public AbstractGangTask {
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    58
  int _actual_size;                      // size of gang obtained
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    59
protected:
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    60
  int _requested_size;                   // size of gang requested
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    61
public:
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    62
 FlexibleGangTask(const char* name): AbstractGangTask(name),
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    63
    _requested_size(0) {}
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    64
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    65
  // The abstract work method.
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    66
  // The argument tells you which member of the gang you are.
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    67
  virtual void work(int i) = 0;
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    68
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    69
  int requested_size() const { return _requested_size; }
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    70
  int actual_size()    const { return _actual_size; }
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    71
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    72
  void set_requested_size(int sz) { _requested_size = sz; }
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    73
  void set_actual_size(int sz)    { _actual_size    = sz; }
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    74
};
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    75
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
// An abstract task to be worked on by a flexible work gang,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// and where the workers will periodically yield, usually
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// in response to some condition that is signalled by means
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
// that are specific to the task at hand.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// You subclass this to supply your own work() method.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
// A second feature of this kind of work gang is that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
// it allows for the signalling of certain exceptional
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
// conditions that may be encountered during the performance
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
// of the task and that may require the task at hand to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
// `aborted' forthwith. Finally, these gangs are `flexible'
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
// in that they can operate at partial capacity with some
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
// gang workers waiting on the bench; in other words, the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
// size of the active worker pool can flex (up to an apriori
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
// maximum) in response to task requests at certain points.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
// The last part (the flexible part) has not yet been fully
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
// fleshed out and is a work in progress.
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    92
class YieldingFlexibleGangTask: public FlexibleGangTask {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  Status _status;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  YieldingFlexibleWorkGang* _gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // Constructor and desctructor: only construct subclasses.
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
    98
  YieldingFlexibleGangTask(const char* name): FlexibleGangTask(name),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    _status(INACTIVE),
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   100
    _gang(NULL) { }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  virtual ~YieldingFlexibleGangTask() { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  friend class YieldingFlexibleWorkGang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  friend class YieldingFlexibleGangWorker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  NOT_PRODUCT(virtual bool is_YieldingFlexibleGang_task() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  void set_status(Status s) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    _status = s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  YieldingFlexibleWorkGang* gang() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    return _gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  void set_gang(YieldingFlexibleWorkGang* gang) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    assert(_gang == NULL || gang == NULL, "Clobber without intermediate reset?");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    _gang = gang;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  // The abstract work method.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  // The argument tells you which member of the gang you are.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  virtual void work(int i) = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  // Subclasses should call the parent's yield() method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  // after having done any work specific to the subclass.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  virtual void yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  // An abstract method supplied by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // a concrete sub-class which is used by the coordinator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // to do any "central yielding" work.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  virtual void coordinator_yield() = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // Subclasses should call the parent's abort() method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // after having done any work specific to the sunbclass.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  virtual void abort();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  Status status()  const { return _status; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  bool yielded()   const { return _status == YIELDED; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  bool completed() const { return _status == COMPLETED; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  bool aborted()   const { return _status == ABORTED; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  bool active()    const { return _status == ACTIVE; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
// Class YieldingWorkGang: A subclass of WorkGang.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
// In particular, a YieldingWorkGang is made up of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
// YieldingGangWorkers, and provides infrastructure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
// supporting yielding to the "GangOverseer",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
// being the thread that orchestrates the WorkGang via run_task().
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   150
class YieldingFlexibleWorkGang: public FlexibleWorkGang {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // Here's the public interface to this class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  // Constructor and destructor.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   154
  YieldingFlexibleWorkGang(const char* name, int workers,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents: 1
diff changeset
   155
                           bool are_GC_task_threads);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  YieldingFlexibleGangTask* yielding_task() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
    assert(task() == NULL || task()->is_YieldingFlexibleGang_task(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
           "Incorrect cast");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    return (YieldingFlexibleGangTask*)task();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  }
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   162
  // Allocate a worker and return a pointer to it.
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   163
  GangWorker* allocate_worker(int which);
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   164
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  // Run a task; returns when the task is done, or the workers yield,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  // or the task is aborted, or the work gang is terminated via stop().
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  // A task that has been yielded can be continued via this same interface
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  // by using the same task repeatedly as the argument to the call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  // It is expected that the YieldingFlexibleGangTask carries the appropriate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  // continuation information used by workers to continue the task
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  // from its last yield point. Thus, a completed task will return
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // immediately with no actual work having been done by the workers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  void run_task(AbstractGangTask* task) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    guarantee(false, "Use start_task instead");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  void start_task(YieldingFlexibleGangTask* new_task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  void continue_task(YieldingFlexibleGangTask* gang_task);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  // Abort a currently running task, if any; returns when all the workers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  // have stopped working on the current task and have returned to their
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  // waiting stations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  void abort_task();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  // Yield: workers wait at their current working stations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  // until signalled to proceed by the overseer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  void yield();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  // Abort: workers are expected to return to their waiting
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // stations, whence they are ready for the next task dispatched
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  // by the overseer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  void abort();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  int _active_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  int _yielded_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  void wait_for_gang();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  // Accessors for fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  int active_workers() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    return _active_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
6759
67b1a69ef5aa 6984287: Regularize how GC parallel workers are specified.
jmasa
parents: 5547
diff changeset
   204
  // Accessors for fields
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  int yielded_workers() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    return _yielded_workers;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  friend class YieldingFlexibleGangWorker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  void reset(); // NYI
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
};