author | sundar |
Mon, 08 Jul 2013 18:36:10 +0530 | |
changeset 18856 | ef883ef43731 |
parent 17173 | ce6a6d533139 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12442
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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> |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
35 |
#if defined(_ALLBSD_SOURCE) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
36 |
#include <sys/sysctl.h> |
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
37 |
#ifdef __APPLE__ |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
38 |
#include <sys/param.h> |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
39 |
#include <sys/mount.h> |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
40 |
#include <mach/mach.h> |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
41 |
#include <sys/proc_info.h> |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
42 |
#include <libproc.h> |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
43 |
#endif |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
44 |
#else |
2 | 45 |
#include <sys/swap.h> |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
46 |
#endif |
2 | 47 |
#include <sys/resource.h> |
48 |
#include <sys/times.h> |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
49 |
#ifndef _ALLBSD_SOURCE |
2 | 50 |
#include <sys/sysinfo.h> |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
51 |
#endif |
2 | 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 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
62 |
#if defined(_ALLBSD_SOURCE) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
63 |
#define MB (1024UL * 1024UL) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
64 |
#else |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
65 |
|
2 | 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 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
71 |
#endif /* _ALLBSD_SOURCE */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
72 |
|
2 | 73 |
static struct dirent* read_dir(DIR* dirp, struct dirent* entry) { |
74 |
#ifdef __solaris__ |
|
75 |
struct dirent* dbuf = readdir(dirp); |
|
76 |
return dbuf; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
77 |
#else /* __linux__ || _ALLBSD_SOURCE */ |
2 | 78 |
struct dirent* p; |
79 |
if (readdir_r(dirp, entry, &p) == 0) { |
|
80 |
return p; |
|
81 |
} else { |
|
82 |
return NULL; |
|
83 |
} |
|
84 |
#endif |
|
85 |
} |
|
86 |
||
87 |
// true = get available swap in bytes |
|
88 |
// false = get total swap in bytes |
|
89 |
static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) { |
|
90 |
#ifdef __solaris__ |
|
91 |
long total, avail; |
|
92 |
int nswap, i, count; |
|
93 |
swaptbl_t *stbl; |
|
94 |
char *strtab; |
|
95 |
||
96 |
// First get the number of swap resource entries |
|
97 |
if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) { |
|
98 |
throw_internal_error(env, "swapctl failed to get nswap"); |
|
99 |
return -1; |
|
100 |
} |
|
101 |
if (nswap == 0) { |
|
102 |
return 0; |
|
103 |
} |
|
104 |
||
105 |
// Allocate storage for resource entries |
|
106 |
stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) + |
|
107 |
sizeof(struct swaptable)); |
|
108 |
if (stbl == NULL) { |
|
109 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
110 |
return -1; |
|
111 |
} |
|
112 |
||
113 |
// Allocate storage for the table |
|
114 |
strtab = (char*) malloc((nswap + 1) * MAXPATHLEN); |
|
115 |
if (strtab == NULL) { |
|
116 |
free(stbl); |
|
117 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
118 |
return -1; |
|
119 |
} |
|
120 |
||
121 |
for (i = 0; i < (nswap + 1); i++) { |
|
122 |
stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN); |
|
123 |
} |
|
124 |
stbl->swt_n = nswap + 1; |
|
125 |
||
126 |
// Get the entries |
|
127 |
if ((count = swapctl(SC_LIST, stbl)) < 0) { |
|
128 |
free(stbl); |
|
129 |
free(strtab); |
|
130 |
throw_internal_error(env, "swapctl failed to get swap list"); |
|
131 |
return -1; |
|
132 |
} |
|
133 |
||
134 |
// Sum the entries to get total and free swap |
|
135 |
total = 0; |
|
136 |
avail = 0; |
|
137 |
for (i = 0; i < count; i++) { |
|
138 |
total += stbl->swt_ent[i].ste_pages; |
|
139 |
avail += stbl->swt_ent[i].ste_free; |
|
140 |
} |
|
141 |
||
142 |
free(stbl); |
|
143 |
free(strtab); |
|
144 |
return available ? ((jlong)avail * page_size) : |
|
145 |
((jlong)total * page_size); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
146 |
#elif defined(__linux__) |
2 | 147 |
int ret; |
148 |
FILE *fp; |
|
149 |
jlong total = 0, avail = 0; |
|
150 |
||
151 |
struct sysinfo si; |
|
152 |
ret = sysinfo(&si); |
|
153 |
if (ret != 0) { |
|
154 |
throw_internal_error(env, "sysinfo failed to get swap size"); |
|
155 |
} |
|
156 |
total = (jlong)si.totalswap * si.mem_unit; |
|
157 |
avail = (jlong)si.freeswap * si.mem_unit; |
|
158 |
||
159 |
return available ? avail : total; |
|
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
160 |
#elif defined(__APPLE__) |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
161 |
struct xsw_usage vmusage; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
162 |
size_t size = sizeof(vmusage); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
163 |
if (sysctlbyname("vm.swapusage", &vmusage, &size, NULL, 0) != 0) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
164 |
throw_internal_error(env, "sysctlbyname failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
165 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
166 |
return available ? (jlong)vmusage.xsu_avail : (jlong)vmusage.xsu_total; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
167 |
#else /* _ALLBSD_SOURCE */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
168 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
169 |
* XXXBSD: there's no way available to get swap info in |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
170 |
* FreeBSD. Usage of libkvm is not an option here |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
171 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
172 |
// throw_internal_error(env, "Unimplemented in FreeBSD"); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
173 |
return (0); |
2 | 174 |
#endif |
175 |
} |
|
176 |
||
177 |
JNIEXPORT void JNICALL |
|
178 |
Java_com_sun_management_UnixOperatingSystem_initialize |
|
179 |
(JNIEnv *env, jclass cls) |
|
180 |
{ |
|
181 |
page_size = sysconf(_SC_PAGESIZE); |
|
182 |
} |
|
183 |
||
184 |
JNIEXPORT jlong JNICALL |
|
185 |
Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize |
|
186 |
(JNIEnv *env, jobject mbean) |
|
187 |
{ |
|
188 |
#ifdef __solaris__ |
|
189 |
psinfo_t psinfo; |
|
190 |
ssize_t result; |
|
191 |
size_t remaining; |
|
192 |
char* addr; |
|
193 |
int fd; |
|
194 |
||
195 |
fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0); |
|
196 |
if (fd < 0) { |
|
197 |
throw_internal_error(env, "Unable to open /proc/self/psinfo"); |
|
198 |
return -1; |
|
199 |
} |
|
200 |
||
201 |
addr = (char *)&psinfo; |
|
202 |
for (remaining = sizeof(psinfo_t); remaining > 0;) { |
|
203 |
result = JVM_Read(fd, addr, remaining); |
|
204 |
if (result < 0) { |
|
205 |
JVM_Close(fd); |
|
206 |
throw_internal_error(env, "Unable to read /proc/self/psinfo"); |
|
207 |
return -1; |
|
208 |
} |
|
209 |
remaining -= result; |
|
210 |
addr += result; |
|
211 |
} |
|
212 |
||
213 |
JVM_Close(fd); |
|
214 |
return (jlong) psinfo.pr_size * 1024; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
215 |
#elif defined(__linux__) |
2 | 216 |
FILE *fp; |
217 |
unsigned long vsize = 0; |
|
218 |
||
219 |
if ((fp = fopen("/proc/self/stat", "r")) == NULL) { |
|
220 |
throw_internal_error(env, "Unable to open /proc/self/stat"); |
|
221 |
return -1; |
|
222 |
} |
|
223 |
||
224 |
// Ignore everything except the vsize entry |
|
225 |
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) { |
|
226 |
throw_internal_error(env, "Unable to get virtual memory usage"); |
|
227 |
fclose(fp); |
|
228 |
return -1; |
|
229 |
} |
|
230 |
||
231 |
fclose(fp); |
|
232 |
return (jlong)vsize; |
|
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
233 |
#elif defined(__APPLE__) |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
234 |
struct task_basic_info t_info; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
235 |
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
236 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
237 |
kern_return_t res = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
238 |
if (res != KERN_SUCCESS) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
239 |
throw_internal_error(env, "task_info failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
240 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
241 |
return t_info.virtual_size; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
242 |
#else /* _ALLBSD_SOURCE */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
243 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
244 |
* XXXBSD: there's no way available to do it in FreeBSD, AFAIK. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
245 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
246 |
// throw_internal_error(env, "Unimplemented in FreeBSD"); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
247 |
return (64 * MB); |
2 | 248 |
#endif |
249 |
} |
|
250 |
||
251 |
JNIEXPORT jlong JNICALL |
|
252 |
Java_com_sun_management_UnixOperatingSystem_getTotalSwapSpaceSize |
|
253 |
(JNIEnv *env, jobject mbean) |
|
254 |
{ |
|
255 |
return get_total_or_available_swap_space_size(env, JNI_FALSE); |
|
256 |
} |
|
257 |
||
258 |
JNIEXPORT jlong JNICALL |
|
259 |
Java_com_sun_management_UnixOperatingSystem_getFreeSwapSpaceSize |
|
260 |
(JNIEnv *env, jobject mbean) |
|
261 |
{ |
|
262 |
return get_total_or_available_swap_space_size(env, JNI_TRUE); |
|
263 |
} |
|
264 |
||
265 |
JNIEXPORT jlong JNICALL |
|
266 |
Java_com_sun_management_UnixOperatingSystem_getProcessCpuTime |
|
267 |
(JNIEnv *env, jobject mbean) |
|
268 |
{ |
|
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
269 |
#ifdef __APPLE__ |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
270 |
struct rusage usage; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
271 |
if (getrusage(RUSAGE_SELF, &usage) != 0) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
272 |
throw_internal_error(env, "getrusage failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
273 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
274 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
275 |
jlong microsecs = |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
276 |
usage.ru_utime.tv_sec * 1000 * 1000 + usage.ru_utime.tv_usec + |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
277 |
usage.ru_stime.tv_sec * 1000 * 1000 + usage.ru_stime.tv_usec; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
278 |
return microsecs * 1000; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
279 |
#else |
2 | 280 |
jlong clk_tck, ns_per_clock_tick; |
281 |
jlong cpu_time_ns; |
|
282 |
struct tms time; |
|
283 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
284 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
285 |
* BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
286 |
* add a magic to handle it |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
287 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
288 |
#if defined(__solaris__) || defined(_SC_CLK_TCK) |
2 | 289 |
clk_tck = (jlong) sysconf(_SC_CLK_TCK); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
290 |
#elif defined(__linux__) || defined(_ALLBSD_SOURCE) |
2 | 291 |
clk_tck = 100; |
292 |
#endif |
|
293 |
if (clk_tck == -1) { |
|
294 |
throw_internal_error(env, |
|
295 |
"sysconf failed - not able to get clock tick"); |
|
296 |
return -1; |
|
297 |
} |
|
298 |
||
299 |
times(&time); |
|
300 |
ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck; |
|
301 |
cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) * |
|
302 |
ns_per_clock_tick; |
|
303 |
return cpu_time_ns; |
|
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
304 |
#endif |
2 | 305 |
} |
306 |
||
307 |
JNIEXPORT jlong JNICALL |
|
308 |
Java_com_sun_management_UnixOperatingSystem_getFreePhysicalMemorySize |
|
309 |
(JNIEnv *env, jobject mbean) |
|
310 |
{ |
|
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
311 |
#ifdef __APPLE__ |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
312 |
mach_msg_type_number_t count; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
313 |
vm_statistics_data_t vm_stats; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
314 |
kern_return_t res; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
315 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
316 |
count = HOST_VM_INFO_COUNT; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
317 |
res = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stats, &count); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
318 |
if (res != KERN_SUCCESS) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
319 |
throw_internal_error(env, "host_statistics failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
320 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
321 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
322 |
return (jlong)vm_stats.free_count * page_size; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
323 |
#elif defined(_ALLBSD_SOURCE) |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
324 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
325 |
* XXBSDL no way to do it in FreeBSD |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
326 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
327 |
// throw_internal_error(env, "unimplemented in FreeBSD") |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
328 |
return (128 * MB); |
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
329 |
#else // solaris / linux |
2 | 330 |
jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES); |
331 |
return (num_avail_physical_pages * page_size); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
332 |
#endif |
2 | 333 |
} |
334 |
||
335 |
JNIEXPORT jlong JNICALL |
|
336 |
Java_com_sun_management_UnixOperatingSystem_getTotalPhysicalMemorySize |
|
337 |
(JNIEnv *env, jobject mbean) |
|
338 |
{ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
339 |
#ifdef _ALLBSD_SOURCE |
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
340 |
jlong result = 0; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
341 |
int mib[2]; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
342 |
size_t rlen; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
343 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
344 |
mib[0] = CTL_HW; |
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
345 |
mib[1] = HW_MEMSIZE; |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
346 |
rlen = sizeof(result); |
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
347 |
if (sysctl(mib, 2, &result, &rlen, NULL, 0) != 0) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
348 |
throw_internal_error(env, "sysctl failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
349 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
350 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
351 |
return result; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
352 |
#else // solaris / linux |
2 | 353 |
jlong num_physical_pages = sysconf(_SC_PHYS_PAGES); |
354 |
return (num_physical_pages * page_size); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
355 |
#endif |
2 | 356 |
} |
357 |
||
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
358 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
359 |
|
2 | 360 |
JNIEXPORT jlong JNICALL |
361 |
Java_com_sun_management_UnixOperatingSystem_getOpenFileDescriptorCount |
|
362 |
(JNIEnv *env, jobject mbean) |
|
363 |
{ |
|
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
364 |
#ifdef __APPLE__ |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
365 |
// This code is influenced by the darwin lsof source |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
366 |
pid_t my_pid; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
367 |
struct proc_bsdinfo bsdinfo; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
368 |
struct proc_fdinfo *fds; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
369 |
int nfiles; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
370 |
kern_return_t kres; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
371 |
int res; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
372 |
size_t fds_size; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
373 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
374 |
kres = pid_for_task(mach_task_self(), &my_pid); |
17173
ce6a6d533139
8009985: [parfait] Uninitialised variable at jdk/src/solaris/native/com/sun/management/UnixOperatingSystem_md.c
sla
parents:
14342
diff
changeset
|
375 |
if (kres != KERN_SUCCESS) { |
12442
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
376 |
throw_internal_error(env, "pid_for_task failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
377 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
378 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
379 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
380 |
// get the maximum number of file descriptors |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
381 |
res = proc_pidinfo(my_pid, PROC_PIDTBSDINFO, 0, &bsdinfo, PROC_PIDTBSDINFO_SIZE); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
382 |
if (res <= 0) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
383 |
throw_internal_error(env, "proc_pidinfo with PROC_PIDTBSDINFO failed"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
384 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
385 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
386 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
387 |
// allocate memory to hold the fd information (we don't acutally use this information |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
388 |
// but need it to get the number of open files) |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
389 |
fds_size = bsdinfo.pbi_nfiles * sizeof(struct proc_fdinfo); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
390 |
fds = malloc(fds_size); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
391 |
if (fds == NULL) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
392 |
JNU_ThrowOutOfMemoryError(env, "could not allocate space for file descriptors"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
393 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
394 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
395 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
396 |
// get the list of open files - the return value is the number of bytes |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
397 |
// proc_pidinfo filled in |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
398 |
res = proc_pidinfo(my_pid, PROC_PIDLISTFDS, 0, fds, fds_size); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
399 |
if (res <= 0) { |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
400 |
free(fds); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
401 |
throw_internal_error(env, "proc_pidinfo failed for PROC_PIDLISTFDS"); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
402 |
return -1; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
403 |
} |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
404 |
nfiles = res / sizeof(struct proc_fdinfo); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
405 |
free(fds); |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
406 |
|
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
407 |
return nfiles; |
c42ceb64ffb2
7147848: com.sun.management.UnixOperatingSystem uses hardcoded dummy values [macosx]
sla
parents:
12047
diff
changeset
|
408 |
#elif defined(_ALLBSD_SOURCE) |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
409 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
410 |
* XXXBSD: there's no way available to do it in FreeBSD, AFAIK. |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
411 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
412 |
// throw_internal_error(env, "Unimplemented in FreeBSD"); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
413 |
return (100); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
414 |
#else /* solaris/linux */ |
2 | 415 |
DIR *dirp; |
416 |
struct dirent dbuf; |
|
417 |
struct dirent* dentp; |
|
418 |
jlong fds = 0; |
|
419 |
||
420 |
dirp = opendir("/proc/self/fd"); |
|
421 |
if (dirp == NULL) { |
|
422 |
throw_internal_error(env, "Unable to open directory /proc/self/fd"); |
|
423 |
return -1; |
|
424 |
} |
|
425 |
||
426 |
// iterate through directory entries, skipping '.' and '..' |
|
427 |
// each entry represents an open file descriptor. |
|
428 |
while ((dentp = read_dir(dirp, &dbuf)) != NULL) { |
|
429 |
if (isdigit(dentp->d_name[0])) { |
|
430 |
fds++; |
|
431 |
} |
|
432 |
} |
|
433 |
||
434 |
closedir(dirp); |
|
435 |
// subtract by 1 which was the fd open for this implementation |
|
436 |
return (fds - 1); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
437 |
#endif |
2 | 438 |
} |
439 |
||
440 |
JNIEXPORT jlong JNICALL |
|
441 |
Java_com_sun_management_UnixOperatingSystem_getMaxFileDescriptorCount |
|
442 |
(JNIEnv *env, jobject mbean) |
|
443 |
{ |
|
444 |
struct rlimit rlp; |
|
445 |
||
446 |
if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) { |
|
447 |
throw_internal_error(env, "getrlimit failed"); |
|
448 |
return -1; |
|
449 |
} |
|
450 |
return (jlong) rlp.rlim_cur; |
|
451 |
} |