Path: blob/master/runtime/jcl/common/com_ibm_jvm_Stats.c
6000 views
/*******************************************************************************1* Copyright (c) 2012, 2016 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122#include "jni.h"23#include "j9.h"24#include "jclprots.h"25#include "jclglob.h"2627/**28* Provides the following statistics:29* used heap memory30* committed heap memory31* max heap memory32* softmx heap memory33* free physical memory,34* total physical memory,35* system load average36* cpuTime37*38* This function avoids any allocation of a new object. Instead it sets the field39* variables in the class Stats.java40*/4142void JNICALL43Java_com_ibm_jvm_Stats_getStats(JNIEnv *env, jobject obj)44{45/* System and Heap statistics */46jlong used = 0;47jlong committed = 0;48jlong max = 0;49jlong softmx = 0;50jlong free = 0;51jlong tot = 0;52jdouble sysLoadAvg = -1.0;53jlong cpuTime = 0;5455/* Access to the current jvm */56J9JavaVM *javaVM = ((J9VMThread *) env)->javaVM;5758/* Port lib access */59PORT_ACCESS_FROM_ENV(env);60J9PortSysInfoLoadData loadData;61IDATA sysInfoResult;6263/* Class and method ID used to set our variables */64jclass setFieldsClass = NULL;65jmethodID methodID = NULL;6667/* Calculation of java heap properties */68committed = javaVM->memoryManagerFunctions->j9gc_heap_total_memory(javaVM);69used = committed - javaVM->memoryManagerFunctions->j9gc_heap_free_memory(javaVM);70max = (jlong) javaVM->managementData->maximumHeapSize;7172/* Calculation of softmx */73softmx = javaVM->memoryManagerFunctions->j9gc_get_softmx( javaVM );7475/* if no softmx has been set, report -Xmx instead as it is the current max heap size */76if (0 == softmx) {77softmx = max;78}7980/* Calculation of free physical memory from existing function in mgmtosext.c*/81free = Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getFreePhysicalMemorySizeImpl(env, obj);8283/* Calculation of total physical memory from port lib api */84tot = (jlong) j9sysinfo_get_physical_memory();8586/* Calculation of system load average */87sysInfoResult = j9sysinfo_get_load_average(&loadData);88if (0 == sysInfoResult) {89sysLoadAvg = (jdouble) loadData.oneMinuteAverage;90}9192/* Calculation of cpu time from existing function in mgmtosext.c*/93cpuTime = Java_com_ibm_lang_management_internal_ExtendedOperatingSystemMXBeanImpl_getProcessCpuTimeImpl(env, obj);9495/* Set the statistics by calling the java setFields function */96setFieldsClass = (*env)->GetObjectClass(env, obj);97methodID = JCL_CACHE_GET(env, MID_com_ibm_jvm_Stats_setFields);98if (NULL == methodID) {99methodID = (*env)->GetMethodID(env, setFieldsClass, "setFields", "(JJJJJDJJ)V");100JCL_CACHE_SET(env, MID_com_ibm_jvm_Stats_setFields, methodID);101}102103/* Call method only if there's no exception pending (prevent crash on C side) */104if (!((*env)->ExceptionCheck(env))) {105(*env)->CallVoidMethod(env, obj, methodID, committed, used, max, free, tot, sysLoadAvg, cpuTime, softmx);106}107}108109110