src/hotspot/share/gc/cms/concurrentMarkSweepThread.cpp
author coleenp
Fri, 16 Mar 2018 09:12:13 -0400
changeset 49449 ef5d5d343e2a
parent 47622 817f2a7019e4
child 54623 1126f0607c70
permissions -rw-r--r--
8199263: Split interfaceSupport.hpp to not require including .inline.hpp files Summary: interfaceSupport.hpp is an inline file so moved to interfaceSupport.inline.hpp and stopped including it in .hpp files Reviewed-by: stefank, rehn, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
47622
817f2a7019e4 8179387: Factor out CMS specific code from GenCollectedHeap into its own subclass
rkennke
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2017, 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
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    26
#include "classfile/systemDictionary.hpp"
47622
817f2a7019e4 8179387: Factor out CMS specific code from GenCollectedHeap into its own subclass
rkennke
parents: 47216
diff changeset
    27
#include "gc/cms/cmsHeap.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 29464
diff changeset
    28
#include "gc/cms/concurrentMarkSweepGeneration.inline.hpp"
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 29464
diff changeset
    29
#include "gc/cms/concurrentMarkSweepThread.hpp"
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 32623
diff changeset
    30
#include "gc/shared/gcId.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    31
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    32
#include "runtime/init.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    33
#include "runtime/java.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    34
#include "runtime/javaCalls.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    35
#include "runtime/mutexLocker.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    36
#include "runtime/os.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6984
diff changeset
    37
#include "runtime/vmThread.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// ======= Concurrent Mark Sweep Thread ========
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
32623
390a27af5657 8134626: Misc cleanups after generation array removal
jwilhelm
parents: 30764
diff changeset
    41
ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::_cmst = NULL;
390a27af5657 8134626: Misc cleanups after generation array removal
jwilhelm
parents: 30764
diff changeset
    42
CMSCollector* ConcurrentMarkSweepThread::_collector         = NULL;
390a27af5657 8134626: Misc cleanups after generation array removal
jwilhelm
parents: 30764
diff changeset
    43
int  ConcurrentMarkSweepThread::_CMS_flag                   = CMS_nil;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
32623
390a27af5657 8134626: Misc cleanups after generation array removal
jwilhelm
parents: 30764
diff changeset
    45
volatile jint ConcurrentMarkSweepThread::_pending_yields    = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  : ConcurrentGCThread() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  assert(UseConcMarkSweepGC,  "UseConcMarkSweepGC should be set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  assert(_cmst == NULL, "CMS thread already created");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  _cmst = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  assert(_collector == NULL, "Collector already set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  _collector = collector;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
29464
02c245ad3ec9 8073545: Use shorter and more descriptive names for GC worker threads
david
parents: 29326
diff changeset
    55
  set_name("CMS Main Thread");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    57
  // An old comment here said: "Priority should be just less
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    58
  // than that of VMThread".  Since the VMThread runs at
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    59
  // NearMaxPriority, the old comment was inaccurate, but
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    60
  // changing the default priority to NearMaxPriority-1
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    61
  // could change current behavior, so the default of
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    62
  // NearMaxPriority stays in place.
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    63
  //
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    64
  // Note that there's a possibility of the VMThread
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    65
  // starving if UseCriticalCMSThreadPriority is on.
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    66
  // That won't happen on Solaris for various reasons,
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    67
  // but may well happen on non-Solaris platforms.
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    68
  create_and_start(UseCriticalCMSThreadPriority ? CriticalPriority : NearMaxPriority);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    71
void ConcurrentMarkSweepThread::run_service() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  assert(this == cmst(), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) {
37073
c39d0903390b 8151605: Change warning() to log_warning(gc) in the GC code
brutisso
parents: 34633
diff changeset
    75
    log_warning(gc)("Couldn't bind CMS thread to processor " UINTX_FORMAT, CPUForCMSThread);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  }
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    77
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    78
  while (!should_terminate()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    sleepBeforeNextCycle();
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    80
    if (should_terminate()) break;
33107
77bf0d2069a3 8134953: Make the GC ID available in a central place
brutisso
parents: 32623
diff changeset
    81
    GCIdMark gc_id_mark;
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 15859
diff changeset
    82
    GCCause::Cause cause = _collector->_full_gc_requested ?
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 15859
diff changeset
    83
      _collector->_full_gc_cause : GCCause::_cms_concurrent_mark;
27686
f91c482793e6 8064702: Remove the CMS foreground collector
brutisso
parents: 27625
diff changeset
    84
    _collector->collect_in_background(cause);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  }
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
    86
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // Check that the state of any protocol for synchronization
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  // between background (CMS) and foreground collector is "clean"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  // (i.e. will not potentially block the foreground collector,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // requiring action by us).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  verify_ok_to_terminate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
void ConcurrentMarkSweepThread::verify_ok_to_terminate() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
           cms_thread_wants_cms_token()),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
         "Must renounce all worldly possessions and desires for nirvana");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  _collector->verify_ok_to_terminate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
