-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjavaApi.cpp
371 lines (335 loc) · 12.7 KB
/
javaApi.cpp
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright 2016 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.
*/
#include <assert.h>
#include "context.h"
#include "counters.h"
#include "engine.h"
#include "incbin.h"
#include "os.h"
#include "profiler.h"
#include "thread.h"
#include "tsc.h"
#include "vmEntry.h"
#include "vmStructs.h"
#include "wallClock.h"
#include <errno.h>
#include <fstream>
#include <sstream>
#include <string.h>
static void throwNew(JNIEnv *env, const char *exception_class,
const char *message) {
jclass cls = env->FindClass(exception_class);
if (cls != NULL) {
env->ThrowNew(cls, message);
}
}
class JniString {
private:
JNIEnv *_env;
const char *_c_string;
jstring _java_string;
int _length;
public:
JniString(JNIEnv *env, jstring java_string) {
_env = env;
_c_string = _env->GetStringUTFChars(java_string, NULL);
_length = _env->GetStringUTFLength(java_string);
_java_string = java_string;
}
JniString(JniString &jniString) = delete;
~JniString() { _env->ReleaseStringUTFChars(_java_string, _c_string); }
const char *c_str() const { return _c_string; }
int length() const { return _length; }
};
extern "C" DLLEXPORT jboolean JNICALL
Java_com_datadoghq_profiler_JavaProfiler_init0(JNIEnv *env, jclass unused) {
// JavaVM* has already been stored when the native library was loaded so we can pass nullptr here
return VM::initProfilerBridge(nullptr, true);
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JavaProfiler_stop0(JNIEnv *env, jobject unused) {
Error error = Profiler::instance()->stop();
if (error) {
throwNew(env, "java/lang/IllegalStateException", error.message());
}
}
extern "C" DLLEXPORT jint JNICALL
Java_com_datadoghq_profiler_JavaProfiler_getTid0(JNIEnv *env, jobject unused) {
return OS::threadId();
}
extern "C" DLLEXPORT jstring JNICALL
Java_com_datadoghq_profiler_JavaProfiler_execute0(JNIEnv *env, jobject unused,
jstring command) {
Arguments args;
JniString command_str(env, command);
Error error = args.parse(command_str.c_str());
if (error) {
throwNew(env, "java/lang/IllegalArgumentException", error.message());
return NULL;
}
Log::open(args);
std::ostringstream out;
error = Profiler::instance()->runInternal(args, out);
if (!error) {
if (out.tellp() >= 0x3fffffff) {
throwNew(env, "java/lang/IllegalStateException",
"Output exceeds string size limit");
return NULL;
}
return env->NewStringUTF(out.str().c_str());
}
throwNew(env, "java/lang/IllegalStateException", error.message());
return NULL;
}
extern "C" DLLEXPORT jlong JNICALL
Java_com_datadoghq_profiler_JavaProfiler_getSamples(JNIEnv *env,
jobject unused) {
return (jlong)Profiler::instance()->total_samples();
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JavaProfiler_filterThread0(JNIEnv *env,
jobject unused,
jboolean enable) {
int tid = ProfiledThread::currentTid();
if (tid < 0) {
return;
}
ThreadFilter *thread_filter = Profiler::instance()->threadFilter();
if (enable) {
thread_filter->add(tid);
} else {
thread_filter->remove(tid);
}
}
extern "C" DLLEXPORT jobject JNICALL
Java_com_datadoghq_profiler_JavaProfiler_getContextPage0(JNIEnv *env,
jobject unused,
jint tid) {
ContextPage page = Contexts::getPage((int)tid);
return env->NewDirectByteBuffer((void *)page.storage, (jlong)page.capacity);
}
extern "C" DLLEXPORT jlong JNICALL
Java_com_datadoghq_profiler_JavaProfiler_getContextPageOffset0(JNIEnv *env,
jobject unused,
jint tid) {
ContextPage page = Contexts::getPage((int)tid);
return (jlong)page.storage;
}
extern "C" DLLEXPORT jint JNICALL
Java_com_datadoghq_profiler_JavaProfiler_getMaxContextPages0(JNIEnv *env,
jobject unused) {
return (jint)Contexts::getMaxPages();
}
extern "C" DLLEXPORT jboolean JNICALL
Java_com_datadoghq_profiler_JavaProfiler_recordTrace0(
JNIEnv *env, jobject unused, jlong rootSpanId, jstring endpoint,
jstring operation, jint sizeLimit) {
JniString endpoint_str(env, endpoint);
u32 endpointLabel = Profiler::instance()->stringLabelMap()->bounded_lookup(
endpoint_str.c_str(), endpoint_str.length(), sizeLimit);
bool acceptValue = endpointLabel != INT_MAX;
if (acceptValue) {
u32 operationLabel = 0;
if (operation != NULL) {
JniString operation_str(env, operation);
operationLabel = Profiler::instance()->contextValueMap()->bounded_lookup(
operation_str.c_str(), operation_str.length(), 1 << 16);
}
TraceRootEvent event(rootSpanId, endpointLabel, operationLabel);
int tid = ProfiledThread::currentTid();
Profiler::instance()->recordTraceRoot(tid, &event);
}
return acceptValue;
}
extern "C" DLLEXPORT jint JNICALL
Java_com_datadoghq_profiler_JavaProfiler_registerConstant0(JNIEnv *env,
jobject unused,
jstring value) {
JniString value_str(env, value);
u32 encoding = Profiler::instance()->contextValueMap()->bounded_lookup(
value_str.c_str(), value_str.length(), 1 << 16);
return encoding == INT_MAX ? -1 : encoding;
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JavaProfiler_dump0(JNIEnv *env, jobject unused,
jstring path) {
JniString path_str(env, path);
Profiler::instance()->dump(path_str.c_str(), path_str.length());
}
extern "C" DLLEXPORT jobject JNICALL
Java_com_datadoghq_profiler_JavaProfiler_getDebugCounters0(JNIEnv *env,
jobject unused) {
#ifdef COUNTERS
return env->NewDirectByteBuffer((void *)Counters::getCounters(),
(jlong)Counters::size());
#else
return env->NewDirectByteBuffer(nullptr, 0);
#endif // COUNTERS
}
extern "C" DLLEXPORT jobjectArray JNICALL
Java_com_datadoghq_profiler_JavaProfiler_describeDebugCounters0(
JNIEnv *env, jobject unused) {
#ifdef COUNTERS
std::vector<const char *> counter_names = Counters::describeCounters();
jobjectArray array = (jobjectArray)env->NewObjectArray(
counter_names.size(), env->FindClass("java/lang/String"),
env->NewStringUTF(""));
for (int i = 0; i < counter_names.size(); i++) {
env->SetObjectArrayElement(array, i,
env->NewStringUTF(counter_names.at(i)));
}
return array;
#else
return nullptr;
#endif // COUNTERS
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JavaProfiler_recordSettingEvent0(
JNIEnv *env, jobject unused, jstring name, jstring value, jstring unit) {
int tid = ProfiledThread::currentTid();
if (tid < 0) {
return;
}
int length = 0;
JniString name_str(env, name);
length += name_str.length();
JniString value_str(env, value);
length += value_str.length();
JniString unit_str(env, unit);
length += unit_str.length();
Profiler::instance()->writeDatadogProfilerSetting(
tid, length, name_str.c_str(), value_str.c_str(), unit_str.c_str());
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JavaProfiler_recordQueueEnd0(
JNIEnv *env, jobject unused, jlong startTime, jlong endTime, jstring task,
jstring scheduler, jthread origin) {
int tid = ProfiledThread::currentTid();
if (tid < 0) {
return;
}
int origin_tid = VMThread::nativeThreadId(env, origin);
if (origin_tid < 0) {
return;
}
JniString task_str(env, task);
JniString scheduler_str(env, scheduler);
int task_offset =
Profiler::instance()->lookupClass(task_str.c_str(), task_str.length());
if (task_offset < 0) {
return;
}
int scheduler_offset = Profiler::instance()->lookupClass(
scheduler_str.c_str(), scheduler_str.length());
if (scheduler_offset < 0) {
return;
}
QueueTimeEvent event;
event._start = startTime;
event._end = endTime;
event._task = task_offset;
event._scheduler = scheduler_offset;
event._origin = origin_tid;
Profiler::instance()->recordQueueTime(tid, &event);
}
extern "C" DLLEXPORT jlong JNICALL
Java_com_datadoghq_profiler_JavaProfiler_currentTicks0(JNIEnv *env,
jobject unused) {
return TSC::ticks();
}
extern "C" DLLEXPORT jlong JNICALL
Java_com_datadoghq_profiler_JavaProfiler_tscFrequency0(JNIEnv *env,
jobject unused) {
return TSC::frequency();
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JavaProfiler_mallocArenaMax0(JNIEnv *env,
jobject unused,
jint maxArenas) {
OS::mallocArenaMax(maxArenas);
}
extern "C" DLLEXPORT jstring JNICALL
Java_com_datadoghq_profiler_JVMAccess_findStringJVMFlag0(JNIEnv *env,
jobject unused,
jstring flagName) {
JniString flag_str(env, flagName);
char** value = static_cast<char**>(JVMFlag::find(flag_str.c_str(), {JVMFlag::Type::String}));
if (value != NULL && *value != NULL) {
return env->NewStringUTF(*value);
}
return NULL;
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JVMAccess_setStringJVMFlag0(JNIEnv *env,
jobject unused,
jstring flagName,
jstring flagValue) {
JniString flag_str(env, flagName);
JniString value_str(env, flagValue);
char** value = static_cast<char**>(JVMFlag::find(flag_str.c_str(), {JVMFlag::Type::String}));
if (value != NULL) {
*value = strdup(value_str.c_str());
}
}
extern "C" DLLEXPORT jboolean JNICALL
Java_com_datadoghq_profiler_JVMAccess_findBooleanJVMFlag0(JNIEnv *env,
jobject unused,
jstring flagName) {
JniString flag_str(env, flagName);
char* value = static_cast<char*>(JVMFlag::find(flag_str.c_str(), {JVMFlag::Type::Bool}));
if (value != NULL) {
return ((*value) & 0xff) == 1;
}
return false;
}
extern "C" DLLEXPORT void JNICALL
Java_com_datadoghq_profiler_JVMAccess_setBooleanJVMFlag0(JNIEnv *env,
jobject unused,
jstring flagName,
jboolean flagValue) {
JniString flag_str(env, flagName);
char* value = static_cast<char*>(JVMFlag::find(flag_str.c_str(), {JVMFlag::Type::Bool}));
if (value != NULL) {
*value = flagValue ? 1 : 0;
}
}
extern "C" DLLEXPORT jlong JNICALL
Java_com_datadoghq_profiler_JVMAccess_findIntJVMFlag0(JNIEnv *env,
jobject unused,
jstring flagName) {
JniString flag_str(env, flagName);
long* value = static_cast<long*>(JVMFlag::find(flag_str.c_str(), {JVMFlag::Type::Int, JVMFlag::Type::Uint, JVMFlag::Type::Intx, JVMFlag::Type::Uintx, JVMFlag::Type::Uint64_t, JVMFlag::Type::Size_t}));
if (value != NULL) {
return *value;
}
return 0;
}
extern "C" DLLEXPORT jdouble JNICALL
Java_com_datadoghq_profiler_JVMAccess_findFloatJVMFlag0(JNIEnv *env,
jobject unused,
jstring flagName) {
JniString flag_str(env, flagName);
double* value = static_cast<double*>(JVMFlag::find(flag_str.c_str(),{ JVMFlag::Type::Double}));
if (value != NULL) {
return *value;
}
return 0.0;
}
extern "C" DLLEXPORT jboolean JNICALL
Java_com_datadoghq_profiler_JVMAccess_healthCheck0(JNIEnv *env,
jobject unused) {
return true;
}