4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <sys/lgrp_user.h>
35 static lgrp_cookie_t
getCookie(JNIEnv
*, jclass
, jobject
);
36 static void throwException(JNIEnv
*, const char *, const char *);
39 * Return the output of the getCookie() method executed on the
43 getCookie(JNIEnv
*env
, jclass clazz
, jobject obj
)
47 fid
= (*env
)->GetFieldID(env
, clazz
, "cookie", "J");
48 return ((lgrp_cookie_t
)(*env
)->GetLongField(env
, obj
, fid
));
52 * Throw an exception of the specified class with the specified message.
55 throwException(JNIEnv
*env
, const char *class, const char *msg
)
59 clazz
= (*env
)->FindClass(env
, class);
61 (*env
)->ThrowNew(env
, clazz
, msg
);
65 * Obtain an lgrp cookie for an lgrp snapshot which contains details
66 * about available resources that the operating system knows about.
68 * If the call fails, then throw an exception which indicates that the
69 * snapshot could not be obtained.
72 JNIEXPORT jlong JNICALL
73 Java_com_sun_solaris_service_locality_LocalityDomain_jl_1init(JNIEnv
*env
,
74 jobject obj
, jint view
)
78 if ((cookie
= lgrp_init(view
)) == LGRP_COOKIE_NONE
) {
79 throwException(env
, "java/lang/Exception",
80 "Could not obtain latency group cookie");
83 return ((jlong
)cookie
);
87 * Release the snapshot in use by this instance. It is assumed that
88 * the cookie is held in the "cookie" field of the invoking instance
90 JNIEXPORT jint JNICALL
91 Java_com_sun_solaris_service_locality_LocalityDomain_jl_1fini(JNIEnv
*env
,
96 clazz
= (*env
)->GetObjectClass(env
, obj
);
97 return ((jint
)lgrp_fini(getCookie(env
, clazz
, obj
)));
101 * Create a new LocalityGroup object which acts as a proxy for the
102 * root LocalityGroup.
104 JNIEXPORT jobject JNICALL
105 Java_com_sun_solaris_service_locality_LocalityDomain_jl_1root(JNIEnv
*env
,
113 clazz
= (*env
)->GetObjectClass(env
, obj
);
115 root
= (jlong
) lgrp_root(getCookie(env
, clazz
, obj
));
117 clazz
= (*env
)->FindClass(env
, "com/sun/solaris/service/locality/"
119 mid
= (*env
)->GetMethodID(env
, clazz
, "<init>", "(Lcom/sun/solaris/"
120 "service/locality/LocalityDomain;JLcom/sun/solaris/service/"
121 "locality/LocalityGroup;)V");
122 lgrp
= (*env
)->NewObject(env
, clazz
, mid
, obj
, root
, NULL
);
127 * Return a new array containing all of the child LocalityGroup ids
128 * for the supplied instance.
130 JNIEXPORT jlongArray JNICALL
131 Java_com_sun_solaris_service_locality_LocalityGroup_jl_1children(JNIEnv
*env
,
136 lgrp_cookie_t cookie
;
138 jsize nchild0
, nchild
;
141 lgrp_id_t
*native_child
;
145 clazz
= (*env
)->GetObjectClass(env
, obj
);
146 fid
= (*env
)->GetFieldID(env
, clazz
, "domain",
147 "Lcom/sun/solaris/service/locality/LocalityDomain;");
148 domain
= (*env
)->GetObjectField(env
, obj
, fid
);
150 cookie
= getCookie(env
, (*env
)->GetObjectClass(env
, domain
), domain
);
151 fid
= (*env
)->GetFieldID(env
, clazz
, "id", "J");
152 id
= (*env
)->GetLongField(env
, obj
, fid
);
154 nchild0
= (jsize
)lgrp_children(cookie
, (lgrp_id_t
)id
, NULL
, 0);
155 children
= (*env
)->NewLongArray(env
, nchild0
);
156 if ((native_child
= calloc(nchild0
, sizeof (lgrp_id_t
))) == NULL
) {
157 throwException(env
, "java/lang/Exception",
158 "Could not allocate memory for native_child array");
161 nchild
= lgrp_children(cookie
, (lgrp_id_t
)id
, native_child
, nchild0
);
162 if (nchild
!= nchild0
) {
167 if ((java_child
= calloc(nchild
, sizeof (jlong
))) == NULL
) {
168 throwException(env
, "java/lang/Exception",
169 "Could not allocate memory for java_child array");
174 for (i
= 0; i
< nchild
; i
++)
175 java_child
[i
] = (jlong
) native_child
[i
];
176 (*env
)->SetLongArrayRegion(env
, children
, 0, nchild
, java_child
);
183 * Return a new array containing all of the cpus contained directly
184 * within the LocalityGroup identified by the supplied instance.
186 JNIEXPORT jintArray JNICALL
187 Java_com_sun_solaris_service_locality_LocalityGroup_jl_1cpus(JNIEnv
*env
,
192 lgrp_cookie_t cookie
;
197 processorid_t
*native_cpus
;
201 clazz
= (*env
)->GetObjectClass(env
, obj
);
202 fid
= (*env
)->GetFieldID(env
, clazz
, "domain",
203 "Lcom/sun/solaris/service/locality/LocalityDomain;");
204 domain
= (*env
)->GetObjectField(env
, obj
, fid
);
206 cookie
= getCookie(env
, (*env
)->GetObjectClass(env
, domain
), domain
);
208 fid
= (*env
)->GetFieldID(env
, clazz
, "id", "J");
209 id
= (*env
)->GetLongField(env
, obj
, fid
);
211 ncpus0
= (jsize
)lgrp_cpus((lgrp_cookie_t
)cookie
, (lgrp_id_t
)id
,
212 NULL
, 0, LGRP_CONTENT_DIRECT
);
213 cpus
= (*env
)->NewIntArray(env
, ncpus0
);
214 if ((native_cpus
= calloc(ncpus0
, sizeof (processorid_t
))) == NULL
) {
215 throwException(env
, "java/lang/Exception",
216 "Could not allocate memory for native_cpus array");
219 ncpus
= (jsize
)lgrp_cpus((lgrp_cookie_t
)cookie
, (lgrp_id_t
)id
,
220 native_cpus
, ncpus0
, LGRP_CONTENT_DIRECT
);
221 if (ncpus
!= ncpus0
) {
226 if ((java_cpus
= calloc(ncpus
, sizeof (jint
))) == NULL
) {
228 throwException(env
, "java/lang/Exception",
229 "Could not allocate memory for java_cpus array");
233 for (i
= 0; i
< ncpus
; i
++)
234 java_cpus
[i
] = (jint
)native_cpus
[i
];
235 (*env
)->SetIntArrayRegion(env
, cpus
, 0, ncpus
, java_cpus
);
242 * Return the latency between two supplied latency group IDs.
245 JNIEXPORT jint JNICALL
246 Java_com_sun_solaris_service_locality_LocalityGroup_jl_1latency(JNIEnv
*env
,
247 jobject obj
, jlong from
, jlong to
)
249 return ((jint
) lgrp_latency((lgrp_id_t
)from
, (lgrp_id_t
)to
));