src/os/linux/vm/os_linux.cpp

Print this page




  86 # include <sys/stat.h>
  87 # include <sys/time.h>
  88 # include <sys/times.h>
  89 # include <sys/utsname.h>
  90 # include <sys/socket.h>
  91 # include <sys/wait.h>
  92 # include <pwd.h>
  93 # include <poll.h>
  94 # include <semaphore.h>
  95 # include <fcntl.h>
  96 # include <string.h>
  97 # include <syscall.h>
  98 # include <sys/sysinfo.h>
  99 # include <gnu/libc-version.h>
 100 # include <sys/ipc.h>
 101 # include <sys/shm.h>
 102 # include <link.h>
 103 # include <stdint.h>
 104 # include <inttypes.h>
 105 # include <sys/ioctl.h>

 106 
 107 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
 108 
 109 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
 110 // getrusage() is prepared to handle the associated failure.
 111 #ifndef RUSAGE_THREAD
 112   #define RUSAGE_THREAD   (1)               /* only the calling thread */
 113 #endif
 114 
 115 #define MAX_PATH    (2 * K)
 116 
 117 #define MAX_SECS 100000000
 118 
 119 // for timer info max values which include all bits
 120 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
 121 
 122 #define LARGEPAGES_BIT (1 << 6)
 123 ////////////////////////////////////////////////////////////////////////////////
 124 // global variables
 125 julong os::Linux::_physical_memory = 0;


5980   } else {
5981     *p = '\0';
5982   }
5983 
5984   // check xawt/libmawt.so
5985   strcpy(libmawtpath, buf);
5986   strcat(libmawtpath, xawtstr);
5987   if (::stat(libmawtpath, &statbuf) == 0) return false;
5988 
5989   // check libawt_xawt.so
5990   strcpy(libmawtpath, buf);
5991   strcat(libmawtpath, new_xawtstr);
5992   if (::stat(libmawtpath, &statbuf) == 0) return false;
5993 
5994   return true;
5995 }
5996 
5997 // Get the default path to the core file
5998 // Returns the length of the string
5999 int os::get_core_path(char* buffer, size_t bufferSize) {
6000   const char* p = get_current_directory(buffer, bufferSize);




































































































































6001 
6002   if (p == NULL) {
6003     assert(p != NULL, "failed to get current directory");
6004     return 0;
6005   }
6006 









6007   return strlen(buffer);
6008 }





































6009 
6010 #ifdef JAVASE_EMBEDDED
6011 //
6012 // A thread to watch the '/dev/mem_notify' device, which will tell us when the OS is running low on memory.
6013 //
6014 MemNotifyThread* MemNotifyThread::_memnotify_thread = NULL;
6015 
6016 // ctor
6017 //
6018 MemNotifyThread::MemNotifyThread(int fd): Thread() {
6019   assert(memnotify_thread() == NULL, "we can only allocate one MemNotifyThread");
6020   _fd = fd;
6021 
6022   if (os::create_thread(this, os::os_thread)) {
6023     _memnotify_thread = this;
6024     os::set_priority(this, NearMaxPriority);
6025     os::start_thread(this);
6026   }
6027 }
6028 




  86 # include <sys/stat.h>
  87 # include <sys/time.h>
  88 # include <sys/times.h>
  89 # include <sys/utsname.h>
  90 # include <sys/socket.h>
  91 # include <sys/wait.h>
  92 # include <pwd.h>
  93 # include <poll.h>
  94 # include <semaphore.h>
  95 # include <fcntl.h>
  96 # include <string.h>
  97 # include <syscall.h>
  98 # include <sys/sysinfo.h>
  99 # include <gnu/libc-version.h>
 100 # include <sys/ipc.h>
 101 # include <sys/shm.h>
 102 # include <link.h>
 103 # include <stdint.h>
 104 # include <inttypes.h>
 105 # include <sys/ioctl.h>
 106 # include <sys/prctl.h>
 107 
 108 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
 109 
 110 // if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
 111 // getrusage() is prepared to handle the associated failure.
 112 #ifndef RUSAGE_THREAD
 113   #define RUSAGE_THREAD   (1)               /* only the calling thread */
 114 #endif
 115 
 116 #define MAX_PATH    (2 * K)
 117 
 118 #define MAX_SECS 100000000
 119 
 120 // for timer info max values which include all bits
 121 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
 122 
 123 #define LARGEPAGES_BIT (1 << 6)
 124 ////////////////////////////////////////////////////////////////////////////////
 125 // global variables
 126 julong os::Linux::_physical_memory = 0;


