test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetThreadCpuTimerInfo/thrtimerinfo001/thrtimerinfo001.cpp
changeset 51551 e409244ce72e
parent 50260 46c67f5e27c2
child 51672 b613bf6a10b1
equal deleted inserted replaced
51550:a2f1923b3e16 51551:e409244ce72e
       
     1 /*
       
     2  * Copyright (c) 2003, 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 "jvmti.h"
       
    27 #include "agent_common.h"
       
    28 #include "jni_tools.h"
       
    29 #include "jvmti_tools.h"
       
    30 
       
    31 #ifdef __cplusplus
       
    32 extern "C" {
       
    33 #endif
       
    34 
       
    35 /* ============================================================================= */
       
    36 
       
    37 static jlong timeout = 0;
       
    38 
       
    39 #define STATUS_FAIL     97
       
    40 
       
    41 #define EVENTS_COUNT    2
       
    42 
       
    43 static jvmtiEvent events[EVENTS_COUNT] = {
       
    44     JVMTI_EVENT_VM_INIT,
       
    45     JVMTI_EVENT_VM_DEATH
       
    46 };
       
    47 
       
    48 #define THREAD_EVENTS_COUNT    2
       
    49 
       
    50 static jvmtiEvent threadEvents[THREAD_EVENTS_COUNT] = {
       
    51     JVMTI_EVENT_THREAD_START,
       
    52     JVMTI_EVENT_THREAD_END
       
    53 };
       
    54 
       
    55 static jvmtiTimerInfo initInfo;
       
    56 
       
    57 /* ============================================================================= */
       
    58 
       
    59 /**
       
    60  * Get timer info and optionally compares it with initial one.
       
    61  * @returns NSK_FALSE if any error occured.
       
    62  */
       
    63 static int checkTimerInfo(jvmtiEnv* jvmti, jvmtiTimerInfo* info,
       
    64                             jvmtiTimerInfo* initInfo, const char where[]) {
       
    65 
       
    66     char buf[64], buf2[64];
       
    67     int success = NSK_TRUE;
       
    68 
       
    69     NSK_DISPLAY0("GetThreadCpuTimerInfo() for current JVMTI env\n");
       
    70     if (!NSK_JVMTI_VERIFY(
       
    71             NSK_CPP_STUB2(GetThreadCpuTimerInfo, jvmti, info))) {
       
    72         return NSK_FALSE;
       
    73     }
       
    74     NSK_DISPLAY0("Got timer info:\n");
       
    75 
       
    76     NSK_DISPLAY1("    max_value:         %s\n",
       
    77                  julong_to_string((julong)info->max_value, buf));
       
    78     NSK_DISPLAY1("    may_skip_forward:  %d\n", (int)info->may_skip_forward);
       
    79     NSK_DISPLAY1("    may_skip_backward: %d\n", (int)info->may_skip_backward);
       
    80 
       
    81     if (initInfo != NULL) {
       
    82         NSK_DISPLAY0("Compare with initial timer info\n");
       
    83         if (info->max_value != initInfo->max_value) {
       
    84             NSK_COMPLAIN4("In %s GetThreadCpuTimerInfo() returned different info:\n"
       
    85                           "#   field:     %s\n"
       
    86                           "#   got value: %s\n"
       
    87                           "#   initial:   %s\n",
       
    88                           where, "max_value",
       
    89                           julong_to_string((julong)info->max_value, buf),
       
    90                           julong_to_string((julong)initInfo->max_value, buf2));
       
    91             success = NSK_FALSE;
       
    92         }
       
    93         if (info->may_skip_forward != initInfo->may_skip_forward) {
       
    94             NSK_COMPLAIN4("In %s GetThreadCpuTimerInfo() returned different info:\n"
       
    95                           "#   field:     %s\n"
       
    96                           "#   got value: %d\n"
       
    97                           "#   initial:   %d\n",
       
    98                             where, "may_skip_forward",
       
    99                             (int)info->may_skip_forward,
       
   100                             (int)initInfo->may_skip_forward);
       
   101             success = NSK_FALSE;
       
   102         }
       
   103         if (info->may_skip_backward != initInfo->may_skip_backward) {
       
   104             NSK_COMPLAIN4("In %s GetThreadCpuTimerInfo() returned different info:\n"
       
   105                           "#   field:     %s\n"
       
   106                           "#   got value: %d\n"
       
   107                           "#   initial:   %d\n",
       
   108                             where, "may_skip_backward",
       
   109                             (int)info->may_skip_backward,
       
   110                             (int)initInfo->may_skip_backward);
       
   111             success = NSK_FALSE;
       
   112         }
       
   113     }
       
   114 
       
   115     return success;
       
   116 }
       
   117 /* ============================================================================= */
       
   118 
       
   119 /** Agent algorithm. */
       
   120 static void JNICALL
       
   121 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
       
   122     NSK_DISPLAY0("Wait for debugee to become ready\n");
       
   123     if (!nsk_jvmti_waitForSync(timeout))
       
   124         return;
       
   125 
       
   126     NSK_DISPLAY0(">>> Testcase #2: Check timer info in agent thread\n");
       
   127     {
       
   128         jvmtiTimerInfo info;
       
   129         if (!checkTimerInfo(jvmti, &info, &initInfo, "agent thread")) {
       
   130             nsk_jvmti_setFailStatus();
       
   131         }
       
   132     }
       
   133 
       
   134     NSK_DISPLAY0(">>> Testcases #3,4: Check timer info in thread events\n");
       
   135     {
       
   136         NSK_DISPLAY1("Enable thread events: %d events\n", THREAD_EVENTS_COUNT);
       
   137         if (nsk_jvmti_enableEvents(JVMTI_ENABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {
       
   138             NSK_DISPLAY0("  ... enabled\n");
       
   139         }
       
   140 
       
   141         NSK_DISPLAY0("Let tested thread to start and finish\n");
       
   142         if (!nsk_jvmti_resumeSync())
       
   143             return;
       
   144         if (!nsk_jvmti_waitForSync(timeout))
       
   145             return;
       
   146 
       
   147         NSK_DISPLAY1("Disable thread events: %d events\n", THREAD_EVENTS_COUNT);
       
   148         if (nsk_jvmti_enableEvents(JVMTI_DISABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {
       
   149             NSK_DISPLAY0("  ... disabled\n");
       
   150         }
       
   151     }
       
   152 
       
   153     NSK_DISPLAY0("Let debugee to finish\n");
       
   154     if (!nsk_jvmti_resumeSync())
       
   155         return;
       
   156 }
       
   157 
       
   158 /* ============================================================================= */
       
   159 
       
   160 /**
       
   161  * Callback for VM_INIT event.
       
   162  */
       
   163 JNIEXPORT void JNICALL
       
   164 callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
       
   165 
       
   166     NSK_DISPLAY0(">>> Testcase #1: Check initial timer info in VM_INIT callback\n");
       
   167     {
       
   168         if (!checkTimerInfo(jvmti, &initInfo, NULL, "VM_INIT callback")) {
       
   169             nsk_jvmti_setFailStatus();
       
   170         }
       
   171     }
       
   172 }
       
   173 
       
   174 /**
       
   175  * Callback for VM_DEATH event.
       
   176  */
       
   177 JNIEXPORT void JNICALL
       
   178 callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
       
   179     int success = NSK_TRUE;
       
   180 
       
   181     NSK_DISPLAY0(">>> Testcase #5: Check timer info in VM_DEATH callback\n");
       
   182     {
       
   183         jvmtiTimerInfo info;
       
   184         success = checkTimerInfo(jvmti, &info, &initInfo, "VM_DEATH callback");
       
   185     }
       
   186 
       
   187     NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);
       
   188     if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) {
       
   189         success = NSK_FALSE;
       
   190     } else {
       
   191         NSK_DISPLAY0("  ... disabled\n");
       
   192     }
       
   193 
       
   194     if (!success) {
       
   195         NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL);
       
   196         NSK_BEFORE_TRACE(exit(STATUS_FAIL));
       
   197     }
       
   198 }
       
   199 
       
   200 /* ============================================================================= */
       
   201 
       
   202 /**
       
   203  * Callback for THREAD_START event.
       
   204  */
       
   205 JNIEXPORT void JNICALL
       
   206 callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
       
   207 
       
   208     NSK_DISPLAY0(">>> Testcase #3: Check timer info in THREAD_START callback\n");
       
   209     {
       
   210         jvmtiTimerInfo info;
       
   211         if (!checkTimerInfo(jvmti, &info, &initInfo, "THREAD_START callback")) {
       
   212             nsk_jvmti_setFailStatus();
       
   213         }
       
   214     }
       
   215 }
       
   216 
       
   217 /**
       
   218  * Callback for THREAD_END event.
       
   219  */
       
   220 JNIEXPORT void JNICALL
       
   221 callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
       
   222 
       
   223     NSK_DISPLAY0(">>> Testcase #4: Check timer info in THREAD_END callback\n");
       
   224     {
       
   225         jvmtiTimerInfo info;
       
   226         if (!checkTimerInfo(jvmti, &info, &initInfo, "THREAD_END callback")) {
       
   227             nsk_jvmti_setFailStatus();
       
   228         }
       
   229     }
       
   230 }
       
   231 
       
   232 /* ============================================================================= */
       
   233 
       
   234 /** Agent library initialization. */
       
   235 #ifdef STATIC_BUILD
       
   236 JNIEXPORT jint JNICALL Agent_OnLoad_thrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {
       
   237     return Agent_Initialize(jvm, options, reserved);
       
   238 }
       
   239 JNIEXPORT jint JNICALL Agent_OnAttach_thrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {
       
   240     return Agent_Initialize(jvm, options, reserved);
       
   241 }
       
   242 JNIEXPORT jint JNI_OnLoad_thrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {
       
   243     return JNI_VERSION_1_8;
       
   244 }
       
   245 #endif
       
   246 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
       
   247     jvmtiEnv* jvmti = NULL;
       
   248 
       
   249     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
       
   250         return JNI_ERR;
       
   251 
       
   252     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
       
   253 
       
   254     if (!NSK_VERIFY((jvmti =
       
   255             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
       
   256         return JNI_ERR;
       
   257 
       
   258     NSK_DISPLAY1("Add required capability: %s\n", "can_get_thread_cpu_time");
       
   259     {
       
   260         jvmtiCapabilities caps;
       
   261 
       
   262         memset(&caps, 0, sizeof(caps));
       
   263         caps.can_get_thread_cpu_time = 1;
       
   264         if (!NSK_JVMTI_VERIFY(
       
   265                 NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
       
   266             return JNI_ERR;
       
   267         }
       
   268     }
       
   269     NSK_DISPLAY0("  ... capability added\n");
       
   270 
       
   271     NSK_DISPLAY1("Set events callbacks: %s\n", "VM_INIT, VM_DEATH, THREAD_START, THREAD_END");
       
   272     {
       
   273         jvmtiEventCallbacks eventCallbacks;
       
   274 
       
   275         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
       
   276         eventCallbacks.VMInit = callbackVMInit;
       
   277         eventCallbacks.VMDeath = callbackVMDeath;
       
   278         eventCallbacks.ThreadStart = callbackThreadStart;
       
   279         eventCallbacks.ThreadEnd = callbackThreadEnd;
       
   280         if (!NSK_JVMTI_VERIFY(
       
   281                 NSK_CPP_STUB3(SetEventCallbacks, jvmti,
       
   282                                     &eventCallbacks, sizeof(eventCallbacks)))) {
       
   283             return JNI_ERR;
       
   284         }
       
   285     }
       
   286     NSK_DISPLAY0("  ... callbacks set\n");
       
   287 
       
   288     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
       
   289         return JNI_ERR;
       
   290 
       
   291     NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);
       
   292     if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {
       
   293         NSK_DISPLAY0("  ... enabled\n");
       
   294     }
       
   295 
       
   296     return JNI_OK;
       
   297 }
       
   298 
       
   299 /* ============================================================================= */
       
   300 
       
   301 #ifdef __cplusplus
       
   302 }
       
   303 #endif