jdk/src/solaris/native/sun/management/MacosxOperatingSystem.c
author mchung
Fri, 08 Nov 2013 12:13:02 -0800
changeset 21633 6cc4c9a56216
parent 14342 jdk/src/solaris/native/com/sun/management/MacosxOperatingSystem.c@8435a30053c1
child 22583 e7d5af0b11e9
permissions -rw-r--r--
8025985: com.sun.management.OSMBeanFactory should not be public Reviewed-by: alanb, erikj, ihse, jbachorik
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12442
diff changeset
     2
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     4
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    10
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    15
 * accompanied this code).
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    16
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    20
 *
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    23
 * questions.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    24
 */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    25
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 14342
diff changeset
    26
#include "sun_management_OperatingSystemImpl.h"
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    27
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    28
#include <sys/time.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    29
#include <mach/mach.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    30
#include <mach/task_info.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    31
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    32
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    33
JNIEXPORT jdouble JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 14342
diff changeset
    34
Java_sun_management_OperatingSystemImpl_getSystemCpuLoad
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    35
(JNIEnv *env, jobject dummy)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    36
{
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    37
    // This code is influenced by the darwin top source
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    38
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    39
    kern_return_t kr;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    40
    mach_msg_type_number_t count;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    41
    host_cpu_load_info_data_t load;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    42
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    43
    static jlong last_used  = 0;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    44
    static jlong last_total = 0;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    45
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    46
    count = HOST_CPU_LOAD_INFO_COUNT;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    47
    kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&load, &count);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    48
    if (kr != KERN_SUCCESS) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    49
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    50
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    51
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    52
    jlong used  = load.cpu_ticks[CPU_STATE_USER] + load.cpu_ticks[CPU_STATE_NICE] + load.cpu_ticks[CPU_STATE_SYSTEM];
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    53
    jlong total = used + load.cpu_ticks[CPU_STATE_IDLE];
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    54
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    55
    if (last_used == 0 || last_total == 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    56
        // First call, just set the last values
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    57
        last_used  = used;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    58
        last_total = total;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    59
        // return 0 since we have no data, not -1 which indicates error
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    60
        return 0;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    61
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    62
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    63
    jlong used_delta  = used - last_used;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    64
    jlong total_delta = total - last_total;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    65
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    66
    jdouble cpu = (jdouble) used_delta / total_delta;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    67
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    68
    last_used  = used;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    69
    last_total = total;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    70
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    71
    return cpu;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    72
}
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    73
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    74
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    75
#define TIME_VALUE_TO_TIMEVAL(a, r) do {  \
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    76
     (r)->tv_sec = (a)->seconds;          \
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    77
     (r)->tv_usec = (a)->microseconds;    \
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    78
} while (0)
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    79
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    80
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    81
#define TIME_VALUE_TO_MICROSECONDS(TV) \
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    82
     ((TV).tv_sec * 1000 * 1000 + (TV).tv_usec)
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    83
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    84
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    85
JNIEXPORT jdouble JNICALL
21633
6cc4c9a56216 8025985: com.sun.management.OSMBeanFactory should not be public
mchung
parents: 14342
diff changeset
    86
Java_sun_management_OperatingSystemImpl_getProcessCpuLoad
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    87
(JNIEnv *env, jobject dummy)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
diff changeset
    88
{
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    89
    // This code is influenced by the darwin top source
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    90
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    91
    struct task_basic_info_64 task_info_data;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    92
    struct task_thread_times_info thread_info_data;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    93
    struct timeval user_timeval, system_timeval, task_timeval;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    94
    struct timeval now;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    95
    mach_port_t task = mach_task_self();
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    96
    kern_return_t kr;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    97
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    98
    static jlong last_task_time = 0;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    99
    static jlong last_time      = 0;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   100
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   101
    mach_msg_type_number_t thread_info_count = TASK_THREAD_TIMES_INFO_COUNT;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   102
    kr = task_info(task,
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   103
            TASK_THREAD_TIMES_INFO,
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   104
            (task_info_t)&thread_info_data,
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   105
            &thread_info_count);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   106
    if (kr != KERN_SUCCESS) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   107
        // Most likely cause: |task| is a zombie.
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   108
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   109
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   110
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   111
    mach_msg_type_number_t count = TASK_BASIC_INFO_64_COUNT;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   112
    kr = task_info(task,
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   113
            TASK_BASIC_INFO_64,
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   114
            (task_info_t)&task_info_data,
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   115
            &count);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   116
    if (kr != KERN_SUCCESS) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   117
        // Most likely cause: |task| is a zombie.
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   118
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   119
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   120
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   121
    /* Set total_time. */
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   122
    // thread info contains live time...
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   123
    TIME_VALUE_TO_TIMEVAL(&thread_info_data.user_time, &user_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   124
    TIME_VALUE_TO_TIMEVAL(&thread_info_data.system_time, &system_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   125
    timeradd(&user_timeval, &system_timeval, &task_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   126
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   127
    // ... task info contains terminated time.
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   128
    TIME_VALUE_TO_TIMEVAL(&task_info_data.user_time, &user_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   129
    TIME_VALUE_TO_TIMEVAL(&task_info_data.system_time, &system_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   130
    timeradd(&user_timeval, &task_timeval, &task_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   131
    timeradd(&system_timeval, &task_timeval, &task_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   132
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   133
    if (gettimeofday(&now, NULL) < 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   134
       return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   135
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   136
    jint ncpus      = JVM_ActiveProcessorCount();
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   137
    jlong time      = TIME_VALUE_TO_MICROSECONDS(now) * ncpus;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   138
    jlong task_time = TIME_VALUE_TO_MICROSECONDS(task_timeval);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   139
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   140
    if ((last_task_time == 0) || (last_time == 0)) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   141
        // First call, just set the last values.
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   142
        last_task_time = task_time;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   143
        last_time      = time;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   144
        // return 0 since we have no data, not -1 which indicates error
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   145
        return 0;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   146
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   147
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   148
    jlong task_time_delta = task_time - last_task_time;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   149
    jlong time_delta      = time - last_time;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   150
    if (time_delta == 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   151
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   152
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   153
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   154
    jdouble cpu = (jdouble) task_time_delta / time_delta;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   155
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   156
    last_task_time = task_time;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   157
    last_time      = time;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   158
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   159
    return cpu;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   160
 }