Skip to content

Commit

Permalink
add fallback for thread count if jemalloc cannot identify
Browse files Browse the repository at this point in the history
  • Loading branch information
lnkuiper committed Nov 4, 2024
1 parent 1986445 commit 961af3a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions extension/jemalloc/jemalloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Add this to `jemalloc.h`:
We also supply our own config string in `jemalloc.c`.
Define this just after the `#include`s.
```c++
#include "malloc_ncpus.h"
#define JE_MALLOC_CONF_BUFFER_SIZE 200
char JE_MALLOC_CONF_BUFFER[JE_MALLOC_CONF_BUFFER_SIZE];
```
Expand All @@ -74,6 +76,9 @@ JEMALLOC_ATTR(constructor)
static void
jemalloc_constructor(void) {
unsigned long long cpu_count = malloc_ncpus();
if (cpu_count == 0) {
cpu_count = duckdb_malloc_ncpus();
}
unsigned long long bgt_count = cpu_count / 16;
if (bgt_count == 0) {
bgt_count = 1;
Expand Down
5 changes: 5 additions & 0 deletions extension/jemalloc/jemalloc/src/jemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "jemalloc/internal/thread_event.h"
#include "jemalloc/internal/util.h"

#include "malloc_ncpus.h"

/******************************************************************************/
/* Data. */

Expand Down Expand Up @@ -4270,6 +4272,9 @@ JEMALLOC_ATTR(constructor)
static void
jemalloc_constructor(void) {
unsigned long long cpu_count = malloc_ncpus();
if (cpu_count == 0) {
cpu_count = duckdb_malloc_ncpus();
}
unsigned long long bgt_count = cpu_count / 16;
if (bgt_count == 0) {
bgt_count = 1;
Expand Down
7 changes: 7 additions & 0 deletions extension/jemalloc/jemalloc_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include "duckdb/common/allocator.hpp"
#include "jemalloc/jemalloc.h"
#include "malloc_ncpus.h"

#include <thread>

namespace duckdb {

Expand Down Expand Up @@ -114,6 +117,10 @@ std::string JemallocExtension::Version() const {

extern "C" {

unsigned duckdb_malloc_ncpus() {
return duckdb::NumericCast<unsigned>(std::thread::hardware_concurrency());
}

DUCKDB_EXTENSION_API void jemalloc_init(duckdb::DatabaseInstance &db) {
duckdb::DuckDB db_wrapper(db);
db_wrapper.LoadExtension<duckdb::JemallocExtension>();
Expand Down

0 comments on commit 961af3a

Please sign in to comment.