src/solaris/native/sun/management/SolarisOperatingSystem.c

Print this page




  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <fcntl.h>
  27 #include <kstat.h>
  28 #include <procfs.h>
  29 #include <unistd.h>
  30 #include <stdlib.h>
  31 #include <stdio.h>
  32 #include <string.h>
  33 #include <sys/sysinfo.h>
  34 #include <sys/lwp.h>
  35 #include <pthread.h>
  36 #include <utmpx.h>
  37 #include <dlfcn.h>
  38 #include <sys/loadavg.h>
  39 #include <jni.h>
  40 #include "jvm.h"
  41 #include "com_sun_management_UnixOperatingSystem.h"
  42 
  43 typedef struct {
  44     kstat_t *kstat;
  45     uint64_t  last_idle;
  46     uint64_t  last_total;
  47     double  last_ratio;
  48 } cpuload_t;
  49 
  50 static cpuload_t   *cpu_loads = NULL;
  51 static unsigned int num_cpus;
  52 static kstat_ctl_t *kstat_ctrl = NULL;
  53 
  54 static void map_cpu_kstat_counters() {
  55     kstat_t     *kstat;
  56     int          i;
  57 
  58     // Get number of CPU(s)
  59     if ((num_cpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
  60         num_cpus = 1;
  61     }


 209 
 210     return load;
 211 }
 212 
 213 /**
 214  * Return the cpu load (0-1) for the current process (i.e the JVM)
 215  * or -1.0 if the get_info() call failed
 216  */
 217 double get_process_load(void) {
 218     psinfo_t info;
 219 
 220     // Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s
 221     // process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000.
 222     if (get_info("/proc/self/psinfo",&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) == 0) {
 223         return (double) info.pr_pctcpu / 0x8000;
 224     }
 225     return -1.0;
 226 }
 227 
 228 JNIEXPORT jdouble JNICALL
 229 Java_com_sun_management_UnixOperatingSystem_getSystemCpuLoad
 230 (JNIEnv *env, jobject dummy)
 231 {
 232     return get_cpu_load(-1);
 233 }
 234 
 235 JNIEXPORT jdouble JNICALL
 236 Java_com_sun_management_UnixOperatingSystem_getProcessCpuLoad
 237 (JNIEnv *env, jobject dummy)
 238 {
 239     return get_process_load();
 240 }
 241 


  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <fcntl.h>
  27 #include <kstat.h>
  28 #include <procfs.h>
  29 #include <unistd.h>
  30 #include <stdlib.h>
  31 #include <stdio.h>
  32 #include <string.h>
  33 #include <sys/sysinfo.h>
  34 #include <sys/lwp.h>
  35 #include <pthread.h>
  36 #include <utmpx.h>
  37 #include <dlfcn.h>
  38 #include <sys/loadavg.h>
  39 #include <jni.h>
  40 #include "jvm.h"
  41 #include "sun_management_OperatingSystemImpl.h"
  42 
  43 typedef struct {
  44     kstat_t *kstat;
  45     uint64_t  last_idle;
  46     uint64_t  last_total;
  47     double  last_ratio;
  48 } cpuload_t;
  49 
  50 static cpuload_t   *cpu_loads = NULL;
  51 static unsigned int num_cpus;
  52 static kstat_ctl_t *kstat_ctrl = NULL;
  53 
  54 static void map_cpu_kstat_counters() {
  55     kstat_t     *kstat;
  56     int          i;
  57 
  58     // Get number of CPU(s)
  59     if ((num_cpus = sysconf(_SC_NPROCESSORS_ONLN)) == -1) {
  60         num_cpus = 1;
  61     }


 209 
 210     return load;
 211 }
 212 
 213 /**
 214  * Return the cpu load (0-1) for the current process (i.e the JVM)
 215  * or -1.0 if the get_info() call failed
 216  */
 217 double get_process_load(void) {
 218     psinfo_t info;
 219 
 220     // Get the percentage of "recent cpu usage" from all the lwp:s in the JVM:s
 221     // process. This is returned as a value between 0.0 and 1.0 multiplied by 0x8000.
 222     if (get_info("/proc/self/psinfo",&info.pr_pctcpu, sizeof(info.pr_pctcpu), offsetof(psinfo_t, pr_pctcpu)) == 0) {
 223         return (double) info.pr_pctcpu / 0x8000;
 224     }
 225     return -1.0;
 226 }
 227 
 228 JNIEXPORT jdouble JNICALL
 229 Java_sun_management_OperatingSystemImpl_getSystemCpuLoad
 230 (JNIEnv *env, jobject dummy)
 231 {
 232     return get_cpu_load(-1);
 233 }
 234 
 235 JNIEXPORT jdouble JNICALL
 236 Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
 237 (JNIEnv *env, jobject dummy)
 238 {
 239     return get_process_load();
 240 }
 241