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