hotspot/src/os/linux/vm/perfMemory_linux.cpp
changeset 37113 5a33bf5089ac
parent 32387 a376fff9d4a5
child 40103 3c3569845229
equal deleted inserted replaced
37111:98572401ab0a 37113:5a33bf5089ac
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    27 #include "memory/allocation.inline.hpp"
    27 #include "memory/allocation.inline.hpp"
    28 #include "memory/resourceArea.hpp"
    28 #include "memory/resourceArea.hpp"
    29 #include "oops/oop.inline.hpp"
    29 #include "oops/oop.inline.hpp"
    30 #include "os_linux.inline.hpp"
    30 #include "os_linux.inline.hpp"
    31 #include "runtime/handles.inline.hpp"
    31 #include "runtime/handles.inline.hpp"
       
    32 #include "runtime/os.hpp"
    32 #include "runtime/perfMemory.hpp"
    33 #include "runtime/perfMemory.hpp"
    33 #include "services/memTracker.hpp"
    34 #include "services/memTracker.hpp"
    34 #include "utilities/exceptions.hpp"
    35 #include "utilities/exceptions.hpp"
    35 
    36 
    36 // put OS-includes here
    37 // put OS-includes here
    98   RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
    99   RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
    99               result);;
   100               result);;
   100   if (result == OS_ERR) {
   101   if (result == OS_ERR) {
   101     if (PrintMiscellaneous && Verbose) {
   102     if (PrintMiscellaneous && Verbose) {
   102       warning("Could not create Perfdata save file: %s: %s\n",
   103       warning("Could not create Perfdata save file: %s: %s\n",
   103               destfile, strerror(errno));
   104               destfile, os::strerror(errno));
   104     }
   105     }
   105   } else {
   106   } else {
   106     int fd = result;
   107     int fd = result;
   107 
   108 
   108     for (size_t remaining = size; remaining > 0;) {
   109     for (size_t remaining = size; remaining > 0;) {
   109 
   110 
   110       RESTARTABLE(::write(fd, addr, remaining), result);
   111       RESTARTABLE(::write(fd, addr, remaining), result);
   111       if (result == OS_ERR) {
   112       if (result == OS_ERR) {
   112         if (PrintMiscellaneous && Verbose) {
   113         if (PrintMiscellaneous && Verbose) {
   113           warning("Could not write Perfdata save file: %s: %s\n",
   114           warning("Could not write Perfdata save file: %s: %s\n",
   114                   destfile, strerror(errno));
   115                   destfile, os::strerror(errno));
   115         }
   116         }
   116         break;
   117         break;
   117       }
   118       }
   118 
   119 
   119       remaining -= (size_t)result;
   120       remaining -= (size_t)result;
   121     }
   122     }
   122 
   123 
   123     result = ::close(fd);
   124     result = ::close(fd);
   124     if (PrintMiscellaneous && Verbose) {
   125     if (PrintMiscellaneous && Verbose) {
   125       if (result == OS_ERR) {
   126       if (result == OS_ERR) {
   126         warning("Could not close %s: %s\n", destfile, strerror(errno));
   127         warning("Could not close %s: %s\n", destfile, os::strerror(errno));
   127       }
   128       }
   128     }
   129     }
   129   }
   130   }
   130   FREE_C_HEAP_ARRAY(char, destfile);
   131   FREE_C_HEAP_ARRAY(char, destfile);
   131 }
   132 }
   306   if (result == OS_ERR) {
   307   if (result == OS_ERR) {
   307     if (PrintMiscellaneous && Verbose) {
   308     if (PrintMiscellaneous && Verbose) {
   308       if (errno == ELOOP) {
   309       if (errno == ELOOP) {
   309         warning("directory %s is a symlink and is not secure\n", dirname);
   310         warning("directory %s is a symlink and is not secure\n", dirname);
   310       } else {
   311       } else {
   311         warning("could not open directory %s: %s\n", dirname, strerror(errno));
   312         warning("could not open directory %s: %s\n", dirname, os::strerror(errno));
   312       }
   313       }
   313     }
   314     }
   314     return dirp;
   315     return dirp;
   315   }
   316   }
   316   int fd = result;
   317   int fd = result;
   417 
   418 
   418   // Determine if the file is secure.
   419   // Determine if the file is secure.
   419   RESTARTABLE(::fstat(fd, &statbuf), result);
   420   RESTARTABLE(::fstat(fd, &statbuf), result);
   420   if (result == OS_ERR) {
   421   if (result == OS_ERR) {
   421     if (PrintMiscellaneous && Verbose) {
   422     if (PrintMiscellaneous && Verbose) {
   422       warning("fstat failed on %s: %s\n", filename, strerror(errno));
   423       warning("fstat failed on %s: %s\n", filename, os::strerror(errno));
   423     }
   424     }
   424     return false;
   425     return false;
   425   }
   426   }
   426   if (statbuf.st_nlink > 1) {
   427   if (statbuf.st_nlink > 1) {
   427     // A file with multiple links is not expected.
   428     // A file with multiple links is not expected.
   457 
   458 
   458   if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
   459   if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
   459     if (PrintMiscellaneous && Verbose) {
   460     if (PrintMiscellaneous && Verbose) {
   460       if (result != 0) {
   461       if (result != 0) {
   461         warning("Could not retrieve passwd entry: %s\n",
   462         warning("Could not retrieve passwd entry: %s\n",
   462                 strerror(result));
   463                 os::strerror(result));
   463       }
   464       }
   464       else if (p == NULL) {
   465       else if (p == NULL) {
   465         // this check is added to protect against an observed problem
   466         // this check is added to protect against an observed problem
   466         // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
   467         // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
   467         // indicating success, but has p == NULL. This was observed when
   468         // indicating success, but has p == NULL. This was observed when
   471         // is safe under any condition, but the use of errno in the output
   472         // is safe under any condition, but the use of errno in the output
   472         // message may result in an erroneous message.
   473         // message may result in an erroneous message.
   473         // Bug Id 89052 was opened with RedHat.
   474         // Bug Id 89052 was opened with RedHat.
   474         //
   475         //
   475         warning("Could not retrieve passwd entry: %s\n",
   476         warning("Could not retrieve passwd entry: %s\n",
   476                 strerror(errno));
   477                 os::strerror(errno));
   477       }
   478       }
   478       else {
   479       else {
   479         warning("Could not determine user name: %s\n",
   480         warning("Could not determine user name: %s\n",
   480                 p->pw_name == NULL ? "pw_name = NULL" :
   481                 p->pw_name == NULL ? "pw_name = NULL" :
   481                                      "pw_name zero length");
   482                                      "pw_name zero length");
   507     if (errno == ESRCH) {
   508     if (errno == ESRCH) {
   508       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
   509       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
   509                   "Process not found");
   510                   "Process not found");
   510     }
   511     }
   511     else /* EPERM */ {
   512     else /* EPERM */ {
   512       THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
   513       THROW_MSG_0(vmSymbols::java_io_IOException(), os::strerror(errno));
   513     }
   514     }
   514   }
   515   }
   515 
   516 
   516   // directory search
   517   // directory search
   517   char* oldest_user = NULL;
   518   char* oldest_user = NULL;
   662   //
   663   //
   663   RESTARTABLE(::unlink(path), result);
   664   RESTARTABLE(::unlink(path), result);
   664   if (PrintMiscellaneous && Verbose && result == OS_ERR) {
   665   if (PrintMiscellaneous && Verbose && result == OS_ERR) {
   665     if (errno != ENOENT) {
   666     if (errno != ENOENT) {
   666       warning("Could not unlink shared memory backing"
   667       warning("Could not unlink shared memory backing"
   667               " store file %s : %s\n", path, strerror(errno));
   668               " store file %s : %s\n", path, os::strerror(errno));
   668     }
   669     }
   669   }
   670   }
   670 }
   671 }
   671 
   672 
   672 
   673 
   770       // we encountered some other failure while attempting
   771       // we encountered some other failure while attempting
   771       // to create the directory
   772       // to create the directory
   772       //
   773       //
   773       if (PrintMiscellaneous && Verbose) {
   774       if (PrintMiscellaneous && Verbose) {
   774         warning("could not create directory %s: %s\n",
   775         warning("could not create directory %s: %s\n",
   775                 dirname, strerror(errno));
   776                 dirname, os::strerror(errno));
   776       }
   777       }
   777       return false;
   778       return false;
   778     }
   779     }
   779   }
   780   }
   780   return true;
   781   return true;
   812   if (result == OS_ERR) {
   813   if (result == OS_ERR) {
   813     if (PrintMiscellaneous && Verbose) {
   814     if (PrintMiscellaneous && Verbose) {
   814       if (errno == ELOOP) {
   815       if (errno == ELOOP) {
   815         warning("file %s is a symlink and is not secure\n", filename);
   816         warning("file %s is a symlink and is not secure\n", filename);
   816       } else {
   817       } else {
   817         warning("could not create file %s: %s\n", filename, strerror(errno));
   818         warning("could not create file %s: %s\n", filename, os::strerror(errno));
   818       }
   819       }
   819     }
   820     }
   820     // close the directory and reset the current working directory
   821     // close the directory and reset the current working directory
   821     close_directory_secure_cwd(dirp, saved_cwd_fd);
   822     close_directory_secure_cwd(dirp, saved_cwd_fd);
   822 
   823 
   836 
   837 
   837   // truncate the file to get rid of any existing data
   838   // truncate the file to get rid of any existing data
   838   RESTARTABLE(::ftruncate(fd, (off_t)0), result);
   839   RESTARTABLE(::ftruncate(fd, (off_t)0), result);
   839   if (result == OS_ERR) {
   840   if (result == OS_ERR) {
   840     if (PrintMiscellaneous && Verbose) {
   841     if (PrintMiscellaneous && Verbose) {
   841       warning("could not truncate shared memory file: %s\n", strerror(errno));
   842       warning("could not truncate shared memory file: %s\n", os::strerror(errno));
   842     }
   843     }
   843     ::close(fd);
   844     ::close(fd);
   844     return -1;
   845     return -1;
   845   }
   846   }
   846   // set the file size
   847   // set the file size
   847   RESTARTABLE(::ftruncate(fd, (off_t)size), result);
   848   RESTARTABLE(::ftruncate(fd, (off_t)size), result);
   848   if (result == OS_ERR) {
   849   if (result == OS_ERR) {
   849     if (PrintMiscellaneous && Verbose) {
   850     if (PrintMiscellaneous && Verbose) {
   850       warning("could not set shared memory file size: %s\n", strerror(errno));
   851       warning("could not set shared memory file size: %s\n", os::strerror(errno));
   851     }
   852     }
   852     ::close(fd);
   853     ::close(fd);
   853     return -1;
   854     return -1;
   854   }
   855   }
   855 
   856 
   895     else if (errno == EACCES) {
   896     else if (errno == EACCES) {
   896       THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
   897       THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
   897                   "Permission denied", OS_ERR);
   898                   "Permission denied", OS_ERR);
   898     }
   899     }
   899     else {
   900     else {
   900       THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR);
   901       THROW_MSG_(vmSymbols::java_io_IOException(), os::strerror(errno), OS_ERR);
   901     }
   902     }
   902   }
   903   }
   903   int fd = result;
   904   int fd = result;
   904 
   905 
   905   // check to see if the file is secure
   906   // check to see if the file is secure
   968   result = ::close(fd);
   969   result = ::close(fd);
   969   assert(result != OS_ERR, "could not close file");
   970   assert(result != OS_ERR, "could not close file");
   970 
   971 
   971   if (mapAddress == MAP_FAILED) {
   972   if (mapAddress == MAP_FAILED) {
   972     if (PrintMiscellaneous && Verbose) {
   973     if (PrintMiscellaneous && Verbose) {
   973       warning("mmap failed -  %s\n", strerror(errno));
   974       warning("mmap failed -  %s\n", os::strerror(errno));
   974     }
   975     }
   975     remove_file(filename);
   976     remove_file(filename);
   976     FREE_C_HEAP_ARRAY(char, filename);
   977     FREE_C_HEAP_ARRAY(char, filename);
   977     return NULL;
   978     return NULL;
   978   }
   979   }
  1032   int result;
  1033   int result;
  1033 
  1034 
  1034   RESTARTABLE(::fstat(fd, &statbuf), result);
  1035   RESTARTABLE(::fstat(fd, &statbuf), result);
  1035   if (result == OS_ERR) {
  1036   if (result == OS_ERR) {
  1036     if (PrintMiscellaneous && Verbose) {
  1037     if (PrintMiscellaneous && Verbose) {
  1037       warning("fstat failed: %s\n", strerror(errno));
  1038       warning("fstat failed: %s\n", os::strerror(errno));
  1038     }
  1039     }
  1039     THROW_MSG_0(vmSymbols::java_io_IOException(),
  1040     THROW_MSG_0(vmSymbols::java_io_IOException(),
  1040                 "Could not determine PerfMemory size");
  1041                 "Could not determine PerfMemory size");
  1041   }
  1042   }
  1042 
  1043 
  1149   result = ::close(fd);
  1150   result = ::close(fd);
  1150   assert(result != OS_ERR, "could not close file");
  1151   assert(result != OS_ERR, "could not close file");
  1151 
  1152 
  1152   if (mapAddress == MAP_FAILED) {
  1153   if (mapAddress == MAP_FAILED) {
  1153     if (PrintMiscellaneous && Verbose) {
  1154     if (PrintMiscellaneous && Verbose) {
  1154       warning("mmap failed: %s\n", strerror(errno));
  1155       warning("mmap failed: %s\n", os::strerror(errno));
  1155     }
  1156     }
  1156     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
  1157     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
  1157               "Could not map PerfMemory");
  1158               "Could not map PerfMemory");
  1158   }
  1159   }
  1159 
  1160