Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/management/GcInfoBuilder.c
38829 views
/*1* Copyright (c) 2003, 2017, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <stdio.h>27#include <jni.h>28#include "management.h"29#include "sun_management_GcInfoBuilder.h"3031JNIEXPORT jint JNICALL Java_sun_management_GcInfoBuilder_getNumGcExtAttributes32(JNIEnv *env, jobject dummy, jobject gc) {33jlong value;3435if (gc == NULL) {36JNU_ThrowNullPointerException(env, "Invalid GarbageCollectorMBean");37return 0;38}39value = jmm_interface->GetLongAttribute(env, gc,40JMM_GC_EXT_ATTRIBUTE_INFO_SIZE);41return (jint) value;42}4344JNIEXPORT void JNICALL Java_sun_management_GcInfoBuilder_fillGcAttributeInfo45(JNIEnv *env, jobject dummy, jobject gc,46jint num_attributes, jobjectArray attributeNames,47jcharArray types, jobjectArray descriptions) {4849jmmExtAttributeInfo* ext_att_info;50jchar* nativeTypes;51jstring attName = NULL;52jstring desc = NULL;53jint ret = 0;54jint i;5556if (gc == NULL) {57JNU_ThrowNullPointerException(env, "Invalid GarbageCollectorMBean");58return;59}6061if (num_attributes <= 0) {62JNU_ThrowIllegalArgumentException(env, "Invalid num_attributes");63return;64}6566ext_att_info = (jmmExtAttributeInfo*) malloc((size_t)num_attributes *67sizeof(jmmExtAttributeInfo));68if (ext_att_info == NULL) {69JNU_ThrowOutOfMemoryError(env, 0);70return;71}72ret = jmm_interface->GetGCExtAttributeInfo(env, gc,73ext_att_info, num_attributes);74if (ret != num_attributes) {75JNU_ThrowInternalError(env, "Unexpected num_attributes");76free(ext_att_info);77return;78}7980nativeTypes = (jchar*) malloc((size_t)num_attributes * sizeof(jchar));81if (nativeTypes == NULL) {82free(ext_att_info);83JNU_ThrowOutOfMemoryError(env, 0);84return;85}86for (i = 0; i < num_attributes; i++) {87nativeTypes[i] = ext_att_info[i].type;88attName = (*env)->NewStringUTF(env, ext_att_info[i].name);89if ((*env)->ExceptionCheck(env)) {90free(ext_att_info);91free(nativeTypes);92return;93}9495(*env)->SetObjectArrayElement(env, attributeNames, i, attName);96if ((*env)->ExceptionCheck(env)) {97free(ext_att_info);98free(nativeTypes);99return;100}101102desc = (*env)->NewStringUTF(env, ext_att_info[i].description);103if ((*env)->ExceptionCheck(env)) {104free(ext_att_info);105free(nativeTypes);106return;107}108109(*env)->SetObjectArrayElement(env, descriptions, i, desc);110if ((*env)->ExceptionCheck(env)) {111free(ext_att_info);112free(nativeTypes);113return;114}115}116(*env)->SetCharArrayRegion(env, types, 0, num_attributes, nativeTypes);117118if (ext_att_info != NULL) {119free(ext_att_info);120}121if (nativeTypes != NULL) {122free(nativeTypes);123}124}125126static void setLongValueAtObjectArray(JNIEnv *env, jobjectArray array,127jsize index, jlong value) {128static const char* class_name = "java/lang/Long";129static const char* signature = "(J)V";130jobject obj = JNU_NewObjectByName(env, class_name, signature, value);131132(*env)->SetObjectArrayElement(env, array, index, obj);133}134135static void setBooleanValueAtObjectArray(JNIEnv *env, jobjectArray array,136jsize index, jboolean value) {137static const char* class_name = "java/lang/Boolean";138static const char* signature = "(Z)V";139jobject obj = JNU_NewObjectByName(env, class_name, signature, value);140141(*env)->SetObjectArrayElement(env, array, index, obj);142}143144static void setByteValueAtObjectArray(JNIEnv *env, jobjectArray array,145jsize index, jbyte value) {146static const char* class_name = "java/lang/Byte";147static const char* signature = "(B)V";148jobject obj = JNU_NewObjectByName(env, class_name, signature, value);149150(*env)->SetObjectArrayElement(env, array, index, obj);151}152153static void setIntValueAtObjectArray(JNIEnv *env, jobjectArray array,154jsize index, jint value) {155static const char* class_name = "java/lang/Integer";156static const char* signature = "(I)V";157jobject obj = JNU_NewObjectByName(env, class_name, signature, value);158159(*env)->SetObjectArrayElement(env, array, index, obj);160}161162static void setShortValueAtObjectArray(JNIEnv *env, jobjectArray array,163jsize index, jshort value) {164static const char* class_name = "java/lang/Short";165static const char* signature = "(S)V";166jobject obj = JNU_NewObjectByName(env, class_name, signature, value);167168(*env)->SetObjectArrayElement(env, array, index, obj);169}170171static void setDoubleValueAtObjectArray(JNIEnv *env, jobjectArray array,172jsize index, jdouble value) {173static const char* class_name = "java/lang/Double";174static const char* signature = "(D)V";175jobject obj = JNU_NewObjectByName(env, class_name, signature, value);176177(*env)->SetObjectArrayElement(env, array, index, obj);178}179180static void setFloatValueAtObjectArray(JNIEnv *env, jobjectArray array,181jsize index, jfloat value) {182static const char* class_name = "java/lang/Float";183static const char* signature = "(D)V";184jobject obj = JNU_NewObjectByName(env, class_name, signature, value);185186(*env)->SetObjectArrayElement(env, array, index, obj);187}188189static void setCharValueAtObjectArray(JNIEnv *env, jobjectArray array,190jsize index, jchar value) {191static const char* class_name = "java/lang/Character";192static const char* signature = "(C)V";193jobject obj = JNU_NewObjectByName(env, class_name, signature, value);194195(*env)->SetObjectArrayElement(env, array, index, obj);196}197198JNIEXPORT jobject JNICALL Java_sun_management_GcInfoBuilder_getLastGcInfo0199(JNIEnv *env, jobject builder, jobject gc,200jint ext_att_count, jobjectArray ext_att_values, jcharArray ext_att_types,201jobjectArray usageBeforeGC, jobjectArray usageAfterGC) {202203jmmGCStat gc_stat;204jchar* nativeTypes;205jsize i;206jvalue v;207208if (gc == NULL) {209JNU_ThrowNullPointerException(env, "Invalid GarbageCollectorMBean");210return 0;211}212213if (ext_att_count <= 0) {214JNU_ThrowIllegalArgumentException(env, "Invalid ext_att_count");215return 0;216}217218gc_stat.usage_before_gc = usageBeforeGC;219gc_stat.usage_after_gc = usageAfterGC;220gc_stat.gc_ext_attribute_values_size = ext_att_count;221if (ext_att_count > 0) {222gc_stat.gc_ext_attribute_values = (jvalue*) malloc((size_t)ext_att_count *223sizeof(jvalue));224if (gc_stat.gc_ext_attribute_values == NULL) {225JNU_ThrowOutOfMemoryError(env, 0);226return 0;227}228} else {229gc_stat.gc_ext_attribute_values = NULL;230}231232233jmm_interface->GetLastGCStat(env, gc, &gc_stat);234if (gc_stat.gc_index == 0) {235if (gc_stat.gc_ext_attribute_values != NULL) {236free(gc_stat.gc_ext_attribute_values);237}238return 0;239}240241// convert the ext_att_types to native types242nativeTypes = (jchar*) malloc((size_t)ext_att_count * sizeof(jchar));243if (nativeTypes == NULL) {244if (gc_stat.gc_ext_attribute_values != NULL) {245free(gc_stat.gc_ext_attribute_values);246}247JNU_ThrowOutOfMemoryError(env, 0);248return 0;249}250(*env)->GetCharArrayRegion(env, ext_att_types, 0, ext_att_count, nativeTypes);251for (i = 0; i < ext_att_count; i++) {252v = gc_stat.gc_ext_attribute_values[i];253switch (nativeTypes[i]) {254case 'Z':255setBooleanValueAtObjectArray(env, ext_att_values, i, v.z);256break;257case 'B':258setByteValueAtObjectArray(env, ext_att_values, i, v.b);259break;260case 'C':261setCharValueAtObjectArray(env, ext_att_values, i, v.c);262break;263case 'S':264setShortValueAtObjectArray(env, ext_att_values, i, v.s);265break;266case 'I':267setIntValueAtObjectArray(env, ext_att_values, i, v.i);268break;269case 'J':270setLongValueAtObjectArray(env, ext_att_values, i, v.j);271break;272case 'F':273setFloatValueAtObjectArray(env, ext_att_values, i, v.f);274break;275case 'D':276setDoubleValueAtObjectArray(env, ext_att_values, i, v.d);277break;278default:279if (gc_stat.gc_ext_attribute_values != NULL) {280free(gc_stat.gc_ext_attribute_values);281}282if (nativeTypes != NULL) {283free(nativeTypes);284}285JNU_ThrowInternalError(env, "Unsupported attribute type");286return 0;287}288}289if (gc_stat.gc_ext_attribute_values != NULL) {290free(gc_stat.gc_ext_attribute_values);291}292if (nativeTypes != NULL) {293free(nativeTypes);294}295296return JNU_NewObjectByName(env,297"com/sun/management/GcInfo",298"(Lsun/management/GcInfoBuilder;JJJ[Ljava/lang/management/MemoryUsage;[Ljava/lang/management/MemoryUsage;[Ljava/lang/Object;)V",299builder,300gc_stat.gc_index,301gc_stat.start_time,302gc_stat.end_time,303usageBeforeGC,304usageAfterGC,305ext_att_values);306}307308309