Skip to content

Commit

Permalink
Patch build system to allow building with CHPL_TARGET_JEMALLOC=system (
Browse files Browse the repository at this point in the history
…chapel-lang#25158)

This PR patches some of our build configuration scripts to be able to
use `CHPL_TARGET_JEMALLOC=system`. This allows us to use our preferred
configuration for performance (`CHPL_MEM=jemalloc`) in situations where
we cannot build the bundled jemalloc (i.e. for packaging)

When using a system jemalloc 5.x, we have to turn off chunk hooks
(jemalloc 5.x does not support it)

Testing:
- [x] `start_test test/release/examples` with `CHPL_MEM=jemalloc` and
`CHPL_TARGET_JEMALLOC` on Mac

Notes:
- Builds on top of chapel-lang#25145
- Fully unblocks chapel-lang#24931 and
chapel-lang#24655, and enables homebrew
to use the preferred config
(chapel-lang#25148)
- Uses diffs from chapel-lang#23409

[Reviewed by @jhh67]
  • Loading branch information
jabraham17 authored Jun 6, 2024
2 parents 6496bea + 6bebce5 commit 1b1b920
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
10 changes: 5 additions & 5 deletions runtime/src/mem/jemalloc/mem-jemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ unsigned CHPL_JE_LG_ARENA;
// Decide whether or not to try to use jemalloc's chunk hooks interface
// jemalloc < 4.0 didn't support chunk_hooks_t
// jemalloc 4.1 changed opt.nareas from size_t to unsigned
// .. so we use chunk hooks interface for jemalloc >= 4.1
#if JEMALLOC_VERSION_MAJOR > 4
#define USE_JE_CHUNK_HOOKS
#endif
// jemalloc 5.x migrated to an extent API
// .. so we use chunk hooks interface for jemalloc >= 4.1 and < 5.0
#if (JEMALLOC_VERSION_MAJOR == 4) && (JEMALLOC_VERSION_MINOR >= 1)
#define USE_JE_CHUNK_HOOKS
#endif
Expand All @@ -66,6 +64,7 @@ static struct shared_heap {
} heap;


#ifdef USE_JE_CHUNK_HOOKS
// compute aligned index into our shared heap, alignment must be a power of 2
static inline void* alignHelper(void* base_ptr, size_t offset, size_t alignment) {
uintptr_t p;
Expand All @@ -75,6 +74,7 @@ static inline void* alignHelper(void* base_ptr, size_t offset, size_t alignment)
p = (p + alignment - 1) & ~(alignment - 1);
return(void*) p;
}
#endif


// *** Chunk hook replacements *** //
Expand Down Expand Up @@ -302,7 +302,7 @@ static void replaceChunkHooks(void) {
}
}
#else
chpl_internal_error("cannot init multi-locale heap: please rebuild with jemalloc >= 4.1");
chpl_internal_error("cannot init multi-locale heap: please rebuild with jemalloc >= 4.1 and < 5.0");
#endif

}
Expand Down
7 changes: 5 additions & 2 deletions third-party/qthread/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ ifeq ($(CFLAGS_NEEDS_RT_INCLUDES), y)
# seems to cause Chapel programs built with the result to hang during
# exit when shutting down Qthreads.
#
# We also need to provide paths to system hwloc if its enabled
# since it might not be in default compiler search paths.
# We also need to provide paths to system hwloc and jemalloc if they are
# enabled, since it might not be in default compiler search paths.
#
# Note that we call compileline with CHPL_MAKE_COMM=none. Under
# CHPL_MAKE_COMM=gasnet, compileline will fail if gasnet is not built,
Expand All @@ -92,6 +92,9 @@ ifeq ($(CFLAGS_NEEDS_RT_INCLUDES), y)
ifeq ($(CHPL_MAKE_HWLOC),system)
SYSTEM_INCS_DEFS := $(SYSTEM_INCS_DEFS)\|hwloc
endif
ifeq ($(CHPL_MAKE_TARGET_JEMALLOC),system)
SYSTEM_INCS_DEFS := $(SYSTEM_INCS_DEFS)\|jemalloc
endif