5981   } else {
5982     *p = '\0';
5983   }
5984 
5985   // check xawt/libmawt.so
5986   strcpy(libmawtpath, buf);
5987   strcat(libmawtpath, xawtstr);
5988   if (::stat(libmawtpath, &statbuf) == 0) return false;
5989 
5990   // check libawt_xawt.so
5991   strcpy(libmawtpath, buf);
5992   strcat(libmawtpath, new_xawtstr);
5993   if (::stat(libmawtpath, &statbuf) == 0) return false;
5994 
5995   return true;
5996 }
5997 
5998 // Get the default path to the core file
5999 // Returns the length of the string
6000 int os::get_core_path(char* buffer, size_t bufferSize) {
6001   /*
6002    * Max length of /proc/sys/kernel/core_pattern is 128 characters.
6003    * See https://www.kernel.org/doc/Documentation/sysctl/kernel.txt
6004    */
6005   const int core_pattern_len = 129;
6006   char core_pattern[core_pattern_len] = {0};
6007 
6008   FILE *core_pattern_file = fopen("/proc/sys/kernel/core_pattern", "r");
6009   if (core_pattern_file != NULL) {
6010     char *ret_ptr = fgets(core_pattern, core_pattern_len, core_pattern_file);
6011     fclose(core_pattern_file);
6012 
6013     if (ret_ptr != NULL) {
6014       char *last_char = core_pattern + strlen(core_pattern) - 1;
6015       if (*last_char == '\n') {
6016         *last_char = '\0';
6017       }
6018     }
6019 
6020   }
6021 
6022   if (strlen(core_pattern) == 0) {
6023     return 0;
6024   }
6025 
6026   // Get executable path for 'e' and 'E'
6027   char exe_path[PATH_MAX] = {0};
6028   readlink("/proc/self/exe", exe_path, PATH_MAX);
6029 
6030   char core_path[PATH_MAX] = {0};
6031   bool with_pid = false;
6032   size_t n = 0;
6033   char *core_pattern_seq = core_pattern;
6034   while (*core_pattern_seq) {
6035 
6036     if (*core_pattern_seq == '%') {
6037       int written = 0;
6038       core_pattern_seq++; // Get format character
6039 
6040       switch (*core_pattern_seq) {
6041 
6042         case '\0': // NULL termination
6043           break;
6044 
6045         case '%': // double percent
6046           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%%");
6047           break;
6048 
6049         case 'p': // pid
6050           with_pid = true;
6051           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%d", current_process_id());
6052           break;
6053 
6054         case 'u': // uid
6055           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%d", getuid());
6056           break;
6057 
6058         case 'g': // gid
6059           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%d", getgid());
6060           break;
6061           
6062         case 'd': // dump mode
6063           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%d",
6064                                prctl(PR_GET_DUMPABLE, NULL, NULL, NULL, NULL));
6065           break;
6066 
6067         case 'h': // hostname
6068           struct utsname un;
6069           uname(&un); // This call will be succeeded.
6070           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%s", un.nodename);
6071           break;
6072 
6073         case 'e': // executable filename (shorten)
6074           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%s", basename(exe_path));
6075           break;
6076 
6077         case 'E': // executable filename (path)
6078           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%s", exe_path);
6079           break;
6080 
6081         case 'c': // core limit size
6082           struct rlimit rlim;
6083           getrlimit(RLIMIT_CORE, &rlim); // Calling getrlimit() to myself will be succeeded.
6084           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%lu", rlim.rlim_cur);
6085           break;
6086 
6087         /*
6088          * We cannot other values.
6089          * e.g. 'P' (global pid), 's' (signal), 't' (UNIX time of dump)
6090          *
6091          *  - 't' will set to core dump time in Linux kernel.
6092          *    Thus we cannot set it in this function.
6093          *  - If user set undefined character, Linux kernel will drop it.
6094          *    We remain it because Linux kernel may define it in the future.
6095          */
6096         default:
6097           written = jio_snprintf(&core_path[n], PATH_MAX - n, "%%%c", *core_pattern_seq);
6098           break;
6099 
6100       }
6101 
6102       n += written;
6103     } else {
6104       core_path[n] = *core_pattern_seq;
6105       n++;
6106     }
6107 
6108     core_pattern_seq++; // Get next character
6109   }
6110 
6111   if (!with_pid && (core_path[0] != '|')) {
6112     char val[2] = {0};
6113     FILE *core_uses_pid_file = fopen("/proc/sys/kernel/core_uses_pid", "r");
6114     if (core_uses_pid_file != NULL) {
6115       fgets(val, 2, core_uses_pid_file);
6116       fclose(core_uses_pid_file);
6117     }
6118 
6119     if (val[0] == '1') {
6120       n += jio_snprintf(&core_path[n], PATH_MAX - n, ".%d", current_process_id());
6121     }
6122 
6123   }
6124 
6125   if (n > bufferSize) {
6126     n = bufferSize;
6127   }
6128 
6129   if (core_path[0] == '/') {
6130     jio_snprintf(buffer, n, core_path);
6131   } else {
6132     char cwd[PATH_MAX];
6133     const char* p = get_current_directory(cwd, PATH_MAX);
6134 
6135     if (p == NULL) {
6136       assert(p != NULL, "failed to get current directory");
6137       return 0;
6138     }
6139 
6140     if (core_path[0] == '|') {
6141       jio_snprintf(buffer, bufferSize, "%s (or dumping to %s/core.%d)",
6142                                            core_path, p, current_process_id());
6143     } else {
6144       jio_snprintf(buffer, bufferSize, "%s/%s", p, core_path);
6145     }
6146 
6147   }
6148 
6149   return strlen(buffer);
6150 }
6151 
6152 // Check core dump limit and report possible place where core can be found
6153 void os::check_or_create_dump(void* exceptionRecord, void* contextRecord, char* buffer, size_t bufferSize) {
6154   int n;
6155   bool success;
6156   char core_path[PATH_MAX];
6157 
6158   n = get_core_path(core_path, PATH_MAX);
6159 
6160   if (n == 0) {
6161     jio_snprintf(buffer, bufferSize, "/core or core.%d (may not exist)", current_process_id());
6162     success = true;
6163   } else if (core_path[0] == '|') {
6164     jio_snprintf(buffer, bufferSize, "Core dumps may be processed with \"%s\"", &core_path[1]);
6165     success = true;
6166   } else {
6167     struct rlimit rlim;
6168     getrlimit(RLIMIT_CORE, &rlim); // Calling getrlimit() to myself will be succeeded.
6169 
6170     switch(rlim.rlim_cur) {
6171       case RLIM_INFINITY:
6172         jio_snprintf(buffer, bufferSize, "%s", core_path);
6173         success = true;
6174         break;
6175       case 0:
6176         jio_snprintf(buffer, bufferSize, "Core dumps have been disabled. To enable core dumping, try \"ulimit -c unlimited\" before starting Java again");
6177         success = false;
6178         break;
6179       default:
6180         jio_snprintf(buffer, bufferSize, "%s (max size %lu kB). To ensure a full core dump, try \"ulimit -c unlimited\" before starting Java again", core_path, (unsigned long)(rlim.rlim_cur >> 10));
6181         success = true;
6182         break;
6183     }
6184   }
6185   VMError::report_coredump_status(buffer, success);
6186 }
6187 
6188 
6189 #ifdef JAVASE_EMBEDDED
6190 //
6191 // A thread to watch the '/dev/mem_notify' device, which will tell us when the OS is running low on memory.
6192 //
6193 MemNotifyThread* MemNotifyThread::_memnotify_thread = NULL;
6194 
6195 // ctor
6196 //
6197 MemNotifyThread::MemNotifyThread(int fd): Thread() {
6198   assert(memnotify_thread() == NULL, "we can only allocate one MemNotifyThread");
6199   _fd = fd;
6200 
6201   if (os::create_thread(this, os::os_thread)) {
6202     _memnotify_thread = this;
6203     os::set_priority(this, NearMaxPriority);
6204     os::start_thread(this);
6205   }
6206 }
6207