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