Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,19 @@ C++ engine `ddprof-lib/src/main/cpp/` (implementation-specific code under `hotsp
- Treat caller contract violations (null arguments, mismatched array lengths, an operation
that must never fail in a test) as bugs: throw instead of returning false or
incrementing a failure counter, and do not add validation that duplicates what the loop
already checks.
already checks. This is the Java API convention; native code does not throw — see the
C++ exceptions rule below.
- **C++ exceptions.** Never write `throw`, `try` or `catch` in
`ddprof-lib/src/main/cpp`: an uncaught exception escaping through a JVMTI callback
terminates the JVM — like `abort`, but opaque — and a try/catch frame on a path that a
pthread cancel or exit forced unwind crosses aborts under `-static-libgcc`
(`libraryPatcher_linux.cpp`); glibc's forced unwind cannot be caught in libc++ builds at
all (`j9WallClock.cpp`). Programming bugs are `assert` + `abort()`; expected failures
(OOM, missing data) are error codes or a `false` return, with `new (std::nothrow)`/mmap
allocation where the failure must be survivable. Plain `new` and std containers still
terminate the process on OOM — never rely on catching that. Enforced in CI: the
`qualityChecks` task fails on any `throw`/`try`/`catch` (or listed throwing std helper)
in native sources outside the checker's exception list.
- Explain in a comment why a non-obvious call, init-order assumption, sentinel return value
(e.g. 0 meaning miss/full), omitted lock, unsynchronized access or read of a specific
buffer (e.g. standby) is safe, and why a workaround or test exclusion exists.
Expand Down
1 change: 1 addition & 0 deletions ddprof-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ tasks.register("qualityChecks") {
"check-jvm-boundary-includes.py",
"check-doc-index.py",
"check-sigsetjmp-shape.py",
"check-cpp-exceptions.py",
"check-tracked-artifacts.py",
"check-copyright.py",
"check-claude-md.py",
Expand Down
7 changes: 7 additions & 0 deletions ddprof-lib/src/main/cpp/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ varargs there, including when building error or label strings. O(N)
linear scans only for small sets (cutoff ~256 elements); larger sets need an index or binary
search. Do not resolve symbols or line tables eagerly in hot paths.

**C++ exceptions.** Never write `throw`/`try`/`catch`: an uncaught exception escaping
through a JVMTI callback terminates the JVM, and a try/catch frame on a pthread-cancel or
forced-unwind path aborts under `-static-libgcc` (`libraryPatcher_linux.cpp`). Bugs are
`assert` + `abort()`; expected failures are error codes. Enforced in CI: the `qualityChecks`
task fails on any exception keyword in native sources outside the checker's exception list
(the rule and its failure-handling convention: root `AGENTS.md`, "C++ exceptions").

**Concurrent data structure iteration.** When iterating a concurrent array (e.g.
`CodeCacheArray`), NULL-check every slot — a slot may be count-allocated but
pointer-not-yet-stored. Never permanently advance an iteration cursor past NULL gaps; stop
Expand Down
68 changes: 32 additions & 36 deletions ddprof-lib/src/main/cpp/callTraceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,46 @@ CallTraceStorage::CallTraceStorage() : _active_storage(nullptr), _standby_storag
// storage holds none while the profiler is idle - including before the
// first start(), as Profiler's member is constructed when the library loads.

// Pre-allocate containers to avoid malloc() during hot path operations
_liveness_checkers.reserve(4); // Typical max: 1-2 checkers, avoid growth
// Pre-allocate the checker list to avoid growth: checkers register rarely
// and 1-2 are typical
_liveness_checkers.reserve(4);

// Initialize counters
Counters::set(CALLTRACE_STORAGE_BYTES, 0);
Counters::set(CALLTRACE_STORAGE_TRACES, 0);
}

bool CallTraceStorage::allocateTables() {
std::unique_ptr<CallTraceHashTable> active_table;
std::unique_ptr<CallTraceHashTable> standby_table;
std::unique_ptr<CallTraceHashTable> scratch_table;
bool ok = false;
try {
// Pre-allocate and pre-size collections with conservative load factor
_traces_buffer.max_load_factor(0.75f);
_traces_buffer.rehash(static_cast<size_t>(2048 / 0.75f));

_preserve_set_buffer.max_load_factor(0.75f);
_preserve_set_buffer.rehash(static_cast<size_t>(1024 / 0.75f));

#ifdef UNIT_TEST
if (_fail_allocation_for_test) {
throw std::bad_alloc();
}
#endif
active_table = std::make_unique<CallTraceHashTable>();
standby_table = std::make_unique<CallTraceHashTable>();
scratch_table = std::make_unique<CallTraceHashTable>();
// Set the conservative load factor; the bucket arrays themselves allocate
// on first rotation (see the member comment).
_traces_buffer.max_load_factor(0.75f);
_preserve_set_buffer.max_load_factor(0.75f);

// nothrow allocation: an OOM here must fail acquire(), not terminate the
// process. The constructor itself does not throw: it allocates via
// LinearAllocator (mmap; a failure is reported by usable()).
std::unique_ptr<CallTraceHashTable> active_table(new (std::nothrow) CallTraceHashTable());
std::unique_ptr<CallTraceHashTable> standby_table(new (std::nothrow) CallTraceHashTable());
std::unique_ptr<CallTraceHashTable> scratch_table(new (std::nothrow) CallTraceHashTable());
// The unique_ptrs own and free any table built before a later one failed,
// so a failed acquire() leaks nothing (each table mmaps an 8 MB chunk).
// The failure decision reads the built[] copies, which the UNIT_TEST
// hooks below may null.
CallTraceHashTable *built[] = {active_table.get(), standby_table.get(), scratch_table.get()};
#ifdef UNIT_TEST
CallTraceHashTable *built[] = {active_table.get(), standby_table.get(), scratch_table.get()};
if (_unusable_table_for_test != NO_UNUSABLE_TABLE) {
built[_unusable_table_for_test]->dropTableForTest();
}
#endif
ok = active_table->usable() && standby_table->usable() && scratch_table->usable();
} catch (const std::bad_alloc&) {
if (_null_table_for_test != NO_TABLE_FOR_TEST) {
built[_null_table_for_test] = nullptr;
}
if (_unusable_table_for_test != NO_TABLE_FOR_TEST &&
built[_unusable_table_for_test] != nullptr) {
built[_unusable_table_for_test]->dropTableForTest();
}
#endif
bool ok = built[0] && built[1] && built[2] &&
built[0]->usable() && built[1]->usable() && built[2]->usable();
if (!ok) {
// Nothing is published: the storage stays idle and start() can fail.
// Give the pre-sized bucket arrays back, as release() does.
CallTraceSet().swap(_traces_buffer);
CallTraceIdSet().swap(_preserve_set_buffer);
// The unique_ptrs above free any table built before the failure.
return false;
}

Expand Down Expand Up @@ -248,7 +245,6 @@ void CallTraceStorage::processTraces(std::function<void(const CallTraceSet&)> pr
{
SharedLockGuard lock(&_liveness_lock);

// Use pre-allocated containers to avoid malloc()
_preserve_set_buffer.clear();

for (const auto& checker : _liveness_checkers) {
Expand All @@ -264,7 +260,6 @@ void CallTraceStorage::processTraces(std::function<void(const CallTraceSet&)> pr
CallTraceHashTable* original_standby = const_cast<CallTraceHashTable*>(__atomic_load_n(&_standby_storage, __ATOMIC_ACQUIRE));
CallTraceHashTable* original_scratch = const_cast<CallTraceHashTable*>(__atomic_load_n(&_scratch_storage, __ATOMIC_ACQUIRE));

// Clear process collection for reuse (no malloc/free)
_traces_buffer.clear();

// Step 1: Collect from current standby directly to _traces_buffer with hook for immediate preservation
Expand Down Expand Up @@ -333,8 +328,9 @@ void CallTraceStorage::processTraces(std::function<void(const CallTraceSet&)> pr
original_active->clearAfterFailedDrain();
}

// Triple-buffer rotation maintains trace continuity with thread-safe malloc-free operations:
// - Pre-allocated collections prevent malloc/free during processTraces
// Triple-buffer rotation maintains trace continuity with thread-safe table operations:
// - Collection node allocation repeats per rotation; the bucket arrays
// rehash only when a rotation holds more traces than any earlier one
// - Standby traces collected first (safe - no signal handler writes to standby)
// - New active (old standby, now empty) receives new traces from signal handlers
// - Old active (now scratch) safely collected after rotation, then cleared
Expand Down
40 changes: 28 additions & 12 deletions ddprof-lib/src/main/cpp/callTraceStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include <thread>
#include <chrono>
#ifdef UNIT_TEST
#include <stdexcept>
#include <cassert>
#include <cstdlib>
#endif

// Forward declarations
Expand Down Expand Up @@ -91,15 +92,19 @@ class CallTraceStorage {

bool allocateTables();
#ifdef UNIT_TEST
bool _fail_allocation_for_test = false;
int _unusable_table_for_test = NO_UNUSABLE_TABLE;
int _unusable_table_for_test = NO_TABLE_FOR_TEST;
int _null_table_for_test = NO_TABLE_FOR_TEST;
#endif
// Swaps the three table pointers to nullptr and deletes the tables once
// drained (a table still guarded after the timeout is leaked).
void freeTables();

// Pre-allocated collections for processTraces (single-threaded operation)
// These collections are reused to eliminate malloc/free cycles
// Per-rotation collections for processTraces (single-threaded operation).
// clear() keeps the bucket arrays; node allocation repeats on every
// rotation, and the buckets rehash when a rotation holds more traces than
// any earlier one. The bucket arrays allocate on first use, not in
// acquire(): std containers cannot pre-size without the throwing
// allocator contract, and acquire() must fail without exceptions.
CallTraceSet _traces_buffer; // All traces for JFR processing
CallTraceIdSet _preserve_set_buffer; // Preserve set for current cycle

Expand Down Expand Up @@ -155,17 +160,28 @@ class CallTraceStorage {
bool acquire();

#ifdef UNIT_TEST
// Makes allocateTables() fail as if memory ran out.
void failAllocationForTest(bool fail) { _fail_allocation_for_test = fail; }
static constexpr int NO_UNUSABLE_TABLE = -1;
// Makes allocateTables() build table `index` (0 active, 1 standby, 2 scratch)
// as if its first allocation failed.
static constexpr int NO_TABLE_FOR_TEST = -1;
// Both hooks take the table index (0 active, 1 standby, 2 scratch). An
// out-of-range index is a test bug: assert in debug, abort in release
// builds of the test binary (NDEBUG compiles the assert out).
// Makes allocateTables() build table `index` as if its first allocation
// failed (the table exists but usable() is false).
void unusableTableForTest(int index) {
if (index < NO_UNUSABLE_TABLE || index > 2) {
throw std::out_of_range("unusableTableForTest: index must be -1..2");
if (index < NO_TABLE_FOR_TEST || index > 2) {
assert(false);
abort();
}
_unusable_table_for_test = index;
}
// Makes allocateTables() fail to construct table `index` at all (the
// nothrow allocation of the table object itself returns null).
void nullTableForTest(int index) {
if (index < NO_TABLE_FOR_TEST || index > 2) {
assert(false);
abort();
}
_null_table_for_test = index;
}
#endif
};

Expand Down
11 changes: 5 additions & 6 deletions ddprof-lib/src/main/cpp/flightRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,8 @@ void Lookup::fillJavaMethodInfo(MethodInfo *mi, ResolvedMethod &resolved) {
// from the first fill as on the jmethodID path.
if (resolved.lineNumberTable() != nullptr) {
int size = resolved.lineNumberTableSize();
// Built before ownership moves, so a std::bad_alloc here leaves the table
// with `resolved`, which frees it.
// Built before ownership moves: `resolved` keeps the table until the
// shared_ptr owns it, so no exit path leaves it unowned.
auto table = std::make_shared<SharedLineNumberTable>(
size, const_cast<jvmtiLineNumberEntry *>(resolved.lineNumberTable()),
false /* malloc'd */);
Expand Down Expand Up @@ -778,9 +778,8 @@ MethodInfo *Lookup::fillMethod(ASGCT_CallFrame &frame, jmethodID method_id,

assert(prof_thread != nullptr);
assert(method_id != nullptr && "Already filtered by caller");
// Reinstates the thread's previous landing pad on every exit from this frame,
// including a std::bad_alloc thrown by one of the map or dictionary inserts
// underneath. Leaving ours installed past the end of this frame would leave
// Reinstates the thread's previous landing pad on every exit from this
// frame. Leaving ours installed past the end of this frame would leave
// checkFault() jumping into a dead stack frame.
JmpCtxScope jmp_scope(prof_thread);
ResolveMethodState state;
Expand Down Expand Up @@ -1029,7 +1028,7 @@ off_t Recording::finishChunk(bool end_recording, bool do_cleanup) {
// fjmethodid=lazy resolves raw frames only of these classes: they stay
// pinned until the local references are deleted below. Always called, so a
// failed GetLoadedClasses() leaves no list at all rather than a stale one,
// and ended on every exit from this function, including a std::bad_alloc.
// and ended on every exit from this function.
JVMSupport::beginDump(env, err ? nullptr : classes, err ? 0 : count);
struct DumpScope {
~DumpScope() { JVMSupport::endDump(); }
Expand Down
3 changes: 1 addition & 2 deletions ddprof-lib/src/main/cpp/flightRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,7 @@ class Lookup {
//
// Deliberately NOT a MethodMap entry: resolveMethod()'s siglongjmp landing pad
// hands this row back with crash protection already disarmed, so it must be
// reachable without touching the map, whose operator[] allocates a node and
// can throw std::bad_alloc.
// reachable without touching the map, whose operator[] allocates a node.
//
// Because it is outside the map, the map walk in writeMethods() cannot see it,
// so writeMethods() emits it separately. It has to reach the method pool:
Expand Down
32 changes: 20 additions & 12 deletions ddprof-lib/src/test/cpp/test_callTraceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include "gtest/gtest.h"
#include <csignal>
#include "callTraceStorage.h"
#include <vector>
#include <unordered_map>
Expand Down Expand Up @@ -73,25 +74,29 @@ TEST_F(CallTraceStorageTest, BasicFunctionality) {
EXPECT_TRUE(found_traces);
}

// When memory runs out, acquire() reports it and publishes no table, so a
// later acquire() allocates all three from scratch instead of finding a
// half-initialized storage.
TEST(CallTraceStorageAcquireTest, AllocationFailureLeavesNoTablePublished) {
// A table whose construction failed (the nothrow allocation of the table
// object returned null) leaves no table published, and acquire() still
// succeeds on the next attempt. LeakSanitizer pins the cleanup: when a later
// slot fails, the unique_ptrs must free the tables built before it (each
// table mmaps an 8 MB chunk).
TEST(CallTraceStorageAcquireTest, NullTableLeavesNoTablePublished) {
CallTraceStorage storage;
storage.release();

storage.failAllocationForTest(true);
EXPECT_FALSE(storage.acquire());
EXPECT_EQ(nullptr, storage.activeTableForTest());
for (int index = 0; index < 3; index++) {
storage.nullTableForTest(index);
EXPECT_FALSE(storage.acquire()) << "null table " << index;
EXPECT_EQ(nullptr, storage.activeTableForTest()) << "null table " << index;
}

storage.failAllocationForTest(false);
storage.nullTableForTest(CallTraceStorage::NO_TABLE_FOR_TEST);
EXPECT_TRUE(storage.acquire());
EXPECT_NE(nullptr, storage.activeTableForTest());
EXPECT_TRUE(storage.acquire()); // idempotent once allocated
}

// A table whose constructor could not allocate its first table does not throw.
// acquire() must still fail and publish none of the three tables.
// A table whose constructor could not allocate its first table does not
// throw. acquire() must still fail and publish none of the three tables.
TEST(CallTraceStorageAcquireTest, UnusableTableLeavesNoTablePublished) {
CallTraceStorage storage;
storage.release();
Expand All @@ -102,8 +107,11 @@ TEST(CallTraceStorageAcquireTest, UnusableTableLeavesNoTablePublished) {
EXPECT_EQ(nullptr, storage.activeTableForTest()) << "unusable table " << index;
}

EXPECT_THROW(storage.unusableTableForTest(3), std::out_of_range);
storage.unusableTableForTest(CallTraceStorage::NO_UNUSABLE_TABLE);
// An out-of-range index is a test bug: assert in debug, abort in release
// builds of the test binary; both raise SIGABRT.
EXPECT_EXIT(storage.unusableTableForTest(3), ::testing::KilledBySignal(SIGABRT), "");
EXPECT_EXIT(storage.nullTableForTest(3), ::testing::KilledBySignal(SIGABRT), "");
storage.unusableTableForTest(CallTraceStorage::NO_TABLE_FOR_TEST);
EXPECT_TRUE(storage.acquire());
EXPECT_NE(nullptr, storage.activeTableForTest());
}
Expand Down
13 changes: 8 additions & 5 deletions doc/architecture/CallTraceStorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,16 @@ before the first `start()`); `acquire()` is the only place they are allocated.
`start()` reallocates them when `_calltrace_buffer[0]` is `NULL`. Every reader takes
its stripe lock and drops the sample when its buffer is `NULL`.

## Pre-allocated Buffers
## Reused Collections

`processTraces()` reuses two member collections instead of allocating per call:
`_traces_buffer` (`CallTraceSet`, pre-sized for 2048 traces) and
`_preserve_set_buffer` (`CallTraceIdSet`, pre-sized for 1024 IDs). Both use
`CountingAllocator`, so their memory is reported under `NM_CALLTRACE`. They are
pre-sized again by every `acquire()` and freed by `release()`.
`_traces_buffer` (`CallTraceSet`) and `_preserve_set_buffer` (`CallTraceIdSet`).
Both use `CountingAllocator`, so their memory is reported under `NM_CALLTRACE`.
`clear()` keeps the bucket arrays; node allocation repeats on every rotation,
and the buckets rehash when a rotation holds more traces than any earlier one.
The bucket arrays allocate on first use, not in `acquire()`: std containers
cannot pre-size without the throwing allocator contract, and `acquire()` must
fail without exceptions. `release()` frees the collections.

## Liveness Preservation

Expand Down
Loading
Loading