-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathwallClock.cpp
More file actions
199 lines (177 loc) · 6.16 KB
/
Copy pathwallClock.cpp
File metadata and controls
199 lines (177 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Copyright The async-profiler authors
* Copyright 2025, Datadog, Inc.
* SPDX-License-Identifier: Apache-2.0
*/
#include "wallClock.h"
#include "stackFrame.h"
#include "context.h"
#include "debugSupport.h"
#include "libraries.h"
#include "log.h"
#include "profiler.h"
#include "stackFrame.h"
#include "thread.h"
#include "threadState.inline.h"
#include "vmStructs.h"
#include "guards.h"
#include <math.h>
#include <random>
#include <algorithm> // For std::sort and std::binary_search
std::atomic<bool> BaseWallClock::_enabled{false};
bool WallClockASGCT::inSyscall(void *ucontext) {
StackFrame frame(ucontext);
uintptr_t pc = frame.pc();
// Consider a thread sleeping, if it has been interrupted in the middle of
// syscall execution, either when PC points to the syscall instruction, or if
// syscall has just returned with EINTR
if (StackFrame::isSyscall((instruction_t *)pc)) {
return true;
}
// Make sure the previous instruction address is readable
uintptr_t prev_pc = pc - SYSCALL_SIZE;
if ((pc & 0xfff) >= SYSCALL_SIZE ||
Libraries::instance()->findLibraryByAddress((instruction_t *)prev_pc) !=
NULL) {
if (StackFrame::isSyscall((instruction_t *)prev_pc) &&
frame.checkInterruptedSyscall()) {
return true;
}
}
return false;
}
void WallClockASGCT::sharedSignalHandler(int signo, siginfo_t *siginfo,
void *ucontext) {
WallClockASGCT *engine = reinterpret_cast<WallClockASGCT *>(Profiler::instance()->wallEngine());
if (signo == SIGVTALRM) {
engine->signalHandler(signo, siginfo, ucontext, engine->_interval);
}
}
void WallClockASGCT::signalHandler(int signo, siginfo_t *siginfo, void *ucontext,
u64 last_sample) {
// Atomically try to enter critical section - prevents all reentrancy races
CriticalSection cs;
if (!cs.entered()) {
return; // Another critical section is active, defer profiling
}
ProfiledThread *current = ProfiledThread::currentSignalSafe();
int tid = current != NULL ? current->tid() : OS::threadId();
Shims::instance().setSighandlerTid(tid);
u64 call_trace_id = 0;
if (current != NULL && _collapsing) {
StackFrame frame(ucontext);
Context &context = Contexts::get();
call_trace_id = current->lookupWallclockCallTraceId(
(u64)frame.pc(), (u64)frame.sp(),
Profiler::instance()->recordingEpoch(),
context.spanId, context.rootSpanId);
if (call_trace_id != 0) {
Counters::increment(SKIPPED_WALLCLOCK_UNWINDS);
}
}
ExecutionEvent event;
VMThread *vm_thread = VMThread::current();
if (vm_thread != NULL && !vm_thread->isThreadAccessible()) {
vm_thread = NULL;
}
int raw_thread_state = vm_thread ? vm_thread->state() : 0;
bool is_java_thread = raw_thread_state >= 4 && raw_thread_state < 12;
bool is_initialized = is_java_thread;
OSThreadState state = OSThreadState::UNKNOWN;
ExecutionMode mode = ExecutionMode::UNKNOWN;
if (vm_thread && is_initialized) {
OSThreadState os_state = vm_thread->osThreadState();
if (os_state != OSThreadState::UNKNOWN) {
state = os_state;
}
mode = getThreadExecutionMode();
}
if (state == OSThreadState::UNKNOWN) {
if (inSyscall(ucontext)) {
state = OSThreadState::SYSCALL;
mode = ExecutionMode::SYSCALL;
} else {
state = OSThreadState::RUNNABLE;
}
}
event._thread_state = state;
event._execution_mode = mode;
event._weight = 1;
Profiler::instance()->recordSample(ucontext, last_sample, tid, BCI_WALL,
call_trace_id, &event);
Shims::instance().setSighandlerTid(-1);
}
Error BaseWallClock::start(Arguments &args) {
int interval = args._event != NULL ? args._interval : args._wall;
if (interval < 0) {
return Error("interval must be positive");
}
_interval = interval ? interval : DEFAULT_WALL_INTERVAL;
_reservoir_size =
args._wall_threads_per_tick ?
args._wall_threads_per_tick
: DEFAULT_WALL_THREADS_PER_TICK;
initialize(args);
_running = true;
if (pthread_create(&_thread, NULL, threadEntry, this) != 0) {
return Error("Unable to create timer thread");
}
return Error::OK;
}
void BaseWallClock::stop() {
_running.store(false);
// the thread join ensures we wait for the thread to finish before returning
// (and possibly removing the object)
pthread_kill(_thread, WAKEUP_SIGNAL);
int res = pthread_join(_thread, NULL);
if (res != 0) {
Log::warn("Unable to join WallClock thread on stop %d", res);
}
}
bool BaseWallClock::isEnabled() const {
return _enabled.load(std::memory_order_acquire);
}
void WallClockASGCT::initialize(Arguments& args) {
_collapsing = args._wall_collapsing;
OS::installSignalHandler(SIGVTALRM, sharedSignalHandler);
}
void WallClockASGCT::timerLoop() {
// todo: re-allocating the vector every time is not efficient
auto collectThreads = [&](std::vector<int>& tids) {
// Get thread IDs from the filter if it's enabled
// Otherwise list all threads in the system
if (Profiler::instance()->threadFilter()->enabled()) {
Profiler::instance()->threadFilter()->collect(tids);
} else {
ThreadList *thread_list = OS::listThreads();
while (thread_list->hasNext()) {
int tid = thread_list->next();
// Don't include the current thread
if (tid != OS::threadId()) {
tids.push_back(tid);
}
tid = thread_list->next();
}
delete thread_list;
}
};
auto sampleThreads = [&](int tid, int& num_failures, int& threads_already_exited, int& permission_denied) {
if (!OS::sendSignalToThread(tid, SIGVTALRM)) {
num_failures++;
if (errno != 0) {
if (errno == ESRCH) {
threads_already_exited++;
} else if (errno == EPERM) {
permission_denied++;
} else {
Log::debug("unexpected error %s", strerror(errno));
}
}
return false;
}
return true;
};
auto doNothing = []() {
};
timerLoopCommon<int>(collectThreads, sampleThreads, doNothing, _reservoir_size, _interval);
}