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