Skip to content

Commit

Permalink
Add helper to print malloc_stats()
Browse files Browse the repository at this point in the history
The `malloc_stats()` API has been useful to debug issues with
memory fragmentation (see
<DataDog/libdatadog#293>) so let's keep a
helper to make it easy to access.
  • Loading branch information
ivoanjo committed Jan 31, 2024
1 parent 482021c commit 0fa8b1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ext/ddtrace_profiling_native_extension/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def add_compiler_flag(flag)
$defs << '-DHAVE_PTHREAD_GETCPUCLOCKID'
end

have_func 'malloc_stats'

# On older Rubies, rb_postponed_job_preregister/rb_postponed_job_trigger did not exist
$defs << '-DNO_POSTPONED_TRIGGER' if RUBY_VERSION < '3.3'

Expand Down
10 changes: 10 additions & 0 deletions ext/ddtrace_profiling_native_extension/profiling.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <ruby.h>
#include <ruby/thread.h>
#include <errno.h>
#include <malloc.h>

#include "clock_id.h"
#include "helpers.h"
Expand Down Expand Up @@ -32,6 +33,7 @@ static void holding_the_gvl_signal_handler(DDTRACE_UNUSED int _signal, DDTRACE_U
static VALUE _native_trigger_holding_the_gvl_signal_handler_on(DDTRACE_UNUSED VALUE _self, VALUE background_thread);
static VALUE _native_enforce_success(DDTRACE_UNUSED VALUE _self, VALUE syserr_errno, VALUE with_gvl);
static void *trigger_enforce_success(void *trigger_args);
static VALUE _native_malloc_stats(DDTRACE_UNUSED VALUE _self);

void DDTRACE_EXPORT Init_ddtrace_profiling_native_extension(void) {
VALUE datadog_module = rb_define_module("Datadog");
Expand Down Expand Up @@ -65,6 +67,7 @@ void DDTRACE_EXPORT Init_ddtrace_profiling_native_extension(void) {
rb_define_singleton_method(testing_module, "_native_install_holding_the_gvl_signal_handler", _native_install_holding_the_gvl_signal_handler, 0);
rb_define_singleton_method(testing_module, "_native_trigger_holding_the_gvl_signal_handler_on", _native_trigger_holding_the_gvl_signal_handler_on, 1);
rb_define_singleton_method(testing_module, "_native_enforce_success", _native_enforce_success, 2);
rb_define_singleton_method(testing_module, "_native_malloc_stats", _native_malloc_stats, 0);
}

static VALUE native_working_p(DDTRACE_UNUSED VALUE _self) {
Expand Down Expand Up @@ -249,3 +252,10 @@ static void *trigger_enforce_success(void *trigger_args) {
ENFORCE_SUCCESS_NO_GVL(syserr_errno);
return NULL;
}

static VALUE _native_malloc_stats(DDTRACE_UNUSED VALUE _self) {
#ifdef HAVE_MALLOC_STATS
malloc_stats();
#endif
return Qnil;
}

0 comments on commit 0fa8b1e

Please sign in to comment.