Skip to content

Multicore statmemprof support, with a caller-supplied Memprof implementation - #1

Open
dimitris-m wants to merge 6 commits into
masterfrom
dm/multicore-memprof-limits-compat
Open

dimitris-m wants to merge 6 commits into
masterfrom
dm/multicore-memprof-limits-compat

Conversation

@dimitris-m

Copy link
Copy Markdown

Based on janestreet#22 by @tmcgilchrist. The first four commits are
taken unchanged from the tip of that branch, one of them authored by
@grouptheoryiscool.

Fix external sampling, MEMTRACE_RATE and locking defects

  • ext_alloc samples when an external allocation crosses the threshold, not one
    allocation later.
  • An empty MEMTRACE_RATE is treated as unset.
  • lock_tracer re-tests the failed flag after acquiring the mutex.
  • The external sample count is an ordinary reference.

Allow the caller to supply the Memprof implementation

start_tracing and trace_if_requested take
?memprof:(module Memtrace.Memprof_sig), where Memprof_sig is
module type of Stdlib.Gc.Memprof, defaulting to Gc.Memprof. This lets
memtrace share a profile with another user of the interface, such as
memprof-limits.

No plans to merge this but we need it in opengrep.

tmcgilchrist and others added 6 commits May 27, 2025 10:34
Signed-off-by: Tim McGilchrist <timmcgil@gmail.com>
Signed-off-by: Tim McGilchrist <timmcgil@gmail.com>
Signed-off-by: Tim McGilchrist <timmcgil@gmail.com>
Signed-off-by: Tim McGilchrist <timmcgil@gmail.com>
Take a sample when an external allocation crosses the sampling
threshold, rather than one allocation later.

Treat an empty MEMTRACE_RATE as unset, so the rate falls back to the
one given by the caller or to the default, rather than raising
Invalid_argument.

Re-test the failed flag after acquiring the tracer mutex. One domain
can mark the tracer as failed while another is already waiting for the
lock, which can cause an assertion failure in the waiting domain.

Use an ordinary reference for the external sample count, which is only
read and written inside the critical section.
Only one Memprof profile can run per domain, so a library that starts
its own profile, such as memprof-limits, cannot be used together with
memtrace. The caller can now pass the Memprof implementation that
memtrace should use, so that both use the same profile. It defaults to
the standard library's Gc.Memprof.

Stopping the tracer also discards the profile, so no further callbacks
arrive once tracing has ended.

Starting tracing manually now takes a final unit argument.

Requires OCaml 5.3 or later. domainslib is needed only for the
examples.
@dimitris-m
dimitris-m force-pushed the dm/multicore-memprof-limits-compat branch from e07ce89 to 874e70b Compare September 17, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants