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

Print this page




  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 <stdio.h>
  27 #include <stdint.h>
  28 #include <stdarg.h>
  29 #include <unistd.h>
  30 #include <errno.h>
  31 #include <string.h>
  32 #include <sys/resource.h>
  33 #include <sys/types.h>
  34 #include <dirent.h>
  35 #include <stdlib.h>
  36 #include <dlfcn.h>
  37 #include <pthread.h>
  38 #include "com_sun_management_UnixOperatingSystem.h"
  39 
  40 struct ticks {
  41     uint64_t  used;
  42     uint64_t  usedKernel;
  43     uint64_t  total;
  44 };
  45 
  46 typedef struct ticks ticks;
  47 
  48 typedef enum {
  49     CPU_LOAD_VM_ONLY,
  50     CPU_LOAD_GLOBAL,
  51 } CpuLoadTarget;
  52 
  53 static struct perfbuf {
  54     int   nProcs;
  55     ticks jvmTicks;
  56     ticks cpuTicks;
  57     ticks *cpus;
  58 } counters;


 293 double get_cpu_load(int which) {
 294     double u, s;
 295     u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL);
 296     if (u < 0) {
 297         return -1.0;
 298     }
 299     // Cap total systemload to 1.0
 300     return MIN((u + s), 1.0);
 301 }
 302 
 303 double get_process_load() {
 304     double u, s;
 305     u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY);
 306     if (u < 0) {
 307         return -1.0;
 308     }
 309     return u + s;
 310 }
 311 
 312 JNIEXPORT jdouble JNICALL
 313 Java_com_sun_management_UnixOperatingSystem_getSystemCpuLoad
 314 (JNIEnv *env, jobject dummy)
 315 {
 316     if(perfInit() == 0) {
 317         return get_cpu_load(-1);
 318     } else {
 319         return -1.0;
 320     }
 321 }
 322 
 323 JNIEXPORT jdouble JNICALL
 324 Java_com_sun_management_UnixOperatingSystem_getProcessCpuLoad
 325 (JNIEnv *env, jobject dummy)
 326 {
 327     if(perfInit() == 0) {
 328         return get_process_load();
 329     } else {
 330         return -1.0;
 331     }
 332 }


  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  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 <stdio.h>
  27 #include <stdint.h>
  28 #include <stdarg.h>
  29 #include <unistd.h>
  30 #include <errno.h>
  31 #include <string.h>
  32 #include <sys/resource.h>
  33 #include <sys/types.h>
  34 #include <dirent.h>
  35 #include <stdlib.h>
  36 #include <dlfcn.h>
  37 #include <pthread.h>
  38 #include "sun_management_OperatingSystemImpl.h"
  39 
  40 struct ticks {
  41     uint64_t  used;
  42     uint64_t  usedKernel;
  43     uint64_t  total;
  44 };
  45 
  46 typedef struct ticks ticks;
  47 
  48 typedef enum {
  49     CPU_LOAD_VM_ONLY,
  50     CPU_LOAD_GLOBAL,
  51 } CpuLoadTarget;
  52 
  53 static struct perfbuf {
  54     int   nProcs;
  55     ticks jvmTicks;
  56     ticks cpuTicks;
  57     ticks *cpus;
  58 } counters;


 293 double get_cpu_load(int which) {
 294     double u, s;
 295     u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL);
 296     if (u < 0) {
 297         return -1.0;
 298     }
 299     // Cap total systemload to 1.0
 300     return MIN((u + s), 1.0);
 301 }
 302 
 303 double get_process_load() {
 304     double u, s;
 305     u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY);
 306     if (u < 0) {
 307         return -1.0;
 308     }
 309     return u + s;
 310 }
 311 
 312 JNIEXPORT jdouble JNICALL
 313 Java_sun_management_OperatingSystemImpl_getSystemCpuLoad
 314 (JNIEnv *env, jobject dummy)
 315 {
 316     if(perfInit() == 0) {
 317         return get_cpu_load(-1);
 318     } else {
 319         return -1.0;
 320     }
 321 }
 322 
 323 JNIEXPORT jdouble JNICALL
 324 Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
 325 (JNIEnv *env, jobject dummy)
 326 {
 327     if(perfInit() == 0) {
 328         return get_process_load();
 329     } else {
 330         return -1.0;
 331     }
 332 }