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