Skip to content

Commit 472281b

Browse files
authored
Add debug option to specify MALLOC_ARENA_MAX (#108)
1 parent 6c7ce82 commit 472281b

7 files changed

Lines changed: 39 additions & 5 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
}
1111

1212
plugins {
13-
id 'io.github.gradle-nexus.publish-plugin' version '1.1.0'
13+
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
1414
id "com.diffplug.spotless" version "6.11.0"
1515
}
1616

ddprof-lib/src/main/cpp/javaApi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,8 @@ extern "C" DLLEXPORT jlong JNICALL
254254
Java_com_datadoghq_profiler_JavaProfiler_tscFrequency0(JNIEnv* env, jobject unused) {
255255
return TSC::frequency();
256256
}
257+
258+
extern "C" DLLEXPORT void JNICALL
259+
Java_com_datadoghq_profiler_JavaProfiler_mallocArenaMax0(JNIEnv* env, jobject unused, jint maxArenas) {
260+
OS::mallocArenaMax(maxArenas);
261+
}

ddprof-lib/src/main/cpp/os.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ class OS {
9393
static int fileSize(int fd);
9494
static int truncateFile(int fd);
9595
static void freePageCache(int fd, off_t start_offset);
96+
97+
static void mallocArenaMax(int arena_max);
9698
};
9799

98100
#endif // _OS_H

ddprof-lib/src/main/cpp/os_linux.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#include <unistd.h>
3636
#include "os.h"
3737

38+
#ifndef __musl__
39+
#include <malloc.h>
40+
#endif
3841

3942
#ifdef __LP64__
4043
# define MMAP_SYSCALL __NR_mmap
@@ -360,4 +363,10 @@ void OS::freePageCache(int fd, off_t start_offset) {
360363
posix_fadvise(fd, start_offset & ~page_mask, 0, POSIX_FADV_DONTNEED);
361364
}
362365

366+
void OS::mallocArenaMax(int arena_max) {
367+
#ifndef __musl__
368+
mallopt(M_ARENA_MAX, arena_max);
369+
#endif
370+
}
371+
363372
#endif // __linux__

ddprof-lib/src/main/cpp/os_macos.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,8 @@ void OS::freePageCache(int fd, off_t start_offset) {
342342
// Not supported on macOS
343343
}
344344

345+
void OS::mallocArenaMax(int arena_max) {
346+
// Not supported on macOS
347+
}
348+
345349
#endif // __APPLE__

ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
public final class JavaProfiler {
4343
private static final String NATIVE_LIBS = "/META-INF/native-libs";
4444
private static final String LIBRARY_NAME = "libjavaProfiler." + (OperatingSystem.current() == OperatingSystem.macos ? "dylib" : "so");
45-
45+
4646
private static final Unsafe UNSAFE;
4747
static {
4848
Unsafe unsafe = null;
@@ -122,6 +122,16 @@ public static synchronized JavaProfiler getInstance(String libLocation, String s
122122
System.load(libLocation);
123123
profiler.initializeContextStorage();
124124
instance = profiler;
125+
126+
String maxArenaValue = System.getProperty("ddprof.debug.malloc_arena_max");
127+
if (maxArenaValue != null) {
128+
try {
129+
mallocArenaMax0(Integer.parseInt(maxArenaValue));
130+
} catch (NumberFormatException e) {
131+
System.out.println("[WARN] Invalid value for ddprof.debug.malloc_arena_max: " + maxArenaValue + ". Expecting an integer.");
132+
}
133+
}
134+
125135
return profiler;
126136
}
127137

@@ -139,7 +149,7 @@ public static synchronized JavaProfiler getInstance(String libLocation, String s
139149
*/
140150
private static Path libraryFromClasspath(OperatingSystem os, Arch arch, String qualifier, Path tempDir) throws IOException {
141151
String resourcePath = NATIVE_LIBS + "/" + os.name().toLowerCase() + "-" + arch.name().toLowerCase() + ((qualifier != null && !qualifier.isEmpty()) ? "-" + qualifier : "") + "/" + LIBRARY_NAME;
142-
152+
143153
InputStream libraryData = JavaProfiler.class.getResourceAsStream(resourcePath);
144154

145155
if (libraryData != null) {
@@ -433,7 +443,7 @@ public boolean isThresholdExceeded(long thresholdMillis, long startTicks, long e
433443
* @param origin the thread the task was submitted on
434444
*/
435445
public void recordQueueTime(long startTicks, long endTicks, Class<?> task, Class<?> scheduler,
436-
Thread origin) {
446+
Thread origin) {
437447
recordQueueEnd0(startTicks, endTicks, task.getName(), scheduler.getName(), origin);
438448
}
439449

@@ -499,7 +509,7 @@ static boolean isMuslProcSelfMaps() throws IOException {
499509
* However, if such string is missing should indicate that the system is not a musl one.
500510
*/
501511
static boolean isMuslJavaExecutable() throws IOException {
502-
512+
503513
byte[] magic = new byte[]{(byte)0x7f, (byte)'E', (byte)'L', (byte)'F'};
504514
byte[] prefix = new byte[]{(byte)'/', (byte)'l', (byte)'d', (byte)'-'}; // '/ld-*'
505515
byte[] musl = new byte[]{(byte)'m', (byte)'u', (byte)'s', (byte)'l'}; // 'musl'
@@ -573,4 +583,6 @@ private static boolean containsArray(byte[] container, int offset, byte[] contai
573583
private static native long currentTicks0();
574584

575585
private static native long tscFrequency0();
586+
587+
private static native void mallocArenaMax0(int max);
576588
}

ddprof-test/src/test/java/com/datadoghq/profiler/context/TagContextTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public void test() throws InterruptedException {
6868
long sum = 0;
6969
long[] weights = new long[strings.length];
7070
for (int i = 0; i < strings.length; i++) {
71+
AtomicLong weight = weightsByTagValue.get(strings[i]);
72+
assertNotNull(weight, "Weight for " + strings[i] + " not found");
7173
weights[i] = weightsByTagValue.get(strings[i]).get();
7274
sum += weights[i];
7375
}

0 commit comments

Comments
 (0)