1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
28*7c478bd9Sstevel@tonic-gate #include <sys/lgrp_user.h>
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include "jlgrp.h"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate static lgrp_cookie_t getCookie(JNIEnv *, jclass, jobject);
33*7c478bd9Sstevel@tonic-gate static void throwException(JNIEnv *, const char *, const char *);
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /*
36*7c478bd9Sstevel@tonic-gate  * Return the output of the getCookie() method executed on the
37*7c478bd9Sstevel@tonic-gate  * supplied instance.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate lgrp_cookie_t
getCookie(JNIEnv * env,jclass clazz,jobject obj)40*7c478bd9Sstevel@tonic-gate getCookie(JNIEnv *env, jclass clazz, jobject obj)
41*7c478bd9Sstevel@tonic-gate {
42*7c478bd9Sstevel@tonic-gate 	jfieldID fid;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "cookie", "J");
45*7c478bd9Sstevel@tonic-gate 	return ((lgrp_cookie_t)(*env)->GetLongField(env, obj, fid));
46*7c478bd9Sstevel@tonic-gate }
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * Throw an exception of the specified class with the specified message.
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate void
throwException(JNIEnv * env,const char * class,const char * msg)52*7c478bd9Sstevel@tonic-gate throwException(JNIEnv *env, const char *class, const char *msg)
53*7c478bd9Sstevel@tonic-gate {
54*7c478bd9Sstevel@tonic-gate 	jclass clazz;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->FindClass(env, class);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	(*env)->ThrowNew(env, clazz, msg);
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate /*
62*7c478bd9Sstevel@tonic-gate  * Obtain an lgrp cookie for an lgrp snapshot which contains details
63*7c478bd9Sstevel@tonic-gate  * about available resources that the operating system knows about.
64*7c478bd9Sstevel@tonic-gate  *
65*7c478bd9Sstevel@tonic-gate  * If the call fails, then throw an exception which indicates that the
66*7c478bd9Sstevel@tonic-gate  * snapshot could not be obtained.
67*7c478bd9Sstevel@tonic-gate  */
68*7c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
69*7c478bd9Sstevel@tonic-gate JNIEXPORT jlong JNICALL
Java_com_sun_solaris_service_locality_LocalityDomain_jl_1init(JNIEnv * env,jobject obj,jint view)70*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityDomain_jl_1init(JNIEnv *env,
71*7c478bd9Sstevel@tonic-gate     jobject obj, jint view)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t cookie;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if ((cookie = lgrp_init(view)) == LGRP_COOKIE_NONE) {
76*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
77*7c478bd9Sstevel@tonic-gate 		    "Could not obtain latency group cookie");
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	return ((jlong)cookie);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate /*
84*7c478bd9Sstevel@tonic-gate  * Release the snapshot in use by this instance. It is assumed that
85*7c478bd9Sstevel@tonic-gate  * the cookie is held in the "cookie" field of the invoking instance
86*7c478bd9Sstevel@tonic-gate  */
87*7c478bd9Sstevel@tonic-gate JNIEXPORT jint JNICALL
Java_com_sun_solaris_service_locality_LocalityDomain_jl_1fini(JNIEnv * env,jobject obj)88*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityDomain_jl_1fini(JNIEnv *env,
89*7c478bd9Sstevel@tonic-gate     jobject obj)
90*7c478bd9Sstevel@tonic-gate {
91*7c478bd9Sstevel@tonic-gate 	jclass clazz;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
94*7c478bd9Sstevel@tonic-gate 	return ((jint)lgrp_fini(getCookie(env, clazz, obj)));
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate /*
98*7c478bd9Sstevel@tonic-gate  * Create a new LocalityGroup object which acts as a proxy for the
99*7c478bd9Sstevel@tonic-gate  * root LocalityGroup.
100*7c478bd9Sstevel@tonic-gate  */
101*7c478bd9Sstevel@tonic-gate JNIEXPORT jobject JNICALL
Java_com_sun_solaris_service_locality_LocalityDomain_jl_1root(JNIEnv * env,jobject obj)102*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityDomain_jl_1root(JNIEnv *env,
103*7c478bd9Sstevel@tonic-gate     jobject obj)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	jclass clazz;
106*7c478bd9Sstevel@tonic-gate 	jmethodID mid;
107*7c478bd9Sstevel@tonic-gate 	jlong root;
108*7c478bd9Sstevel@tonic-gate 	jobject lgrp;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	root = (jlong) lgrp_root(getCookie(env, clazz, obj));
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->FindClass(env, "com/sun/solaris/service/locality/"
115*7c478bd9Sstevel@tonic-gate 	    "LocalityGroup");
116*7c478bd9Sstevel@tonic-gate 	mid = (*env)->GetMethodID(env, clazz, "<init>", "(Lcom/sun/solaris/"
117*7c478bd9Sstevel@tonic-gate 	    "service/locality/LocalityDomain;JLcom/sun/solaris/service/"
118*7c478bd9Sstevel@tonic-gate 	    "locality/LocalityGroup;)V");
119*7c478bd9Sstevel@tonic-gate 	lgrp = (*env)->NewObject(env, clazz, mid, obj, root, NULL);
120*7c478bd9Sstevel@tonic-gate 	return (lgrp);
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*
124*7c478bd9Sstevel@tonic-gate  * Return a new array containing all of the child LocalityGroup ids
125*7c478bd9Sstevel@tonic-gate  * for the supplied instance.
126*7c478bd9Sstevel@tonic-gate  */
127*7c478bd9Sstevel@tonic-gate JNIEXPORT jlongArray JNICALL
Java_com_sun_solaris_service_locality_LocalityGroup_jl_1children(JNIEnv * env,jobject obj)128*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityGroup_jl_1children(JNIEnv *env,
129*7c478bd9Sstevel@tonic-gate     jobject obj)
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	jclass clazz;
132*7c478bd9Sstevel@tonic-gate 	jfieldID fid;
133*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t cookie;
134*7c478bd9Sstevel@tonic-gate 	jlong id;
135*7c478bd9Sstevel@tonic-gate 	jsize nchild0, nchild;
136*7c478bd9Sstevel@tonic-gate 	jlongArray children;
137*7c478bd9Sstevel@tonic-gate 	int i;
138*7c478bd9Sstevel@tonic-gate 	lgrp_id_t *native_child;
139*7c478bd9Sstevel@tonic-gate 	jlong *java_child;
140*7c478bd9Sstevel@tonic-gate 	jobject domain;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
143*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "domain",
144*7c478bd9Sstevel@tonic-gate 	    "Lcom/sun/solaris/service/locality/LocalityDomain;");
145*7c478bd9Sstevel@tonic-gate 	domain = (*env)->GetObjectField(env, obj, fid);
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	cookie = getCookie(env, (*env)->GetObjectClass(env, domain), domain);
148*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "id", "J");
149*7c478bd9Sstevel@tonic-gate 	id = (*env)->GetLongField(env, obj, fid);
150*7c478bd9Sstevel@tonic-gate retry:
151*7c478bd9Sstevel@tonic-gate 	nchild0 = (jsize)lgrp_children(cookie, (lgrp_id_t)id, NULL, 0);
152*7c478bd9Sstevel@tonic-gate 	children = (*env)->NewLongArray(env, nchild0);
153*7c478bd9Sstevel@tonic-gate 	if ((native_child = calloc(nchild0, sizeof (lgrp_id_t))) == NULL) {
154*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
155*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for native_child array");
156*7c478bd9Sstevel@tonic-gate 		return (NULL);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 	nchild = lgrp_children(cookie, (lgrp_id_t)id, native_child, nchild0);
159*7c478bd9Sstevel@tonic-gate 	if (nchild != nchild0) {
160*7c478bd9Sstevel@tonic-gate 		free(native_child);
161*7c478bd9Sstevel@tonic-gate 		goto retry;
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if ((java_child = calloc(nchild, sizeof (jlong))) == NULL) {
165*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
166*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for java_child array");
167*7c478bd9Sstevel@tonic-gate 		free(native_child);
168*7c478bd9Sstevel@tonic-gate 		return (NULL);
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++)
172*7c478bd9Sstevel@tonic-gate 		java_child[i] = (jlong) native_child[i];
173*7c478bd9Sstevel@tonic-gate 	(*env)->SetLongArrayRegion(env, children, 0, nchild, java_child);
174*7c478bd9Sstevel@tonic-gate 	free(native_child);
175*7c478bd9Sstevel@tonic-gate 	free(java_child);
176*7c478bd9Sstevel@tonic-gate 	return (children);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate /*
180*7c478bd9Sstevel@tonic-gate  * Return a new array containing all of the cpus contained directly
181*7c478bd9Sstevel@tonic-gate  * within the LocalityGroup identified by the supplied instance.
182*7c478bd9Sstevel@tonic-gate  */
183*7c478bd9Sstevel@tonic-gate JNIEXPORT jintArray JNICALL
Java_com_sun_solaris_service_locality_LocalityGroup_jl_1cpus(JNIEnv * env,jobject obj)184*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityGroup_jl_1cpus(JNIEnv *env,
185*7c478bd9Sstevel@tonic-gate     jobject obj)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	jclass clazz;
188*7c478bd9Sstevel@tonic-gate 	jfieldID fid;
189*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t cookie;
190*7c478bd9Sstevel@tonic-gate 	jlong id;
191*7c478bd9Sstevel@tonic-gate 	jsize ncpus0, ncpus;
192*7c478bd9Sstevel@tonic-gate 	jintArray cpus;
193*7c478bd9Sstevel@tonic-gate 	int i;
194*7c478bd9Sstevel@tonic-gate 	processorid_t *native_cpus;
195*7c478bd9Sstevel@tonic-gate 	jint *java_cpus;
196*7c478bd9Sstevel@tonic-gate 	jobject domain;
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
199*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "domain",
200*7c478bd9Sstevel@tonic-gate 	    "Lcom/sun/solaris/service/locality/LocalityDomain;");
201*7c478bd9Sstevel@tonic-gate 	domain = (*env)->GetObjectField(env, obj, fid);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	cookie = getCookie(env, (*env)->GetObjectClass(env, domain), domain);
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "id", "J");
206*7c478bd9Sstevel@tonic-gate 	id = (*env)->GetLongField(env, obj, fid);
207*7c478bd9Sstevel@tonic-gate retry:
208*7c478bd9Sstevel@tonic-gate 	ncpus0 = (jsize)lgrp_cpus((lgrp_cookie_t)cookie, (lgrp_id_t)id,
209*7c478bd9Sstevel@tonic-gate 	    NULL, 0, LGRP_CONTENT_DIRECT);
210*7c478bd9Sstevel@tonic-gate 	cpus = (*env)->NewIntArray(env, ncpus0);
211*7c478bd9Sstevel@tonic-gate 	if ((native_cpus = calloc(ncpus0, sizeof (processorid_t))) == NULL) {
212*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
213*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for native_cpus array");
214*7c478bd9Sstevel@tonic-gate 		return (NULL);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	ncpus = (jsize)lgrp_cpus((lgrp_cookie_t)cookie, (lgrp_id_t)id,
217*7c478bd9Sstevel@tonic-gate 	    native_cpus, ncpus0, LGRP_CONTENT_DIRECT);
218*7c478bd9Sstevel@tonic-gate 	if (ncpus != ncpus0) {
219*7c478bd9Sstevel@tonic-gate 		free(native_cpus);
220*7c478bd9Sstevel@tonic-gate 		goto retry;
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	if ((java_cpus = calloc(ncpus, sizeof (jint))) == NULL) {
224*7c478bd9Sstevel@tonic-gate 		free(native_cpus);
225*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
226*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for java_cpus array");
227*7c478bd9Sstevel@tonic-gate 		return (NULL);
228*7c478bd9Sstevel@tonic-gate 	}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncpus; i++)
231*7c478bd9Sstevel@tonic-gate 		java_cpus[i] = (jint)native_cpus[i];
232*7c478bd9Sstevel@tonic-gate 	(*env)->SetIntArrayRegion(env, cpus, 0, ncpus, java_cpus);
233*7c478bd9Sstevel@tonic-gate 	free(native_cpus);
234*7c478bd9Sstevel@tonic-gate 	free(java_cpus);
235*7c478bd9Sstevel@tonic-gate 	return (cpus);
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate /*
239*7c478bd9Sstevel@tonic-gate  * Return the latency between two supplied latency group IDs.
240*7c478bd9Sstevel@tonic-gate  */
241*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
242*7c478bd9Sstevel@tonic-gate JNIEXPORT jint JNICALL
Java_com_sun_solaris_service_locality_LocalityGroup_jl_1latency(JNIEnv * env,jobject obj,jlong from,jlong to)243*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityGroup_jl_1latency(JNIEnv *env,
244*7c478bd9Sstevel@tonic-gate     jobject obj, jlong from, jlong to)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	return ((jint) lgrp_latency((lgrp_id_t)from, (lgrp_id_t)to));
247*7c478bd9Sstevel@tonic-gate }
248