1 /*
2 * Copyright (c) 2003, 2012, 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
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 "com_sun_management_UnixOperatingSystem.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 #else
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 static jlong page_size = 0;
61
62 #if defined(_ALLBSD_SOURCE)
63 #define MB (1024UL * 1024UL)
64 #else
65
66 /* This gets us the new structured proc interfaces of 5.6 & later */
67 /* - see comment in <sys/procfs.h> */
68 #define _STRUCTURED_PROC 1
69 #include <sys/procfs.h>
70
71 #endif /* _ALLBSD_SOURCE */
72
73 static struct dirent* read_dir(DIR* dirp, struct dirent* entry) {
74 #ifdef __solaris__
75 struct dirent* dbuf = readdir(dirp);
76 return dbuf;
77 #else /* __linux__ || _ALLBSD_SOURCE */
78 struct dirent* p;
79 if (readdir_r(dirp, entry, &p) == 0) {
80 return p;
81 } else {
82 return NULL;
309 (JNIEnv *env, jobject mbean)
310 {
311 #ifdef __APPLE__
312 mach_msg_type_number_t count;
313 vm_statistics_data_t vm_stats;
314 kern_return_t res;
315
316 count = HOST_VM_INFO_COUNT;
317 res = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count);
318 if (res != KERN_SUCCESS) {
319 throw_internal_error(env, "host_statistics failed");
320 return -1;
321 }
322 return (jlong)vm_stats.free_count * page_size;
323 #elif defined(_ALLBSD_SOURCE)
324 /*
325 * XXBSDL no way to do it in FreeBSD
326 */
327 // throw_internal_error(env, "unimplemented in FreeBSD")
328 return (128 * MB);
329 #else // solaris / linux
330 jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES);
331 return (num_avail_physical_pages * page_size);
332 #endif
333 }
334
335 JNIEXPORT jlong JNICALL
336 Java_com_sun_management_UnixOperatingSystem_getTotalPhysicalMemorySize
337 (JNIEnv *env, jobject mbean)
338 {
339 #ifdef _ALLBSD_SOURCE
340 jlong result = 0;
341 int mib[2];
342 size_t rlen;
343
344 mib[0] = CTL_HW;
345 mib[1] = HW_MEMSIZE;
346 rlen = sizeof(result);
347 if (sysctl(mib, 2, &result, &rlen, NULL, 0) != 0) {
348 throw_internal_error(env, "sysctl failed");
349 return -1;
350 }
351 return result;
352 #else // solaris / linux
353 jlong num_physical_pages = sysconf(_SC_PHYS_PAGES);
354 return (num_physical_pages * page_size);
355 #endif
356 }
357
358
359
360 JNIEXPORT jlong JNICALL
361 Java_com_sun_management_UnixOperatingSystem_getOpenFileDescriptorCount
362 (JNIEnv *env, jobject mbean)
363 {
364 #ifdef __APPLE__
365 // This code is influenced by the darwin lsof source
366 pid_t my_pid;
367 struct proc_bsdinfo bsdinfo;
368 struct proc_fdinfo *fds;
369 int nfiles;
370 kern_return_t kres;
371 int res;
|
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
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 "com_sun_management_UnixOperatingSystem.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;
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_com_sun_management_UnixOperatingSystem_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_com_sun_management_UnixOperatingSystem_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;
|