Mercurial > hg > truffle
annotate src/os/solaris/vm/perfMemory_solaris.cpp @ 7606:bf623b2d5508
8006204: please JTREGify test/compiler/7190310/Test7190310.java
Summary: Add proper jtreg annotations in the preceding comment, including an explicit timeout.
Reviewed-by: kvn, twisti
Contributed-by: david.r.chase@oracle.com
author | kvn |
---|---|
date | Wed, 16 Jan 2013 14:55:18 -0800 |
parents | 716c64bda5ba |
children | 4b7cf00ccb08 |
rev | line source |
---|---|
0 | 1 /* |
6842
b9a9ed0f8eeb
7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents:
6197
diff
changeset
|
2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. |
0 | 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. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1353
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1353
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1353
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #include "precompiled.hpp" |
26 #include "classfile/vmSymbols.hpp" | |
27 #include "memory/allocation.inline.hpp" | |
28 #include "memory/resourceArea.hpp" | |
29 #include "oops/oop.inline.hpp" | |
30 #include "os_solaris.inline.hpp" | |
31 #include "runtime/handles.inline.hpp" | |
32 #include "runtime/perfMemory.hpp" | |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
33 #include "services/memTracker.hpp" |
1972 | 34 #include "utilities/exceptions.hpp" |
0 | 35 |
36 // put OS-includes here | |
37 # include <sys/types.h> | |
38 # include <sys/mman.h> | |
39 # include <errno.h> | |
40 # include <stdio.h> | |
41 # include <unistd.h> | |
42 # include <sys/stat.h> | |
43 # include <signal.h> | |
44 # include <pwd.h> | |
45 # include <procfs.h> | |
46 | |
47 | |
48 static char* backing_store_file_name = NULL; // name of the backing store | |
49 // file, if successfully created. | |
50 | |
51 // Standard Memory Implementation Details | |
52 | |
53 // create the PerfData memory region in standard memory. | |
54 // | |
55 static char* create_standard_memory(size_t size) { | |
56 | |
57 // allocate an aligned chuck of memory | |
58 char* mapAddress = os::reserve_memory(size); | |
59 | |
60 if (mapAddress == NULL) { | |
61 return NULL; | |
62 } | |
63 | |
64 // commit memory | |
65 if (!os::commit_memory(mapAddress, size)) { | |
66 if (PrintMiscellaneous && Verbose) { | |
67 warning("Could not commit PerfData memory\n"); | |
68 } | |
69 os::release_memory(mapAddress, size); | |
70 return NULL; | |
71 } | |
72 | |
73 return mapAddress; | |
74 } | |
75 | |
76 // delete the PerfData memory region | |
77 // | |
78 static void delete_standard_memory(char* addr, size_t size) { | |
79 | |
80 // there are no persistent external resources to cleanup for standard | |
81 // memory. since DestroyJavaVM does not support unloading of the JVM, | |
82 // cleanup of the memory resource is not performed. The memory will be | |
83 // reclaimed by the OS upon termination of the process. | |
84 // | |
85 return; | |
86 } | |
87 | |
88 // save the specified memory region to the given file | |
89 // | |
90 // Note: this function might be called from signal handler (by os::abort()), | |
91 // don't allocate heap memory. | |
92 // | |
93 static void save_memory_to_file(char* addr, size_t size) { | |
94 | |
95 const char* destfile = PerfMemory::get_perfdata_file_path(); | |
96 assert(destfile[0] != '\0', "invalid PerfData file path"); | |
97 | |
98 int result; | |
99 | |
100 RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), | |
101 result);; | |
102 if (result == OS_ERR) { | |
103 if (PrintMiscellaneous && Verbose) { | |
104 warning("Could not create Perfdata save file: %s: %s\n", | |
105 destfile, strerror(errno)); | |
106 } | |
107 } else { | |
108 | |
109 int fd = result; | |
110 | |
111 for (size_t remaining = size; remaining > 0;) { | |
112 | |
113 RESTARTABLE(::write(fd, addr, remaining), result); | |
114 if (result == OS_ERR) { | |
115 if (PrintMiscellaneous && Verbose) { | |
116 warning("Could not write Perfdata save file: %s: %s\n", | |
117 destfile, strerror(errno)); | |
118 } | |
119 break; | |
120 } | |
121 remaining -= (size_t)result; | |
122 addr += result; | |
123 } | |
124 | |
125 RESTARTABLE(::close(fd), result); | |
126 if (PrintMiscellaneous && Verbose) { | |
127 if (result == OS_ERR) { | |
128 warning("Could not close %s: %s\n", destfile, strerror(errno)); | |
129 } | |
130 } | |
131 } | |
6197 | 132 FREE_C_HEAP_ARRAY(char, destfile, mtInternal); |
0 | 133 } |
134 | |
135 | |
136 // Shared Memory Implementation Details | |
137 | |
138 // Note: the solaris and linux shared memory implementation uses the mmap | |
139 // interface with a backing store file to implement named shared memory. | |
140 // Using the file system as the name space for shared memory allows a | |
141 // common name space to be supported across a variety of platforms. It | |
142 // also provides a name space that Java applications can deal with through | |
143 // simple file apis. | |
144 // | |
145 // The solaris and linux implementations store the backing store file in | |
146 // a user specific temporary directory located in the /tmp file system, | |
147 // which is always a local file system and is sometimes a RAM based file | |
148 // system. | |
149 | |
150 // return the user specific temporary directory name. | |
151 // | |
152 // the caller is expected to free the allocated memory. | |
153 // | |
154 static char* get_user_tmp_dir(const char* user) { | |
155 | |
156 const char* tmpdir = os::get_temp_directory(); | |
157 const char* perfdir = PERFDATA_NAME; | |
1353
a2ea687fdc7c
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
605
diff
changeset
|
158 size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3; |
6197 | 159 char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); |
0 | 160 |
161 // construct the path name to user specific tmp directory | |
1353
a2ea687fdc7c
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
605
diff
changeset
|
162 snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user); |
0 | 163 |
164 return dirname; | |
165 } | |
166 | |
167 // convert the given file name into a process id. if the file | |
168 // does not meet the file naming constraints, return 0. | |
169 // | |
170 static pid_t filename_to_pid(const char* filename) { | |
171 | |
172 // a filename that doesn't begin with a digit is not a | |
173 // candidate for conversion. | |
174 // | |
175 if (!isdigit(*filename)) { | |
176 return 0; | |
177 } | |
178 | |
179 // check if file name can be converted to an integer without | |
180 // any leftover characters. | |
181 // | |
182 char* remainder = NULL; | |
183 errno = 0; | |
184 pid_t pid = (pid_t)strtol(filename, &remainder, 10); | |
185 | |
186 if (errno != 0) { | |
187 return 0; | |
188 } | |
189 | |
190 // check for left over characters. If any, then the filename is | |
191 // not a candidate for conversion. | |
192 // | |
193 if (remainder != NULL && *remainder != '\0') { | |
194 return 0; | |
195 } | |
196 | |
197 // successful conversion, return the pid | |
198 return pid; | |
199 } | |
200 | |
201 | |
202 // check if the given path is considered a secure directory for | |
203 // the backing store files. Returns true if the directory exists | |
204 // and is considered a secure location. Returns false if the path | |
605 | 205 // is a symbolic link or if an error occurred. |
0 | 206 // |
207 static bool is_directory_secure(const char* path) { | |
208 struct stat statbuf; | |
209 int result = 0; | |
210 | |
211 RESTARTABLE(::lstat(path, &statbuf), result); | |
212 if (result == OS_ERR) { | |
213 return false; | |
214 } | |
215 | |
216 // the path exists, now check it's mode | |
217 if (S_ISLNK(statbuf.st_mode) || !S_ISDIR(statbuf.st_mode)) { | |
218 // the path represents a link or some non-directory file type, | |
219 // which is not what we expected. declare it insecure. | |
220 // | |
221 return false; | |
222 } | |
223 else { | |
224 // we have an existing directory, check if the permissions are safe. | |
225 // | |
226 if ((statbuf.st_mode & (S_IWGRP|S_IWOTH)) != 0) { | |
227 // the directory is open for writing and could be subjected | |
228 // to a symlnk attack. declare it insecure. | |
229 // | |
230 return false; | |
231 } | |
232 } | |
233 return true; | |
234 } | |
235 | |
236 | |
237 // return the user name for the given user id | |
238 // | |
239 // the caller is expected to free the allocated memory. | |
240 // | |
241 static char* get_user_name(uid_t uid) { | |
242 | |
243 struct passwd pwent; | |
244 | |
245 // determine the max pwbuf size from sysconf, and hardcode | |
246 // a default if this not available through sysconf. | |
247 // | |
248 long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
249 if (bufsize == -1) | |
250 bufsize = 1024; | |
251 | |
6197 | 252 char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal); |
0 | 253 |
254 #ifdef _GNU_SOURCE | |
255 struct passwd* p = NULL; | |
256 int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p); | |
257 #else // _GNU_SOURCE | |
258 struct passwd* p = getpwuid_r(uid, &pwent, pwbuf, (int)bufsize); | |
259 #endif // _GNU_SOURCE | |
260 | |
261 if (p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { | |
262 if (PrintMiscellaneous && Verbose) { | |
263 if (p == NULL) { | |
264 warning("Could not retrieve passwd entry: %s\n", | |
265 strerror(errno)); | |
266 } | |
267 else { | |
268 warning("Could not determine user name: %s\n", | |
269 p->pw_name == NULL ? "pw_name = NULL" : | |
270 "pw_name zero length"); | |
271 } | |
272 } | |
6197 | 273 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); |
0 | 274 return NULL; |
275 } | |
276 | |
6197 | 277 char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal); |
0 | 278 strcpy(user_name, p->pw_name); |
279 | |
6197 | 280 FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal); |
0 | 281 return user_name; |
282 } | |
283 | |
284 // return the name of the user that owns the process identified by vmid. | |
285 // | |
286 // This method uses a slow directory search algorithm to find the backing | |
287 // store file for the specified vmid and returns the user name, as determined | |
288 // by the user name suffix of the hsperfdata_<username> directory name. | |
289 // | |
290 // the caller is expected to free the allocated memory. | |
291 // | |
292 static char* get_user_name_slow(int vmid, TRAPS) { | |
293 | |
294 // short circuit the directory search if the process doesn't even exist. | |
295 if (kill(vmid, 0) == OS_ERR) { | |
296 if (errno == ESRCH) { | |
297 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), | |
298 "Process not found"); | |
299 } | |
300 else /* EPERM */ { | |
301 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); | |
302 } | |
303 } | |
304 | |
305 // directory search | |
306 char* oldest_user = NULL; | |
307 time_t oldest_ctime = 0; | |
308 | |
309 const char* tmpdirname = os::get_temp_directory(); | |
310 | |
311 DIR* tmpdirp = os::opendir(tmpdirname); | |
312 | |
313 if (tmpdirp == NULL) { | |
314 return NULL; | |
315 } | |
316 | |
317 // for each entry in the directory that matches the pattern hsperfdata_*, | |
318 // open the directory and check if the file for the given vmid exists. | |
319 // The file with the expected name and the latest creation date is used | |
320 // to determine the user name for the process id. | |
321 // | |
322 struct dirent* dentry; | |
6197 | 323 char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal); |
0 | 324 errno = 0; |
325 while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) { | |
326 | |
327 // check if the directory entry is a hsperfdata file | |
328 if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) { | |
329 continue; | |
330 } | |
331 | |
332 char* usrdir_name = NEW_C_HEAP_ARRAY(char, | |
6197 | 333 strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal); |
0 | 334 strcpy(usrdir_name, tmpdirname); |
1353
a2ea687fdc7c
6938627: Make temporary directory use property java.io.tmpdir when specified
coleenp
parents:
605
diff
changeset
|
335 strcat(usrdir_name, "/"); |
0 | 336 strcat(usrdir_name, dentry->d_name); |
337 | |
338 DIR* subdirp = os::opendir(usrdir_name); | |
339 | |
340 if (subdirp == NULL) { | |
6197 | 341 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); |
0 | 342 continue; |
343 } | |
344 | |
345 // Since we don't create the backing store files in directories | |
346 // pointed to by symbolic links, we also don't follow them when | |
347 // looking for the files. We check for a symbolic link after the | |
348 // call to opendir in order to eliminate a small window where the | |
349 // symlink can be exploited. | |
350 // | |
351 if (!is_directory_secure(usrdir_name)) { | |
6197 | 352 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); |
0 | 353 os::closedir(subdirp); |
354 continue; | |
355 } | |
356 | |
357 struct dirent* udentry; | |
6197 | 358 char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal); |
0 | 359 errno = 0; |
360 while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) { | |
361 | |
362 if (filename_to_pid(udentry->d_name) == vmid) { | |
363 struct stat statbuf; | |
364 int result; | |
365 | |
366 char* filename = NEW_C_HEAP_ARRAY(char, | |
6197 | 367 strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal); |
0 | 368 |
369 strcpy(filename, usrdir_name); | |
370 strcat(filename, "/"); | |
371 strcat(filename, udentry->d_name); | |
372 | |
373 // don't follow symbolic links for the file | |
374 RESTARTABLE(::lstat(filename, &statbuf), result); | |
375 if (result == OS_ERR) { | |
6197 | 376 FREE_C_HEAP_ARRAY(char, filename, mtInternal); |
0 | 377 continue; |
378 } | |
379 | |
380 // skip over files that are not regular files. | |
381 if (!S_ISREG(statbuf.st_mode)) { | |
6197 | 382 FREE_C_HEAP_ARRAY(char, filename, mtInternal); |
0 | 383 continue; |
384 } | |
385 | |
386 // compare and save filename with latest creation time | |
387 if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) { | |
388 | |
389 if (statbuf.st_ctime > oldest_ctime) { | |
390 char* user = strchr(dentry->d_name, '_') + 1; | |
391 | |
6197 | 392 if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal); |
393 oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal); | |
0 | 394 |
395 strcpy(oldest_user, user); | |
396 oldest_ctime = statbuf.st_ctime; | |
397 } | |
398 } | |
399 | |
6197 | 400 FREE_C_HEAP_ARRAY(char, filename, mtInternal); |
0 | 401 } |
402 } | |
403 os::closedir(subdirp); | |
6197 | 404 FREE_C_HEAP_ARRAY(char, udbuf, mtInternal); |
405 FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal); | |
0 | 406 } |
407 os::closedir(tmpdirp); | |
6197 | 408 FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal); |
0 | 409 |
410 return(oldest_user); | |
411 } | |
412 | |
413 // return the name of the user that owns the JVM indicated by the given vmid. | |
414 // | |
415 static char* get_user_name(int vmid, TRAPS) { | |
416 | |
417 char psinfo_name[PATH_MAX]; | |
418 int result; | |
419 | |
420 snprintf(psinfo_name, PATH_MAX, "/proc/%d/psinfo", vmid); | |
421 | |
422 RESTARTABLE(::open(psinfo_name, O_RDONLY), result); | |
423 | |
424 if (result != OS_ERR) { | |
425 int fd = result; | |
426 | |
427 psinfo_t psinfo; | |
428 char* addr = (char*)&psinfo; | |
429 | |
430 for (size_t remaining = sizeof(psinfo_t); remaining > 0;) { | |
431 | |
432 RESTARTABLE(::read(fd, addr, remaining), result); | |
433 if (result == OS_ERR) { | |
434 THROW_MSG_0(vmSymbols::java_io_IOException(), "Read error"); | |
435 } | |
436 remaining-=result; | |
437 addr+=result; | |
438 } | |
439 | |
440 RESTARTABLE(::close(fd), result); | |
441 | |
442 // get the user name for the effective user id of the process | |
443 char* user_name = get_user_name(psinfo.pr_euid); | |
444 | |
445 return user_name; | |
446 } | |
447 | |
448 if (result == OS_ERR && errno == EACCES) { | |
449 | |
450 // In this case, the psinfo file for the process id existed, | |
451 // but we didn't have permission to access it. | |
452 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), | |
453 strerror(errno)); | |
454 } | |
455 | |
456 // at this point, we don't know if the process id itself doesn't | |
457 // exist or if the psinfo file doesn't exit. If the psinfo file | |
458 // doesn't exist, then we are running on Solaris 2.5.1 or earlier. | |
459 // since the structured procfs and old procfs interfaces can't be | |
460 // mixed, we attempt to find the file through a directory search. | |
461 | |
462 return get_user_name_slow(vmid, CHECK_NULL); | |
463 } | |
464 | |
465 // return the file name of the backing store file for the named | |
466 // shared memory region for the given user name and vmid. | |
467 // | |
468 // the caller is expected to free the allocated memory. | |
469 // | |
470 static char* get_sharedmem_filename(const char* dirname, int vmid) { | |
471 | |
472 // add 2 for the file separator and a NULL terminator. | |
473 size_t nbytes = strlen(dirname) + UINT_CHARS + 2; | |
474 | |
6197 | 475 char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); |
0 | 476 snprintf(name, nbytes, "%s/%d", dirname, vmid); |
477 | |
478 return name; | |
479 } | |
480 | |
481 | |
482 // remove file | |
483 // | |
484 // this method removes the file specified by the given path | |
485 // | |
486 static void remove_file(const char* path) { | |
487 | |
488 int result; | |
489 | |
490 // if the file is a directory, the following unlink will fail. since | |
491 // we don't expect to find directories in the user temp directory, we | |
492 // won't try to handle this situation. even if accidentially or | |
493 // maliciously planted, the directory's presence won't hurt anything. | |
494 // | |
495 RESTARTABLE(::unlink(path), result); | |
496 if (PrintMiscellaneous && Verbose && result == OS_ERR) { | |
497 if (errno != ENOENT) { | |
498 warning("Could not unlink shared memory backing" | |
499 " store file %s : %s\n", path, strerror(errno)); | |
500 } | |
501 } | |
502 } | |
503 | |
504 | |
505 // remove file | |
506 // | |
507 // this method removes the file with the given file name in the | |
508 // named directory. | |
509 // | |
510 static void remove_file(const char* dirname, const char* filename) { | |
511 | |
512 size_t nbytes = strlen(dirname) + strlen(filename) + 2; | |
6197 | 513 char* path = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal); |
0 | 514 |
515 strcpy(path, dirname); | |
516 strcat(path, "/"); | |
517 strcat(path, filename); | |
518 | |
519 remove_file(path); | |
520 | |
6197 | 521 FREE_C_HEAP_ARRAY(char, path, mtInternal); |
0 | 522 } |
523 | |
524 | |
525 // cleanup stale shared memory resources | |
526 // | |
527 // This method attempts to remove all stale shared memory files in | |
528 // the named user temporary directory. It scans the named directory | |
529 // for files matching the pattern ^$[0-9]*$. For each file found, the | |
530 // process id is extracted from the file name and a test is run to | |
531 // determine if the process is alive. If the process is not alive, | |
532 // any stale file resources are removed. | |
533 // | |
534 static void cleanup_sharedmem_resources(const char* dirname) { | |
535 | |
536 // open the user temp directory | |
537 DIR* dirp = os::opendir(dirname); | |
538 | |
539 if (dirp == NULL) { | |
540 // directory doesn't exist, so there is nothing to cleanup | |
541 return; | |
542 } | |
543 | |
544 if (!is_directory_secure(dirname)) { | |
545 // the directory is not a secure directory | |
546 return; | |
547 } | |
548 | |
549 // for each entry in the directory that matches the expected file | |
550 // name pattern, determine if the file resources are stale and if | |
551 // so, remove the file resources. Note, instrumented HotSpot processes | |
552 // for this user may start and/or terminate during this search and | |
553 // remove or create new files in this directory. The behavior of this | |
554 // loop under these conditions is dependent upon the implementation of | |
555 // opendir/readdir. | |
556 // | |
557 struct dirent* entry; | |
6197 | 558 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal); |
0 | 559 errno = 0; |
560 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) { | |
561 | |
562 pid_t pid = filename_to_pid(entry->d_name); | |
563 | |
564 if (pid == 0) { | |
565 | |
566 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { | |
567 | |
568 // attempt to remove all unexpected files, except "." and ".." | |
569 remove_file(dirname, entry->d_name); | |
570 } | |
571 | |
572 errno = 0; | |
573 continue; | |
574 } | |
575 | |
576 // we now have a file name that converts to a valid integer | |
577 // that could represent a process id . if this process id | |
578 // matches the current process id or the process is not running, | |
579 // then remove the stale file resources. | |
580 // | |
581 // process liveness is detected by sending signal number 0 to | |
582 // the process id (see kill(2)). if kill determines that the | |
583 // process does not exist, then the file resources are removed. | |
584 // if kill determines that that we don't have permission to | |
585 // signal the process, then the file resources are assumed to | |
586 // be stale and are removed because the resources for such a | |
587 // process should be in a different user specific directory. | |
588 // | |
589 if ((pid == os::current_process_id()) || | |
590 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) { | |
591 | |
592 remove_file(dirname, entry->d_name); | |
593 } | |
594 errno = 0; | |
595 } | |
596 os::closedir(dirp); | |
6197 | 597 FREE_C_HEAP_ARRAY(char, dbuf, mtInternal); |
0 | 598 } |
599 | |
600 // make the user specific temporary directory. Returns true if | |
601 // the directory exists and is secure upon return. Returns false | |
602 // if the directory exists but is either a symlink, is otherwise | |
603 // insecure, or if an error occurred. | |
604 // | |
605 static bool make_user_tmp_dir(const char* dirname) { | |
606 | |
607 // create the directory with 0755 permissions. note that the directory | |
608 // will be owned by euid::egid, which may not be the same as uid::gid. | |
609 // | |
610 if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) { | |
611 if (errno == EEXIST) { | |
612 // The directory already exists and was probably created by another | |
613 // JVM instance. However, this could also be the result of a | |
614 // deliberate symlink. Verify that the existing directory is safe. | |
615 // | |
616 if (!is_directory_secure(dirname)) { | |
617 // directory is not secure | |
618 if (PrintMiscellaneous && Verbose) { | |
619 warning("%s directory is insecure\n", dirname); | |
620 } | |
621 return false; | |
622 } | |
623 } | |
624 else { | |
625 // we encountered some other failure while attempting | |
626 // to create the directory | |
627 // | |
628 if (PrintMiscellaneous && Verbose) { | |
629 warning("could not create directory %s: %s\n", | |
630 dirname, strerror(errno)); | |
631 } | |
632 return false; | |
633 } | |
634 } | |
635 return true; | |
636 } | |
637 | |
638 // create the shared memory file resources | |
639 // | |
640 // This method creates the shared memory file with the given size | |
641 // This method also creates the user specific temporary directory, if | |
642 // it does not yet exist. | |
643 // | |
644 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) { | |
645 | |
646 // make the user temporary directory | |
647 if (!make_user_tmp_dir(dirname)) { | |
648 // could not make/find the directory or the found directory | |
649 // was not secure | |
650 return -1; | |
651 } | |
652 | |
653 int result; | |
654 | |
655 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE), result); | |
656 if (result == OS_ERR) { | |
657 if (PrintMiscellaneous && Verbose) { | |
658 warning("could not create file %s: %s\n", filename, strerror(errno)); | |
659 } | |
660 return -1; | |
661 } | |
662 | |
663 // save the file descriptor | |
664 int fd = result; | |
665 | |
666 // set the file size | |
667 RESTARTABLE(::ftruncate(fd, (off_t)size), result); | |
668 if (result == OS_ERR) { | |
669 if (PrintMiscellaneous && Verbose) { | |
670 warning("could not set shared memory file size: %s\n", strerror(errno)); | |
671 } | |
672 RESTARTABLE(::close(fd), result); | |
673 return -1; | |
674 } | |
675 | |
676 return fd; | |
677 } | |
678 | |
679 // open the shared memory file for the given user and vmid. returns | |
680 // the file descriptor for the open file or -1 if the file could not | |
681 // be opened. | |
682 // | |
683 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) { | |
684 | |
685 // open the file | |
686 int result; | |
687 RESTARTABLE(::open(filename, oflags), result); | |
688 if (result == OS_ERR) { | |
689 if (errno == ENOENT) { | |
690 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), | |
691 "Process not found"); | |
692 } | |
693 else if (errno == EACCES) { | |
694 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), | |
695 "Permission denied"); | |
696 } | |
697 else { | |
698 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); | |
699 } | |
700 } | |
701 | |
702 return result; | |
703 } | |
704 | |
705 // create a named shared memory region. returns the address of the | |
706 // memory region on success or NULL on failure. A return value of | |
707 // NULL will ultimately disable the shared memory feature. | |
708 // | |
709 // On Solaris and Linux, the name space for shared memory objects | |
710 // is the file system name space. | |
711 // | |
712 // A monitoring application attaching to a JVM does not need to know | |
713 // the file system name of the shared memory object. However, it may | |
714 // be convenient for applications to discover the existence of newly | |
715 // created and terminating JVMs by watching the file system name space | |
716 // for files being created or removed. | |
717 // | |
718 static char* mmap_create_shared(size_t size) { | |
719 | |
720 int result; | |
721 int fd; | |
722 char* mapAddress; | |
723 | |
724 int vmid = os::current_process_id(); | |
725 | |
726 char* user_name = get_user_name(geteuid()); | |
727 | |
728 if (user_name == NULL) | |
729 return NULL; | |
730 | |
731 char* dirname = get_user_tmp_dir(user_name); | |
732 char* filename = get_sharedmem_filename(dirname, vmid); | |
733 | |
734 // cleanup any stale shared memory files | |
735 cleanup_sharedmem_resources(dirname); | |
736 | |
737 assert(((size > 0) && (size % os::vm_page_size() == 0)), | |
738 "unexpected PerfMemory region size"); | |
739 | |
740 fd = create_sharedmem_resources(dirname, filename, size); | |
741 | |
6197 | 742 FREE_C_HEAP_ARRAY(char, user_name, mtInternal); |
743 FREE_C_HEAP_ARRAY(char, dirname, mtInternal); | |
0 | 744 |
745 if (fd == -1) { | |
6197 | 746 FREE_C_HEAP_ARRAY(char, filename, mtInternal); |
0 | 747 return NULL; |
748 } | |
749 | |
750 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); | |
751 | |
752 // attempt to close the file - restart it if it was interrupted, | |
753 // but ignore other failures | |
754 RESTARTABLE(::close(fd), result); | |
755 assert(result != OS_ERR, "could not close file"); | |
756 | |
757 if (mapAddress == MAP_FAILED) { | |
758 if (PrintMiscellaneous && Verbose) { | |
759 warning("mmap failed - %s\n", strerror(errno)); | |
760 } | |
761 remove_file(filename); | |
6197 | 762 FREE_C_HEAP_ARRAY(char, filename, mtInternal); |
0 | 763 return NULL; |
764 } | |
765 | |
766 // save the file name for use in delete_shared_memory() | |
767 backing_store_file_name = filename; | |
768 | |
769 // clear the shared memory region | |
770 (void)::memset((void*) mapAddress, 0, size); | |
771 | |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
772 // it does not go through os api, the operation has to record from here |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
773 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC); |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
774 MemTracker::record_virtual_memory_type((address)mapAddress, mtInternal); |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
775 |
0 | 776 return mapAddress; |
777 } | |
778 | |
779 // release a named shared memory region | |
780 // | |
781 static void unmap_shared(char* addr, size_t bytes) { | |
782 os::release_memory(addr, bytes); | |
783 } | |
784 | |
785 // create the PerfData memory region in shared memory. | |
786 // | |
787 static char* create_shared_memory(size_t size) { | |
788 | |
789 // create the shared memory region. | |
790 return mmap_create_shared(size); | |
791 } | |
792 | |
793 // delete the shared PerfData memory region | |
794 // | |
795 static void delete_shared_memory(char* addr, size_t size) { | |
796 | |
797 // cleanup the persistent shared memory resources. since DestroyJavaVM does | |
798 // not support unloading of the JVM, unmapping of the memory resource is | |
799 // not performed. The memory will be reclaimed by the OS upon termination of | |
800 // the process. The backing store file is deleted from the file system. | |
801 | |
802 assert(!PerfDisableSharedMem, "shouldn't be here"); | |
803 | |
804 if (backing_store_file_name != NULL) { | |
805 remove_file(backing_store_file_name); | |
806 // Don't.. Free heap memory could deadlock os::abort() if it is called | |
807 // from signal handler. OS will reclaim the heap memory. | |
808 // FREE_C_HEAP_ARRAY(char, backing_store_file_name); | |
809 backing_store_file_name = NULL; | |
810 } | |
811 } | |
812 | |
813 // return the size of the file for the given file descriptor | |
814 // or 0 if it is not a valid size for a shared memory file | |
815 // | |
816 static size_t sharedmem_filesize(int fd, TRAPS) { | |
817 | |
818 struct stat statbuf; | |
819 int result; | |
820 | |
821 RESTARTABLE(::fstat(fd, &statbuf), result); | |
822 if (result == OS_ERR) { | |
823 if (PrintMiscellaneous && Verbose) { | |
824 warning("fstat failed: %s\n", strerror(errno)); | |
825 } | |
826 THROW_MSG_0(vmSymbols::java_io_IOException(), | |
827 "Could not determine PerfMemory size"); | |
828 } | |
829 | |
830 if ((statbuf.st_size == 0) || | |
831 ((size_t)statbuf.st_size % os::vm_page_size() != 0)) { | |
832 THROW_MSG_0(vmSymbols::java_lang_Exception(), | |
833 "Invalid PerfMemory size"); | |
834 } | |
835 | |
836 return (size_t)statbuf.st_size; | |
837 } | |
838 | |
839 // attach to a named shared memory region. | |
840 // | |
841 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) { | |
842 | |
843 char* mapAddress; | |
844 int result; | |
845 int fd; | |
846 size_t size; | |
847 const char* luser = NULL; | |
848 | |
849 int mmap_prot; | |
850 int file_flags; | |
851 | |
852 ResourceMark rm; | |
853 | |
854 // map the high level access mode to the appropriate permission | |
855 // constructs for the file and the shared memory mapping. | |
856 if (mode == PerfMemory::PERF_MODE_RO) { | |
857 mmap_prot = PROT_READ; | |
858 file_flags = O_RDONLY; | |
859 } | |
860 else if (mode == PerfMemory::PERF_MODE_RW) { | |
861 #ifdef LATER | |
862 mmap_prot = PROT_READ | PROT_WRITE; | |
863 file_flags = O_RDWR; | |
864 #else | |
865 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | |
866 "Unsupported access mode"); | |
867 #endif | |
868 } | |
869 else { | |
870 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | |
871 "Illegal access mode"); | |
872 } | |
873 | |
874 if (user == NULL || strlen(user) == 0) { | |
875 luser = get_user_name(vmid, CHECK); | |
876 } | |
877 else { | |
878 luser = user; | |
879 } | |
880 | |
881 if (luser == NULL) { | |
882 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | |
883 "Could not map vmid to user Name"); | |
884 } | |
885 | |
886 char* dirname = get_user_tmp_dir(luser); | |
887 | |
888 // since we don't follow symbolic links when creating the backing | |
889 // store file, we don't follow them when attaching either. | |
890 // | |
891 if (!is_directory_secure(dirname)) { | |
6197 | 892 FREE_C_HEAP_ARRAY(char, dirname, mtInternal); |
0 | 893 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
894 "Process not found"); | |
895 } | |
896 | |
897 char* filename = get_sharedmem_filename(dirname, vmid); | |
898 | |
899 // copy heap memory to resource memory. the open_sharedmem_file | |
900 // method below need to use the filename, but could throw an | |
901 // exception. using a resource array prevents the leak that | |
902 // would otherwise occur. | |
903 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); | |
904 strcpy(rfilename, filename); | |
905 | |
906 // free the c heap resources that are no longer needed | |
6197 | 907 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal); |
908 FREE_C_HEAP_ARRAY(char, dirname, mtInternal); | |
909 FREE_C_HEAP_ARRAY(char, filename, mtInternal); | |
0 | 910 |
911 // open the shared memory file for the give vmid | |
912 fd = open_sharedmem_file(rfilename, file_flags, CHECK); | |
913 assert(fd != OS_ERR, "unexpected value"); | |
914 | |
915 if (*sizep == 0) { | |
916 size = sharedmem_filesize(fd, CHECK); | |
917 assert(size != 0, "unexpected size"); | |
918 } | |
919 | |
920 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0); | |
921 | |
922 // attempt to close the file - restart if it gets interrupted, | |
923 // but ignore other failures | |
924 RESTARTABLE(::close(fd), result); | |
925 assert(result != OS_ERR, "could not close file"); | |
926 | |
927 if (mapAddress == MAP_FAILED) { | |
928 if (PrintMiscellaneous && Verbose) { | |
929 warning("mmap failed: %s\n", strerror(errno)); | |
930 } | |
931 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), | |
932 "Could not map PerfMemory"); | |
933 } | |
934 | |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
935 // it does not go through os api, the operation has to record from here |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
936 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC); |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
937 MemTracker::record_virtual_memory_type((address)mapAddress, mtInternal); |
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
938 |
0 | 939 *addr = mapAddress; |
940 *sizep = size; | |
941 | |
942 if (PerfTraceMemOps) { | |
943 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " | |
944 INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress); | |
945 } | |
946 } | |
947 | |
948 | |
949 | |
950 | |
951 // create the PerfData memory region | |
952 // | |
953 // This method creates the memory region used to store performance | |
954 // data for the JVM. The memory may be created in standard or | |
955 // shared memory. | |
956 // | |
957 void PerfMemory::create_memory_region(size_t size) { | |
958 | |
959 if (PerfDisableSharedMem) { | |
960 // do not share the memory for the performance data. | |
961 _start = create_standard_memory(size); | |
962 } | |
963 else { | |
964 _start = create_shared_memory(size); | |
965 if (_start == NULL) { | |
966 | |
967 // creation of the shared memory region failed, attempt | |
968 // to create a contiguous, non-shared memory region instead. | |
969 // | |
970 if (PrintMiscellaneous && Verbose) { | |
971 warning("Reverting to non-shared PerfMemory region.\n"); | |
972 } | |
973 PerfDisableSharedMem = true; | |
974 _start = create_standard_memory(size); | |
975 } | |
976 } | |
977 | |
978 if (_start != NULL) _capacity = size; | |
979 | |
980 } | |
981 | |
982 // delete the PerfData memory region | |
983 // | |
984 // This method deletes the memory region used to store performance | |
985 // data for the JVM. The memory region indicated by the <address, size> | |
986 // tuple will be inaccessible after a call to this method. | |
987 // | |
988 void PerfMemory::delete_memory_region() { | |
989 | |
990 assert((start() != NULL && capacity() > 0), "verify proper state"); | |
991 | |
992 // If user specifies PerfDataSaveFile, it will save the performance data | |
993 // to the specified file name no matter whether PerfDataSaveToFile is specified | |
994 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag | |
995 // -XX:+PerfDataSaveToFile. | |
996 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) { | |
997 save_memory_to_file(start(), capacity()); | |
998 } | |
999 | |
1000 if (PerfDisableSharedMem) { | |
1001 delete_standard_memory(start(), capacity()); | |
1002 } | |
1003 else { | |
1004 delete_shared_memory(start(), capacity()); | |
1005 } | |
1006 } | |
1007 | |
1008 // attach to the PerfData memory region for another JVM | |
1009 // | |
1010 // This method returns an <address, size> tuple that points to | |
1011 // a memory buffer that is kept reasonably synchronized with | |
1012 // the PerfData memory region for the indicated JVM. This | |
1013 // buffer may be kept in synchronization via shared memory | |
1014 // or some other mechanism that keeps the buffer updated. | |
1015 // | |
1016 // If the JVM chooses not to support the attachability feature, | |
1017 // this method should throw an UnsupportedOperation exception. | |
1018 // | |
1019 // This implementation utilizes named shared memory to map | |
1020 // the indicated process's PerfData memory region into this JVMs | |
1021 // address space. | |
1022 // | |
1023 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) { | |
1024 | |
1025 if (vmid == 0 || vmid == os::current_process_id()) { | |
1026 *addrp = start(); | |
1027 *sizep = capacity(); | |
1028 return; | |
1029 } | |
1030 | |
1031 mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK); | |
1032 } | |
1033 | |
1034 // detach from the PerfData memory region of another JVM | |
1035 // | |
1036 // This method detaches the PerfData memory region of another | |
1037 // JVM, specified as an <address, size> tuple of a buffer | |
1038 // in this process's address space. This method may perform | |
1039 // arbitrary actions to accomplish the detachment. The memory | |
1040 // region specified by <address, size> will be inaccessible after | |
1041 // a call to this method. | |
1042 // | |
1043 // If the JVM chooses not to support the attachability feature, | |
1044 // this method should throw an UnsupportedOperation exception. | |
1045 // | |
1046 // This implementation utilizes named shared memory to detach | |
1047 // the indicated process's PerfData memory region from this | |
1048 // process's address space. | |
1049 // | |
1050 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) { | |
1051 | |
1052 assert(addr != 0, "address sanity check"); | |
1053 assert(bytes > 0, "capacity sanity check"); | |
1054 | |
1055 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) { | |
1056 // prevent accidental detachment of this process's PerfMemory region | |
1057 return; | |
1058 } | |
1059 | |
1060 unmap_shared(addr, bytes); | |
1061 } | |
1062 | |
1063 char* PerfMemory::backing_store_filename() { | |
1064 return backing_store_file_name; | |
1065 } |