< 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


 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 #if defined(_ALLBSD_SOURCE)
  78   #define readdir64 readdir









  79 #endif

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


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

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