< prev index next >

src/hotspot/os/posix/os_posix.cpp

Print this page




  47 #include <sys/mman.h>
  48 #include <sys/resource.h>
  49 #include <sys/utsname.h>
  50 #include <time.h>
  51 #include <unistd.h>
  52 #include <utmpx.h>
  53 
  54 // Todo: provide a os::get_max_process_id() or similar. Number of processes
  55 // may have been configured, can be read more accurately from proc fs etc.
  56 #ifndef MAX_PID
  57 #define MAX_PID INT_MAX
  58 #endif
  59 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
  60 
  61 #define ROOT_UID 0
  62 
  63 #ifndef MAP_ANONYMOUS
  64   #define MAP_ANONYMOUS MAP_ANON
  65 #endif
  66 






  67 #define check_with_errno(check_type, cond, msg)                             \
  68   do {                                                                      \
  69     int err = errno;                                                        \
  70     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  71                os::errno_name(err));                                        \
  72 } while (false)
  73 
  74 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  75 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  76 
  77 // Check core dump limit and report possible place where core can be found
  78 void os::check_dump_limit(char* buffer, size_t bufferSize) {
  79   if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
  80     jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
  81     VMError::record_coredump_status(buffer, false);
  82     return;
  83   }
  84 
  85   int n;
  86   struct rlimit rlim;


 159 
 160 size_t os::lasterror(char *buf, size_t len) {
 161   if (errno == 0)  return 0;
 162 
 163   const char *s = os::strerror(errno);
 164   size_t n = ::strlen(s);
 165   if (n >= len) {
 166     n = len - 1;
 167   }
 168   ::strncpy(buf, s, n);
 169   buf[n] = '\0';
 170   return n;
 171 }
 172 
 173 void os::wait_for_keypress_at_exit(void) {
 174   // don't do anything on posix platforms
 175   return;
 176 }
 177 
 178 int os::create_file_for_heap(const char* dir) {

 179 













 180   const char name_template[] = "/jvmheap.XXXXXX";
 181 
 182   size_t fullname_len = strlen(dir) + strlen(name_template);
 183   char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);
 184   if (fullname == NULL) {
 185     vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));
 186     return -1;
 187   }
 188   int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);
 189   assert((size_t)n == fullname_len, "Unexpected number of characters in string");
 190 
 191   os::native_path(fullname);
 192 
 193   // set the file creation mask.
 194   mode_t file_mode = S_IRUSR | S_IWUSR;
 195 
 196   // create a new file.
 197   int fd = mkstemp(fullname);
 198 
 199   if (fd < 0) {
 200     warning("Could not create file for heap with template %s", fullname);
 201     os::free(fullname);
 202     return -1;
 203   }
 204 
 205   // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.
 206   int ret = unlink(fullname);
 207   assert_with_errno(ret == 0, "unlink returned error");

 208 
 209   os::free(fullname);


 210   return fd;
 211 }
 212 
 213 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {
 214   char * addr;
 215   int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;
 216   if (requested_addr != NULL) {
 217     assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");
 218     flags |= MAP_FIXED;
 219   }
 220 
 221   // Map reserved/uncommitted pages PROT_NONE so we fail early if we
 222   // touch an uncommitted page. Otherwise, the read/write might
 223   // succeed if we have enough swap space to back the physical page.
 224   addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
 225                        flags, -1, 0);
 226 
 227   if (addr != MAP_FAILED) {
 228     MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);
 229     return addr;




  47 #include <sys/mman.h>
  48 #include <sys/resource.h>
  49 #include <sys/utsname.h>
  50 #include <time.h>
  51 #include <unistd.h>
  52 #include <utmpx.h>
  53 
  54 // Todo: provide a os::get_max_process_id() or similar. Number of processes
  55 // may have been configured, can be read more accurately from proc fs etc.
  56 #ifndef MAX_PID
  57 #define MAX_PID INT_MAX
  58 #endif
  59 #define IS_VALID_PID(p) (p > 0 && p < MAX_PID)
  60 
  61 #define ROOT_UID 0
  62 
  63 #ifndef MAP_ANONYMOUS
  64   #define MAP_ANONYMOUS MAP_ANON
  65 #endif
  66 
  67 #ifndef O_TMPFILE
  68 #ifdef __O_TMPFILE
  69 #define O_TMPFILE __O_TMPFILE
  70 #endif
  71 #endif
  72 
  73 #define check_with_errno(check_type, cond, msg)                             \
  74   do {                                                                      \
  75     int err = errno;                                                        \
  76     check_type(cond, "%s; error='%s' (errno=%s)", msg, os::strerror(err),   \
  77                os::errno_name(err));                                        \
  78 } while (false)
  79 
  80 #define assert_with_errno(cond, msg)    check_with_errno(assert, cond, msg)
  81 #define guarantee_with_errno(cond, msg) check_with_errno(guarantee, cond, msg)
  82 
  83 // Check core dump limit and report possible place where core can be found
  84 void os::check_dump_limit(char* buffer, size_t bufferSize) {
  85   if (!FLAG_IS_DEFAULT(CreateCoredumpOnCrash) && !CreateCoredumpOnCrash) {
  86     jio_snprintf(buffer, bufferSize, "CreateCoredumpOnCrash is disabled from command line");
  87     VMError::record_coredump_status(buffer, false);
  88     return;
  89   }
  90 
  91   int n;
  92   struct rlimit rlim;


 165 
 166 size_t os::lasterror(char *buf, size_t len) {
 167   if (errno == 0)  return 0;
 168 
 169   const char *s = os::strerror(errno);
 170   size_t n = ::strlen(s);
 171   if (n >= len) {
 172     n = len - 1;
 173   }
 174   ::strncpy(buf, s, n);
 175   buf[n] = '\0';
 176   return n;
 177 }
 178 
 179 void os::wait_for_keypress_at_exit(void) {
 180   // don't do anything on posix platforms
 181   return;
 182 }
 183 
 184 int os::create_file_for_heap(const char* dir) {
 185   int fd;
 186 
 187 #ifdef O_TMPFILE
 188   char* native_dir = os::strdup(dir);
 189   if (native_dir == NULL) {
 190     vm_exit_during_initialization(err_msg("strdup failed during creation of backing file for heap (%s)", os::strerror(errno)));
 191     return -1;
 192   }
 193   os::native_path(native_dir);
 194   fd = os::open(dir, O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
 195   os::free(native_dir);
 196 
 197   if (fd == -1)
 198 #endif
 199   {
 200     const char name_template[] = "/jvmheap.XXXXXX";
 201 
 202     size_t fullname_len = strlen(dir) + strlen(name_template);
 203     char *fullname = (char*)os::malloc(fullname_len + 1, mtInternal);
 204     if (fullname == NULL) {
 205       vm_exit_during_initialization(err_msg("Malloc failed during creation of backing file for heap (%s)", os::strerror(errno)));
 206       return -1;
 207     }
 208     int n = snprintf(fullname, fullname_len + 1, "%s%s", dir, name_template);
 209     assert((size_t)n == fullname_len, "Unexpected number of characters in string");
 210 
 211     os::native_path(fullname);
 212 



 213     // create a new file.
 214     fd = mkstemp(fullname);
 215 
 216     if (fd < 0) {
 217       warning("Could not create file for heap with template %s", fullname);
 218       os::free(fullname);
 219       return -1;
 220     } else {

 221       // delete the name from the filesystem. When 'fd' is closed, the file (and space) will be deleted.
 222       int ret = unlink(fullname);
 223       assert_with_errno(ret == 0, "unlink returned error");
 224     }
 225 
 226     os::free(fullname);
 227   }
 228 
 229   return fd;
 230 }
 231 
 232 static char* reserve_mmapped_memory(size_t bytes, char* requested_addr) {
 233   char * addr;
 234   int flags = MAP_PRIVATE NOT_AIX( | MAP_NORESERVE ) | MAP_ANONYMOUS;
 235   if (requested_addr != NULL) {
 236     assert((uintptr_t)requested_addr % os::vm_page_size() == 0, "Requested address should be aligned to OS page size");
 237     flags |= MAP_FIXED;
 238   }
 239 
 240   // Map reserved/uncommitted pages PROT_NONE so we fail early if we
 241   // touch an uncommitted page. Otherwise, the read/write might
 242   // succeed if we have enough swap space to back the physical page.
 243   addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,
 244                        flags, -1, 0);
 245 
 246   if (addr != MAP_FAILED) {
 247     MemTracker::record_virtual_memory_reserve((address)addr, bytes, CALLER_PC);
 248     return addr;


< prev index next >