2
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
26
#include "nmt/mallocTracker.hpp"
27
#include "nmt/nmtCommon.hpp"
28
#include "nmt/nmtUsage.hpp"
29
#include "nmt/threadStackTracker.hpp"
30
#include "nmt/virtualMemoryTracker.hpp"
31
#include "runtime/threadCritical.hpp"
33
// Enabled all options for snapshot.
34
const NMTUsageOptions NMTUsage::OptionsAll = { true, true, true };
35
// Skip expensive thread stacks when refreshing usage.
36
const NMTUsageOptions NMTUsage::OptionsNoTS = { false, true, true };
38
NMTUsage::NMTUsage(NMTUsageOptions options) :
43
_usage_options(options) { }
45
void NMTUsage::walk_thread_stacks() {
46
// Snapping the thread stacks involves walking the areas to figure out how
47
// much memory had been committed if they are backed by virtual memory. This
48
// needs to happen before we take the snapshot of the virtual memory since it
49
// will update this information.
50
VirtualMemoryTracker::snapshot_thread_stacks();
53
void NMTUsage::update_malloc_usage() {
54
// Thread critical needed keep values in sync, total area size
55
// is deducted from mtChunk in the end to give correct values.
57
const MallocMemorySnapshot* ms = MallocMemorySummary::as_snapshot();
59
size_t total_arena_size = 0;
60
for (int i = 0; i < mt_number_of_types; i++) {
61
MEMFLAGS flag = NMTUtil::index_to_flag(i);
62
const MallocMemory* mm = ms->by_type(flag);
63
_malloc_by_type[i] = mm->malloc_size() + mm->arena_size();
64
total_arena_size += mm->arena_size();
68
_malloc_total = ms->total();
70
// Adjustment due to mtChunk double counting.
71
_malloc_by_type[NMTUtil::flag_to_index(mtChunk)] -= total_arena_size;
72
_malloc_total -= total_arena_size;
74
// Adjust mtNMT to include malloc overhead.
75
_malloc_by_type[NMTUtil::flag_to_index(mtNMT)] += ms->malloc_overhead();
78
void NMTUsage::update_vm_usage() {
79
const VirtualMemorySnapshot* vms = VirtualMemorySummary::as_snapshot();
81
// Reset total to allow recalculation.
82
_vm_total.committed = 0;
83
_vm_total.reserved = 0;
84
for (int i = 0; i < mt_number_of_types; i++) {
85
MEMFLAGS flag = NMTUtil::index_to_flag(i);
86
const VirtualMemory* vm = vms->by_type(flag);
88
_vm_by_type[i].reserved = vm->reserved();
89
_vm_by_type[i].committed = vm->committed();
90
_vm_total.reserved += vm->reserved();
91
_vm_total.committed += vm->committed();
95
void NMTUsage::refresh() {
96
if (_usage_options.include_malloc) {
97
update_malloc_usage();
100
if (_usage_options.include_vm) {
101
// Thread stacks only makes sense if virtual memory
102
// is also included. It must be executed before the
103
// over all usage is calculated.
104
if (_usage_options.update_thread_stacks) {
105
walk_thread_stacks();
111
size_t NMTUsage::total_reserved() const {
112
return _malloc_total + _vm_total.reserved;
115
size_t NMTUsage::total_committed() const {
116
return _malloc_total + _vm_total.committed;
119
size_t NMTUsage::reserved(MEMFLAGS flag) const {
120
int index = NMTUtil::flag_to_index(flag);
121
return _malloc_by_type[index] + _vm_by_type[index].reserved;
124
size_t NMTUsage::committed(MEMFLAGS flag) const {
125
int index = NMTUtil::flag_to_index(flag);
126
return _malloc_by_type[index] + _vm_by_type[index].committed;