Skip to content

Commit

Permalink
do not use mmap when the mmap limit is too low (netdata#19714)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsaou authored Feb 26, 2025
1 parent fbc551a commit b71bc97
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,8 @@ set(LIBNETDATA_FILES
src/libnetdata/os/run_dir.h
src/libnetdata/os/file_lock.c
src/libnetdata/os/file_lock.h
src/libnetdata/os/mmap_limit.c
src/libnetdata/os/mmap_limit.h
)

list(APPEND LIBNETDATA_FILES ${INICFG_FILES})
Expand Down
13 changes: 11 additions & 2 deletions src/libnetdata/aral/aral.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ struct aral {
#define aral_pages_head_free(ar, marked) (marked ? &ar->aral_lock.pages_marked_free : &ar->aral_lock.pages_free)
#define aral_pages_head_full(ar, marked) (marked ? &ar->aral_lock.pages_marked_full : &ar->aral_lock.pages_full)

static inline bool aral_malloc_use_mmap(ARAL *ar __maybe_unused, size_t size) {
unsigned long long mmap_limit = os_mmap_limit();

if(mmap_limit > 256 * 1000 && size >= ARAL_MALLOC_USE_MMAP_ABOVE)
return true;

return false;
}

const char *aral_name(ARAL *ar) {
return ar->config.name;
}
Expand Down Expand Up @@ -503,7 +512,7 @@ static ALWAYS_INLINE size_t aral_next_allocation_size___adders_lock_needed(ARAL
ar->ops[idx].adders.allocation_size = size;
}

if(!ar->config.mmap.enabled && size < ARAL_MALLOC_USE_MMAP_ABOVE) {
if(!ar->config.mmap.enabled && aral_malloc_use_mmap(ar, size)) {
// when doing malloc, don't allocate entire pages, but only what needed
size =
aral_elements_in_page_size(ar, size) * ar->config.element_size +
Expand Down Expand Up @@ -553,7 +562,7 @@ static ARAL_PAGE *aral_create_page___no_lock_needed(ARAL *ar, size_t size TRACE_
else {
size_t ARAL_PAGE_size = memory_alignment(sizeof(ARAL_PAGE), SYSTEM_REQUIRED_ALIGNMENT);

if (size >= ARAL_MALLOC_USE_MMAP_ABOVE) {
if (aral_malloc_use_mmap(ar, size)) {
bool mapped;
uint8_t *ptr =
nd_mmap_advanced(NULL, size, MAP_ANONYMOUS | MAP_PRIVATE, 1, false, ar->config.options & ARAL_DONT_DUMP, NULL);
Expand Down
21 changes: 21 additions & 0 deletions src/libnetdata/os/mmap_limit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#include "mmap_limit.h"
#include "libnetdata/libnetdata.h"

unsigned long long os_mmap_limit(void) {
static unsigned long long cached_limit = 0;

if (cached_limit)
return cached_limit;

#if defined(OS_LINUX)
if(read_single_number_file("/proc/sys/vm/max_map_count", &cached_limit) != 0)
cached_limit = 65536;
#else
// For other operating systems, assume no limit.
cached_limit = UINT32_MAX;
#endif

return cached_limit;
}
10 changes: 10 additions & 0 deletions src/libnetdata/os/mmap_limit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef NETDATA_MMAP_LIMIT_H
#define NETDATA_MMAP_LIMIT_H

#include "libnetdata/common.h"

unsigned long long os_mmap_limit(void);

#endif //NETDATA_MMAP_LIMIT_H
1 change: 1 addition & 0 deletions src/libnetdata/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "boot_id.h"
#include "run_dir.h"
#include "file_lock.h"
#include "mmap_limit.h"

// this includes windows.h to the whole of netdata
// so various conflicts arise
Expand Down

0 comments on commit b71bc97

Please sign in to comment.