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