Skip to content

Commit

Permalink
Merge pull request #585 from mronstro/RONDB-789
Browse files Browse the repository at this point in the history
RONDB-789: Automatic memory management when running in a container
  • Loading branch information
mronstro authored Dec 3, 2024
2 parents dd13385 + b279d61 commit a4acab9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions storage/ndb/include/portlib/NdbHW.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct ndb_hwinfo
* Number of Processors, Cores and Sockets
* MHz of CPU and Model name of CPU.
*/
Uint32 is_running_in_container;
Uint32 cpu_cnt_max;
Uint32 cpu_cnt;
Uint32 total_cpu_capacity;
Expand Down
18 changes: 18 additions & 0 deletions storage/ndb/src/common/portlib/NdbHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,7 @@ static int Ndb_ReloadHWInfo(struct ndb_hwinfo *hwinfo)
hwinfo->num_shared_l3_caches = l3_cache_id;
hwinfo->cpu_model_name[0] = 0;
hwinfo->hw_memory_size = mem_status.ullTotalPhys;
hwinfo->is_running_in_container = 0;
if (hwinfo->cpu_cnt_max < hwinfo->cpu_cnt)
{
hwinfo->cpu_cnt = hwinfo->cpu_cnt_max;
Expand Down Expand Up @@ -1661,6 +1662,7 @@ static int Ndb_ReloadHWInfo(struct ndb_hwinfo * hwinfo)
hwinfo->cpu_model_name[size_var] = 0; // Null terminate
hwinfo->is_cpuinfo_available = 0;
hwinfo->is_cpudata_available = 0;
hwinfo->is_running_in_container = 0;
return 0;

error_exit:
Expand Down Expand Up @@ -1755,6 +1757,7 @@ static int Ndb_ReloadHWInfo(struct ndb_hwinfo * hwinfo)
hwinfo->cpu_model_name[0] = 0; // Null terminate
hwinfo->is_cpuinfo_available = 0;
hwinfo->is_cpudata_available = 0;
hwinfo->is_running_in_container = 0;
return 0;

error_exit:
Expand Down Expand Up @@ -2147,6 +2150,20 @@ static int
get_meminfo(struct ndb_hwinfo *hwinfo)
{
char buf[1024];
FILE * cgroup_meminfo = fopen("/sys/fs/cgroup/memory.max", "r");
if (cgroup_meminfo != nullptr) {
hwinfo->is_running_in_container = 1;
FileGuard g(cgroup_meminfo); // close at end...
int ret_code = fgets(buf, sizeof(buf), cgroup_meminfo))
Uint64 memory_size = 0;
if (sscanf(buf, "%llu", &memory_size) == 1) {
hwinfo->hw_memory_size = memory_size;
return 0;
}
perror("failed to read /sys/fs/cgroup/memory.max");
return -1;
}
hwinfo->is_running_in_container = 0;
FILE * meminfo = fopen("/proc/meminfo", "r");
if (meminfo == nullptr)
{
Expand Down Expand Up @@ -2719,6 +2736,7 @@ static int Ndb_ReloadHWInfo(struct ndb_hwinfo * hwinfo)
hwinfo->cpu_cnt_max = ncpu;
hwinfo->cpu_cnt = ncpu;
check_cpu_online(hwinfo);
hwinfo->is_running_in_container = 0;
return 0;
}

Expand Down
8 changes: 7 additions & 1 deletion storage/ndb/src/kernel/vm/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,11 @@ Configuration::get_total_memory(const ndb_mgm_configuration_iterator *p,
struct ndb_hwinfo *hwinfo = Ndb_GetHWInfo(false);
return hwinfo->hw_memory_size;
total_memory_set = false;
globalData.theUseContainerMemoryFlag = hwinfo->is_running_in_container;
}
else
{
globalData.theUseContainerMemoryFlag = false;
total_memory_set = true;
return total_memory_size;
}
Expand Down Expand Up @@ -1053,7 +1055,11 @@ Configuration::compute_os_overhead(
ndb_mgm_get_int64_parameter(p, CFG_DB_OS_CPU_OVERHEAD, &os_cpu_overhead);
if (os_static_overhead == 0)
{
os_static_overhead = Uint64(1400) * MBYTE64;
if (globalData.theUseContainerMemoryFlag) {
os_static_overhead = Uint64(200) * MBYTE64;
} else {
os_static_overhead = Uint64(1400) * MBYTE64;
}
}
if (os_cpu_overhead == 0)
{
Expand Down
1 change: 1 addition & 0 deletions storage/ndb/src/kernel/vm/GlobalData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct GlobalData {

bool theGracefulShutdownFlag;
bool theUseOnlyIPv4Flag;
bool theUseContainerMemoryFlag;


NdbMutex *theIO_lag_mutex;
Expand Down

0 comments on commit a4acab9

Please sign in to comment.