J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004 Sun Microsystems, Inc. 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 Sun Microsystems 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 |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | /* Example of using JVMTI_EVENT_GARBAGE_COLLECTION_START and |
| 33 | * JVMTI_EVENT_GARBAGE_COLLECTION_FINISH events. |
| 34 | */ |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include "jni.h" |
| 41 | #include "jvmti.h" |
| 42 | |
| 43 | /* For stdout_message(), fatal_error(), and check_jvmti_error() */ |
| 44 | #include "agent_util.h" |
| 45 | |
| 46 | /* Global static data */ |
| 47 | static jvmtiEnv *jvmti; |
| 48 | static int gc_count; |
| 49 | static jrawMonitorID lock; |
| 50 | |
| 51 | /* Worker thread that waits for garbage collections */ |
| 52 | static void JNICALL |
| 53 | worker(jvmtiEnv* jvmti, JNIEnv* jni, void *p) |
| 54 | { |
| 55 | jvmtiError err; |
| 56 | |
| 57 | stdout_message("GC worker started...\n"); |
| 58 | |
| 59 | for (;;) { |
| 60 | err = (*jvmti)->RawMonitorEnter(jvmti, lock); |
| 61 | check_jvmti_error(jvmti, err, "raw monitor enter"); |
| 62 | while (gc_count == 0) { |
| 63 | err = (*jvmti)->RawMonitorWait(jvmti, lock, 0); |
| 64 | if (err != JVMTI_ERROR_NONE) { |
| 65 | err = (*jvmti)->RawMonitorExit(jvmti, lock); |
| 66 | check_jvmti_error(jvmti, err, "raw monitor wait"); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | gc_count = 0; |
| 71 | |
| 72 | err = (*jvmti)->RawMonitorExit(jvmti, lock); |
| 73 | check_jvmti_error(jvmti, err, "raw monitor exit"); |
| 74 | |
| 75 | /* Perform arbitrary JVMTI/JNI work here to do post-GC cleanup */ |
| 76 | stdout_message("post-GarbageCollectionFinish actions...\n"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* Creates a new jthread */ |
| 81 | static jthread |
| 82 | alloc_thread(JNIEnv *env) |
| 83 | { |
| 84 | jclass thrClass; |
| 85 | jmethodID cid; |
| 86 | jthread res; |
| 87 | |
| 88 | thrClass = (*env)->FindClass(env, "java/lang/Thread"); |
| 89 | if ( thrClass == NULL ) { |
| 90 | fatal_error("Cannot find Thread class\n"); |
| 91 | } |
| 92 | cid = (*env)->GetMethodID(env, thrClass, "<init>", "()V"); |
| 93 | if ( cid == NULL ) { |
| 94 | fatal_error("Cannot find Thread constructor method\n"); |
| 95 | } |
| 96 | res = (*env)->NewObject(env, thrClass, cid); |
| 97 | if ( res == NULL ) { |
| 98 | fatal_error("Cannot create new Thread object\n"); |
| 99 | } |
| 100 | return res; |
| 101 | } |
| 102 | |
| 103 | /* Callback for JVMTI_EVENT_VM_INIT */ |
| 104 | static void JNICALL |
| 105 | vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread) |
| 106 | { |
| 107 | jvmtiError err; |
| 108 | |
| 109 | stdout_message("VMInit...\n"); |
| 110 | |
| 111 | err = (*jvmti)->RunAgentThread(jvmti, alloc_thread(env), &worker, NULL, |
| 112 | JVMTI_THREAD_MAX_PRIORITY); |
| 113 | check_jvmti_error(jvmti, err, "running agent thread"); |
| 114 | } |
| 115 | |
| 116 | /* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_START */ |
| 117 | static void JNICALL |
| 118 | gc_start(jvmtiEnv* jvmti_env) |
| 119 | { |
| 120 | stdout_message("GarbageCollectionStart...\n"); |
| 121 | } |
| 122 | |
| 123 | /* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */ |
| 124 | static void JNICALL |
| 125 | gc_finish(jvmtiEnv* jvmti_env) |
| 126 | { |
| 127 | jvmtiError err; |
| 128 | |
| 129 | stdout_message("GarbageCollectionFinish...\n"); |
| 130 | |
| 131 | err = (*jvmti)->RawMonitorEnter(jvmti, lock); |
| 132 | check_jvmti_error(jvmti, err, "raw monitor enter"); |
| 133 | gc_count++; |
| 134 | err = (*jvmti)->RawMonitorNotify(jvmti, lock); |
| 135 | check_jvmti_error(jvmti, err, "raw monitor notify"); |
| 136 | err = (*jvmti)->RawMonitorExit(jvmti, lock); |
| 137 | check_jvmti_error(jvmti, err, "raw monitor exit"); |
| 138 | } |
| 139 | |
| 140 | /* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */ |
| 141 | JNIEXPORT jint JNICALL |
| 142 | Agent_OnLoad(JavaVM *vm, char *options, void *reserved) |
| 143 | { |
| 144 | jint rc; |
| 145 | jvmtiError err; |
| 146 | jvmtiCapabilities capabilities; |
| 147 | jvmtiEventCallbacks callbacks; |
| 148 | |
| 149 | /* Get JVMTI environment */ |
| 150 | rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION); |
| 151 | if (rc != JNI_OK) { |
| 152 | fatal_error("ERROR: Unable to create jvmtiEnv, rc=%d\n", rc); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | /* Get/Add JVMTI capabilities */ |
| 157 | (void)memset(&capabilities, 0, sizeof(capabilities)); |
| 158 | capabilities.can_generate_garbage_collection_events = 1; |
| 159 | err = (*jvmti)->AddCapabilities(jvmti, &capabilities); |
| 160 | check_jvmti_error(jvmti, err, "add capabilities"); |
| 161 | |
| 162 | /* Set callbacks and enable event notifications */ |
| 163 | memset(&callbacks, 0, sizeof(callbacks)); |
| 164 | callbacks.VMInit = &vm_init; |
| 165 | callbacks.GarbageCollectionStart = &gc_start; |
| 166 | callbacks.GarbageCollectionFinish = &gc_finish; |
| 167 | err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks)); |
| 168 | check_jvmti_error(jvmti, err, "set event callbacks"); |
| 169 | err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, |
| 170 | JVMTI_EVENT_VM_INIT, NULL); |
| 171 | check_jvmti_error(jvmti, err, "set event notification"); |
| 172 | err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, |
| 173 | JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL); |
| 174 | check_jvmti_error(jvmti, err, "set event notification"); |
| 175 | err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, |
| 176 | JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL); |
| 177 | check_jvmti_error(jvmti, err, "set event notification"); |
| 178 | |
| 179 | /* Create the necessary raw monitor */ |
| 180 | err = (*jvmti)->CreateRawMonitor(jvmti, "lock", &lock); |
| 181 | check_jvmti_error(jvmti, err, "create raw monitor"); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* Agent_OnUnload() is called last */ |
| 186 | JNIEXPORT void JNICALL |
| 187 | Agent_OnUnload(JavaVM *vm) |
| 188 | { |
| 189 | } |