|
1 /* |
|
2 * Copyright (c) 2004, 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 "jni_tools.h" |
|
27 #include "agent_common.h" |
|
28 #include "jvmti_tools.h" |
|
29 |
|
30 #define PASSED 0 |
|
31 #define STATUS_FAILED 2 |
|
32 |
|
33 #ifdef __cplusplus |
|
34 extern "C" { |
|
35 #endif |
|
36 |
|
37 /* ========================================================================== */ |
|
38 |
|
39 /* scaffold objects */ |
|
40 static jlong timeout = 0; |
|
41 |
|
42 /* event counts */ |
|
43 static int GarbageCollectionStartEventsCount = 0; |
|
44 static int GarbageCollectionFinishEventsCount = 0; |
|
45 |
|
46 /* ========================================================================== */ |
|
47 |
|
48 /** callback functions **/ |
|
49 |
|
50 static void JNICALL |
|
51 GarbageCollectionStart(jvmtiEnv *jvmti_env) { |
|
52 GarbageCollectionStartEventsCount++; |
|
53 NSK_DISPLAY0("GarbageCollectionStart\n"); |
|
54 } |
|
55 |
|
56 static void JNICALL |
|
57 GarbageCollectionFinish(jvmtiEnv *jvmti_env) { |
|
58 GarbageCollectionFinishEventsCount++; |
|
59 NSK_DISPLAY0("GarbageCollectionFinish\n"); |
|
60 } |
|
61 |
|
62 /* ========================================================================== */ |
|
63 |
|
64 /** Agent algorithm. */ |
|
65 static void JNICALL |
|
66 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { |
|
67 |
|
68 if (!nsk_jvmti_waitForSync(timeout)) |
|
69 return; |
|
70 |
|
71 NSK_DISPLAY1("GarbageCollectionStart events received: %d\n", |
|
72 GarbageCollectionStartEventsCount); |
|
73 if (!NSK_VERIFY(GarbageCollectionStartEventsCount != 0)) |
|
74 nsk_jvmti_setFailStatus(); |
|
75 |
|
76 NSK_DISPLAY1("GarbageCollectionFinish events received: %d\n", |
|
77 GarbageCollectionFinishEventsCount); |
|
78 if (!NSK_VERIFY(GarbageCollectionFinishEventsCount != 0)) |
|
79 nsk_jvmti_setFailStatus(); |
|
80 |
|
81 if (!nsk_jvmti_resumeSync()) |
|
82 return; |
|
83 } |
|
84 |
|
85 /* ========================================================================== */ |
|
86 |
|
87 /** Agent library initialization. */ |
|
88 #ifdef STATIC_BUILD |
|
89 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t007(JavaVM *jvm, char *options, void *reserved) { |
|
90 return Agent_Initialize(jvm, options, reserved); |
|
91 } |
|
92 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t007(JavaVM *jvm, char *options, void *reserved) { |
|
93 return Agent_Initialize(jvm, options, reserved); |
|
94 } |
|
95 JNIEXPORT jint JNI_OnLoad_ma10t007(JavaVM *jvm, char *options, void *reserved) { |
|
96 return JNI_VERSION_1_8; |
|
97 } |
|
98 #endif |
|
99 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { |
|
100 jvmtiEnv* jvmti = NULL; |
|
101 jvmtiCapabilities caps; |
|
102 jvmtiEventCallbacks callbacks; |
|
103 |
|
104 NSK_DISPLAY0("Agent_OnLoad\n"); |
|
105 |
|
106 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) |
|
107 return JNI_ERR; |
|
108 |
|
109 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; |
|
110 |
|
111 if (!NSK_VERIFY((jvmti = |
|
112 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) |
|
113 return JNI_ERR; |
|
114 |
|
115 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) |
|
116 return JNI_ERR; |
|
117 |
|
118 memset(&caps, 0, sizeof(caps)); |
|
119 caps.can_generate_garbage_collection_events = 1; |
|
120 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { |
|
121 return JNI_ERR; |
|
122 } |
|
123 |
|
124 memset(&callbacks, 0, sizeof(callbacks)); |
|
125 callbacks.GarbageCollectionStart = &GarbageCollectionStart; |
|
126 callbacks.GarbageCollectionFinish = &GarbageCollectionFinish; |
|
127 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks))) |
|
128 return JNI_ERR; |
|
129 |
|
130 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, |
|
131 jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL))) |
|
132 return JNI_ERR; |
|
133 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, |
|
134 jvmti, JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL))) |
|
135 return JNI_ERR; |
|
136 |
|
137 return JNI_OK; |
|
138 } |
|
139 |
|
140 /* ========================================================================== */ |
|
141 |
|
142 #ifdef __cplusplus |
|
143 } |
|
144 #endif |