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);




  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       jio_snprintf(&core_path[n], PATH_MAX - n, ".%d", current_process_id());
6121     }
6122 
6123   }
6124 
6125   if (core_path[0] == '/') {
6126     jio_snprintf(buffer, bufferSize, core_path);
6127   } else {
6128     char cwd[PATH_MAX];
6129     const char* p = get_current_directory(cwd, PATH_MAX);
6130 
6131     if (p == NULL) {
6132       assert(p != NULL, "failed to get current directory");
6133       return 0;
6134     }
6135 
6136     if (core_path[0] == '|') {
6137       jio_snprintf(buffer, bufferSize, "\"%s\" (or dumping to %s/core.%d)",
6138                                        &core_path[1], p, current_process_id());
6139     } else {
6140       jio_snprintf(buffer, bufferSize, "%s/%s", p, core_path);
6141     }
6142 
6143   }
6144 
6145   return strlen(buffer);
6146 }
6147 
6148 #ifdef JAVASE_EMBEDDED
6149 //
6150 // A thread to watch the '/dev/mem_notify' device, which will tell us when the OS is running low on memory.
6151 //
6152 MemNotifyThread* MemNotifyThread::_memnotify_thread = NULL;
6153 
6154 // ctor
6155 //
6156 MemNotifyThread::MemNotifyThread(int fd): Thread() {
6157   assert(memnotify_thread() == NULL, "we can only allocate one MemNotifyThread");
6158   _fd = fd;
6159 
6160   if (os::create_thread(this, os::os_thread)) {
6161     _memnotify_thread = this;
6162     os::set_priority(this, NearMaxPriority);