Skip to content
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

Ergonomics Profile #9

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e84d698
Add option for dedicated ergonomics profile
Jul 17, 2023
9cea06f
change gb
Jul 17, 2023
f58c9cd
change gb to prevent overflow
Jul 17, 2023
e8e0324
test G
Jul 24, 2023
b84eba2
use G
Jul 24, 2023
765fc11
add GC selection for dedicated ergonomics profile
Jul 25, 2023
0bb062e
fix oopsie
Jul 25, 2023
5bc7f7e
wip
brunoborges Aug 7, 2023
08cc6ce
Remove auto import
brunoborges Aug 7, 2023
b254d08
Real world doc for ErgonomicsProfile flag
brunoborges Aug 7, 2023
b5ff702
Remove leading whitespace
brunoborges Aug 7, 2023
5768a4a
Validate ergonomic profile in a function
brunoborges Aug 7, 2023
613c117
Fix where we set the sys property
brunoborges Aug 7, 2023
6559389
Store after it may have been changed
brunoborges Aug 7, 2023
93cad80
Set the profile before the flags
brunoborges Aug 7, 2023
92c1df1
Minor fixes
brunoborges Aug 8, 2023
d8a19e6
No need to change MinRAMPercentage here.
brunoborges Aug 8, 2023
a7c5576
Remove unnecessary new line
brunoborges Aug 8, 2023
34fa5de
Merge branch 'main' into ergonomics-profile
brunoborges Oct 6, 2023
6fd0069
Expose selected GC name through JMX
brunoborges Oct 6, 2023
ce6d869
Merge branch 'master' into ergonomics-profile
brunoborges Apr 9, 2024
0bcc5b3
Merge remote-tracking branch 'upstream/master' into ergonomics-profile
brunoborges Apr 25, 2024
eafd4f4
use auto by default
brunoborges Apr 25, 2024
436ba32
missing qupte
brunoborges Apr 25, 2024
a0199e4
simplified
brunoborges Apr 26, 2024
439bae6
jtreg test for ergo profile
brunoborges Apr 26, 2024
9c4b3ec
test max ram percentage
brunoborges Apr 28, 2024
a7db925
Tests for ergo profiles
brunoborges Apr 29, 2024
4cd37d0
Do not consider ZGC for now
brunoborges May 8, 2024
6232c64
Remove getGCName API from Runtime MBean and VMManagement
brunoborges May 9, 2024
fc3e25b
Update src/hotspot/share/gc/shared/gcConfig.cpp
brunoborges May 10, 2024
7aa4ec5
ZGC is not part of initial implementation
brunoborges May 10, 2024
63bc938
ZGC is not part of initial implementation
brunoborges May 10, 2024
92fad0c
merge
brunoborges May 10, 2024
da546d2
Add AutoErgonomicsProfile bool flag to enable auto selection
brunoborges May 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/hotspot/share/gc/shared/gcConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void GCConfig::fail_if_non_included_gc_is_selected() {
NOT_ZGC( FAIL_IF_SELECTED(UseZGC));
}

