jdk/src/share/demo/jvmti/gctest/gctest.c
author jgish
Thu, 18 Apr 2013 16:33:11 -0400
changeset 17159 bb566a21b661
parent 14342 8435a30053c1
permissions -rw-r--r--
8012005: LogManager needs test to ensure stack trace is not being done to find bundle Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Example of using JVMTI_EVENT_GARBAGE_COLLECTION_START and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *                  JVMTI_EVENT_GARBAGE_COLLECTION_FINISH events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include "jvmti.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/* For stdout_message(), fatal_error(), and check_jvmti_error() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include "agent_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/* Global static data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static jvmtiEnv     *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static int           gc_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static jrawMonitorID lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/* Worker thread that waits for garbage collections */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
worker(jvmtiEnv* jvmti, JNIEnv* jni, void *p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    stdout_message("GC worker started...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        err = (*jvmti)->RawMonitorEnter(jvmti, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        check_jvmti_error(jvmti, err, "raw monitor enter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        while (gc_count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            err = (*jvmti)->RawMonitorWait(jvmti, lock, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (err != JVMTI_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                err = (*jvmti)->RawMonitorExit(jvmti, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                check_jvmti_error(jvmti, err, "raw monitor wait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        gc_count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        err = (*jvmti)->RawMonitorExit(jvmti, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        check_jvmti_error(jvmti, err, "raw monitor exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        /* Perform arbitrary JVMTI/JNI work here to do post-GC cleanup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        stdout_message("post-GarbageCollectionFinish actions...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
/* Creates a new jthread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
static jthread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
alloc_thread(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    jclass    thrClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    jmethodID cid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    jthread   res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    thrClass = (*env)->FindClass(env, "java/lang/Thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    if ( thrClass == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        fatal_error("Cannot find Thread class\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    cid      = (*env)->GetMethodID(env, thrClass, "<init>", "()V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    if ( cid == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        fatal_error("Cannot find Thread constructor method\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    res      = (*env)->NewObject(env, thrClass, cid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    if ( res == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        fatal_error("Cannot create new Thread object\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
/* Callback for JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    stdout_message("VMInit...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    err = (*jvmti)->RunAgentThread(jvmti, alloc_thread(env), &worker, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        JVMTI_THREAD_MAX_PRIORITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    check_jvmti_error(jvmti, err, "running agent thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
/* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
gc_start(jvmtiEnv* jvmti_env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    stdout_message("GarbageCollectionStart...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
/* Callback for JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
gc_finish(jvmtiEnv* jvmti_env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    stdout_message("GarbageCollectionFinish...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    err = (*jvmti)->RawMonitorEnter(jvmti, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    check_jvmti_error(jvmti, err, "raw monitor enter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    gc_count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    err = (*jvmti)->RawMonitorNotify(jvmti, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    check_jvmti_error(jvmti, err, "raw monitor notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    err = (*jvmti)->RawMonitorExit(jvmti, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    check_jvmti_error(jvmti, err, "raw monitor exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
/* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    jint                rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jvmtiError          err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    jvmtiCapabilities   capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    jvmtiEventCallbacks callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /* Get JVMTI environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (rc != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        fatal_error("ERROR: Unable to create jvmtiEnv, rc=%d\n", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /* Get/Add JVMTI capabilities */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    (void)memset(&capabilities, 0, sizeof(capabilities));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    capabilities.can_generate_garbage_collection_events = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    err = (*jvmti)->AddCapabilities(jvmti, &capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    check_jvmti_error(jvmti, err, "add capabilities");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /* Set callbacks and enable event notifications */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    memset(&callbacks, 0, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    callbacks.VMInit                  = &vm_init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    callbacks.GarbageCollectionStart  = &gc_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    callbacks.GarbageCollectionFinish = &gc_finish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    check_jvmti_error(jvmti, err, "set event callbacks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        JVMTI_EVENT_VM_INIT, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    check_jvmti_error(jvmti, err, "set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    check_jvmti_error(jvmti, err, "set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    check_jvmti_error(jvmti, err, "set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /* Create the necessary raw monitor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    err = (*jvmti)->CreateRawMonitor(jvmti, "lock", &lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    check_jvmti_error(jvmti, err, "create raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
/* Agent_OnUnload() is called last */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
Agent_OnUnload(JavaVM *vm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}