< prev index next >

src/jdk.management/unix/native/libmanagement_ext/OperatingSystemImpl.c

Print this page


   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  57 #include <stdlib.h>
  58 #include <unistd.h>
  59 
  60 #if defined(_AIX)
  61 #include <libperfstat.h>
  62 #endif
  63 
  64 static jlong page_size = 0;
  65 
  66 #if defined(_ALLBSD_SOURCE) || defined(_AIX)
  67 #define MB      (1024UL * 1024UL)
  68 #else
  69 
  70 /* This gets us the new structured proc interfaces of 5.6 & later */
  71 /* - see comment in <sys/procfs.h> */
  72 #define _STRUCTURED_PROC 1
  73 #include <sys/procfs.h>
  74 
  75 #endif /* _ALLBSD_SOURCE */
  76 
  77 static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
  78 #ifdef __solaris__
  79     struct dirent* dbuf = readdir(dirp);
  80     return dbuf;
  81 #else /* __linux__ || _ALLBSD_SOURCE */
  82     struct dirent* p;
  83     if (readdir_r(dirp, entry, &p) == 0) {
  84         return p;
  85     } else {
  86         return NULL;
  87     }
  88 #endif
  89 }
  90 
  91 // true = get available swap in bytes
  92 // false = get total swap in bytes
  93 static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) {
  94 #ifdef __solaris__
  95     long total, avail;
  96     int nswap, i, count;
  97     swaptbl_t *stbl;
  98     char *strtab;
  99 
 100     // First get the number of swap resource entries
 101     if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) {
 102         throw_internal_error(env, "swapctl failed to get nswap");
 103         return -1;
 104     }
 105     if (nswap == 0) {
 106         return 0;
 107     }
 108 
 109     // Allocate storage for resource entries
 110     stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +


 415     // get the list of open files - the return value is the number of bytes
 416     // proc_pidinfo filled in
 417     res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, fds_size);
 418     if (res <= 0) {
 419         free(fds);
 420         throw_internal_error(env, "proc_pidinfo failed for PROC_PIDLISTFDS");
 421         return -1;
 422     }
 423     nfiles = res / sizeof(struct proc_fdinfo);
 424     free(fds);
 425 
 426     return nfiles;
 427 #elif defined(_ALLBSD_SOURCE)
 428     /*
 429      * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
 430      */
 431     // throw_internal_error(env, "Unimplemented in FreeBSD");
 432     return (100);
 433 #else /* solaris/linux */
 434     DIR *dirp;
 435     struct dirent dbuf;
 436     struct dirent* dentp;
 437     jlong fds = 0;
 438 
 439 #if defined(_AIX)
 440 /* AIX does not understand '/proc/self' - it requires the real process ID */
 441 #define FD_DIR aix_fd_dir
 442     char aix_fd_dir[32];     /* the pid has at most 19 digits */
 443     snprintf(aix_fd_dir, 32, "/proc/%d/fd", getpid());
 444 #else
 445 #define FD_DIR "/proc/self/fd"
 446 #endif
 447 
 448     dirp = opendir(FD_DIR);
 449     if (dirp == NULL) {
 450         throw_internal_error(env, "Unable to open directory /proc/self/fd");
 451         return -1;
 452     }
 453 
 454     // iterate through directory entries, skipping '.' and '..'
 455     // each entry represents an open file descriptor.
 456     while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
 457         if (isdigit(dentp->d_name[0])) {
 458             fds++;
 459         }
 460     }
 461 
 462     closedir(dirp);
 463     // subtract by 1 which was the fd open for this implementation
 464     return (fds - 1);
 465 #endif
 466 }
 467 
 468 JNIEXPORT jlong JNICALL
 469 Java_com_sun_management_internal_OperatingSystemImpl_getMaxFileDescriptorCount0
 470   (JNIEnv *env, jobject mbean)
 471 {
 472     struct rlimit rlp;
 473 
 474     if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
 475         throw_internal_error(env, "getrlimit failed");
 476         return -1;
   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  57 #include <stdlib.h>
  58 #include <unistd.h>
  59 
  60 #if defined(_AIX)
  61 #include <libperfstat.h>
  62 #endif
  63 
  64 static jlong page_size = 0;
  65 
  66 #if defined(_ALLBSD_SOURCE) || defined(_AIX)
  67 #define MB      (1024UL * 1024UL)
  68 #else
  69 
  70 /* This gets us the new structured proc interfaces of 5.6 & later */
  71 /* - see comment in <sys/procfs.h> */
  72 #define _STRUCTURED_PROC 1
  73 #include <sys/procfs.h>
  74 
  75 #endif /* _ALLBSD_SOURCE */
  76 














  77 // true = get available swap in bytes
  78 // false = get total swap in bytes
  79 static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) {
  80 #ifdef __solaris__
  81     long total, avail;
  82     int nswap, i, count;
  83     swaptbl_t *stbl;
  84     char *strtab;
  85 
  86     // First get the number of swap resource entries
  87     if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) {
  88         throw_internal_error(env, "swapctl failed to get nswap");
  89         return -1;
  90     }
  91     if (nswap == 0) {
  92         return 0;
  93     }
  94 
  95     // Allocate storage for resource entries
  96     stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +


 401     // get the list of open files - the return value is the number of bytes
 402     // proc_pidinfo filled in
 403     res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, fds_size);
 404     if (res <= 0) {
 405         free(fds);
 406         throw_internal_error(env, "proc_pidinfo failed for PROC_PIDLISTFDS");
 407         return -1;
 408     }
 409     nfiles = res / sizeof(struct proc_fdinfo);
 410     free(fds);
 411 
 412     return nfiles;
 413 #elif defined(_ALLBSD_SOURCE)
 414     /*
 415      * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
 416      */
 417     // throw_internal_error(env, "Unimplemented in FreeBSD");
 418     return (100);
 419 #else /* solaris/linux */
 420     DIR *dirp;

 421     struct dirent* dentp;
 422     jlong fds = 0;
 423 
 424 #if defined(_AIX)
 425 /* AIX does not understand '/proc/self' - it requires the real process ID */
 426 #define FD_DIR aix_fd_dir
 427     char aix_fd_dir[32];     /* the pid has at most 19 digits */
 428     snprintf(aix_fd_dir, 32, "/proc/%d/fd", getpid());
 429 #else
 430 #define FD_DIR "/proc/self/fd"
 431 #endif
 432 
 433     dirp = opendir(FD_DIR);
 434     if (dirp == NULL) {
 435         throw_internal_error(env, "Unable to open directory /proc/self/fd");
 436         return -1;
 437     }
 438 
 439     // iterate through directory entries, skipping '.' and '..'
 440     // each entry represents an open file descriptor.
 441     while ((dentp = readdir(dirp)) != NULL) {
 442         if (isdigit(dentp->d_name[0])) {
 443             fds++;
 444         }
 445     }
 446 
 447     closedir(dirp);
 448     // subtract by 1 which was the fd open for this implementation
 449     return (fds - 1);
 450 #endif
 451 }
 452 
 453 JNIEXPORT jlong JNICALL
 454 Java_com_sun_management_internal_OperatingSystemImpl_getMaxFileDescriptorCount0
 455   (JNIEnv *env, jobject mbean)
 456 {
 457     struct rlimit rlp;
 458 
 459     if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
 460         throw_internal_error(env, "getrlimit failed");
 461         return -1;
< prev index next >