Skip to content

Commit daa5416

Browse files
committed
Set jemalloc dirty_decay_ms and muzzy_decay_ms to 0 by default, add function to set the values to something else
1 parent bbad94a commit daa5416

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

cpp/src/arrow/memory_pool.cc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
// See discussion in https://github.com/jemalloc/jemalloc/issues/1621
4747

4848
#ifdef NDEBUG
49-
const char* je_arrow_malloc_conf = "oversize_threshold:0";
49+
const char* je_arrow_malloc_conf = "oversize_threshold:0,dirty_decay_ms:0,muzzy_decay_ms:0";
5050
#else
5151
// In debug mode, add memory poisoning on alloc / free
5252
const char* je_arrow_malloc_conf = "oversize_threshold:0,junk:true";
@@ -389,6 +389,30 @@ MemoryPool* default_memory_pool() {
389389
#endif
390390
}
391391

392+
#define RETURN_IF_JEMALLOC_ERROR(ERR) \
393+
do { \
394+
if (err != 0) { \
395+
return Status::UnknownError(std::strerror(ERR)); \
396+
} \
397+
} while (0)
398+
399+
Status jemalloc_set_decay_ms(int ms) {
400+
#ifdef ARROW_JEMALLOC
401+
ssize_t decay_time_ms = static_cast<ssize_t>(ms);
402+
403+
int err = mallctl("arenas.dirty_decay_ms", nullptr, nullptr, &decay_time_ms,
404+
sizeof(decay_time_ms));
405+
RETURN_IF_JEMALLOC_ERROR(err);
406+
err = mallctl("arenas.muzzy_decay_ms", nullptr, nullptr, &decay_time_ms,
407+
sizeof(decay_time_ms));
408+
RETURN_IF_JEMALLOC_ERROR(err);
409+
410+
return Status::OK();
411+
#else
412+
return Status::Invalid("jemalloc support is not built");
413+
#endif
414+
}
415+
392416
///////////////////////////////////////////////////////////////////////
393417
// LoggingMemoryPool implementation
394418

cpp/src/arrow/memory_pool.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ ARROW_EXPORT MemoryPool* system_memory_pool();
160160
/// May return NotImplemented if jemalloc is not available.
161161
ARROW_EXPORT Status jemalloc_memory_pool(MemoryPool** out);
162162

163+
/// \brief Set jemalloc memory page purging behavior for future-created arenas
164+
/// to the indicated number of milliseconds. See dirty_decay_ms and
165+
/// muzzy_decay_ms options in jemalloc for a description of what these do. The
166+
/// default is configured to 0 which releases memory more aggressively to the
167+
/// operating system. If you have a long-running application that uses a lot of
168+
/// memory, you may wish to set this to a higher value. The jemalloc default is
169+
/// 10000 (10 seconds)
170+
ARROW_EXPORT
171+
Status jemalloc_set_decay_ms(int ms);
172+
163173
/// Return a process-wide memory pool based on mimalloc.
164174
///
165175
/// May return NotImplemented if mimalloc is not available.

python/pyarrow/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ def parse_git(root, **kwargs):
108108
from pyarrow.lib import (MemoryPool, LoggingMemoryPool, ProxyMemoryPool,
109109
total_allocated_bytes, set_memory_pool,
110110
default_memory_pool, logging_memory_pool,
111-
proxy_memory_pool, log_memory_allocations)
111+
proxy_memory_pool, log_memory_allocations,
112+
jemalloc_set_decay_ms)
112113

113114
# I/O
114115
from pyarrow.lib import (HdfsFile, NativeFile, PythonFile,

python/pyarrow/includes/libarrow.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
260260

261261
cdef CMemoryPool* c_default_memory_pool" arrow::default_memory_pool"()
262262

263+
CStatus c_jemalloc_set_decay_ms" arrow::jemalloc_set_decay_ms"(int ms)
264+
263265
cdef cppclass CListType" arrow::ListType"(CDataType):
264266
CListType(const shared_ptr[CDataType]& value_type)
265267
CListType(const shared_ptr[CField]& field)

python/pyarrow/memory.pxi

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,21 @@ def total_allocated_bytes():
151151
"""
152152
cdef CMemoryPool* pool = c_get_memory_pool()
153153
return pool.bytes_allocated()
154+
155+
156+
def jemalloc_set_decay_ms(decay_ms):
157+
"""
158+
Set arenas.dirty_decay_ms and arenas.muzzy_decay_ms to indicated number of
159+
milliseconds. A value of 0 (the default) results in dirty / muzzy memory
160+
pages being released right away to the OS, while a higher value will result
161+
in a time-based decay. See the jemalloc docs for more information
162+
163+
It's best to set this at the start of your application.
164+
165+
Parameters
166+
----------
167+
decay_ms : int
168+
Number of milliseconds to set for jemalloc decay conf parameters. Note
169+
that this change will only affect future memory arenas
170+
"""
171+
check_status(c_jemalloc_set_decay_ms(decay_ms))

0 commit comments

Comments
 (0)