1 /* 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 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 <inttypes.h> 39 #include "sun_management_OperatingSystemImpl.h" 40 41 struct ticks { 42 uint64_t used; 43 uint64_t usedKernel; 44 uint64_t total; 45 }; 46 47 typedef struct ticks ticks; 48 49 typedef enum { 50 CPU_LOAD_VM_ONLY, 51 CPU_LOAD_GLOBAL, 52 } CpuLoadTarget; 53 54 static struct perfbuf { 55 int nProcs; 56 ticks jvmTicks; 57 ticks cpuTicks; 58 ticks *cpus; 59 } counters; 294 double get_cpu_load(int which) { 295 double u, s; 296 u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL); 297 if (u < 0) { 298 return -1.0; 299 } 300 // Cap total systemload to 1.0 301 return MIN((u + s), 1.0); 302 } 303 304 double get_process_load() { 305 double u, s; 306 u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY); 307 if (u < 0) { 308 return -1.0; 309 } 310 return u + s; 311 } 312 313 JNIEXPORT jdouble JNICALL 314 Java_sun_management_OperatingSystemImpl_getSystemCpuLoad0 315 (JNIEnv *env, jobject dummy) 316 { 317 if(perfInit() == 0) { 318 return get_cpu_load(-1); 319 } else { 320 return -1.0; 321 } 322 } 323 324 JNIEXPORT jdouble JNICALL 325 Java_sun_management_OperatingSystemImpl_getProcessCpuLoad0 326 (JNIEnv *env, jobject dummy) 327 { 328 if(perfInit() == 0) { 329 return get_process_load(); 330 } else { 331 return -1.0; 332 } 333 } | 1 /* 2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 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 <inttypes.h> 39 #include "com_sun_management_internal_OperatingSystemImpl.h" 40 41 struct ticks { 42 uint64_t used; 43 uint64_t usedKernel; 44 uint64_t total; 45 }; 46 47 typedef struct ticks ticks; 48 49 typedef enum { 50 CPU_LOAD_VM_ONLY, 51 CPU_LOAD_GLOBAL, 52 } CpuLoadTarget; 53 54 static struct perfbuf { 55 int nProcs; 56 ticks jvmTicks; 57 ticks cpuTicks; 58 ticks *cpus; 59 } counters; 294 double get_cpu_load(int which) { 295 double u, s; 296 u = get_cpuload_internal(which, &s, CPU_LOAD_GLOBAL); 297 if (u < 0) { 298 return -1.0; 299 } 300 // Cap total systemload to 1.0 301 return MIN((u + s), 1.0); 302 } 303 304 double get_process_load() { 305 double u, s; 306 u = get_cpuload_internal(-1, &s, CPU_LOAD_VM_ONLY); 307 if (u < 0) { 308 return -1.0; 309 } 310 return u + s; 311 } 312 313 JNIEXPORT jdouble JNICALL 314 Java_com_sun_management_internal_OperatingSystemImpl_getSystemCpuLoad0 315 (JNIEnv *env, jobject dummy) 316 { 317 if(perfInit() == 0) { 318 return get_cpu_load(-1); 319 } else { 320 return -1.0; 321 } 322 } 323 324 JNIEXPORT jdouble JNICALL 325 Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuLoad0 326 (JNIEnv *env, jobject dummy) 327 { 328 if(perfInit() == 0) { 329 return get_process_load(); 330 } else { 331 return -1.0; 332 } 333 } |