jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c
author sundar
Mon, 08 Jul 2013 18:36:10 +0530
changeset 18856 ef883ef43731
parent 17173 ce6a6d533139
permissions -rw-r--r--
8020035: nashorn jdk buildfile BuildNashorn.gmk still renamed jdk.nashorn.internal.objects package Reviewed-by: attila, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
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) 2003, 2012, 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>
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    37
#ifdef __APPLE__
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    38
#include <sys/param.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    39
#include <sys/mount.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    40
#include <mach/mach.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    41
#include <sys/proc_info.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    42
#include <libproc.h>
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
    43
#endif
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    44
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include <sys/swap.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    46
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include <sys/resource.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include <sys/times.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    49
#ifndef _ALLBSD_SOURCE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#include <sys/sysinfo.h>
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    51
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <dirent.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
static jlong page_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    62
#if defined(_ALLBSD_SOURCE)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    63
#define MB      (1024UL * 1024UL)
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    64
#else
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    65
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/* This gets us the new structured proc interfaces of 5.6 & later */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
/* - see comment in <sys/procfs.h> */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#define _STRUCTURED_PROC 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#include <sys/procfs.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    71
#endif /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    struct dirent* dbuf = readdir(dirp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    return dbuf;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    77
#else /* __linux__ || _ALLBSD_SOURCE */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    struct dirent* p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    if (readdir_r(dirp, entry, &p) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
// true = get available swap in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
// false = get total swap in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    long total, avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    int nswap, i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    swaptbl_t *stbl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    char *strtab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // First get the number of swap resource entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        throw_internal_error(env, "swapctl failed to get nswap");
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
    if (nswap == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // Allocate storage for resource entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                               sizeof(struct swaptable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (stbl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // Allocate storage for the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    strtab = (char*) malloc((nswap + 1) * MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if (strtab == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    for (i = 0; i < (nswap + 1); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
      stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    stbl->swt_n = nswap + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // Get the entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    if ((count = swapctl(SC_LIST, stbl)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        free(strtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throw_internal_error(env, "swapctl failed to get swap list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // Sum the entries to get total and free swap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    total = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    avail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    for (i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      total += stbl->swt_ent[i].ste_pages;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
      avail += stbl->swt_ent[i].ste_free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    free(stbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    free(strtab);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    return available ? ((jlong)avail * page_size) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                       ((jlong)total * page_size);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   146
#elif defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    int ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    FILE *fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    jlong total = 0, avail = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    struct sysinfo si;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    ret = sysinfo(&si);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    if (ret != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throw_internal_error(env, "sysinfo failed to get swap size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    total = (jlong)si.totalswap * si.mem_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    avail = (jlong)si.freeswap * si.mem_unit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    return available ? avail : total;
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   160
#elif defined(__APPLE__)
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   161
    struct xsw_usage vmusage;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   162
    size_t size = sizeof(vmusage);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   163
    if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   164
        throw_internal_error(env, "sysctlbyname failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   165
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   166
    return available ? (jlong)vmusage.xsu_avail : (jlong)vmusage.xsu_total;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   167
#else /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   168
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   169
     * 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
   170
     *         FreeBSD.  Usage of libkvm is not an option here
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   171
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   172
    // throw_internal_error(env, "Unimplemented in FreeBSD");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   173
    return (0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
Java_com_sun_management_UnixOperatingSystem_initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    page_size = sysconf(_SC_PAGESIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
#ifdef __solaris__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    psinfo_t psinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    ssize_t result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    size_t remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    char* addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    if (fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        throw_internal_error(env, "Unable to open /proc/self/psinfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    addr = (char *)&psinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    for (remaining = sizeof(psinfo_t); remaining > 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        result = JVM_Read(fd, addr, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (result < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw_internal_error(env, "Unable to read /proc/self/psinfo");
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
        remaining -= result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        addr += result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    JVM_Close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    return (jlong) psinfo.pr_size * 1024;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   215
#elif defined(__linux__)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    FILE *fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    unsigned long vsize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    if ((fp = fopen("/proc/self/stat", "r")) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throw_internal_error(env, "Unable to open /proc/self/stat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    // Ignore everything except the vsize entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    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
   226
        throw_internal_error(env, "Unable to get virtual memory usage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        fclose(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    fclose(fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    return (jlong)vsize;
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   233
#elif defined(__APPLE__)
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   234
    struct task_basic_info t_info;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   235
    mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   236
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   237
    kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   238
    if (res != KERN_SUCCESS) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   239
        throw_internal_error(env, "task_info failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   240
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   241
    return t_info.virtual_size;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   242
#else /* _ALLBSD_SOURCE */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   243
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   244
     * 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
   245
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   246
    // throw_internal_error(env, "Unimplemented in FreeBSD");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   247
    return (64 * MB);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
#endif
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_getTotalSwapSpaceSize
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
    return get_total_or_available_swap_space_size(env, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
Java_com_sun_management_UnixOperatingSystem_getFreeSwapSpaceSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    return get_total_or_available_swap_space_size(env, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
Java_com_sun_management_UnixOperatingSystem_getProcessCpuTime
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
{
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   269
#ifdef __APPLE__
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   270
    struct rusage usage;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   271
    if (getrusage(RUSAGE_SELF, &usage) != 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   272
        throw_internal_error(env, "getrusage failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   273
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   274
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   275
    jlong microsecs =
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   276
        usage.ru_utime.tv_sec * 1000 * 1000 + usage.ru_utime.tv_usec +
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   277
        usage.ru_stime.tv_sec * 1000 * 1000 + usage.ru_stime.tv_usec;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   278
    return microsecs * 1000;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   279
#else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    jlong clk_tck, ns_per_clock_tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    jlong cpu_time_ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    struct tms time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   284
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   285
     * BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   286
     *          add a magic to handle it
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   287
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   288
#if defined(__solaris__) || defined(_SC_CLK_TCK)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    clk_tck = (jlong) sysconf(_SC_CLK_TCK);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   290
#elif defined(__linux__) || defined(_ALLBSD_SOURCE)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    clk_tck = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (clk_tck == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        throw_internal_error(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                             "sysconf failed - not able to get clock tick");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    times(&time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                      ns_per_clock_tick;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    return cpu_time_ns;
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   304
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
Java_com_sun_management_UnixOperatingSystem_getFreePhysicalMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
{
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   311
#ifdef __APPLE__
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   312
    mach_msg_type_number_t count;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   313
    vm_statistics_data_t vm_stats;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   314
    kern_return_t res;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   315
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   316
    count = HOST_VM_INFO_COUNT;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   317
    res = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   318
    if (res != KERN_SUCCESS) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   319
        throw_internal_error(env, "host_statistics failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   320
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   321
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   322
    return (jlong)vm_stats.free_count * page_size;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   323
#elif defined(_ALLBSD_SOURCE)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   324
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   325
     * XXBSDL no way to do it in FreeBSD
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   326
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   327
    // throw_internal_error(env, "unimplemented in FreeBSD")
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   328
    return (128 * MB);
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   329
#else // solaris / linux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    return (num_avail_physical_pages * page_size);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   332
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
Java_com_sun_management_UnixOperatingSystem_getTotalPhysicalMemorySize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
{
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   339
#ifdef _ALLBSD_SOURCE
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   340
    jlong result = 0;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   341
    int mib[2];
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   342
    size_t rlen;
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   343
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   344
    mib[0] = CTL_HW;
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   345
    mib[1] = HW_MEMSIZE;
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   346
    rlen = sizeof(result);
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   347
    if (sysctl(mib, 2, &result, &rlen, NULL, 0) != 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   348
        throw_internal_error(env, "sysctl failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   349
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   350
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   351
    return result;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   352
#else // solaris / linux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    return (num_physical_pages * page_size);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   355
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   358
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   359
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
Java_com_sun_management_UnixOperatingSystem_getOpenFileDescriptorCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
{
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   364
#ifdef __APPLE__
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   365
    // This code is influenced by the darwin lsof source
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   366
    pid_t my_pid;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   367
    struct proc_bsdinfo bsdinfo;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   368
    struct proc_fdinfo *fds;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   369
    int nfiles;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   370
    kern_return_t kres;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   371
    int res;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   372
    size_t fds_size;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   373
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   374
    kres = pid_for_task(mach_task_self(), &my_pid);
17173
ce6a6d533139 8009985: [parfait] Uninitialised variable at jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c
sla
parents: 14342
diff changeset
   375
    if (kres != KERN_SUCCESS) {
12442
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   376
        throw_internal_error(env, "pid_for_task failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   377
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   378
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   379
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   380
    // get the maximum number of file descriptors
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   381
    res = proc_pidinfo(my_pid, PROC_PIDTBSDINFO, 0, &bsdinfo, PROC_PIDTBSDINFO_SIZE);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   382
    if (res <= 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   383
        throw_internal_error(env, "proc_pidinfo with PROC_PIDTBSDINFO failed");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   384
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   385
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   386
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   387
    // allocate memory to hold the fd information (we don't acutally use this information
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   388
    // but need it to get the number of open files)
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   389
    fds_size = bsdinfo.pbi_nfiles * sizeof(struct proc_fdinfo);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   390
    fds = malloc(fds_size);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   391
    if (fds == NULL) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   392
        JNU_ThrowOutOfMemoryError(env, "could not allocate space for file descriptors");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   393
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   394
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   395
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   396
    // get the list of open files - the return value is the number of bytes
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   397
    // proc_pidinfo filled in
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   398
    res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, fds_size);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   399
    if (res <= 0) {
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   400
        free(fds);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   401
        throw_internal_error(env, "proc_pidinfo failed for PROC_PIDLISTFDS");
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   402
        return -1;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   403
    }
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   404
    nfiles = res / sizeof(struct proc_fdinfo);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   405
    free(fds);
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   406
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   407
    return nfiles;
c42ceb64ffb2 7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents: 12047
diff changeset
   408
#elif defined(_ALLBSD_SOURCE)
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   409
    /*
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   410
     * 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
   411
     */
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   412
    // throw_internal_error(env, "Unimplemented in FreeBSD");
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   413
    return (100);
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   414
#else /* solaris/linux */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    DIR *dirp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    struct dirent dbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    struct dirent* dentp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    jlong fds = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    dirp = opendir("/proc/self/fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    if (dirp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        throw_internal_error(env, "Unable to open directory /proc/self/fd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    // iterate through directory entries, skipping '.' and '..'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    // each entry represents an open file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (isdigit(dentp->d_name[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            fds++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    closedir(dirp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    // subtract by 1 which was the fd open for this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    return (fds - 1);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
   437
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
Java_com_sun_management_UnixOperatingSystem_getMaxFileDescriptorCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
  (JNIEnv *env, jobject mbean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    struct rlimit rlp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        throw_internal_error(env, "getrlimit failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    return (jlong) rlp.rlim_cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
}