COMPILELINE_STDERR_REDIRECT=2> /dev/null
COMM_NONE_CHPLENV_CACHE := $(shell echo "$(CHPL_MAKE_CHPLENV_CACHE)" | sed 's/|CHPL_MAKE_COMM=[^|]*|/|CHPL_MAKE_COMM=none|/')
Expand Down
26 changes: 25 additions & 1 deletion util/chplenv/chpl_jemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ def get_compile_args(flag):
ucp_val = get_uniq_cfg_path(flag)
return third_party_utils.get_bundled_compile_args('jemalloc',
ucp=ucp_val)
elif jemalloc_val == 'system':
# try pkg-config
args = third_party_utils.pkgconfig_get_system_compile_args('jemalloc')
if args != (None, None):
return args
# try homebrew
jemalloc_prefix = chpl_platform.get_homebrew_prefix('jemalloc')
if jemalloc_prefix:
return ([], ['-I{0}'.format(os.path.join(jemalloc_prefix, 'include'))])

envname = "CHPL_TARGET_JEMALLOC" if flag == "target" else "CHPL_HOST_JEMALLOC"
error("Could not find a suitable jemalloc installation. Please install jemalloc or set {}=bundled".format(envname, envname))

return ([ ], [ ])

# flag is host or target
Expand All @@ -116,7 +129,18 @@ def get_link_args(flag):
return (libs, morelibs)

if jemalloc_val == 'system':
return ([ ], ['-ljemalloc'])
# try pkg-config
args = third_party_utils.pkgconfig_get_system_link_args('jemalloc')
if args != (None, None):
return args
# try homebrew
jemalloc_prefix = chpl_platform.get_homebrew_prefix('jemalloc')
if jemalloc_prefix:
return ([], ['-L{0}'.format(os.path.join(jemalloc_prefix, 'lib')),
'-ljemalloc'])

envname = "CHPL_TARGET_JEMALLOC" if flag == "target" else "CHPL_HOST_JEMALLOC"
error("Could not find a suitable jemalloc installation. Please install jemalloc or set {}=bundled or {}=none.".format(envname, envname))

return ([ ], [ ])

Expand Down
2 changes: 1 addition & 1 deletion util/chplenv/compile_link_args_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_runtime_includes_and_defines():
system.append("-isystem" + os.path.join(sdk_path, "hip", "include"))
system.append("-isystem" + os.path.join(sdk_path, "hsa", "include"))

if mem == "jemalloc":
if mem == "jemalloc" and chpl_jemalloc.get('target') == "bundled":
# set -DCHPL_JEMALLOC_PREFIX=chpl_je_
# this is needed since it affects code inside of headers
bundled.append("-DCHPL_JEMALLOC_PREFIX=chpl_je_")
Expand Down
7 changes: 6 additions & 1 deletion util/chplenv/third-party-pkgs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def generate_subdir_map():
pkg_aliases['CHPL_GMP-bundled'] = 'gmp'
pkg_aliases['CHPL_HWLOC-bundled'] = 'hwloc'
pkg_aliases['CHPL_JEMALLOC-bundled'] = 'jemalloc'
pkg_aliases['CHPL_TARGET_JEMALLOC-bundled'] = 'jemalloc'
pkg_aliases['CHPL_HOST_JEMALLOC-bundled'] = 'jemalloc'
pkg_aliases['CHPL_LIBFABRIC-bundled'] = 'libfabric'
pkg_aliases['CHPL_LLVM-bundled'] = 'llvm'
pkg_aliases['CHPL_UNWIND-bundled'] = 'libunwind'
Expand All @@ -71,7 +73,10 @@ def compute_required_pkgs():
(var, val) = var.strip(), val.strip()
# Ignore platform variables
# Ignore CHPL_MEM (use CHPL_JEMALLOC)
if not (var.endswith('_PLATFORM') or var == 'CHPL_MEM'):
# Ignore CHPL_HOST_MEM (use CHPL_JEMALLOC)
# Ignore CHPL_TARGET_MEM (use CHPL_JEMALLOC)
ignore_vars = ['CHPL_MEM', 'CHPL_HOST_MEM', 'CHPL_TARGET_MEM']
if not (var.endswith('_PLATFORM') or var in ignore_vars):
if val in get_all_pkgs():
req_pkgs.add(val)
elif var + '-' + val in pkg_aliases:
Expand Down

0 comments on commit 1b1b920

Please sign in to comment.