hotspot/src/share/vm/runtime/sweeper.cpp
author thartmann
Wed, 17 Sep 2014 08:00:07 +0200
changeset 26796 666464578742
parent 26578 1cb9e59a06ab
child 26799 0c349e16bbe8
permissions -rw-r--r--
8015774: Add support for multiple code heaps Summary: Support for segmentation of the code cache. Separate code heaps are created and used to store code of different types. Reviewed-by: kvn, iveresov, roland, anoll, egahlin, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 24351
diff changeset
     2
 * Copyright (c) 1997, 2014, 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: 5533
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5533
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: 5533
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: 6453
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    26
#include "code/codeCache.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
    27
#include "code/compiledIC.hpp"
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
    28
#include "code/icBuffer.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    29
#include "code/nmethod.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    30
#include "compiler/compileBroker.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    31
#include "memory/resourceArea.hpp"
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
    32
#include "oops/method.hpp"
25351
7c198a690050 8044775: Improve usage of umbrella header atomic.inline.hpp.
goetz
parents: 24445
diff changeset
    33
#include "runtime/atomic.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    34
#include "runtime/compilationPolicy.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    35
#include "runtime/mutexLocker.hpp"
24351
61b33cc6d3cf 8042195: Introduce umbrella header orderAccess.inline.hpp.
goetz
parents: 22872
diff changeset
    36
#include "runtime/orderAccess.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    37
#include "runtime/os.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    38
#include "runtime/sweeper.hpp"
24351
61b33cc6d3cf 8042195: Introduce umbrella header orderAccess.inline.hpp.
goetz
parents: 22872
diff changeset
    39
#include "runtime/thread.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    40
#include "runtime/vm_operations.hpp"
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
    41
#include "trace/tracing.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    42
#include "utilities/events.hpp"
21767
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
    43
#include "utilities/ticks.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6453
diff changeset
    44
