-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8157023: Integrate NMT with JFR #11449
Closed
+582
−8
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
08b8a57
8157023: Integrate NMT with JFR
kstefanj 67e7197
egahlin comments
kstefanj a4c3b96
Basic test for NMT events
kstefanj ed8f4fe
Add missing newlines at end of files
kstefanj 5a3c01b
Test comments from egahlin
kstefanj b3af872
Update profile.jfc and explict disable NMT in test
kstefanj 5f8a109
egahlin and carterkozak comments
kstefanj 57ab657
Some refactoring after comments
kstefanj 0401786
Special snapshot for NMT events in JFR
kstefanj 96998e7
tstuefe review - no rename
kstefanj b79dc65
Renaming and updating comments
kstefanj 0b3668b
Rename the files
kstefanj 2335255
mgonlun review
kstefanj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
* | ||
*/ | ||
|
||
#include "precompiled.hpp" | ||
#include "jfr/jfrEvents.hpp" | ||
#include "services/memJfrReporter.hpp" | ||
#include "services/memTracker.hpp" | ||
#include "services/nmtUsage.hpp" | ||
#include "utilities/globalDefinitions.hpp" | ||
#include "utilities/ticks.hpp" | ||
|
||
// Helper class to avoid refreshing the NMTUsage to often and allow | ||
// the two JFR events to use the same data. | ||
class MemJFRCurrentUsage : public AllStatic { | ||
private: | ||
// The age threshold in milliseconds. If older than this refresh the usage. | ||
static const uint64_t AgeThreshold = 50; | ||
|
||
static Ticks _timestamp; | ||
static NMTUsage* _usage; | ||
|
||
public: | ||
static NMTUsage* get_usage(); | ||
static Ticks get_timestamp(); | ||
}; | ||
|
||
Ticks MemJFRCurrentUsage::_timestamp; | ||
NMTUsage* MemJFRCurrentUsage::_usage = nullptr; | ||
|
||
NMTUsage* MemJFRCurrentUsage::get_usage() { | ||
Tickspan since_baselined = Ticks::now() - _timestamp; | ||
|
||
if (_usage == nullptr) { | ||
// First time, create a new NMTUsage. | ||
_usage = new NMTUsage(NMTUsage::OptionsNoTS); | ||
} else if (since_baselined.milliseconds() < AgeThreshold) { | ||
// There is recent enough usage information, return it. | ||
return _usage; | ||
} | ||
|
||
// Refresh the usage information. | ||
_usage->refresh(); | ||
_timestamp.stamp(); | ||
|
||
return _usage; | ||
} | ||
|
||
Ticks MemJFRCurrentUsage::get_timestamp() { | ||
return _timestamp; | ||
} | ||
|
||
void MemJFRReporter::send_total_event() { | ||
if (!MemTracker::enabled()) { | ||
return; | ||
} | ||
|
||
NMTUsage* usage = MemJFRCurrentUsage::get_usage(); | ||
Ticks timestamp = MemJFRCurrentUsage::get_timestamp(); | ||
|
||
EventNativeMemoryUsageTotal event(UNTIMED); | ||
event.set_starttime(timestamp); | ||
event.set_reserved(usage->total_reserved()); | ||
event.set_committed(usage->total_committed()); | ||
event.commit(); | ||
} | ||
|
||
void MemJFRReporter::send_type_event(const Ticks& starttime, const char* type, size_t reserved, size_t committed) { | ||
EventNativeMemoryUsage event(UNTIMED); | ||
event.set_starttime(starttime); | ||
event.set_type(type); | ||
event.set_reserved(reserved); | ||
event.set_committed(committed); | ||
event.commit(); | ||
} | ||
|
||
void MemJFRReporter::send_type_events() { | ||
if (!MemTracker::enabled()) { | ||
return; | ||
} | ||
|
||
NMTUsage* usage = MemJFRCurrentUsage::get_usage(); | ||
Ticks timestamp = MemJFRCurrentUsage::get_timestamp(); | ||
|
||
for (int index = 0; index < mt_number_of_types; index ++) { | ||
MEMFLAGS flag = NMTUtil::index_to_flag(index); | ||
if (flag == mtNone) { | ||
// Skip mtNone since it is not really used. | ||
continue; | ||
} | ||
send_type_event(timestamp, NMTUtil::flag_to_name(flag), usage->reserved(flag), usage->committed(flag)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
* | ||
*/ | ||
|
||
#ifndef SHARE_SERVICES_MEMJFRREPORTER_HPP | ||
#define SHARE_SERVICES_MEMJFRREPORTER_HPP | ||
|
||
#include "memory/allocation.hpp" | ||
#include "services/nmtUsage.hpp" | ||
#include "utilities/globalDefinitions.hpp" | ||
#include "utilities/ticks.hpp" | ||
|
||
// MemJFRReporter is only used by threads sending periodic JFR | ||
// events. These threads are synchronized at a higher level, | ||
// so no more synchronization is needed. | ||
class MemJFRReporter : public AllStatic { | ||
private: | ||
static void send_type_event(const Ticks& starttime, const char* tag, size_t reserved, size_t committed); | ||
public: | ||
static void send_total_event(); | ||
static void send_type_events(); | ||
}; | ||
|
||
#endif //SHARE_SERVICES_MEMJFRREPORTER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
* | ||
*/ | ||
|
||
#include "precompiled.hpp" | ||
#include "runtime/threadCritical.hpp" | ||
#include "services/nmtCommon.hpp" | ||
#include "services/nmtUsage.hpp" | ||
#include "services/mallocTracker.hpp" | ||
#include "services/threadStackTracker.hpp" | ||
#include "services/virtualMemoryTracker.hpp" | ||
|
||
// Enabled all options for snapshot. | ||
const NMTUsageOptions NMTUsage::OptionsAll = { true, true, true }; | ||
// Skip expensive thread stacks when refreshing usage. | ||
const NMTUsageOptions NMTUsage::OptionsNoTS = { false, true, true }; | ||
|
||
NMTUsage::NMTUsage(NMTUsageOptions options) : | ||
_malloc_by_type(), | ||
_malloc_total(), | ||
_vm_by_type(), | ||
_vm_total(), | ||
_usage_options(options) { } | ||
|
||
void NMTUsage::walk_thread_stacks() { | ||
// If backed by virtual memory, snapping the thread stacks involves walking | ||
// them to to figure out how much memory is committed if they are backed by | ||
// virtual memory. This needs ot happen before we take the snapshot of the | ||
// virtual memory since it will update this information. | ||
if (ThreadStackTracker::track_as_vm()) { | ||
VirtualMemoryTracker::snapshot_thread_stacks(); | ||
} | ||
} | ||
|
||
void NMTUsage::update_malloc_usage() { | ||
// Thread critical needed keep values in sync, total area size | ||
// is deducted from mtChunk in the end to give correct values. | ||
ThreadCritical tc; | ||
const MallocMemorySnapshot* ms = MallocMemorySummary::as_snapshot(); | ||
|
||
size_t total_arena_size = 0; | ||
for (int i = 0; i < mt_number_of_types; i++) { | ||
MEMFLAGS flag = NMTUtil::index_to_flag(i); | ||
const MallocMemory* mm = ms->by_type(flag); | ||
_malloc_by_type[i] = mm->malloc_size() + mm->arena_size(); | ||
total_arena_size += mm->arena_size(); | ||
} | ||
|
||
// Total malloc size. | ||
_malloc_total = ms->total(); | ||
|
||
// Adjustment due to mtChunk double counting. | ||
_malloc_by_type[NMTUtil::flag_to_index(mtChunk)] -= total_arena_size; | ||
_malloc_total -= total_arena_size; | ||
|
||
// Adjust mtNMT to include malloc overhead. | ||
_malloc_by_type[NMTUtil::flag_to_index(mtNMT)] += ms->malloc_overhead(); | ||
} | ||
|
||
void NMTUsage::update_vm_usage() { | ||
const VirtualMemorySnapshot* vms = VirtualMemorySummary::as_snapshot(); | ||
|
||
// Reset total to allow recalculation. | ||
_vm_total.committed = 0; | ||
_vm_total.reserved = 0; | ||
for (int i = 0; i < mt_number_of_types; i++) { | ||
MEMFLAGS flag = NMTUtil::index_to_flag(i); | ||
const VirtualMemory* vm = vms->by_type(flag); | ||
|
||
_vm_by_type[i].reserved = vm->reserved(); | ||
_vm_by_type[i].committed = vm->committed(); | ||
_vm_total.reserved += vm->reserved(); | ||
_vm_total.committed += vm->committed(); | ||
} | ||
} | ||
|
||
void NMTUsage::refresh() { | ||
if (_usage_options.include_malloc) { | ||
update_malloc_usage(); | ||
} | ||
|
||
if (_usage_options.include_vm) { | ||
// Thread stacks only makes sense if virtual memory | ||
// is also included. It must be executed before the | ||
// over all usage is calculated. | ||
if (_usage_options.update_thread_stacks) { | ||
walk_thread_stacks(); | ||
} | ||
update_vm_usage(); | ||
} | ||
} | ||
|
||
size_t NMTUsage::total_reserved() const { | ||
return _malloc_total + _vm_total.reserved; | ||
} | ||
|
||
size_t NMTUsage::total_committed() const { | ||
return _malloc_total + _vm_total.reserved; | ||
} | ||
|
||
size_t NMTUsage::reserved(MEMFLAGS flag) const { | ||
int index = NMTUtil::flag_to_index(flag); | ||
return _malloc_by_type[index] + _vm_by_type[index].reserved; | ||
} | ||
|
||
size_t NMTUsage::committed(MEMFLAGS flag) const { | ||
int index = NMTUtil::flag_to_index(flag); | ||
return _malloc_by_type[index] + _vm_by_type[index].committed; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest turning this type "string" into a JFR-constant type, like "JfrNMTType" to only send id's, instead of the entire strings. Can be done in a follow-up change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to do this at a later point, created JDK-8298278 to track this.