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