void GCConfig::select_gc_ergonomically() {
void select_gc_ergonomically_shared() {
if (os::is_server_class_machine()) {
#if INCLUDE_G1GC
FLAG_SET_ERGO_IF_DEFAULT(UseG1GC, true);
Expand All @@ -111,6 +111,41 @@ void GCConfig::select_gc_ergonomically() {
}
}

// Selects the garbage collector (GC) ergonomically based on the system's characteristics.
// It first checks the physical memory available on the system and the number of active processors.
// Depending on these factors, it sets the appropriate GC flag to true using the FLAG_SET_ERGO_IF_DEFAULT macro.
// If the system has only one active processor, it selects the Serial GC, no matter how much memory is available.
// If the system has more than one active processor and the physical memory is greater than or equal to 16GB, it selects the ZGC.
brunoborges marked this conversation as resolved.
Show resolved Hide resolved
// If the physical memory is greater than 2GB, it selects the G1GC.
// Otherwise, it selects the Parallel GC.
void select_gc_ergonomically_dedicated() {
brunoborges marked this conversation as resolved.
Show resolved Hide resolved
julong phys_mem = os::physical_memory();
if (os::active_processor_count() <= 1) {
#if INCLUDE_SERIALGC
FLAG_SET_ERGO_IF_DEFAULT(UseSerialGC, true);
#endif
} else if (phys_mem > 2048*M) {
#if INCLUDE_G1GC
FLAG_SET_ERGO_IF_DEFAULT(UseG1GC, true);
#endif
} else {
#if INCLUDE_PARALLELGC
FLAG_SET_ERGO_IF_DEFAULT(UseParallelGC, true);
#endif
}
}

void GCConfig::select_gc_ergonomically() {
if (strcmp(ErgonomicsProfile, "shared") == 0) {
select_gc_ergonomically_shared();
} else if (strcmp(ErgonomicsProfile, "dedicated") == 0) {
select_gc_ergonomically_dedicated();
} else {
// We have got to have at least one GC selected. Otherwise, we will crash.
ShouldNotReachHere();
}
brunoborges marked this conversation as resolved.
Show resolved Hide resolved
}

bool GCConfig::is_no_gc_selected() {
FOR_EACH_INCLUDED_GC(gc) {
if (gc->_flag) {
Expand Down
7 changes: 7 additions & 0 deletions src/hotspot/share/gc/shared/gc_globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@
product(bool, UseShenandoahGC, false, \
"Use the Shenandoah garbage collector") \
\
/* notice: once stable enough, goal is to change default to auto */ \
product(ccstr, ErgonomicsProfile, "shared", \
"Ergonomics profile to use. " \
"\"auto\" for automatic selection. " \
"\"shared\" for traditional environments (default). " \
"\"dedicated\" for environments with dedicated resources.") \
\
/* notice: the max range value here is INT_MAX not UINT_MAX */ \
/* to protect from overflows */ \
product(uint, ParallelGCThreads, 0, \
Expand Down
59 changes: 59 additions & 0 deletions src/hotspot/share/runtime/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
#if INCLUDE_JFR
#include "jfr/jfr.hpp"
#endif
#ifdef LINUX
#include "osContainer_linux.hpp"
#endif

#include <limits>

Expand Down Expand Up @@ -1438,6 +1441,15 @@ void Arguments::set_use_compressed_klass_ptrs() {
#endif // _LP64
}

static void validate_ergonomics_profile() {
if (
strcmp(ErgonomicsProfile, "shared") != 0 &&
strcmp(ErgonomicsProfile, "dedicated") != 0 &&
strcmp(ErgonomicsProfile, "auto") != 0){
vm_exit_during_initialization(err_msg("Unsupported ErgonomicsProfile: %s", ErgonomicsProfile));
}
}

void Arguments::set_conservative_max_heap_alignment() {
// The conservative maximum required alignment for the heap is the maximum of
// the alignments imposed by several sources: any requirements from the heap
Expand All @@ -1451,6 +1463,8 @@ void Arguments::set_conservative_max_heap_alignment() {

jint Arguments::set_ergonomics_flags() {
GCConfig::initialize();
// Store the name of the selected GC
PropertyList_add(&_system_properties, new SystemProperty("java.vm.gc.name", GCConfig::gc_name(), false));

set_conservative_max_heap_alignment();

Expand All @@ -1465,6 +1479,27 @@ jint Arguments::set_ergonomics_flags() {
return JNI_OK;
}

void Arguments::set_ergonomics_profile() {
validate_ergonomics_profile();

// Check if the value is 'auto'.
if (strcmp(ErgonomicsProfile, "auto") == 0) {
// Set the ergonomics profile based on platform.
// If it is a Linux environment, check if we are inside a container.
// If yes, we apply dedicated automatically.
// May support other platforms in the future (e.g. Windows Containers, Solaris Zones, FreeBSD Jails, Virtuozzo OpenVZ, etc).
#ifdef LINUX
if (OSContainer::is_containerized()){
FLAG_SET_ERGO(ErgonomicsProfile, "dedicated");
} else
#endif //LINUX
FLAG_SET_ERGO(ErgonomicsProfile, "shared");
}

// Store so we can expose through JMX RuntimeMBean
PropertyList_add(&_system_properties, new SystemProperty("java.vm.ergonomics.profile", ErgonomicsProfile, false));
}

size_t Arguments::limit_heap_by_allocatable_memory(size_t limit) {
size_t max_allocatable;
size_t result = limit;
Expand Down Expand Up @@ -1509,6 +1544,8 @@ void Arguments::set_heap_size() {
: (julong)MaxRAM;
}

set_ergonomics_profiles_heap_size_max_ram_percentage(phys_mem);

// If the maximum heap size has not been set with -Xmx,
// then set it as fraction of the size of physical memory,
// respecting the maximum and minimum sizes of the heap.
Expand Down Expand Up @@ -1616,6 +1653,25 @@ void Arguments::set_heap_size() {
}
}

void Arguments::set_ergonomics_profiles_heap_size_max_ram_percentage(julong phys_mem) {
// Update default heap size for dedicated ergonomics profile
if (strcmp(ErgonomicsProfile, "dedicated") == 0) {
FLAG_SET_ERGO_IF_DEFAULT(InitialRAMPercentage, 50.0);

if (phys_mem >= 16 * G) {
FLAG_SET_ERGO_IF_DEFAULT(MaxRAMPercentage, 90.0);
} else if (phys_mem >= 6 * G) {
FLAG_SET_ERGO_IF_DEFAULT(MaxRAMPercentage, 85.0);
} else if (phys_mem >= 4 * G) {
FLAG_SET_ERGO_IF_DEFAULT(MaxRAMPercentage, 80.0);
} else if (phys_mem >= 0.5 * G) {
FLAG_SET_ERGO_IF_DEFAULT(MaxRAMPercentage, 75.0);
} else {
FLAG_SET_ERGO_IF_DEFAULT(MaxRAMPercentage, 50.0);
}
}
}

// This option inspects the machine and attempts to set various
// parameters to be optimal for long-running, memory allocation
// intensive jobs. It is intended for machines with large
Expand Down Expand Up @@ -3654,6 +3710,9 @@ jint Arguments::parse(const JavaVMInitArgs* initial_cmd_args) {
}

jint Arguments::apply_ergo() {
// Set the ergonomics profile
set_ergonomics_profile();

// Set flags based on ergonomics.
jint result = set_ergonomics_flags();
if (result != JNI_OK) return result;
Expand Down
3 changes: 3 additions & 0 deletions src/hotspot/share/runtime/arguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class Arguments : AllStatic {
static bool _ClipInlining;

// GC ergonomics
static void set_ergonomics_profile();
static void set_conservative_max_heap_alignment();
static void set_use_compressed_oops();
static void set_use_compressed_klass_ptrs();
Expand All @@ -270,6 +271,8 @@ class Arguments : AllStatic {
// Setup heap size
static void set_heap_size();

static void set_ergonomics_profiles_heap_size_max_ram_percentage(julong phys_mem);

// Bytecode rewriting
static void set_bytecode_flags();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,16 @@ public default long getPid() {
* to the system properties.
*/
public java.util.Map<String, String> getSystemProperties();

/**
* Returns the selected ergonomics profile for this Java virtual machine.
* The profile will be one of the following:
* <ul>
* <li>shared</li>
* <li>dedicated</li>
* </ul>
*
* @return the name of the selected ergonomics profile
*/
public String getJvmErgonomicsProfile();
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ public Map<String,String> getSystemProperties() {
return map;
}

public String getJvmErgonomicsProfile() {
return jvm.getErgonomicsProfile();
}

public ObjectName getObjectName() {
return Util.newObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public interface VMManagement {
public long getStartupTime();
public long getUptime();
public int getAvailableProcessors();
public String getErgonomicsProfile();

// Compilation Subsystem
public String getCompilerName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ public synchronized List<String> getVmArguments() {
private native long getUptime0();
public native int getAvailableProcessors();

public String getErgonomicsProfile() {
return System.getProperty("java.vm.ergonomics.profile");
}

// Compilation Subsystem
public String getCompilerName() {
@SuppressWarnings("removal")
Expand Down
Loading