GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — async-profiler/src/objectSampler.cpp at v4.2 · async-profiler/async-profiler

Expand file tree

/

objectSampler.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
193 lines (154 loc) · 5.39 KB
/

objectSampler.cpp

Copy path

File metadata and controls

193 lines (154 loc) · 5.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include "objectSampler.h"
#include "profiler.h"
#include "tsc.h"
u64 ObjectSampler::_interval;
bool ObjectSampler::_live;
volatile u64 ObjectSampler::_allocated_bytes;
static u32 lookupClassId(jvmtiEnv* jvmti, jclass cls) {
u32 class_id = 0;
char* class_name;
if (jvmti->GetClassSignature(cls, &class_name, NULL) == 0) {
if (class_name[0] == 'L') {
class_id = Profiler::instance()->classMap()->lookup(class_name + 1, strlen(class_name) - 2);
} else {
class_id = Profiler::instance()->classMap()->lookup(class_name);
}
jvmti->Deallocate((unsigned char*)class_name);
}
return class_id;
}
class LiveRefs {
private:
enum { MAX_REFS = 1024 };
SpinLock _lock;
jweak _refs[MAX_REFS];
struct {
jlong size;
u64 trace;
u64 time;
} _values[MAX_REFS];
bool _full;
static inline bool collected(jweak w) {
return *(void**)((uintptr_t)w & ~(uintptr_t)1) == NULL;
}
public:
LiveRefs() : _lock(1) {
}
void init() {
memset(_refs, 0, sizeof(_refs));
memset(_values, 0, sizeof(_values));
_full = false;
_lock.unlock();
}
void gc() {
_full = false;
}
void add(JNIEnv* jni, jobject object, jlong size, u64 trace) {
if (_full) {
return;
}
jweak wobject = jni->NewWeakGlobalRef(object);
if (wobject == NULL) {
return;
}
if (_lock.tryLock()) {
u32 start = (((uintptr_t)object >> 4) * 31 + ((uintptr_t)jni >> 4) + trace) & (MAX_REFS - 1);
u32 i = start;
do {
jweak w = _refs[i];
if (w == NULL || collected(w)) {
if (w != NULL) jni->DeleteWeakGlobalRef(w);
_refs[i] = wobject;
_values[i].size = size;
_values[i].trace = trace;
_values[i].time = TSC::ticks();
_lock.unlock();
return;
}
} while ((i = (i + 1) & (MAX_REFS - 1)) != start);
_full = true;
_lock.unlock();
}
jni->DeleteWeakGlobalRef(wobject);
}
void dump(JNIEnv* jni) {
_lock.lock();
jvmtiEnv* jvmti = VM::jvmti();
Profiler* profiler = Profiler::instance();
// Reset counters before dumping to collect live objects only.
profiler->tryResetCounters();
for (u32 i = 0; i < MAX_REFS; i++) {
if ((i % 32) == 0) jni->PushLocalFrame(64);
jweak w = _refs[i];
if (w != NULL) {
jobject obj = jni->NewLocalRef(w);
if (obj != NULL) {
LiveObject event;
event._start_time = TSC::ticks();
event._alloc_size = _values[i].size;
event._alloc_time = _values[i].time;
event._class_id = lookupClassId(jvmti, jni->GetObjectClass(obj));
int tid = _values[i].trace >> 32;
u32 call_trace_id = (u32)_values[i].trace;
profiler->recordExternalSamples(1, event._alloc_size, tid, call_trace_id, LIVE_OBJECT, &event);
}
jni->DeleteWeakGlobalRef(w);
}
if ((i % 32) == 31 || i == MAX_REFS - 1) jni->PopLocalFrame(NULL);
}
}
};
static LiveRefs live_refs;
void ObjectSampler::SampledObjectAlloc(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread,
jobject object, jclass object_klass, jlong size) {
if (_enabled) {
recordAllocation(jvmti, jni, ALLOC_SAMPLE, object, object_klass, size);
}
}
void ObjectSampler::GarbageCollectionStart(jvmtiEnv* jvmti) {
live_refs.gc();
}
void ObjectSampler::recordAllocation(jvmtiEnv* jvmti, JNIEnv* jni, EventType event_type,
jobject object, jclass object_klass, jlong size) {
AllocEvent event;
event._start_time = TSC::ticks();
event._total_size = size > _interval ? size : _interval;
event._instance_size = size;
event._class_id = lookupClassId(jvmti, object_klass);
u64 trace = Profiler::instance()->recordSample(NULL, event._total_size, event_type, &event);
if (_live && trace != 0) {
live_refs.add(jni, object, size, trace);
}
}
void ObjectSampler::initLiveRefs(bool live) {
_live = live;
if (_live) {
live_refs.init();
}
}
void ObjectSampler::dumpLiveRefs() {
if (_live) {
live_refs.dump(VM::jni());
}
}
Error ObjectSampler::start(Arguments& args) {
_interval = args._alloc > 0 ? args._alloc : DEFAULT_ALLOC_INTERVAL;
initLiveRefs(args._live);
jvmtiEnv* jvmti = VM::jvmti();
jvmti->SetHeapSamplingInterval(_interval);
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL);
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
return Error::OK;
}
void ObjectSampler::stop() {
jvmtiEnv* jvmti = VM::jvmti();
jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL);
VM::releaseSampleObjectsCapability();
dumpLiveRefs();
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.