1 /*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
131
132 /* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */
133 static void JNICALL
134 gc_finish(jvmtiEnv* jvmti_env)
135 {
136 jvmtiError err;
137
138 stdout_message("GarbageCollectionFinish...\n");
139
140 err = (*jvmti)->RawMonitorEnter(jvmti, lock);
141 check_jvmti_error(jvmti, err, "raw monitor enter");
142 gc_count++;
143 err = (*jvmti)->RawMonitorNotify(jvmti, lock);
144 check_jvmti_error(jvmti, err, "raw monitor notify");
145 err = (*jvmti)->RawMonitorExit(jvmti, lock);
146 check_jvmti_error(jvmti, err, "raw monitor exit");
147 }
148
149 /* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
150 JNIEXPORT jint JNICALL
151 Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
152 {
153 jint rc;
154 jvmtiError err;
155 jvmtiCapabilities capabilities;
156 jvmtiEventCallbacks callbacks;
157
158 /* Get JVMTI environment */
159 rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);
160 if (rc != JNI_OK) {
161 fatal_error("ERROR: Unable to create jvmtiEnv, rc=%d\n", rc);
162 return -1;
163 }
164
165 /* Get/Add JVMTI capabilities */
166 (void)memset(&capabilities, 0, sizeof(capabilities));
167 capabilities.can_generate_garbage_collection_events = 1;
168 err = (*jvmti)->AddCapabilities(jvmti, &capabilities);
169 check_jvmti_error(jvmti, err, "add capabilities");
170
171 /* Set callbacks and enable event notifications */
176 err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
177 check_jvmti_error(jvmti, err, "set event callbacks");
178 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
179 JVMTI_EVENT_VM_INIT, NULL);
180 check_jvmti_error(jvmti, err, "set event notification");
181 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
182 JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
183 check_jvmti_error(jvmti, err, "set event notification");
184 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
185 JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);
186 check_jvmti_error(jvmti, err, "set event notification");
187
188 /* Create the necessary raw monitor */
189 err = (*jvmti)->CreateRawMonitor(jvmti, "lock", &lock);
190 check_jvmti_error(jvmti, err, "create raw monitor");
191 return 0;
192 }
193
194 /* Agent_OnUnload() is called last */
195 JNIEXPORT void JNICALL
196 Agent_OnUnload(JavaVM *vm)
197 {
198 }
|
1 /*
2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
131
132 /* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */
133 static void JNICALL
134 gc_finish(jvmtiEnv* jvmti_env)
135 {
136 jvmtiError err;
137
138 stdout_message("GarbageCollectionFinish...\n");
139
140 err = (*jvmti)->RawMonitorEnter(jvmti, lock);
141 check_jvmti_error(jvmti, err, "raw monitor enter");
142 gc_count++;
143 err = (*jvmti)->RawMonitorNotify(jvmti, lock);
144 check_jvmti_error(jvmti, err, "raw monitor notify");
145 err = (*jvmti)->RawMonitorExit(jvmti, lock);
146 check_jvmti_error(jvmti, err, "raw monitor exit");
147 }
148
149 /* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
150 JNIEXPORT jint JNICALL
151 DEF_Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
152 {
153 jint rc;
154 jvmtiError err;
155 jvmtiCapabilities capabilities;
156 jvmtiEventCallbacks callbacks;
157
158 /* Get JVMTI environment */
159 rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);
160 if (rc != JNI_OK) {
161 fatal_error("ERROR: Unable to create jvmtiEnv, rc=%d\n", rc);
162 return -1;
163 }
164
165 /* Get/Add JVMTI capabilities */
166 (void)memset(&capabilities, 0, sizeof(capabilities));
167 capabilities.can_generate_garbage_collection_events = 1;
168 err = (*jvmti)->AddCapabilities(jvmti, &capabilities);
169 check_jvmti_error(jvmti, err, "add capabilities");
170
171 /* Set callbacks and enable event notifications */
176 err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
177 check_jvmti_error(jvmti, err, "set event callbacks");
178 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
179 JVMTI_EVENT_VM_INIT, NULL);
180 check_jvmti_error(jvmti, err, "set event notification");
181 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
182 JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
183 check_jvmti_error(jvmti, err, "set event notification");
184 err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
185 JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);
186 check_jvmti_error(jvmti, err, "set event notification");
187
188 /* Create the necessary raw monitor */
189 err = (*jvmti)->CreateRawMonitor(jvmti, "lock", &lock);
190 check_jvmti_error(jvmti, err, "create raw monitor");
191 return 0;
192 }
193
194 /* Agent_OnUnload() is called last */
195 JNIEXPORT void JNICALL
196 DEF_Agent_OnUnload(JavaVM *vm)
197 {
198 }
|