8000 BUG#37697570 BUG#37697649 Defining RAM usage limits in jit-executor · mysql/mysql-server@1c05000 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 1c05000

Browse files
rennoxdahlerlend
authored andcommitted
BUG#37697570 BUG#37697649 Defining RAM usage limits in jit-executor
By defining minimum and maximum allowed RAM limits the following issues get resolved: - BUG#37697649 infinite loop at GET mrs script - BUG#37697570 too large maximumRamUser leads to HTTP 501 The ram to be used by the jit-executor is defined as follows: - Default RAM: 25% of system memory - Maximum Allowed: 80% of system memory - Minimum Allowed: 10 MB If configured RAM is above the max allowed, the jit-executor will run with defaults, which means it will use as much as needed with a limit of 80% of the system memory. If configured RAM is below the min allowed, the jit-executor will use the default value of 25% of the system memory. Change-Id: I69cce62b0e7a39234c3ad59431817b8947b4deda
1 parent 28697ae commit 1c05000

File tree

5 files changed

+165
-4
lines changed

5 files changed

+165
-4
lines changed

router/src/jit_executor/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ SET(jit_executor_plugin_SOURCE
6262
utils/utils_json.cc
6363
utils/utils_path.cc
6464
utils/utils_string.cc
65+
utils/utils_system.cc
6566

6667
jit_executor_common_context.cc
6768
jit_executor_plugin.cc

router/src/jit_executor/src/jit_executor_component.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ void JitExecutorComponent::update_active_contexts(
149149
// Total pool is verified here as it could be 0, meaning, no active context
150150
// handlers will be left after this update
151151
if (total_pool != 0 && m_global_config.maximum_ram_size.has_value()) {
152-
uint64_t mem_per_pool_item = *m_global_config.maximum_ram_size / total_pool;
152+
// Use double to get the most accurate value per pool item
153+
double mem_per_pool_item =
154+
*m_global_config.maximum_ram_size / static_cast<double>(total_pool);
153155

154156
for (const auto &it : candidate_context_handlers) {
155-
it.second->set_max_heap_size(mem_per_pool_item * it.second->pool_size());
157+
it.second->set_max_heap_size(
158+
static_cast<uint64_t>(mem_per_pool_item * it.second->pool_size()));
156159
}
157160
}
158161

router/src/jit_executor/src/jit_executor_service_handlers.cc

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525

2626
#include "jit_executor_service_handlers.h"
2727

28+
#include <cinttypes>
2829
#include <memory>
2930
#include <vector>
3031

3132
#include "include/my_thread.h"
3233
#include "mysql/harness/logging/logging.h"
34+
#include "utils/utils_system.h"
3335

3436
namespace jit_executor {
3537

@@ -56,9 +58,52 @@ bool ServiceHandlers::init() {
5658

5759
void ServiceHandlers::init_common_context() {
5860
std::vector<std::string> isolate_args;
61+
62+
// System memory in MB
63+
static const auto total_memory =
64+
shcore::getPhysicalMemorySize() / 1024 / 1024;
65+
66+
// Default: 25% of the system memory
67+
static const uint64_t default_max_heap_size = total_memory * 0.25;
68+
5969
// Using a default of 1024 MB if nothing else is configured
60-
auto max_heap_size = m_config.max_heap_size.value_or(1024);
61-
isolate_args.push_back("-Xmx" + std::to_string(max_heap_size) + "m");
70+
auto max_heap_size = m_config.max_heap_size.value_or(default_max_heap_size);
71+
72+
if (total_memory > 0) {
73+
// Serial GC would use max 80% of system memory
74+
auto graal_default_max_heap_size =
75+
static_cast<uint64_t>(total_memory * 0.8);
76+
77+
// 10 MB is the minimum allowed
78+
if (max_heap_size < 10) {
79+
log_warning(
80+
"The configured maximumRamUsage=%" PRIu64
81+
" is lower than the minimum allowed value of 10MB, ignoring "
82+
"configuration, using default (25%% of the system memory: %" PRIu64
83+
"MB).",
84+
max_heap_size, default_max_heap_size);
85+
max_heap_size = default_max_heap_size;
86+
}
87+
88+
if (max_heap_size < graal_default_max_heap_size) {
89+
isolate_args.push_back("-Xmx" + std::to_string(max_heap_size) + "m");
90+
} else {
91+
// Convert the value to gigabytes
92+
log_warning("The configured maximumRamUsage=%" PRIu64
93+
" exceeds the maximum allowed value %" PRIu64
94+
" (80%% of the system memory %" PRIu64
95+
"GB) ignoring configuration, using max RAM possible.",
96+
max_heap_size, graal_default_max_heap_size,
97+
total_memory / 1024);
98+
}
99+
} else {
100+
// Not expected to happen, just in case!
101+
log_warning(
102+
"Unable to retrieve the available system memory, using the configured "
103+
"value of maximumRamUsage=%" PRIu64,
104+
max_heap_size);
105+
}
106+
62107
m_common_context = std::make_unique<CommonContext>(
63108
m_config.fs, m_config.module_files, m_config.globals, isolate_args);
64109
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License, version 2.0,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is designed to work with certain software (including
9+
* but not limited to OpenSSL) that is licensed under separate terms,
10+
* as designated in a particular file or component or in included license
11+
* documentation. The authors of MySQL hereby grant you an additional
12+
* permission to link the program and your derivative works with the
13+
* separately licensed software that they have either included with
14+
* the program or referenced in the documentation.
15+
*
16+
* This program is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19+
* the GNU General Public License, version 2.0, for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program; if not, write to the Free Software Foundation, Inc.,
23+
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24+
*/
25+
26+
#include <cstdint>
27+
#include <cstdio>
28+
#include <cstdlib>
29+
30+
#ifdef _WIN32
31+
#include <windows.h>
32+
#elif defined(unix) || defined(__unix__) || defined(__unix)
33+
#include <unistd.h>
34+
#endif
35+
36+
#if defined(__linux__)
37+
#include <sys/sysinfo.h>
38+
#elif defined(__APPLE__)
39+
#include <sys/sysctl.h>
40+
#endif
41+
42+
namespace shcore {
43+
44+
uint64_t getPhysicalMemorySize() {
45+
uint64_t memsize = 0;
46+
47+
#ifdef _WIN32
48+
MEMORYSTATUSEX memInfo;
49+
memInfo.dwLength = sizeof(MEMORYSTATUSEX);
50+
if (GlobalMemoryStatusEx(&memInfo) == 0) {
51+
return 0;
52+
}
53+
memsize = memInfo.ullTotalPhys;
54+
#elif defined(__linux__)
55+
struct sysinfo info;
56+
if (sysinfo(&info) != 0) {
57+
return 0;
58+
}
59+
memsize = info.totalram;
60+
#elif defined(__APPLE__)
61+
int mib[2];
62+
mib[0] = CTL_HW;
63+
mib[1] = HW_MEMSIZE;
64+
u_int namelen = sizeof(mib) / sizeof(mib[0]);
65+
u_long size = sizeof(memsize);
66+
if (sysctl(mib, namelen, &memsize, &size, NULL, 0) != 0) {
67+
return 0;
68+
}
69+
#endif
70+
71+
return memsize;
72+
}
73+
} // namespace shcore
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License, version 2.0,
6+
* as published by the Free Software Foundation.
7+
*
8+
* This program is designed to work with certain software (including
9+
* but not limited to OpenSSL) that is licensed under separate terms,
10+
* as designated in a particular file or component or in included license
11+
* documentation. The authors of MySQL hereby grant you an additional
12+
* permission to link the program and your derivative works with the
13+
* separately licensed software that they have either included with
14+
* the program or referenced in the documentation.
15+
*
16+
* This program is distributed in the hope that it will be useful, but
17+
* WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
19+
* the GNU General Public License, version 2.0, for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program; if not, write to the Free Software Foundation, Inc.,
23+
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24+
*/
25+
26+
#ifndef ROUTER_SRC_JIT_EXECUTOR_SRC_UTILS_UTILS_SYSTEM_H_
27+
#define ROUTER_SRC_JIT_EXECUTOR_SRC_UTILS_UTILS_SYSTEM_H_
28+
29+
#include <cstdint>
30+
31+
namespace shcore {
32+
/**
33+
* Returns the amount of physical memory in megabytes or 0 if fails retrieving
34+
* the value.
35+
*/
36+
uint64_t getPhysicalMemorySize();
37+
} // namespace shcore
38+
39+
#endif // ROUTER_SRC_JIT_EXECUTOR_SRC_UTILS_UTILS_SYSTEM_H_

0 commit comments

Comments
 (0)
0