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
  23  * questions.
  24  */
  25 
  26 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jlong.h"
  29 #include "jvm.h"
  30 #include "management_ext.h"
  31 #include "com_sun_management_internal_OperatingSystemImpl.h"
  32 
  33 #include <sys/types.h>
  34 #include <sys/stat.h>
  35 #if defined(_ALLBSD_SOURCE)
  36 #include <sys/sysctl.h>
  37 #ifdef __APPLE__
  38 #include <sys/param.h>
  39 #include <sys/mount.h>
  40 #include <mach/mach.h>
  41 #include <sys/proc_info.h>
  42 #include <libproc.h>
  43 #endif
  44 #elif !defined(_AIX)
  45 #include <sys/swap.h>
  46 #endif
  47 #include <sys/resource.h>
  48 #include <sys/times.h>
  49 #ifndef _ALLBSD_SOURCE
  50 #include <sys/sysinfo.h>
  51 #endif
  52 #include <ctype.h>
  53 #include <dirent.h>
  54 #include <errno.h>
  55 #include <fcntl.h>
  56 #include <limits.h>
  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
 100     stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +
 101                                sizeof(struct swaptable));
 102     if (stbl == NULL) {
 103         JNU_ThrowOutOfMemoryError(env, 0);
 104         return -1;
 105     }
 106 
 107     // Allocate storage for the table
 108     strtab = (char*) malloc((nswap + 1) * MAXPATHLEN);
 109     if (strtab == NULL) {
 110         free(stbl);
 111         JNU_ThrowOutOfMemoryError(env, 0);
 112         return -1;
 113     }
 114 
 115     for (i = 0; i < (nswap + 1); i++) {
 116       stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN);
 117     }
 118     stbl->swt_n = nswap + 1;
 119 
 120     // Get the entries
 121     if ((count = swapctl(SC_LIST, stbl)) < 0) {
 122         free(stbl);
 123         free(strtab);
 124         throw_internal_error(env, "swapctl failed to get swap list");
 125         return -1;
 126     }
 127 
 128     // Sum the entries to get total and free swap
 129     total = 0;
 130     avail = 0;
 131     for (i = 0; i < count; i++) {
 132       total += stbl->swt_ent[i].ste_pages;
 133       avail += stbl->swt_ent[i].ste_free;
 134     }
 135 
 136     free(stbl);
 137     free(strtab);
 138     return available ? ((jlong)avail * page_size) :
 139                        ((jlong)total * page_size);
 140 #elif defined(__linux__)
 141     int ret;
 142     FILE *fp;
 143     jlong total = 0, avail = 0;
 144 
 145     struct sysinfo si;
 146     ret = sysinfo(&si);
 147     if (ret != 0) {
 148         throw_internal_error(env, "sysinfo failed to get swap size");
 149     }
 150     total = (jlong)si.totalswap * si.mem_unit;
 151     avail = (jlong)si.freeswap * si.mem_unit;
 152 
 153     return available ? avail : total;
 154 #elif defined(__APPLE__)
 155     struct xsw_usage vmusage;
 156     size_t size = sizeof(vmusage);
 157     if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
 158         throw_internal_error(env, "sysctlbyname failed");
 159     }
 160     return available ? (jlong)vmusage.xsu_avail : (jlong)vmusage.xsu_total;
 161 #else /* _ALLBSD_SOURCE */
 162     /*
 163      * XXXBSD: there's no way available to get swap info in
 164      *         FreeBSD.  Usage of libkvm is not an option here
 165      */
 166     // throw_internal_error(env, "Unimplemented in FreeBSD");
 167     return (0);
 168 #endif
 169 }
 170 
 171 JNIEXPORT void JNICALL
 172 Java_com_sun_management_internal_OperatingSystemImpl_initialize0
 173   (JNIEnv *env, jclass cls)
 174 {
 175     page_size = sysconf(_SC_PAGESIZE);
 176 }
 177 
 178 JNIEXPORT jlong JNICALL
 179 Java_com_sun_management_internal_OperatingSystemImpl_getCommittedVirtualMemorySize0
 180   (JNIEnv *env, jobject mbean)
 181 {
 182 #ifdef __solaris__
 183     psinfo_t psinfo;
 184     ssize_t result;
 185     size_t remaining;
 186     char* addr;
 187     int fd;
 188 
 189     fd = open64("/proc/self/psinfo", O_RDONLY, 0);
 190     if (fd < 0) {
 191         throw_internal_error(env, "Unable to open /proc/self/psinfo");
 192         return -1;
 193     }
 194 
 195     addr = (char *)&psinfo;
 196     for (remaining = sizeof(psinfo_t); remaining > 0;) {
 197         result = read(fd, addr, remaining);
 198         if (result < 0) {
 199             if (errno != EINTR) {
 200                 close(fd);
 201                 throw_internal_error(env, "Unable to read /proc/self/psinfo");
 202                 return -1;
 203             }
 204         } else {
 205             remaining -= result;
 206             addr += result;
 207         }
 208     }
 209 
 210     close(fd);
 211     return (jlong) psinfo.pr_size * 1024;
 212 #elif defined(__linux__)
 213     FILE *fp;
 214     unsigned long vsize = 0;
 215 
 216     if ((fp = fopen("/proc/self/stat", "r")) == NULL) {
 217         throw_internal_error(env, "Unable to open /proc/self/stat");
 218         return -1;
 219     }
 220 
 221     // Ignore everything except the vsize entry
 222     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) {
 223         throw_internal_error(env, "Unable to get virtual memory usage");
 224         fclose(fp);
 225         return -1;
 226     }
 227 
 228     fclose(fp);
 229     return (jlong)vsize;
 230 #elif defined(__APPLE__)
 231     struct task_basic_info t_info;
 232     mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
 233 
 234     kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
 235     if (res != KERN_SUCCESS) {
 236         throw_internal_error(env, "task_info failed");
 237     }
 238     return t_info.virtual_size;
 239 #else /* _ALLBSD_SOURCE */
 240     /*
 241      * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
 242      */
 243     // throw_internal_error(env, "Unimplemented in FreeBSD");
 244     return (64 * MB);
 245 #endif
 246 }
 247 
 248 JNIEXPORT jlong JNICALL
 249 Java_com_sun_management_internal_OperatingSystemImpl_getTotalSwapSpaceSize0
 250   (JNIEnv *env, jobject mbean)
 251 {
 252     return get_total_or_available_swap_space_size(env, JNI_FALSE);
 253 }
 254 
 255 JNIEXPORT jlong JNICALL
 256 Java_com_sun_management_internal_OperatingSystemImpl_getFreeSwapSpaceSize0
 257   (JNIEnv *env, jobject mbean)
 258 {
 259     return get_total_or_available_swap_space_size(env, JNI_TRUE);
 260 }
 261 
 262 JNIEXPORT jlong JNICALL
 263 Java_com_sun_management_internal_OperatingSystemImpl_getProcessCpuTime0
 264   (JNIEnv *env, jobject mbean)
 265 {
 266 #ifdef __APPLE__
 267     struct rusage usage;
 268     if (getrusage(RUSAGE_SELF, &usage) != 0) {
 269         throw_internal_error(env, "getrusage failed");
 270         return -1;
 271     }
 272     jlong microsecs =
 273         usage.ru_utime.tv_sec * 1000 * 1000 + usage.ru_utime.tv_usec +
 274         usage.ru_stime.tv_sec * 1000 * 1000 + usage.ru_stime.tv_usec;
 275     return microsecs * 1000;
 276 #else
 277     jlong clk_tck, ns_per_clock_tick;
 278     jlong cpu_time_ns;
 279     struct tms time;
 280 
 281     /*
 282      * BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so
 283      *          add a magic to handle it
 284      */
 285 #if defined(__solaris__) || defined(_SC_CLK_TCK)
 286     clk_tck = (jlong) sysconf(_SC_CLK_TCK);
 287 #elif defined(__linux__) || defined(_ALLBSD_SOURCE)
 288     clk_tck = 100;
 289 #endif
 290     if (clk_tck == -1) {
 291         throw_internal_error(env,
 292                              "sysconf failed - not able to get clock tick");
 293         return -1;
 294     }
 295 
 296     times(&time);
 297     ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck;
 298     cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) *
 299                       ns_per_clock_tick;
 300     return cpu_time_ns;
 301 #endif
 302 }
 303 
 304 JNIEXPORT jlong JNICALL
 305 Java_com_sun_management_internal_OperatingSystemImpl_getFreePhysicalMemorySize0
 306   (JNIEnv *env, jobject mbean)
 307 {
 308 #ifdef __APPLE__
 309     mach_msg_type_number_t count;
 310     vm_statistics_data_t vm_stats;
 311     kern_return_t res;
 312 
 313     count = HOST_VM_INFO_COUNT;
 314     res = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count);
 315     if (res != KERN_SUCCESS) {
 316         throw_internal_error(env, "host_statistics failed");
 317         return -1;
 318     }
 319     return (jlong)vm_stats.free_count * page_size;
 320 #elif defined(_ALLBSD_SOURCE)
 321     /*
 322      * XXBSDL no way to do it in FreeBSD
 323      */
 324     // throw_internal_error(env, "unimplemented in FreeBSD")
 325     return (128 * MB);
 326 #elif defined(_AIX)
 327     perfstat_memory_total_t memory_info;
 328     if (-1 != perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1)) {
 329         return (jlong)(memory_info.real_free * 4L * 1024L);
 330     }
 331     return -1;
 332 #else // solaris / linux
 333     jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
 334     return (num_avail_physical_pages * page_size);
 335 #endif
 336 }
 337 
 338 JNIEXPORT jlong JNICALL
 339 Java_com_sun_management_internal_OperatingSystemImpl_getTotalPhysicalMemorySize0
 340   (JNIEnv *env, jobject mbean)
 341 {
 342 #ifdef _ALLBSD_SOURCE
 343     jlong result = 0;
 344     int mib[2];
 345     size_t rlen;
 346 
 347     mib[0] = CTL_HW;
 348     mib[1] = HW_MEMSIZE;
 349     rlen = sizeof(result);
 350     if (sysctl(mib, 2, &result, &rlen, NULL, 0) != 0) {
 351         throw_internal_error(env, "sysctl failed");
 352         return -1;
 353     }
 354     return result;
 355 #elif defined(_AIX)
 356     perfstat_memory_total_t memory_info;
 357     if (-1 != perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1)) {
 358         return (jlong)(memory_info.real_total * 4L * 1024L);
 359     }
 360     return -1;
 361 #else // solaris / linux
 362     jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
 363     return (num_physical_pages * page_size);
 364 #endif
 365 }
 366 
 367 
 368 
 369 JNIEXPORT jlong JNICALL
 370 Java_com_sun_management_internal_OperatingSystemImpl_getOpenFileDescriptorCount0
 371   (JNIEnv *env, jobject mbean)
 372 {
 373 #ifdef __APPLE__
 374     // This code is influenced by the darwin lsof source
 375     pid_t my_pid;
 376     struct proc_bsdinfo bsdinfo;
 377     struct proc_fdinfo *fds;
 378     int nfiles;
 379     kern_return_t kres;
 380     int res;
 381     size_t fds_size;
 382 
 383     kres = pid_for_task(mach_task_self(), &my_pid);
 384     if (kres != KERN_SUCCESS) {
 385         throw_internal_error(env, "pid_for_task failed");
 386         return -1;
 387     }
 388 
 389     // get the maximum number of file descriptors
 390     res = proc_pidinfo(my_pid, PROC_PIDTBSDINFO, 0, &bsdinfo, PROC_PIDTBSDINFO_SIZE);
 391     if (res <= 0) {
 392         throw_internal_error(env, "proc_pidinfo with PROC_PIDTBSDINFO failed");
 393         return -1;
 394     }
 395 
 396     // allocate memory to hold the fd information (we don't acutally use this information
 397     // but need it to get the number of open files)
 398     fds_size = bsdinfo.pbi_nfiles * sizeof(struct proc_fdinfo);
 399     fds = malloc(fds_size);
 400     if (fds == NULL) {
 401         JNU_ThrowOutOfMemoryError(env, "could not allocate space for file descriptors");
 402         return -1;
 403     }
 404 
 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;
 466     }
 467     return (jlong) rlp.rlim_cur;
 468 }