// create and start a new ConcurrentMarkSweep Thread for given CMS generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) {
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   105
  guarantee(_cmst == NULL, "start() called twice!");
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   106
  ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector);
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   107
  assert(_cmst == th, "Where did the just-created CMS thread go?");
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   108
  return th;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   111
void ConcurrentMarkSweepThread::stop_service() {
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   112
  // Now post a notify on CGC_lock so as to nudge
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   113
  // CMS thread(s) that might be slumbering in
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   114
  // sleepBeforeNextCycle.
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   115
  MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   116
  CGC_lock->notify_all();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  assert(tc != NULL, "Null ThreadClosure");
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   121
  if (cmst() != NULL && !cmst()->has_terminated()) {
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   122
    tc->do_thread(cmst());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  assert(Universe::is_fully_initialized(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
         "Called too early, make sure heap is fully initialized");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  if (_collector != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    AbstractWorkGang* gang = _collector->conc_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    if (gang != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
      gang->threads_do(tc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
void ConcurrentMarkSweepThread::print_all_on(outputStream* st) {
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   135
  if (cmst() != NULL && !cmst()->has_terminated()) {
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   136
    cmst()->print_on(st);
13857
93015306affa 7194254: jstack reports wrong thread priorities
dholmes
parents: 11601
diff changeset
   137
    st->cr();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  if (_collector != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    AbstractWorkGang* gang = _collector->conc_workers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    if (gang != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      gang->print_worker_threads_on(st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  assert(UseConcMarkSweepGC, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  MutexLockerEx x(CGC_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
                  Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  if (!is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    assert(Thread::current()->is_VM_thread(), "Not a VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    CMSSynchronousYieldRequest yr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    while (CMS_flag_is_set(CMS_cms_has_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
      // indicate that we want to get the token
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
      set_CMS_flag(CMS_vm_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      CGC_lock->wait(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    // claim the token and proceed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    clear_CMS_flag(CMS_vm_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    set_CMS_flag(CMS_vm_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    assert(Thread::current()->is_ConcurrentGC_thread(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
           "Not a CMS thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    // The following barrier assumes there's only one CMS thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    // This will need to be modified is there are more CMS threads than one.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      set_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      CGC_lock->wait(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    // claim the token
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    clear_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    set_CMS_flag(CMS_cms_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  assert(UseConcMarkSweepGC, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  MutexLockerEx x(CGC_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
                  Mutex::_no_safepoint_check_flag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  if (!is_cms_thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    assert(Thread::current()->is_VM_thread(), "Not a VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    assert(CMS_flag_is_set(CMS_vm_has_token), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    clear_CMS_flag(CMS_vm_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    if (CMS_flag_is_set(CMS_cms_wants_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
      // wake-up a waiting CMS thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
      CGC_lock->notify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
           "Should have been cleared");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    assert(Thread::current()->is_ConcurrentGC_thread(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
           "Not a CMS thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    assert(CMS_flag_is_set(CMS_cms_has_token), "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    clear_CMS_flag(CMS_cms_has_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
    if (CMS_flag_is_set(CMS_vm_wants_token)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
      // wake-up a waiting VM thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
      CGC_lock->notify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
           "Should have been cleared");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   207
// Wait until any cms_lock event
6984
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 5547
diff changeset
   208
void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  MutexLockerEx x(CGC_lock,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
                  Mutex::_no_safepoint_check_flag);
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   211
  if (should_terminate() || _collector->_full_gc_requested) {
6984
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 5547
diff changeset
   212
    return;
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 5547
diff changeset
   213
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  set_CMS_flag(CMS_cms_wants_token);   // to provoke notifies
6984
c6718f921eb6 6992998: CMSWaitDuration=0 causes hangs with +ExplicitGCInvokesConcurrent
ysr
parents: 5547
diff changeset
   215
  CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  clear_CMS_flag(CMS_cms_wants_token);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
         "Should not be set");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   221
// Wait until the next synchronous GC, a concurrent full gc request,
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   222
// or a timeout, whichever is earlier.
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   223
void ConcurrentMarkSweepThread::wait_on_cms_lock_for_scavenge(long t_millis) {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   224
  // Wait time in millis or 0 value representing infinite wait for a scavenge
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   225
  assert(t_millis >= 0, "Wait time for scavenge should be 0 or positive");
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   226
47622
817f2a7019e4 8179387: Factor out CMS specific code from GenCollectedHeap into its own subclass
rkennke
parents: 47216
diff changeset
   227
  CMSHeap* heap = CMSHeap::heap();
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   228
  double start_time_secs = os::elapsedTime();
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   229
  double end_time_secs = start_time_secs + (t_millis / ((double) MILLIUNITS));
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   230
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   231
  // Total collections count before waiting loop
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   232
  unsigned int before_count;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   233
  {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   234
    MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag);
47622
817f2a7019e4 8179387: Factor out CMS specific code from GenCollectedHeap into its own subclass
rkennke
parents: 47216
diff changeset
   235
    before_count = heap->total_collections();
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   236
  }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   237
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   238
  unsigned int loop_count = 0;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   239
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   240
  while(!should_terminate()) {
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   241
    double now_time = os::elapsedTime();
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   242
    long wait_time_millis;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   243
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   244
    if(t_millis != 0) {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   245
      // New wait limit
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   246
      wait_time_millis = (long) ((end_time_secs - now_time) * MILLIUNITS);
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   247
      if(wait_time_millis <= 0) {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   248
        // Wait time is over
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   249
        break;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   250
      }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   251
    } else {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   252
      // No wait limit, wait if necessary forever
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   253
      wait_time_millis = 0;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   254
    }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   255
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   256
    // Wait until the next event or the remaining timeout
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   257
    {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   258
      MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag);
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   259
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   260
      if (should_terminate() || _collector->_full_gc_requested) {
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   261
        return;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   262
      }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   263
      set_CMS_flag(CMS_cms_wants_token);   // to provoke notifies
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   264
      assert(t_millis == 0 || wait_time_millis > 0, "Sanity");
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   265
      CGC_lock->wait(Mutex::_no_safepoint_check_flag, wait_time_millis);
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   266
      clear_CMS_flag(CMS_cms_wants_token);
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   267
      assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token),
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   268
             "Should not be set");
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   269
    }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   270
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   271
    // Extra wait time check before entering the heap lock to get the collection count
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   272
    if(t_millis != 0 && os::elapsedTime() >= end_time_secs) {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   273
      // Wait time is over
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   274
      break;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   275
    }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   276
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   277
    // Total collections count after the event
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   278
    unsigned int after_count;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   279
    {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   280
      MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag);
47622
817f2a7019e4 8179387: Factor out CMS specific code from GenCollectedHeap into its own subclass
rkennke
parents: 47216
diff changeset
   281
      after_count = heap->total_collections();
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   282
    }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   283
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   284
    if(before_count != after_count) {
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   285
      // There was a collection - success
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   286
      break;
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   287
    }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   288
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   289
    // Too many loops warning
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   290
    if(++loop_count == 0) {
37073
c39d0903390b 8151605: Change warning() to log_warning(gc) in the GC code
brutisso
parents: 34633
diff changeset
   291
      log_warning(gc)("wait_on_cms_lock_for_scavenge() has looped %u times", loop_count - 1);
15859
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   292
    }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   293
  }
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   294
}
b8d7620fb179 7189971: Implement CMSWaitDuration for non-incremental mode of CMS
jmasa
parents: 13857
diff changeset
   295
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
void ConcurrentMarkSweepThread::sleepBeforeNextCycle() {
37081
7656f5356a5d 8140257: Add support for "gc service threads" to ConcurrentGCThread
drwhite
parents: 37073
diff changeset
   297
  while (!should_terminate()) {
27625
07829380b8cd 8061308: Remove iCMS
brutisso
parents: 24424
diff changeset
   298
    if(CMSWaitDuration >= 0) {
07829380b8cd 8061308: Remove iCMS
brutisso
parents: 24424
diff changeset
   299
      // Wait until the next synchronous GC, a concurrent full gc
07829380b8cd 8061308: Remove iCMS
brutisso
parents: 24424
diff changeset
   300
      // request or a timeout, whichever is earlier.
07829380b8cd 8061308: Remove iCMS
brutisso
parents: 24424
diff changeset
   301
      wait_on_cms_lock_for_scavenge(CMSWaitDuration);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    } else {
27625
07829380b8cd 8061308: Remove iCMS
brutisso
parents: 24424
diff changeset
   303
      // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently
07829380b8cd 8061308: Remove iCMS
brutisso
parents: 24424
diff changeset
   304
      wait_on_cms_lock(CMSCheckInterval);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    // Check if we should start a CMS collection cycle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    if (_collector->shouldConcurrentCollect()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    // .. collection criterion not yet met, let's go back
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    // and wait some more
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
}