src/os/bsd/vm/os_bsd.cpp

changeset 6472
2b8e28fdf503
parent 6462
e2722a66aba7
parent 6054
3b32d287da89
child 6513
bbfbe9b06038
     1.1 --- a/src/os/bsd/vm/os_bsd.cpp	Wed Oct 16 10:52:41 2013 +0200
     1.2 +++ b/src/os/bsd/vm/os_bsd.cpp	Tue Nov 05 17:38:04 2013 -0800
     1.3 @@ -100,6 +100,7 @@
     1.4  # include <stdint.h>
     1.5  # include <inttypes.h>
     1.6  # include <sys/ioctl.h>
     1.7 +# include <sys/syscall.h>
     1.8  
     1.9  #if defined(__FreeBSD__) || defined(__NetBSD__)
    1.10  # include <elf.h>
    1.11 @@ -152,14 +153,27 @@
    1.12  // utility functions
    1.13  
    1.14  static int SR_initialize();
    1.15 +static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
    1.16  
    1.17  julong os::available_memory() {
    1.18    return Bsd::available_memory();
    1.19  }
    1.20  
    1.21 +// available here means free
    1.22  julong os::Bsd::available_memory() {
    1.23 -  // XXXBSD: this is just a stopgap implementation
    1.24 -  return physical_memory() >> 2;
    1.25 +  uint64_t available = physical_memory() >> 2;
    1.26 +#ifdef __APPLE__
    1.27 +  mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
    1.28 +  vm_statistics64_data_t vmstat;
    1.29 +  kern_return_t kerr = host_statistics64(mach_host_self(), HOST_VM_INFO64,
    1.30 +                                         (host_info64_t)&vmstat, &count);
    1.31 +  assert(kerr == KERN_SUCCESS,
    1.32 +         "host_statistics64 failed - check mach_host_self() and count");
    1.33 +  if (kerr == KERN_SUCCESS) {
    1.34 +    available = vmstat.free_count * os::vm_page_size();
    1.35 +  }
    1.36 +#endif
    1.37 +  return available;
    1.38  }
    1.39  
    1.40  julong os::physical_memory() {
    1.41 @@ -247,7 +261,17 @@
    1.42     * since it returns a 64 bit value)
    1.43     */
    1.44    mib[0] = CTL_HW;
    1.45 +
    1.46 +#if defined (HW_MEMSIZE) // Apple
    1.47    mib[1] = HW_MEMSIZE;
    1.48 +#elif defined(HW_PHYSMEM) // Most of BSD
    1.49 +  mib[1] = HW_PHYSMEM;
    1.50 +#elif defined(HW_REALMEM) // Old FreeBSD
    1.51 +  mib[1] = HW_REALMEM;
    1.52 +#else
    1.53 +  #error No ways to get physmem
    1.54 +#endif
    1.55 +
    1.56    len = sizeof(mem_val);
    1.57    if (sysctl(mib, 2, &mem_val, &len, NULL, 0) != -1) {
    1.58         assert(len == sizeof(mem_val), "unexpected data size");
    1.59 @@ -679,18 +703,12 @@
    1.60      return NULL;
    1.61    }
    1.62  
    1.63 +  osthread->set_thread_id(os::Bsd::gettid());
    1.64 +
    1.65  #ifdef __APPLE__
    1.66 -  // thread_id is mach thread on macos, which pthreads graciously caches and provides for us
    1.67 -  mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self());
    1.68 -  guarantee(thread_id != 0, "thread id missing from pthreads");
    1.69 -  osthread->set_thread_id(thread_id);
    1.70 -
    1.71 -  uint64_t unique_thread_id = locate_unique_thread_id(thread_id);
    1.72 +  uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
    1.73    guarantee(unique_thread_id != 0, "unique thread id was not found");
    1.74    osthread->set_unique_thread_id(unique_thread_id);
    1.75 -#else
    1.76 -  // thread_id is pthread_id on BSD
    1.77 -  osthread->set_thread_id(::pthread_self());
    1.78  #endif
    1.79    // initialize signal mask for this thread
    1.80    os::Bsd::hotspot_sigmask(thread);
    1.81 @@ -847,18 +865,13 @@
    1.82      return false;
    1.83    }
    1.84  
    1.85 +  osthread->set_thread_id(os::Bsd::gettid());
    1.86 +
    1.87    // Store pthread info into the OSThread
    1.88  #ifdef __APPLE__
    1.89 -  // thread_id is mach thread on macos, which pthreads graciously caches and provides for us
    1.90 -  mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self());
    1.91 -  guarantee(thread_id != 0, "just checking");
    1.92 -  osthread->set_thread_id(thread_id);
    1.93 -
    1.94 -  uint64_t unique_thread_id = locate_unique_thread_id(thread_id);
    1.95 +  uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
    1.96    guarantee(unique_thread_id != 0, "just checking");
    1.97    osthread->set_unique_thread_id(unique_thread_id);
    1.98 -#else
    1.99 -  osthread->set_thread_id(::pthread_self());
   1.100  #endif
   1.101    osthread->set_pthread_id(::pthread_self());
   1.102  
   1.103 @@ -932,17 +945,15 @@
   1.104  // Used by VMSelfDestructTimer and the MemProfiler.
   1.105  double os::elapsedTime() {
   1.106  
   1.107 -  return (double)(os::elapsed_counter()) * 0.000001;
   1.108 +  return ((double)os::elapsed_counter()) / os::elapsed_frequency();
   1.109  }
   1.110  
   1.111  jlong os::elapsed_counter() {
   1.112 -  timeval time;
   1.113 -  int status = gettimeofday(&time, NULL);
   1.114 -  return jlong(time.tv_sec) * 1000 * 1000 + jlong(time.tv_usec) - initial_time_count;
   1.115 +  return javaTimeNanos() - initial_time_count;
   1.116  }
   1.117  
   1.118  jlong os::elapsed_frequency() {
   1.119 -  return (1000 * 1000);
   1.120 +  return NANOSECS_PER_SEC; // nanosecond resolution
   1.121  }
   1.122  
   1.123  bool os::supports_vtime() { return true; }
   1.124 @@ -1125,6 +1136,30 @@
   1.125    return n;
   1.126  }
   1.127  
   1.128 +// Information of current thread in variety of formats
   1.129 +pid_t os::Bsd::gettid() {
   1.130 +  int retval = -1;
   1.131 +
   1.132 +#ifdef __APPLE__ //XNU kernel
   1.133 +  // despite the fact mach port is actually not a thread id use it
   1.134 +  // instead of syscall(SYS_thread_selfid) as it certainly fits to u4
   1.135 +  retval = ::pthread_mach_thread_np(::pthread_self());
   1.136 +  guarantee(retval != 0, "just checking");
   1.137 +  return retval;
   1.138 +
   1.139 +#elif __FreeBSD__
   1.140 +  retval = syscall(SYS_thr_self);
   1.141 +#elif __OpenBSD__
   1.142 +  retval = syscall(SYS_getthrid);
   1.143 +#elif __NetBSD__
   1.144 +  retval = (pid_t) syscall(SYS__lwp_self);
   1.145 +#endif
   1.146 +
   1.147 +  if (retval == -1) {
   1.148 +    return getpid();
   1.149 +  }
   1.150 +}
   1.151 +
   1.152  intx os::current_thread_id() {
   1.153  #ifdef __APPLE__
   1.154    return (intx)::pthread_mach_thread_np(::pthread_self());
   1.155 @@ -1132,6 +1167,7 @@
   1.156    return (intx)::pthread_self();
   1.157  #endif
   1.158  }
   1.159 +
   1.160  int os::current_process_id() {
   1.161  
   1.162    // Under the old bsd thread library, bsd gives each thread
   1.163 @@ -1858,7 +1894,7 @@
   1.164      bool timedwait(unsigned int sec, int nsec);
   1.165    private:
   1.166      jlong currenttime() const;
   1.167 -    semaphore_t _semaphore;
   1.168 +    os_semaphore_t _semaphore;
   1.169  };
   1.170  
   1.171  Semaphore::Semaphore() : _semaphore(0) {
   1.172 @@ -1926,7 +1962,7 @@
   1.173  
   1.174  bool Semaphore::timedwait(unsigned int sec, int nsec) {
   1.175    struct timespec ts;
   1.176 -  jlong endtime = unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
   1.177 +  unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
   1.178  
   1.179    while (1) {
   1.180      int result = sem_timedwait(&_semaphore, &ts);
   1.181 @@ -3500,7 +3536,7 @@
   1.182    Bsd::_main_thread = pthread_self();
   1.183  
   1.184    Bsd::clock_init();
   1.185 -  initial_time_count = os::elapsed_counter();
   1.186 +  initial_time_count = javaTimeNanos();
   1.187  
   1.188  #ifdef __APPLE__
   1.189    // XXXDARWIN
   1.190 @@ -3545,8 +3581,6 @@
   1.191  #endif
   1.192    }
   1.193  
   1.194 -  os::large_page_init();
   1.195 -
   1.196    // initialize suspend/resume support - must do this before signal_sets_init()
   1.197    if (SR_initialize() != 0) {
   1.198      perror("SR_initialize failed");
   1.199 @@ -4666,6 +4700,10 @@
   1.200  // as libawt.so, and renamed libawt_xawt.so
   1.201  //
   1.202  bool os::is_headless_jre() {
   1.203 +#ifdef __APPLE__
   1.204 +    // We no longer build headless-only on Mac OS X
   1.205 +    return false;
   1.206 +#else
   1.207      struct stat statbuf;
   1.208      char buf[MAXPATHLEN];
   1.209      char libmawtpath[MAXPATHLEN];
   1.210 @@ -4697,6 +4735,7 @@
   1.211      if (::stat(libmawtpath, &statbuf) == 0) return false;
   1.212  
   1.213      return true;
   1.214 +#endif
   1.215  }
   1.216  
   1.217  // Get the default path to the core file

mercurial