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 | #include "precompiled.hpp" |
25 | |
26 | #include "runtime/atomic.hpp" |
27 | #include "runtime/threadCritical.hpp" |
28 | #include "services/mallocTracker.hpp" |
29 | #include "services/memTracker.hpp" |
30 | #include "services/virtualMemoryTracker.hpp" |
31 | #include "services/threadStackTracker.hpp" |
32 | |
33 | volatile size_t ThreadStackTracker::_thread_count = 0; |
34 | SortedLinkedList<SimpleThreadStackSite, ThreadStackTracker::compare_thread_stack_base>* ThreadStackTracker::_simple_thread_stacks = NULL; |
35 | |
36 | bool ThreadStackTracker::late_initialize(NMT_TrackingLevel level) { |
37 | if (level == NMT_detail && !track_as_vm()) { |
38 | _simple_thread_stacks = new (std::nothrow, ResourceObj::C_HEAP, mtNMT) |
39 | SortedLinkedList<SimpleThreadStackSite, ThreadStackTracker::compare_thread_stack_base>(); |
40 | return (_simple_thread_stacks != NULL); |
41 | } |
42 | return true; |
43 | } |
44 | |
45 | bool ThreadStackTracker::transition(NMT_TrackingLevel from, NMT_TrackingLevel to) { |
46 | assert (from != NMT_minimal, "cannot convert from the lowest tracking level to anything" ); |
47 | if (to == NMT_minimal) { |
48 | assert(from == NMT_summary || from == NMT_detail, "Just check" ); |
49 | ThreadCritical tc; |
50 | if (_simple_thread_stacks != NULL) { |
51 | delete _simple_thread_stacks; |
52 | _simple_thread_stacks = NULL; |
53 | } |
54 | } |
55 | return true; |
56 | } |
57 | |
58 | int ThreadStackTracker::compare_thread_stack_base(const SimpleThreadStackSite& s1, const SimpleThreadStackSite& s2) { |
59 | return s1.base() - s2.base(); |
60 | } |
61 | |
62 | void ThreadStackTracker::new_thread_stack(void* base, size_t size, const NativeCallStack& stack) { |
63 | assert(MemTracker::tracking_level() >= NMT_summary, "Must be" ); |
64 | assert(base != NULL, "Should have been filtered" ); |
65 | if (track_as_vm()) { |
66 | ThreadCritical tc; |
67 | VirtualMemoryTracker::add_reserved_region((address)base, size, stack, mtThreadStack); |
68 | _thread_count ++; |
69 | } else { |
70 | // Use a slot in mallocMemorySummary for thread stack bookkeeping |
71 | MallocMemorySummary::record_malloc(size, mtThreadStack); |
72 | if (MemTracker::tracking_level() == NMT_detail) { |
73 | ThreadCritical tc; |
74 | assert(_simple_thread_stacks != NULL, "Must be initialized" ); |
75 | SimpleThreadStackSite site((address)base, size, stack); |
76 | _simple_thread_stacks->add(site); |
77 | } |
78 | } |
79 | } |
80 | |
81 | void ThreadStackTracker::delete_thread_stack(void* base, size_t size) { |
82 | assert(MemTracker::tracking_level() >= NMT_summary, "Must be" ); |
83 | assert(base != NULL, "Should have been filtered" ); |
84 | if(track_as_vm()) { |
85 | ThreadCritical tc; |
86 | VirtualMemoryTracker::remove_released_region((address)base, size); |
87 | _thread_count--; |
88 | } else { |
89 | // Use a slot in mallocMemorySummary for thread stack bookkeeping |
90 | MallocMemorySummary::record_free(size, mtThreadStack); |
91 | if (MemTracker::tracking_level() == NMT_detail) { |
92 | ThreadCritical tc; |
93 | assert(_simple_thread_stacks != NULL, "Must be initialized" ); |
94 | SimpleThreadStackSite site((address)base, size); |
95 | bool removed = _simple_thread_stacks->remove(site); |
96 | assert(removed, "Must exist" ); |
97 | } |
98 | } |
99 | } |
100 | |
101 | bool ThreadStackTracker::walk_simple_thread_stack_site(MallocSiteWalker* walker) { |
102 | if (!track_as_vm()) { |
103 | LinkedListImpl<MallocSite> _sites; |
104 | { |
105 | ThreadCritical tc; |
106 | assert(_simple_thread_stacks != NULL, "Must be initialized" ); |
107 | LinkedListIterator<SimpleThreadStackSite> itr(_simple_thread_stacks->head()); |
108 | const SimpleThreadStackSite* ts = itr.next(); |
109 | // Consolidate sites and convert to MallocSites, so we can piggyback into |
110 | // malloc snapshot |
111 | while (ts != NULL) { |
112 | MallocSite site(*ts->call_stack(), mtThreadStack); |
113 | MallocSite* exist = _sites.find(site); |
114 | if (exist != NULL) { |
115 | exist->allocate(ts->size()); |
116 | } else { |
117 | site.allocate(ts->size()); |
118 | _sites.add(site); |
119 | } |
120 | ts = itr.next(); |
121 | } |
122 | } |
123 | |
124 | // Piggyback to malloc snapshot |
125 | LinkedListIterator<MallocSite> site_itr(_sites.head()); |
126 | const MallocSite* s = site_itr.next(); |
127 | while (s != NULL) { |
128 | walker->do_malloc_site(s); |
129 | s = site_itr.next(); |
130 | } |
131 | } |
132 | return true; |
133 | } |
134 | |