xref: /titanic_52/usr/src/cmd/pools/poold/libjlgrp/jlgrp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/lgrp_user.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "jlgrp.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate static lgrp_cookie_t getCookie(JNIEnv *, jclass, jobject);
36*7c478bd9Sstevel@tonic-gate static void throwException(JNIEnv *, const char *, const char *);
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Return the output of the getCookie() method executed on the
40*7c478bd9Sstevel@tonic-gate  * supplied instance.
41*7c478bd9Sstevel@tonic-gate  */
42*7c478bd9Sstevel@tonic-gate lgrp_cookie_t
43*7c478bd9Sstevel@tonic-gate getCookie(JNIEnv *env, jclass clazz, jobject obj)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	jfieldID fid;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "cookie", "J");
48*7c478bd9Sstevel@tonic-gate 	return ((lgrp_cookie_t)(*env)->GetLongField(env, obj, fid));
49*7c478bd9Sstevel@tonic-gate }
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /*
52*7c478bd9Sstevel@tonic-gate  * Throw an exception of the specified class with the specified message.
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate void
55*7c478bd9Sstevel@tonic-gate throwException(JNIEnv *env, const char *class, const char *msg)
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	jclass clazz;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->FindClass(env, class);
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	(*env)->ThrowNew(env, clazz, msg);
62*7c478bd9Sstevel@tonic-gate }
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * Obtain an lgrp cookie for an lgrp snapshot which contains details
66*7c478bd9Sstevel@tonic-gate  * about available resources that the operating system knows about.
67*7c478bd9Sstevel@tonic-gate  *
68*7c478bd9Sstevel@tonic-gate  * If the call fails, then throw an exception which indicates that the
69*7c478bd9Sstevel@tonic-gate  * snapshot could not be obtained.
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
72*7c478bd9Sstevel@tonic-gate JNIEXPORT jlong JNICALL
73*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityDomain_jl_1init(JNIEnv *env,
74*7c478bd9Sstevel@tonic-gate     jobject obj, jint view)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t cookie;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	if ((cookie = lgrp_init(view)) == LGRP_COOKIE_NONE) {
79*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
80*7c478bd9Sstevel@tonic-gate 		    "Could not obtain latency group cookie");
81*7c478bd9Sstevel@tonic-gate 	}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	return ((jlong)cookie);
84*7c478bd9Sstevel@tonic-gate }
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /*
87*7c478bd9Sstevel@tonic-gate  * Release the snapshot in use by this instance. It is assumed that
88*7c478bd9Sstevel@tonic-gate  * the cookie is held in the "cookie" field of the invoking instance
89*7c478bd9Sstevel@tonic-gate  */
90*7c478bd9Sstevel@tonic-gate JNIEXPORT jint JNICALL
91*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityDomain_jl_1fini(JNIEnv *env,
92*7c478bd9Sstevel@tonic-gate     jobject obj)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	jclass clazz;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
97*7c478bd9Sstevel@tonic-gate 	return ((jint)lgrp_fini(getCookie(env, clazz, obj)));
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * Create a new LocalityGroup object which acts as a proxy for the
102*7c478bd9Sstevel@tonic-gate  * root LocalityGroup.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate JNIEXPORT jobject JNICALL
105*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityDomain_jl_1root(JNIEnv *env,
106*7c478bd9Sstevel@tonic-gate     jobject obj)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	jclass clazz;
109*7c478bd9Sstevel@tonic-gate 	jmethodID mid;
110*7c478bd9Sstevel@tonic-gate 	jlong root;
111*7c478bd9Sstevel@tonic-gate 	jobject lgrp;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	root = (jlong) lgrp_root(getCookie(env, clazz, obj));
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->FindClass(env, "com/sun/solaris/service/locality/"
118*7c478bd9Sstevel@tonic-gate 	    "LocalityGroup");
119*7c478bd9Sstevel@tonic-gate 	mid = (*env)->GetMethodID(env, clazz, "<init>", "(Lcom/sun/solaris/"
120*7c478bd9Sstevel@tonic-gate 	    "service/locality/LocalityDomain;JLcom/sun/solaris/service/"
121*7c478bd9Sstevel@tonic-gate 	    "locality/LocalityGroup;)V");
122*7c478bd9Sstevel@tonic-gate 	lgrp = (*env)->NewObject(env, clazz, mid, obj, root, NULL);
123*7c478bd9Sstevel@tonic-gate 	return (lgrp);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate /*
127*7c478bd9Sstevel@tonic-gate  * Return a new array containing all of the child LocalityGroup ids
128*7c478bd9Sstevel@tonic-gate  * for the supplied instance.
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate JNIEXPORT jlongArray JNICALL
131*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityGroup_jl_1children(JNIEnv *env,
132*7c478bd9Sstevel@tonic-gate     jobject obj)
133*7c478bd9Sstevel@tonic-gate {
134*7c478bd9Sstevel@tonic-gate 	jclass clazz;
135*7c478bd9Sstevel@tonic-gate 	jfieldID fid;
136*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t cookie;
137*7c478bd9Sstevel@tonic-gate 	jlong id;
138*7c478bd9Sstevel@tonic-gate 	jsize nchild0, nchild;
139*7c478bd9Sstevel@tonic-gate 	jlongArray children;
140*7c478bd9Sstevel@tonic-gate 	int i;
141*7c478bd9Sstevel@tonic-gate 	lgrp_id_t *native_child;
142*7c478bd9Sstevel@tonic-gate 	jlong *java_child;
143*7c478bd9Sstevel@tonic-gate 	jobject domain;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
146*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "domain",
147*7c478bd9Sstevel@tonic-gate 	    "Lcom/sun/solaris/service/locality/LocalityDomain;");
148*7c478bd9Sstevel@tonic-gate 	domain = (*env)->GetObjectField(env, obj, fid);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	cookie = getCookie(env, (*env)->GetObjectClass(env, domain), domain);
151*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "id", "J");
152*7c478bd9Sstevel@tonic-gate 	id = (*env)->GetLongField(env, obj, fid);
153*7c478bd9Sstevel@tonic-gate retry:
154*7c478bd9Sstevel@tonic-gate 	nchild0 = (jsize)lgrp_children(cookie, (lgrp_id_t)id, NULL, 0);
155*7c478bd9Sstevel@tonic-gate 	children = (*env)->NewLongArray(env, nchild0);
156*7c478bd9Sstevel@tonic-gate 	if ((native_child = calloc(nchild0, sizeof (lgrp_id_t))) == NULL) {
157*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
158*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for native_child array");
159*7c478bd9Sstevel@tonic-gate 		return (NULL);
160*7c478bd9Sstevel@tonic-gate 	}
161*7c478bd9Sstevel@tonic-gate 	nchild = lgrp_children(cookie, (lgrp_id_t)id, native_child, nchild0);
162*7c478bd9Sstevel@tonic-gate 	if (nchild != nchild0) {
163*7c478bd9Sstevel@tonic-gate 		free(native_child);
164*7c478bd9Sstevel@tonic-gate 		goto retry;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	if ((java_child = calloc(nchild, sizeof (jlong))) == NULL) {
168*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
169*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for java_child array");
170*7c478bd9Sstevel@tonic-gate 		free(native_child);
171*7c478bd9Sstevel@tonic-gate 		return (NULL);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nchild; i++)
175*7c478bd9Sstevel@tonic-gate 		java_child[i] = (jlong) native_child[i];
176*7c478bd9Sstevel@tonic-gate 	(*env)->SetLongArrayRegion(env, children, 0, nchild, java_child);
177*7c478bd9Sstevel@tonic-gate 	free(native_child);
178*7c478bd9Sstevel@tonic-gate 	free(java_child);
179*7c478bd9Sstevel@tonic-gate 	return (children);
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate /*
183*7c478bd9Sstevel@tonic-gate  * Return a new array containing all of the cpus contained directly
184*7c478bd9Sstevel@tonic-gate  * within the LocalityGroup identified by the supplied instance.
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate JNIEXPORT jintArray JNICALL
187*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityGroup_jl_1cpus(JNIEnv *env,
188*7c478bd9Sstevel@tonic-gate     jobject obj)
189*7c478bd9Sstevel@tonic-gate {
190*7c478bd9Sstevel@tonic-gate 	jclass clazz;
191*7c478bd9Sstevel@tonic-gate 	jfieldID fid;
192*7c478bd9Sstevel@tonic-gate 	lgrp_cookie_t cookie;
193*7c478bd9Sstevel@tonic-gate 	jlong id;
194*7c478bd9Sstevel@tonic-gate 	jsize ncpus0, ncpus;
195*7c478bd9Sstevel@tonic-gate 	jintArray cpus;
196*7c478bd9Sstevel@tonic-gate 	int i;
197*7c478bd9Sstevel@tonic-gate 	processorid_t *native_cpus;
198*7c478bd9Sstevel@tonic-gate 	jint *java_cpus;
199*7c478bd9Sstevel@tonic-gate 	jobject domain;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	clazz = (*env)->GetObjectClass(env, obj);
202*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "domain",
203*7c478bd9Sstevel@tonic-gate 	    "Lcom/sun/solaris/service/locality/LocalityDomain;");
204*7c478bd9Sstevel@tonic-gate 	domain = (*env)->GetObjectField(env, obj, fid);
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	cookie = getCookie(env, (*env)->GetObjectClass(env, domain), domain);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	fid = (*env)->GetFieldID(env, clazz, "id", "J");
209*7c478bd9Sstevel@tonic-gate 	id = (*env)->GetLongField(env, obj, fid);
210*7c478bd9Sstevel@tonic-gate retry:
211*7c478bd9Sstevel@tonic-gate 	ncpus0 = (jsize)lgrp_cpus((lgrp_cookie_t)cookie, (lgrp_id_t)id,
212*7c478bd9Sstevel@tonic-gate 	    NULL, 0, LGRP_CONTENT_DIRECT);
213*7c478bd9Sstevel@tonic-gate 	cpus = (*env)->NewIntArray(env, ncpus0);
214*7c478bd9Sstevel@tonic-gate 	if ((native_cpus = calloc(ncpus0, sizeof (processorid_t))) == NULL) {
215*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
216*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for native_cpus array");
217*7c478bd9Sstevel@tonic-gate 		return (NULL);
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	ncpus = (jsize)lgrp_cpus((lgrp_cookie_t)cookie, (lgrp_id_t)id,
220*7c478bd9Sstevel@tonic-gate 	    native_cpus, ncpus0, LGRP_CONTENT_DIRECT);
221*7c478bd9Sstevel@tonic-gate 	if (ncpus != ncpus0) {
222*7c478bd9Sstevel@tonic-gate 		free(native_cpus);
223*7c478bd9Sstevel@tonic-gate 		goto retry;
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	if ((java_cpus = calloc(ncpus, sizeof (jint))) == NULL) {
227*7c478bd9Sstevel@tonic-gate 		free(native_cpus);
228*7c478bd9Sstevel@tonic-gate 		throwException(env, "java/lang/Exception",
229*7c478bd9Sstevel@tonic-gate 		    "Could not allocate memory for java_cpus array");
230*7c478bd9Sstevel@tonic-gate 		return (NULL);
231*7c478bd9Sstevel@tonic-gate 	}
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ncpus; i++)
234*7c478bd9Sstevel@tonic-gate 		java_cpus[i] = (jint)native_cpus[i];
235*7c478bd9Sstevel@tonic-gate 	(*env)->SetIntArrayRegion(env, cpus, 0, ncpus, java_cpus);
236*7c478bd9Sstevel@tonic-gate 	free(native_cpus);
237*7c478bd9Sstevel@tonic-gate 	free(java_cpus);
238*7c478bd9Sstevel@tonic-gate 	return (cpus);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate  * Return the latency between two supplied latency group IDs.
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
245*7c478bd9Sstevel@tonic-gate JNIEXPORT jint JNICALL
246*7c478bd9Sstevel@tonic-gate Java_com_sun_solaris_service_locality_LocalityGroup_jl_1latency(JNIEnv *env,
247*7c478bd9Sstevel@tonic-gate     jobject obj, jlong from, jlong to)
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	return ((jint) lgrp_latency((lgrp_id_t)from, (lgrp_id_t)to));
250*7c478bd9Sstevel@tonic-gate }
251