Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetCurrentThreadCpuTime/curthrcputime001/curthrcputime001.cpp
40951 views
1
/*
2
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
#include <stdlib.h>
25
#include <string.h>
26
#include "jvmti.h"
27
#include "agent_common.h"
28
#include "jni_tools.h"
29
#include "jvmti_tools.h"
30
31
extern "C" {
32
33
/* ============================================================================= */
34
35
static jlong timeout = 0;
36
37
#define TESTED_THREAD_NAME "curthrcputime001Thread"
38
39
#define STATUS_FAIL 97
40
41
#define EVENTS_COUNT 2
42
43
static jvmtiEvent events[EVENTS_COUNT] = {
44
JVMTI_EVENT_VM_INIT,
45
JVMTI_EVENT_VM_DEATH
46
};
47
48
#define THREAD_EVENTS_COUNT 2
49
50
static jvmtiEvent threadEvents[THREAD_EVENTS_COUNT] = {
51
JVMTI_EVENT_THREAD_START,
52
JVMTI_EVENT_THREAD_END
53
};
54
55
static julong prevTestedThreadTime = 0;
56
static julong prevAgentThreadTime = 0;
57
58
static int iterations = 0;
59
60
/* ============================================================================= */
61
62
/**
63
* Get time and optionally compares it with initial one.
64
* @returns NSK_FALSE if any error occured.
65
*/
66
static int checkCpuTime(jvmtiEnv* jvmti, jthread thread, julong* time,
67
julong* prevTime, const char where[]) {
68
69
char buf[64], buf2[64], buf3[64];
70
int success = NSK_TRUE;
71
72
NSK_DISPLAY1("GetCurrentThreadCpuTime() for current thread: 0x%p\n", (void*)thread);
73
if (!NSK_JVMTI_VERIFY(jvmti->GetCurrentThreadCpuTime((jlong *)time))) {
74
return NSK_FALSE;
75
}
76
NSK_DISPLAY1(" ... got cpu time: %s\n", julong_to_string(*time, buf));
77
78
if (*time == 0) {
79
NSK_DISPLAY2("# WARNING: In %s GetCurrentThreadCpuTime() returned zero cpu time: %s\n",
80
where, julong_to_string(*time, buf));
81
}
82
83
if (prevTime != NULL) {
84
julong diff = *time - *prevTime;
85
86
NSK_DISPLAY1("Compare with previous time: %s\n",
87
julong_to_string(*prevTime, buf));
88
NSK_DISPLAY1(" ... difference: %s\n",
89
julong_to_string(diff, buf));
90
91
if (*time < *prevTime) {
92
NSK_COMPLAIN4("In %s GetCurrentThreadCpuTime() returned decreased cpu time:\n"
93
"# got cpu time: %s\n"
94
"# previous: %s\n"
95
"# difference: %s\n",
96
where,
97
julong_to_string(*time, buf),
98
julong_to_string(*prevTime, buf2),
99
julong_to_string(diff, buf3));
100
success = NSK_FALSE;
101
}
102
103
if (*time == *prevTime) {
104
NSK_DISPLAY3("# WARNING: In %s GetCurrentThreadCpuTime() returned not increased cpu time:\n"
105
"# got cpu time: %s\n"
106
"# previous: %s\n",
107
where,
108
julong_to_string(*time, buf),
109
julong_to_string(*prevTime, buf2));
110
}
111
*prevTime = *time;
112
}
113
114
return success;
115
}
116
117
/** Run some code. */
118
static void runIterations(int n) {
119
int k;
120
121
for (k = 0; k < n; k++) {
122
int s = k;
123
int i;
124
125
for (i = 0; i < n; i++) {
126
if (i % 2 == 0) {
127
s += i * 10;
128
} else {
129
s -= i * 10;
130
}
131
}
132
}
133
}
134
135
/* ============================================================================= */
136
137
/** Agent algorithm. */
138
static void JNICALL
139
agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
140
141
jthread testAgentThread = nsk_jvmti_getAgentThread();
142
NSK_DISPLAY1("Started agent thread: 0x%p\n", testAgentThread);
143
144
NSK_DISPLAY0("Wait for debugee to become ready\n");
145
if (!nsk_jvmti_waitForSync(timeout))
146
return;
147
148
NSK_DISPLAY0(">>> Testcase #2: Check initial cpu time in agent thread\n");
149
{
150
if (!checkCpuTime(jvmti, testAgentThread, &prevAgentThreadTime, NULL, "agent thread")) {
151
nsk_jvmti_setFailStatus();
152
}
153
}
154
155
NSK_DISPLAY0(">>> Testcases #3,5: Check cpu times in tested thread events\n");
156
{
157
runIterations(iterations);
158
159
NSK_DISPLAY1("Enable thread events: %d events\n", THREAD_EVENTS_COUNT);
160
if (nsk_jvmti_enableEvents(JVMTI_ENABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {
161
NSK_DISPLAY0(" ... enabled\n");
162
}
163
164
NSK_DISPLAY0("Let tested thread to start\n");
165
if (!nsk_jvmti_resumeSync())
166
return;
167
if (!nsk_jvmti_waitForSync(timeout))
168
return;
169
170
171
NSK_DISPLAY0(">>> Testcase #4: Check middle cpu time in agent thread\n");
172
{
173
julong time = 0;
174
runIterations(iterations);
175
if (!checkCpuTime(jvmti, testAgentThread, &time, &prevAgentThreadTime, "agent thread")) {
176
nsk_jvmti_setFailStatus();
177
}
178
}
179
180
NSK_DISPLAY0("Let tested thread to finish\n");
181
if (!nsk_jvmti_resumeSync())
182
return;
183
if (!nsk_jvmti_waitForSync(timeout))
184
return;
185
186
NSK_DISPLAY1("Disable thread events: %d events\n", THREAD_EVENTS_COUNT);
187
if (nsk_jvmti_enableEvents(JVMTI_DISABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {
188
NSK_DISPLAY0(" ... disabled\n");
189
}
190
}
191
192
NSK_DISPLAY0(">>> Testcase #6: Check final cpu time in agent thread\n");
193
{
194
julong time = 0;
195
runIterations(iterations);
196
if (!checkCpuTime(jvmti, testAgentThread, &time, &prevAgentThreadTime, "agent thread")) {
197
nsk_jvmti_setFailStatus();
198
}
199
}
200
201
NSK_DISPLAY0("Let debugee to finish\n");
202
if (!nsk_jvmti_resumeSync())
203
return;
204
}
205
206
/* ============================================================================= */
207
208
/**
209
* Callback for VM_INIT event.
210
*/
211
JNIEXPORT void JNICALL
212
callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
213
214
NSK_DISPLAY0(">>> Testcase #1: Check initial cpu time in VM_INIT callback\n");
215
{
216
julong time = 0;
217
if (!checkCpuTime(jvmti, thread, &time, NULL, "VM_INIT callback")) {
218
nsk_jvmti_setFailStatus();
219
}
220
}
221
}
222
223
/**
224
* Callback for VM_DEATH event.
225
*/
226
JNIEXPORT void JNICALL
227
callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
228
int success = NSK_TRUE;
229
230
NSK_DISPLAY0(">>> Testcase #7: Check initial cpu time in VM_DEATH callback\n");
231
{
232
julong time = 0;
233
if (!checkCpuTime(jvmti, NULL, &time, NULL, "VM_DEATH callback")) {
234
success = NSK_FALSE;
235
nsk_jvmti_setFailStatus();
236
}
237
}
238
239
NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);
240
if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) {
241
success = NSK_FALSE;
242
} else {
243
NSK_DISPLAY0(" ... disabled\n");
244
}
245
246
if (!success) {
247
NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL);
248
NSK_BEFORE_TRACE(exit(STATUS_FAIL));
249
}
250
}
251
252
/* ============================================================================= */
253
254
/**
255
* Callback for THREAD_START event.
256
*/
257
JNIEXPORT void JNICALL
258
callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
259
260
jvmtiThreadInfo threadInfo;
261
{
262
if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(thread, &threadInfo))) {
263
nsk_jvmti_setFailStatus();
264
return;
265
}
266
NSK_DISPLAY1(" <THREAD_START> for thread: %s\n", nsk_null_string(threadInfo.name));
267
}
268
269
if (threadInfo.name != NULL && strcmp(threadInfo.name, TESTED_THREAD_NAME) == 0) {
270
NSK_DISPLAY0(">>> Testcase #3: Check initial cpu time in THREAD_START callback\n");
271
if (!checkCpuTime(jvmti, thread, &prevTestedThreadTime, NULL, "THREAD_START callback")) {
272
nsk_jvmti_setFailStatus();
273
}
274
}
275
}
276
277
/**
278
* Callback for THREAD_END event.
279
*/
280
JNIEXPORT void JNICALL
281
callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
282
283
jvmtiThreadInfo threadInfo;
284
{
285
if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(thread, &threadInfo))) {
286
nsk_jvmti_setFailStatus();
287
return;
288
}
289
NSK_DISPLAY1(" <THREAD_END> for thread: %s\n", nsk_null_string(threadInfo.name));
290
}
291
292
if (threadInfo.name != NULL && strcmp(threadInfo.name, TESTED_THREAD_NAME) == 0) {
293
julong time = 0;
294
NSK_DISPLAY0(">>> Testcase #5: Check final cpu time in THREAD_END callback\n");
295
if (!checkCpuTime(jvmti, thread, &time, &prevTestedThreadTime, "THREAD_END callback")) {
296
nsk_jvmti_setFailStatus();
297
}
298
}
299
}
300
301
/* ============================================================================= */
302
303
/** Agent library initialization. */
304
#ifdef STATIC_BUILD
305
JNIEXPORT jint JNICALL Agent_OnLoad_curthrcputime001(JavaVM *jvm, char *options, void *reserved) {
306
return Agent_Initialize(jvm, options, reserved);
307
}
308
JNIEXPORT jint JNICALL Agent_OnAttach_curthrcputime001(JavaVM *jvm, char *options, void *reserved) {
309
return Agent_Initialize(jvm, options, reserved);
310
}
311
JNIEXPORT jint JNI_OnLoad_curthrcputime001(JavaVM *jvm, char *options, void *reserved) {
312
return JNI_VERSION_1_8;
313
}
314
#endif
315
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
316
jvmtiEnv* jvmti = NULL;
317
318
if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
319
return JNI_ERR;
320
321
timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
322
323
iterations = nsk_jvmti_findOptionIntValue("iterations", 1000);
324
if (!NSK_VERIFY(iterations >= 1000))
325
return JNI_ERR;
326
327
if (!NSK_VERIFY((jvmti =
328
nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
329
return JNI_ERR;
330
331
NSK_DISPLAY1("Add required capability: %s\n", "can_get_current_thread_cpu_time");
332
{
333
jvmtiCapabilities caps;
334
335
memset(&caps, 0, sizeof(caps));
336
caps.can_get_current_thread_cpu_time = 1;
337
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
338
return JNI_ERR;
339
}
340
}
341
NSK_DISPLAY0(" ... capability added\n");
342
343
NSK_DISPLAY1("Set events callbacks: %s\n", "VM_INIT, VM_DEATH, THREAD_START, THREAD_END");
344
{
345
jvmtiEventCallbacks eventCallbacks;
346
347
memset(&eventCallbacks, 0, sizeof(eventCallbacks));
348
eventCallbacks.VMInit = callbackVMInit;
349
eventCallbacks.VMDeath = callbackVMDeath;
350
eventCallbacks.ThreadStart = callbackThreadStart;
351
eventCallbacks.ThreadEnd = callbackThreadEnd;
352
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
353
return JNI_ERR;
354
}
355
}
356
NSK_DISPLAY0(" ... callbacks set\n");
357
358
if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
359
return JNI_ERR;
360
361
NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);
362
if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {
363
NSK_DISPLAY0(" ... enabled\n");
364
}
365
366
return JNI_OK;
367
}
368
369
/* ============================================================================= */
370
371
}
372
373