1 /*
2  * Copyright (c) 2003, 2020, 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 #if defined(__FreeBSD__) || defined(__DragonFly__)
60 #include <vm/vm_param.h>
61 #endif
62 
63 #if defined(_AIX)
64 #include <libperfstat.h>
65 #endif
66 
67 static jlong page_size = 0;
68 
69 #if defined(_ALLBSD_SOURCE) || defined(_AIX)
70 #define MB      (1024UL * 1024UL)
71 #else
72 
73 /* This gets us the new structured proc interfaces of 5.6 & later */
74 /* - see comment in <sys/procfs.h> */
75 #define _STRUCTURED_PROC 1
76 #include <sys/procfs.h>
77 
78 #endif /* _ALLBSD_SOURCE */
79 
read_dir(DIR * dirp,struct dirent * entry)80 static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
81 #ifdef __solaris__
82     struct dirent* dbuf = readdir(dirp);
83     return dbuf;
84 #else /* __linux__ || _ALLBSD_SOURCE */
85     struct dirent* p;
86     if (readdir_r(dirp, entry, &p) == 0) {
87         return p;
88     } else {
89         return NULL;
90     }
91 #endif
92 }
93 
94 // true = get available swap in bytes
95 // false = get total swap in bytes
get_total_or_available_swap_space_size(JNIEnv * env,jboolean available)96 static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) {
97 #ifdef __solaris__
98     long total, avail;
99     int nswap, i, count;
100     swaptbl_t *stbl;
101     char *strtab;
102 
103     // First get the number of swap resource entries
104     if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) {
105         throw_internal_error(env, "swapctl failed to get nswap");
106         return -1;
107     }
108     if (nswap == 0) {
109         return 0;
110     }
111 
112     // Allocate storage for resource entries
113     stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) +
114                                sizeof(struct swaptable));
115     if (stbl == NULL) {
116         JNU_ThrowOutOfMemoryError(env, 0);
117         return -1;
118     }
119 
120     // Allocate storage for the table
121     strtab = (char*) malloc((nswap + 1) * MAXPATHLEN);
122     if (strtab == NULL) {
123         free(stbl);
124         JNU_ThrowOutOfMemoryError(env, 0);
125         return -1;
126     }
127 
128     for (i = 0; i < (nswap + 1); i++) {
129       stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN);
130     }
131     stbl->swt_n = nswap + 1;
132 
133     // Get the entries
134     if ((count = swapctl(SC_LIST, stbl)) < 0) {
135         free(stbl);
136         free(strtab);
137         throw_internal_error(env, "swapctl failed to get swap list");
138         return -1;
139     }
140 
141     // Sum the entries to get total and free swap
142     total = 0;
143     avail = 0;
144     for (i = 0; i < count; i++) {
145       total += stbl->swt_ent[i].ste_pages;
146       avail += stbl->swt_ent[i].ste_free;
147     }
148 
149     free(stbl);
150     free(strtab);
151     return available ? ((jlong)avail * page_size) :
152                        ((jlong)total * page_size);
153 #elif defined(__linux__)
154     int ret;
155     FILE *fp;
156     jlong total = 0, avail = 0;
157 
158     struct sysinfo si;
159     ret = sysinfo(&si);
160     if (ret != 0) {
161         throw_internal_error(env, "sysinfo failed to get swap size");
162     }
163     total = (jlong)si.totalswap * si.mem_unit;
164     avail = (jlong)si.freeswap * si.mem_unit;
165 
166     return available ? avail : total;
167 #elif defined(__APPLE__)
168     struct xsw_usage vmusage;
169     size_t size = sizeof(vmusage);
170     if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) {
171         throw_internal_error(env, "sysctlbyname failed");
172     }
173     return available ? (jlong)vmusage.xsu_avail : (jlong)vmusage.xsu_total;
174 #elif defined(__FreeBSD__)
175     struct xswdev xsw;
176     size_t mibsize, size;
177     jlong npages;
178     int mib[16], n;
179 
180     mibsize = sizeof(mib) / sizeof(mib[0]);
181     if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
182        return (0);
183     for (n = 0, npages = 0; ; n++) {
184        mib[mibsize] = n;
185        size = sizeof(xsw);
186        if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1)
187            break;
188        npages += xsw.xsw_nblks;
189        if (available)
190            npages -= xsw.xsw_used;
191     }
192     return (npages * page_size);
193 #else /* _ALLBSD_SOURCE */
194     /*
195      * XXXBSD: there's no way available to get swap info in
196      *         FreeBSD.  Usage of libkvm is not an option here
197      */
198     // throw_internal_error(env, "Unimplemented in FreeBSD");
199     return (0);
200 #endif
201 }
202 
203 JNIEXPORT void JNICALL
Java_sun_management_OperatingSystemImpl_initialize0(JNIEnv * env,jclass cls)204 Java_sun_management_OperatingSystemImpl_initialize0
205   (JNIEnv *env, jclass cls)
206 {
207     page_size = sysconf(_SC_PAGESIZE);
208 }
209 
210 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getCommittedVirtualMemorySize0(JNIEnv * env,jobject mbean)211 Java_sun_management_OperatingSystemImpl_getCommittedVirtualMemorySize0
212   (JNIEnv *env, jobject mbean)
213 {
214 #ifdef __solaris__
215     psinfo_t psinfo;
216     ssize_t result;
217     size_t remaining;
218     char* addr;
219     int fd;
220 
221     fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0);
222     if (fd < 0) {
223         throw_internal_error(env, "Unable to open /proc/self/psinfo");
224         return -1;
225     }
226 
227     addr = (char *)&psinfo;
228     for (remaining = sizeof(psinfo_t); remaining > 0;) {
229         result = JVM_Read(fd, addr, remaining);
230         if (result < 0) {
231             JVM_Close(fd);
232             throw_internal_error(env, "Unable to read /proc/self/psinfo");
233             return -1;
234         }
235         remaining -= result;
236         addr += result;
237     }
238 
239     JVM_Close(fd);
240     return (jlong) psinfo.pr_size * 1024;
241 #elif defined(__linux__)
242     FILE *fp;
243     unsigned long vsize = 0;
244 
245     if ((fp = fopen("/proc/self/stat", "r")) == NULL) {
246         throw_internal_error(env, "Unable to open /proc/self/stat");
247         return -1;
248     }
249 
250     // Ignore everything except the vsize entry
251     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) {
252         throw_internal_error(env, "Unable to get virtual memory usage");
253         fclose(fp);
254         return -1;
255     }
256 
257     fclose(fp);
258     return (jlong)vsize;
259 #elif defined(__APPLE__)
260     struct task_basic_info t_info;
261     mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
262 
263     kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
264     if (res != KERN_SUCCESS) {
265         throw_internal_error(env, "task_info failed");
266     }
267     return t_info.virtual_size;
268 #elif defined(__FreeBSD__)
269     FILE *fp;
270     unsigned long end, start;
271     jlong total = 0;
272 
273     if ((fp = fopen("/proc/curproc/map", "r")) == NULL) {
274         throw_internal_error(env, "Unable to open /proc/curproc/map");
275         return -1;
276     }
277 
278     for (;;) {
279        // Ignore everything except start and end entries
280        if (fscanf(fp, "0x%lx 0x%lx %*[^\n]\n", &start, &end) != 2 || start > end)
281            break;
282        total += end - start;
283     }
284 
285     fclose(fp);
286     return total;
287 #else /* _ALLBSD_SOURCE */
288     /*
289      * XXXBSD: there's no way available to do it in BSD, AFAIK.
290      */
291     // throw_internal_error(env, "Unimplemented in BSD");
292     return (64 * MB);
293 #endif
294 }
295 
296 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getTotalSwapSpaceSize0(JNIEnv * env,jobject mbean)297 Java_sun_management_OperatingSystemImpl_getTotalSwapSpaceSize0
298   (JNIEnv *env, jobject mbean)
299 {
300     return get_total_or_available_swap_space_size(env, JNI_FALSE);
301 }
302 
303 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getFreeSwapSpaceSize0(JNIEnv * env,jobject mbean)304 Java_sun_management_OperatingSystemImpl_getFreeSwapSpaceSize0
305   (JNIEnv *env, jobject mbean)
306 {
307     return get_total_or_available_swap_space_size(env, JNI_TRUE);
308 }
309 
310 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getProcessCpuTime0(JNIEnv * env,jobject mbean)311 Java_sun_management_OperatingSystemImpl_getProcessCpuTime0
312   (JNIEnv *env, jobject mbean)
313 {
314 #ifdef __APPLE__
315     struct rusage usage;
316     if (getrusage(RUSAGE_SELF, &usage) != 0) {
317         throw_internal_error(env, "getrusage failed");
318         return -1;
319     }
320     jlong microsecs =
321         usage.ru_utime.tv_sec * 1000 * 1000 + usage.ru_utime.tv_usec +
322         usage.ru_stime.tv_sec * 1000 * 1000 + usage.ru_stime.tv_usec;
323     return microsecs * 1000;
324 #else
325     jlong clk_tck, ns_per_clock_tick;
326     jlong cpu_time_ns;
327     struct tms time;
328 
329     /*
330      * BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so
331      *          add a magic to handle it
332      */
333 #if defined(__solaris__) || defined(_SC_CLK_TCK)
334     clk_tck = (jlong) sysconf(_SC_CLK_TCK);
335 #elif defined(__linux__) || defined(_ALLBSD_SOURCE)
336     clk_tck = 100;
337 #endif
338     if (clk_tck == -1) {
339         throw_internal_error(env,
340                              "sysconf failed - not able to get clock tick");
341         return -1;
342     }
343 
344     times(&time);
345     ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck;
346     cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) *
347                       ns_per_clock_tick;
348     return cpu_time_ns;
349 #endif
350 }
351 
352 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getFreePhysicalMemorySize0(JNIEnv * env,jobject mbean)353 Java_sun_management_OperatingSystemImpl_getFreePhysicalMemorySize0
354   (JNIEnv *env, jobject mbean)
355 {
356 #ifdef __APPLE__
357     mach_msg_type_number_t count;
358     vm_statistics_data_t vm_stats;
359     kern_return_t res;
360 
361     count = HOST_VM_INFO_COUNT;
362     res = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count);
363     if (res != KERN_SUCCESS) {
364         throw_internal_error(env, "host_statistics failed");
365         return -1;
366     }
367     return (jlong)vm_stats.free_count * page_size;
368 #elif defined(__FreeBSD__) || defined(__DragonFly__)
369     static const char *vm_stats[] = {
370        "vm.stats.vm.v_free_count",
371 #if defined(__DragonFly__) || __FreeBSD_version < 1200016
372        "vm.stats.vm.v_cache_count",
373 #endif
374        /* "vm.stats.vm.v_inactive_count", */
375        NULL
376     };
377     size_t size;
378     jlong free_pages;
379 #ifdef __DragonFly__
380     u_long i, npages;
381 #else
382     u_int i, npages;
383 #endif
384     for (i = 0, free_pages = 0, size = sizeof(npages); vm_stats[i] != NULL; i++) {
385        if (sysctlbyname(vm_stats[i], &npages, &size, NULL, 0) == -1)
386            return 0;
387        free_pages += npages;
388     }
389     return (free_pages * page_size);
390 #elif defined(_ALLBSD_SOURCE)
391     /*
392      * XXBSDL no way to do it in BSD
393      */
394     // throw_internal_error(env, "unimplemented in BSD")
395     return (128 * MB);
396 #elif defined(_AIX)
397     perfstat_memory_total_t memory_info;
398     if (-1 != perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1)) {
399         return (jlong)(memory_info.real_free * 4L * 1024L);
400     }
401     return -1;
402 #else // solaris / linux
403     jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
404     return (num_avail_physical_pages * page_size);
405 #endif
406 }
407 
408 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getTotalPhysicalMemorySize0(JNIEnv * env,jobject mbean)409 Java_sun_management_OperatingSystemImpl_getTotalPhysicalMemorySize0
410   (JNIEnv *env, jobject mbean)
411 {
412 #if defined(_ALLBSD_SOURCE) && !defined(_SC_PHYS_PAGES)
413     jlong result = 0;
414     int mib[2];
415     size_t rlen;
416 
417     mib[0] = CTL_HW;
418 #if defined (HW_MEMSIZE) // Apple
419     mib[1] = HW_MEMSIZE;
420 #elif defined(HW_PHYSMEM) // Most of BSD
421     mib[1] = HW_PHYSMEM;
422 #else
423   #error No ways to get physmem
424 #endif
425     rlen = sizeof(result);
426     if (sysctl(mib, 2, &result, &rlen, NULL, 0) != 0) {
427         throw_internal_error(env, "sysctl failed");
428         return -1;
429     }
430     return result;
431 #elif defined(_AIX)
432     perfstat_memory_total_t memory_info;
433     if (-1 != perfstat_memory_total(NULL, &memory_info, sizeof(perfstat_memory_total_t), 1)) {
434         return (jlong)(memory_info.real_total * 4L * 1024L);
435     }
436     return -1;
437 #else // solaris / linux
438     jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
439     return (num_physical_pages * page_size);
440 #endif
441 }
442 
443 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getOpenFileDescriptorCount0(JNIEnv * env,jobject mbean)444 Java_sun_management_OperatingSystemImpl_getOpenFileDescriptorCount0
445   (JNIEnv *env, jobject mbean)
446 {
447 #ifdef __APPLE__
448     // This code is influenced by the darwin lsof source
449     pid_t my_pid;
450     struct proc_bsdinfo bsdinfo;
451     struct proc_fdinfo *fds;
452     int nfiles;
453     kern_return_t kres;
454     int res;
455     size_t fds_size;
456 
457     kres = pid_for_task(mach_task_self(), &my_pid);
458     if (kres != KERN_SUCCESS) {
459         throw_internal_error(env, "pid_for_task failed");
460         return -1;
461     }
462 
463     // get the maximum number of file descriptors
464     res = proc_pidinfo(my_pid, PROC_PIDTBSDINFO, 0, &bsdinfo, PROC_PIDTBSDINFO_SIZE);
465     if (res <= 0) {
466         throw_internal_error(env, "proc_pidinfo with PROC_PIDTBSDINFO failed");
467         return -1;
468     }
469 
470     // allocate memory to hold the fd information (we don't acutally use this information
471     // but need it to get the number of open files)
472     fds_size = bsdinfo.pbi_nfiles * sizeof(struct proc_fdinfo);
473     fds = malloc(fds_size);
474     if (fds == NULL) {
475         JNU_ThrowOutOfMemoryError(env, "could not allocate space for file descriptors");
476         return -1;
477     }
478 
479     // get the list of open files - the return value is the number of bytes
480     // proc_pidinfo filled in
481     res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, fds_size);
482     if (res <= 0) {
483         free(fds);
484         throw_internal_error(env, "proc_pidinfo failed for PROC_PIDLISTFDS");
485         return -1;
486     }
487     nfiles = res / sizeof(struct proc_fdinfo);
488     free(fds);
489 
490     return nfiles;
491 #elif defined(__OpenBSD__)
492     return getdtablecount();
493 #else /* solaris/linux */
494     DIR *dirp;
495     struct dirent dbuf;
496     struct dirent* dentp;
497     jlong fds = 0;
498 
499 #if defined(_AIX)
500 /* AIX does not understand '/proc/self' - it requires the real process ID */
501 #define FD_DIR aix_fd_dir
502     char aix_fd_dir[32];     /* the pid has at most 19 digits */
503     snprintf(aix_fd_dir, 32, "/proc/%d/fd", getpid());
504 #elif defined(_ALLBSD_SOURCE)
505 #define FD_DIR "/dev/fd"
506 #else
507 #define FD_DIR "/proc/self/fd"
508 #endif
509 
510     dirp = opendir(FD_DIR);
511     if (dirp == NULL) {
512         throw_internal_error(env, "Unable to open directory " FD_DIR);
513         return -1;
514     }
515 
516     // iterate through directory entries, skipping '.' and '..'
517     // each entry represents an open file descriptor.
518     while ((dentp = read_dir(dirp, &dbuf)) != NULL) {
519         if (isdigit(dentp->d_name[0])) {
520             fds++;
521         }
522     }
523 
524     closedir(dirp);
525     // subtract by 1 which was the fd open for this implementation
526     return (fds - 1);
527 #endif
528 }
529 
530 #ifndef __linux__
531 JNIEXPORT jdouble JNICALL
Java_sun_management_OperatingSystemImpl_getSingleCpuLoad0(JNIEnv * env,jobject mbean,jint cpu_number)532 Java_sun_management_OperatingSystemImpl_getSingleCpuLoad0
533   (JNIEnv *env, jobject mbean, jint cpu_number)
534 {
535     return -1.0;
536 }
537 
538 JNIEXPORT jint JNICALL
Java_sun_management_OperatingSystemImpl_getHostConfiguredCpuCount0(JNIEnv * env,jobject mbean)539 Java_sun_management_OperatingSystemImpl_getHostConfiguredCpuCount0
540   (JNIEnv *env, jobject mbean)
541 {
542     return -1;
543 }
544 
545 JNIEXPORT jint JNICALL
Java_sun_management_OperatingSystemImpl_getHostOnlineCpuCount0(JNIEnv * env,jobject mbean)546 Java_sun_management_OperatingSystemImpl_getHostOnlineCpuCount0
547 (JNIEnv *env, jobject mbean)
548 {
549     return -1;
550 }
551 
552 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getHostTotalCpuTicks0(JNIEnv * env,jobject mbean)553 Java_sun_management_OperatingSystemImpl_getHostTotalCpuTicks0
554 (JNIEnv *env, jobject mbean)
555 {
556     return -1;
557 }
558 #endif
559 
560 JNIEXPORT jlong JNICALL
Java_sun_management_OperatingSystemImpl_getMaxFileDescriptorCount0(JNIEnv * env,jobject mbean)561 Java_sun_management_OperatingSystemImpl_getMaxFileDescriptorCount0
562   (JNIEnv *env, jobject mbean)
563 {
564     struct rlimit rlp;
565 
566     if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
567         throw_internal_error(env, "getrlimit failed");
568         return -1;
569     }
570     return (jlong) rlp.rlim_cur;
571 }
572