1 /* 2 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef OS_LINUX_OS_LINUX_HPP 26 #define OS_LINUX_OS_LINUX_HPP 27 28 // Linux_OS defines the interface to Linux operating systems 29 30 // Information about the protection of the page at address '0' on this os. 31 static bool zero_page_read_protected() { return true; } 32 33 class Linux { 34 friend class os; 35 friend class OSContainer; 36 friend class TestReserveMemorySpecial; 37 38 static bool libjsig_is_loaded; // libjsig that interposes sigaction(), 39 // __sigaction(), signal() is loaded 40 static struct sigaction *(*get_signal_action)(int); 41 42 static void check_signal_handler(int sig); 43 44 static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *); 45 static int (*_pthread_setname_np)(pthread_t, const char*); 46 47 static address _initial_thread_stack_bottom; 48 static uintptr_t _initial_thread_stack_size; 49 50 static const char *_glibc_version; 51 static const char *_libpthread_version; 52 53 static bool _supports_fast_thread_cpu_time; 54 55 static GrowableArray<int>* _cpu_to_node; 56 static GrowableArray<int>* _nindex_to_node; 57 58 protected: 59 60 static julong _physical_memory; 61 static pthread_t _main_thread; 62 static int _page_size; 63 64 static julong available_memory(); 65 static julong physical_memory() { return _physical_memory; } 66 static void set_physical_memory(julong phys_mem) { _physical_memory = phys_mem; } 67 static int active_processor_count(); 68 69 static void initialize_system_info(); 70 71 static int commit_memory_impl(char* addr, size_t bytes, bool exec); 72 static int commit_memory_impl(char* addr, size_t bytes, 73 size_t alignment_hint, bool exec); 74 75 static void set_glibc_version(const char *s) { _glibc_version = s; } 76 static void set_libpthread_version(const char *s) { _libpthread_version = s; } 77 78 static void rebuild_cpu_to_node_map(); 79 static void rebuild_nindex_to_node_map(); 80 static GrowableArray<int>* cpu_to_node() { return _cpu_to_node; } 81 static GrowableArray<int>* nindex_to_node() { return _nindex_to_node; } 82 83 static size_t find_large_page_size(); 84 static size_t setup_large_page_size(); 85 86 static bool setup_large_page_type(size_t page_size); 87 static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size); 88 static bool hugetlbfs_sanity_check(bool warn, size_t page_size); 89 90 static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec); 91 static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec); 92 static char* reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec); 93 static char* reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec); 94 95 static bool release_memory_special_impl(char* base, size_t bytes); 96 static bool release_memory_special_shm(char* base, size_t bytes); 97 static bool release_memory_special_huge_tlbfs(char* base, size_t bytes); 98 99 static void print_full_memory_info(outputStream* st); 100 static void print_container_info(outputStream* st); 101 static void print_steal_info(outputStream* st); 102 static void print_distro_info(outputStream* st); 103 static void print_libversion_info(outputStream* st); 104 static void print_proc_sys_info(outputStream* st); 105 static void print_ld_preload_file(outputStream* st); 106 static void print_uptime_info(outputStream* st); 107 108 public: 109 struct CPUPerfTicks { 110 uint64_t used; 111 uint64_t usedKernel; 112 uint64_t total; 113 uint64_t steal; 114 bool has_steal_ticks; 115 }; 116 117 // which_logical_cpu=-1 returns accumulated ticks for all cpus. 118 static bool get_tick_information(CPUPerfTicks* pticks, int which_logical_cpu); 119 static bool _stack_is_executable; 120 static void *dlopen_helper(const char *name, char *ebuf, int ebuflen); 121 static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen); 122 123 static void init_thread_fpu_state(); 124 static int get_fpu_control_word(); 125 static void set_fpu_control_word(int fpu_control); 126 static pthread_t main_thread(void) { return _main_thread; } 127 // returns kernel thread id (similar to LWP id on Solaris), which can be 128 // used to access /proc 129 static pid_t gettid(); 130 static void hotspot_sigmask(Thread* thread); 131 132 static address initial_thread_stack_bottom(void) { return _initial_thread_stack_bottom; } 133 static uintptr_t initial_thread_stack_size(void) { return _initial_thread_stack_size; } 134 135 static int page_size(void) { return _page_size; } 136 static void set_page_size(int val) { _page_size = val; } 137 138 static address ucontext_get_pc(const ucontext_t* uc); 139 static void ucontext_set_pc(ucontext_t* uc, address pc); 140 static intptr_t* ucontext_get_sp(const ucontext_t* uc); 141 static intptr_t* ucontext_get_fp(const ucontext_t* uc); 142 143 // For Analyzer Forte AsyncGetCallTrace profiling support: 144 // 145 // This interface should be declared in os_linux_i486.hpp, but 146 // that file provides extensions to the os class and not the 147 // Linux class. 148 static ExtendedPC fetch_frame_from_ucontext(Thread* thread, const ucontext_t* uc, 149 intptr_t** ret_sp, intptr_t** ret_fp); 150 151 static bool get_frame_at_stack_banging_point(JavaThread* thread, ucontext_t* uc, frame* fr); 152 153 // This boolean allows users to forward their own non-matching signals 154 // to JVM_handle_linux_signal, harmlessly. 155 static bool signal_handlers_are_installed; 156 157 static int get_our_sigflags(int); 158 static void set_our_sigflags(int, int); 159 static void signal_sets_init(); 160 static void install_signal_handlers(); 161 static void set_signal_handler(int, bool); 162 163 static sigset_t* unblocked_signals(); 164 static sigset_t* vm_signals(); 165 166 // For signal-chaining 167 static struct sigaction *get_chained_signal_action(int sig); 168 static bool chained_handler(int sig, siginfo_t* siginfo, void* context); 169 170 // GNU libc and libpthread version strings 171 static const char *glibc_version() { return _glibc_version; } 172 static const char *libpthread_version() { return _libpthread_version; } 173 174 static void libpthread_init(); 175 static void sched_getcpu_init(); 176 static bool libnuma_init(); 177 static void* libnuma_dlsym(void* handle, const char* name); 178 // libnuma v2 (libnuma_1.2) symbols 179 static void* libnuma_v2_dlsym(void* handle, const char* name); 180 181 // Return default guard size for the specified thread type 182 static size_t default_guard_size(os::ThreadType thr_type); 183 184 static void capture_initial_stack(size_t max_size); 185 186 // Stack overflow handling 187 static bool manually_expand_stack(JavaThread * t, address addr); 188 189 // fast POSIX clocks support 190 static void fast_thread_clock_init(void); 191 192 static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) { 193 return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1; 194 } 195 196 static bool supports_fast_thread_cpu_time() { 197 return _supports_fast_thread_cpu_time; 198 } 199 200 static jlong fast_thread_cpu_time(clockid_t clockid); 201 202 // Stack repair handling 203 204 // none present 205 206 private: 207 static void numa_init(); 208 static void expand_stack_to(address bottom); 209 210 typedef int (*sched_getcpu_func_t)(void); 211 typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen); 212 typedef int (*numa_max_node_func_t)(void); 213 typedef int (*numa_num_configured_nodes_func_t)(void); 214 typedef int (*numa_available_func_t)(void); 215 typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node); 216 typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask); 217 typedef void (*numa_interleave_memory_v2_func_t)(void *start, size_t size, struct bitmask* mask); 218 typedef struct bitmask* (*numa_get_membind_func_t)(void); 219 typedef struct bitmask* (*numa_get_interleave_mask_func_t)(void); 220 typedef long (*numa_move_pages_func_t)(int pid, unsigned long count, void **pages, const int *nodes, int *status, int flags); 221 222 typedef void (*numa_set_bind_policy_func_t)(int policy); 223 typedef int (*numa_bitmask_isbitset_func_t)(struct bitmask *bmp, unsigned int n); 224 typedef int (*numa_distance_func_t)(int node1, int node2); 225 226 static sched_getcpu_func_t _sched_getcpu; 227 static numa_node_to_cpus_func_t _numa_node_to_cpus; 228 static numa_max_node_func_t _numa_max_node; 229 static numa_num_configured_nodes_func_t _numa_num_configured_nodes; 230 static numa_available_func_t _numa_available; 231 static numa_tonode_memory_func_t _numa_tonode_memory; 232 static numa_interleave_memory_func_t _numa_interleave_memory; 233 static numa_interleave_memory_v2_func_t _numa_interleave_memory_v2; 234 static numa_set_bind_policy_func_t _numa_set_bind_policy; 235 static numa_bitmask_isbitset_func_t _numa_bitmask_isbitset; 236 static numa_distance_func_t _numa_distance; 237 static numa_get_membind_func_t _numa_get_membind; 238 static numa_get_interleave_mask_func_t _numa_get_interleave_mask; 239 static numa_move_pages_func_t _numa_move_pages; 240 static unsigned long* _numa_all_nodes; 241 static struct bitmask* _numa_all_nodes_ptr; 242 static struct bitmask* _numa_nodes_ptr; 243 static struct bitmask* _numa_interleave_bitmask; 244 static struct bitmask* _numa_membind_bitmask; 245 246 static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; } 247 static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; } 248 static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; } 249 static void set_numa_num_configured_nodes(numa_num_configured_nodes_func_t func) { _numa_num_configured_nodes = func; } 250 static void set_numa_available(numa_available_func_t func) { _numa_available = func; } 251 static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; } 252 static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; } 253 static void set_numa_interleave_memory_v2(numa_interleave_memory_v2_func_t func) { _numa_interleave_memory_v2 = func; } 254 static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; } 255 static void set_numa_bitmask_isbitset(numa_bitmask_isbitset_func_t func) { _numa_bitmask_isbitset = func; } 256 static void set_numa_distance(numa_distance_func_t func) { _numa_distance = func; } 257 static void set_numa_get_membind(numa_get_membind_func_t func) { _numa_get_membind = func; } 258 static void set_numa_get_interleave_mask(numa_get_interleave_mask_func_t func) { _numa_get_interleave_mask = func; } 259 static void set_numa_move_pages(numa_move_pages_func_t func) { _numa_move_pages = func; } 260 static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; } 261 static void set_numa_all_nodes_ptr(struct bitmask **ptr) { _numa_all_nodes_ptr = (ptr == NULL ? NULL : *ptr); } 262 static void set_numa_nodes_ptr(struct bitmask **ptr) { _numa_nodes_ptr = (ptr == NULL ? NULL : *ptr); } 263 static void set_numa_interleave_bitmask(struct bitmask* ptr) { _numa_interleave_bitmask = ptr ; } 264 static void set_numa_membind_bitmask(struct bitmask* ptr) { _numa_membind_bitmask = ptr ; } 265 static int sched_getcpu_syscall(void); 266 267 enum NumaAllocationPolicy{ 268 NotInitialized, 269 Membind, 270 Interleave 271 }; 272 static NumaAllocationPolicy _current_numa_policy; 273 274 public: 275 static int sched_getcpu() { return _sched_getcpu != NULL ? _sched_getcpu() : -1; } 276 static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) { 277 return _numa_node_to_cpus != NULL ? _numa_node_to_cpus(node, buffer, bufferlen) : -1; 278 } 279 static int numa_max_node() { return _numa_max_node != NULL ? _numa_max_node() : -1; } 280 static int numa_num_configured_nodes() { 281 return _numa_num_configured_nodes != NULL ? _numa_num_configured_nodes() : -1; 282 } 283 static int numa_available() { return _numa_available != NULL ? _numa_available() : -1; } 284 static int numa_tonode_memory(void *start, size_t size, int node) { 285 return _numa_tonode_memory != NULL ? _numa_tonode_memory(start, size, node) : -1; 286 } 287 288 static bool is_running_in_interleave_mode() { 289 return _current_numa_policy == Interleave; 290 } 291 292 static void set_configured_numa_policy(NumaAllocationPolicy numa_policy) { 293 _current_numa_policy = numa_policy; 294 } 295 296 static NumaAllocationPolicy identify_numa_policy() { 297 for (int node = 0; node <= Linux::numa_max_node(); node++) { 298 if (Linux::_numa_bitmask_isbitset(Linux::_numa_interleave_bitmask, node)) { 299 return Interleave; 300 } 301 } 302 return Membind; 303 } 304 305 static void numa_interleave_memory(void *start, size_t size) { 306 // Prefer v2 API 307 if (_numa_interleave_memory_v2 != NULL) { 308 if (is_running_in_interleave_mode()) { 309 _numa_interleave_memory_v2(start, size, _numa_interleave_bitmask); 310 } else if (_numa_membind_bitmask != NULL) { 311 _numa_interleave_memory_v2(start, size, _numa_membind_bitmask); 312 } 313 } else if (_numa_interleave_memory != NULL) { 314 _numa_interleave_memory(start, size, _numa_all_nodes); 315 } 316 } 317 static void numa_set_bind_policy(int policy) { 318 if (_numa_set_bind_policy != NULL) { 319 _numa_set_bind_policy(policy); 320 } 321 } 322 static int numa_distance(int node1, int node2) { 323 return _numa_distance != NULL ? _numa_distance(node1, node2) : -1; 324 } 325 static long numa_move_pages(int pid, unsigned long count, void **pages, const int *nodes, int *status, int flags) { 326 return _numa_move_pages != NULL ? _numa_move_pages(pid, count, pages, nodes, status, flags) : -1; 327 } 328 static int get_node_by_cpu(int cpu_id); 329 static int get_existing_num_nodes(); 330 // Check if numa node is configured (non-zero memory node). 331 static bool is_node_in_configured_nodes(unsigned int n) { 332 if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) { 333 return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n); 334 } else 335 return false; 336 } 337 // Check if numa node exists in the system (including zero memory nodes). 338 static bool is_node_in_existing_nodes(unsigned int n) { 339 if (_numa_bitmask_isbitset != NULL && _numa_nodes_ptr != NULL) { 340 return _numa_bitmask_isbitset(_numa_nodes_ptr, n); 341 } else if (_numa_bitmask_isbitset != NULL && _numa_all_nodes_ptr != NULL) { 342 // Not all libnuma API v2 implement numa_nodes_ptr, so it's not possible 343 // to trust the API version for checking its absence. On the other hand, 344 // numa_nodes_ptr found in libnuma 2.0.9 and above is the only way to get 345 // a complete view of all numa nodes in the system, hence numa_nodes_ptr 346 // is used to handle CPU and nodes on architectures (like PowerPC) where 347 // there can exist nodes with CPUs but no memory or vice-versa and the 348 // nodes may be non-contiguous. For most of the architectures, like 349 // x86_64, numa_node_ptr presents the same node set as found in 350 // numa_all_nodes_ptr so it's possible to use numa_all_nodes_ptr as a 351 // substitute. 352 return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n); 353 } else 354 return false; 355 } 356 // Check if node is in bound node set. 357 static bool is_node_in_bound_nodes(int node) { 358 if (_numa_bitmask_isbitset != NULL) { 359 if (is_running_in_interleave_mode()) { 360 return _numa_bitmask_isbitset(_numa_interleave_bitmask, node); 361 } else { 362 return _numa_membind_bitmask != NULL ? _numa_bitmask_isbitset(_numa_membind_bitmask, node) : false; 363 } 364 } 365 return false; 366 } 367 // Check if bound to only one numa node. 368 // Returns true if bound to a single numa node, otherwise returns false. 369 static bool is_bound_to_single_node() { 370 int nodes = 0; 371 struct bitmask* bmp = NULL; 372 unsigned int node = 0; 373 unsigned int highest_node_number = 0; 374 375 if (_numa_get_membind != NULL && _numa_max_node != NULL && _numa_bitmask_isbitset != NULL) { 376 bmp = _numa_get_membind(); 377 highest_node_number = _numa_max_node(); 378 } else { 379 return false; 380 } 381 382 for (node = 0; node <= highest_node_number; node++) { 383 if (_numa_bitmask_isbitset(bmp, node)) { 384 nodes++; 385 } 386 } 387 388 if (nodes == 1) { 389 return true; 390 } else { 391 return false; 392 } 393 } 394 }; 395 396 #endif // OS_LINUX_OS_LINUX_HPP