jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c
author michaelm
Tue, 06 Mar 2012 20:34:38 +0000
changeset 12047 320a714614e9
parent 5506 202f599c92aa
child 12442 c42ceb64ffb2
permissions -rw-r--r--
7113349: Initial changeset for Macosx port to jdk Reviewed-by: jjh, alanb, dholmes, anthony, ohrstrom, ksrini, jrose, weijun, smarks Contributed-by: Alan Bateman <alan.bateman@oracle.com>, Alexander Potochkin <alexander.potochkin@oracle.com>, Alexander Zuev <alexander.zuev@oracle.com>, Andrew Brygin <andrew.brygin@oracle.com>, Artem Ananiev <artem.ananiev@oracle.com>, Alex Strange <astrange@apple.com>, Bino George <bino@apple.com>, Christine Lu <christine.lu@oracle.com>, David Katleman <david.katleman@oracle.com>, David Durrence <david_durrence@apple.com>, Dmitry Cherepanov <dmitry.cherepanov@oracle.com>, Greg Lewis <glewis@eyesbeyond.com>, Kevin Miller <kevin_m_miller@apple.com>, Kurt Miller <kurt@intricatesoftware.com>, Landon Fuller <landonf@plausiblelabs.com>, Leonid Romanov <leonid.romanov@oracle.com>, Loefty Walkowiak <loefty@apple.com>, Mark Reinhold <mark.reinhold@oracle.com>, Naoto Sato <naoto.sato@oracle.com>, Philip Race <philip.race@oracle.com>, Roger Hoover <rhoover@apple.com>, Scott Kovatch <scott.kovatch@oracle.com>, Sergey ByloKhov <sergey.bylokhov@oracle.com>, Mike Swingler <swingler@apple.com>, Tomas Hurka <tomas.hurka@oracle.com>
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>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    35
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    36
#include <sys/sysctl.h>
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    37
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <sys/swap.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    39
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include <sys/resource.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include <sys/times.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    42
#ifndef _ALLBSD_SOURCE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <sys/sysinfo.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    44
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include <dirent.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jlong page_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    55
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    56
#define MB      (1024UL * 1024UL)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    57
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    58
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* This gets us the new structured proc interfaces of 5.6 & later */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/* - see comment in <sys/procfs.h> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#define _STRUCTURED_PROC 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#include <sys/procfs.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    64
#endif /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    65
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    struct dirent* dbuf = readdir(dirp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    return dbuf;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    70
#else /* __linux__ || _ALLBSD_SOURCE */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    struct dirent* p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    if (readdir_r(dirp, entry, &p) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
// true = get available swap in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
// false = get total swap in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    long total, avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    int nswap, i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    swaptbl_t *stbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    char *strtab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // First get the number of swap resource entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        throw_internal_error(env, "swapctl failed to get nswap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (nswap == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // Allocate storage for resource entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                               sizeof(struct swaptable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if (stbl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // Allocate storage for the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    strtab = (char*) malloc((nswap + 1) * MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (strtab == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    for (i = 0; i < (nswap + 1); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    stbl->swt_n = nswap + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // Get the entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    if ((count = swapctl(SC_LIST, stbl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        free(strtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        throw_internal_error(env, "swapctl failed to get swap list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // Sum the entries to get total and free swap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    avail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    for (i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
      total += stbl->swt_ent[i].ste_pages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
      avail += stbl->swt_ent[i].ste_free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    free(strtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    return available ? ((jlong)avail * page_size) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                       ((jlong)total * page_size);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   139
#elif defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    FILE *fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    jlong total = 0, avail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    struct sysinfo si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    ret = sysinfo(&si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    if (ret != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throw_internal_error(env, "sysinfo failed to get swap size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    total = (jlong)si.totalswap * si.mem_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    avail = (jlong)si.freeswap * si.mem_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    return available ? avail : total;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   153
#else /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   154
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   155
     * XXXBSD: there's no way available to get swap info in
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   156
     *         FreeBSD.  Usage of libkvm is not an option here
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   157
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   158
    // throw_internal_error(env, "Unimplemented in FreeBSD");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   159
    return (0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
Java_com_sun_management_UnixOperatingSystem_initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    page_size = sysconf(_SC_PAGESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    psinfo_t psinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    ssize_t result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    size_t remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    char* addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        throw_internal_error(env, "Unable to open /proc/self/psinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    addr = (char *)&psinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    for (remaining = sizeof(psinfo_t); remaining > 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        result = JVM_Read(fd, addr, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw_internal_error(env, "Unable to read /proc/self/psinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        remaining -= result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        addr += result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    return (jlong) psinfo.pr_size * 1024;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   201
#elif defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    FILE *fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    unsigned long vsize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    if ((fp = fopen("/proc/self/stat", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        throw_internal_error(env, "Unable to open /proc/self/stat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // Ignore everything except the vsize entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    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
   212
        throw_internal_error(env, "Unable to get virtual memory usage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        fclose(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    fclose(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    return (jlong)vsize;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   219
#else /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   220
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   221
     * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   222
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   223
    // throw_internal_error(env, "Unimplemented in FreeBSD");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   224
    return (64 * MB);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
Java_com_sun_management_UnixOperatingSystem_getTotalSwapSpaceSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    return get_total_or_available_swap_space_size(env, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
Java_com_sun_management_UnixOperatingSystem_getFreeSwapSpaceSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    return get_total_or_available_swap_space_size(env, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
Java_com_sun_management_UnixOperatingSystem_getProcessCpuTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    jlong clk_tck, ns_per_clock_tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    jlong cpu_time_ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    struct tms time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   250
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   251
     * BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   252
     *          add a magic to handle it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   253
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   254
#if defined(__solaris__) || defined(_SC_CLK_TCK)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    clk_tck = (jlong) sysconf(_SC_CLK_TCK);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   256
#elif defined(__linux__) || defined(_ALLBSD_SOURCE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    clk_tck = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    if (clk_tck == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        throw_internal_error(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                             "sysconf failed - not able to get clock tick");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    times(&time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                      ns_per_clock_tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    return cpu_time_ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
Java_com_sun_management_UnixOperatingSystem_getFreePhysicalMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
{
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   276
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   277
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   278
     * XXBSDL no way to do it in FreeBSD
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   279
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   280
    // throw_internal_error(env, "unimplemented in FreeBSD")
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   281
    return (128 * MB);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   282
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    return (num_avail_physical_pages * page_size);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   285
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
Java_com_sun_management_UnixOperatingSystem_getTotalPhysicalMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
{
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   292
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   293
    jlong result;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   294
    int mib[2];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   295
    size_t rlen;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   296
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   297
    mib[0] = CTL_HW;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   298
    mib[1] = HW_PHYSMEM;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   299
    rlen = sizeof(result);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   300
    if (sysctl(mib, 2, &result, &rlen, NULL, 0) == -1)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   301
        result = 256 * MB;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   302
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   303
    return (result);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   304
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    return (num_physical_pages * page_size);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   307
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
Java_com_sun_management_UnixOperatingSystem_getOpenFileDescriptorCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
{
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   314
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   315
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   316
     * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   317
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   318
    // throw_internal_error(env, "Unimplemented in FreeBSD");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   319
    return (100);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   320
#else /* solaris/linux */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    DIR *dirp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    struct dirent dbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    struct dirent* dentp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    jlong fds = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    dirp = opendir("/proc/self/fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    if (dirp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        throw_internal_error(env, "Unable to open directory /proc/self/fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    // iterate through directory entries, skipping '.' and '..'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    // each entry represents an open file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (isdigit(dentp->d_name[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            fds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    closedir(dirp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    // subtract by 1 which was the fd open for this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    return (fds - 1);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   343
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
Java_com_sun_management_UnixOperatingSystem_getMaxFileDescriptorCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    struct rlimit rlp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throw_internal_error(env, "getrlimit failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    return (jlong) rlp.rlim_cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
}