4632N/A/*
4632N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4632N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4632N/A *
4632N/A * This code is free software; you can redistribute it and/or modify it
4632N/A * under the terms of the GNU General Public License version 2 only, as
4632N/A * published by the Free Software Foundation. Oracle designates this
4632N/A * particular file as subject to the "Classpath" exception as provided
4632N/A * by Oracle in the LICENSE file that accompanied this code.
4632N/A *
4632N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4632N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4632N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4632N/A * version 2 for more details (a copy is included in the LICENSE file that
4632N/A * accompanied this code).
4632N/A *
4632N/A * You should have received a copy of the GNU General Public License version
4632N/A * 2 along with this work; if not, write to the Free Software Foundation,
4632N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4632N/A *
4632N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4632N/A * or visit www.oracle.com if you need additional information or have any
4632N/A * questions.
4632N/A */
4632N/A
4632N/A#include "com_sun_management_UnixOperatingSystem.h"
4632N/A
4958N/A#include <sys/time.h>
4958N/A#include <mach/mach.h>
4958N/A#include <mach/task_info.h>
4958N/A
4958N/A
4632N/AJNIEXPORT jdouble JNICALL
4632N/AJava_com_sun_management_UnixOperatingSystem_getSystemCpuLoad
4632N/A(JNIEnv *env, jobject dummy)
4632N/A{
4958N/A // This code is influenced by the darwin top source
4958N/A
4958N/A kern_return_t kr;
4958N/A mach_msg_type_number_t count;
4958N/A host_cpu_load_info_data_t load;
4958N/A
4958N/A static jlong last_used = 0;
4958N/A static jlong last_total = 0;
4958N/A
4958N/A count = HOST_CPU_LOAD_INFO_COUNT;
4958N/A kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&load, &count);
4958N/A if (kr != KERN_SUCCESS) {
4958N/A return -1;
4958N/A }
4958N/A
4958N/A jlong used = load.cpu_ticks[CPU_STATE_USER] + load.cpu_ticks[CPU_STATE_NICE] + load.cpu_ticks[CPU_STATE_SYSTEM];
4958N/A jlong total = used + load.cpu_ticks[CPU_STATE_IDLE];
4958N/A
4958N/A if (last_used == 0 || last_total == 0) {
4958N/A // First call, just set the last values
4958N/A last_used = used;
4958N/A last_total = total;
4958N/A // return 0 since we have no data, not -1 which indicates error
4958N/A return 0;
4958N/A }
4958N/A
4958N/A jlong used_delta = used - last_used;
4958N/A jlong total_delta = total - last_total;
4958N/A
4958N/A jdouble cpu = (jdouble) used_delta / total_delta;
4958N/A
4958N/A last_used = used;
4958N/A last_total = total;
4958N/A
4958N/A return cpu;
4632N/A}
4632N/A
4958N/A
4958N/A#define TIME_VALUE_TO_TIMEVAL(a, r) do { \
4958N/A (r)->tv_sec = (a)->seconds; \
4958N/A (r)->tv_usec = (a)->microseconds; \
4958N/A} while (0)
4958N/A
4958N/A
4958N/A#define TIME_VALUE_TO_MICROSECONDS(TV) \
4958N/A ((TV).tv_sec * 1000 * 1000 + (TV).tv_usec)
4958N/A
4958N/A
4632N/AJNIEXPORT jdouble JNICALL
4632N/AJava_com_sun_management_UnixOperatingSystem_getProcessCpuLoad
4632N/A(JNIEnv *env, jobject dummy)
4632N/A{
4958N/A // This code is influenced by the darwin top source
4958N/A
4958N/A struct task_basic_info_64 task_info_data;
4958N/A struct task_thread_times_info thread_info_data;
4958N/A struct timeval user_timeval, system_timeval, task_timeval;
4958N/A struct timeval now;
4958N/A mach_port_t task = mach_task_self();
4958N/A kern_return_t kr;
4958N/A
4958N/A static jlong last_task_time = 0;
4958N/A static jlong last_time = 0;
4958N/A
4958N/A mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
4958N/A kr = task_info(task,
4958N/A TASK_THREAD_TIMES_INFO,
4958N/A (task_info_t)&thread_info_data,
4958N/A &thread_info_count);
4958N/A if (kr != KERN_SUCCESS) {
4958N/A // Most likely cause: |task| is a zombie.
4958N/A return -1;
4958N/A }
4958N/A
4958N/A mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
4958N/A kr = task_info(task,
4958N/A TASK_BASIC_INFO_64,
4958N/A (task_info_t)&task_info_data,
4958N/A &count);
4958N/A if (kr != KERN_SUCCESS) {
4958N/A // Most likely cause: |task| is a zombie.
4958N/A return -1;
4958N/A }
4958N/A
4958N/A /* Set total_time. */
4958N/A // thread info contains live time...
4958N/A TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);
4958N/A TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);
4958N/A timeradd(&user_timeval, &system_timeval, &task_timeval);
4958N/A
4958N/A // ... task info contains terminated time.
4958N/A TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval);
4958N/A TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval);
4958N/A timeradd(&user_timeval, &task_timeval, &task_timeval);
4958N/A timeradd(&system_timeval, &task_timeval, &task_timeval);
4958N/A
4958N/A if (gettimeofday(&now, NULL) < 0) {
4958N/A return -1;
4958N/A }
4958N/A jint ncpus = JVM_ActiveProcessorCount();
4958N/A jlong time = TIME_VALUE_TO_MICROSECONDS(now) * ncpus;
4958N/A jlong task_time = TIME_VALUE_TO_MICROSECONDS(task_timeval);
4958N/A
4958N/A if ((last_task_time == 0) || (last_time == 0)) {
4958N/A // First call, just set the last values.
4958N/A last_task_time = task_time;
4958N/A last_time = time;
4958N/A // return 0 since we have no data, not -1 which indicates error
4958N/A return 0;
4958N/A }
4958N/A
4958N/A jlong task_time_delta = task_time - last_task_time;
4958N/A jlong time_delta = time - last_time;
4958N/A if (time_delta == 0) {
4958N/A return -1;
4958N/A }
4958N/A
4958N/A jdouble cpu = (jdouble) task_time_delta / time_delta;
4958N/A
4958N/A last_task_time = task_time;
4958N/A last_time = time;
4958N/A
4958N/A return cpu;
4958N/A }