author | michaelm |
Tue, 06 Mar 2012 20:34:38 +0000 | |
changeset 12047 | 320a714614e9 |
parent 5506 | 202f599c92aa |
child 12442 | c42ceb64ffb2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2005, 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> |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
37 |
#else |
2 | 38 |
#include <sys/swap.h> |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
39 |
#endif |
2 | 40 |
#include <sys/resource.h> |
41 |
#include <sys/times.h> |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
42 |
#ifndef _ALLBSD_SOURCE |
2 | 43 |
#include <sys/sysinfo.h> |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
44 |
#endif |
2 | 45 |
#include <ctype.h> |
46 |
#include <dirent.h> |
|
47 |
#include <errno.h> |
|
48 |
#include <fcntl.h> |
|
49 |
#include <limits.h> |
|
50 |
#include <stdlib.h> |
|
51 |
#include <unistd.h> |
|
52 |
||
53 |
static jlong page_size = 0; |
|
54 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
55 |
#if defined(_ALLBSD_SOURCE) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
56 |
#define MB (1024UL * 1024UL) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
57 |
#else |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
58 |
|
2 | 59 |
/* This gets us the new structured proc interfaces of 5.6 & later */ |
60 |
/* - see comment in <sys/procfs.h> */ |
|
61 |
#define _STRUCTURED_PROC 1 |
|
62 |
#include <sys/procfs.h> |
|
63 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
64 |
#endif /* _ALLBSD_SOURCE */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
65 |
|
2 | 66 |
static struct dirent* read_dir(DIR* dirp, struct dirent* entry) { |
67 |
#ifdef __solaris__ |
|
68 |
struct dirent* dbuf = readdir(dirp); |
|
69 |
return dbuf; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
70 |
#else /* __linux__ || _ALLBSD_SOURCE */ |
2 | 71 |
struct dirent* p; |
72 |
if (readdir_r(dirp, entry, &p) == 0) { |
|
73 |
return p; |
|
74 |
} else { |
|
75 |
return NULL; |
|
76 |
} |
|
77 |
#endif |
|
78 |
} |
|
79 |
||
80 |
// true = get available swap in bytes |
|
81 |
// false = get total swap in bytes |
|
82 |
static jlong get_total_or_available_swap_space_size(JNIEnv* env, jboolean available) { |
|
83 |
#ifdef __solaris__ |
|
84 |
long total, avail; |
|
85 |
int nswap, i, count; |
|
86 |
swaptbl_t *stbl; |
|
87 |
char *strtab; |
|
88 |
||
89 |
// First get the number of swap resource entries |
|
90 |
if ((nswap = swapctl(SC_GETNSWP, NULL)) == -1) { |
|
91 |
throw_internal_error(env, "swapctl failed to get nswap"); |
|
92 |
return -1; |
|
93 |
} |
|
94 |
if (nswap == 0) { |
|
95 |
return 0; |
|
96 |
} |
|
97 |
||
98 |
// Allocate storage for resource entries |
|
99 |
stbl = (swaptbl_t*) malloc(nswap * sizeof(swapent_t) + |
|
100 |
sizeof(struct swaptable)); |
|
101 |
if (stbl == NULL) { |
|
102 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
103 |
return -1; |
|
104 |
} |
|
105 |
||
106 |
// Allocate storage for the table |
|
107 |
strtab = (char*) malloc((nswap + 1) * MAXPATHLEN); |
|
108 |
if (strtab == NULL) { |
|
109 |
free(stbl); |
|
110 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
111 |
return -1; |
|
112 |
} |
|
113 |
||
114 |
for (i = 0; i < (nswap + 1); i++) { |
|
115 |
stbl->swt_ent[i].ste_path = strtab + (i * MAXPATHLEN); |
|
116 |
} |
|
117 |
stbl->swt_n = nswap + 1; |
|
118 |
||
119 |
// Get the entries |
|
120 |
if ((count = swapctl(SC_LIST, stbl)) < 0) { |
|
121 |
free(stbl); |
|
122 |
free(strtab); |
|
123 |
throw_internal_error(env, "swapctl failed to get swap list"); |
|
124 |
return -1; |
|
125 |
} |
|
126 |
||
127 |
// Sum the entries to get total and free swap |
|
128 |
total = 0; |
|
129 |
avail = 0; |
|
130 |
for (i = 0; i < count; i++) { |
|
131 |
total += stbl->swt_ent[i].ste_pages; |
|
132 |
avail += stbl->swt_ent[i].ste_free; |
|
133 |
} |
|
134 |
||
135 |
free(stbl); |
|
136 |
free(strtab); |
|
137 |
return available ? ((jlong)avail * page_size) : |
|
138 |
((jlong)total * page_size); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
139 |
#elif defined(__linux__) |
2 | 140 |
int ret; |
141 |
FILE *fp; |
|
142 |
jlong total = 0, avail = 0; |
|
143 |
||
144 |
struct sysinfo si; |
|
145 |
ret = sysinfo(&si); |
|
146 |
if (ret != 0) { |
|
147 |
throw_internal_error(env, "sysinfo failed to get swap size"); |
|
148 |
} |
|
149 |
total = (jlong)si.totalswap * si.mem_unit; |
|
150 |
avail = (jlong)si.freeswap * si.mem_unit; |
|
151 |
||
152 |
return available ? avail : total; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
153 |
#else /* _ALLBSD_SOURCE */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
154 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
155 |
* 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
|
156 |
* FreeBSD. Usage of libkvm is not an option here |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
157 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
158 |
// throw_internal_error(env, "Unimplemented in FreeBSD"); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
159 |
return (0); |
2 | 160 |
#endif |
161 |
} |
|
162 |
||
163 |
JNIEXPORT void JNICALL |
|
164 |
Java_com_sun_management_UnixOperatingSystem_initialize |
|
165 |
(JNIEnv *env, jclass cls) |
|
166 |
{ |
|
167 |
page_size = sysconf(_SC_PAGESIZE); |
|
168 |
} |
|
169 |
||
170 |
JNIEXPORT jlong JNICALL |
|
171 |
Java_com_sun_management_UnixOperatingSystem_getCommittedVirtualMemorySize |
|
172 |
(JNIEnv *env, jobject mbean) |
|
173 |
{ |
|
174 |
#ifdef __solaris__ |
|
175 |
psinfo_t psinfo; |
|
176 |
ssize_t result; |
|
177 |
size_t remaining; |
|
178 |
char* addr; |
|
179 |
int fd; |
|
180 |
||
181 |
fd = JVM_Open("/proc/self/psinfo", O_RDONLY, 0); |
|
182 |
if (fd < 0) { |
|
183 |
throw_internal_error(env, "Unable to open /proc/self/psinfo"); |
|
184 |
return -1; |
|
185 |
} |
|
186 |
||
187 |
addr = (char *)&psinfo; |
|
188 |
for (remaining = sizeof(psinfo_t); remaining > 0;) { |
|
189 |
result = JVM_Read(fd, addr, remaining); |
|
190 |
if (result < 0) { |
|
191 |
JVM_Close(fd); |
|
192 |
throw_internal_error(env, "Unable to read /proc/self/psinfo"); |
|
193 |
return -1; |
|
194 |
} |
|
195 |
remaining -= result; |
|
196 |
addr += result; |
|
197 |
} |
|
198 |
||
199 |
JVM_Close(fd); |
|
200 |
return (jlong) psinfo.pr_size * 1024; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
201 |
#elif defined(__linux__) |
2 | 202 |
FILE *fp; |
203 |
unsigned long vsize = 0; |
|
204 |
||
205 |
if ((fp = fopen("/proc/self/stat", "r")) == NULL) { |
|
206 |
throw_internal_error(env, "Unable to open /proc/self/stat"); |
|
207 |
return -1; |
|
208 |
} |
|
209 |
||
210 |
// Ignore everything except the vsize entry |
|
211 |
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) { |
|
212 |
throw_internal_error(env, "Unable to get virtual memory usage"); |
|
213 |
fclose(fp); |
|
214 |
return -1; |
|
215 |
} |
|
216 |
||
217 |
fclose(fp); |
|
218 |
return (jlong)vsize; |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
219 |
#else /* _ALLBSD_SOURCE */ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
220 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
221 |
* 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
|
222 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
223 |
// throw_internal_error(env, "Unimplemented in FreeBSD"); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
224 |
return (64 * MB); |
2 | 225 |
#endif |
226 |
} |
|
227 |
||
228 |
JNIEXPORT jlong JNICALL |
|
229 |
Java_com_sun_management_UnixOperatingSystem_getTotalSwapSpaceSize |
|
230 |
(JNIEnv *env, jobject mbean) |
|
231 |
{ |
|
232 |
return get_total_or_available_swap_space_size(env, JNI_FALSE); |
|
233 |
} |
|
234 |
||
235 |
JNIEXPORT jlong JNICALL |
|
236 |
Java_com_sun_management_UnixOperatingSystem_getFreeSwapSpaceSize |
|
237 |
(JNIEnv *env, jobject mbean) |
|
238 |
{ |
|
239 |
return get_total_or_available_swap_space_size(env, JNI_TRUE); |
|
240 |
} |
|
241 |
||
242 |
JNIEXPORT jlong JNICALL |
|
243 |
Java_com_sun_management_UnixOperatingSystem_getProcessCpuTime |
|
244 |
(JNIEnv *env, jobject mbean) |
|
245 |
{ |
|
246 |
jlong clk_tck, ns_per_clock_tick; |
|
247 |
jlong cpu_time_ns; |
|
248 |
struct tms time; |
|
249 |
||
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
250 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
251 |
* BSDNOTE: FreeBSD implements _SC_CLK_TCK since FreeBSD 5, so |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
252 |
* add a magic to handle it |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
253 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
254 |
#if defined(__solaris__) || defined(_SC_CLK_TCK) |
2 | 255 |
clk_tck = (jlong) sysconf(_SC_CLK_TCK); |
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
256 |
#elif defined(__linux__) || defined(_ALLBSD_SOURCE) |
2 | 257 |
clk_tck = 100; |
258 |
#endif |
|
259 |
if (clk_tck == -1) { |
|
260 |
throw_internal_error(env, |
|
261 |
"sysconf failed - not able to get clock tick"); |
|
262 |
return -1; |
|
263 |
} |
|
264 |
||
265 |
times(&time); |
|
266 |
ns_per_clock_tick = (jlong) 1000 * 1000 * 1000 / (jlong) clk_tck; |
|
267 |
cpu_time_ns = ((jlong)time.tms_utime + (jlong) time.tms_stime) * |
|
268 |
ns_per_clock_tick; |
|
269 |
return cpu_time_ns; |
|
270 |
} |
|
271 |
||
272 |
JNIEXPORT jlong JNICALL |
|
273 |
Java_com_sun_management_UnixOperatingSystem_getFreePhysicalMemorySize |
|
274 |
(JNIEnv *env, jobject mbean) |
|
275 |
{ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
276 |
#ifdef _ALLBSD_SOURCE |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
277 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
278 |
* XXBSDL no way to do it in FreeBSD |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
279 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
280 |
// throw_internal_error(env, "unimplemented in FreeBSD") |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
281 |
return (128 * MB); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
282 |
#else |
2 | 283 |
jlong num_avail_physical_pages = sysconf(_SC_AVPHYS_PAGES); |
284 |
return (num_avail_physical_pages * page_size); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
285 |
#endif |
2 | 286 |
} |
287 |
||
288 |
JNIEXPORT jlong JNICALL |
|
289 |
Java_com_sun_management_UnixOperatingSystem_getTotalPhysicalMemorySize |
|
290 |
(JNIEnv *env, jobject mbean) |
|
291 |
{ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
292 |
#ifdef _ALLBSD_SOURCE |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
293 |
jlong result; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
294 |
int mib[2]; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
295 |
size_t rlen; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
296 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
297 |
mib[0] = CTL_HW; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
298 |
mib[1] = HW_PHYSMEM; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
299 |
rlen = sizeof(result); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
300 |
if (sysctl(mib, 2, &result, &rlen, NULL, 0) == -1) |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
301 |
result = 256 * MB; |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
302 |
|
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
303 |
return (result); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
304 |
#else |
2 | 305 |
jlong num_physical_pages = sysconf(_SC_PHYS_PAGES); |
306 |
return (num_physical_pages * page_size); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
307 |
#endif |
2 | 308 |
} |
309 |
||
310 |
JNIEXPORT jlong JNICALL |
|
311 |
Java_com_sun_management_UnixOperatingSystem_getOpenFileDescriptorCount |
|
312 |
(JNIEnv *env, jobject mbean) |
|
313 |
{ |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
314 |
#ifdef _ALLBSD_SOURCE |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
315 |
/* |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
316 |
* 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
|
317 |
*/ |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
318 |
// throw_internal_error(env, "Unimplemented in FreeBSD"); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
319 |
return (100); |
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
320 |
#else /* solaris/linux */ |
2 | 321 |
DIR *dirp; |
322 |
struct dirent dbuf; |
|
323 |
struct dirent* dentp; |
|
324 |
jlong fds = 0; |
|
325 |
||
326 |
dirp = opendir("/proc/self/fd"); |
|
327 |
if (dirp == NULL) { |
|
328 |
throw_internal_error(env, "Unable to open directory /proc/self/fd"); |
|
329 |
return -1; |
|
330 |
} |
|
331 |
||
332 |
// iterate through directory entries, skipping '.' and '..' |
|
333 |
// each entry represents an open file descriptor. |
|
334 |
while ((dentp = read_dir(dirp, &dbuf)) != NULL) { |
|
335 |
if (isdigit(dentp->d_name[0])) { |
|
336 |
fds++; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
closedir(dirp); |
|
341 |
// subtract by 1 which was the fd open for this implementation |
|
342 |
return (fds - 1); |
|
12047
320a714614e9
7113349: Initial changeset for Macosx port to jdk
michaelm
parents:
5506
diff
changeset
|
343 |
#endif |
2 | 344 |
} |
345 |
||
346 |
JNIEXPORT jlong JNICALL |
|
347 |
Java_com_sun_management_UnixOperatingSystem_getMaxFileDescriptorCount |
|
348 |
(JNIEnv *env, jobject mbean) |
|
349 |
{ |
|
350 |
struct rlimit rlp; |
|
351 |
||
352 |
if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) { |
|
353 |
throw_internal_error(env, "getrlimit failed"); |
|
354 |
return -1; |
|
355 |
} |
|
356 |
return (jlong) rlp.rlim_cur; |
|
357 |
} |