Skip to content

Commit 15603bc

Browse files
authored
[PROF-12714] Add CriticalNative support (#282)
1 parent 0a7db6a commit 15603bc

File tree

4 files changed

+52
-24
lines changed

4 files changed

+52
-24
lines changed

ddprof-lib/src/main/cpp/javaApi.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Java_com_datadoghq_profiler_JavaProfiler_stop0(JNIEnv *env, jobject unused) {
7878
}
7979

8080
extern "C" DLLEXPORT jint JNICALL
81-
Java_com_datadoghq_profiler_JavaProfiler_getTid0(JNIEnv *env, jobject unused) {
81+
Java_com_datadoghq_profiler_JavaProfiler_getTid0(JNIEnv *env, jclass unused) {
8282
return OS::threadId();
8383
}
8484

@@ -113,22 +113,26 @@ Java_com_datadoghq_profiler_JavaProfiler_execute0(JNIEnv *env, jobject unused,
113113

114114
extern "C" DLLEXPORT jstring JNICALL
115115
Java_com_datadoghq_profiler_JavaProfiler_getStatus0(JNIEnv* env,
116-
jobject unused) {
116+
jclass unused) {
117117
char msg[2048];
118118
int ret = Profiler::instance()->status((char*)msg, sizeof(msg) - 1);
119119
return env->NewStringUTF(msg);
120120
}
121121

122122
extern "C" DLLEXPORT jlong JNICALL
123123
Java_com_datadoghq_profiler_JavaProfiler_getSamples(JNIEnv *env,
124-
jobject unused) {
124+
jclass unused) {
125125
return (jlong)Profiler::instance()->total_samples();
126126
}
127127

128128
// some duplication between add and remove, though we want to avoid having an extra branch in the hot path
129+
130+
// JavaCritical is faster JNI, but more restrictive - parameters and return value have to be
131+
// primitives or arrays of primitive types.
132+
// We direct corresponding JNI calls to JavaCritical to make sure the parameters/return value
133+
// still compatible in the event of signature changes in the future.
129134
extern "C" DLLEXPORT void JNICALL
130-
Java_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0(JNIEnv *env,
131-
jobject unused) {
135+
JavaCritical_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0() {
132136
ProfiledThread *current = ProfiledThread::current();
133137
if (unlikely(current == nullptr)) {
134138
return;
@@ -157,8 +161,7 @@ Java_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0(JNIEnv *env,
157161
}
158162

159163
extern "C" DLLEXPORT void JNICALL
160-
Java_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0(JNIEnv *env,
161-
jobject unused) {
164+
JavaCritical_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0() {
162165
ProfiledThread *current = ProfiledThread::current();
163166
if (unlikely(current == nullptr)) {
164167
return;
@@ -171,7 +174,7 @@ Java_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0(JNIEnv *env,
171174
if (unlikely(!thread_filter->enabled())) {
172175
return;
173176
}
174-
177+
175178
int slot_id = current->filterSlotId();
176179
if (unlikely(slot_id == -1)) {
177180
// Thread doesn't have a slot ID yet - nothing to remove
@@ -180,9 +183,22 @@ Java_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0(JNIEnv *env,
180183
thread_filter->remove(slot_id);
181184
}
182185

186+
187+
extern "C" DLLEXPORT void JNICALL
188+
Java_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0(JNIEnv *env,
189+
jclass unused) {
190+
JavaCritical_com_datadoghq_profiler_JavaProfiler_filterThreadAdd0();
191+
}
192+
193+
extern "C" DLLEXPORT void JNICALL
194+
Java_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0(JNIEnv *env,
195+
jclass unused) {
196+
JavaCritical_com_datadoghq_profiler_JavaProfiler_filterThreadRemove0();
197+
}
198+
183199
extern "C" DLLEXPORT jobject JNICALL
184200
Java_com_datadoghq_profiler_JavaProfiler_getContextPage0(JNIEnv *env,
185-
jobject unused,
201+
jclass unused,
186202
jint tid) {
187203
ContextPage page = Contexts::getPage((int)tid);
188204
if (page.storage == 0) {
@@ -193,21 +209,21 @@ Java_com_datadoghq_profiler_JavaProfiler_getContextPage0(JNIEnv *env,
193209

194210
extern "C" DLLEXPORT jlong JNICALL
195211
Java_com_datadoghq_profiler_JavaProfiler_getContextPageOffset0(JNIEnv *env,
196-
jobject unused,
212+
jclass unused,
197213
jint tid) {
198214
ContextPage page = Contexts::getPage((int)tid);
199215
return (jlong)page.storage;
200216
}
201217

202218
extern "C" DLLEXPORT jint JNICALL
203219
Java_com_datadoghq_profiler_JavaProfiler_getMaxContextPages0(JNIEnv *env,
204-
jobject unused) {
220+
jclass unused) {
205221
return (jint)Contexts::getMaxPages();
206222
}
207223

208224
extern "C" DLLEXPORT jboolean JNICALL
209225
Java_com_datadoghq_profiler_JavaProfiler_recordTrace0(
210-
JNIEnv *env, jobject unused, jlong rootSpanId, jstring endpoint,
226+
JNIEnv *env, jclass unused, jlong rootSpanId, jstring endpoint,
211227
jstring operation, jint sizeLimit) {
212228
JniString endpoint_str(env, endpoint);
213229
u32 endpointLabel = Profiler::instance()->stringLabelMap()->bounded_lookup(
@@ -229,7 +245,7 @@ Java_com_datadoghq_profiler_JavaProfiler_recordTrace0(
229245

230246
extern "C" DLLEXPORT jint JNICALL
231247
Java_com_datadoghq_profiler_JavaProfiler_registerConstant0(JNIEnv *env,
232-
jobject unused,
248+
jclass unused,
233249
jstring value) {
234250
JniString value_str(env, value);
235251
u32 encoding = Profiler::instance()->contextValueMap()->bounded_lookup(
@@ -238,15 +254,15 @@ Java_com_datadoghq_profiler_JavaProfiler_registerConstant0(JNIEnv *env,
238254
}
239255

240256
extern "C" DLLEXPORT void JNICALL
241-
Java_com_datadoghq_profiler_JavaProfiler_dump0(JNIEnv *env, jobject unused,
257+
Java_com_datadoghq_profiler_JavaProfiler_dump0(JNIEnv *env, jclass unused,
242258
jstring path) {
243259
JniString path_str(env, path);
244260
Profiler::instance()->dump(path_str.c_str(), path_str.length());
245261
}
246262

247263
extern "C" DLLEXPORT jobject JNICALL
248264
Java_com_datadoghq_profiler_JavaProfiler_getDebugCounters0(JNIEnv *env,
249-
jobject unused) {
265+
jclass unused) {
250266
#ifdef COUNTERS
251267
return env->NewDirectByteBuffer((void *)Counters::getCounters(),
252268
(jlong)Counters::size());
@@ -257,7 +273,7 @@ Java_com_datadoghq_profiler_JavaProfiler_getDebugCounters0(JNIEnv *env,
257273

258274
extern "C" DLLEXPORT jobjectArray JNICALL
259275
Java_com_datadoghq_profiler_JavaProfiler_describeDebugCounters0(
260-
JNIEnv *env, jobject unused) {
276+
JNIEnv *env, jclass unused) {
261277
#ifdef COUNTERS
262278
std::vector<const char *> counter_names = Counters::describeCounters();
263279
jobjectArray array = (jobjectArray)env->NewObjectArray(
@@ -275,7 +291,7 @@ Java_com_datadoghq_profiler_JavaProfiler_describeDebugCounters0(
275291

276292
extern "C" DLLEXPORT void JNICALL
277293
Java_com_datadoghq_profiler_JavaProfiler_recordSettingEvent0(
278-
JNIEnv *env, jobject unused, jstring name, jstring value, jstring unit) {
294+
JNIEnv *env, jclass unused, jstring name, jstring value, jstring unit) {
279295
int tid = ProfiledThread::currentTid();
280296
if (tid < 0) {
281297
return;
@@ -298,7 +314,7 @@ static int dictionarizeClassName(JNIEnv* env, jstring className) {
298314

299315
extern "C" DLLEXPORT void JNICALL
300316
Java_com_datadoghq_profiler_JavaProfiler_recordQueueEnd0(
301-
JNIEnv *env, jobject unused, jlong startTime, jlong endTime, jstring task,
317+
JNIEnv *env, jclass unused, jlong startTime, jlong endTime, jstring task,
302318
jstring scheduler, jthread origin, jstring queueType, jint queueLength) {
303319
int tid = ProfiledThread::currentTid();
304320
if (tid < 0) {
@@ -334,19 +350,19 @@ Java_com_datadoghq_profiler_JavaProfiler_recordQueueEnd0(
334350

335351
extern "C" DLLEXPORT jlong JNICALL
336352
Java_com_datadoghq_profiler_JavaProfiler_currentTicks0(JNIEnv *env,
337-
jobject unused) {
353+
jclass unused) {
338354
return TSC::ticks();
339355
}
340356

341357
extern "C" DLLEXPORT jlong JNICALL
342358
Java_com_datadoghq_profiler_JavaProfiler_tscFrequency0(JNIEnv *env,
343-
jobject unused) {
359+
jclass unused) {
344360
return TSC::frequency();
345361
}
346362

347363
extern "C" DLLEXPORT void JNICALL
348364
Java_com_datadoghq_profiler_JavaProfiler_mallocArenaMax0(JNIEnv *env,
349-
jobject unused,
365+
jclass unused,
350366
jint maxArenas) {
351367
ddprof::OS::mallocArenaMax(maxArenas);
352368
}

ddprof-lib/src/main/cpp/vmStructs_dd.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ namespace ddprof {
4444
initOffsets();
4545
initJvmFunctions();
4646
initUnsafeFunctions();
47+
initCriticalJNINatives();
4748
}
4849

4950
void VMStructs_::initOffsets() {
@@ -98,6 +99,16 @@ namespace ddprof {
9899
}
99100
}
100101

102+
void VMStructs_::initCriticalJNINatives() {
103+
#ifdef __aarch64__
104+
// aarch64 does not support CriticalJNINatives
105+
JVMFlag* flag = JVMFlag::find("CriticalJNINatives", {JVMFlag::Type::Bool});
106+
if (flag != nullptr && flag->get()) {
107+
flag->set(0);
108+
}
109+
#endif // __aarch64__
110+
}
111+
101112
const void *VMStructs_::findHeapUsageFunc() {
102113
if (VM::hotspot_version() < 17) {
103114
// For JDK 11 it is really unreliable to find the memory_usage function -

ddprof-lib/src/main/cpp/vmStructs_dd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace ddprof {
5252
static void initOffsets();
5353
static void initJvmFunctions();
5454
static void initUnsafeFunctions();
55+
static void initCriticalJNINatives();
5556

5657
static void checkNativeBinding(jvmtiEnv *jvmti, JNIEnv *jni, jmethodID method,
5758
void *address);

ddprof-lib/src/main/java/com/datadoghq/profiler/JavaProfiler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void stop() throws IllegalStateException {
148148
*
149149
* @return Number of samples
150150
*/
151-
public native long getSamples();
151+
public static native long getSamples();
152152

153153
/**
154154
* Get profiler agent version, e.g. "1.0"
@@ -402,8 +402,8 @@ public Map<String, Long> getDebugCounters() {
402402
private native void stop0() throws IllegalStateException;
403403
private native String execute0(String command) throws IllegalArgumentException, IllegalStateException, IOException;
404404

405-
private native void filterThreadAdd0();
406-
private native void filterThreadRemove0();
405+
private static native void filterThreadAdd0();
406+
private static native void filterThreadRemove0();
407407

408408
private static native int getTid0();
409409
private static native ByteBuffer getContextPage0(int tid);

0 commit comments

Comments
 (0)