xref: /illumos-gate/usr/src/cmd/pools/poold/libjkstat/jkstat.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*414388d7Ssl108498  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stddef.h>
287c478bd9Sstevel@tonic-gate #include <kstat.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "jkstat.h"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Class descriptors
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #define	DOUBLE_CLASS_DESC	"java/lang/Double"
367c478bd9Sstevel@tonic-gate #define	LONG_CLASS_DESC		"java/lang/Long"
377c478bd9Sstevel@tonic-gate #define	UI64_CLASS_DESC		"com/sun/solaris/service/pools/UnsignedInt64"
387c478bd9Sstevel@tonic-gate #define	HRTIME_CLASS_DESC	"com/sun/solaris/service/pools/HRTime"
397c478bd9Sstevel@tonic-gate #define	KSTAT_CLASS_DESC	"com/sun/solaris/service/kstat/Kstat"
407c478bd9Sstevel@tonic-gate #define	KSTATCTL_CLASS_DESC	"com/sun/solaris/service/kstat/KstatCtl"
417c478bd9Sstevel@tonic-gate #define	KSTAT_READ_EX_CLASS_DESC \
427c478bd9Sstevel@tonic-gate 	"com/sun/solaris/service/kstat/KstatReadException"
437c478bd9Sstevel@tonic-gate #define	KSTAT_TNS_EX_CLASS_DESC	\
447c478bd9Sstevel@tonic-gate 	"com/sun/solaris/service/kstat/KstatTypeNotSupportedException"
457c478bd9Sstevel@tonic-gate #define	THROWABLE_CLASS_DESC	"java/lang/Throwable"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define	CLASS_FIELD_DESC(class_desc)	"L" class_desc ";"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Cached class, method, and field IDs.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate static jclass doubleclass;
537c478bd9Sstevel@tonic-gate static jclass hrtimeclass;
547c478bd9Sstevel@tonic-gate static jclass kstatclass;
557c478bd9Sstevel@tonic-gate static jclass kstatctlclass;
567c478bd9Sstevel@tonic-gate static jclass longclass;
577c478bd9Sstevel@tonic-gate static jclass ui64class;
587c478bd9Sstevel@tonic-gate static jfieldID kstat_kctl_fieldid;
597c478bd9Sstevel@tonic-gate static jfieldID kstat_ksp_fieldid;
607c478bd9Sstevel@tonic-gate static jfieldID kstatctl_kctl_fieldid;
617c478bd9Sstevel@tonic-gate static jmethodID doublecons_mid;
627c478bd9Sstevel@tonic-gate static jmethodID hrtimecons_mid;
637c478bd9Sstevel@tonic-gate static jmethodID kstatcons_mid;
647c478bd9Sstevel@tonic-gate static jmethodID longcons_mid;
657c478bd9Sstevel@tonic-gate static jmethodID ui64cons_mid;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static jobject
makeUnsignedInt64(JNIEnv * env,uint64_t value)687c478bd9Sstevel@tonic-gate makeUnsignedInt64(JNIEnv *env, uint64_t value)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	jobject valueObj;
717c478bd9Sstevel@tonic-gate 	jobject byteArray;
727c478bd9Sstevel@tonic-gate 	jbyte *bytes;
737c478bd9Sstevel@tonic-gate 	int i;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (!(byteArray = (*env)->NewByteArray(env, 9)))
767c478bd9Sstevel@tonic-gate 		return (NULL); /* OutOfMemoryError thrown */
777c478bd9Sstevel@tonic-gate 	if (!(bytes = (*env)->GetByteArrayElements(env, byteArray, NULL)))
787c478bd9Sstevel@tonic-gate 		return (NULL); /* OutOfMemoryError thrown */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * Interpret the uint64_t as a 9-byte big-endian signed quantity
827c478bd9Sstevel@tonic-gate 	 * suitable for constructing an UnsignedInt64 or BigInteger.
837c478bd9Sstevel@tonic-gate 	 */
847c478bd9Sstevel@tonic-gate 	for (i = 8; i >= 1; i--) {
857c478bd9Sstevel@tonic-gate 		bytes[i] = value & 0xff;
867c478bd9Sstevel@tonic-gate 		value >>= 8;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	bytes[0] = 0;
897c478bd9Sstevel@tonic-gate 	(*env)->ReleaseByteArrayElements(env, byteArray, bytes, 0);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (!(valueObj = (*env)->NewObject(env, ui64class, ui64cons_mid,
927c478bd9Sstevel@tonic-gate 	    byteArray)))
937c478bd9Sstevel@tonic-gate 		return (NULL); /* exception thrown */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	return (valueObj);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Return a Long object with the given value.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static jobject
makeLong(JNIEnv * env,jlong value)1027c478bd9Sstevel@tonic-gate makeLong(JNIEnv *env, jlong value)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	jobject valueObj;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	if (!(valueObj = (*env)->NewObject(env, longclass, longcons_mid,
1077c478bd9Sstevel@tonic-gate 	    value)))
1087c478bd9Sstevel@tonic-gate 		return (NULL); /* exception thrown */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	return (valueObj);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Return a Double object with the given value.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate static jobject
makeDouble(JNIEnv * env,jdouble value)1177c478bd9Sstevel@tonic-gate makeDouble(JNIEnv *env, jdouble value)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	jobject valueObj;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (!(valueObj = (*env)->NewObject(env, doubleclass, doublecons_mid,
1227c478bd9Sstevel@tonic-gate 	    value)))
1237c478bd9Sstevel@tonic-gate 		return (NULL); /* exception thrown */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	return (valueObj);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Returns the kctl_t * from kstat_open(3kstat).
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1327c478bd9Sstevel@tonic-gate JNIEXPORT jlong JNICALL
Java_com_sun_solaris_service_kstat_KstatCtl_open(JNIEnv * env,jobject obj)1337c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_KstatCtl_open(JNIEnv *env, jobject obj)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	return ((jlong)(uintptr_t)kstat_open());
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Invokes kstat_close(3kstat).
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1427c478bd9Sstevel@tonic-gate JNIEXPORT jint JNICALL
Java_com_sun_solaris_service_kstat_KstatCtl_close(JNIEnv * env,jobject obj,jlong kctl)1437c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_KstatCtl_close(JNIEnv *env, jobject obj,
1447c478bd9Sstevel@tonic-gate     jlong kctl)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	if (kctl)
147*414388d7Ssl108498 		return (kstat_close((kstat_ctl_t *)(uintptr_t)kctl));
1487c478bd9Sstevel@tonic-gate 	else
1497c478bd9Sstevel@tonic-gate 		return (0);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Invoke kstat_read(3kstat) for the given Kstat object.
1547c478bd9Sstevel@tonic-gate  */
Java_com_sun_solaris_service_kstat_Kstat_read(JNIEnv * env,jobject obj)1557c478bd9Sstevel@tonic-gate JNIEXPORT void JNICALL Java_com_sun_solaris_service_kstat_Kstat_read(
1567c478bd9Sstevel@tonic-gate     JNIEnv *env, jobject obj)
1577c478bd9Sstevel@tonic-gate {
158*414388d7Ssl108498 	kstat_ctl_t *kctl =
159*414388d7Ssl108498 	    ((kstat_ctl_t *)(uintptr_t)(*env)->GetLongField(env, obj,
1607c478bd9Sstevel@tonic-gate 	    kstat_kctl_fieldid));
161*414388d7Ssl108498 	kstat_t *ksp = ((kstat_t *)(uintptr_t)(*env)->GetLongField(env, obj,
1627c478bd9Sstevel@tonic-gate 	    kstat_ksp_fieldid));
1637c478bd9Sstevel@tonic-gate 	kid_t kid;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	if (!ksp || !kctl)
1667c478bd9Sstevel@tonic-gate 		return; /* exception thronw */
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	kid = kstat_read((kstat_ctl_t *)kctl, (kstat_t *)ksp, NULL);
1697c478bd9Sstevel@tonic-gate 	if (kid == -1) {
1707c478bd9Sstevel@tonic-gate 		jclass e;
1717c478bd9Sstevel@tonic-gate 		if (!(e = (*env)->FindClass(env, KSTAT_READ_EX_CLASS_DESC)))
1727c478bd9Sstevel@tonic-gate 			return; /* exception thrown */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 		(*env)->Throw(env, (*env)->NewObject(env, e,
1757c478bd9Sstevel@tonic-gate 		    (*env)->GetStaticMethodID(env, e, "<init>",
1767c478bd9Sstevel@tonic-gate 		    "()" CLASS_FIELD_DESC(THROWABLE_CLASS_DESC))));
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * Return a Kstat object corresponding to the result of
1827c478bd9Sstevel@tonic-gate  * kstat_lookup(3kstat).
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate JNIEXPORT jobject JNICALL
Java_com_sun_solaris_service_kstat_KstatCtl_lookup(JNIEnv * env,jobject obj,jstring moduleObj,jint instance,jstring nameObj)1857c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_KstatCtl_lookup(JNIEnv *env, jobject obj,
1867c478bd9Sstevel@tonic-gate     jstring moduleObj, jint instance, jstring nameObj)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	const char *module = NULL;
1897c478bd9Sstevel@tonic-gate 	const char *name = NULL;
1907c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kctl;
1917c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
1927c478bd9Sstevel@tonic-gate 	jobject kstatObject = NULL;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	if (moduleObj == NULL || nameObj == NULL)
1957c478bd9Sstevel@tonic-gate 		return (NULL);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (!(module = (*env)->GetStringUTFChars(env, moduleObj, NULL)))
1987c478bd9Sstevel@tonic-gate 		goto done; /* exception thrown */
1997c478bd9Sstevel@tonic-gate 	if (!(name = (*env)->GetStringUTFChars(env, nameObj, NULL)))
2007c478bd9Sstevel@tonic-gate 		goto done; /* exception thrown */
2017c478bd9Sstevel@tonic-gate 
202*414388d7Ssl108498 	kctl = (kstat_ctl_t *)(uintptr_t)(*env)->GetLongField(env, obj,
2037c478bd9Sstevel@tonic-gate 	    kstatctl_kctl_fieldid);
2047c478bd9Sstevel@tonic-gate 	ksp = kstat_lookup(kctl, (char *)module, instance, (char *)name);
2057c478bd9Sstevel@tonic-gate 	if (ksp)
2067c478bd9Sstevel@tonic-gate 		kstatObject = (*env)->NewObject(env, kstatclass, kstatcons_mid,
2077c478bd9Sstevel@tonic-gate 		    (jlong)(uintptr_t)kctl, (jlong)(uintptr_t)ksp);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate done:
2107c478bd9Sstevel@tonic-gate 	if (name)
2117c478bd9Sstevel@tonic-gate 		(*env)->ReleaseStringUTFChars(env, nameObj, name);
2127c478bd9Sstevel@tonic-gate 	if (module)
2137c478bd9Sstevel@tonic-gate 		(*env)->ReleaseStringUTFChars(env, moduleObj, module);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	return (kstatObject);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Returns the named value -- the value of the named kstat, or field in
2207c478bd9Sstevel@tonic-gate  * a raw kstat, as applicable, and available.  Returns <i>null</i> if no
2217c478bd9Sstevel@tonic-gate  * such named kstat or field is available.
2227c478bd9Sstevel@tonic-gate  *
2237c478bd9Sstevel@tonic-gate  * Throws KstatTypeNotSupportedException if the raw kstat is not
2247c478bd9Sstevel@tonic-gate  * understood.  (Presently, none are.)
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate JNIEXPORT jobject JNICALL
Java_com_sun_solaris_service_kstat_Kstat_getValue(JNIEnv * env,jobject obj,jstring nameObj)2277c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_Kstat_getValue(JNIEnv *env, jobject obj,
2287c478bd9Sstevel@tonic-gate     jstring nameObj)
2297c478bd9Sstevel@tonic-gate {
230*414388d7Ssl108498 	kstat_t *ksp = ((kstat_t *)(uintptr_t)(*env)->GetLongField(env, obj,
2317c478bd9Sstevel@tonic-gate 	    kstat_ksp_fieldid));
2327c478bd9Sstevel@tonic-gate 	jobject valueObj = NULL;
2337c478bd9Sstevel@tonic-gate 	kstat_named_t *ksnp;
2347c478bd9Sstevel@tonic-gate 	const char *name;
2357c478bd9Sstevel@tonic-gate 	jclass exceptionClass;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (!nameObj)
2387c478bd9Sstevel@tonic-gate 		return (NULL);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (!(name = (*env)->GetStringUTFChars(env, nameObj, NULL)))
2417c478bd9Sstevel@tonic-gate 		return (NULL); /* exception thrown */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (!(exceptionClass = (*env)->FindClass(env,
2447c478bd9Sstevel@tonic-gate 	    KSTAT_TNS_EX_CLASS_DESC))) {
2457c478bd9Sstevel@tonic-gate 		(*env)->ReleaseStringUTFChars(env, nameObj, name);
2467c478bd9Sstevel@tonic-gate 		return (NULL); /* exception thrown */
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	switch (ksp->ks_type) {
2507c478bd9Sstevel@tonic-gate 	case KSTAT_TYPE_NAMED:
2517c478bd9Sstevel@tonic-gate 		ksnp = kstat_data_lookup(ksp, (char *)name);
2527c478bd9Sstevel@tonic-gate 		if (ksnp == NULL)
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		switch (ksnp->data_type) {
2557c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_CHAR:
2567c478bd9Sstevel@tonic-gate 			valueObj = makeLong(env, ksnp->value.c[0]);
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_INT32:
2597c478bd9Sstevel@tonic-gate 			valueObj = makeLong(env, ksnp->value.i32);
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_UINT32:
2627c478bd9Sstevel@tonic-gate 			valueObj = makeLong(env, ksnp->value.ui32);
2637c478bd9Sstevel@tonic-gate 			break;
2647c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_INT64:
2657c478bd9Sstevel@tonic-gate 			valueObj = makeLong(env, ksnp->value.i64);
2667c478bd9Sstevel@tonic-gate 			break;
2677c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_UINT64:
2687c478bd9Sstevel@tonic-gate 			valueObj = makeUnsignedInt64(env, ksnp->value.ui64);
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_STRING:
2717c478bd9Sstevel@tonic-gate 			valueObj = (*env)->NewStringUTF(env,
2727c478bd9Sstevel@tonic-gate 			    KSTAT_NAMED_STR_PTR(ksnp));
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_FLOAT:
2757c478bd9Sstevel@tonic-gate 			valueObj = makeDouble(env, ksnp->value.f);
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 		case KSTAT_DATA_DOUBLE:
2787c478bd9Sstevel@tonic-gate 			valueObj = makeDouble(env, ksnp->value.d);
2797c478bd9Sstevel@tonic-gate 			break;
2807c478bd9Sstevel@tonic-gate 		default:
2817c478bd9Sstevel@tonic-gate 			goto fail;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		break;
2847c478bd9Sstevel@tonic-gate 	default:
2857c478bd9Sstevel@tonic-gate 		goto fail;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	(*env)->ReleaseStringUTFChars(env, nameObj, name);
2897c478bd9Sstevel@tonic-gate 	return (valueObj);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate fail:
2927c478bd9Sstevel@tonic-gate 	(*env)->ReleaseStringUTFChars(env, nameObj, name);
2937c478bd9Sstevel@tonic-gate 	(*env)->Throw(env, (*env)->NewObject(env, exceptionClass,
2947c478bd9Sstevel@tonic-gate 	    (*env)->GetStaticMethodID(env, exceptionClass, "<init>",
2957c478bd9Sstevel@tonic-gate 	    "()" CLASS_FIELD_DESC(THROWABLE_CLASS_DESC))));
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	return (valueObj);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * Given a Kstat object, return, as an HRTime object, its kstat_t's
3027c478bd9Sstevel@tonic-gate  * field at the given offset.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate static jobject
ksobj_get_hrtime(JNIEnv * env,jobject obj,offset_t ksfieldoff)3057c478bd9Sstevel@tonic-gate ksobj_get_hrtime(JNIEnv *env, jobject obj, offset_t ksfieldoff)
3067c478bd9Sstevel@tonic-gate {
307*414388d7Ssl108498 	kstat_t *ksp = ((kstat_t *)(uintptr_t)(*env)->GetLongField(env, obj,
3087c478bd9Sstevel@tonic-gate 	    kstat_ksp_fieldid));
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if (!ksp)
3117c478bd9Sstevel@tonic-gate 		return (NULL); /* exception thrown */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	return ((*env)->NewObject(env, hrtimeclass, hrtimecons_mid,
3147c478bd9Sstevel@tonic-gate 	    makeUnsignedInt64(env, *((hrtime_t *)ksp + ksfieldoff *
3157c478bd9Sstevel@tonic-gate 	    sizeof (hrtime_t)))));
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * Given a Kstat object, return as an HRTime object its ks_snaptime
3207c478bd9Sstevel@tonic-gate  * field.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate JNIEXPORT jobject JNICALL
Java_com_sun_solaris_service_kstat_Kstat_getSnapTime(JNIEnv * env,jobject obj)3237c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_Kstat_getSnapTime(JNIEnv *env, jobject obj)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	return (ksobj_get_hrtime(env, obj, offsetof(kstat_t, ks_snaptime)));
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate /*
3297c478bd9Sstevel@tonic-gate  * Given a Kstat object, return as an HRTime object its ks_crtime
3307c478bd9Sstevel@tonic-gate  * field.
3317c478bd9Sstevel@tonic-gate  */
3327c478bd9Sstevel@tonic-gate JNIEXPORT jobject JNICALL
Java_com_sun_solaris_service_kstat_Kstat_getCreationTime(JNIEnv * env,jobject obj)3337c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_Kstat_getCreationTime(JNIEnv *env,
3347c478bd9Sstevel@tonic-gate     jobject obj)
3357c478bd9Sstevel@tonic-gate {
3367c478bd9Sstevel@tonic-gate 	return (ksobj_get_hrtime(env, obj, offsetof(kstat_t, ks_crtime)));
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * Invoke kstat_chain_update(3kstat) for the kstat chain corresponding
3417c478bd9Sstevel@tonic-gate  * to the given KstatCtl object.
3427c478bd9Sstevel@tonic-gate  */
3437c478bd9Sstevel@tonic-gate JNIEXPORT void JNICALL
Java_com_sun_solaris_service_kstat_KstatCtl_chainUpdate(JNIEnv * env,jobject obj)3447c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_KstatCtl_chainUpdate(JNIEnv *env,
3457c478bd9Sstevel@tonic-gate     jobject obj)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	kstat_ctl_t *kctl;
3487c478bd9Sstevel@tonic-gate 
349*414388d7Ssl108498 	kctl = (kstat_ctl_t *)(uintptr_t)(*env)->GetLongField(env, obj,
3507c478bd9Sstevel@tonic-gate 	    kstatctl_kctl_fieldid);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	(void) kstat_chain_update(kctl);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * Cache class, method, and field IDs.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3597c478bd9Sstevel@tonic-gate JNIEXPORT void JNICALL
Java_com_sun_solaris_service_kstat_KstatCtl_init(JNIEnv * env,jclass clazz)3607c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_kstat_KstatCtl_init(JNIEnv *env, jclass clazz)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	jclass doubleclass_lref;
3637c478bd9Sstevel@tonic-gate 	jclass hrtimeclass_lref;
3647c478bd9Sstevel@tonic-gate 	jclass kstatclass_lref;
3657c478bd9Sstevel@tonic-gate 	jclass kstatctlclass_lref;
3667c478bd9Sstevel@tonic-gate 	jclass longclass_lref;
3677c478bd9Sstevel@tonic-gate 	jclass ui64class_lref;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (!(doubleclass_lref = (*env)->FindClass(env, DOUBLE_CLASS_DESC)))
3707c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3717c478bd9Sstevel@tonic-gate 	if (!(doubleclass = (*env)->NewGlobalRef(env, doubleclass_lref)))
3727c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3737c478bd9Sstevel@tonic-gate 	if (!(doublecons_mid = (*env)->GetMethodID(env, doubleclass, "<init>",
3747c478bd9Sstevel@tonic-gate 	    "(D)V")))
3757c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if (!(hrtimeclass_lref = (*env)->FindClass(env, HRTIME_CLASS_DESC)))
3787c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3797c478bd9Sstevel@tonic-gate 	if (!(hrtimeclass = (*env)->NewGlobalRef(env, hrtimeclass_lref)))
3807c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3817c478bd9Sstevel@tonic-gate 	if (!(hrtimecons_mid = (*env)->GetMethodID(env, hrtimeclass, "<init>",
3827c478bd9Sstevel@tonic-gate 	    "(" CLASS_FIELD_DESC(UI64_CLASS_DESC) ")V")))
3837c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (!(kstatclass_lref = (*env)->FindClass(env, KSTAT_CLASS_DESC)))
3867c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3877c478bd9Sstevel@tonic-gate 	if (!(kstatclass = (*env)->NewGlobalRef(env, kstatclass_lref)))
3887c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3897c478bd9Sstevel@tonic-gate 	if (!(kstatcons_mid = (*env)->GetMethodID(env, kstatclass, "<init>",
3907c478bd9Sstevel@tonic-gate 	    "(JJ)V")))
3917c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3927c478bd9Sstevel@tonic-gate 	if (!(kstat_kctl_fieldid = (*env)->GetFieldID(env, kstatclass, "kctl",
3937c478bd9Sstevel@tonic-gate 	    "J")))
3947c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3957c478bd9Sstevel@tonic-gate 	if (!(kstat_ksp_fieldid = (*env)->GetFieldID(env, kstatclass, "ksp",
3967c478bd9Sstevel@tonic-gate 	    "J")))
3977c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	if (!(kstatctlclass_lref = (*env)->FindClass(env, KSTATCTL_CLASS_DESC)))
4007c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4017c478bd9Sstevel@tonic-gate 	if (!(kstatctlclass = (*env)->NewGlobalRef(env, kstatctlclass_lref)))
4027c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4037c478bd9Sstevel@tonic-gate 	if (!(kstatctl_kctl_fieldid = (*env)->GetFieldID(env, kstatctlclass,
4047c478bd9Sstevel@tonic-gate 	    "kctl", "J")))
4057c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (!(longclass_lref = (*env)->FindClass(env, LONG_CLASS_DESC)))
4087c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4097c478bd9Sstevel@tonic-gate 	if (!(longclass = (*env)->NewGlobalRef(env, longclass_lref)))
4107c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4117c478bd9Sstevel@tonic-gate 	if (!(longcons_mid = (*env)->GetMethodID(env, longclass, "<init>",
4127c478bd9Sstevel@tonic-gate 	    "(J)V")))
4137c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (!(ui64class_lref = (*env)->FindClass(env, UI64_CLASS_DESC)))
4167c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4177c478bd9Sstevel@tonic-gate 	if (!(ui64class = (*env)->NewGlobalRef(env, ui64class_lref)))
4187c478bd9Sstevel@tonic-gate 		return; /* exception thrown */
4197c478bd9Sstevel@tonic-gate 	ui64cons_mid = (*env)->GetMethodID(env, ui64class, "<init>", "([B)V");
4207c478bd9Sstevel@tonic-gate }
421