Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/management/MacosxOperatingSystem.c
32287 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "sun_management_OperatingSystemImpl.h"2627#include <sys/time.h>28#include <mach/mach.h>29#include <mach/task_info.h>303132JNIEXPORT jdouble JNICALL33Java_sun_management_OperatingSystemImpl_getSystemCpuLoad034(JNIEnv *env, jobject dummy)35{36// This code is influenced by the darwin top source3738kern_return_t kr;39mach_msg_type_number_t count;40host_cpu_load_info_data_t load;4142static jlong last_used = 0;43static jlong last_total = 0;4445count = HOST_CPU_LOAD_INFO_COUNT;46kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&load, &count);47if (kr != KERN_SUCCESS) {48return -1;49}5051jlong used = load.cpu_ticks[CPU_STATE_USER] + load.cpu_ticks[CPU_STATE_NICE] + load.cpu_ticks[CPU_STATE_SYSTEM];52jlong total = used + load.cpu_ticks[CPU_STATE_IDLE];5354if (last_used == 0 || last_total == 0) {55// First call, just set the last values56last_used = used;57last_total = total;58// return 0 since we have no data, not -1 which indicates error59return 0;60}6162jlong used_delta = used - last_used;63jlong total_delta = total - last_total;6465jdouble cpu = (jdouble) used_delta / total_delta;6667last_used = used;68last_total = total;6970return cpu;71}727374#define TIME_VALUE_TO_TIMEVAL(a, r) do { \75(r)->tv_sec = (a)->seconds; \76(r)->tv_usec = (a)->microseconds; \77} while (0)787980#define TIME_VALUE_TO_MICROSECONDS(TV) \81((TV).tv_sec * 1000 * 1000 + (TV).tv_usec)828384JNIEXPORT jdouble JNICALL85Java_sun_management_OperatingSystemImpl_getProcessCpuLoad86(JNIEnv *env, jobject dummy)87{88// This code is influenced by the darwin top source8990struct task_basic_info_64 task_info_data;91struct task_thread_times_info thread_info_data;92struct timeval user_timeval, system_timeval, task_timeval;93struct timeval now;94mach_port_t task = mach_task_self();95kern_return_t kr;9697static jlong last_task_time = 0;98static jlong last_time = 0;99100mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;101kr = task_info(task,102TASK_THREAD_TIMES_INFO,103(task_info_t)&thread_info_data,104&thread_info_count);105if (kr != KERN_SUCCESS) {106// Most likely cause: |task| is a zombie.107return -1;108}109110mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;111kr = task_info(task,112TASK_BASIC_INFO_64,113(task_info_t)&task_info_data,114&count);115if (kr != KERN_SUCCESS) {116// Most likely cause: |task| is a zombie.117return -1;118}119120/* Set total_time. */121// thread info contains live time...122TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);123TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);124timeradd(&user_timeval, &system_timeval, &task_timeval);125126// ... task info contains terminated time.127TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval);128TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval);129timeradd(&user_timeval, &task_timeval, &task_timeval);130timeradd(&system_timeval, &task_timeval, &task_timeval);131132if (gettimeofday(&now, NULL) < 0) {133return -1;134}135jint ncpus = JVM_ActiveProcessorCount();136jlong time = TIME_VALUE_TO_MICROSECONDS(now) * ncpus;137jlong task_time = TIME_VALUE_TO_MICROSECONDS(task_timeval);138139if ((last_task_time == 0) || (last_time == 0)) {140// First call, just set the last values.141last_task_time = task_time;142last_time = time;143// return 0 since we have no data, not -1 which indicates error144return 0;145}146147jlong task_time_delta = task_time - last_task_time;148jlong time_delta = time - last_time;149if (time_delta == 0) {150return -1;151}152153jdouble cpu = (jdouble) task_time_delta / time_delta;154155last_task_time = task_time;156last_time = time;157158return cpu;159}160161162