Mercurial > hg > truffle
annotate src/os/solaris/vm/perfMemory_solaris.cpp @ 15275:0c9406f9338f
Update c1visualizer to version compatible with Java 8
author | Christian Wimmer <christian.wimmer@oracle.com> |
---|---|
date | Tue, 22 Apr 2014 16:41:46 -0700 |
parents | 4ca6dc0799b6 |
children | 52b4284cb496 |
rev | line source |
---|---|
0 | 1 /* |
14909 | 2 * Copyright (c) 2001, 2013, 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 | |
10969
a837fa3d3f86
8013057: assert(_needs_gc || SafepointSynchronize::is_at_safepoint()) failed: only read at safepoint
dcubed
parents:
9064
diff
changeset
|
65 if (!os::commit_memory(mapAddress, size, !ExecMem)) { |
0 | 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 | |
10978
e95fc50106cf
7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents:
10969
diff
changeset
|
125 result = ::close(fd); |
0 | 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 } | |
14909 | 436 remaining-=result; |
437 addr+=result; | |
0 | 438 } |
439 | |
10978
e95fc50106cf
7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents:
10969
diff
changeset
|
440 ::close(fd); |
0 | 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 } | |
10978
e95fc50106cf
7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents:
10969
diff
changeset
|
672 ::close(fd); |
0 | 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) { | |
9064
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
690 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
691 "Process not found", OS_ERR); |
0 | 692 } |
693 else if (errno == EACCES) { | |
9064
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
694 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
695 "Permission denied", OS_ERR); |
0 | 696 } |
697 else { | |
9064
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
698 THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR); |
0 | 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 | |
10978
e95fc50106cf
7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents:
10969
diff
changeset
|
752 result = ::close(fd); |
0 | 753 assert(result != OS_ERR, "could not close file"); |
754 | |
755 if (mapAddress == MAP_FAILED) { | |
756 if (PrintMiscellaneous && Verbose) { | |
757 warning("mmap failed - %s\n", strerror(errno)); | |
758 } | |
759 remove_file(filename); | |
6197 | 760 FREE_C_HEAP_ARRAY(char, filename, mtInternal); |
0 | 761 return NULL; |
762 } | |
763 | |
764 // save the file name for use in delete_shared_memory() | |
765 backing_store_file_name = filename; | |
766 | |
767 // clear the shared memory region | |
768 (void)::memset((void*) mapAddress, 0, size); | |
769 | |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
770 // it does not go through os api, the operation has to record from here |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10978
diff
changeset
|
771 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, mtInternal, CURRENT_PC); |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
772 |
0 | 773 return mapAddress; |
774 } | |
775 | |
776 // release a named shared memory region | |
777 // | |
778 static void unmap_shared(char* addr, size_t bytes) { | |
779 os::release_memory(addr, bytes); | |
780 } | |
781 | |
782 // create the PerfData memory region in shared memory. | |
783 // | |
784 static char* create_shared_memory(size_t size) { | |
785 | |
786 // create the shared memory region. | |
787 return mmap_create_shared(size); | |
788 } | |
789 | |
790 // delete the shared PerfData memory region | |
791 // | |
792 static void delete_shared_memory(char* addr, size_t size) { | |
793 | |
794 // cleanup the persistent shared memory resources. since DestroyJavaVM does | |
795 // not support unloading of the JVM, unmapping of the memory resource is | |
796 // not performed. The memory will be reclaimed by the OS upon termination of | |
797 // the process. The backing store file is deleted from the file system. | |
798 | |
799 assert(!PerfDisableSharedMem, "shouldn't be here"); | |
800 | |
801 if (backing_store_file_name != NULL) { | |
802 remove_file(backing_store_file_name); | |
803 // Don't.. Free heap memory could deadlock os::abort() if it is called | |
804 // from signal handler. OS will reclaim the heap memory. | |
805 // FREE_C_HEAP_ARRAY(char, backing_store_file_name); | |
806 backing_store_file_name = NULL; | |
807 } | |
808 } | |
809 | |
810 // return the size of the file for the given file descriptor | |
811 // or 0 if it is not a valid size for a shared memory file | |
812 // | |
813 static size_t sharedmem_filesize(int fd, TRAPS) { | |
814 | |
815 struct stat statbuf; | |
816 int result; | |
817 | |
818 RESTARTABLE(::fstat(fd, &statbuf), result); | |
819 if (result == OS_ERR) { | |
820 if (PrintMiscellaneous && Verbose) { | |
821 warning("fstat failed: %s\n", strerror(errno)); | |
822 } | |
823 THROW_MSG_0(vmSymbols::java_io_IOException(), | |
824 "Could not determine PerfMemory size"); | |
825 } | |
826 | |
827 if ((statbuf.st_size == 0) || | |
828 ((size_t)statbuf.st_size % os::vm_page_size() != 0)) { | |
829 THROW_MSG_0(vmSymbols::java_lang_Exception(), | |
830 "Invalid PerfMemory size"); | |
831 } | |
832 | |
833 return (size_t)statbuf.st_size; | |
834 } | |
835 | |
836 // attach to a named shared memory region. | |
837 // | |
838 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) { | |
839 | |
840 char* mapAddress; | |
841 int result; | |
842 int fd; | |
9064
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
843 size_t size = 0; |
0 | 844 const char* luser = NULL; |
845 | |
846 int mmap_prot; | |
847 int file_flags; | |
848 | |
849 ResourceMark rm; | |
850 | |
851 // map the high level access mode to the appropriate permission | |
852 // constructs for the file and the shared memory mapping. | |
853 if (mode == PerfMemory::PERF_MODE_RO) { | |
854 mmap_prot = PROT_READ; | |
855 file_flags = O_RDONLY; | |
856 } | |
857 else if (mode == PerfMemory::PERF_MODE_RW) { | |
858 #ifdef LATER | |
859 mmap_prot = PROT_READ | PROT_WRITE; | |
860 file_flags = O_RDWR; | |
861 #else | |
862 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | |
863 "Unsupported access mode"); | |
864 #endif | |
865 } | |
866 else { | |
867 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | |
868 "Illegal access mode"); | |
869 } | |
870 | |
871 if (user == NULL || strlen(user) == 0) { | |
872 luser = get_user_name(vmid, CHECK); | |
873 } | |
874 else { | |
875 luser = user; | |
876 } | |
877 | |
878 if (luser == NULL) { | |
879 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), | |
880 "Could not map vmid to user Name"); | |
881 } | |
882 | |
883 char* dirname = get_user_tmp_dir(luser); | |
884 | |
885 // since we don't follow symbolic links when creating the backing | |
886 // store file, we don't follow them when attaching either. | |
887 // | |
888 if (!is_directory_secure(dirname)) { | |
6197 | 889 FREE_C_HEAP_ARRAY(char, dirname, mtInternal); |
0 | 890 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
891 "Process not found"); | |
892 } | |
893 | |
894 char* filename = get_sharedmem_filename(dirname, vmid); | |
895 | |
896 // copy heap memory to resource memory. the open_sharedmem_file | |
897 // method below need to use the filename, but could throw an | |
898 // exception. using a resource array prevents the leak that | |
899 // would otherwise occur. | |
900 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1); | |
901 strcpy(rfilename, filename); | |
902 | |
903 // free the c heap resources that are no longer needed | |
6197 | 904 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal); |
905 FREE_C_HEAP_ARRAY(char, dirname, mtInternal); | |
906 FREE_C_HEAP_ARRAY(char, filename, mtInternal); | |
0 | 907 |
908 // open the shared memory file for the give vmid | |
14909 | 909 fd = open_sharedmem_file(rfilename, file_flags, CHECK); |
910 assert(fd != OS_ERR, "unexpected value"); | |
0 | 911 |
912 if (*sizep == 0) { | |
913 size = sharedmem_filesize(fd, CHECK); | |
9064
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
914 } else { |
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
915 size = *sizep; |
0 | 916 } |
917 | |
9064
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
918 assert(size > 0, "unexpected size <= 0"); |
4b7cf00ccb08
8006001: [parfait] Possible file leak in hotspot/src/os/linux/vm/perfMemory_linux.cpp
ccheung
parents:
6882
diff
changeset
|
919 |
0 | 920 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0); |
921 | |
10978
e95fc50106cf
7178026: os::close can restart ::close but that is not a restartable syscall
rdurbin
parents:
10969
diff
changeset
|
922 result = ::close(fd); |
0 | 923 assert(result != OS_ERR, "could not close file"); |
924 | |
925 if (mapAddress == MAP_FAILED) { | |
926 if (PrintMiscellaneous && Verbose) { | |
927 warning("mmap failed: %s\n", strerror(errno)); | |
928 } | |
929 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), | |
930 "Could not map PerfMemory"); | |
931 } | |
932 | |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
933 // it does not go through os api, the operation has to record from here |
10986
1f4355cee9a2
8013651: NMT: reserve/release sequence id's in incorrect order due to race
zgu
parents:
10978
diff
changeset
|
934 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, mtInternal, CURRENT_PC); |
6882
716c64bda5ba
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
6842
diff
changeset
|
935 |
0 | 936 *addr = mapAddress; |
937 *sizep = size; | |
938 | |
939 if (PerfTraceMemOps) { | |
940 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at " | |
941 INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress); | |
942 } | |
943 } | |
944 | |
945 | |
946 | |
947 | |
948 // create the PerfData memory region | |
949 // | |
950 // This method creates the memory region used to store performance | |
951 // data for the JVM. The memory may be created in standard or | |
952 // shared memory. | |
953 // | |
954 void PerfMemory::create_memory_region(size_t size) { | |
955 | |
956 if (PerfDisableSharedMem) { | |
957 // do not share the memory for the performance data. | |
958 _start = create_standard_memory(size); | |
959 } | |
960 else { | |
961 _start = create_shared_memory(size); | |
962 if (_start == NULL) { | |
963 | |
964 // creation of the shared memory region failed, attempt | |
965 // to create a contiguous, non-shared memory region instead. | |
966 // | |
967 if (PrintMiscellaneous && Verbose) { | |
968 warning("Reverting to non-shared PerfMemory region.\n"); | |
969 } | |
970 PerfDisableSharedMem = true; | |
971 _start = create_standard_memory(size); | |
972 } | |
973 } | |
974 | |
975 if (_start != NULL) _capacity = size; | |
976 | |
977 } | |
978 | |
979 // delete the PerfData memory region | |
980 // | |
981 // This method deletes the memory region used to store performance | |
982 // data for the JVM. The memory region indicated by the <address, size> | |
983 // tuple will be inaccessible after a call to this method. | |
984 // | |
985 void PerfMemory::delete_memory_region() { | |
986 | |
987 assert((start() != NULL && capacity() > 0), "verify proper state"); | |
988 | |
989 // If user specifies PerfDataSaveFile, it will save the performance data | |
990 // to the specified file name no matter whether PerfDataSaveToFile is specified | |
991 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag | |
992 // -XX:+PerfDataSaveToFile. | |
993 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) { | |
994 save_memory_to_file(start(), capacity()); | |
995 } | |
996 | |
997 if (PerfDisableSharedMem) { | |
998 delete_standard_memory(start(), capacity()); | |
999 } | |
1000 else { | |
1001 delete_shared_memory(start(), capacity()); | |
1002 } | |
1003 } | |
1004 | |
1005 // attach to the PerfData memory region for another JVM | |
1006 // | |
1007 // This method returns an <address, size> tuple that points to | |
1008 // a memory buffer that is kept reasonably synchronized with | |
1009 // the PerfData memory region for the indicated JVM. This | |
1010 // buffer may be kept in synchronization via shared memory | |
1011 // or some other mechanism that keeps the buffer updated. | |
1012 // | |
1013 // If the JVM chooses not to support the attachability feature, | |
1014 // this method should throw an UnsupportedOperation exception. | |
1015 // | |
1016 // This implementation utilizes named shared memory to map | |
1017 // the indicated process's PerfData memory region into this JVMs | |
1018 // address space. | |
1019 // | |
1020 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) { | |
1021 | |
1022 if (vmid == 0 || vmid == os::current_process_id()) { | |
1023 *addrp = start(); | |
1024 *sizep = capacity(); | |
1025 return; | |
1026 } | |
1027 | |
1028 mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK); | |
1029 } | |
1030 | |
1031 // detach from the PerfData memory region of another JVM | |
1032 // | |
1033 // This method detaches the PerfData memory region of another | |
1034 // JVM, specified as an <address, size> tuple of a buffer | |
1035 // in this process's address space. This method may perform | |
1036 // arbitrary actions to accomplish the detachment. The memory | |
1037 // region specified by <address, size> will be inaccessible after | |
1038 // a call to this method. | |
1039 // | |
1040 // If the JVM chooses not to support the attachability feature, | |
1041 // this method should throw an UnsupportedOperation exception. | |
1042 // | |
1043 // This implementation utilizes named shared memory to detach | |
1044 // the indicated process's PerfData memory region from this | |
1045 // process's address space. | |
1046 // | |
1047 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) { | |
1048 | |
1049 assert(addr != 0, "address sanity check"); | |
1050 assert(bytes > 0, "capacity sanity check"); | |
1051 | |
1052 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) { | |
1053 // prevent accidental detachment of this process's PerfMemory region | |
1054 return; | |
1055 } | |
1056 | |
1057 unmap_shared(addr, bytes); | |
1058 } | |
1059 | |
1060 char* PerfMemory::backing_store_filename() { | |
1061 return backing_store_file_name; | |
1062 } |