Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetCurrentThreadCpuTimerInfo/curthrtimerinfo001/curthrtimerinfo001.cpp
40952 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdlib.h>24#include <string.h>25#include "jvmti.h"26#include "agent_common.h"27#include "jni_tools.h"28#include "jvmti_tools.h"2930extern "C" {3132/* ============================================================================= */3334static jlong timeout = 0;3536#define STATUS_FAIL 973738#define EVENTS_COUNT 23940static jvmtiEvent events[EVENTS_COUNT] = {41JVMTI_EVENT_VM_INIT,42JVMTI_EVENT_VM_DEATH43};4445#define THREAD_EVENTS_COUNT 24647static jvmtiEvent threadEvents[THREAD_EVENTS_COUNT] = {48JVMTI_EVENT_THREAD_START,49JVMTI_EVENT_THREAD_END50};5152static jvmtiTimerInfo initInfo;5354/* ============================================================================= */5556/**57* Get timer info and optionally compares it with initial one.58* @returns NSK_FALSE if any error occured.59*/60static int checkTimerInfo(jvmtiEnv* jvmti, jvmtiTimerInfo* info,61jvmtiTimerInfo* initInfo, const char where[]) {6263char buf[64], buf2[64];64int success = NSK_TRUE;6566NSK_DISPLAY0("GetCurrentThreadCpuTimerInfo() for current JVMTI env\n");67if (!NSK_JVMTI_VERIFY(68jvmti->GetCurrentThreadCpuTimerInfo(info))) {69return NSK_FALSE;70}71NSK_DISPLAY0("Got timer info:\n");7273NSK_DISPLAY1(" max_value: %s\n",74julong_to_string((julong)info->max_value, buf));75NSK_DISPLAY1(" may_skip_forward: %d\n", (int)info->may_skip_forward);76NSK_DISPLAY1(" may_skip_backward: %d\n", (int)info->may_skip_backward);7778if (initInfo != NULL) {79NSK_DISPLAY0("Compare with initial timer info\n");80if (info->max_value != initInfo->max_value) {81NSK_COMPLAIN4("In %s GetCurrentThreadCpuTimerInfo() returned different info:\n"82"# field: %s\n"83"# got value: %s\n"84"# initial: %s\n",85where, "max_value",86julong_to_string((julong)info->max_value, buf),87julong_to_string((julong)initInfo->max_value, buf2));88success = NSK_FALSE;89}90if (info->may_skip_forward != initInfo->may_skip_forward) {91NSK_COMPLAIN4("In %s GetCurrentThreadCpuTimerInfo() returned different info:\n"92"# field: %s\n"93"# got value: %d\n"94"# initial: %d\n",95where, "may_skip_forward",96(int)info->may_skip_forward,97(int)initInfo->may_skip_forward);98success = NSK_FALSE;99}100if (info->may_skip_backward != initInfo->may_skip_backward) {101NSK_COMPLAIN4("In %s GetCurrentThreadCpuTimerInfo() returned different info:\n"102"# field: %s\n"103"# got value: %d\n"104"# initial: %d\n",105where, "may_skip_backward",106(int)info->may_skip_backward,107(int)initInfo->may_skip_backward);108success = NSK_FALSE;109}110}111112return success;113}114/* ============================================================================= */115116/** Agent algorithm. */117static void JNICALL118agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {119NSK_DISPLAY0("Wait for debugee to become ready\n");120if (!nsk_jvmti_waitForSync(timeout))121return;122123NSK_DISPLAY0(">>> Testcase #2: Check timer info in agent thread\n");124{125jvmtiTimerInfo info;126if (!checkTimerInfo(jvmti, &info, &initInfo, "agent thread")) {127nsk_jvmti_setFailStatus();128}129}130131NSK_DISPLAY0(">>> Testcases #3,4: Check timer info in thread events\n");132{133NSK_DISPLAY1("Enable thread events: %d events\n", THREAD_EVENTS_COUNT);134if (nsk_jvmti_enableEvents(JVMTI_ENABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {135NSK_DISPLAY0(" ... enabled\n");136}137138NSK_DISPLAY0("Let tested thread to start and finish\n");139if (!nsk_jvmti_resumeSync())140return;141if (!nsk_jvmti_waitForSync(timeout))142return;143144NSK_DISPLAY1("Disable thread events: %d events\n", THREAD_EVENTS_COUNT);145if (nsk_jvmti_enableEvents(JVMTI_DISABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {146NSK_DISPLAY0(" ... disabled\n");147}148}149150NSK_DISPLAY0("Let debugee to finish\n");151if (!nsk_jvmti_resumeSync())152return;153}154155/* ============================================================================= */156157/**158* Callback for VM_INIT event.159*/160JNIEXPORT void JNICALL161callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {162163NSK_DISPLAY0(">>> Testcase #1: Check initial timer info in VM_INIT callback\n");164{165if (!checkTimerInfo(jvmti, &initInfo, NULL, "VM_INIT callback")) {166nsk_jvmti_setFailStatus();167}168}169}170171/**172* Callback for VM_DEATH event.173*/174JNIEXPORT void JNICALL175callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {176int success = NSK_TRUE;177178NSK_DISPLAY0(">>> Testcase #5: Check timer info in VM_DEATH callback\n");179{180jvmtiTimerInfo info;181success = checkTimerInfo(jvmti, &info, &initInfo, "VM_DEATH callback");182}183184NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);185if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) {186success = NSK_FALSE;187} else {188NSK_DISPLAY0(" ... disabled\n");189}190191if (!success) {192NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL);193NSK_BEFORE_TRACE(exit(STATUS_FAIL));194}195}196197/* ============================================================================= */198199/**200* Callback for THREAD_START event.201*/202JNIEXPORT void JNICALL203callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {204205NSK_DISPLAY0(">>> Testcase #3: Check timer info in THREAD_START callback\n");206{207jvmtiTimerInfo info;208if (!checkTimerInfo(jvmti, &info, &initInfo, "THREAD_START callback")) {209nsk_jvmti_setFailStatus();210}211}212}213214/**215* Callback for THREAD_END event.216*/217JNIEXPORT void JNICALL218callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {219220NSK_DISPLAY0(">>> Testcase #4: Check timer info in THREAD_END callback\n");221{222jvmtiTimerInfo info;223if (!checkTimerInfo(jvmti, &info, &initInfo, "THREAD_END callback")) {224nsk_jvmti_setFailStatus();225}226}227}228229/* ============================================================================= */230231/** Agent library initialization. */232#ifdef STATIC_BUILD233JNIEXPORT jint JNICALL Agent_OnLoad_curthrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {234return Agent_Initialize(jvm, options, reserved);235}236JNIEXPORT jint JNICALL Agent_OnAttach_curthrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {237return Agent_Initialize(jvm, options, reserved);238}239JNIEXPORT jint JNI_OnLoad_curthrtimerinfo001(JavaVM *jvm, char *options, void *reserved) {240return JNI_VERSION_1_8;241}242#endif243jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {244jvmtiEnv* jvmti = NULL;245246if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))247return JNI_ERR;248249timeout = nsk_jvmti_getWaitTime() * 60 * 1000;250251if (!NSK_VERIFY((jvmti =252nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))253return JNI_ERR;254255NSK_DISPLAY1("Add required capability: %s\n", "can_get_current_thread_cpu_time");256{257jvmtiCapabilities caps;258259memset(&caps, 0, sizeof(caps));260caps.can_get_current_thread_cpu_time = 1;261if (!NSK_JVMTI_VERIFY(262jvmti->AddCapabilities(&caps))) {263return JNI_ERR;264}265}266NSK_DISPLAY0(" ... capability added\n");267268NSK_DISPLAY1("Set events callbacks: %s\n", "VM_INIT, VM_DEATH, THREAD_START, THREAD_END");269{270jvmtiEventCallbacks eventCallbacks;271272memset(&eventCallbacks, 0, sizeof(eventCallbacks));273eventCallbacks.VMInit = callbackVMInit;274eventCallbacks.VMDeath = callbackVMDeath;275eventCallbacks.ThreadStart = callbackThreadStart;276eventCallbacks.ThreadEnd = callbackThreadEnd;277if (!NSK_JVMTI_VERIFY(278jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {279return JNI_ERR;280}281}282NSK_DISPLAY0(" ... callbacks set\n");283284if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))285return JNI_ERR;286287NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);288if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {289NSK_DISPLAY0(" ... enabled\n");290}291292return JNI_OK;293}294295/* ============================================================================= */296297}298299300