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/javaApi.cpp at v4.2.1 · async-profiler/async-profiler

Expand file tree

/

javaApi.cpp

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
216 lines (180 loc) · 7.04 KB
/

javaApi.cpp

Copy path

File metadata and controls

216 lines (180 loc) · 7.04 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <string.h>
#include "asprof.h"
#include "incbin.h"
#include "javaApi.h"
#include "os.h"
#include "profiler.h"
#include "vmStructs.h"
INCLUDE_HELPER_CLASS(SERVER_NAME, SERVER_CLASS, "one/profiler/Server")
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);
}
}
extern "C" DLLEXPORT void JNICALL
Java_one_profiler_AsyncProfiler_start0(JNIEnv* env, jobject unused, jstring event, jlong interval, jboolean reset) {
Arguments args;
const char* event_str = env->GetStringUTFChars(event, NULL);
if (strcmp(event_str, EVENT_ALLOC) == 0) {
args._alloc = interval > 0 ? interval : 0;
} else if (strcmp(event_str, EVENT_LOCK) == 0) {
args._lock = interval >= 0 ? interval : DEFAULT_LOCK_INTERVAL;
} else {
args._event = event_str;
args._interval = interval;
}
Error error = Profiler::instance()->start(args, reset);
env->ReleaseStringUTFChars(event, event_str);
if (error) {
throwNew(env, "java/lang/IllegalStateException", error.message());
}
}
extern "C" DLLEXPORT void JNICALL
Java_one_profiler_AsyncProfiler_stop0(JNIEnv* env, jobject unused) {
Error error = Profiler::instance()->stop();
if (error) {
throwNew(env, "java/lang/IllegalStateException", error.message());
}
}
extern "C" DLLEXPORT jstring JNICALL
Java_one_profiler_AsyncProfiler_execute0(JNIEnv* env, jobject unused, jstring command) {
Arguments args;
const char* command_str = env->GetStringUTFChars(command, NULL);
Error error = args.parse(command_str);
env->ReleaseStringUTFChars(command, command_str);
if (error) {
throwNew(env, "java/lang/IllegalArgumentException", error.message());
return NULL;
}
Log::open(args);
if (!args.hasOutputFile()) {
BufferWriter out;
error = Profiler::instance()->runInternal(args, out);
if (!error) {
out << '\0';
if (out.size() >= 0x3fffffff) {
throwNew(env, "java/lang/IllegalStateException", "Output exceeds string size limit");
return NULL;
}
return env->NewStringUTF(out.buf());
}
} else {
FileWriter out(args.file());
if (!out.is_open()) {
throwNew(env, "java/io/IOException", strerror(errno));
return NULL;
}
error = Profiler::instance()->runInternal(args, out);
if (!error) {
return env->NewStringUTF("OK");
}
}
throwNew(env, "java/lang/IllegalStateException", error.message());
return NULL;
}
extern "C" DLLEXPORT jbyteArray JNICALL
Java_one_profiler_AsyncProfiler_execute1(JNIEnv* env, jobject unused, jstring command) {
Arguments args;
const char* command_str = env->GetStringUTFChars(command, NULL);
Error error = args.parse(command_str);
env->ReleaseStringUTFChars(command, command_str);
if (error) {
throwNew(env, "java/lang/IllegalArgumentException", error.message());
return NULL;
}
if (args.hasOutputFile()) {
throwNew(env, "java/lang/IllegalArgumentException", "execute1 calls should not specify an output file argument");
return NULL;
}
Log::open(args);
BufferWriter out;
// TODO: This is doing one more copy than necessary, from ProtoWriter to BufferWriter
error = Profiler::instance()->runInternal(args, out);
if (error) {
throwNew(env, "java/lang/IllegalStateException", error.message());
return NULL;
}
jbyteArray output = env->NewByteArray(out.size());
env->SetByteArrayRegion(output, 0, out.size(), (const jbyte*) out.buf());
return output;
}
extern "C" DLLEXPORT jlong JNICALL
Java_one_profiler_AsyncProfiler_getSamples(JNIEnv* env, jobject unused) {
return (jlong)Profiler::instance()->total_samples();
}
extern "C" DLLEXPORT void JNICALL
Java_one_profiler_AsyncProfiler_filterThread0(JNIEnv* env, jobject unused, jthread thread, jboolean enable) {
int thread_id;
if (thread == NULL) {
thread_id = OS::threadId();
} else if ((thread_id = VMThread::nativeThreadId(env, thread)) < 0) {
return;
}
ThreadFilter* thread_filter = Profiler::instance()->threadFilter();
if (enable) {
thread_filter->add(thread_id);
} else {
thread_filter->remove(thread_id);
}
}
#define F(name, sig) {(char*)#name, (char*)sig, (void*)Java_one_profiler_AsyncProfiler_##name}
static const JNINativeMethod profiler_natives[] = {
F(start0, "(Ljava/lang/String;JZ)V"),
F(stop0, "()V"),
F(execute0, "(Ljava/lang/String;)Ljava/lang/String;"),
F(execute1, "(Ljava/lang/String;)[B"),
F(getSamples, "()J"),
F(filterThread0, "(Ljava/lang/Thread;Z)V"),
};
static const JNINativeMethod* execute0 = &profiler_natives[2];
#undef F
// Since AsyncProfiler class can be renamed or moved to another package (shaded),
// we look for the actual class in the stack trace.
void JavaAPI::registerNatives(jvmtiEnv* jvmti, JNIEnv* jni) {
jvmtiFrameInfo frame[10];
jint frame_count;
if (jvmti->GetStackTrace(NULL, 0, sizeof(frame) / sizeof(frame[0]), frame, &frame_count) != 0) {
return;
}
jclass System = jni->FindClass("java/lang/System");
jmethodID load = jni->GetStaticMethodID(System, "load", "(Ljava/lang/String;)V");
jmethodID loadLibrary = jni->GetStaticMethodID(System, "loadLibrary", "(Ljava/lang/String;)V");
// Look for System.load() or System.loadLibrary() method in the stack trace.
// The next frame will belong to AsyncProfiler class.
for (int i = 0; i < frame_count - 1; i++) {
if (frame[i].method == load || frame[i].method == loadLibrary) {
jclass profiler_class;
if (jvmti->GetMethodDeclaringClass(frame[i + 1].method, &profiler_class) == 0) {
for (int j = 0; j < sizeof(profiler_natives) / sizeof(JNINativeMethod); j++) {
jni->RegisterNatives(profiler_class, &profiler_natives[j], 1);
}
}
break;
}
}
jni->ExceptionClear();
}
bool JavaAPI::startHttpServer(jvmtiEnv* jvmti, JNIEnv* jni, const char* address) {
jclass handler = jni->FindClass("com/sun/net/httpserver/HttpHandler");
jobject loader;
if (handler != NULL && jvmti->GetClassLoader(handler, &loader) == 0) {
jclass cls = jni->DefineClass(SERVER_NAME, loader, (const jbyte*)SERVER_CLASS, INCBIN_SIZEOF(SERVER_CLASS));
if (cls != NULL && jni->RegisterNatives(cls, execute0, 1) == 0) {
jmethodID method = jni->GetStaticMethodID(cls, "start", "(Ljava/lang/String;)V");
if (method != NULL) {
jni->CallStaticVoidMethod(cls, method, jni->NewStringUTF(address));
if (!jni->ExceptionCheck()) {
return true;
}
}
}
}
jni->ExceptionDescribe();
return false;
}
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.