annotate src/share/native/sun/management/GcInfoBuilder.c @ 9359:343f4f8ba098

8029735: Enhance service mgmt natives Reviewed-by: sla, mschoene
author jbachorik
date Mon, 23 Dec 2013 15:33:11 +0100
parents 00cd9dc3c2b5
children b91cf8924877
rev   line source
duke@0 1 /*
ohair@2362 2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2362 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2362 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2362 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2362 22 * or visit www.oracle.com if you need additional information or have any
ohair@2362 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 #include <stdlib.h>
duke@0 27 #include <stdio.h>
duke@0 28 #include <jni.h>
duke@0 29 #include "management.h"
duke@0 30 #include "sun_management_GcInfoBuilder.h"
duke@0 31
duke@0 32 JNIEXPORT jint JNICALL Java_sun_management_GcInfoBuilder_getNumGcExtAttributes
duke@0 33 (JNIEnv *env, jobject dummy, jobject gc) {
duke@0 34 jlong value;
duke@0 35
duke@0 36 if (gc == NULL) {
duke@0 37 JNU_ThrowNullPointerException(env, "Invalid GarbageCollectorMBean");
duke@0 38 return 0;
duke@0 39 }
duke@0 40 value = jmm_interface->GetLongAttribute(env, gc,
duke@0 41 JMM_GC_EXT_ATTRIBUTE_INFO_SIZE);
duke@0 42 return (jint) value;
duke@0 43 }
duke@0 44
duke@0 45 JNIEXPORT void JNICALL Java_sun_management_GcInfoBuilder_fillGcAttributeInfo
duke@0 46 (JNIEnv *env, jobject dummy, jobject gc,
duke@0 47 jint num_attributes, jobjectArray attributeNames,
duke@0 48 jcharArray types, jobjectArray descriptions) {
duke@0 49
duke@0 50 jmmExtAttributeInfo* ext_att_info;
duke@0 51 jchar* nativeTypes;
duke@0 52 jstring attName = NULL;
duke@0 53 jstring desc = NULL;
duke@0 54 jint ret = 0;
duke@0 55 jint i;
duke@0 56
duke@0 57 if (gc == NULL) {
duke@0 58 JNU_ThrowNullPointerException(env, "Invalid GarbageCollectorMBean");
duke@0 59 return;
duke@0 60 }
duke@0 61
jbachorik@9359 62 if (num_attributes <= 0) {
duke@0 63 JNU_ThrowIllegalArgumentException(env, "Invalid num_attributes");
duke@0 64 return;
duke@0 65 }
duke@0 66
jbachorik@9359 67 ext_att_info = (jmmExtAttributeInfo*) malloc((size_t)num_attributes *
duke@0 68 sizeof(jmmExtAttributeInfo));
duke@0 69 if (ext_att_info == NULL) {
duke@0 70 JNU_ThrowOutOfMemoryError(env, 0);
duke@0 71 return;
duke@0 72 }
duke@0 73 ret = jmm_interface->GetGCExtAttributeInfo(env, gc,
duke@0 74 ext_att_info, num_attributes);
duke@0 75 if (ret != num_attributes) {
duke@0 76 JNU_ThrowInternalError(env, "Unexpected num_attributes");
duke@0 77 free(ext_att_info);
duke@0 78 return;
duke@0 79 }
duke@0 80
jbachorik@9359 81 nativeTypes = (jchar*) malloc((size_t)num_attributes * sizeof(jchar));
duke@0 82 if (nativeTypes == NULL) {
duke@0 83 free(ext_att_info);
duke@0 84 JNU_ThrowOutOfMemoryError(env, 0);
duke@0 85 return;
duke@0 86 }
duke@0 87 for (i = 0; i < num_attributes; i++) {
duke@0 88 nativeTypes[i] = ext_att_info[i].type;
duke@0 89 attName = (*env)->NewStringUTF(env, ext_att_info[i].name);
duke@0 90 desc = (*env)->NewStringUTF(env, ext_att_info[i].description);
duke@0 91 (*env)->SetObjectArrayElement(env, attributeNames, i, attName);
duke@0 92 (*env)->SetObjectArrayElement(env, descriptions, i, desc);
duke@0 93 }
duke@0 94 (*env)->SetCharArrayRegion(env, types, 0, num_attributes, nativeTypes);
duke@0 95
duke@0 96 if (ext_att_info != NULL) {
duke@0 97 free(ext_att_info);
duke@0 98 }
duke@0 99 if (nativeTypes != NULL) {
duke@0 100 free(nativeTypes);
duke@0 101 }
duke@0 102 }
duke@0 103
duke@0 104 static void setLongValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 105 jsize index, jlong value) {
duke@0 106 static const char* class_name = "java/lang/Long";
duke@0 107 static const char* signature = "(J)V";
duke@0 108 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 109
duke@0 110 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 111 }
duke@0 112
duke@0 113 static void setBooleanValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 114 jsize index, jboolean value) {
duke@0 115 static const char* class_name = "java/lang/Boolean";
duke@0 116 static const char* signature = "(Z)V";
duke@0 117 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 118
duke@0 119 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 120 }
duke@0 121
duke@0 122 static void setByteValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 123 jsize index, jbyte value) {
duke@0 124 static const char* class_name = "java/lang/Byte";
duke@0 125 static const char* signature = "(B)V";
duke@0 126 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 127
duke@0 128 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 129 }
duke@0 130
duke@0 131 static void setIntValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 132 jsize index, jint value) {
duke@0 133 static const char* class_name = "java/lang/Integer";
duke@0 134 static const char* signature = "(I)V";
duke@0 135 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 136
duke@0 137 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 138 }
duke@0 139
duke@0 140 static void setShortValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 141 jsize index, jshort value) {
duke@0 142 static const char* class_name = "java/lang/Short";
duke@0 143 static const char* signature = "(S)V";
duke@0 144 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 145
duke@0 146 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 147 }
duke@0 148
duke@0 149 static void setDoubleValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 150 jsize index, jdouble value) {
duke@0 151 static const char* class_name = "java/lang/Double";
duke@0 152 static const char* signature = "(D)V";
duke@0 153 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 154
duke@0 155 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 156 }
duke@0 157
duke@0 158 static void setFloatValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 159 jsize index, jfloat value) {
duke@0 160 static const char* class_name = "java/lang/Float";
duke@0 161 static const char* signature = "(D)V";
duke@0 162 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 163
duke@0 164 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 165 }
duke@0 166
duke@0 167 static void setCharValueAtObjectArray(JNIEnv *env, jobjectArray array,
duke@0 168 jsize index, jchar value) {
duke@0 169 static const char* class_name = "java/lang/Character";
duke@0 170 static const char* signature = "(C)V";
duke@0 171 jobject obj = JNU_NewObjectByName(env, class_name, signature, value);
duke@0 172
duke@0 173 (*env)->SetObjectArrayElement(env, array, index, obj);
duke@0 174 }
duke@0 175
duke@0 176 JNIEXPORT jobject JNICALL Java_sun_management_GcInfoBuilder_getLastGcInfo0
duke@0 177 (JNIEnv *env, jobject builder, jobject gc,
duke@0 178 jint ext_att_count, jobjectArray ext_att_values, jcharArray ext_att_types,
duke@0 179 jobjectArray usageBeforeGC, jobjectArray usageAfterGC) {
duke@0 180
duke@0 181 jmmGCStat gc_stat;
duke@0 182 jchar* nativeTypes;
duke@0 183 jsize i;
duke@0 184 jvalue v;
duke@0 185
duke@0 186 if (gc == NULL) {
duke@0 187 JNU_ThrowNullPointerException(env, "Invalid GarbageCollectorMBean");
duke@0 188 return 0;
duke@0 189 }
duke@0 190
jbachorik@9359 191 if (ext_att_count <= 0) {
jbachorik@9359 192 JNU_ThrowIllegalArgumentException(env, "Invalid ext_att_count");
jbachorik@9359 193 return;
jbachorik@9359 194 }
jbachorik@9359 195
duke@0 196 gc_stat.usage_before_gc = usageBeforeGC;
duke@0 197 gc_stat.usage_after_gc = usageAfterGC;
duke@0 198 gc_stat.gc_ext_attribute_values_size = ext_att_count;
duke@0 199 if (ext_att_count > 0) {
jbachorik@9359 200 gc_stat.gc_ext_attribute_values = (jvalue*) malloc((size_t)ext_att_count *
duke@0 201 sizeof(jvalue));
duke@0 202 if (gc_stat.gc_ext_attribute_values == NULL) {
duke@0 203 JNU_ThrowOutOfMemoryError(env, 0);
duke@0 204 return 0;
duke@0 205 }
duke@0 206 } else {
duke@0 207 gc_stat.gc_ext_attribute_values = NULL;
duke@0 208 }
duke@0 209
duke@0 210
duke@0 211 jmm_interface->GetLastGCStat(env, gc, &gc_stat);
duke@0 212 if (gc_stat.gc_index == 0) {
duke@0 213 if (gc_stat.gc_ext_attribute_values != NULL) {
duke@0 214 free(gc_stat.gc_ext_attribute_values);
duke@0 215 }
duke@0 216 return 0;
duke@0 217 }
duke@0 218
duke@0 219 // convert the ext_att_types to native types
jbachorik@9359 220 nativeTypes = (jchar*) malloc((size_t)ext_att_count * sizeof(jchar));
duke@0 221 if (nativeTypes == NULL) {
duke@0 222 if (gc_stat.gc_ext_attribute_values != NULL) {
duke@0 223 free(gc_stat.gc_ext_attribute_values);
duke@0 224 }
duke@0 225 JNU_ThrowOutOfMemoryError(env, 0);
duke@0 226 return 0;
duke@0 227 }
duke@0 228 (*env)->GetCharArrayRegion(env, ext_att_types, 0, ext_att_count, nativeTypes);
duke@0 229 for (i = 0; i < ext_att_count; i++) {
duke@0 230 v = gc_stat.gc_ext_attribute_values[i];
duke@0 231 switch (nativeTypes[i]) {
duke@0 232 case 'Z':
duke@0 233 setBooleanValueAtObjectArray(env, ext_att_values, i, v.z);
duke@0 234 break;
duke@0 235 case 'B':
duke@0 236 setByteValueAtObjectArray(env, ext_att_values, i, v.b);
duke@0 237 break;
duke@0 238 case 'C':
duke@0 239 setCharValueAtObjectArray(env, ext_att_values, i, v.c);
duke@0 240 break;
duke@0 241 case 'S':
duke@0 242 setShortValueAtObjectArray(env, ext_att_values, i, v.s);
duke@0 243 break;
duke@0 244 case 'I':
duke@0 245 setIntValueAtObjectArray(env, ext_att_values, i, v.i);
duke@0 246 break;
duke@0 247 case 'J':
duke@0 248 setLongValueAtObjectArray(env, ext_att_values, i, v.j);
duke@0 249 break;
duke@0 250 case 'F':
duke@0 251 setFloatValueAtObjectArray(env, ext_att_values, i, v.f);
duke@0 252 break;
duke@0 253 case 'D':
duke@0 254 setDoubleValueAtObjectArray(env, ext_att_values, i, v.d);
duke@0 255 break;
duke@0 256 default:
duke@0 257 if (gc_stat.gc_ext_attribute_values != NULL) {
duke@0 258 free(gc_stat.gc_ext_attribute_values);
duke@0 259 }
duke@0 260 if (nativeTypes != NULL) {
duke@0 261 free(nativeTypes);
duke@0 262 }
duke@0 263 JNU_ThrowInternalError(env, "Unsupported attribute type");
duke@0 264 return 0;
duke@0 265 }
duke@0 266 }
duke@0 267 if (gc_stat.gc_ext_attribute_values != NULL) {
duke@0 268 free(gc_stat.gc_ext_attribute_values);
duke@0 269 }
duke@0 270 if (nativeTypes != NULL) {
duke@0 271 free(nativeTypes);
duke@0 272 }
duke@0 273
duke@0 274 return JNU_NewObjectByName(env,
duke@0 275 "com/sun/management/GcInfo",
duke@0 276 "(Lsun/management/GcInfoBuilder;JJJ[Ljava/lang/management/MemoryUsage;[Ljava/lang/management/MemoryUsage;[Ljava/lang/Object;)V",
duke@0 277 builder,
duke@0 278 gc_stat.gc_index,
duke@0 279 gc_stat.start_time,
duke@0 280 gc_stat.end_time,
duke@0 281 usageBeforeGC,
duke@0 282 usageAfterGC,
duke@0 283 ext_att_values);
duke@0 284 }