Skip to content
Merged
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
11 changes: 11 additions & 0 deletions ddprof-lib/src/main/cpp/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ Error Arguments::parse(const char *args) {
_lightweight = false;
}
}
CASE("wallsampler")
if (value != NULL) {
switch (value[0]) {
case 'j':
_wallclock_sampler = JVMTI;
break;
case 'a':
default:
_wallclock_sampler = ASGCT;
}
}

DEFAULT()
if (_unknown_arg == NULL)
Expand Down
59 changes: 41 additions & 18 deletions ddprof-lib/src/main/cpp/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ enum JfrOption {
NO_SYSTEM_INFO | NO_SYSTEM_PROPS | NO_NATIVE_LIBS | NO_CPU_LOAD
};

enum WallclockSampler {
ASGCT,
JVMTI
};

struct Multiplier {
char symbol;
long multiplier;
Expand Down Expand Up @@ -123,37 +128,55 @@ class Arguments {
long _wall;
bool _wall_collapsing;
int _wall_threads_per_tick;
WallclockSampler _wallclock_sampler;
long _memory;
bool _record_allocations;
bool _record_liveness;
double _live_samples_ratio;
bool _record_heap_usage;
bool _gc_generations;
int _jstackdepth;
int _jstackdepth;
int _safe_mode;
const char *_file;
const char *_log;
const char *_loglevel;
const char *_unknown_arg;
const char *_filter;
const char* _file;
const char* _log;
const char* _loglevel;
const char* _unknown_arg;
const char* _filter;
CStack _cstack;
int _jfr_options;
std::vector<std::string> _context_attributes;
bool _lightweight;

Arguments(bool persistent = false)
: _buf(NULL), _shared(false), _persistent(persistent),
_action(ACTION_NONE), _ring(RING_ANY), _event(NULL), _interval(0),
_cpu(-1), _wall(-1), _wall_collapsing(false),
_wall_threads_per_tick(DEFAULT_WALL_THREADS_PER_TICK), _memory(-1),
_record_allocations(false), _record_liveness(false),
_live_samples_ratio(
0.1), // default to liveness-tracking 10% of the allocation samples
_record_heap_usage(false), _gc_generations(false),
_jstackdepth(DEFAULT_JSTACKDEPTH), _safe_mode(0), _file(NULL),
_log(NULL), _loglevel(NULL), _unknown_arg(NULL), _filter(NULL),
_cstack(CSTACK_DEFAULT), _jfr_options(0), _context_attributes({}),
_lightweight(false) {}
: _buf(NULL),
_shared(false),
_persistent(persistent),
_action(ACTION_NONE),
_ring(RING_ANY),
_event(NULL),
_interval(0),
_cpu(-1),
_wall(-1),
_wall_collapsing(false),
_wall_threads_per_tick(DEFAULT_WALL_THREADS_PER_TICK),
_memory(-1),
_record_allocations(false),
_record_liveness(false),
_live_samples_ratio(0.1), // default to liveness-tracking 10% of the allocation samples
_record_heap_usage(false),
_gc_generations(false),
_jstackdepth(DEFAULT_JSTACKDEPTH),
_safe_mode(0),
_file(NULL),
_log(NULL),
_loglevel(NULL),
_unknown_arg(NULL),
_filter(NULL),
_cstack(CSTACK_DEFAULT),
_jfr_options(0),
_context_attributes({}),
_lightweight(false),
_wallclock_sampler(ASGCT) {}

~Arguments();

Expand Down
4 changes: 2 additions & 2 deletions ddprof-lib/src/main/cpp/callTraceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ CallTrace *CallTraceStorage::findCallTrace(LongHashTable *table, u64 hash) {
}

u32 CallTraceStorage::put(int num_frames, ASGCT_CallFrame *frames,
bool truncated, u64 counter) {
bool truncated, u64 weight) {
// Currently, CallTraceStorage is a singleton used globally in Profiler and
// therefore start-stop operation requires data structures cleanup. This
// cleanup may and will race this method and the racing can cause all sorts of
Expand Down Expand Up @@ -268,7 +268,7 @@ u32 CallTraceStorage::put(int num_frames, ASGCT_CallFrame *frames,

CallTraceSample &s = table->values()[slot];
atomicInc(s.samples);
atomicInc(s.counter, counter);
atomicInc(s.counter, weight);

_lock.unlockShared();
return capacity - (INITIAL_CAPACITY - 1) + slot;
Expand Down
2 changes: 1 addition & 1 deletion ddprof-lib/src/main/cpp/callTraceStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CallTraceStorage {
void clear();
void collectTraces(std::map<u32, CallTrace *> &map);

u32 put(int num_frames, ASGCT_CallFrame *frames, bool truncated, u64 counter);
u32 put(int num_frames, ASGCT_CallFrame *frames, bool truncated, u64 weight);
};

#endif // _CALLTRACESTORAGE
2 changes: 1 addition & 1 deletion ddprof-lib/src/main/cpp/flightRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ void FlightRecorder::recordHeapUsage(int lock_index, long value, bool live) {
}

void FlightRecorder::recordEvent(int lock_index, int tid, u32 call_trace_id,
int event_type, Event *event, u64 counter) {
int event_type, Event *event) {
if (_rec != NULL) {
RecordingBuffer *buf = _rec->buffer(lock_index);
switch (event_type) {
Expand Down
2 changes: 1 addition & 1 deletion ddprof-lib/src/main/cpp/flightRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class FlightRecorder {
bool active() const { return _rec != NULL; }

void recordEvent(int lock_index, int tid, u32 call_trace_id, int event_type,
Event *event, u64 counter);
Event *event);

void recordLog(LogLevel level, const char *message, size_t len);

Expand Down
21 changes: 14 additions & 7 deletions ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ static void (*orig_busHandler)(int signo, siginfo_t *siginfo, void *ucontext);

static Engine noop_engine;
static PerfEvents perf_events;
static WallClock wall_engine;
static WallClockASGCT wall_asgct_engine;
static WallClockJVMTI wall_jvmti_engine;
static J9WallClock j9_engine;
static ITimer itimer;
static CTimer ctimer;
Expand Down Expand Up @@ -690,7 +691,7 @@ void Profiler::recordExternalSample(u64 counter, int tid,
call_trace_id =
_call_trace_storage.put(num_frames, frames, truncated, counter);
}
_jfr.recordEvent(lock_index, tid, call_trace_id, event_type, event, counter);
_jfr.recordEvent(lock_index, tid, call_trace_id, event_type, event);

_locks[lock_index].unlock();
}
Expand Down Expand Up @@ -763,7 +764,7 @@ void Profiler::recordSample(void *ucontext, u64 counter, int tid,
thread->recordCallTraceId(call_trace_id);
}
}
_jfr.recordEvent(lock_index, tid, call_trace_id, event_type, event, counter);
_jfr.recordEvent(lock_index, tid, call_trace_id, event_type, event);

_locks[lock_index].unlock();
}
Expand Down Expand Up @@ -801,13 +802,13 @@ void Profiler::recordQueueTime(int tid, QueueTimeEvent *event) {
_locks[lock_index].unlock();
}

void Profiler::recordExternalSample(u64 counter, int tid, int num_frames,
void Profiler::recordExternalSample(u64 weight, int tid, int num_frames,
ASGCT_CallFrame *frames, bool truncated,
jint event_type, Event *event) {
atomicInc(_total_samples);

u32 call_trace_id =
_call_trace_storage.put(num_frames, frames, truncated, counter);
_call_trace_storage.put(num_frames, frames, truncated, weight);

u32 lock_index = getLockIndex(tid);
if (!_locks[lock_index].tryLock() &&
Expand All @@ -818,7 +819,7 @@ void Profiler::recordExternalSample(u64 counter, int tid, int num_frames,
return;
}

_jfr.recordEvent(lock_index, tid, call_trace_id, event_type, event, counter);
_jfr.recordEvent(lock_index, tid, call_trace_id, event_type, event);

_locks[lock_index].unlock();
}
Expand Down Expand Up @@ -1064,7 +1065,13 @@ Engine *Profiler::selectWallEngine(Arguments &args) {
"wallclock profiler on your own risk.");
}
}
return (Engine *)&wall_engine;
switch (args._wallclock_sampler) {
case JVMTI:
return (Engine*)&wall_jvmti_engine;
case ASGCT:
default:
return (Engine*)&wall_asgct_engine;
}
}

Engine *Profiler::selectAllocEngine(Arguments &args) {
Expand Down
6 changes: 3 additions & 3 deletions ddprof-lib/src/main/cpp/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ class Profiler {
void switchThreadEvents(jvmtiEventMode mode);
int convertNativeTrace(int native_frames, const void **callchain,
ASGCT_CallFrame *frames);
void recordSample(void *ucontext, u64 counter, int tid, jint event_type,
void recordSample(void *ucontext, u64 weight, int tid, jint event_type,
u32 call_trace_id, Event *event);
void recordExternalSample(u64 counter, int tid, jvmtiFrameInfo *jvmti_frames,
void recordExternalSample(u64 weight, int tid, jvmtiFrameInfo *jvmti_frames,
jint num_jvmti_frames, bool truncated,
jint event_type, Event *event);
void recordExternalSample(u64 counter, int tid, int num_frames,
void recordExternalSample(u64 weight, int tid, int num_frames,
ASGCT_CallFrame *frames, bool truncated,
jint event_type, Event *event);
void recordWallClockEpoch(int tid, WallClockEpochEvent *event);
Expand Down
63 changes: 63 additions & 0 deletions ddprof-lib/src/main/cpp/reservoirSampler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024 Datadog
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef RESERVOIR_SAMPLER_H
#define RESERVOIR_SAMPLER_H

#include <math.h>
#include <random>
#include <vector>


template <class T>
class ReservoirSampler {
private:
const int size;
std::mt19937 generator;
std::uniform_real_distribution<double> uniform;
std::uniform_int_distribution<int> random_index;
std::vector<T> reservoir;

public:
ReservoirSampler(const int size) :
size(size),
generator([]() {
std::random_device rd;
std::seed_seq seed_seq{rd(), rd(), rd(), rd()};
return std::mt19937(seed_seq);
}()),
uniform(1e-16, 1.0),
random_index(0, size - 1) {
reservoir.reserve(size);
}

std::vector<T>& sample(const std::vector<T> &input) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't go into the actual algorithm we chose here and whether it was the correct one. Please tell me if I should spend more time here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am quite confident of the algorithm here.
Probably not worth spending a lot of time on it - @richardstartin has a link to an article describing this algorithm, IIRC, if you are interested.

reservoir.clear();
for (int i = 0; i < size && i < input.size(); i++) {
reservoir.push_back(input[i]);
}
double weight = exp(log(uniform(generator)) / size);
int target = size + (int) (log(uniform(generator)) / log(1 - weight));
while (target < input.size()) {
reservoir[random_index(generator)] = input[target];
weight *= exp(log(uniform(generator)) / size);
target += (int) (log(uniform(generator)) / log(1 - weight));
}
return reservoir;
}
};

#endif //RESERVOIR_SAMPLER_H
16 changes: 16 additions & 0 deletions ddprof-lib/src/main/cpp/vmStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,22 @@ class NMethod : VMStructs {
int findScopeOffset(const void *pc);
};

// Copied from JDK's globalDefinitions.hpp 'JavaThreadState' enum
enum JVMJavaThreadState {
_thread_uninitialized = 0, // should never happen (missing initialization)
_thread_new = 2, // just starting up, i.e., in process of being initialized
_thread_new_trans = 3, // corresponding transition state (not used, included for completeness)
_thread_in_native = 4, // running in native code
_thread_in_native_trans = 5, // corresponding transition state
_thread_in_vm = 6, // running in VM
_thread_in_vm_trans = 7, // corresponding transition state
_thread_in_Java = 8, // running in Java or in stub code
_thread_in_Java_trans = 9, // corresponding transition state (not used, included for completeness)
_thread_blocked = 10, // blocked in vm
_thread_blocked_trans = 11, // corresponding transition state
_thread_max_state = 12 // maximum thread state+1 - used for statistics allocation
};

class VMThread : VMStructs {
public:
static VMThread *current();
Expand Down
Loading