53 #include "runtime/osThread.hpp" 54 #include "runtime/perfMemory.hpp" 55 #include "runtime/semaphore.hpp" 56 #include "runtime/sharedRuntime.hpp" 57 #include "runtime/statSampler.hpp" 58 #include "runtime/stubRoutines.hpp" 59 #include "runtime/thread.inline.hpp" 60 #include "runtime/threadCritical.hpp" 61 #include "runtime/timer.hpp" 62 #include "services/attachListener.hpp" 63 #include "services/memTracker.hpp" 64 #include "services/runtimeService.hpp" 65 #include "utilities/align.hpp" 66 #include "utilities/decoder.hpp" 67 #include "utilities/defaultStream.hpp" 68 #include "utilities/events.hpp" 69 #include "utilities/growableArray.hpp" 70 #include "utilities/vmError.hpp" 71 72 // put OS-includes here 73 # include <sys/types.h> 74 # include <sys/mman.h> 75 # include <sys/stat.h> 76 # include <sys/select.h> 77 # include <pthread.h> 78 # include <signal.h> 79 # include <errno.h> 80 # include <dlfcn.h> 81 # include <stdio.h> 82 # include <unistd.h> 83 # include <sys/resource.h> 84 # include <pthread.h> 85 # include <sys/stat.h> 86 # include <sys/time.h> 87 # include <sys/times.h> 88 # include <sys/utsname.h> 89 # include <sys/socket.h> 90 # include <sys/wait.h> 91 # include <time.h> 92 # include <pwd.h> 93 # include <poll.h> 94 # include <fcntl.h> 95 # include <string.h> 96 # include <sys/param.h> 97 # include <sys/sysctl.h> 98 # include <sys/ipc.h> 99 # include <sys/shm.h> 100 #ifndef __APPLE__ 101 # include <link.h> 102 #endif 103 # include <stdint.h> 104 # include <inttypes.h> 105 # include <sys/ioctl.h> 106 # include <sys/syscall.h> 107 108 #if defined(__FreeBSD__) || defined(__NetBSD__) 109 #include <elf.h> 110 #endif 111 112 #ifdef __APPLE__ 113 #include <mach/mach.h> // semaphore_* API 114 #include <mach-o/dyld.h> 115 #include <sys/proc_info.h> 116 #include <objc/objc-auto.h> 117 #endif 118 119 #ifndef MAP_ANONYMOUS 120 #define MAP_ANONYMOUS MAP_ANON 121 #endif 122 123 #define MAX_PATH (2 * K) 124 125 // for timer info max values which include all bits 126 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 127 128 #define LARGEPAGES_BIT (1 << 6) 129 130 //////////////////////////////////////////////////////////////////////////////// 131 // global variables 132 julong os::Bsd::_physical_memory = 0; 133 134 #ifdef __APPLE__ 135 mach_timebase_info_data_t os::Bsd::_timebase_info = {0, 0}; 136 volatile uint64_t os::Bsd::_max_abstime = 0; 137 #else 138 int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL; 139 #endif 140 pthread_t os::Bsd::_main_thread; 141 int os::Bsd::_page_size = -1; 142 143 static jlong initial_time_count=0; 144 145 static int clock_tics_per_sec = 100; 146 147 // For diagnostics to print a message once. see run_periodic_checks 148 static sigset_t check_signal_done; 149 static bool check_signals = true; 2101 return bsd_mprotect(addr, size, PROT_NONE); 2102 } 2103 2104 bool os::unguard_memory(char* addr, size_t size) { 2105 return bsd_mprotect(addr, size, PROT_READ|PROT_WRITE); 2106 } 2107 2108 bool os::Bsd::hugetlbfs_sanity_check(bool warn, size_t page_size) { 2109 return false; 2110 } 2111 2112 // Large page support 2113 2114 static size_t _large_page_size = 0; 2115 2116 void os::large_page_init() { 2117 } 2118 2119 2120 char* os::reserve_memory_special(size_t bytes, size_t alignment, char* req_addr, bool exec) { 2121 fatal("This code is not used or maintained."); 2122 2123 // "exec" is passed in but not used. Creating the shared image for 2124 // the code cache doesn't have an SHM_X executable permission to check. 2125 assert(UseLargePages && UseSHM, "only for SHM large pages"); 2126 2127 key_t key = IPC_PRIVATE; 2128 char *addr; 2129 2130 bool warn_on_failure = UseLargePages && 2131 (!FLAG_IS_DEFAULT(UseLargePages) || 2132 !FLAG_IS_DEFAULT(LargePageSizeInBytes)); 2133 2134 // Create a large shared memory region to attach to based on size. 2135 // Currently, size is the total size of the heap 2136 int shmid = shmget(key, bytes, IPC_CREAT|SHM_R|SHM_W); 2137 if (shmid == -1) { 2138 // Possible reasons for shmget failure: 2139 // 1. shmmax is too small for Java heap. 2140 // > check shmmax value: cat /proc/sys/kernel/shmmax 2141 // > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax 2142 // 2. not enough large page memory. 2143 // > check available large pages: cat /proc/meminfo 2144 // > increase amount of large pages: 2145 // echo new_value > /proc/sys/vm/nr_hugepages 2146 // Note 1: different Bsd may use different name for this property, 2147 // e.g. on Redhat AS-3 it is "hugetlb_pool". 2148 // Note 2: it's possible there's enough physical memory available but 2149 // they are so fragmented after a long run that they can't 2150 // coalesce into large pages. Try to reserve large pages when 2151 // the system is still "fresh". 2152 if (warn_on_failure) { 2153 warning("Failed to reserve shared memory (errno = %d).", errno); 2154 } 2155 return NULL; 2156 } 2157 2158 // attach to the region 2159 addr = (char*)shmat(shmid, req_addr, 0); 2160 int err = errno; 2161 2162 // Remove shmid. If shmat() is successful, the actual shared memory segment 2163 // will be deleted when it's detached by shmdt() or when the process 2164 // terminates. If shmat() is not successful this will remove the shared 2165 // segment immediately. 2166 shmctl(shmid, IPC_RMID, NULL); 2167 2168 if ((intptr_t)addr == -1) { 2169 if (warn_on_failure) { 2170 warning("Failed to attach shared memory (errno = %d).", err); 2171 } 2172 return NULL; 2173 } 2174 2175 // The memory is committed 2176 MemTracker::record_virtual_memory_reserve_and_commit((address)addr, bytes, CALLER_PC); 2177 2178 return addr; 2179 } 2180 2181 bool os::release_memory_special(char* base, size_t bytes) { 2182 if (MemTracker::tracking_level() > NMT_minimal) { 2183 Tracker tkr(Tracker::release); 2184 // detaching the SHM segment will also delete it, see reserve_memory_special() 2185 int rslt = shmdt(base); 2186 if (rslt == 0) { 2187 tkr.record((address)base, bytes); 2188 return true; 2189 } else { 2190 return false; 2191 } 2192 } else { 2193 return shmdt(base) == 0; 2194 } 2195 } 2196 2197 size_t os::large_page_size() { 2198 return _large_page_size; 2199 } 2200 2201 // HugeTLBFS allows application to commit large page memory on demand; 2202 // with SysV SHM the entire memory region must be allocated as shared 2203 // memory. 2204 bool os::can_commit_large_page_memory() { 2205 return UseHugeTLBFS; 2206 } 2207 2208 bool os::can_execute_large_page_memory() { 2209 return UseHugeTLBFS; 2210 } 2211 2212 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr, int file_desc) { 2213 assert(file_desc >= 0, "file_desc is not valid"); 2214 char* result = pd_attempt_reserve_memory_at(bytes, requested_addr); 2215 if (result != NULL) { 2216 if (replace_existing_mapping_with_file_mapping(result, bytes, file_desc) == NULL) { 2217 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory")); 2218 } 2219 } 2220 return result; 2221 } 2222 2223 // Reserve memory at an arbitrary address, only if that area is 2224 // available (and not reserved for something else). 2225 2226 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) { 2227 const int max_tries = 10; 2228 char* base[max_tries]; 2229 size_t size[max_tries]; | 53 #include "runtime/osThread.hpp" 54 #include "runtime/perfMemory.hpp" 55 #include "runtime/semaphore.hpp" 56 #include "runtime/sharedRuntime.hpp" 57 #include "runtime/statSampler.hpp" 58 #include "runtime/stubRoutines.hpp" 59 #include "runtime/thread.inline.hpp" 60 #include "runtime/threadCritical.hpp" 61 #include "runtime/timer.hpp" 62 #include "services/attachListener.hpp" 63 #include "services/memTracker.hpp" 64 #include "services/runtimeService.hpp" 65 #include "utilities/align.hpp" 66 #include "utilities/decoder.hpp" 67 #include "utilities/defaultStream.hpp" 68 #include "utilities/events.hpp" 69 #include "utilities/growableArray.hpp" 70 #include "utilities/vmError.hpp" 71 72 // put OS-includes here 73 # include <dlfcn.h> 74 # include <errno.h> 75 # include <fcntl.h> 76 # include <inttypes.h> 77 # include <poll.h> 78 # include <pthread.h> 79 # include <pwd.h> 80 # include <signal.h> 81 # include <stdint.h> 82 # include <stdio.h> 83 # include <string.h> 84 # include <sys/ioctl.h> 85 # include <sys/mman.h> 86 # include <sys/param.h> 87 # include <sys/resource.h> 88 # include <sys/socket.h> 89 # include <sys/stat.h> 90 # include <sys/syscall.h> 91 # include <sys/sysctl.h> 92 # include <sys/time.h> 93 # include <sys/times.h> 94 # include <sys/types.h> 95 # include <sys/wait.h> 96 # include <time.h> 97 # include <unistd.h> 98 99 #if defined(__FreeBSD__) || defined(__NetBSD__) 100 #include <elf.h> 101 #endif 102 103 #ifdef __APPLE__ 104 #include <mach-o/dyld.h> 105 #endif 106 107 #ifndef MAP_ANONYMOUS 108 #define MAP_ANONYMOUS MAP_ANON 109 #endif 110 111 #define MAX_PATH (2 * K) 112 113 // for timer info max values which include all bits 114 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF) 115 116 //////////////////////////////////////////////////////////////////////////////// 117 // global variables 118 julong os::Bsd::_physical_memory = 0; 119 120 #ifdef __APPLE__ 121 mach_timebase_info_data_t os::Bsd::_timebase_info = {0, 0}; 122 volatile uint64_t os::Bsd::_max_abstime = 0; 123 #else 124 int (*os::Bsd::_clock_gettime)(clockid_t, struct timespec *) = NULL; 125 #endif 126 pthread_t os::Bsd::_main_thread; 127 int os::Bsd::_page_size = -1; 128 129 static jlong initial_time_count=0; 130 131 static int clock_tics_per_sec = 100; 132 133 // For diagnostics to print a message once. see run_periodic_checks 134 static sigset_t check_signal_done; 135 static bool check_signals = true; 2087 return bsd_mprotect(addr, size, PROT_NONE); 2088 } 2089 2090 bool os::unguard_memory(char* addr, size_t size) { 2091 return bsd_mprotect(addr, size, PROT_READ|PROT_WRITE); 2092 } 2093 2094 bool os::Bsd::hugetlbfs_sanity_check(bool warn, size_t page_size) { 2095 return false; 2096 } 2097 2098 // Large page support 2099 2100 static size_t _large_page_size = 0; 2101 2102 void os::large_page_init() { 2103 } 2104 2105 2106 char* os::reserve_memory_special(size_t bytes, size_t alignment, char* req_addr, bool exec) { 2107 fatal("os::reserve_memory_special should not be called on BSD."); 2108 return NULL; 2109 } 2110 2111 bool os::release_memory_special(char* base, size_t bytes) { 2112 fatal("os::release_memory_special should not be called on BSD."); 2113 return false; 2114 } 2115 2116 size_t os::large_page_size() { 2117 return _large_page_size; 2118 } 2119 2120 bool os::can_commit_large_page_memory() { 2121 // Does not matter, we do not support huge pages. 2122 return false; 2123 } 2124 2125 bool os::can_execute_large_page_memory() { 2126 // Does not matter, we do not support huge pages. 2127 return false; 2128 } 2129 2130 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr, int file_desc) { 2131 assert(file_desc >= 0, "file_desc is not valid"); 2132 char* result = pd_attempt_reserve_memory_at(bytes, requested_addr); 2133 if (result != NULL) { 2134 if (replace_existing_mapping_with_file_mapping(result, bytes, file_desc) == NULL) { 2135 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory")); 2136 } 2137 } 2138 return result; 2139 } 2140 2141 // Reserve memory at an arbitrary address, only if that area is 2142 // available (and not reserved for something else). 2143 2144 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) { 2145 const int max_tries = 10; 2146 char* base[max_tries]; 2147 size_t size[max_tries]; |