#include "utilities/xmlstream.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 24351
diff changeset
    46
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 24351
diff changeset
    47
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    48
#ifdef ASSERT
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    49
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    50
#define SWEEP(nm) record_sweep(nm, __LINE__)
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    51
// Sweeper logging code
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    52
class SweeperRecord {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    53
 public:
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    54
  int traversal;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    55
  int invocation;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    56
  int compile_id;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    57
  long traversal_mark;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    58
  int state;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    59
  const char* kind;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    60
  address vep;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    61
  address uep;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    62
  int line;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    63
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    64
  void print() {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    65
      tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    66
                    PTR_FORMAT " state = %d traversal_mark %d line = %d",
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    67
                    traversal,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    68
                    invocation,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    69
                    compile_id,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    70
                    kind == NULL ? "" : kind,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    71
                    uep,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    72
                    vep,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    73
                    state,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    74
                    traversal_mark,
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    75
                    line);
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    76
  }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    77
};
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    78
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    79
static int _sweep_index = 0;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    80
static SweeperRecord* _records = NULL;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    81
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    82
void NMethodSweeper::report_events(int id, address entry) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    83
  if (_records != NULL) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    84
    for (int i = _sweep_index; i < SweeperLogEntries; i++) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    85
      if (_records[i].uep == entry ||
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    86
          _records[i].vep == entry ||
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    87
          _records[i].compile_id == id) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    88
        _records[i].print();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    89
      }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    90
    }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    91
    for (int i = 0; i < _sweep_index; i++) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    92
      if (_records[i].uep == entry ||
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    93
          _records[i].vep == entry ||
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    94
          _records[i].compile_id == id) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    95
        _records[i].print();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    96
      }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    97
    }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    98
  }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
    99
}
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   100
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   101
void NMethodSweeper::report_events() {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   102
  if (_records != NULL) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   103
    for (int i = _sweep_index; i < SweeperLogEntries; i++) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   104
      // skip empty records
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   105
      if (_records[i].vep == NULL) continue;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   106
      _records[i].print();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   107
    }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   108
    for (int i = 0; i < _sweep_index; i++) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   109
      // skip empty records
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   110
      if (_records[i].vep == NULL) continue;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   111
      _records[i].print();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   112
    }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   113
  }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   114
}
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   115
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   116
void NMethodSweeper::record_sweep(nmethod* nm, int line) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   117
  if (_records != NULL) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   118
    _records[_sweep_index].traversal = _traversals;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   119
    _records[_sweep_index].traversal_mark = nm->_stack_traversal_mark;
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   120
    _records[_sweep_index].invocation = _sweep_fractions_left;
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   121
    _records[_sweep_index].compile_id = nm->compile_id();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   122
    _records[_sweep_index].kind = nm->compile_kind();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   123
    _records[_sweep_index].state = nm->_state;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   124
    _records[_sweep_index].vep = nm->verified_entry_point();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   125
    _records[_sweep_index].uep = nm->entry_point();
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   126
    _records[_sweep_index].line = line;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   127
    _sweep_index = (_sweep_index + 1) % SweeperLogEntries;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   128
  }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   129
}
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   130
#else
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   131
#define SWEEP(nm)
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   132
#endif
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   133
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   134
NMethodIterator NMethodSweeper::_current;                      // Current nmethod
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   135
long     NMethodSweeper::_traversals                   = 0;    // Stack scan count, also sweep ID.
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   136
long     NMethodSweeper::_total_nof_code_cache_sweeps  = 0;    // Total number of full sweeps of the code cache
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   137
long     NMethodSweeper::_time_counter                 = 0;    // Virtual time used to periodically invoke sweeper
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   138
long     NMethodSweeper::_last_sweep                   = 0;    // Value of _time_counter when the last sweep happened
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   139
int      NMethodSweeper::_seen                         = 0;    // Nof. nmethod we have currently processed in current pass of CodeCache
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   140
int      NMethodSweeper::_flushed_count                = 0;    // Nof. nmethods flushed in current sweep
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   141
int      NMethodSweeper::_zombified_count              = 0;    // Nof. nmethods made zombie in current sweep
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   142
int      NMethodSweeper::_marked_for_reclamation_count = 0;    // Nof. nmethods marked for reclaim in current sweep
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   144
volatile bool NMethodSweeper::_should_sweep            = true; // Indicates if we should invoke the sweeper
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   145
volatile int  NMethodSweeper::_sweep_fractions_left    = 0;    // Nof. invocations left until we are completed with this pass
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   146
volatile int  NMethodSweeper::_sweep_started           = 0;    // Flag to control conc sweeper
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   147
volatile int  NMethodSweeper::_bytes_changed           = 0;    // Counts the total nmethod size if the nmethod changed from:
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   148
                                                               //   1) alive       -> not_entrant
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   149
                                                               //   2) not_entrant -> zombie
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   150
                                                               //   3) zombie      -> marked_for_reclamation
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   151
int    NMethodSweeper::_hotness_counter_reset_val       = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   153
long   NMethodSweeper::_total_nof_methods_reclaimed     = 0;   // Accumulated nof methods flushed
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   154
long   NMethodSweeper::_total_nof_c2_methods_reclaimed  = 0;   // Accumulated nof methods flushed
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   155
size_t NMethodSweeper::_total_flushed_size              = 0;   // Total number of bytes flushed from the code cache
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   156
Tickspan NMethodSweeper::_total_time_sweeping;                 // Accumulated time sweeping
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   157
Tickspan NMethodSweeper::_total_time_this_sweep;               // Total time this sweep
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   158
Tickspan NMethodSweeper::_peak_sweep_time;                     // Peak time for a full sweep
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   159
Tickspan NMethodSweeper::_peak_sweep_fraction_time;            // Peak time sweeping one fraction
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   160
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   161
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   162
class MarkActivationClosure: public CodeBlobClosure {
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   163
public:
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   164
  virtual void do_code_blob(CodeBlob* cb) {
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   165
    assert(cb->is_nmethod(), "CodeBlob should be nmethod");
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   166
    nmethod* nm = (nmethod*)cb;
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   167
    nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   168
    // If we see an activation belonging to a non_entrant nmethod, we mark it.
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   169
    if (nm->is_not_entrant()) {
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   170
      nm->mark_as_seen_on_stack();
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   171
    }
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   172
  }
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   173
};
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   174
static MarkActivationClosure mark_activation_closure;
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   175
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   176
class SetHotnessClosure: public CodeBlobClosure {
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   177
public:
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   178
  virtual void do_code_blob(CodeBlob* cb) {
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   179
    assert(cb->is_nmethod(), "CodeBlob should be nmethod");
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   180
    nmethod* nm = (nmethod*)cb;
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   181
    nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   182
  }
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   183
};
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   184
static SetHotnessClosure set_hotness_closure;
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   185
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   186
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   187
int NMethodSweeper::hotness_counter_reset_val() {
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   188
  if (_hotness_counter_reset_val == 0) {
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   189
    _hotness_counter_reset_val = (ReservedCodeCacheSize < M) ? 1 : (ReservedCodeCacheSize / M) * 2;
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   190
  }
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   191
  return _hotness_counter_reset_val;
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   192
}
17132
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   193
bool NMethodSweeper::sweep_in_progress() {
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   194
  return !_current.end();
17132
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   195
}
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   196
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   197
// Scans the stacks of all Java threads and marks activations of not-entrant methods.
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   198
// No need to synchronize access, since 'mark_active_nmethods' is always executed at a
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   199
// safepoint.
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   200
void NMethodSweeper::mark_active_nmethods() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   202
  // If we do not want to reclaim not-entrant or zombie methods there is no need
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   203
  // to scan stacks
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   204
  if (!MethodFlushing) {
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   205
    return;
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   206
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   208
  // Increase time so that we can estimate when to invoke the sweeper again.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   209
  _time_counter++;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   210
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  // Check for restart
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   212
  assert(CodeCache::find_blob_unsafe(_current.method()) == _current.method(), "Sweeper nmethod cached state invalid");
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   213
  if (!sweep_in_progress()) {
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   214
    _seen = 0;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   215
    _sweep_fractions_left = NmethodSweepFraction;
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   216
    _current = NMethodIterator();
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   217
    // Initialize to first nmethod
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   218
    _current.next();
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   219
    _traversals += 1;
21767
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
   220
    _total_time_this_sweep = Tickspan();
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   221
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    if (PrintMethodFlushing) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      tty->print_cr("### Sweep: stack traversal %d", _traversals);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    }
3908
24b55ad4c228 6863023: need non-perm oops in code cache for JSR 292
jrose
parents: 1
diff changeset
   225
    Threads::nmethods_do(&mark_activation_closure);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   227
  } else {
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   228
    // Only set hotness counter
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   229
    Threads::nmethods_do(&set_hotness_closure);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   232
  OrderAccess::storestore();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
}
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   234
/**
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   235
 * This function invokes the sweeper if at least one of the three conditions is met:
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   236
 *    (1) The code cache is getting full
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   237
 *    (2) There are sufficient state changes in/since the last sweep.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   238
 *    (3) We have not been sweeping for 'some time'
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   239
 */
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   240
void NMethodSweeper::possibly_sweep() {
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   241
  assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
21727
4a5fc611c9a7 8028306: nsk stress tests, CodeCache fills, then safepoint asserts
anoll
parents: 21575
diff changeset
   242
  // Only compiler threads are allowed to sweep
4a5fc611c9a7 8028306: nsk stress tests, CodeCache fills, then safepoint asserts
anoll
parents: 21575
diff changeset
   243
  if (!MethodFlushing || !sweep_in_progress() || !Thread::current()->is_Compiler_thread()) {
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   244
    return;
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   245
  }
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   246
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   247
  // If there was no state change while nmethod sweeping, 'should_sweep' will be false.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   248
  // This is one of the two places where should_sweep can be set to true. The general
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   249
  // idea is as follows: If there is enough free space in the code cache, there is no
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   250
  // need to invoke the sweeper. The following formula (which determines whether to invoke
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   251
  // the sweeper or not) depends on the assumption that for larger ReservedCodeCacheSizes
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   252
  // we need less frequent sweeps than for smaller ReservedCodecCacheSizes. Furthermore,
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   253
  // the formula considers how much space in the code cache is currently used. Here are
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   254
  // some examples that will (hopefully) help in understanding.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   255
  //
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   256
  // Small ReservedCodeCacheSizes:  (e.g., < 16M) We invoke the sweeper every time, since
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   257
  //                                              the result of the division is 0. This
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   258
  //                                              keeps the used code cache size small
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   259
  //                                              (important for embedded Java)
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   260
  // Large ReservedCodeCacheSize :  (e.g., 256M + code cache is 10% full). The formula
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   261
  //                                              computes: (256 / 16) - 1 = 15
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   262
  //                                              As a result, we invoke the sweeper after
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   263
  //                                              15 invocations of 'mark_active_nmethods.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   264
  // Large ReservedCodeCacheSize:   (e.g., 256M + code Cache is 90% full). The formula
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   265
  //                                              computes: (256 / 16) - 10 = 6.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   266
  if (!_should_sweep) {
22208
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   267
    const int time_since_last_sweep = _time_counter - _last_sweep;
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   268
    // ReservedCodeCacheSize has an 'unsigned' type. We need a 'signed' type for max_wait_time,
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   269
    // since 'time_since_last_sweep' can be larger than 'max_wait_time'. If that happens using
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   270
    // an unsigned type would cause an underflow (wait_until_next_sweep becomes a large positive
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   271
    // value) that disables the intended periodic sweeps.
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   272
    const int max_wait_time = ReservedCodeCacheSize / (16 * M);
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   273
    double wait_until_next_sweep = max_wait_time - time_since_last_sweep -
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   274
        MAX2(CodeCache::reverse_free_ratio(CodeBlobType::MethodProfiled),
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   275
             CodeCache::reverse_free_ratio(CodeBlobType::MethodNonProfiled));
22208
fb1e20bd389f 8029091: Bug in calculation of code cache sweeping interval
anoll
parents: 21767
diff changeset
   276
    assert(wait_until_next_sweep <= (double)max_wait_time, "Calculation of code cache sweeper interval is incorrect");
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   277
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   278
    if ((wait_until_next_sweep <= 0.0) || !CompileBroker::should_compile_new_jobs()) {
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   279
      _should_sweep = true;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   280
    }
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   281
  }
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   282
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   283
  if (_should_sweep && _sweep_fractions_left > 0) {
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   284
    // Only one thread at a time will sweep
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   285
    jint old = Atomic::cmpxchg( 1, &_sweep_started, 0 );
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   286
    if (old != 0) {
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   287
      return;
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   288
    }
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   289
#ifdef ASSERT
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   290
    if (LogSweeper && _records == NULL) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   291
      // Create the ring buffer for the logging code
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 11789
diff changeset
   292
      _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   293
      memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   294
    }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   295
#endif
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   296
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   297
    if (_sweep_fractions_left > 0) {
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   298
      sweep_code_cache();
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   299
      _sweep_fractions_left--;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   300
    }
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   301
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   302
    // We are done with sweeping the code cache once.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   303
    if (_sweep_fractions_left == 0) {
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   304
      _total_nof_code_cache_sweeps++;
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   305
      _last_sweep = _time_counter;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   306
      // Reset flag; temporarily disables sweeper
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   307
      _should_sweep = false;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   308
      // If there was enough state change, 'possibly_enable_sweeper()'
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   309
      // sets '_should_sweep' to true
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   310
      possibly_enable_sweeper();
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   311
      // Reset _bytes_changed only if there was enough state change. _bytes_changed
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   312
      // can further increase by calls to 'report_state_change'.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   313
      if (_should_sweep) {
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   314
        _bytes_changed = 0;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   315
      }
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   316
    }
22859
7b88983393b7 8029396: PPC64 (part 212): Several memory ordering fixes in C-code.
goetz
parents: 21767
diff changeset
   317
    // Release work, because another compiler thread could continue.
7b88983393b7 8029396: PPC64 (part 212): Several memory ordering fixes in C-code.
goetz
parents: 21767
diff changeset
   318
    OrderAccess::release_store((int*)&_sweep_started, 0);
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   319
  }
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   320
}
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   321
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   322
void NMethodSweeper::sweep_code_cache() {
21767
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
   323
  Ticks sweep_start_counter = Ticks::now();
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   324
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   325
  _flushed_count                = 0;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   326
  _zombified_count              = 0;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   327
  _marked_for_reclamation_count = 0;
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   328
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   329
  if (PrintMethodFlushing && Verbose) {
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   330
    tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left);
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   331
  }
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   332
17132
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   333
  if (!CompileBroker::should_compile_new_jobs()) {
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   334
    // If we have turned off compilations we might as well do full sweeps
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   335
    // in order to reach the clean state faster. Otherwise the sleeping compiler
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   336
    // threads will slow down sweeping.
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   337
    _sweep_fractions_left = 1;
17132
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   338
  }
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   339
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   340
  // We want to visit all nmethods after NmethodSweepFraction
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   341
  // invocations so divide the remaining number of nmethods by the
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   342
  // remaining number of invocations.  This is only an estimate since
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   343
  // the number of nmethods changes during the sweep so the final
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   344
  // stage must iterate until it there are no more nmethods.
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   345
  int todo = (CodeCache::nof_nmethods() - _seen) / _sweep_fractions_left;
