hotspot/src/share/vm/gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp
author zgu
Thu, 01 May 2014 05:52:28 -0700
changeset 24331 c0bc7e5653fb
parent 22551 9bf46d16dcc6
child 24424 2658d7834c6e
permissions -rw-r--r--
6885993: Named Thread: introduce print() and print_on(outputStream* st) methods Summary: Eliminating duplicated code by introducing print_on(outputStream* st) methods in NamedThread Reviewed-by: twisti, coleenp, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
22234
da823d78ad65 8029233: Update copyright year to match last edit in jdk8 hotspot repository for 2013
mikael
parents: 15859
diff changeset
     2
 * Copyright (c) 2001, 2013, 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: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
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: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    25
#ifndef SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    26
#define SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    28
#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    29
#include "gc_implementation/shared/concurrentGCThread.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 10565
diff changeset
    30
#include "runtime/thread.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    31
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
class ConcurrentMarkSweepGeneration;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
class CMSCollector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
8684
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
    35
// The Concurrent Mark Sweep GC Thread
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
class ConcurrentMarkSweepThread: public ConcurrentGCThread {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  friend class ConcurrentMarkSweepGeneration;   // XXX should remove friendship
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  friend class CMSCollector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  virtual void run();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  static ConcurrentMarkSweepThread*     _cmst;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  static CMSCollector*                  _collector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  static SurrogateLockerThread*         _slt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  static SurrogateLockerThread::SLT_msg_type _sltBuffer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  static Monitor*                       _sltMonitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  static bool _should_terminate;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  enum CMS_flag_type {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    CMS_nil             = NoBits,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
    CMS_cms_wants_token = nth_bit(0),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    CMS_cms_has_token   = nth_bit(1),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
    CMS_vm_wants_token  = nth_bit(2),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    CMS_vm_has_token    = nth_bit(3)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  static int _CMS_flag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  static bool CMS_flag_is_set(int b)        { return (_CMS_flag & b) != 0;   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  static bool set_CMS_flag(int b)           { return (_CMS_flag |= b) != 0;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  static bool clear_CMS_flag(int b)         { return (_CMS_flag &= ~b) != 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  void sleepBeforeNextCycle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // CMS thread should yield for a young gen collection, direct allocation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  // and iCMS activity.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  static char _pad_1[64 - sizeof(jint)];    // prevent cache-line sharing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  static volatile jint _pending_yields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  static volatile jint _pending_decrements; // decrements to _pending_yields
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  static char _pad_2[64 - sizeof(jint)];    // prevent cache-line sharing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // Tracing messages, enabled by CMSTraceThreadState.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  static inline void trace_state(const char* desc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
8684
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
    77
  static volatile int _icms_disabled;   // a counter to track #iCMS disable & enable
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  static volatile bool _should_run;     // iCMS may run
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  static volatile bool _should_stop;    // iCMS should stop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // debugging
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  void verify_ok_to_terminate() const PRODUCT_RETURN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  // Constructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  ConcurrentMarkSweepThread(CMSCollector* collector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  static void makeSurrogateLockerThread(TRAPS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  static SurrogateLockerThread* slt() { return _slt; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  // Tester
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  bool is_ConcurrentGC_thread() const { return true;       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  static void threads_do(ThreadClosure* tc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // Printing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  static void print_all_on(outputStream* st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  static void print_all()                             { print_all_on(tty); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  // Returns the CMS Thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  static ConcurrentMarkSweepThread* cmst()    { return _cmst; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  static CMSCollector*         collector()    { return _collector;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // Create and start the CMS Thread, or stop it on shutdown
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  static ConcurrentMarkSweepThread* start(CMSCollector* collector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  static void stop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  static bool should_terminate() { return _should_terminate; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // Synchronization using CMS token
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  static void synchronize(bool is_cms_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  static void desynchronize(bool is_cms_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  static bool vm_thread_has_cms_token() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    return CMS_flag_is_set(CMS_vm_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  static bool cms_thread_has_cms_token() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    return CMS_flag_is_set(CMS_cms_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  static bool vm_thread_wants_cms_token() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    return CMS_flag_is_set(CMS_vm_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  static bool cms_thread_wants_cms_token() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    return CMS_flag_is_set(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // Wait on CMS lock until the next synchronous GC
6984
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 6763
diff changeset
   126
  // or given timeout, whichever is earlier. A timeout value
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 6763
diff changeset
   127
  // of 0 indicates that there is no upper bound on the wait time.
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 6763
diff changeset
   128
  // A concurrent full gc request terminates the wait.
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 6763
diff changeset
   129
  void wait_on_cms_lock(long t_millis);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 14583
diff changeset
   131
  // Wait on CMS lock until the next synchronous GC
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 14583
diff changeset
   132
  // or given timeout, whichever is earlier. A timeout value
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 14583
diff changeset
   133
  // of 0 indicates that there is no upper bound on the wait time.
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 14583
diff changeset
   134
  // A concurrent full gc request terminates the wait.
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 14583
diff changeset
   135
  void wait_on_cms_lock_for_scavenge(long t_millis);
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 14583
diff changeset
   136
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   137
  // The CMS thread will yield during the work portion of its cycle
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  // only when requested to.  Both synchronous and asychronous requests
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   139
  // are provided:
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   140
  // (1) A synchronous request is used for young gen collections and
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   141
  //     for direct allocations.  The requesting thread increments
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   142
  //     _pending_yields at the beginning of an operation, and decrements
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   143
  //     _pending_yields when that operation is completed.
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   144
  //     In turn, the CMS thread yields when _pending_yields is positive,
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   145
  //     and continues to yield until the value reverts to 0.
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   146
  // (2) An asynchronous request, on the other hand, is used by iCMS
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   147
  //     for the stop_icms() operation. A single yield satisfies all of
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   148
  //     the outstanding asynch yield requests, of which there may
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   149
  //     occasionally be several in close succession. To accomplish
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   150
  //     this, an asynch-requesting thread atomically increments both
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   151
  //     _pending_yields and _pending_decrements. An asynchr requesting
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   152
  //     thread does not wait and "acknowledge" completion of an operation
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   153
  //     and deregister the request, like the synchronous version described
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   154
  //     above does. In turn, after yielding, the CMS thread decrements both
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   155
  //     _pending_yields and _pending_decrements by the value seen in
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   156
  //     _pending_decrements before the decrement.
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   157
  //  NOTE: The above scheme is isomorphic to having two request counters,
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   158
  //  one for async requests and one for sync requests, and for the CMS thread
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   159
  //  to check the sum of the two counters to decide whether it should yield
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   160
  //  and to clear only the async counter when it yields. However, it turns out
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   161
  //  to be more efficient for CMS code to just check a single counter
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   162
  //  _pending_yields that holds the sum (of both sync and async requests), and
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   163
  //  a second counter _pending_decrements that only holds the async requests,
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   164
  //  for greater efficiency, since in a typical CMS run, there are many more
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 22234
diff changeset
   165
  //  potential (i.e. static) yield points than there are actual
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   166
  //  (i.e. dynamic) yields because of requests, which are few and far between.
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   167
  //
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  // Note that, while "_pending_yields >= _pending_decrements" is an invariant,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  // we cannot easily test that invariant, since the counters are manipulated via
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  // atomic instructions without explicit locking and we cannot read
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  // the two counters atomically together: one suggestion is to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  // use (for example) 16-bit counters so as to be able to read the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  // two counters atomically even on 32-bit platforms. Notice that
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   174
  // the second assert in acknowledge_yield_request() below does indeed
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  // check a form of the above invariant, albeit indirectly.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  static void increment_pending_yields()   {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
    Atomic::inc(&_pending_yields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    assert(_pending_yields >= 0, "can't be negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  static void decrement_pending_yields()   {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    Atomic::dec(&_pending_yields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    assert(_pending_yields >= 0, "can't be negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  static void asynchronous_yield_request() {
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   186
    assert(CMSIncrementalMode, "Currently only used w/iCMS");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    increment_pending_yields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    Atomic::inc(&_pending_decrements);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    assert(_pending_decrements >= 0, "can't be negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  static void acknowledge_yield_request() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    jint decrement = _pending_decrements;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    if (decrement > 0) {
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   194
      assert(CMSIncrementalMode, "Currently only used w/iCMS");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
      // Order important to preserve: _pending_yields >= _pending_decrements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      Atomic::add(-decrement, &_pending_decrements);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      Atomic::add(-decrement, &_pending_yields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      assert(_pending_decrements >= 0, "can't be negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      assert(_pending_yields >= 0, "can't be negative");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  static bool should_yield()   { return _pending_yields > 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  // CMS incremental mode.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  static void start_icms(); // notify thread to start a quantum of work
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  static void stop_icms();  // request thread to stop working
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  void icms_wait();         // if asked to stop, wait until notified to start
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  // Incremental mode is enabled globally by the flag CMSIncrementalMode.  It
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  // must also be enabled/disabled dynamically to allow foreground collections.
8684
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   211
#define ICMS_ENABLING_ASSERT                                                      \
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   212
          assert((CMSIncrementalMode  && _icms_disabled >= 0) ||                  \
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   213
                 (!CMSIncrementalMode && _icms_disabled <= 0), "Error")
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   214
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   215
  static inline void enable_icms() {
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   216
    ICMS_ENABLING_ASSERT;
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   217
    Atomic::dec(&_icms_disabled);
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   218
  }
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   219
  static inline void disable_icms() {
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   220
   ICMS_ENABLING_ASSERT;
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   221
   Atomic::inc(&_icms_disabled);
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   222
  }
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   223
  static inline bool icms_is_disabled() {
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   224
   ICMS_ENABLING_ASSERT;
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   225
   return _icms_disabled > 0;
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   226
  }
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   227
  static inline bool icms_is_enabled() {
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   228
   return !icms_is_disabled();
7ebbd0b3e295 6987703: iCMS: Intermittent hang with gc/gctests/CallGC/CallGC01 and +ExplicitGCInvokesConcurrent
ysr
parents: 7397
diff changeset
   229
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
inline void ConcurrentMarkSweepThread::trace_state(const char* desc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  if (CMSTraceThreadState) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    char buf[128];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    TimeStamp& ts = gclog_or_tty->time_stamp();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    if (!ts.is_updated()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      ts.update();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    jio_snprintf(buf, sizeof(buf), " [%.3f:  CMSThread %s] ",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
                 ts.seconds(), desc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    buf[sizeof(buf) - 1] = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    gclog_or_tty->print(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
6763
711b8a44c4dd 6692906: CMS: parallel concurrent marking may be prone to hanging or stalling mutators for periods of time
ysr
parents: 5547
diff changeset
   246
// For scoped increment/decrement of (synchronous) yield requests
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
class CMSSynchronousYieldRequest: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  CMSSynchronousYieldRequest() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
    ConcurrentMarkSweepThread::increment_pending_yields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  ~CMSSynchronousYieldRequest() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    ConcurrentMarkSweepThread::decrement_pending_yields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
// Used to emit a warning in case of unexpectedly excessive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
// looping (in "apparently endless loops") in CMS code.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
class CMSLoopCountWarn: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  const char* _src;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  const char* _msg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  const intx  _threshold;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  intx        _ticks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  inline CMSLoopCountWarn(const char* src, const char* msg,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
                          const intx threshold) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    _src(src), _msg(msg), _threshold(threshold), _ticks(0) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  inline void tick() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    _ticks++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    if (CMSLoopWarn && _ticks % _threshold == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
      warning("%s has looped %d times %s", _src, _ticks, _msg);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
   278
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
   279
#endif // SHARE_VM_GC_IMPLEMENTATION_CONCURRENTMARKSWEEP_CONCURRENTMARKSWEEPTHREAD_HPP