comparison src/os/bsd/vm/os_bsd.cpp @ 13086:096c224171c4

Merge with http://hg.openjdk.java.net/hsx/hsx25/hotspot/
author Doug Simon <doug.simon@oracle.com>
date Wed, 20 Nov 2013 00:10:38 +0100
parents cefad50507d8 3b32d287da89
children d8041d695d19
comparison
equal deleted inserted replaced
12782:92b7ec34ddfa 13086:096c224171c4
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
1902 void wait(); 1938 void wait();
1903 bool trywait(); 1939 bool trywait();
1904 bool timedwait(unsigned int sec, int nsec); 1940 bool timedwait(unsigned int sec, int nsec);
1905 private: 1941 private:
1906 jlong currenttime() const; 1942 jlong currenttime() const;
1907 semaphore_t _semaphore; 1943 os_semaphore_t _semaphore;
1908 }; 1944 };
1909 1945
1910 Semaphore::Semaphore() : _semaphore(0) { 1946 Semaphore::Semaphore() : _semaphore(0) {
1911 SEM_INIT(_semaphore, 0); 1947 SEM_INIT(_semaphore, 0);
1912 } 1948 }
1970 return sem_trywait(&_semaphore) == 0; 2006 return sem_trywait(&_semaphore) == 0;
1971 } 2007 }
1972 2008
1973 bool Semaphore::timedwait(unsigned int sec, int nsec) { 2009 bool Semaphore::timedwait(unsigned int sec, int nsec) {
1974 struct timespec ts; 2010 struct timespec ts;
1975 jlong endtime = unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec); 2011 unpackTime(&ts, false, (sec * NANOSECS_PER_SEC) + nsec);
1976 2012
1977 while (1) { 2013 while (1) {
1978 int result = sem_timedwait(&_semaphore, &ts); 2014 int result = sem_timedwait(&_semaphore, &ts);
1979 if (result == 0) { 2015 if (result == 0) {
1980 return true; 2016 return true;
3542 3578
3543 // main_thread points to the aboriginal thread 3579 // main_thread points to the aboriginal thread
3544 Bsd::_main_thread = pthread_self(); 3580 Bsd::_main_thread = pthread_self();
3545 3581
3546 Bsd::clock_init(); 3582 Bsd::clock_init();
3547 initial_time_count = os::elapsed_counter(); 3583 initial_time_count = javaTimeNanos();
3548 3584
3549 #ifdef __APPLE__ 3585 #ifdef __APPLE__
3550 // XXXDARWIN 3586 // XXXDARWIN
3551 // Work around the unaligned VM callbacks in hotspot's 3587 // Work around the unaligned VM callbacks in hotspot's
3552 // sharedRuntime. The callbacks don't use SSE2 instructions, and work on 3588 // sharedRuntime. The callbacks don't use SSE2 instructions, and work on
4706 // 4742 //
4707 // Since JDK8 xawt/libmawt.so was moved into the same directory 4743 // Since JDK8 xawt/libmawt.so was moved into the same directory
4708 // as libawt.so, and renamed libawt_xawt.so 4744 // as libawt.so, and renamed libawt_xawt.so
4709 // 4745 //
4710 bool os::is_headless_jre() { 4746 bool os::is_headless_jre() {
4747 #ifdef __APPLE__
4748 // We no longer build headless-only on Mac OS X
4749 return false;
4750 #else
4711 struct stat statbuf; 4751 struct stat statbuf;
4712 char buf[MAXPATHLEN]; 4752 char buf[MAXPATHLEN];
4713 char libmawtpath[MAXPATHLEN]; 4753 char libmawtpath[MAXPATHLEN];
4714 const char *xawtstr = "/xawt/libmawt" JNI_LIB_SUFFIX; 4754 const char *xawtstr = "/xawt/libmawt" JNI_LIB_SUFFIX;
4715 const char *new_xawtstr = "/libawt_xawt" JNI_LIB_SUFFIX; 4755 const char *new_xawtstr = "/libawt_xawt" JNI_LIB_SUFFIX;
4737 strcpy(libmawtpath, buf); 4777 strcpy(libmawtpath, buf);
4738 strcat(libmawtpath, new_xawtstr); 4778 strcat(libmawtpath, new_xawtstr);
4739 if (::stat(libmawtpath, &statbuf) == 0) return false; 4779 if (::stat(libmawtpath, &statbuf) == 0) return false;
4740 4780
4741 return true; 4781 return true;
4782 #endif
4742 } 4783 }
4743 4784
4744 // Get the default path to the core file 4785 // Get the default path to the core file
4745 // Returns the length of the string 4786 // Returns the length of the string
4746 int os::get_core_path(char* buffer, size_t bufferSize) { 4787 int os::get_core_path(char* buffer, size_t bufferSize) {