20019
3e9220f93c61 8022883: Assertion failed: sweptCount >= flushedCount + markedCount + zombifiedCount
anoll
parents: 18025
diff changeset
   346
  int swept_count = 0;
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   347
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   348
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   349
  assert(!SafepointSynchronize::is_at_safepoint(), "should not be in safepoint when we get here");
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   350
  assert(!CodeCache_lock->owned_by_self(), "just checking");
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   351
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   352
  int freed_memory = 0;
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   353
  {
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   354
    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   355
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   356
    // The last invocation iterates until there are no more nmethods
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   357
    while ((swept_count < todo || _sweep_fractions_left == 1) && !_current.end()) {
20019
3e9220f93c61 8022883: Assertion failed: sweptCount >= flushedCount + markedCount + zombifiedCount
anoll
parents: 18025
diff changeset
   358
      swept_count++;
11789
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   359
      if (SafepointSynchronize::is_synchronizing()) { // Safepoint request
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   360
        if (PrintMethodFlushing && Verbose) {
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   361
          tty->print_cr("### Sweep at %d out of %d, invocation: %d, yielding to safepoint", _seen, CodeCache::nof_nmethods(), _sweep_fractions_left);
11789
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   362
        }
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   363
        MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   364
11789
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   365
        assert(Thread::current()->is_Java_thread(), "should be java thread");
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   366
        JavaThread* thread = (JavaThread*)Thread::current();
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   367
        ThreadBlockInVM tbivm(thread);
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   368
        thread->java_suspend_self();
2ff80dcfc1cf 7145345: Code cache sweeper must cooperate with safepoints
iveresov
parents: 9942
diff changeset
   369
      }
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   370
      // Since we will give up the CodeCache_lock, always skip ahead
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   371
      // to the next nmethod.  Other blobs can be deleted by other
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   372
      // threads but nmethods are only reclaimed by the sweeper.
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   373
      nmethod* nm = _current.method();
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   374
      _current.next();
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   375
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   376
      // Now ready to process nmethod and give up CodeCache_lock
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   377
      {
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   378
        MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   379
        freed_memory += process_nmethod(nm);
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   380
      }
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   381
      _seen++;
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   382
    }
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   383
  }
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   384
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   385
  assert(_sweep_fractions_left > 1 || _current.end(), "must have scanned the whole cache");
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   386
21767
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
   387
  const Ticks sweep_end_counter = Ticks::now();
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
   388
  const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   389
  _total_time_sweeping  += sweep_time;
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   390
  _total_time_this_sweep += sweep_time;
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   391
  _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   392
  _total_flushed_size += freed_memory;
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   393
  _total_nof_methods_reclaimed += _flushed_count;
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   394
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   395
  EventSweepCodeCache event(UNTIMED);
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   396
  if (event.should_commit()) {
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   397
    event.set_starttime(sweep_start_counter);
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   398
    event.set_endtime(sweep_end_counter);
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   399
    event.set_sweepIndex(_traversals);
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   400
    event.set_sweepFractionIndex(NmethodSweepFraction - _sweep_fractions_left + 1);
20019
3e9220f93c61 8022883: Assertion failed: sweptCount >= flushedCount + markedCount + zombifiedCount
anoll
parents: 18025
diff changeset
   401
    event.set_sweptCount(swept_count);
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   402
    event.set_flushedCount(_flushed_count);
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   403
    event.set_markedCount(_marked_for_reclamation_count);
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   404
    event.set_zombifiedCount(_zombified_count);
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   405
    event.commit();
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   406
  }
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   407
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   408
#ifdef ASSERT
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   409
  if(PrintMethodFlushing) {
21767
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
   410
    tty->print_cr("### sweeper:      sweep time(%d): "
41eaa9a17059 8028128: Add a type safe alternative for working with counter based data
mgronlun
parents: 21727
diff changeset
   411
      INT64_FORMAT, _sweep_fractions_left, (jlong)sweep_time.value());
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   412
  }
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   413
#endif
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   414
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   415
  if (_sweep_fractions_left == 1) {
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   416
    _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   417
    log_sweep("finished");
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   418
  }
