annotate src/os/bsd/vm/perfMemory_bsd.cpp @ 20779:d7b6bdd51abe

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