comparison src/os/bsd/vm/os_bsd.cpp @ 14422:2b8e28fdf503

Merge
author kvn
date Tue, 05 Nov 2013 17:38:04 -0800
parents e2722a66aba7 3b32d287da89
children 8a9bb7821e28 bbfbe9b06038
comparison
equal deleted inserted replaced
14421:3068270ba476 14422:2b8e28fdf503
98 # include <link.h> 98 # include <link.h>
99 #endif 99 #endif
100 # include <stdint.h> 100 # include <stdint.h>
101 # include <inttypes.h> 101 # include <inttypes.h>
102 # include <sys/ioctl.h> 102 # include <sys/ioctl.h>
103 # include <sys/syscall.h>
103 104
104 #if defined(__FreeBSD__) || defined(__NetBSD__) 105 #if defined(__FreeBSD__) || defined(__NetBSD__)
105 # include <elf.h> 106 # include <elf.h>
106 #endif 107 #endif
107 108
150 151
151 //////////////////////////////////////////////////////////////////////////////// 152 ////////////////////////////////////////////////////////////////////////////////
152 // utility functions 153 // utility functions
153 154
154 static int SR_initialize(); 155 static int SR_initialize();
156 static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);
155 157
156 julong os::available_memory() { 158 julong os::available_memory() {
157 return Bsd::available_memory(); 159 return Bsd::available_memory();
158 } 160 }
159 161
162 // available here means free
160 julong os::Bsd::available_memory() { 163 julong os::Bsd::available_memory() {
161 // XXXBSD: this is just a stopgap implementation 164 uint64_t available = physical_memory() >> 2;
162 return physical_memory() >> 2; 165 #ifdef __APPLE__
166 mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
167 vm_statistics64_data_t vmstat;
168 kern_return_t kerr = host_statistics64(mach_host_self(), HOST_VM_INFO64,
169 (host_info64_t)&vmstat, &count);
170 assert(kerr == KERN_SUCCESS,
171 "host_statistics64 failed - check mach_host_self() and count");
172 if (kerr == KERN_SUCCESS) {
173 available = vmstat.free_count * os::vm_page_size();
174 }
175 #endif
176 return available;
163 } 177 }
164 178
165 julong os::physical_memory() { 179 julong os::physical_memory() {
166 return Bsd::physical_memory(); 180 return Bsd::physical_memory();
167 } 181 }
245 259
246 /* get physical memory via hw.memsize sysctl (hw.memsize is used 260 /* get physical memory via hw.memsize sysctl (hw.memsize is used
247 * since it returns a 64 bit value) 261 * since it returns a 64 bit value)
248 */ 262 */
249 mib[0] = CTL_HW; 263 mib[0] = CTL_HW;
264
265 #if defined (HW_MEMSIZE) // Apple
250 mib[1] = HW_MEMSIZE; 266 mib[1] = HW_MEMSIZE;
267 #elif defined(HW_PHYSMEM) // Most of BSD
268 mib[1] = HW_PHYSMEM;
269 #elif defined(HW_REALMEM) // Old FreeBSD
270 mib[1] = HW_REALMEM;
271 #else
272 #error No ways to get physmem
273 #endif
274
251 len = sizeof(mem_val); 275 len = sizeof(mem_val);
252 if (sysctl(mib, 2, &mem_val, &len, NULL, 0) != -1) { 276 if (sysctl(mib, 2, &mem_val, &len, NULL, 0) != -1) {
253 assert(len == sizeof(mem_val), "unexpected data size"); 277 assert(len == sizeof(mem_val), "unexpected data size");
254 _physical_memory = mem_val; 278 _physical_memory = mem_val;
255 } else { 279 } else {
677 osthread->set_state(ZOMBIE); 701 osthread->set_state(ZOMBIE);
678 sync->notify_all(); 702 sync->notify_all();
679 return NULL; 703 return NULL;
680 } 704 }
681 705
706 osthread->set_thread_id(os::Bsd::gettid());
707
682 #ifdef __APPLE__ 708 #ifdef __APPLE__
683 // thread_id is mach thread on macos, which pthreads graciously caches and provides for us 709 uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
684 mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self());
685 guarantee(thread_id != 0, "thread id missing from pthreads");
686 osthread->set_thread_id(thread_id);
687
688 uint64_t unique_thread_id = locate_unique_thread_id(thread_id);
689 guarantee(unique_thread_id != 0, "unique thread id was not found"); 710 guarantee(unique_thread_id != 0, "unique thread id was not found");
690 osthread->set_unique_thread_id(unique_thread_id); 711 osthread->set_unique_thread_id(unique_thread_id);
691 #else
692 // thread_id is pthread_id on BSD
693 osthread->set_thread_id(::pthread_self());
694 #endif 712 #endif
695 // initialize signal mask for this thread 713 // initialize signal mask for this thread
696 os::Bsd::hotspot_sigmask(thread); 714 os::Bsd::hotspot_sigmask(thread);
697 715
698 // initialize floating point control register 716 // initialize floating point control register
845 863
846 if (osthread == NULL) { 864 if (osthread == NULL) {
847 return false; 865 return false;
848 } 866 }
849 867
868 osthread->set_thread_id(os::Bsd::gettid());
869
850 // Store pthread info into the OSThread 870 // Store pthread info into the OSThread
851 #ifdef __APPLE__ 871 #ifdef __APPLE__
852 // thread_id is mach thread on macos, which pthreads graciously caches and provides for us 872 uint64_t unique_thread_id = locate_unique_thread_id(osthread->thread_id());
853 mach_port_t thread_id = ::pthread_mach_thread_np(::pthread_self());
854 guarantee(thread_id != 0, "just checking");
855 osthread->set_thread_id(thread_id);
856
857 uint64_t unique_thread_id = locate_unique_thread_id(thread_id);
858 guarantee(unique_thread_id != 0, "just checking"); 873 guarantee(unique_thread_id != 0, "just checking");
859 osthread->set_unique_thread_id(unique_thread_id); 874 osthread->set_unique_thread_id(unique_thread_id);
860 #else
861 osthread->set_thread_id(::pthread_self());
862 #endif 875 #endif
863 osthread->set_pthread_id(::pthread_self()); 876 osthread->set_pthread_id(::pthread_self());
864 877
865 // initialize floating point control register 878 // initialize floating point control register
866 os::Bsd::init_thread_fpu_state(); 879 os::Bsd::init_thread_fpu_state();
930 943
931 // Time since start-up in seconds to a fine granularity. 944 // Time since start-up in seconds to a fine granularity.
932 // Used by VMSelfDestructTimer and the MemProfiler. 945 // Used by VMSelfDestructTimer and the MemProfiler.
933 double os::elapsedTime() { 946 double os::elapsedTime() {
934 947
935 return (double)(os::elapsed_counter()) * 0.000001; 948 return ((double)os::elapsed_counter()) / os::elapsed_frequency();
936 } 949 }
937 950
938 jlong os::elapsed_counter() { 951 jlong os::elapsed_counter() {
939 timeval time; 952 return javaTimeNanos() - initial_time_count;
940 int status = gettimeofday(&time, NULL);
941 return jlong(time.tv_sec) * 1000 * 1000 + jlong(time.tv_usec) - initial_time_count;
942 } 953 }
943 954
944 jlong os::elapsed_frequency() { 955 jlong os::elapsed_frequency() {
945 return (1000 * 1000); 956 return NANOSECS_PER_SEC; // nanosecond resolution
946 } 957 }
947 958
948 bool os::supports_vtime() { return true; } 959 bool os::supports_vtime() { return true; }
949 bool os::enable_vtime() { return false; } 960 bool os::enable_vtime() { return false; }
950 bool os::vtime_enabled() { return false; } 961 bool os::vtime_enabled() { return false; }
1123 ::strncpy(buf, s, n); 1134 ::strncpy(buf, s, n);
1124 buf[n] = '\0'; 1135 buf[n] = '\0';
1125 return n; 1136 return n;
1126 } 1137 }
1127 1138
1139 // Information of current thread in variety of formats
1140 pid_t os::Bsd::gettid() {
1141 int retval = -1;
1142
1143 #ifdef __APPLE__ //XNU kernel
1144 // despite the fact mach port is actually not a thread id use it
1145 // instead of syscall(SYS_thread_selfid) as it certainly fits to u4
1146 retval = ::pthread_mach_thread_np(::pthread_self());
1147 guarantee(retval != 0, "just checking");
1148 return retval;
1149
1150 #elif __FreeBSD__
1151 retval = syscall(SYS_thr_self);
1152 #elif __OpenBSD__
1153 retval = syscall(SYS_getthrid);
1154 #elif __NetBSD__
1155 retval = (pid_t) syscall(SYS__lwp_self);
1156 #endif
1157
1158 if (retval == -1) {
1159 return getpid();
1160 }
1161 }
1162
1128 intx os::current_thread_id() { 1163 intx os::current_thread_id() {
1129 #ifdef __APPLE__ 1164 #ifdef __APPLE__
1130 return (intx)::pthread_mach_thread_np(::pthread_self()); 1165 return (intx)::pthread_mach_thread_np(::pthread_self());
1131 #else 1166 #else
1132 return (intx)::pthread_self(); 1167 return (intx)::pthread_self();
1133 #endif 1168 #endif
1134 } 1169 }
1170
1135 int os::current_process_id() { 1171 int os::current_process_id() {
1136 1172
1137 // Under the old bsd thread library, bsd gives each thread 1173 // Under the old bsd thread library, bsd gives each thread
1138 // its own process id. Because of this each thread will return 1174 // its own process id. Because of this each thread will return
1139 // a different pid if this method were to return the result 1175 // a different pid if this method were to return the result
1856 void wait(); 1892 void wait();
1857 bool trywait(); 1893 bool trywait();
1858 bool timedwait(unsigned int sec, int nsec); 1894 bool timedwait(unsigned int sec, int nsec);
1859 private: 1895 private:
1860 jlong currenttime() const; 1896 jlong currenttime() const;
1861 semaphore_t _semaphore; 1897 os_semaphore_t _semaphore;
1862 }; 1898 };
1863 1899
1864 Semaphore::Semaphore() : _semaphore(0) { 1900 Semaphore::Semaphore() : _semaphore(0) {
1865 SEM_INIT(_semaphore, 0); 1901 SEM_INIT(_semaphore, 0);
1866 } 1902 }
1924 return sem_trywait(&_semaphore) == 0; 1960 return sem_trywait(&_semaphore) == 0;
1925 } 1961 }
1926 1962
1927 bool Semaphore::timedwait(unsigned int sec, int nsec) { 1963 bool Semaphore::timedwait(unsigned int sec, int nsec) {
1928 struct timespec ts; 1964 struct timespec ts;
1929 jlong endtime = unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); 1965 unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
1930 1966
1931 while (1) { 1967 while (1) {
1932 int result = sem_timedwait(&_semaphore, &ts); 1968 int result = sem_timedwait(&_semaphore, &ts);
1933 if (result == 0) { 1969 if (result == 0) {
1934 return true; 1970 return true;
3498 3534
3499 // main_thread points to the aboriginal thread 3535 // main_thread points to the aboriginal thread
3500 Bsd::_main_thread = pthread_self(); 3536 Bsd::_main_thread = pthread_self();
3501 3537
3502 Bsd::clock_init(); 3538 Bsd::clock_init();
3503 initial_time_count = os::elapsed_counter(); 3539 initial_time_count = javaTimeNanos();
3504 3540
3505 #ifdef __APPLE__ 3541 #ifdef __APPLE__
3506 // XXXDARWIN 3542 // XXXDARWIN
3507 // Work around the unaligned VM callbacks in hotspot's 3543 // Work around the unaligned VM callbacks in hotspot's
3508 // sharedRuntime. The callbacks don't use SSE2 instructions, and work on 3544 // sharedRuntime. The callbacks don't use SSE2 instructions, and work on
3542 #ifndef PRODUCT 3578 #ifndef PRODUCT
3543 if(Verbose && PrintMiscellaneous) 3579 if(Verbose && PrintMiscellaneous)
3544 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page); 3580 tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
3545 #endif 3581 #endif
3546 } 3582 }
3547
3548 os::large_page_init();
3549 3583
3550 // initialize suspend/resume support - must do this before signal_sets_init() 3584 // initialize suspend/resume support - must do this before signal_sets_init()
3551 if (SR_initialize() != 0) { 3585 if (SR_initialize() != 0) {
3552 perror("SR_initialize failed"); 3586 perror("SR_initialize failed");
3553 return JNI_ERR; 3587 return JNI_ERR;
4664 // 4698 //
4665 // Since JDK8 xawt/libmawt.so was moved into the same directory 4699 // Since JDK8 xawt/libmawt.so was moved into the same directory
4666 // as libawt.so, and renamed libawt_xawt.so 4700 // as libawt.so, and renamed libawt_xawt.so
4667 // 4701 //
4668 bool os::is_headless_jre() { 4702 bool os::is_headless_jre() {
4703 #ifdef __APPLE__
4704 // We no longer build headless-only on Mac OS X
4705 return false;
4706 #else
4669 struct stat statbuf; 4707 struct stat statbuf;
4670 char buf[MAXPATHLEN]; 4708 char buf[MAXPATHLEN];
4671 char libmawtpath[MAXPATHLEN]; 4709 char libmawtpath[MAXPATHLEN];
4672 const char *xawtstr = "/xawt/libmawt" JNI_LIB_SUFFIX; 4710 const char *xawtstr = "/xawt/libmawt" JNI_LIB_SUFFIX;
4673 const char *new_xawtstr = "/libawt_xawt" JNI_LIB_SUFFIX; 4711 const char *new_xawtstr = "/libawt_xawt" JNI_LIB_SUFFIX;
4695 strcpy(libmawtpath, buf); 4733 strcpy(libmawtpath, buf);
4696 strcat(libmawtpath, new_xawtstr); 4734 strcat(libmawtpath, new_xawtstr);
4697 if (::stat(libmawtpath, &statbuf) == 0) return false; 4735 if (::stat(libmawtpath, &statbuf) == 0) return false;
4698 4736
4699 return true; 4737 return true;
4738 #endif
4700 } 4739 }
4701 4740
4702 // Get the default path to the core file 4741 // Get the default path to the core file
4703 // Returns the length of the string 4742 // Returns the length of the string
4704 int os::get_core_path(char* buffer, size_t bufferSize) { 4743 int os::get_core_path(char* buffer, size_t bufferSize) {