17132
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   419
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   420
  // Sweeper is the only case where memory is released, check here if it
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   421
  // is time to restart the compiler. Only checking if there is a certain
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   422
  // amount of free memory in the code cache might lead to re-enabling
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   423
  // compilation although no memory has been released. For example, there are
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   424
  // cases when compilation was disabled although there is 4MB (or more) free
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   425
  // memory in the code cache. The reason is code cache fragmentation. Therefore,
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   426
  // it only makes sense to re-enable compilation if we have actually freed memory.
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   427
  // Note that typically several kB are released for sweeping 16MB of the code
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   428
  // cache. As a result, 'freed_memory' > 0 to restart the compiler.
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   429
  if (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0)) {
17132
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   430
    CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   431
    log_sweep("restart_compiler");
dffd513b2a8c 8012547: Code cache flushing can get stuck reclaming of memory
neliasso
parents: 13728
diff changeset
   432
  }
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   433
}
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   434
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   435
/**
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   436
 * This function updates the sweeper statistics that keep track of nmethods
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   437
 * state changes. If there is 'enough' state change, the sweeper is invoked
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   438
 * as soon as possible. There can be data races on _bytes_changed. The data
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   439
 * races are benign, since it does not matter if we loose a couple of bytes.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   440
 * In the worst case we call the sweeper a little later. Also, we are guaranteed
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   441
 * to invoke the sweeper if the code cache gets full.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   442
 */
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   443
void NMethodSweeper::report_state_change(nmethod* nm) {
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   444
  _bytes_changed += nm->total_size();
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   445
  possibly_enable_sweeper();
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   446
}
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   447
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   448
/**
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   449
 * Function determines if there was 'enough' state change in the code cache to invoke
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   450
 * the sweeper again. Currently, we determine 'enough' as more than 1% state change in
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   451
 * the code cache since the last sweep.
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   452
 */
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   453
void NMethodSweeper::possibly_enable_sweeper() {
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   454
  double percent_changed = ((double)_bytes_changed / (double)ReservedCodeCacheSize) * 100;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   455
  if (percent_changed > 1.0) {
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   456
    _should_sweep = true;
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   457
  }
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   458
}
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   459
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   460
class NMethodMarker: public StackObj {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   461
 private:
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   462
  CompilerThread* _thread;
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   463
 public:
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   464
  NMethodMarker(nmethod* nm) {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   465
    _thread = CompilerThread::current();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   466
    if (!nm->is_zombie() && !nm->is_unloaded()) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   467
      // Only expose live nmethods for scanning
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   468
      _thread->set_scanned_nmethod(nm);
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   469
    }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   470
  }
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   471
  ~NMethodMarker() {
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   472
    _thread->set_scanned_nmethod(NULL);
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   473
  }
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   474
};
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   475
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   476
void NMethodSweeper::release_nmethod(nmethod *nm) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   477
  // Clean up any CompiledICHolders
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   478
  {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   479
    ResourceMark rm;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   480
    MutexLocker ml_patch(CompiledIC_lock);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   481
    RelocIterator iter(nm);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   482
    while (iter.next()) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   483
      if (iter.type() == relocInfo::virtual_call_type) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   484
        CompiledIC::cleanup_call_site(iter.virtual_call_reloc());
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   485
      }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   486
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   487
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   488
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   489
  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   490
  nm->flush();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   491
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   493
int NMethodSweeper::process_nmethod(nmethod *nm) {
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   494
  assert(!CodeCache_lock->owned_by_self(), "just checking");
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   495
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   496
  int freed_memory = 0;
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   497
  // Make sure this nmethod doesn't get unloaded during the scan,
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   498
  // since safepoints may happen during acquired below locks.
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   499
  NMethodMarker nmm(nm);
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   500
  SWEEP(nm);
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   501
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
  // Skip methods that are currently referenced by the VM
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
  if (nm->is_locked_by_vm()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
    // But still remember to clean-up inline caches for alive nmethods
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
    if (nm->is_alive()) {
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   506
      // Clean inline caches that point to zombie/non-entrant methods
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   507
      MutexLocker cl(CompiledIC_lock);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
      nm->cleanup_inline_caches();
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   509
      SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
    }
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   511
    return freed_memory;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  if (nm->is_zombie()) {
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   515
    // If it is the first time we see nmethod then we mark it. Otherwise,
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   516
    // we reclaim it. When we have seen a zombie method twice, we know that
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   517
    // there are no inline caches that refer to it.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
    if (nm->is_marked_for_reclamation()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
      assert(!nm->is_locked_by_vm(), "must not flush locked nmethods");
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   520
      if (PrintMethodFlushing && Verbose) {
4750
71fd601907dc 4360113: Evict nmethods when code cache gets full
kvn
parents: 3919
diff changeset
   521
        tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (marked for reclamation) being flushed", nm->compile_id(), nm);
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   522
      }
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   523
      freed_memory = nm->total_size();
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   524
      if (nm->is_compiled_by_c2()) {
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   525
        _total_nof_c2_methods_reclaimed++;
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   526
      }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   527
      release_nmethod(nm);
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   528
      _flushed_count++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    } else {
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   530
      if (PrintMethodFlushing && Verbose) {
4750
71fd601907dc 4360113: Evict nmethods when code cache gets full
kvn
parents: 3919
diff changeset
   531
        tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (zombie) being marked for reclamation", nm->compile_id(), nm);
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   532
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
      nm->mark_for_reclamation();
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   534
      // Keep track of code cache state change
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   535
      _bytes_changed += nm->total_size();
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   536
      _marked_for_reclamation_count++;
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   537
      SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  } else if (nm->is_not_entrant()) {
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   540
    // If there are no current activations of this method on the
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
    // stack we can safely convert it to a zombie method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
    if (nm->can_not_entrant_be_converted()) {
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   543
      if (PrintMethodFlushing && Verbose) {
4750
71fd601907dc 4360113: Evict nmethods when code cache gets full
kvn
parents: 3919
diff changeset
   544
        tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (not entrant) being made zombie", nm->compile_id(), nm);
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   545
      }
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   546
      // Code cache state change is tracked in make_zombie()
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
      nm->make_zombie();
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   548
      _zombified_count++;
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   549
      SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
      // Still alive, clean up its inline caches
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   552
      MutexLocker cl(CompiledIC_lock);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
      nm->cleanup_inline_caches();
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   554
      SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  } else if (nm->is_unloaded()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
    // Unloaded code, just make it a zombie
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   558
    if (PrintMethodFlushing && Verbose) {
4750
71fd601907dc 4360113: Evict nmethods when code cache gets full
kvn
parents: 3919
diff changeset
   559
      tty->print_cr("### Nmethod %3d/" PTR_FORMAT " (unloaded) being made zombie", nm->compile_id(), nm);
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   560
    }
3696
9e5d9b5e1049 4957990: Perm heap bloat in JVM
ysr
parents: 1
diff changeset
   561
    if (nm->is_osr_method()) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   562
      SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
      // No inline caches will ever point to osr methods, so we can just remove it
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   564
      freed_memory = nm->total_size();
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   565
      if (nm->is_compiled_by_c2()) {
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   566
        _total_nof_c2_methods_reclaimed++;
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   567
      }
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   568
      release_nmethod(nm);
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   569
      _flushed_count++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
    } else {
21575
6a9645992cee 8027593: performance drop with constrained codecache starting with hs25 b111
anoll
parents: 20290
diff changeset
   571
      // Code cache state change is tracked in make_zombie()
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
      nm->make_zombie();
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17132
diff changeset
   573
      _zombified_count++;
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   574
      SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  } else {
24442
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   577
    possibly_flush(nm);
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   578
    // Clean-up all inline caches that point to zombie/non-reentrant methods
5533
e8d9ff82ec62 6950075: nmethod sweeper should operate concurrently
never
parents: 4750
diff changeset
   579
    MutexLocker cl(CompiledIC_lock);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
    nm->cleanup_inline_caches();
9942
2e86734ba620 6996747: SIGSEGV in nmethod::cleanup_inline_caches / CompiledIC::verify
never
parents: 9138
diff changeset
   581
    SWEEP(nm);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
  }
20290
2127dc70bce9 8020151: PSR:PERF Large performance regressions when code cache is filled
anoll
parents: 20019
diff changeset
   583
  return freed_memory;
4750
71fd601907dc 4360113: Evict nmethods when code cache gets full
kvn
parents: 3919
diff changeset
   584
}
71fd601907dc 4360113: Evict nmethods when code cache gets full
kvn
parents: 3919
diff changeset
   585
24442
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   586
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   587
void NMethodSweeper::possibly_flush(nmethod* nm) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   588
  if (UseCodeCacheFlushing) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   589
    if (!nm->is_locked_by_vm() && !nm->is_osr_method() && !nm->is_native_method()) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   590
      bool make_not_entrant = false;
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   591
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   592
      // Do not make native methods and OSR-methods not-entrant
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   593
      nm->dec_hotness_counter();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   594
      // Get the initial value of the hotness counter. This value depends on the
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   595
      // ReservedCodeCacheSize
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   596
      int reset_val = hotness_counter_reset_val();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   597
      int time_since_reset = reset_val - nm->hotness_counter();
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   598
      int code_blob_type = (CodeCache::get_code_blob_type(nm->comp_level()));
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 26578
diff changeset
   599
      double threshold = -reset_val + (CodeCache::reverse_free_ratio(code_blob_type) * NmethodSweepActivity);
24442
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   600
      // The less free space in the code cache we have - the bigger reverse_free_ratio() is.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   601
      // I.e., 'threshold' increases with lower available space in the code cache and a higher
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   602
      // NmethodSweepActivity. If the current hotness counter - which decreases from its initial
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   603
      // value until it is reset by stack walking - is smaller than the computed threshold, the
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   604
      // corresponding nmethod is considered for removal.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   605
      if ((NmethodSweepActivity > 0) && (nm->hotness_counter() < threshold) && (time_since_reset > MinPassesBeforeFlush)) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   606
        // A method is marked as not-entrant if the method is
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   607
        // 1) 'old enough': nm->hotness_counter() < threshold
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   608
        // 2) The method was in_use for a minimum amount of time: (time_since_reset > MinPassesBeforeFlush)
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   609
        //    The second condition is necessary if we are dealing with very small code cache
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   610
        //    sizes (e.g., <10m) and the code cache size is too small to hold all hot methods.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   611
        //    The second condition ensures that methods are not immediately made not-entrant
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   612
        //    after compilation.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   613
        make_not_entrant = true;
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   614
      }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   615
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   616
      // The stack-scanning low-cost detection may not see the method was used (which can happen for
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   617
      // flat profiles). Check the age counter for possible data.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   618
      if (UseCodeAging && make_not_entrant && (nm->is_compiled_by_c2() || nm->is_compiled_by_c1())) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   619
        MethodCounters* mc = nm->method()->method_counters();
24445
518f34b4384e 8043063: Code aging should allocate MethodCounters when flushing a method
iveresov
parents: 24442
diff changeset
   620
        if (mc == NULL) {
518f34b4384e 8043063: Code aging should allocate MethodCounters when flushing a method
iveresov
parents: 24442
diff changeset
   621
          // Sometimes we can get here without MethodCounters. For example if we run with -Xcomp.
518f34b4384e 8043063: Code aging should allocate MethodCounters when flushing a method
iveresov
parents: 24442
diff changeset
   622
          // Try to allocate them.
26578
1cb9e59a06ab 8053886: assert(false) failed: Should not allocate with exception pending
zmajo
parents: 25351
diff changeset
   623
          mc = nm->method()->get_method_counters(Thread::current());
24445
518f34b4384e 8043063: Code aging should allocate MethodCounters when flushing a method
iveresov
parents: 24442
diff changeset
   624
        }
24442
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   625
        if (mc != NULL) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   626
          // Snapshot the value as it's changed concurrently
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   627
          int age = mc->nmethod_age();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   628
          if (MethodCounters::is_nmethod_hot(age)) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   629
            // The method has gone through flushing, and it became relatively hot that it deopted
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   630
            // before we could take a look at it. Give it more time to appear in the stack traces,
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   631
            // proportional to the number of deopts.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   632
            MethodData* md = nm->method()->method_data();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   633
            if (md != NULL && time_since_reset > (int)(MinPassesBeforeFlush * (md->tenure_traps() + 1))) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   634
              // It's been long enough, we still haven't seen it on stack.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   635
              // Try to flush it, but enable counters the next time.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   636
              mc->reset_nmethod_age();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   637
            } else {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   638
              make_not_entrant = false;
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   639
            }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   640
          } else if (MethodCounters::is_nmethod_warm(age)) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   641
            // Method has counters enabled, and the method was used within
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   642
            // previous MinPassesBeforeFlush sweeps. Reset the counter. Stay in the existing
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   643
            // compiled state.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   644
            mc->reset_nmethod_age();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   645
            // delay the next check
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   646
            nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   647
            make_not_entrant = false;
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   648
          } else if (MethodCounters::is_nmethod_age_unset(age)) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   649
            // No counters were used before. Set the counters to the detection
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   650
            // limit value. If the method is going to be used again it will be compiled
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   651
            // with counters that we're going to use for analysis the the next time.
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   652
            mc->reset_nmethod_age();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   653
          } else {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   654
            // Method was totally idle for 10 sweeps
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   655
            // The counter already has the initial value, flush it and may be recompile
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   656
            // later with counters
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   657
          }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   658
        }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   659
      }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   660
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   661
      if (make_not_entrant) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   662
        nm->make_not_entrant();
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   663
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   664
        // Code cache state change is tracked in make_not_entrant()
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   665
        if (PrintMethodFlushing && Verbose) {
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   666
          tty->print_cr("### Nmethod %d/" PTR_FORMAT "made not-entrant: hotness counter %d/%d threshold %f",
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   667
              nm->compile_id(), nm, nm->hotness_counter(), reset_val, threshold);
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   668
        }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   669
      }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   670
    }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   671
  }
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   672
}
4d4ae31dea26 8032463: VirtualDispatch test timeout with DeoptimizeALot
iveresov
parents: 24424
diff changeset
   673
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   674
// Print out some state information about the current sweep and the
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   675
// state of the code cache if it's requested.
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   676
void NMethodSweeper::log_sweep(const char* msg, const char* format, ...) {
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   677
  if (PrintMethodFlushing) {
9138
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   678
    stringStream s;
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   679
    // Dump code cache state into a buffer before locking the tty,
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   680
    // because log_state() will use locks causing lock conflicts.
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   681
    CodeCache::log_state(&s);
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   682
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   683
    ttyLocker ttyl;
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   684
    tty->print("### sweeper: %s ", msg);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   685
    if (format != NULL) {
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   686
      va_list ap;
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   687
      va_start(ap, format);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   688
      tty->vprint(format, ap);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   689
      va_end(ap);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   690
    }
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 24351
diff changeset
   691
    tty->print_cr("%s", s.as_string());
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   692
  }
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   693
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   694
  if (LogCompilation && (xtty != NULL)) {
9138
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   695
    stringStream s;
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   696
    // Dump code cache state into a buffer before locking the tty,
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   697
    // because log_state() will use locks causing lock conflicts.
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   698
    CodeCache::log_state(&s);
35e4b04eadf4 7036236: VM crashes assert((!inside_attrs()) || is_error_reported()) failed ...
iveresov
parents: 8921
diff changeset
   699
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   700
    ttyLocker ttyl;
5926
a36f90d986b6 6968385: malformed xml in sweeper logging
never
parents: 5924
diff changeset
   701
    xtty->begin_elem("sweeper state='%s' traversals='" INTX_FORMAT "' ", msg, (intx)traversal_count());
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   702
    if (format != NULL) {
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   703
      va_list ap;
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   704
      va_start(ap, format);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   705
      xtty->vprint(format, ap);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   706
      va_end(ap);
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   707
    }
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 24351
diff changeset
   708
    xtty->print("%s", s.as_string());
5924
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   709
    xtty->stamp();
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   710
    xtty->end_elem();
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   711
  }
dc9d04930c82 6965184: possible races in make_not_entrant_or_zombie
never
parents: 5895
diff changeset
   712
}
22210
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   713
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   714
void NMethodSweeper::print() {
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   715
  ttyLocker ttyl;
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   716
  tty->print_cr("Code cache sweeper statistics:");
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   717
  tty->print_cr("  Total sweep time:                %1.0lfms", (double)_total_time_sweeping.value()/1000000);
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   718
  tty->print_cr("  Total number of full sweeps:     %ld", _total_nof_code_cache_sweeps);
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   719
  tty->print_cr("  Total number of flushed methods: %ld(%ld C2 methods)", _total_nof_methods_reclaimed,
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   720
                                                    _total_nof_c2_methods_reclaimed);
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   721
  tty->print_cr("  Total size of flushed methods:   " SIZE_FORMAT "kB", _total_flushed_size/K);
b0408ba029f6 8025277: Add -XX: flag to print code cache sweeper statistics
anoll
parents: 22208
diff changeset
   722
}