src/os/bsd/vm/perfMemory_bsd.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial