Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetCurrentThreadCpuTimerInfo/curthrtimerinfo001/curthrtimerinfo001.cpp
40952 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 STATUS_FAIL 97
38
39
#define EVENTS_COUNT 2
40
41
static jvmtiEvent events[EVENTS_COUNT] = {
42
JVMTI_EVENT_VM_INIT,
43
JVMTI_EVENT_VM_DEATH
44
};
45
46
#define THREAD_EVENTS_COUNT 2
47
48
static jvmtiEvent threadEvents[THREAD_EVENTS_COUNT] = {
49
JVMTI_EVENT_THREAD_START,
50
JVMTI_EVENT_THREAD_END
51
};
52
53
static jvmtiTimerInfo initInfo;
54
55
/* ============================================================================= */
56
57
/**
58
* Get timer info and optionally compares it with initial one.
59
* @returns NSK_FALSE if any error occured.
60
*/
61
static int checkTimerInfo(jvmtiEnv* jvmti, jvmtiTimerInfo* info,
62
jvmtiTimerInfo* initInfo, const char where[]) {
63
64
char buf[64], buf2[64];
65
int success = NSK_TRUE;
66
67
NSK_DISPLAY0("GetCurrentThreadCpuTimerInfo() for current JVMTI env\n");
68
if (!NSK_JVMTI_VERIFY(
69
jvmti->GetCurrentThreadCpuTimerInfo(info))) {
70
return NSK_FALSE;
71
}
72
NSK_DISPLAY0("Got timer info:\n");
73
74
NSK_DISPLAY1(" max_value: %s\n",
75
julong_to_string((julong)info->max_value, buf));
76
NSK_DISPLAY1(" may_skip_forward: %d\n", (int)info->may_skip_forward);
77
NSK_DISPLAY1(" may_skip_backward: %d\n", (int)info->may_skip_backward);
78
79
if (initInfo != NULL) {
80
NSK_DISPLAY0("Compare with initial timer info\n");
81
if (info->max_value != initInfo->max_value) {
82
NSK_COMPLAIN4("In %s GetCurrentThreadCpuTimerInfo() returned different info:\n"
83
"# field: %s\n"
84
"# got value: %s\n"
85
"# initial: %s\n",
86
where, "max_value",
87
julong_to_string((julong)info->max_value, buf),
88
julong_to_string((julong)initInfo->max_value, buf2));
89
success = NSK_FALSE;
90
}
91
if (info->may_skip_forward != initInfo->may_skip_forward) {
92
NSK_COMPLAIN4("In %s GetCurrentThreadCpuTimerInfo() returned different info:\n"
93
"# field: %s\n"
94
"# got value: %d\n"
95
"# initial: %d\n",
96
where, "may_skip_forward",
97
(int)info->may_skip_forward,
98
(int)initInfo->may_skip_forward);
99
success = NSK_FALSE;
100
}
101
if (info->may_skip_backward != initInfo->may_skip_backward) {
102
NSK_COMPLAIN4("In %s GetCurrentThreadCpuTimerInfo() returned different info:\n"
103
"# field: %s\n"
104
"# got value: %d\n"
105
"# initial: %d\n",
106
where, "may_skip_backward",
107
(int)info->may_skip_backward,
108
(int)initInfo->may_skip_backward);
109
success = NSK_FALSE;
110
}
111
}
112
113
return success;
114
}
115
/* ============================================================================= */
116
117
/** Agent algorithm. */
118
static void JNICALL
119
agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
120
NSK_DISPLAY0("Wait for debugee to become ready\n");
121
if (!nsk_jvmti_waitForSync(timeout))
122
return;
123
124
NSK_DISPLAY0(">>> Testcase #2: Check timer info in agent thread\n");
125
{
126
jvmtiTimerInfo info;
127
if (!checkTimerInfo(jvmti, &info, &initInfo, "agent thread")) {
128
nsk_jvmti_setFailStatus();
129
}
130
}
131
132
NSK_DISPLAY0(">>> Testcases #3,4: Check timer info in thread events\n");
133
{
134
NSK_DISPLAY1("Enable thread events: %d events\n", THREAD_EVENTS_COUNT);
135
if (nsk_jvmti_enableEvents(JVMTI_ENABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {
136
NSK_DISPLAY0(" ... enabled\n");
137
}
138
139
NSK_DISPLAY0("Let tested thread to start and finish\n");
140
if (!nsk_jvmti_resumeSync())
141
return;
142
if (!nsk_jvmti_waitForSync(timeout))
143
return;
144
145
NSK_DISPLAY1("Disable thread events: %d events\n", THREAD_EVENTS_COUNT);
146
if (nsk_jvmti_enableEvents(JVMTI_DISABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {
147
NSK_DISPLAY0(" ... disabled\n");
148
}
149
}
150
151
NSK_DISPLAY0("Let debugee to finish\n");
152
if (!nsk_jvmti_resumeSync())
153
return;
154
}
155
156
/* ============================================================================= */
157
158
/**
159
* Callback for VM_INIT event.
160
*/
161
JNIEXPORT void JNICALL
162
callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
163
164
NSK_DISPLAY0(">>> Testcase #1: Check initial timer info in VM_INIT callback\n");
165
{
166
if (!checkTimerInfo(jvmti, &initInfo, NULL, "VM_INIT callback")) {
167
nsk_jvmti_setFailStatus();
168
}
169
}
170
}
171
172
/**
173
* Callback for VM_DEATH event.
174
*/
175
JNIEXPORT void JNICALL
176
callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
177
int success = NSK_TRUE;
178
179
NSK_DISPLAY0(">>> Testcase #5: Check timer info in VM_DEATH callback\n");
180
{
181
jvmtiTimerInfo info;
182
success = checkTimerInfo(jvmti, &info, &initInfo, "VM_DEATH callback");
183
}
184
185
NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);
186
if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) {
187
success = NSK_FALSE;
188
} else {
189
NSK_DISPLAY0(" ... disabled\n");
190
}
191
192
if (!success) {
193
NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL);
194
NSK_BEFORE_TRACE(exit(STATUS_FAIL));
195
}
196
}
197
198
/* ============================================================================= */
199
200
/**
201
* Callback for THREAD_START event.
202
*/
203
JNIEXPORT void JNICALL
204
callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
205
206
NSK_DISPLAY0(">>> Testcase #3: Check timer info in THREAD_START callback\n");
207
{
208
jvmtiTimerInfo info;
209
if (!checkTimerInfo(jvmti, &info, &initInfo, "THREAD_START callback")) {
210
nsk_jvmti_setFailStatus();
211
}
212
}
213
}
214
215
/**
216
* Callback for THREAD_END event.
217
*/
218
JNIEXPORT void JNICALL
219
callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
220
221
NSK_DISPLAY0(">>> Testcase #4: Check timer info in THREAD_END callback\n");
222
{
223
jvmtiTimerInfo info;
224
if (!checkTimerInfo(jvmti, &info, &initInfo, "THREAD_END callback")) {
225
nsk_jvmti_setFailStatus();
226
}
227
}
228
}
229
230
/* ============================================================================= */
231
232
/** Agent library initialization. */
233
#ifdef STATIC_BUILD
234
JNIEXPORT jint JNICALL Agent_OnLoad_curthrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {
235
return Agent_Initialize(jvm, options, reserved);
236
}
237
JNIEXPORT jint JNICALL Agent_OnAttach_curthrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {
238
return Agent_Initialize(jvm, options, reserved);
239
}
240
JNIEXPORT jint JNI_OnLoad_curthrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {
241
return JNI_VERSION_1_8;
242
}
243
#endif
244
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
245
jvmtiEnv* jvmti = NULL;
246
247
if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
248
return JNI_ERR;
249
250
timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
251
252
if (!NSK_VERIFY((jvmti =
253
nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
254
return JNI_ERR;
255
256
NSK_DISPLAY1("Add required capability: %s\n", "can_get_current_thread_cpu_time");
257
{
258
jvmtiCapabilities caps;
259
260
memset(&caps, 0, sizeof(caps));
261
caps.can_get_current_thread_cpu_time = 1;
262
if (!NSK_JVMTI_VERIFY(
263
jvmti->AddCapabilities(&caps))) {
264
return JNI_ERR;
265
}
266
}
267
NSK_DISPLAY0(" ... capability added\n");
268
269
NSK_DISPLAY1("Set events callbacks: %s\n", "VM_INIT, VM_DEATH, THREAD_START, THREAD_END");
270
{
271
jvmtiEventCallbacks eventCallbacks;
272
273
memset(&eventCallbacks, 0, sizeof(eventCallbacks));
274
eventCallbacks.VMInit = callbackVMInit;
275
eventCallbacks.VMDeath = callbackVMDeath;
276
eventCallbacks.ThreadStart = callbackThreadStart;
277
eventCallbacks.ThreadEnd = callbackThreadEnd;
278
if (!NSK_JVMTI_VERIFY(
279
jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
280
return JNI_ERR;
281
}
282
}
283
NSK_DISPLAY0(" ... callbacks set\n");
284
285
if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
286
return JNI_ERR;
287
288
NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);
289
if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {
290
NSK_DISPLAY0(" ... enabled\n");
291
}
292
293
return JNI_OK;
294
}
295
296
/* ============================================================================= */
297
298
}
299
300