src/hotspot/share/gc/shared/concurrentGCPhaseManager.cpp
author stefank
Thu, 22 Feb 2018 18:36:32 +0100
changeset 49048 4e8c86b75428
parent 47216 71c04702a3d5
child 54623 1126f0607c70
permissions -rw-r--r--
8198525: Move _size_policy out of GenCollectorPolicy into GenCollectedHeap Reviewed-by: pliden, sjohanss
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46384
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     1
/*
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     4
 *
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     8
 *
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    14
 *
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    18
 *
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    21
 * questions.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    22
 *
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    23
 */
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    24
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    25
#include "precompiled.hpp"
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    26
#include "gc/shared/concurrentGCPhaseManager.hpp"
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    27
#include "runtime/mutexLocker.hpp"
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    28
#include "runtime/thread.hpp"
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    29
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    30
#define assert_ConcurrentGC_thread() \
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    31
  assert(Thread::current()->is_ConcurrentGC_thread(), "precondition")
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    32
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    33
#define assert_not_enter_unconstrained(phase) \
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    34
  assert((phase) != UNCONSTRAINED_PHASE, "Cannot enter \"unconstrained\" phase")
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    35
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    36
#define assert_manager_is_tos(manager, stack, kind)  \
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    37
  assert((manager) == (stack)->_top, kind " manager is not top of stack")
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    38
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    39
ConcurrentGCPhaseManager::Stack::Stack() :
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    40
  _requested_phase(UNCONSTRAINED_PHASE),
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    41
  _top(NULL)
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    42
{ }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    43
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    44
ConcurrentGCPhaseManager::ConcurrentGCPhaseManager(int phase, Stack* stack) :
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    45
  _phase(phase),
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    46
  _active(true),
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    47
  _prev(NULL),
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    48
  _stack(stack)
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    49
{
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    50
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    51
  assert_not_enter_unconstrained(phase);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    52
  assert(stack != NULL, "precondition");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    53
  MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    54
  if (stack->_top != NULL) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    55
    assert(stack->_top->_active, "precondition");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    56
    _prev = stack->_top;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    57
  }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    58
  stack->_top = this;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    59
  ml.notify_all();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    60
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    61
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    62
ConcurrentGCPhaseManager::~ConcurrentGCPhaseManager() {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    63
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    64
  MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    65
  assert_manager_is_tos(this, _stack, "This");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    66
  wait_when_requested_impl();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    67
  _stack->_top = _prev;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    68
  ml.notify_all();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    69
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    70
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    71
bool ConcurrentGCPhaseManager::is_requested() const {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    72
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    73
  MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    74
  assert_manager_is_tos(this, _stack, "This");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    75
  return _active && (_stack->_requested_phase == _phase);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    76
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    77
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    78
bool ConcurrentGCPhaseManager::wait_when_requested_impl() const {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    79
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    80
  assert_lock_strong(CGCPhaseManager_lock);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    81
  bool waited = false;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    82
  while (_active && (_stack->_requested_phase == _phase)) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    83
    waited = true;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    84
    CGCPhaseManager_lock->wait(Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    85
  }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    86
  return waited;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    87
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    88
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    89
bool ConcurrentGCPhaseManager::wait_when_requested() const {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    90
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    91
  MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    92
  assert_manager_is_tos(this, _stack, "This");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    93
  return wait_when_requested_impl();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    94
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    95
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    96
void ConcurrentGCPhaseManager::set_phase(int phase, bool force) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    97
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    98
  assert_not_enter_unconstrained(phase);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
    99
  MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   100
  assert_manager_is_tos(this, _stack, "This");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   101
  if (!force) wait_when_requested_impl();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   102
  _phase = phase;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   103
  ml.notify_all();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   104
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   105
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   106
void ConcurrentGCPhaseManager::deactivate() {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   107
  assert_ConcurrentGC_thread();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   108
  MonitorLockerEx ml(CGCPhaseManager_lock, Mutex::_no_safepoint_check_flag);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   109
  assert_manager_is_tos(this, _stack, "This");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   110
  _active = false;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   111
  ml.notify_all();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   112
}
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   113
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   114
bool ConcurrentGCPhaseManager::wait_for_phase(int phase, Stack* stack) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   115
  assert(Thread::current()->is_Java_thread(), "precondition");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   116
  assert(stack != NULL, "precondition");
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   117
  MonitorLockerEx ml(CGCPhaseManager_lock);
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   118
  // Update request and notify service of change.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   119
  if (stack->_requested_phase != phase) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   120
    stack->_requested_phase = phase;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   121
    ml.notify_all();
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   122
  }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   123
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   124
  if (phase == UNCONSTRAINED_PHASE) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   125
    return true;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   126
  }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   127
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   128
  // Wait until phase or IDLE is active.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   129
  while (true) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   130
    bool idle = false;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   131
    for (ConcurrentGCPhaseManager* manager = stack->_top;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   132
         manager != NULL;
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   133
         manager = manager->_prev) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   134
      if (manager->_phase == phase) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   135
        return true;            // phase is active.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   136
      } else if (manager->_phase == IDLE_PHASE) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   137
        idle = true;            // Note idle active, continue search for phase.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   138
      }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   139
    }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   140
    if (idle) {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   141
      return false;             // idle is active and phase is not.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   142
    } else {
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   143
      ml.wait();                // Wait for phase change.
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   144
    }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   145
  }
dacebddcdea0 8169517: WhiteBox should provide concurrent GC phase control
kbarrett
parents:
diff changeset
   146
}