jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 12047 320a714614e9
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "management.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "com_sun_management_UnixOperatingSystem.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <sys/swap.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <sys/resource.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <sys/times.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <sys/sysinfo.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <dirent.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
static jlong page_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/* This gets us the new structured proc interfaces of 5.6 & later */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/* - see comment in <sys/procfs.h> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define _STRUCTURED_PROC 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <sys/procfs.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    struct dirent* dbuf = readdir(dirp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    return dbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#else /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    struct dirent* p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if (readdir_r(dirp, entry, &p) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
// true = get available swap in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
// false = get total swap in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    long total, avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int nswap, i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    swaptbl_t *stbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    char *strtab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // First get the number of swap resource entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throw_internal_error(env, "swapctl failed to get nswap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    if (nswap == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // Allocate storage for resource entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                               sizeof(struct swaptable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    if (stbl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Allocate storage for the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    strtab = (char*) malloc((nswap + 1) * MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    if (strtab == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    for (i = 0; i < (nswap + 1); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    stbl->swt_n = nswap + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // Get the entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if ((count = swapctl(SC_LIST, stbl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        free(strtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        throw_internal_error(env, "swapctl failed to get swap list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // Sum the entries to get total and free swap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    avail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    for (i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      total += stbl->swt_ent[i].ste_pages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      avail += stbl->swt_ent[i].ste_free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    free(strtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    return available ? ((jlong)avail * page_size) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                       ((jlong)total * page_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
#else /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    FILE *fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    jlong total = 0, avail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    struct sysinfo si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    ret = sysinfo(&si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    if (ret != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throw_internal_error(env, "sysinfo failed to get swap size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    total = (jlong)si.totalswap * si.mem_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    avail = (jlong)si.freeswap * si.mem_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    return available ? avail : total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
Java_com_sun_management_UnixOperatingSystem_initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    page_size = sysconf(_SC_PAGESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    psinfo_t psinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    ssize_t result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    size_t remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    char* addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throw_internal_error(env, "Unable to open /proc/self/psinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    addr = (char *)&psinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    for (remaining = sizeof(psinfo_t); remaining > 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        result = JVM_Read(fd, addr, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw_internal_error(env, "Unable to read /proc/self/psinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        remaining -= result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        addr += result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    return (jlong) psinfo.pr_size * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
#else /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    FILE *fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    unsigned long vsize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    if ((fp = fopen("/proc/self/stat", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        throw_internal_error(env, "Unable to open /proc/self/stat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // Ignore everything except the vsize entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (fscanf(fp, "%*d %*s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u %*d %lu %*[^\n]\n", &vsize) == EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        throw_internal_error(env, "Unable to get virtual memory usage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        fclose(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    fclose(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    return (jlong)vsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
Java_com_sun_management_UnixOperatingSystem_getTotalSwapSpaceSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    return get_total_or_available_swap_space_size(env, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
Java_com_sun_management_UnixOperatingSystem_getFreeSwapSpaceSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return get_total_or_available_swap_space_size(env, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
Java_com_sun_management_UnixOperatingSystem_getProcessCpuTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    jlong clk_tck, ns_per_clock_tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    jlong cpu_time_ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    struct tms time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    clk_tck = (jlong) sysconf(_SC_CLK_TCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
#else /* __linux__ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    clk_tck = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    if (clk_tck == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        throw_internal_error(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                             "sysconf failed - not able to get clock tick");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    times(&time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                      ns_per_clock_tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    return cpu_time_ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
Java_com_sun_management_UnixOperatingSystem_getFreePhysicalMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    return (num_avail_physical_pages * page_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
Java_com_sun_management_UnixOperatingSystem_getTotalPhysicalMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    return (num_physical_pages * page_size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
Java_com_sun_management_UnixOperatingSystem_getOpenFileDescriptorCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    DIR *dirp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    struct dirent dbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    struct dirent* dentp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    jlong fds = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    dirp = opendir("/proc/self/fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    if (dirp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        throw_internal_error(env, "Unable to open directory /proc/self/fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    // iterate through directory entries, skipping '.' and '..'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // each entry represents an open file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (isdigit(dentp->d_name[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            fds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    closedir(dirp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // subtract by 1 which was the fd open for this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    return (fds - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
Java_com_sun_management_UnixOperatingSystem_getMaxFileDescriptorCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    struct rlimit rlp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        throw_internal_error(env, "getrlimit failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    return (jlong) rlp.rlim_cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
}