Fri, 24 Jun 2016 17:12:13 +0800
[Code Reorganization] Removed GC related modifications made by Loongson, for example, UseOldNUMA.
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP |
aoqi@0 | 26 | #define SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | #include "runtime/mutex.hpp" |
aoqi@0 | 29 | #include "utilities/growableArray.hpp" |
aoqi@0 | 30 | |
aoqi@0 | 31 | // |
aoqi@0 | 32 | // The GCTaskManager is a queue of GCTasks, and accessors |
aoqi@0 | 33 | // to allow the queue to be accessed from many threads. |
aoqi@0 | 34 | // |
aoqi@0 | 35 | |
aoqi@0 | 36 | // Forward declarations of types defined in this file. |
aoqi@0 | 37 | class GCTask; |
aoqi@0 | 38 | class GCTaskQueue; |
aoqi@0 | 39 | class SynchronizedGCTaskQueue; |
aoqi@0 | 40 | class GCTaskManager; |
aoqi@0 | 41 | class NotifyDoneClosure; |
aoqi@0 | 42 | // Some useful subclasses of GCTask. You can also make up your own. |
aoqi@0 | 43 | class NoopGCTask; |
aoqi@0 | 44 | class BarrierGCTask; |
aoqi@0 | 45 | class ReleasingBarrierGCTask; |
aoqi@0 | 46 | class NotifyingBarrierGCTask; |
aoqi@0 | 47 | class WaitForBarrierGCTask; |
aoqi@0 | 48 | class IdleGCTask; |
aoqi@0 | 49 | // A free list of Monitor*'s. |
aoqi@0 | 50 | class MonitorSupply; |
aoqi@0 | 51 | |
aoqi@0 | 52 | // Forward declarations of classes referenced in this file via pointer. |
aoqi@0 | 53 | class GCTaskThread; |
aoqi@0 | 54 | class Mutex; |
aoqi@0 | 55 | class Monitor; |
aoqi@0 | 56 | class ThreadClosure; |
aoqi@0 | 57 | |
aoqi@0 | 58 | // The abstract base GCTask. |
aoqi@0 | 59 | class GCTask : public ResourceObj { |
aoqi@0 | 60 | public: |
aoqi@0 | 61 | // Known kinds of GCTasks, for predicates. |
aoqi@0 | 62 | class Kind : AllStatic { |
aoqi@0 | 63 | public: |
aoqi@0 | 64 | enum kind { |
aoqi@0 | 65 | unknown_task, |
aoqi@0 | 66 | ordinary_task, |
aoqi@0 | 67 | barrier_task, |
aoqi@0 | 68 | noop_task, |
aoqi@0 | 69 | idle_task |
aoqi@0 | 70 | }; |
aoqi@0 | 71 | static const char* to_string(kind value); |
aoqi@0 | 72 | }; |
aoqi@0 | 73 | private: |
aoqi@0 | 74 | // Instance state. |
aoqi@0 | 75 | const Kind::kind _kind; // For runtime type checking. |
aoqi@0 | 76 | const uint _affinity; // Which worker should run task. |
aoqi@0 | 77 | GCTask* _newer; // Tasks are on doubly-linked ... |
aoqi@0 | 78 | GCTask* _older; // ... lists. |
aoqi@0 | 79 | public: |
aoqi@0 | 80 | virtual char* name() { return (char *)"task"; } |
aoqi@0 | 81 | |
aoqi@0 | 82 | // Abstract do_it method |
aoqi@0 | 83 | virtual void do_it(GCTaskManager* manager, uint which) = 0; |
aoqi@0 | 84 | // Accessors |
aoqi@0 | 85 | Kind::kind kind() const { |
aoqi@0 | 86 | return _kind; |
aoqi@0 | 87 | } |
aoqi@0 | 88 | uint affinity() const { |
aoqi@0 | 89 | return _affinity; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | GCTask* newer() const { |
aoqi@0 | 92 | return _newer; |
aoqi@0 | 93 | } |
aoqi@0 | 94 | void set_newer(GCTask* n) { |
aoqi@0 | 95 | _newer = n; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | GCTask* older() const { |
aoqi@0 | 98 | return _older; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | void set_older(GCTask* p) { |
aoqi@0 | 101 | _older = p; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | // Predicates. |
aoqi@0 | 104 | bool is_ordinary_task() const { |
aoqi@0 | 105 | return kind()==Kind::ordinary_task; |
aoqi@0 | 106 | } |
aoqi@0 | 107 | bool is_barrier_task() const { |
aoqi@0 | 108 | return kind()==Kind::barrier_task; |
aoqi@0 | 109 | } |
aoqi@0 | 110 | bool is_noop_task() const { |
aoqi@0 | 111 | return kind()==Kind::noop_task; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | bool is_idle_task() const { |
aoqi@0 | 114 | return kind()==Kind::idle_task; |
aoqi@0 | 115 | } |
aoqi@0 | 116 | void print(const char* message) const PRODUCT_RETURN; |
aoqi@0 | 117 | protected: |
aoqi@0 | 118 | // Constructors: Only create subclasses. |
aoqi@0 | 119 | // An ordinary GCTask. |
aoqi@0 | 120 | GCTask(); |
aoqi@0 | 121 | // A GCTask of a particular kind, usually barrier or noop. |
aoqi@0 | 122 | GCTask(Kind::kind kind); |
aoqi@0 | 123 | // An ordinary GCTask with an affinity. |
aoqi@0 | 124 | GCTask(uint affinity); |
aoqi@0 | 125 | // A GCTask of a particular kind, with and affinity. |
aoqi@0 | 126 | GCTask(Kind::kind kind, uint affinity); |
aoqi@0 | 127 | // We want a virtual destructor because virtual methods, |
aoqi@0 | 128 | // but since ResourceObj's don't have their destructors |
aoqi@0 | 129 | // called, we don't have one at all. Instead we have |
aoqi@0 | 130 | // this method, which gets called by subclasses to clean up. |
aoqi@0 | 131 | virtual void destruct(); |
aoqi@0 | 132 | // Methods. |
aoqi@0 | 133 | void initialize(); |
aoqi@0 | 134 | }; |
aoqi@0 | 135 | |
aoqi@0 | 136 | // A doubly-linked list of GCTasks. |
aoqi@0 | 137 | // The list is not synchronized, because sometimes we want to |
aoqi@0 | 138 | // build up a list and then make it available to other threads. |
aoqi@0 | 139 | // See also: SynchronizedGCTaskQueue. |
aoqi@0 | 140 | class GCTaskQueue : public ResourceObj { |
aoqi@0 | 141 | private: |
aoqi@0 | 142 | // Instance state. |
aoqi@0 | 143 | GCTask* _insert_end; // Tasks are enqueued at this end. |
aoqi@0 | 144 | GCTask* _remove_end; // Tasks are dequeued from this end. |
aoqi@0 | 145 | uint _length; // The current length of the queue. |
aoqi@0 | 146 | const bool _is_c_heap_obj; // Is this a CHeapObj? |
aoqi@0 | 147 | public: |
aoqi@0 | 148 | // Factory create and destroy methods. |
aoqi@0 | 149 | // Create as ResourceObj. |
aoqi@0 | 150 | static GCTaskQueue* create(); |
aoqi@0 | 151 | // Create as CHeapObj. |
aoqi@0 | 152 | static GCTaskQueue* create_on_c_heap(); |
aoqi@0 | 153 | // Destroyer. |
aoqi@0 | 154 | static void destroy(GCTaskQueue* that); |
aoqi@0 | 155 | // Accessors. |
aoqi@0 | 156 | // These just examine the state of the queue. |
aoqi@0 | 157 | bool is_empty() const { |
aoqi@0 | 158 | assert(((insert_end() == NULL && remove_end() == NULL) || |
aoqi@0 | 159 | (insert_end() != NULL && remove_end() != NULL)), |
aoqi@0 | 160 | "insert_end and remove_end don't match"); |
aoqi@0 | 161 | assert((insert_end() != NULL) || (_length == 0), "Not empty"); |
aoqi@0 | 162 | return insert_end() == NULL; |
aoqi@0 | 163 | } |
aoqi@0 | 164 | uint length() const { |
aoqi@0 | 165 | return _length; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | // Methods. |
aoqi@0 | 168 | // Enqueue one task. |
aoqi@0 | 169 | void enqueue(GCTask* task); |
aoqi@0 | 170 | // Enqueue a list of tasks. Empties the argument list. |
aoqi@0 | 171 | void enqueue(GCTaskQueue* list); |
aoqi@0 | 172 | // Dequeue one task. |
aoqi@0 | 173 | GCTask* dequeue(); |
aoqi@0 | 174 | // Dequeue one task, preferring one with affinity. |
aoqi@0 | 175 | GCTask* dequeue(uint affinity); |
aoqi@0 | 176 | protected: |
aoqi@0 | 177 | // Constructor. Clients use factory, but there might be subclasses. |
aoqi@0 | 178 | GCTaskQueue(bool on_c_heap); |
aoqi@0 | 179 | // Destructor-like method. |
aoqi@0 | 180 | // Because ResourceMark doesn't call destructors. |
aoqi@0 | 181 | // This method cleans up like one. |
aoqi@0 | 182 | virtual void destruct(); |
aoqi@0 | 183 | // Accessors. |
aoqi@0 | 184 | GCTask* insert_end() const { |
aoqi@0 | 185 | return _insert_end; |
aoqi@0 | 186 | } |
aoqi@0 | 187 | void set_insert_end(GCTask* value) { |
aoqi@0 | 188 | _insert_end = value; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | GCTask* remove_end() const { |
aoqi@0 | 191 | return _remove_end; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | void set_remove_end(GCTask* value) { |
aoqi@0 | 194 | _remove_end = value; |
aoqi@0 | 195 | } |
aoqi@0 | 196 | void increment_length() { |
aoqi@0 | 197 | _length += 1; |
aoqi@0 | 198 | } |
aoqi@0 | 199 | void decrement_length() { |
aoqi@0 | 200 | _length -= 1; |
aoqi@0 | 201 | } |
aoqi@0 | 202 | void set_length(uint value) { |
aoqi@0 | 203 | _length = value; |
aoqi@0 | 204 | } |
aoqi@0 | 205 | bool is_c_heap_obj() const { |
aoqi@0 | 206 | return _is_c_heap_obj; |
aoqi@0 | 207 | } |
aoqi@0 | 208 | // Methods. |
aoqi@0 | 209 | void initialize(); |
aoqi@0 | 210 | GCTask* remove(); // Remove from remove end. |
aoqi@0 | 211 | GCTask* remove(GCTask* task); // Remove from the middle. |
aoqi@0 | 212 | void print(const char* message) const PRODUCT_RETURN; |
aoqi@0 | 213 | // Debug support |
aoqi@0 | 214 | void verify_length() const PRODUCT_RETURN; |
aoqi@0 | 215 | }; |
aoqi@0 | 216 | |
aoqi@0 | 217 | // A GCTaskQueue that can be synchronized. |
aoqi@0 | 218 | // This "has-a" GCTaskQueue and a mutex to do the exclusion. |
aoqi@0 | 219 | class SynchronizedGCTaskQueue : public CHeapObj<mtGC> { |
aoqi@0 | 220 | private: |
aoqi@0 | 221 | // Instance state. |
aoqi@0 | 222 | GCTaskQueue* _unsynchronized_queue; // Has-a unsynchronized queue. |
aoqi@0 | 223 | Monitor * _lock; // Lock to control access. |
aoqi@0 | 224 | public: |
aoqi@0 | 225 | // Factory create and destroy methods. |
aoqi@0 | 226 | static SynchronizedGCTaskQueue* create(GCTaskQueue* queue, Monitor * lock) { |
aoqi@0 | 227 | return new SynchronizedGCTaskQueue(queue, lock); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | static void destroy(SynchronizedGCTaskQueue* that) { |
aoqi@0 | 230 | if (that != NULL) { |
aoqi@0 | 231 | delete that; |
aoqi@0 | 232 | } |
aoqi@0 | 233 | } |
aoqi@0 | 234 | // Accessors |
aoqi@0 | 235 | GCTaskQueue* unsynchronized_queue() const { |
aoqi@0 | 236 | return _unsynchronized_queue; |
aoqi@0 | 237 | } |
aoqi@0 | 238 | Monitor * lock() const { |
aoqi@0 | 239 | return _lock; |
aoqi@0 | 240 | } |
aoqi@0 | 241 | // GCTaskQueue wrapper methods. |
aoqi@0 | 242 | // These check that you hold the lock |
aoqi@0 | 243 | // and then call the method on the queue. |
aoqi@0 | 244 | bool is_empty() const { |
aoqi@0 | 245 | guarantee(own_lock(), "don't own the lock"); |
aoqi@0 | 246 | return unsynchronized_queue()->is_empty(); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | void enqueue(GCTask* task) { |
aoqi@0 | 249 | guarantee(own_lock(), "don't own the lock"); |
aoqi@0 | 250 | unsynchronized_queue()->enqueue(task); |
aoqi@0 | 251 | } |
aoqi@0 | 252 | void enqueue(GCTaskQueue* list) { |
aoqi@0 | 253 | guarantee(own_lock(), "don't own the lock"); |
aoqi@0 | 254 | unsynchronized_queue()->enqueue(list); |
aoqi@0 | 255 | } |
aoqi@0 | 256 | GCTask* dequeue() { |
aoqi@0 | 257 | guarantee(own_lock(), "don't own the lock"); |
aoqi@0 | 258 | return unsynchronized_queue()->dequeue(); |
aoqi@0 | 259 | } |
aoqi@0 | 260 | GCTask* dequeue(uint affinity) { |
aoqi@0 | 261 | guarantee(own_lock(), "don't own the lock"); |
aoqi@0 | 262 | return unsynchronized_queue()->dequeue(affinity); |
aoqi@0 | 263 | } |
aoqi@0 | 264 | uint length() const { |
aoqi@0 | 265 | guarantee(own_lock(), "don't own the lock"); |
aoqi@0 | 266 | return unsynchronized_queue()->length(); |
aoqi@0 | 267 | } |
aoqi@0 | 268 | // For guarantees. |
aoqi@0 | 269 | bool own_lock() const { |
aoqi@0 | 270 | return lock()->owned_by_self(); |
aoqi@0 | 271 | } |
aoqi@0 | 272 | protected: |
aoqi@0 | 273 | // Constructor. Clients use factory, but there might be subclasses. |
aoqi@0 | 274 | SynchronizedGCTaskQueue(GCTaskQueue* queue, Monitor * lock); |
aoqi@0 | 275 | // Destructor. Not virtual because no virtuals. |
aoqi@0 | 276 | ~SynchronizedGCTaskQueue(); |
aoqi@0 | 277 | }; |
aoqi@0 | 278 | |
aoqi@0 | 279 | // This is an abstract base class for getting notifications |
aoqi@0 | 280 | // when a GCTaskManager is done. |
aoqi@0 | 281 | class NotifyDoneClosure : public CHeapObj<mtGC> { |
aoqi@0 | 282 | public: |
aoqi@0 | 283 | // The notification callback method. |
aoqi@0 | 284 | virtual void notify(GCTaskManager* manager) = 0; |
aoqi@0 | 285 | protected: |
aoqi@0 | 286 | // Constructor. |
aoqi@0 | 287 | NotifyDoneClosure() { |
aoqi@0 | 288 | // Nothing to do. |
aoqi@0 | 289 | } |
aoqi@0 | 290 | // Virtual destructor because virtual methods. |
aoqi@0 | 291 | virtual ~NotifyDoneClosure() { |
aoqi@0 | 292 | // Nothing to do. |
aoqi@0 | 293 | } |
aoqi@0 | 294 | }; |
aoqi@0 | 295 | |
aoqi@0 | 296 | // Dynamic number of GC threads |
aoqi@0 | 297 | // |
aoqi@0 | 298 | // GC threads wait in get_task() for work (i.e., a task) to perform. |
aoqi@0 | 299 | // When the number of GC threads was static, the number of tasks |
aoqi@0 | 300 | // created to do a job was equal to or greater than the maximum |
aoqi@0 | 301 | // number of GC threads (ParallelGCThreads). The job might be divided |
aoqi@0 | 302 | // into a number of tasks greater than the number of GC threads for |
aoqi@0 | 303 | // load balancing (i.e., over partitioning). The last task to be |
aoqi@0 | 304 | // executed by a GC thread in a job is a work stealing task. A |
aoqi@0 | 305 | // GC thread that gets a work stealing task continues to execute |
aoqi@0 | 306 | // that task until the job is done. In the static number of GC theads |
aoqi@0 | 307 | // case, tasks are added to a queue (FIFO). The work stealing tasks are |
aoqi@0 | 308 | // the last to be added. Once the tasks are added, the GC threads grab |
aoqi@0 | 309 | // a task and go. A single thread can do all the non-work stealing tasks |
aoqi@0 | 310 | // and then execute a work stealing and wait for all the other GC threads |
aoqi@0 | 311 | // to execute their work stealing task. |
aoqi@0 | 312 | // In the dynamic number of GC threads implementation, idle-tasks are |
aoqi@0 | 313 | // created to occupy the non-participating or "inactive" threads. An |
aoqi@0 | 314 | // idle-task makes the GC thread wait on a barrier that is part of the |
aoqi@0 | 315 | // GCTaskManager. The GC threads that have been "idled" in a IdleGCTask |
aoqi@0 | 316 | // are released once all the active GC threads have finished their work |
aoqi@0 | 317 | // stealing tasks. The GCTaskManager does not wait for all the "idled" |
aoqi@0 | 318 | // GC threads to resume execution. When those GC threads do resume |
aoqi@0 | 319 | // execution in the course of the thread scheduling, they call get_tasks() |
aoqi@0 | 320 | // as all the other GC threads do. Because all the "idled" threads are |
aoqi@0 | 321 | // not required to execute in order to finish a job, it is possible for |
aoqi@0 | 322 | // a GC thread to still be "idled" when the next job is started. Such |
aoqi@0 | 323 | // a thread stays "idled" for the next job. This can result in a new |
aoqi@0 | 324 | // job not having all the expected active workers. For example if on |
aoqi@0 | 325 | // job requests 4 active workers out of a total of 10 workers so the |
aoqi@0 | 326 | // remaining 6 are "idled", if the next job requests 6 active workers |
aoqi@0 | 327 | // but all 6 of the "idled" workers are still idle, then the next job |
aoqi@0 | 328 | // will only get 4 active workers. |
aoqi@0 | 329 | // The implementation for the parallel old compaction phase has an |
aoqi@0 | 330 | // added complication. In the static case parold partitions the chunks |
aoqi@0 | 331 | // ready to be filled into stacks, one for each GC thread. A GC thread |
aoqi@0 | 332 | // executing a draining task (drains the stack of ready chunks) |
aoqi@0 | 333 | // claims a stack according to it's id (the unique ordinal value assigned |
aoqi@0 | 334 | // to each GC thread). In the dynamic case not all GC threads will |
aoqi@0 | 335 | // actively participate so stacks with ready to fill chunks can only be |
aoqi@0 | 336 | // given to the active threads. An initial implementation chose stacks |
aoqi@0 | 337 | // number 1-n to get the ready chunks and required that GC threads |
aoqi@0 | 338 | // 1-n be the active workers. This was undesirable because it required |
aoqi@0 | 339 | // certain threads to participate. In the final implementation a |
aoqi@0 | 340 | // list of stacks equal in number to the active workers are filled |
aoqi@0 | 341 | // with ready chunks. GC threads that participate get a stack from |
aoqi@0 | 342 | // the task (DrainStacksCompactionTask), empty the stack, and then add it to a |
aoqi@0 | 343 | // recycling list at the end of the task. If the same GC thread gets |
aoqi@0 | 344 | // a second task, it gets a second stack to drain and returns it. The |
aoqi@0 | 345 | // stacks are added to a recycling list so that later stealing tasks |
aoqi@0 | 346 | // for this tasks can get a stack from the recycling list. Stealing tasks |
aoqi@0 | 347 | // use the stacks in its work in a way similar to the draining tasks. |
aoqi@0 | 348 | // A thread is not guaranteed to get anything but a stealing task and |
aoqi@0 | 349 | // a thread that only gets a stealing task has to get a stack. A failed |
aoqi@0 | 350 | // implementation tried to have the GC threads keep the stack they used |
aoqi@0 | 351 | // during a draining task for later use in the stealing task but that didn't |
aoqi@0 | 352 | // work because as noted a thread is not guaranteed to get a draining task. |
aoqi@0 | 353 | // |
aoqi@0 | 354 | // For PSScavenge and ParCompactionManager the GC threads are |
aoqi@0 | 355 | // held in the GCTaskThread** _thread array in GCTaskManager. |
aoqi@0 | 356 | |
aoqi@0 | 357 | |
aoqi@0 | 358 | class GCTaskManager : public CHeapObj<mtGC> { |
aoqi@0 | 359 | friend class ParCompactionManager; |
aoqi@0 | 360 | friend class PSParallelCompact; |
aoqi@0 | 361 | friend class PSScavenge; |
aoqi@0 | 362 | friend class PSRefProcTaskExecutor; |
aoqi@0 | 363 | friend class RefProcTaskExecutor; |
aoqi@0 | 364 | friend class GCTaskThread; |
aoqi@0 | 365 | friend class IdleGCTask; |
aoqi@0 | 366 | private: |
aoqi@0 | 367 | // Instance state. |
aoqi@0 | 368 | NotifyDoneClosure* _ndc; // Notify on completion. |
aoqi@0 | 369 | const uint _workers; // Number of workers. |
aoqi@0 | 370 | Monitor* _monitor; // Notification of changes. |
aoqi@0 | 371 | SynchronizedGCTaskQueue* _queue; // Queue of tasks. |
aoqi@0 | 372 | GCTaskThread** _thread; // Array of worker threads. |
aoqi@0 | 373 | uint _active_workers; // Number of active workers. |
aoqi@0 | 374 | uint _busy_workers; // Number of busy workers. |
aoqi@0 | 375 | uint _blocking_worker; // The worker that's blocking. |
aoqi@0 | 376 | bool* _resource_flag; // Array of flag per threads. |
aoqi@0 | 377 | uint _delivered_tasks; // Count of delivered tasks. |
aoqi@0 | 378 | uint _completed_tasks; // Count of completed tasks. |
aoqi@0 | 379 | uint _barriers; // Count of barrier tasks. |
aoqi@0 | 380 | uint _emptied_queue; // Times we emptied the queue. |
aoqi@0 | 381 | NoopGCTask* _noop_task; // The NoopGCTask instance. |
aoqi@0 | 382 | uint _noop_tasks; // Count of noop tasks. |
aoqi@0 | 383 | WaitForBarrierGCTask* _idle_inactive_task;// Task for inactive workers |
aoqi@0 | 384 | volatile uint _idle_workers; // Number of idled workers |
aoqi@0 | 385 | public: |
aoqi@0 | 386 | // Factory create and destroy methods. |
aoqi@0 | 387 | static GCTaskManager* create(uint workers) { |
aoqi@0 | 388 | return new GCTaskManager(workers); |
aoqi@0 | 389 | } |
aoqi@0 | 390 | static GCTaskManager* create(uint workers, NotifyDoneClosure* ndc) { |
aoqi@0 | 391 | return new GCTaskManager(workers, ndc); |
aoqi@0 | 392 | } |
aoqi@0 | 393 | static void destroy(GCTaskManager* that) { |
aoqi@0 | 394 | if (that != NULL) { |
aoqi@0 | 395 | delete that; |
aoqi@0 | 396 | } |
aoqi@0 | 397 | } |
aoqi@0 | 398 | // Accessors. |
aoqi@0 | 399 | uint busy_workers() const { |
aoqi@0 | 400 | return _busy_workers; |
aoqi@0 | 401 | } |
aoqi@0 | 402 | volatile uint idle_workers() const { |
aoqi@0 | 403 | return _idle_workers; |
aoqi@0 | 404 | } |
aoqi@0 | 405 | // Pun between Monitor* and Mutex* |
aoqi@0 | 406 | Monitor* monitor() const { |
aoqi@0 | 407 | return _monitor; |
aoqi@0 | 408 | } |
aoqi@0 | 409 | Monitor * lock() const { |
aoqi@0 | 410 | return _monitor; |
aoqi@0 | 411 | } |
aoqi@0 | 412 | WaitForBarrierGCTask* idle_inactive_task() { |
aoqi@0 | 413 | return _idle_inactive_task; |
aoqi@0 | 414 | } |
aoqi@0 | 415 | // Methods. |
aoqi@0 | 416 | // Add the argument task to be run. |
aoqi@0 | 417 | void add_task(GCTask* task); |
aoqi@0 | 418 | // Add a list of tasks. Removes task from the argument list. |
aoqi@0 | 419 | void add_list(GCTaskQueue* list); |
aoqi@0 | 420 | // Claim a task for argument worker. |
aoqi@0 | 421 | GCTask* get_task(uint which); |
aoqi@0 | 422 | // Note the completion of a task by the argument worker. |
aoqi@0 | 423 | void note_completion(uint which); |
aoqi@0 | 424 | // Is the queue blocked from handing out new tasks? |
aoqi@0 | 425 | bool is_blocked() const { |
aoqi@0 | 426 | return (blocking_worker() != sentinel_worker()); |
aoqi@0 | 427 | } |
aoqi@0 | 428 | // Request that all workers release their resources. |
aoqi@0 | 429 | void release_all_resources(); |
aoqi@0 | 430 | // Ask if a particular worker should release its resources. |
aoqi@0 | 431 | bool should_release_resources(uint which); // Predicate. |
aoqi@0 | 432 | // Note the release of resources by the argument worker. |
aoqi@0 | 433 | void note_release(uint which); |
aoqi@0 | 434 | // Create IdleGCTasks for inactive workers and start workers |
aoqi@0 | 435 | void task_idle_workers(); |
aoqi@0 | 436 | // Release the workers in IdleGCTasks |
aoqi@0 | 437 | void release_idle_workers(); |
aoqi@0 | 438 | // Constants. |
aoqi@0 | 439 | // A sentinel worker identifier. |
aoqi@0 | 440 | static uint sentinel_worker() { |
aoqi@0 | 441 | return (uint) -1; // Why isn't there a max_uint? |
aoqi@0 | 442 | } |
aoqi@0 | 443 | |
aoqi@0 | 444 | // Execute the task queue and wait for the completion. |
aoqi@0 | 445 | void execute_and_wait(GCTaskQueue* list); |
aoqi@0 | 446 | |
aoqi@0 | 447 | void print_task_time_stamps(); |
aoqi@0 | 448 | void print_threads_on(outputStream* st); |
aoqi@0 | 449 | void threads_do(ThreadClosure* tc); |
aoqi@0 | 450 | |
aoqi@0 | 451 | protected: |
aoqi@0 | 452 | // Constructors. Clients use factory, but there might be subclasses. |
aoqi@0 | 453 | // Create a GCTaskManager with the appropriate number of workers. |
aoqi@0 | 454 | GCTaskManager(uint workers); |
aoqi@0 | 455 | // Create a GCTaskManager that calls back when there's no more work. |
aoqi@0 | 456 | GCTaskManager(uint workers, NotifyDoneClosure* ndc); |
aoqi@0 | 457 | // Make virtual if necessary. |
aoqi@0 | 458 | ~GCTaskManager(); |
aoqi@0 | 459 | // Accessors. |
aoqi@0 | 460 | uint workers() const { |
aoqi@0 | 461 | return _workers; |
aoqi@0 | 462 | } |
aoqi@0 | 463 | void set_active_workers(uint v) { |
aoqi@0 | 464 | assert(v <= _workers, "Trying to set more workers active than there are"); |
aoqi@0 | 465 | _active_workers = MIN2(v, _workers); |
aoqi@0 | 466 | assert(v != 0, "Trying to set active workers to 0"); |
aoqi@0 | 467 | _active_workers = MAX2(1U, _active_workers); |
aoqi@0 | 468 | } |
aoqi@0 | 469 | // Sets the number of threads that will be used in a collection |
aoqi@0 | 470 | void set_active_gang(); |
aoqi@0 | 471 | |
aoqi@0 | 472 | NotifyDoneClosure* notify_done_closure() const { |
aoqi@0 | 473 | return _ndc; |
aoqi@0 | 474 | } |
aoqi@0 | 475 | SynchronizedGCTaskQueue* queue() const { |
aoqi@0 | 476 | return _queue; |
aoqi@0 | 477 | } |
aoqi@0 | 478 | NoopGCTask* noop_task() const { |
aoqi@0 | 479 | return _noop_task; |
aoqi@0 | 480 | } |
aoqi@0 | 481 | // Bounds-checking per-thread data accessors. |
aoqi@0 | 482 | GCTaskThread* thread(uint which); |
aoqi@0 | 483 | void set_thread(uint which, GCTaskThread* value); |
aoqi@0 | 484 | bool resource_flag(uint which); |
aoqi@0 | 485 | void set_resource_flag(uint which, bool value); |
aoqi@0 | 486 | // Modifier methods with some semantics. |
aoqi@0 | 487 | // Is any worker blocking handing out new tasks? |
aoqi@0 | 488 | uint blocking_worker() const { |
aoqi@0 | 489 | return _blocking_worker; |
aoqi@0 | 490 | } |
aoqi@0 | 491 | void set_blocking_worker(uint value) { |
aoqi@0 | 492 | _blocking_worker = value; |
aoqi@0 | 493 | } |
aoqi@0 | 494 | void set_unblocked() { |
aoqi@0 | 495 | set_blocking_worker(sentinel_worker()); |
aoqi@0 | 496 | } |
aoqi@0 | 497 | // Count of busy workers. |
aoqi@0 | 498 | void reset_busy_workers() { |
aoqi@0 | 499 | _busy_workers = 0; |
aoqi@0 | 500 | } |
aoqi@0 | 501 | uint increment_busy_workers(); |
aoqi@0 | 502 | uint decrement_busy_workers(); |
aoqi@0 | 503 | // Count of tasks delivered to workers. |
aoqi@0 | 504 | uint delivered_tasks() const { |
aoqi@0 | 505 | return _delivered_tasks; |
aoqi@0 | 506 | } |
aoqi@0 | 507 | void increment_delivered_tasks() { |
aoqi@0 | 508 | _delivered_tasks += 1; |
aoqi@0 | 509 | } |
aoqi@0 | 510 | void reset_delivered_tasks() { |
aoqi@0 | 511 | _delivered_tasks = 0; |
aoqi@0 | 512 | } |
aoqi@0 | 513 | // Count of tasks completed by workers. |
aoqi@0 | 514 | uint completed_tasks() const { |
aoqi@0 | 515 | return _completed_tasks; |
aoqi@0 | 516 | } |
aoqi@0 | 517 | void increment_completed_tasks() { |
aoqi@0 | 518 | _completed_tasks += 1; |
aoqi@0 | 519 | } |
aoqi@0 | 520 | void reset_completed_tasks() { |
aoqi@0 | 521 | _completed_tasks = 0; |
aoqi@0 | 522 | } |
aoqi@0 | 523 | // Count of barrier tasks completed. |
aoqi@0 | 524 | uint barriers() const { |
aoqi@0 | 525 | return _barriers; |
aoqi@0 | 526 | } |
aoqi@0 | 527 | void increment_barriers() { |
aoqi@0 | 528 | _barriers += 1; |
aoqi@0 | 529 | } |
aoqi@0 | 530 | void reset_barriers() { |
aoqi@0 | 531 | _barriers = 0; |
aoqi@0 | 532 | } |
aoqi@0 | 533 | // Count of how many times the queue has emptied. |
aoqi@0 | 534 | uint emptied_queue() const { |
aoqi@0 | 535 | return _emptied_queue; |
aoqi@0 | 536 | } |
aoqi@0 | 537 | void increment_emptied_queue() { |
aoqi@0 | 538 | _emptied_queue += 1; |
aoqi@0 | 539 | } |
aoqi@0 | 540 | void reset_emptied_queue() { |
aoqi@0 | 541 | _emptied_queue = 0; |
aoqi@0 | 542 | } |
aoqi@0 | 543 | // Count of the number of noop tasks we've handed out, |
aoqi@0 | 544 | // e.g., to handle resource release requests. |
aoqi@0 | 545 | uint noop_tasks() const { |
aoqi@0 | 546 | return _noop_tasks; |
aoqi@0 | 547 | } |
aoqi@0 | 548 | void increment_noop_tasks() { |
aoqi@0 | 549 | _noop_tasks += 1; |
aoqi@0 | 550 | } |
aoqi@0 | 551 | void reset_noop_tasks() { |
aoqi@0 | 552 | _noop_tasks = 0; |
aoqi@0 | 553 | } |
aoqi@0 | 554 | void increment_idle_workers() { |
aoqi@0 | 555 | _idle_workers++; |
aoqi@0 | 556 | } |
aoqi@0 | 557 | void decrement_idle_workers() { |
aoqi@0 | 558 | _idle_workers--; |
aoqi@0 | 559 | } |
aoqi@0 | 560 | // Other methods. |
aoqi@0 | 561 | void initialize(); |
aoqi@0 | 562 | |
aoqi@0 | 563 | public: |
aoqi@0 | 564 | // Return true if all workers are currently active. |
aoqi@0 | 565 | bool all_workers_active() { return workers() == active_workers(); } |
aoqi@0 | 566 | uint active_workers() const { |
aoqi@0 | 567 | return _active_workers; |
aoqi@0 | 568 | } |
aoqi@0 | 569 | }; |
aoqi@0 | 570 | |
aoqi@0 | 571 | // |
aoqi@0 | 572 | // Some exemplary GCTasks. |
aoqi@0 | 573 | // |
aoqi@0 | 574 | |
aoqi@0 | 575 | // A noop task that does nothing, |
aoqi@0 | 576 | // except take us around the GCTaskThread loop. |
aoqi@0 | 577 | class NoopGCTask : public GCTask { |
aoqi@0 | 578 | private: |
aoqi@0 | 579 | const bool _is_c_heap_obj; // Is this a CHeapObj? |
aoqi@0 | 580 | public: |
aoqi@0 | 581 | // Factory create and destroy methods. |
aoqi@0 | 582 | static NoopGCTask* create(); |
aoqi@0 | 583 | static NoopGCTask* create_on_c_heap(); |
aoqi@0 | 584 | static void destroy(NoopGCTask* that); |
aoqi@0 | 585 | |
aoqi@0 | 586 | virtual char* name() { return (char *)"noop task"; } |
aoqi@0 | 587 | // Methods from GCTask. |
aoqi@0 | 588 | void do_it(GCTaskManager* manager, uint which) { |
aoqi@0 | 589 | // Nothing to do. |
aoqi@0 | 590 | } |
aoqi@0 | 591 | protected: |
aoqi@0 | 592 | // Constructor. |
aoqi@0 | 593 | NoopGCTask(bool on_c_heap) : |
aoqi@0 | 594 | GCTask(GCTask::Kind::noop_task), |
aoqi@0 | 595 | _is_c_heap_obj(on_c_heap) { |
aoqi@0 | 596 | // Nothing to do. |
aoqi@0 | 597 | } |
aoqi@0 | 598 | // Destructor-like method. |
aoqi@0 | 599 | void destruct(); |
aoqi@0 | 600 | // Accessors. |
aoqi@0 | 601 | bool is_c_heap_obj() const { |
aoqi@0 | 602 | return _is_c_heap_obj; |
aoqi@0 | 603 | } |
aoqi@0 | 604 | }; |
aoqi@0 | 605 | |
aoqi@0 | 606 | // A BarrierGCTask blocks other tasks from starting, |
aoqi@0 | 607 | // and waits until it is the only task running. |
aoqi@0 | 608 | class BarrierGCTask : public GCTask { |
aoqi@0 | 609 | public: |
aoqi@0 | 610 | // Factory create and destroy methods. |
aoqi@0 | 611 | static BarrierGCTask* create() { |
aoqi@0 | 612 | return new BarrierGCTask(); |
aoqi@0 | 613 | } |
aoqi@0 | 614 | static void destroy(BarrierGCTask* that) { |
aoqi@0 | 615 | if (that != NULL) { |
aoqi@0 | 616 | that->destruct(); |
aoqi@0 | 617 | delete that; |
aoqi@0 | 618 | } |
aoqi@0 | 619 | } |
aoqi@0 | 620 | // Methods from GCTask. |
aoqi@0 | 621 | void do_it(GCTaskManager* manager, uint which); |
aoqi@0 | 622 | protected: |
aoqi@0 | 623 | // Constructor. Clients use factory, but there might be subclasses. |
aoqi@0 | 624 | BarrierGCTask() : |
aoqi@0 | 625 | GCTask(GCTask::Kind::barrier_task) { |
aoqi@0 | 626 | // Nothing to do. |
aoqi@0 | 627 | } |
aoqi@0 | 628 | // Destructor-like method. |
aoqi@0 | 629 | void destruct(); |
aoqi@0 | 630 | |
aoqi@0 | 631 | virtual char* name() { return (char *)"barrier task"; } |
aoqi@0 | 632 | // Methods. |
aoqi@0 | 633 | // Wait for this to be the only task running. |
aoqi@0 | 634 | void do_it_internal(GCTaskManager* manager, uint which); |
aoqi@0 | 635 | }; |
aoqi@0 | 636 | |
aoqi@0 | 637 | // A ReleasingBarrierGCTask is a BarrierGCTask |
aoqi@0 | 638 | // that tells all the tasks to release their resource areas. |
aoqi@0 | 639 | class ReleasingBarrierGCTask : public BarrierGCTask { |
aoqi@0 | 640 | public: |
aoqi@0 | 641 | // Factory create and destroy methods. |
aoqi@0 | 642 | static ReleasingBarrierGCTask* create() { |
aoqi@0 | 643 | return new ReleasingBarrierGCTask(); |
aoqi@0 | 644 | } |
aoqi@0 | 645 | static void destroy(ReleasingBarrierGCTask* that) { |
aoqi@0 | 646 | if (that != NULL) { |
aoqi@0 | 647 | that->destruct(); |
aoqi@0 | 648 | delete that; |
aoqi@0 | 649 | } |
aoqi@0 | 650 | } |
aoqi@0 | 651 | // Methods from GCTask. |
aoqi@0 | 652 | void do_it(GCTaskManager* manager, uint which); |
aoqi@0 | 653 | protected: |
aoqi@0 | 654 | // Constructor. Clients use factory, but there might be subclasses. |
aoqi@0 | 655 | ReleasingBarrierGCTask() : |
aoqi@0 | 656 | BarrierGCTask() { |
aoqi@0 | 657 | // Nothing to do. |
aoqi@0 | 658 | } |
aoqi@0 | 659 | // Destructor-like method. |
aoqi@0 | 660 | void destruct(); |
aoqi@0 | 661 | }; |
aoqi@0 | 662 | |
aoqi@0 | 663 | // A NotifyingBarrierGCTask is a BarrierGCTask |
aoqi@0 | 664 | // that calls a notification method when it is the only task running. |
aoqi@0 | 665 | class NotifyingBarrierGCTask : public BarrierGCTask { |
aoqi@0 | 666 | private: |
aoqi@0 | 667 | // Instance state. |
aoqi@0 | 668 | NotifyDoneClosure* _ndc; // The callback object. |
aoqi@0 | 669 | public: |
aoqi@0 | 670 | // Factory create and destroy methods. |
aoqi@0 | 671 | static NotifyingBarrierGCTask* create(NotifyDoneClosure* ndc) { |
aoqi@0 | 672 | return new NotifyingBarrierGCTask(ndc); |
aoqi@0 | 673 | } |
aoqi@0 | 674 | static void destroy(NotifyingBarrierGCTask* that) { |
aoqi@0 | 675 | if (that != NULL) { |
aoqi@0 | 676 | that->destruct(); |
aoqi@0 | 677 | delete that; |
aoqi@0 | 678 | } |
aoqi@0 | 679 | } |
aoqi@0 | 680 | // Methods from GCTask. |
aoqi@0 | 681 | void do_it(GCTaskManager* manager, uint which); |
aoqi@0 | 682 | protected: |
aoqi@0 | 683 | // Constructor. Clients use factory, but there might be subclasses. |
aoqi@0 | 684 | NotifyingBarrierGCTask(NotifyDoneClosure* ndc) : |
aoqi@0 | 685 | BarrierGCTask(), |
aoqi@0 | 686 | _ndc(ndc) { |
aoqi@0 | 687 | assert(notify_done_closure() != NULL, "can't notify on NULL"); |
aoqi@0 | 688 | } |
aoqi@0 | 689 | // Destructor-like method. |
aoqi@0 | 690 | void destruct(); |
aoqi@0 | 691 | // Accessor. |
aoqi@0 | 692 | NotifyDoneClosure* notify_done_closure() const { return _ndc; } |
aoqi@0 | 693 | }; |
aoqi@0 | 694 | |
aoqi@0 | 695 | // A WaitForBarrierGCTask is a BarrierGCTask |
aoqi@0 | 696 | // with a method you can call to wait until |
aoqi@0 | 697 | // the BarrierGCTask is done. |
aoqi@0 | 698 | // This may cover many of the uses of NotifyingBarrierGCTasks. |
aoqi@0 | 699 | class WaitForBarrierGCTask : public BarrierGCTask { |
aoqi@0 | 700 | friend class GCTaskManager; |
aoqi@0 | 701 | friend class IdleGCTask; |
aoqi@0 | 702 | private: |
aoqi@0 | 703 | // Instance state. |
aoqi@0 | 704 | Monitor* _monitor; // Guard and notify changes. |
aoqi@0 | 705 | volatile bool _should_wait; // true=>wait, false=>proceed. |
aoqi@0 | 706 | const bool _is_c_heap_obj; // Was allocated on the heap. |
aoqi@0 | 707 | public: |
aoqi@0 | 708 | virtual char* name() { return (char *) "waitfor-barrier-task"; } |
aoqi@0 | 709 | |
aoqi@0 | 710 | // Factory create and destroy methods. |
aoqi@0 | 711 | static WaitForBarrierGCTask* create(); |
aoqi@0 | 712 | static WaitForBarrierGCTask* create_on_c_heap(); |
aoqi@0 | 713 | static void destroy(WaitForBarrierGCTask* that); |
aoqi@0 | 714 | // Methods. |
aoqi@0 | 715 | void do_it(GCTaskManager* manager, uint which); |
aoqi@0 | 716 | void wait_for(bool reset); |
aoqi@0 | 717 | void set_should_wait(bool value) { |
aoqi@0 | 718 | _should_wait = value; |
aoqi@0 | 719 | } |
aoqi@0 | 720 | protected: |
aoqi@0 | 721 | // Constructor. Clients use factory, but there might be subclasses. |
aoqi@0 | 722 | WaitForBarrierGCTask(bool on_c_heap); |
aoqi@0 | 723 | // Destructor-like method. |
aoqi@0 | 724 | void destruct(); |
aoqi@0 | 725 | // Accessors. |
aoqi@0 | 726 | Monitor* monitor() const { |
aoqi@0 | 727 | return _monitor; |
aoqi@0 | 728 | } |
aoqi@0 | 729 | bool should_wait() const { |
aoqi@0 | 730 | return _should_wait; |
aoqi@0 | 731 | } |
aoqi@0 | 732 | bool is_c_heap_obj() { |
aoqi@0 | 733 | return _is_c_heap_obj; |
aoqi@0 | 734 | } |
aoqi@0 | 735 | }; |
aoqi@0 | 736 | |
aoqi@0 | 737 | // Task that is used to idle a GC task when fewer than |
aoqi@0 | 738 | // the maximum workers are wanted. |
aoqi@0 | 739 | class IdleGCTask : public GCTask { |
aoqi@0 | 740 | const bool _is_c_heap_obj; // Was allocated on the heap. |
aoqi@0 | 741 | public: |
aoqi@0 | 742 | bool is_c_heap_obj() { |
aoqi@0 | 743 | return _is_c_heap_obj; |
aoqi@0 | 744 | } |
aoqi@0 | 745 | // Factory create and destroy methods. |
aoqi@0 | 746 | static IdleGCTask* create(); |
aoqi@0 | 747 | static IdleGCTask* create_on_c_heap(); |
aoqi@0 | 748 | static void destroy(IdleGCTask* that); |
aoqi@0 | 749 | |
aoqi@0 | 750 | virtual char* name() { return (char *)"idle task"; } |
aoqi@0 | 751 | // Methods from GCTask. |
aoqi@0 | 752 | virtual void do_it(GCTaskManager* manager, uint which); |
aoqi@0 | 753 | protected: |
aoqi@0 | 754 | // Constructor. |
aoqi@0 | 755 | IdleGCTask(bool on_c_heap) : |
aoqi@0 | 756 | GCTask(GCTask::Kind::idle_task), |
aoqi@0 | 757 | _is_c_heap_obj(on_c_heap) { |
aoqi@0 | 758 | // Nothing to do. |
aoqi@0 | 759 | } |
aoqi@0 | 760 | // Destructor-like method. |
aoqi@0 | 761 | void destruct(); |
aoqi@0 | 762 | }; |
aoqi@0 | 763 | |
aoqi@0 | 764 | class MonitorSupply : public AllStatic { |
aoqi@0 | 765 | private: |
aoqi@0 | 766 | // State. |
aoqi@0 | 767 | // Control multi-threaded access. |
aoqi@0 | 768 | static Mutex* _lock; |
aoqi@0 | 769 | // The list of available Monitor*'s. |
aoqi@0 | 770 | static GrowableArray<Monitor*>* _freelist; |
aoqi@0 | 771 | public: |
aoqi@0 | 772 | // Reserve a Monitor*. |
aoqi@0 | 773 | static Monitor* reserve(); |
aoqi@0 | 774 | // Release a Monitor*. |
aoqi@0 | 775 | static void release(Monitor* instance); |
aoqi@0 | 776 | private: |
aoqi@0 | 777 | // Accessors. |
aoqi@0 | 778 | static Mutex* lock() { |
aoqi@0 | 779 | return _lock; |
aoqi@0 | 780 | } |
aoqi@0 | 781 | static GrowableArray<Monitor*>* freelist() { |
aoqi@0 | 782 | return _freelist; |
aoqi@0 | 783 | } |
aoqi@0 | 784 | }; |
aoqi@0 | 785 | |
aoqi@0 | 786 | #endif // SHARE_VM_GC_IMPLEMENTATION_PARALLELSCAVENGE_GCTASKMANAGER_HPP |