forked from stackimpact/stackimpact-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_profiler.cpp
More file actions
114 lines (75 loc) · 3.09 KB
/
lock_profiler.cpp
File metadata and controls
114 lines (75 loc) · 3.09 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
#include <signal.h>
#include <sys/time.h>
#include <string>
#include <sstream>
#include <iostream>
#include "lock_profiler.h"
using namespace std;
bool LockProfiler::SetupProfiler() {
return true;
}
void LockProfiler::DestroyProfiler() {
}
void LockProfiler::StartProfiler() {
Agent* agent = &Agent::instance;
profile_recorder->Reset();
num_samples = 0;
agent->jvmti->GetTime(&start_ts);
agent->jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL);
agent->jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL);
}
void LockProfiler::StopProfiler() {
Agent* agent = &Agent::instance;
agent->jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL);
agent->jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL);
}
void LockProfiler::RecordLock(long enter_ts, long wait_time) {
// only sample waits initiated during current profiling cycle
if (enter_ts < start_ts) {
return;
}
// ensure sample limit
if (num_samples > max_samples) {
return;
}
num_samples++;
// only sample lock waits above sampling rate or close to it
if (wait_time < sampling_rate * 1000 && wait_time > fastrand() % sampling_rate) {
return;
}
profile_recorder->RecordSample((double)wait_time / 1000000, NULL);
}
void JNICALL LockProfiler::MonitorContendedEnter(jvmtiEnv* jvmti, JNIEnv* env, jthread thread, jobject object) {
jlong enter_ts;
jvmti->GetTime(&enter_ts);
jvmti->SetTag(object, enter_ts);
}
void JNICALL LockProfiler::MonitorContendedEntered(jvmtiEnv* jvmti, JNIEnv* env, jthread thread, jobject object) {
jlong entered_ts;
jvmti->GetTime(&entered_ts);
jlong enter_ts;
jvmti->GetTag(object, &enter_ts);
if (enter_ts > 0) {
Agent* agent = &Agent::instance;
agent->lock_profiler->RecordLock(enter_ts, entered_ts - enter_ts);
}
}
extern "C" JNIEXPORT jboolean JNICALL Java_com_stackimpact_agent_profilers_LockProfiler_setupLockProfiler(JNIEnv* env, jobject unused, jlong sampling_rate, jlong max_samples) {
Agent* agent = &Agent::instance;
agent->lock_profiler->sampling_rate = (long)sampling_rate;
agent->lock_profiler->max_samples = (long)max_samples;
return agent->lock_profiler->SetupProfiler();
}
extern "C" JNIEXPORT void JNICALL Java_com_stackimpact_agent_profilers_LockProfiler_destroyLockProfiler(JNIEnv* env, jobject unused) {
Agent* agent = &Agent::instance;
agent->lock_profiler->DestroyProfiler();
}
extern "C" JNIEXPORT void JNICALL Java_com_stackimpact_agent_profilers_LockProfiler_startLockProfiler(JNIEnv* env, jobject unused) {
Agent* agent = &Agent::instance;
agent->lock_profiler->StartProfiler();
}
extern "C" JNIEXPORT jobjectArray JNICALL Java_com_stackimpact_agent_profilers_LockProfiler_stopLockProfiler(JNIEnv* env, jobject unused) {
Agent* agent = &Agent::instance;
agent->lock_profiler->StopProfiler();
return agent->lock_profiler->profile_recorder->ExportProfile(env);
}