-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathwallClock.h
More file actions
176 lines (147 loc) · 5.46 KB
/
Copy pathwallClock.h
File metadata and controls
176 lines (147 loc) · 5.46 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
/*
* Copyright 2018 Andrei Pangin
*
* 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 _WALLCLOCK_H
#define _WALLCLOCK_H
#include "engine.h"
#include "os.h"
#include "profiler.h"
#include "reservoirSampler.h"
#include "threadFilter.h"
#include "threadState.h"
#include "tsc.h"
#include "vmStructs_dd.h"
class BaseWallClock : public Engine {
private:
static std::atomic<bool> _enabled;
std::atomic<bool> _running;
protected:
long _interval;
// Maximum number of threads sampled in one iteration. This limit serves as a
// throttle when generating profiling signals. Otherwise applications with too
// many threads may suffer from a big profiling overhead. Also, keeping this
// limit low enough helps to avoid contention on a spin lock inside
// Profiler::recordSample().
int _reservoir_size;
pthread_t _thread;
virtual void timerLoop() = 0;
virtual void initialize(Arguments& args) {};
static void *threadEntry(void *wall_clock) {
((BaseWallClock *)wall_clock)->timerLoop();
return NULL;
}
bool isEnabled() const;
template <typename ThreadType, typename CollectThreadsFunc, typename SampleThreadsFunc, typename CleanThreadFunc>
void timerLoopCommon(CollectThreadsFunc collectThreads, SampleThreadsFunc sampleThreads, CleanThreadFunc cleanThreads, int reservoirSize, u64 interval) {
if (!_enabled.load(std::memory_order_acquire)) {
return;
}
// Dither the sampling interval to introduce some randomness and prevent step-locking
const double stddev = ((double)_interval) / 10.0; // 10% standard deviation
// Set up random engine and normal distribution
std::random_device rd;
std::mt19937 generator(rd());
std::normal_distribution<double> distribution(interval, stddev);
std::vector<ThreadType> threads;
threads.reserve(reservoirSize);
int self = OS::threadId();
ThreadFilter* thread_filter = Profiler::instance()->threadFilter();
thread_filter->remove(self);
u64 startTime = TSC::ticks();
WallClockEpochEvent epoch(startTime);
ReservoirSampler<ThreadType> reservoir(reservoirSize);
while (_running.load(std::memory_order_relaxed)) {
collectThreads(threads);
int num_failures = 0;
int threads_already_exited = 0;
int permission_denied = 0;
std::vector<ThreadType> sample = reservoir.sample(threads);
for (ThreadType thread : sample) {
if (!sampleThreads(thread, num_failures, threads_already_exited, permission_denied)) {
continue;
}
}
epoch.updateNumSamplableThreads(threads.size());
epoch.updateNumFailedSamples(num_failures);
epoch.updateNumSuccessfulSamples(sample.size() - num_failures);
epoch.updateNumExitedThreads(threads_already_exited);
epoch.updateNumPermissionDenied(permission_denied);
u64 endTime = TSC::ticks();
u64 duration = TSC::ticks_to_millis(endTime - startTime);
if (epoch.hasChanged() || duration >= 1000) {
epoch.endEpoch(duration);
Profiler::instance()->recordWallClockEpoch(self, &epoch);
epoch.newEpoch(endTime);
startTime = endTime;
} else {
epoch.clean();
}
for (ThreadType thread : threads) {
cleanThreads(thread);
}
threads.clear();
// Get a random sleep duration
// clamp the random interval to <1,2N-1>
// the probability of clamping is extremely small, close to zero
OS::sleep(std::min(std::max((long int)1, static_cast<long int>(distribution(generator))), ((_interval * 2) - 1)));
}
}
public:
BaseWallClock() :
_interval(LONG_MAX),
_reservoir_size(0),
_running(false),
_thread(0) {}
virtual ~BaseWallClock() = default;
const char* units() {
return "ns";
}
virtual const char* name() = 0;
long interval() const { return _interval; }
inline void enableEvents(bool enabled) {
_enabled.store(enabled, std::memory_order_release);
}
Error start(Arguments& args);
void stop();
};
class WallClockASGCT : public BaseWallClock {
private:
bool _collapsing;
static bool inSyscall(void* ucontext);
static void sharedSignalHandler(int signo, siginfo_t* siginfo, void* ucontext);
void signalHandler(int signo, siginfo_t* siginfo, void* ucontext, u64 last_sample);
void initialize(Arguments& args) override;
void timerLoop() override;
public:
WallClockASGCT() : BaseWallClock(), _collapsing(false) {}
const char* name() override {
return "WallClock (ASGCT)";
}
};
class WallClockJVMTI : public BaseWallClock {
private:
void timerLoop() override;
public:
struct ThreadEntry {
ddprof::VMThread* native;
jthread java;
int tid;
};
WallClockJVMTI() : BaseWallClock() {}
const char* name() override {
return "WallClock (JVMTI)";
}
};
#endif // _WALLCLOCK_H