src/hotspot/share/services/threadStackTracker.hpp
author zgu
Tue, 26 Mar 2019 15:50:34 -0400
changeset 54292 c31faeacf00a
permissions -rw-r--r--
8204552: NMT: Separate thread stack tracking from virtual memory tracking Reviewed-by: stuefe, minqi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54292
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     1
/*
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     2
 * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     3
 *
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     6
 * published by the Free Software Foundation.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     7
 *
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    12
 * accompanied this code).
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    13
 *
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    17
 *
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    20
 * questions.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    21
 *
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    22
 */
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    23
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    24
#ifndef SHARE_SERVICES_THREADSTACKTRACKER_HPP
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    25
#define SHARE_SERVICES_THREADSTACKTRACKER_HPP
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    26
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    27
#if INCLUDE_NMT
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    28
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    29
#include "services/allocationSite.hpp"
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    30
#include "services/mallocSiteTable.hpp"
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    31
#include "services/nmtCommon.hpp"
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    32
#include "utilities/nativeCallStack.hpp"
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    33
#include "utilities/linkedlist.hpp"
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    34
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    35
class SimpleThreadStackSite;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    36
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    37
class SimpleThreadStack {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    38
  friend class SimpleThreadStackSite;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    39
private:
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    40
  address _base;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    41
  size_t  _size;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    42
public:
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    43
  SimpleThreadStack() : _base(NULL), _size(0) { }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    44
  bool equals(const SimpleThreadStack& s) const {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    45
    return base() == s.base();
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    46
  }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    47
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    48
  size_t  size() const { return _size; }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    49
  address base() const { return _base; }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    50
private:
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    51
  void set_size(size_t size)  { _size = size; }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    52
  void set_base(address base) { _base = base; }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    53
};
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    54
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    55
class SimpleThreadStackSite : public AllocationSite<SimpleThreadStack> {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    56
public:
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    57
  SimpleThreadStackSite(address base, size_t size, const NativeCallStack& stack) :
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    58
    AllocationSite<SimpleThreadStack>(stack, mtThreadStack) {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    59
    data()->set_size(size);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    60
    data()->set_base(base);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    61
  }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    62
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    63
  SimpleThreadStackSite(address base, size_t size) :
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    64
    AllocationSite<SimpleThreadStack>(NativeCallStack::empty_stack(), mtThreadStack) {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    65
    data()->set_base(base);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    66
    data()->set_size(size);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    67
  }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    68
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    69
  bool equals(const SimpleThreadStackSite& mts) const {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    70
    bool eq = base() == mts.base();
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    71
    assert(!eq || size() == mts.size(), "Must match");
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    72
    return eq;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    73
  }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    74
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    75
  size_t  size() const { return peek()->size(); }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    76
  address base() const { return peek()->base(); }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    77
};
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    78
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    79
  /*
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    80
   * Most of platforms, that hotspot support, have their thread stacks backed by
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    81
   * virtual memory by default. For these cases, thread stack tracker simply
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    82
   * delegates tracking to virtual memory tracker.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    83
   * However, there are exceptions, (e.g. AIX), that platforms can provide stacks
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    84
   * that are not page aligned. A hypothetical VM implementation, it can provide
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    85
   * it own stacks. In these case, track_as_vm() should return false and manage
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    86
   * stack tracking by this tracker internally.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    87
   * During memory snapshot, tracked thread stacks memory data is walked and stored
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    88
   * along with malloc'd data inside baseline. The regions are not scanned and assumed
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    89
   * all committed for now. Can add scanning phase when there is a need.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    90
   */
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    91
class ThreadStackTracker : AllStatic {
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    92
private:
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    93
  static volatile size_t _thread_count;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    94
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    95
  static int compare_thread_stack_base(const SimpleThreadStackSite& s1, const SimpleThreadStackSite& s2);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    96
  static SortedLinkedList<SimpleThreadStackSite, compare_thread_stack_base>* _simple_thread_stacks;
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    97
public:
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    98
  // Late phase initialization
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
    99
  static bool late_initialize(NMT_TrackingLevel level);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   100
  static bool transition(NMT_TrackingLevel from, NMT_TrackingLevel to);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   101
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   102
  static void new_thread_stack(void* base, size_t size, const NativeCallStack& stack);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   103
  static void delete_thread_stack(void* base, size_t size);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   104
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   105
  static bool   track_as_vm()  { return AIX_ONLY(false) NOT_AIX(true); }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   106
  static size_t thread_count() { return _thread_count; }
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   107
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   108
  // Snapshot support. Piggyback thread stack data in malloc slot, NMT always handles
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   109
  // thread stack slot specially since beginning.
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   110
  static bool walk_simple_thread_stack_site(MallocSiteWalker* walker);
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   111
};
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   112
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   113
#endif // INCLUDE_NMT
c31faeacf00a 8204552: NMT: Separate thread stack tracking from virtual memory tracking
zgu
parents:
diff changeset
   114
#endif // SHARE_SERVICES_THREADSTACKTRACKER_HPP