1 /*
   2  * Copyright (c) 2003, 2013, 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.h"
  31 #include "sun_management_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 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) +
 111                                sizeof(struct swaptable));
 112     if (stbl == NULL) {
 113         JNU_ThrowOutOfMemoryError(env, 0);
 114         return -1;
 115     }
 116 
 117     // Allocate storage for the table
 118     strtab = (char*) malloc((nswap + 1) * MAXPATHLEN);
 119     if (strtab == NULL) {
 120         free(stbl);
 121         JNU_ThrowOutOfMemoryError(env, 0);
 122         return -1;
 123     }
 124 
 125     for (i = 0; i < (nswap + 1); i++) {
 126       stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN);
 127     }
 128     stbl->swt_n = nswap + 1;
 129 
 130     // Get the entries
 131     if ((count = swapctl(SC_LIST, stbl)) < 0) {
 132         free(stbl);
 133         free(strtab);
 134         throw_internal_error(env, "swapctl failed to get swap list");
 135         return -1;
 136     }
 137 
 138     // Sum the entries to get total and free swap
 139     total = 0;
 140     avail = 0;
 141     for (i = 0; i < count; i++) {
 142       total += stbl->swt_ent[i].ste_pages;
 143       avail += stbl->swt_ent[i].ste_free;
 144     }
 145 
 146     free(stbl);
 147     free(strtab);
 148     return available ? ((jlong)avail * page_size) :
 149                        ((jlong)total * page_size);
 150 #elif defined(__linux__)
 151     int ret;
 152     FILE *fp;
 153     jlong total = 0, avail = 0;
 154 
 155     struct sysinfo si;
 156     ret = sysinfo(&si);
 157     if (ret != 0) {
 158         throw_internal_error(env, "sysinfo failed to get swap size");
 159     }
 160     total = (jlong)si.totalswap * si.mem_unit;
 161     avail = (jlong)si.freeswap * si.mem_unit;
 162 
 163     return available ? avail : total;
 164 #elif defined(__APPLE__)
 165     struct xsw_usage vmusage;
 166     size_t size = sizeof(vmusage);
 167     if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
 168         throw_internal_error(env, "sysctlbyname failed");
 169     }
 170     return available ? (jlong)vmusage.xsu_avail : (jlong)vmusage.xsu_total;
 171 #else /* _ALLBSD_SOURCE */
 172     /*
 173      * XXXBSD: there's no way available to get swap info in
 174      *         FreeBSD.  Usage of libkvm is not an option here
 175      */
 176     // throw_internal_error(env, "Unimplemented in FreeBSD");
 177     return (0);
 178 #endif
 179 }
 180 
 181 JNIEXPORT void JNICALL
 182 Java_sun_management_OperatingSystemImpl_initialize
 183   (JNIEnv *env, jclass cls)
 184 {
 185     page_size = sysconf(_SC_PAGESIZE);
 186 }
 187 
 188 JNIEXPORT jlong JNICALL
 189 Java_sun_management_OperatingSystemImpl_getCommittedVirtualMemorySize
 190   (JNIEnv *env, jobject mbean)
 191 {
 192 #ifdef __solaris__
 193     psinfo_t psinfo;
 194     ssize_t result;
 195     size_t remaining;
 196     char* addr;
 197     int fd;
 198 
 199     fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0);
 200     if (fd < 0) {
 201         throw_internal_error(env, "Unable to open /proc/self/psinfo");
 202         return -1;
 203     }
 204 
 205     addr = (char *)&psinfo;
 206     for (remaining = sizeof(psinfo_t); remaining > 0;) {
 207         result = JVM_Read(fd, addr, remaining);
 208         if (result < 0) {
 209             JVM_Close(fd);
 210             throw_internal_error(env, "Unable to read /proc/self/psinfo");
 211             return -1;
 212         }
 213         remaining -= result;
 214         addr += result;
 215     }
 216 
 217     JVM_Close(fd);
 218     return (jlong) psinfo.pr_size * 1024;
 219 #elif defined(__linux__)
 220     FILE *fp;
 221     unsigned long vsize = 0;
 222 
 223     if ((fp = fopen("/proc/self/stat", "r")) == NULL) {
 224         throw_internal_error(env, "Unable to open /proc/self/stat");
 225         return -1;
 226     }
 227 
 228     // Ignore everything except the vsize entry
 229     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) {
 230         throw_internal_error(env, "Unable to get virtual memory usage");
 231         fclose(fp);
 232         return -1;
 233     }
 234 
 235     fclose(fp);
 236     return (jlong)vsize;
 237 #elif defined(__APPLE__)
 238     struct task_basic_info t_info;
 239     mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
 240 
 241     kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
 242     if (res != KERN_SUCCESS) {
 243         throw_internal_error(env, "task_info failed");
 244     }
 245     return t_info.virtual_size;
 246 #else /* _ALLBSD_SOURCE */
 247     /*
 248      * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
 249      */
 250     // throw_internal_error(env, "Unimplemented in FreeBSD");
 251     return (64 * MB);
 252 #endif
 253 }
 254 
 255 JNIEXPORT jlong JNICALL
 256 Java_sun_management_OperatingSystemImpl_getTotalSwapSpaceSize
 257   (JNIEnv *env, jobject mbean)
 258 {
 259     return get_total_or_available_swap_space_size(env, JNI_FALSE);
 260 }
 261 
 262 JNIEXPORT jlong JNICALL
 263 Java_sun_management_OperatingSystemImpl_getFreeSwapSpaceSize
 264   (JNIEnv *env, jobject mbean)
 265 {
 266     return get_total_or_available_swap_space_size(env, JNI_TRUE);
 267 }
 268 
 269 JNIEXPORT jlong JNICALL
 270 Java_sun_management_OperatingSystemImpl_getProcessCpuTime
 271   (JNIEnv *env, jobject mbean)
 272 {
 273 #ifdef __APPLE__
 274     struct rusage usage;
 275     if (getrusage(RUSAGE_SELF, &usage) != 0) {
 276         throw_internal_error(env, "getrusage failed");
 277         return -1;
 278     }
 279     jlong microsecs =
 280         usage.ru_utime.tv_sec * 1000 * 1000 + usage.ru_utime.tv_usec +
 281         usage.ru_stime.tv_sec * 1000 * 1000 + usage.ru_stime.tv_usec;
 282     return microsecs * 1000;
 283 #else
 284     jlong clk_tck, ns_per_clock_tick;
 285     jlong cpu_time_ns;
 286     struct tms time;
 287 
 288     /*
 289      * BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so
 290      *          add a magic to handle it
 291      */
 292 #if defined(__solaris__) || defined(_SC_CLK_TCK)
 293     clk_tck = (jlong) sysconf(_SC_CLK_TCK);
 294 #elif defined(__linux__) || defined(_ALLBSD_SOURCE)
 295     clk_tck = 100;
 296 #endif
 297     if (clk_tck == -1) {
 298         throw_internal_error(env,
 299                              "sysconf failed - not able to get clock tick");
 300         return -1;
 301     }
 302 
 303     times(&time);
 304     ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck;
 305     cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) *
 306                       ns_per_clock_tick;
 307     return cpu_time_ns;
 308 #endif
 309 }
 310 
 311 JNIEXPORT jlong JNICALL
 312 Java_sun_management_OperatingSystemImpl_getFreePhysicalMemorySize
 313   (JNIEnv *env, jobject mbean)
 314 {
 315 #ifdef __APPLE__
 316     mach_msg_type_number_t count;
 317     vm_statistics_data_t vm_stats;
 318     kern_return_t res;
 319 
 320     count = HOST_VM_INFO_COUNT;
 321     res = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count);
 322     if (res != KERN_SUCCESS) {
 323         throw_internal_error(env, "host_statistics failed");
 324         return -1;
 325     }
 326     return (jlong)vm_stats.free_count * page_size;
 327 #elif defined(_ALLBSD_SOURCE)
 328     /*
 329      * XXBSDL no way to do it in FreeBSD
 330      */
 331     // throw_internal_error(env, "unimplemented in FreeBSD")
 332     return (128 * MB);
 333 #elif defined(_AIX)
 334     perfstat_memory_total_t memory_info;
 335     if (-1 != perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1)) {
 336         return (jlong)(memory_info.real_free * 4L * 1024L);
 337     }
 338     return -1;
 339 #else // solaris / linux
 340     jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
 341     return (num_avail_physical_pages * page_size);
 342 #endif
 343 }
 344 
 345 JNIEXPORT jlong JNICALL
 346 Java_sun_management_OperatingSystemImpl_getTotalPhysicalMemorySize
 347   (JNIEnv *env, jobject mbean)
 348 {
 349 #ifdef _ALLBSD_SOURCE
 350     jlong result = 0;
 351     int mib[2];
 352     size_t rlen;
 353 
 354     mib[0] = CTL_HW;
 355     mib[1] = HW_MEMSIZE;
 356     rlen = sizeof(result);
 357     if (sysctl(mib, 2, &result, &rlen, NULL, 0) != 0) {
 358         throw_internal_error(env, "sysctl failed");
 359         return -1;
 360     }
 361     return result;
 362 #elif defined(_AIX)
 363     perfstat_memory_total_t memory_info;
 364     if (-1 != perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1)) {
 365         return (jlong)(memory_info.real_total * 4L * 1024L);
 366     }
 367     return -1;
 368 #else // solaris / linux
 369     jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
 370     return (num_physical_pages * page_size);
 371 #endif
 372 }
 373 
 374 
 375 
 376 JNIEXPORT jlong JNICALL
 377 Java_sun_management_OperatingSystemImpl_getOpenFileDescriptorCount
 378   (JNIEnv *env, jobject mbean)
 379 {
 380 #ifdef __APPLE__
 381     // This code is influenced by the darwin lsof source
 382     pid_t my_pid;
 383     struct proc_bsdinfo bsdinfo;
 384     struct proc_fdinfo *fds;
 385     int nfiles;
 386     kern_return_t kres;
 387     int res;
 388     size_t fds_size;
 389 
 390     kres = pid_for_task(mach_task_self(), &my_pid);
 391     if (kres != KERN_SUCCESS) {
 392         throw_internal_error(env, "pid_for_task failed");
 393         return -1;
 394     }
 395 
 396     // get the maximum number of file descriptors
 397     res = proc_pidinfo(my_pid, PROC_PIDTBSDINFO, 0, &bsdinfo, PROC_PIDTBSDINFO_SIZE);
 398     if (res <= 0) {
 399         throw_internal_error(env, "proc_pidinfo with PROC_PIDTBSDINFO failed");
 400         return -1;
 401     }
 402 
 403     // allocate memory to hold the fd information (we don't acutally use this information
 404     // but need it to get the number of open files)
 405     fds_size = bsdinfo.pbi_nfiles * sizeof(struct proc_fdinfo);
 406     fds = malloc(fds_size);
 407     if (fds == NULL) {
 408         JNU_ThrowOutOfMemoryError(env, "could not allocate space for file descriptors");
 409         return -1;
 410     }
 411 
 412     // get the list of open files - the return value is the number of bytes
 413     // proc_pidinfo filled in
 414     res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, fds_size);
 415     if (res <= 0) {
 416         free(fds);
 417         throw_internal_error(env, "proc_pidinfo failed for PROC_PIDLISTFDS");
 418         return -1;
 419     }
 420     nfiles = res / sizeof(struct proc_fdinfo);
 421     free(fds);
 422 
 423     return nfiles;
 424 #elif defined(_ALLBSD_SOURCE)
 425     /*
 426      * XXXBSD: there's no way available to do it in FreeBSD, AFAIK.
 427      */
 428     // throw_internal_error(env, "Unimplemented in FreeBSD");
 429     return (100);
 430 #else /* solaris/linux */
 431     DIR *dirp;
 432     struct dirent dbuf;
 433     struct dirent* dentp;
 434     jlong fds = 0;
 435 
 436     dirp = opendir("/proc/self/fd");
 437     if (dirp == NULL) {
 438         throw_internal_error(env, "Unable to open directory /proc/self/fd");
 439         return -1;
 440     }
 441 
 442     // iterate through directory entries, skipping '.' and '..'
 443     // each entry represents an open file descriptor.
 444     while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
 445         if (isdigit(dentp->d_name[0])) {
 446             fds++;
 447         }
 448     }
 449 
 450     closedir(dirp);
 451     // subtract by 1 which was the fd open for this implementation
 452     return (fds - 1);
 453 #endif
 454 }
 455 
 456 JNIEXPORT jlong JNICALL
 457 Java_sun_management_OperatingSystemImpl_getMaxFileDescriptorCount
 458   (JNIEnv *env, jobject mbean)
 459 {
 460     struct rlimit rlp;
 461 
 462     if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
 463         throw_internal_error(env, "getrlimit failed");
 464         return -1;
 465     }
 466     return (jlong) rlp.rlim_cur;
 467 }