-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: using jemalloc? #9093
Draft
lucasxia01
wants to merge
3
commits into
master
Choose a base branch
from
lx/jemalloc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−0
Draft
feat: using jemalloc? #9093
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Based on https://github.com/apple/foundationdb/blob/0946f495797408b28d02b52b1c0a7c4b6a401736/cmake/Jemalloc.cmake | ||
|
||
add_library(jemalloc INTERFACE) | ||
add_library(im_jemalloc_pic STATIC IMPORTED) | ||
add_library(im_jemalloc STATIC IMPORTED) | ||
include(ExternalProject) | ||
set(JEMALLOC_DIR "${CMAKE_BINARY_DIR}/jemalloc") | ||
ExternalProject_add(Jemalloc_project | ||
URL "https://github.com/jemalloc/jemalloc/releases/download/5.3.0/jemalloc-5.3.0.tar.bz2" | ||
URL_HASH SHA256=2db82d1e7119df3e71b7640219b6dfe84789bc0537983c3b7ac4f7189aecfeaa | ||
BUILD_BYPRODUCTS "${JEMALLOC_DIR}/include/jemalloc/jemalloc.h" | ||
"${JEMALLOC_DIR}/lib/libjemalloc.a" | ||
"${JEMALLOC_DIR}/lib/libjemalloc_pic.a" | ||
CONFIGURE_COMMAND ./configure --prefix=${JEMALLOC_DIR} --enable-static | ||
BUILD_IN_SOURCE ON | ||
BUILD_COMMAND make | ||
INSTALL_DIR "${JEMALLOC_DIR}" | ||
INSTALL_COMMAND make install) | ||
add_dependencies(im_jemalloc Jemalloc_project) | ||
add_dependencies(im_jemalloc_pic Jemalloc_project) | ||
set_target_properties(im_jemalloc_pic PROPERTIES IMPORTED_LOCATION "${JEMALLOC_DIR}/lib/libjemalloc_pic.a") | ||
set_target_properties(im_jemalloc PROPERTIES IMPORTED_LOCATION "${JEMALLOC_DIR}/lib/libjemalloc.a") | ||
target_link_libraries(jemalloc INTERFACE im_jemalloc_pic im_jemalloc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
barretenberg/cpp/src/barretenberg/common/jemalloc_helper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
|
||
// Aztec-specific: We include this file from | ||
// https://github.com/tabokie/rocksdb/blob/54dd1848405edeaf63a96432ec11471a133a7633/port/jemalloc_helper.h | ||
// to help with clang issues with linking jemalloc. File replicated below. | ||
|
||
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. | ||
// This source code is licensed under both the GPLv2 (found in the | ||
// COPYING file in the root directory) and Apache 2.0 License | ||
// (found in the LICENSE.Apache file in the root directory). | ||
|
||
#pragma once | ||
|
||
#if defined(__clang__) | ||
// glibc's `posix_memalign()` declaration specifies `throw()` while clang's | ||
// declaration does not. There is a hack in clang to make its re-declaration | ||
// compatible with glibc's if they are declared consecutively. That hack breaks | ||
// if yet another `posix_memalign()` declaration comes between glibc's and | ||
// clang's declarations. Include "mm_malloc.h" here ensures glibc's and clang's | ||
// declarations both come before "jemalloc.h"'s `posix_memalign()` declaration. | ||
// | ||
// This problem could also be avoided if "jemalloc.h"'s `posix_memalign()` | ||
// declaration did not specify `throw()` when built with clang. | ||
#include <mm_malloc.h> | ||
#endif | ||
|
||
#ifdef ROCKSDB_JEMALLOC | ||
#ifdef __FreeBSD__ | ||
#include <malloc_np.h> | ||
#else | ||
#define JEMALLOC_MANGLE | ||
#include <jemalloc/jemalloc.h> | ||
#endif | ||
|
||
#ifndef JEMALLOC_CXX_THROW | ||
#define JEMALLOC_CXX_THROW | ||
#endif | ||
|
||
#if defined(OS_WIN) && defined(_MSC_VER) | ||
|
||
// MSVC does not have weak symbol support. As long as ROCKSDB_JEMALLOC is defined, | ||
// Jemalloc memory allocator is used. | ||
static inline bool HasJemalloc() | ||
{ | ||
return true; | ||
} | ||
|
||
#else | ||
|
||
// Declare non-standard jemalloc APIs as weak symbols. We can null-check these | ||
// symbols to detect whether jemalloc is linked with the binary. | ||
extern "C" JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN void JEMALLOC_NOTHROW* mallocx(size_t, int) JEMALLOC_ATTR(malloc) | ||
JEMALLOC_ALLOC_SIZE(1) __attribute__((__weak__)); | ||
extern "C" JEMALLOC_ALLOCATOR JEMALLOC_RESTRICT_RETURN void JEMALLOC_NOTHROW* rallocx(void*, size_t, int) | ||
JEMALLOC_ALLOC_SIZE(2) __attribute__((__weak__)); | ||
extern "C" size_t JEMALLOC_NOTHROW xallocx(void*, size_t, size_t, int) __attribute__((__weak__)); | ||
extern "C" size_t JEMALLOC_NOTHROW sallocx(const void*, int) JEMALLOC_ATTR(pure) __attribute__((__weak__)); | ||
extern "C" void JEMALLOC_NOTHROW dallocx(void*, int) __attribute__((__weak__)); | ||
extern "C" void JEMALLOC_NOTHROW sdallocx(void*, size_t, int) __attribute__((__weak__)); | ||
extern "C" size_t JEMALLOC_NOTHROW nallocx(size_t, int) JEMALLOC_ATTR(pure) __attribute__((__weak__)); | ||
extern "C" int JEMALLOC_NOTHROW mallctl(const char*, void*, size_t*, void*, size_t) __attribute__((__weak__)); | ||
extern "C" int JEMALLOC_NOTHROW mallctlnametomib(const char*, size_t*, size_t*) __attribute__((__weak__)); | ||
extern "C" int JEMALLOC_NOTHROW mallctlbymib(const size_t*, size_t, void*, size_t*, void*, size_t) | ||
__attribute__((__weak__)); | ||
extern "C" void JEMALLOC_NOTHROW malloc_stats_print(void (*)(void*, const char*), void*, const char*) | ||
__attribute__((__weak__)); | ||
extern "C" size_t JEMALLOC_NOTHROW malloc_usable_size(JEMALLOC_USABLE_SIZE_CONST void*) JEMALLOC_CXX_THROW | ||
__attribute__((__weak__)); | ||
|
||
// Check if Jemalloc is linked with the binary. Note the main program might be | ||
// using a different memory allocator even this method return true. | ||
// It is loosely based on folly::usingJEMalloc(), minus the check that actually | ||
// allocate memory and see if it is through jemalloc, to handle the dlopen() | ||
// case: | ||
// https://github.com/facebook/folly/blob/76cf8b5841fb33137cfbf8b224f0226437c855bc/folly/memory/Malloc.h#L147 | ||
static inline bool HasJemalloc() | ||
{ | ||
return mallocx != nullptr && rallocx != nullptr && xallocx != nullptr && sallocx != nullptr && dallocx != nullptr && | ||
sdallocx != nullptr && nallocx != nullptr && mallctl != nullptr && mallctlnametomib != nullptr && | ||
mallctlbymib != nullptr && malloc_stats_print != nullptr && malloc_usable_size != nullptr; | ||
} | ||
|
||
#endif | ||
|
||
#endif // ROCKSDB_JEMALLOC |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is some tricky stuff to get it from here to working, but in theory it's just making jemalloc emit as jemalloc and not malloc and then linking the header properly