Skip to content
Closed
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
12 changes: 12 additions & 0 deletions ddprof-lib/src/main/cpp/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,18 @@ Error Arguments::parse(const char* args) {
}
}

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) _unknown_arg = arg;
}
Expand Down
9 changes: 8 additions & 1 deletion ddprof-lib/src/main/cpp/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ enum JfrOption {
JFR_SYNC_OPTS = NO_SYSTEM_INFO | NO_SYSTEM_PROPS | NO_NATIVE_LIBS | NO_CPU_LOAD
};

enum WallclockSampler {
ASGCT,
JVMTI
};


struct Multiplier {
char symbol;
Expand Down Expand Up @@ -134,6 +139,7 @@ class Arguments {
long _wall;
bool _wall_collapsing;
int _wall_threads_per_tick;
WallclockSampler _wallclock_sampler;
long _memory;
bool _record_allocations;
bool _record_liveness;
Expand Down Expand Up @@ -178,7 +184,8 @@ class Arguments {
_cstack(CSTACK_DEFAULT),
_jfr_options(0),
_context_attributes({}),
_lightweight(false) {
_lightweight(false),
_wallclock_sampler(ASGCT){
}

~Arguments();
Expand Down
27 changes: 26 additions & 1 deletion ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "perfEvents.h"
#include "objectSampler.h"
#include "wallClock.h"
#include "wallClock_jvmti.h"
#include "j9Ext.h"
#include "j9WallClock.h"
#include "ctimer.h"
Expand Down Expand Up @@ -60,6 +61,7 @@ static Engine noop_engine;
static PerfEvents perf_events;
static WallClock wall_engine;
static J9WallClock j9_engine;
static WallClock jvmti_wall_engine;
static ITimer itimer;
static CTimer ctimer;

Expand Down Expand Up @@ -117,6 +119,9 @@ void Profiler::onThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
}
updateThreadName(jvmti, jni, thread, true);

MutexLocker jtm(_jthread_mutex);
_jthreads.insert(std::make_pair(tid, thread));

_cpu_engine->registerThread(tid);
_wall_engine->registerThread(tid);
}
Expand All @@ -128,6 +133,9 @@ void Profiler::onThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
}
updateThreadName(jvmti, jni, thread, true);

MutexLocker jtm(_jthread_mutex);
_jthreads.erase(tid);

_cpu_engine->unregisterThread(tid);
// unregister here because JNI callers generally don't know about thread exits
_wall_engine->unregisterThread(tid);
Expand All @@ -143,6 +151,16 @@ void Profiler::unregisterThread(int tid) {
_instance->_wall_engine->unregisterThread(tid);
}

void Profiler::getThreads(const vector<int>& tids, std::vector<jthread>& threads) {
MutexLocker ml(_jthread_mutex);
for (int tid : tids) {
const auto& it = _jthreads.find(tid);
if (it != _jthreads.end()) {
threads.push_back(it->second);
}
}
}

const char* Profiler::asgctError(int code) {
switch (code) {
case ticks_no_Java_frame:
Expand Down Expand Up @@ -987,7 +1005,14 @@ Engine* Profiler::selectWallEngine(Arguments& args) {
Log::warn("Safe jmethodID access is not available on this JVM. Using wallclock profiler on your own risk.");
}
}
return (Engine*)&wall_engine;
switch (args._wallclock_sampler) {
case JVMTI:
return (Engine*)&jvmti_wall_engine;
case ASGCT:
default:
return (Engine*)&wall_engine;
}

}

Engine* Profiler::selectAllocEngine(Arguments& args) {
Expand Down
5 changes: 5 additions & 0 deletions ddprof-lib/src/main/cpp/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "vmEntry.h"
#include "objectSampler.h"
#include "thread.h"
#include <vector>

// avoid linking against newer symbols here for wide compatibility
#ifdef __GLIBC__
Expand Down Expand Up @@ -113,6 +114,9 @@ class Profiler {
int _safe_mode;
CStack _cstack;

Mutex _jthread_mutex;
map<int, jthread> _jthreads;

volatile jvmtiEventMode _thread_events_state;

SpinLock _stubs_lock;
Expand Down Expand Up @@ -272,6 +276,7 @@ class Profiler {

static int registerThread(int tid);
static void unregisterThread(int tid);
void getThreads(const vector<int>& tids, std::vector<jthread>& threads);

// CompiledMethodLoad is also needed to enable DebugNonSafepoints info by default
static void JNICALL CompiledMethodLoad(jvmtiEnv* jvmti, jmethodID method,
Expand Down
59 changes: 59 additions & 0 deletions ddprof-lib/src/main/cpp/reservoir_sampler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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())()),
uniform(1e-16, 1.0),
random_index(0, size - 1) {
reservoir.reserve(size);
}

std::vector<T>& sample(const std::vector<T> &input) {
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
28 changes: 28 additions & 0 deletions ddprof-lib/src/main/cpp/threadState.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,30 @@ enum class ThreadState : int {
SYSCALL = 9 // does not originate in the JVM, used when the current frame is known to be a syscall
};

static ThreadState convertJvmtiThreadState(int state) {
switch (state & 0xFFFF) {
case JVMTI_THREAD_STATE_ALIVE:
return ThreadState::NEW;
case JVMTI_THREAD_STATE_TERMINATED:
return ThreadState::TERMINATED;
case JVMTI_THREAD_STATE_RUNNABLE:
return ThreadState::RUNNABLE;
case JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER:
return ThreadState::MONITOR_WAIT;
case JVMTI_THREAD_STATE_WAITING:
case JVMTI_THREAD_STATE_WAITING_INDEFINITELY:
case JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT:
case JVMTI_THREAD_STATE_PARKED:
return ThreadState::CONDVAR_WAIT;
case JVMTI_THREAD_STATE_SLEEPING:
return ThreadState::SLEEPING;
case JVMTI_THREAD_STATE_IN_OBJECT_WAIT:
return ThreadState::OBJECT_WAIT;
default:
return ThreadState::UNKNOWN;
}
}


enum class ExecutionMode : int {
UNKNOWN = 0,
Expand Down Expand Up @@ -45,4 +69,8 @@ static ExecutionMode convertJvmExecutionState(int state) {
}
}

static ExecutionMode getExecutionModeFromJvmtiThreadState(int state) {
return (state & 0x400000) == 0x400000 ? ExecutionMode::NATIVE : ExecutionMode::JAVA;
}

#endif //JAVA_PROFILER_LIBRARY_THREAD_STATE_H
20 changes: 5 additions & 15 deletions ddprof-lib/src/main/cpp/wallClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "thread.h"
#include "tsc.h"
#include "vmStructs.h"
#include "reservoir_sampler.h"

std::atomic<bool> WallClock::_enabled{false};

Expand Down Expand Up @@ -139,10 +140,9 @@ void WallClock::timerLoop() {
if (!_enabled.load(std::memory_order_acquire)) {
return;
}
ReservoirSampler<int> reservoir(_reservoir_size);
std::vector<int> tids;
tids.reserve(_reservoir_size);
std::vector<int> reservoir;
reservoir.reserve(_reservoir_size);
int self = OS::threadId();
ThreadFilter* thread_filter = Profiler::instance()->threadFilter();
thread_filter->remove(self);
Expand All @@ -168,21 +168,12 @@ void WallClock::timerLoop() {
}
delete thread_list;
}
for (int i = 0; i < _reservoir_size && i < tids.size(); i++) {
reservoir.push_back(tids[i]);
}
double weight = exp(log(uniform(generator)) / _reservoir_size);
int target = _reservoir_size + (int) (log(uniform(generator)) / log(1 - weight));
while (target < tids.size()) {
reservoir[random_index(generator)] = tids[target];
weight *= exp(log(uniform(generator)) / _reservoir_size);
target += (int) (log(uniform(generator)) / log(1 - weight));
}

int num_failures = 0;
int threads_already_exited = 0;
int permission_denied = 0;
for (int tid : reservoir) {
std::vector<int> sample = reservoir.sample(tids);
for (int tid : sample) {
if (!OS::sendSignalToThread(tid, SIGVTALRM)) {
num_failures++;
if (errno != 0) {
Expand All @@ -202,7 +193,7 @@ void WallClock::timerLoop() {

epoch.updateNumSamplableThreads(tids.size());
epoch.updateNumFailedSamples(num_failures);
epoch.updateNumSuccessfulSamples(reservoir.size() - num_failures);
epoch.updateNumSuccessfulSamples(sample.size() - num_failures);
epoch.updateNumExitedThreads(threads_already_exited);
epoch.updateNumPermissionDenied(permission_denied);
u64 endTime = TSC::ticks();
Expand All @@ -216,7 +207,6 @@ void WallClock::timerLoop() {
epoch.clean();
}

reservoir.clear();
tids.clear();
OS::sleep(_interval);
}
Expand Down
2 changes: 1 addition & 1 deletion ddprof-lib/src/main/cpp/wallClock.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class WallClock : public Engine {
}

const char* name() {
return "WallClock";
return "WallClock - AsyncGetCallTrace";
}

long interval() const {
Expand Down
Loading