Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetCurrentThreadCpuTime/curthrcputime001/curthrcputime001.cpp
40951 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 TESTED_THREAD_NAME "curthrcputime001Thread"3738#define STATUS_FAIL 973940#define EVENTS_COUNT 24142static jvmtiEvent events[EVENTS_COUNT] = {43JVMTI_EVENT_VM_INIT,44JVMTI_EVENT_VM_DEATH45};4647#define THREAD_EVENTS_COUNT 24849static jvmtiEvent threadEvents[THREAD_EVENTS_COUNT] = {50JVMTI_EVENT_THREAD_START,51JVMTI_EVENT_THREAD_END52};5354static julong prevTestedThreadTime = 0;55static julong prevAgentThreadTime = 0;5657static int iterations = 0;5859/* ============================================================================= */6061/**62* Get time and optionally compares it with initial one.63* @returns NSK_FALSE if any error occured.64*/65static int checkCpuTime(jvmtiEnv* jvmti, jthread thread, julong* time,66julong* prevTime, const char where[]) {6768char buf[64], buf2[64], buf3[64];69int success = NSK_TRUE;7071NSK_DISPLAY1("GetCurrentThreadCpuTime() for current thread: 0x%p\n", (void*)thread);72if (!NSK_JVMTI_VERIFY(jvmti->GetCurrentThreadCpuTime((jlong *)time))) {73return NSK_FALSE;74}75NSK_DISPLAY1(" ... got cpu time: %s\n", julong_to_string(*time, buf));7677if (*time == 0) {78NSK_DISPLAY2("# WARNING: In %s GetCurrentThreadCpuTime() returned zero cpu time: %s\n",79where, julong_to_string(*time, buf));80}8182if (prevTime != NULL) {83julong diff = *time - *prevTime;8485NSK_DISPLAY1("Compare with previous time: %s\n",86julong_to_string(*prevTime, buf));87NSK_DISPLAY1(" ... difference: %s\n",88julong_to_string(diff, buf));8990if (*time < *prevTime) {91NSK_COMPLAIN4("In %s GetCurrentThreadCpuTime() returned decreased cpu time:\n"92"# got cpu time: %s\n"93"# previous: %s\n"94"# difference: %s\n",95where,96julong_to_string(*time, buf),97julong_to_string(*prevTime, buf2),98julong_to_string(diff, buf3));99success = NSK_FALSE;100}101102if (*time == *prevTime) {103NSK_DISPLAY3("# WARNING: In %s GetCurrentThreadCpuTime() returned not increased cpu time:\n"104"# got cpu time: %s\n"105"# previous: %s\n",106where,107julong_to_string(*time, buf),108julong_to_string(*prevTime, buf2));109}110*prevTime = *time;111}112113return success;114}115116/** Run some code. */117static void runIterations(int n) {118int k;119120for (k = 0; k < n; k++) {121int s = k;122int i;123124for (i = 0; i < n; i++) {125if (i % 2 == 0) {126s += i * 10;127} else {128s -= i * 10;129}130}131}132}133134/* ============================================================================= */135136/** Agent algorithm. */137static void JNICALL138agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {139140jthread testAgentThread = nsk_jvmti_getAgentThread();141NSK_DISPLAY1("Started agent thread: 0x%p\n", testAgentThread);142143NSK_DISPLAY0("Wait for debugee to become ready\n");144if (!nsk_jvmti_waitForSync(timeout))145return;146147NSK_DISPLAY0(">>> Testcase #2: Check initial cpu time in agent thread\n");148{149if (!checkCpuTime(jvmti, testAgentThread, &prevAgentThreadTime, NULL, "agent thread")) {150nsk_jvmti_setFailStatus();151}152}153154NSK_DISPLAY0(">>> Testcases #3,5: Check cpu times in tested thread events\n");155{156runIterations(iterations);157158NSK_DISPLAY1("Enable thread events: %d events\n", THREAD_EVENTS_COUNT);159if (nsk_jvmti_enableEvents(JVMTI_ENABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {160NSK_DISPLAY0(" ... enabled\n");161}162163NSK_DISPLAY0("Let tested thread to start\n");164if (!nsk_jvmti_resumeSync())165return;166if (!nsk_jvmti_waitForSync(timeout))167return;168169170NSK_DISPLAY0(">>> Testcase #4: Check middle cpu time in agent thread\n");171{172julong time = 0;173runIterations(iterations);174if (!checkCpuTime(jvmti, testAgentThread, &time, &prevAgentThreadTime, "agent thread")) {175nsk_jvmti_setFailStatus();176}177}178179NSK_DISPLAY0("Let tested thread to finish\n");180if (!nsk_jvmti_resumeSync())181return;182if (!nsk_jvmti_waitForSync(timeout))183return;184185NSK_DISPLAY1("Disable thread events: %d events\n", THREAD_EVENTS_COUNT);186if (nsk_jvmti_enableEvents(JVMTI_DISABLE, THREAD_EVENTS_COUNT, threadEvents, NULL)) {187NSK_DISPLAY0(" ... disabled\n");188}189}190191NSK_DISPLAY0(">>> Testcase #6: Check final cpu time in agent thread\n");192{193julong time = 0;194runIterations(iterations);195if (!checkCpuTime(jvmti, testAgentThread, &time, &prevAgentThreadTime, "agent thread")) {196nsk_jvmti_setFailStatus();197}198}199200NSK_DISPLAY0("Let debugee to finish\n");201if (!nsk_jvmti_resumeSync())202return;203}204205/* ============================================================================= */206207/**208* Callback for VM_INIT event.209*/210JNIEXPORT void JNICALL211callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {212213NSK_DISPLAY0(">>> Testcase #1: Check initial cpu time in VM_INIT callback\n");214{215julong time = 0;216if (!checkCpuTime(jvmti, thread, &time, NULL, "VM_INIT callback")) {217nsk_jvmti_setFailStatus();218}219}220}221222/**223* Callback for VM_DEATH event.224*/225JNIEXPORT void JNICALL226callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {227int success = NSK_TRUE;228229NSK_DISPLAY0(">>> Testcase #7: Check initial cpu time in VM_DEATH callback\n");230{231julong time = 0;232if (!checkCpuTime(jvmti, NULL, &time, NULL, "VM_DEATH callback")) {233success = NSK_FALSE;234nsk_jvmti_setFailStatus();235}236}237238NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);239if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) {240success = NSK_FALSE;241} else {242NSK_DISPLAY0(" ... disabled\n");243}244245if (!success) {246NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL);247NSK_BEFORE_TRACE(exit(STATUS_FAIL));248}249}250251/* ============================================================================= */252253/**254* Callback for THREAD_START event.255*/256JNIEXPORT void JNICALL257callbackThreadStart(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {258259jvmtiThreadInfo threadInfo;260{261if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(thread, &threadInfo))) {262nsk_jvmti_setFailStatus();263return;264}265NSK_DISPLAY1(" <THREAD_START> for thread: %s\n", nsk_null_string(threadInfo.name));266}267268if (threadInfo.name != NULL && strcmp(threadInfo.name, TESTED_THREAD_NAME) == 0) {269NSK_DISPLAY0(">>> Testcase #3: Check initial cpu time in THREAD_START callback\n");270if (!checkCpuTime(jvmti, thread, &prevTestedThreadTime, NULL, "THREAD_START callback")) {271nsk_jvmti_setFailStatus();272}273}274}275276/**277* Callback for THREAD_END event.278*/279JNIEXPORT void JNICALL280callbackThreadEnd(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {281282jvmtiThreadInfo threadInfo;283{284if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(thread, &threadInfo))) {285nsk_jvmti_setFailStatus();286return;287}288NSK_DISPLAY1(" <THREAD_END> for thread: %s\n", nsk_null_string(threadInfo.name));289}290291if (threadInfo.name != NULL && strcmp(threadInfo.name, TESTED_THREAD_NAME) == 0) {292julong time = 0;293NSK_DISPLAY0(">>> Testcase #5: Check final cpu time in THREAD_END callback\n");294if (!checkCpuTime(jvmti, thread, &time, &prevTestedThreadTime, "THREAD_END callback")) {295nsk_jvmti_setFailStatus();296}297}298}299300/* ============================================================================= */301302/** Agent library initialization. */303#ifdef STATIC_BUILD304JNIEXPORT jint JNICALL Agent_OnLoad_curthrcputime001(JavaVM *jvm, char *options, void *reserved) {305return Agent_Initialize(jvm, options, reserved);306}307JNIEXPORT jint JNICALL Agent_OnAttach_curthrcputime001(JavaVM *jvm, char *options, void *reserved) {308return Agent_Initialize(jvm, options, reserved);309}310JNIEXPORT jint JNI_OnLoad_curthrcputime001(JavaVM *jvm, char *options, void *reserved) {311return JNI_VERSION_1_8;312}313#endif314jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {315jvmtiEnv* jvmti = NULL;316317if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))318return JNI_ERR;319320timeout = nsk_jvmti_getWaitTime() * 60 * 1000;321322iterations = nsk_jvmti_findOptionIntValue("iterations", 1000);323if (!NSK_VERIFY(iterations >= 1000))324return JNI_ERR;325326if (!NSK_VERIFY((jvmti =327nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))328return JNI_ERR;329330NSK_DISPLAY1("Add required capability: %s\n", "can_get_current_thread_cpu_time");331{332jvmtiCapabilities caps;333334memset(&caps, 0, sizeof(caps));335caps.can_get_current_thread_cpu_time = 1;336if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {337return JNI_ERR;338}339}340NSK_DISPLAY0(" ... capability added\n");341342NSK_DISPLAY1("Set events callbacks: %s\n", "VM_INIT, VM_DEATH, THREAD_START, THREAD_END");343{344jvmtiEventCallbacks eventCallbacks;345346memset(&eventCallbacks, 0, sizeof(eventCallbacks));347eventCallbacks.VMInit = callbackVMInit;348eventCallbacks.VMDeath = callbackVMDeath;349eventCallbacks.ThreadStart = callbackThreadStart;350eventCallbacks.ThreadEnd = callbackThreadEnd;351if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {352return JNI_ERR;353}354}355NSK_DISPLAY0(" ... callbacks set\n");356357if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))358return JNI_ERR;359360NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);361if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {362NSK_DISPLAY0(" ... enabled\n");363}364365return JNI_OK;366}367368/* ============================================================================= */369370}371372373