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

Print this page




   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 "com_sun_management_UnixOperatingSystem.h"
  27 
  28 #include <sys/time.h>
  29 #include <mach/mach.h>
  30 #include <mach/task_info.h>
  31 
  32 
  33 JNIEXPORT jdouble JNICALL
  34 Java_com_sun_management_UnixOperatingSystem_getSystemCpuLoad
  35 (JNIEnv *env, jobject dummy)
  36 {
  37     // This code is influenced by the darwin top source
  38 
  39     kern_return_t kr;
  40     mach_msg_type_number_t count;
  41     host_cpu_load_info_data_t load;
  42 
  43     static jlong last_used  = 0;
  44     static jlong last_total = 0;
  45 
  46     count = HOST_CPU_LOAD_INFO_COUNT;
  47     kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&load, &count);
  48     if (kr != KERN_SUCCESS) {
  49         return -1;
  50     }
  51 
  52     jlong used  = load.cpu_ticks[CPU_STATE_USER] + load.cpu_ticks[CPU_STATE_NICE] + load.cpu_ticks[CPU_STATE_SYSTEM];
  53     jlong total = used + load.cpu_ticks[CPU_STATE_IDLE];
  54 


  66     jdouble cpu = (jdouble) used_delta / total_delta;
  67 
  68     last_used  = used;
  69     last_total = total;
  70 
  71     return cpu;
  72 }
  73 
  74 
  75 #define TIME_VALUE_TO_TIMEVAL(a, r) do {  \
  76      (r)->tv_sec = (a)->seconds;          \
  77      (r)->tv_usec = (a)->microseconds;    \
  78 } while (0)
  79 
  80 
  81 #define TIME_VALUE_TO_MICROSECONDS(TV) \
  82      ((TV).tv_sec * 1000 * 1000 + (TV).tv_usec)
  83 
  84 
  85 JNIEXPORT jdouble JNICALL
  86 Java_com_sun_management_UnixOperatingSystem_getProcessCpuLoad
  87 (JNIEnv *env, jobject dummy)
  88 {
  89     // This code is influenced by the darwin top source
  90 
  91     struct task_basic_info_64 task_info_data;
  92     struct task_thread_times_info thread_info_data;
  93     struct timeval user_timeval, system_timeval, task_timeval;
  94     struct timeval now;
  95     mach_port_t task = mach_task_self();
  96     kern_return_t kr;
  97 
  98     static jlong last_task_time = 0;
  99     static jlong last_time      = 0;
 100 
 101     mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
 102     kr = task_info(task,
 103             TASK_THREAD_TIMES_INFO,
 104             (task_info_t)&thread_info_data,
 105             &thread_info_count);
 106     if (kr != KERN_SUCCESS) {




   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 "sun_management_OperatingSystemImpl.h"
  27 
  28 #include <sys/time.h>
  29 #include <mach/mach.h>
  30 #include <mach/task_info.h>
  31 
  32 
  33 JNIEXPORT jdouble JNICALL
  34 Java_sun_management_OperatingSystemImpl_getSystemCpuLoad
  35 (JNIEnv *env, jobject dummy)
  36 {
  37     // This code is influenced by the darwin top source
  38 
  39     kern_return_t kr;
  40     mach_msg_type_number_t count;
  41     host_cpu_load_info_data_t load;
  42 
  43     static jlong last_used  = 0;
  44     static jlong last_total = 0;
  45 
  46     count = HOST_CPU_LOAD_INFO_COUNT;
  47     kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&load, &count);
  48     if (kr != KERN_SUCCESS) {
  49         return -1;
  50     }
  51 
  52     jlong used  = load.cpu_ticks[CPU_STATE_USER] + load.cpu_ticks[CPU_STATE_NICE] + load.cpu_ticks[CPU_STATE_SYSTEM];
  53     jlong total = used + load.cpu_ticks[CPU_STATE_IDLE];
  54 


  66     jdouble cpu = (jdouble) used_delta / total_delta;
  67 
  68     last_used  = used;
  69     last_total = total;
  70 
  71     return cpu;
  72 }
  73 
  74 
  75 #define TIME_VALUE_TO_TIMEVAL(a, r) do {  \
  76      (r)->tv_sec = (a)->seconds;          \
  77      (r)->tv_usec = (a)->microseconds;    \
  78 } while (0)
  79 
  80 
  81 #define TIME_VALUE_TO_MICROSECONDS(TV) \
  82      ((TV).tv_sec * 1000 * 1000 + (TV).tv_usec)
  83 
  84 
  85 JNIEXPORT jdouble JNICALL
  86 Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
  87 (JNIEnv *env, jobject dummy)
  88 {
  89     // This code is influenced by the darwin top source
  90 
  91     struct task_basic_info_64 task_info_data;
  92     struct task_thread_times_info thread_info_data;
  93     struct timeval user_timeval, system_timeval, task_timeval;
  94     struct timeval now;
  95     mach_port_t task = mach_task_self();
  96     kern_return_t kr;
  97 
  98     static jlong last_task_time = 0;
  99     static jlong last_time      = 0;
 100 
 101     mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
 102     kr = task_info(task,
 103             TASK_THREAD_TIMES_INFO,
 104             (task_info_t)&thread_info_data,
 105             &thread_info_count);
 106     if (kr != KERN_SUCCESS) {