1 /*
  2  * Copyright (c) 1999, 2023, 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 #include "runtime/os.hpp"
 29 
 30 // os::Linux defines the interface to Linux operating systems
 31 
 32 class os::Linux {
 33   friend class CgroupSubsystem;
 34   friend class os;
 35   friend class OSContainer;
 36   friend class TestReserveMemorySpecial;
 37 
 38   static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
 39   static int (*_pthread_setname_np)(pthread_t, const char*);
 40 
 41   static address   _initial_thread_stack_bottom;
 42   static uintptr_t _initial_thread_stack_size;
 43 
 44   static const char *_libc_version;
 45   static const char *_libpthread_version;
 46 
 47   static bool _supports_fast_thread_cpu_time;
 48 
 49   static GrowableArray<int>* _cpu_to_node;
 50   static GrowableArray<int>* _nindex_to_node;
 51 
 52   static julong available_memory_in_container();
 53 
 54  protected:
 55 
 56   static julong _physical_memory;
 57   static pthread_t _main_thread;
 58 
 59   static julong available_memory();
 60   static julong free_memory();
 61 
 62   static int active_processor_count();
 63 
 64   static void initialize_system_info();
 65 
 66   static int commit_memory_impl(char* addr, size_t bytes, bool exec);
 67   static int commit_memory_impl(char* addr, size_t bytes,
 68                                 size_t alignment_hint, bool exec);
 69 
 70   static void set_libc_version(const char *s)       { _libc_version = s; }
 71   static void set_libpthread_version(const char *s) { _libpthread_version = s; }
 72 
 73   static void rebuild_cpu_to_node_map();
 74   static void rebuild_nindex_to_node_map();
 75   static GrowableArray<int>* cpu_to_node()    { return _cpu_to_node; }
 76   static GrowableArray<int>* nindex_to_node()  { return _nindex_to_node; }
 77 
 78   static bool setup_large_page_type(size_t page_size);
 79   static bool transparent_huge_pages_sanity_check(bool warn, size_t pages_size);
 80   static bool hugetlbfs_sanity_check(bool warn, size_t page_size);
 81   static bool shm_hugetlbfs_sanity_check(bool warn, size_t page_size);
 82 
 83   static int hugetlbfs_page_size_flag(size_t page_size);
 84 
 85   static char* reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec);
 86   static char* reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, size_t page_size, char* req_addr, bool exec);
 87   static bool commit_memory_special(size_t bytes, size_t page_size, char* req_addr, bool exec);
 88 
 89   static bool release_memory_special_impl(char* base, size_t bytes);
 90   static bool release_memory_special_shm(char* base, size_t bytes);
 91   static bool release_memory_special_huge_tlbfs(char* base, size_t bytes);
 92 
 93   static void print_process_memory_info(outputStream* st);
 94   static void print_system_memory_info(outputStream* st);
 95   static bool print_container_info(outputStream* st);
 96   static void print_steal_info(outputStream* st);
 97   static void print_distro_info(outputStream* st);
 98   static void print_libversion_info(outputStream* st);
 99   static void print_proc_sys_info(outputStream* st);
100   static bool print_ld_preload_file(outputStream* st);
101   static void print_uptime_info(outputStream* st);
102 
103  public:
104   struct CPUPerfTicks {
105     uint64_t used;
106     uint64_t usedKernel;
107     uint64_t total;
108     uint64_t steal;
109     bool     has_steal_ticks;
110   };
111 
112   // which_logical_cpu=-1 returns accumulated ticks for all cpus.
113   static bool get_tick_information(CPUPerfTicks* pticks, int which_logical_cpu);
114   static bool _stack_is_executable;
115   static void *dlopen_helper(const char *name, char *ebuf, int ebuflen);
116   static void *dll_load_in_vmthread(const char *name, char *ebuf, int ebuflen);
117   static const char *dll_path(void* lib);
118 
119   static void init_thread_fpu_state();
120   static int  get_fpu_control_word();
121   static void set_fpu_control_word(int fpu_control);
122   static pthread_t main_thread(void)                                { return _main_thread; }
123   // returns kernel thread id (similar to LWP id on Solaris), which can be
124   // used to access /proc
125   static pid_t gettid();
126 
127   static address   initial_thread_stack_bottom(void)                { return _initial_thread_stack_bottom; }
128   static uintptr_t initial_thread_stack_size(void)                  { return _initial_thread_stack_size; }
129 
130   static julong physical_memory() { return _physical_memory; }
131   static julong host_swap();
132 
133   static intptr_t* ucontext_get_sp(const ucontext_t* uc);
134   static intptr_t* ucontext_get_fp(const ucontext_t* uc);
135 
136   // GNU libc and libpthread version strings
137   static const char *libc_version()           { return _libc_version; }
138   static const char *libpthread_version()     { return _libpthread_version; }
139 
140   static void libpthread_init();
141   static void sched_getcpu_init();
142   static bool libnuma_init();
143   static void* libnuma_dlsym(void* handle, const char* name);
144   // libnuma v2 (libnuma_1.2) symbols
145   static void* libnuma_v2_dlsym(void* handle, const char* name);
146 
147   // Return default guard size for the specified thread type
148   static size_t default_guard_size(os::ThreadType thr_type);
149 
150   static bool adjustStackSizeForGuardPages(); // See comments in os_linux.cpp
151 
152   static void capture_initial_stack(size_t max_size);
153 
154   // Stack overflow handling
155   static bool manually_expand_stack(JavaThread * t, address addr);
156   static void expand_stack_to(address bottom);
157 
158   // fast POSIX clocks support
159   static void fast_thread_clock_init(void);
160 
161   static int pthread_getcpuclockid(pthread_t tid, clockid_t *clock_id) {
162     return _pthread_getcpuclockid ? _pthread_getcpuclockid(tid, clock_id) : -1;
163   }
164 
165   static bool supports_fast_thread_cpu_time() {
166     return _supports_fast_thread_cpu_time;
167   }
168 
169   static jlong fast_thread_cpu_time(clockid_t clockid);
170 
171   // Determine if the vmid is the parent pid for a child in a PID namespace.
172   // Return the namespace pid if so, otherwise -1.
173   static int get_namespace_pid(int vmid);
174 
175   // Output structure for query_process_memory_info() (all values in KB)
176   struct meminfo_t {
177     ssize_t vmsize;     // current virtual size
178     ssize_t vmpeak;     // peak virtual size
179     ssize_t vmrss;      // current resident set size
180     ssize_t vmhwm;      // peak resident set size
181     ssize_t vmswap;     // swapped out
182     ssize_t rssanon;    // resident set size (anonymous mappings, needs 4.5)
183     ssize_t rssfile;    // resident set size (file mappings, needs 4.5)
184     ssize_t rssshmem;   // resident set size (shared mappings, needs 4.5)
185   };
186 
187   // Attempts to query memory information about the current process and return it in the output structure.
188   // May fail (returns false) or succeed (returns true) but not all output fields are available; unavailable
189   // fields will contain -1.
190   static bool query_process_memory_info(meminfo_t* info);
191 
192   // Stack repair handling
193 
194   // none present
195 
196  private:
197   static void numa_init();
198 
199   typedef int (*sched_getcpu_func_t)(void);
200   typedef int (*numa_node_to_cpus_func_t)(int node, unsigned long *buffer, int bufferlen);
201   typedef int (*numa_node_to_cpus_v2_func_t)(int node, void *mask);
202   typedef int (*numa_max_node_func_t)(void);
203   typedef int (*numa_num_configured_nodes_func_t)(void);
204   typedef int (*numa_available_func_t)(void);
205   typedef int (*numa_tonode_memory_func_t)(void *start, size_t size, int node);
206   typedef void (*numa_interleave_memory_func_t)(void *start, size_t size, unsigned long *nodemask);
207   typedef void (*numa_interleave_memory_v2_func_t)(void *start, size_t size, struct bitmask* mask);
208   typedef struct bitmask* (*numa_get_membind_func_t)(void);
209   typedef struct bitmask* (*numa_get_interleave_mask_func_t)(void);
210   typedef long (*numa_move_pages_func_t)(int pid, unsigned long count, void **pages, const int *nodes, int *status, int flags);
211   typedef void (*numa_set_preferred_func_t)(int node);
212   typedef void (*numa_set_bind_policy_func_t)(int policy);
213   typedef int (*numa_bitmask_isbitset_func_t)(struct bitmask *bmp, unsigned int n);
214   typedef int (*numa_distance_func_t)(int node1, int node2);
215 
216   static sched_getcpu_func_t _sched_getcpu;
217   static numa_node_to_cpus_func_t _numa_node_to_cpus;
218   static numa_node_to_cpus_v2_func_t _numa_node_to_cpus_v2;
219   static numa_max_node_func_t _numa_max_node;
220   static numa_num_configured_nodes_func_t _numa_num_configured_nodes;
221   static numa_available_func_t _numa_available;
222   static numa_tonode_memory_func_t _numa_tonode_memory;
223   static numa_interleave_memory_func_t _numa_interleave_memory;
224   static numa_interleave_memory_v2_func_t _numa_interleave_memory_v2;
225   static numa_set_bind_policy_func_t _numa_set_bind_policy;
226   static numa_bitmask_isbitset_func_t _numa_bitmask_isbitset;
227   static numa_distance_func_t _numa_distance;
228   static numa_get_membind_func_t _numa_get_membind;
229   static numa_get_interleave_mask_func_t _numa_get_interleave_mask;
230   static numa_move_pages_func_t _numa_move_pages;
231   static numa_set_preferred_func_t _numa_set_preferred;
232   static unsigned long* _numa_all_nodes;
233   static struct bitmask* _numa_all_nodes_ptr;
234   static struct bitmask* _numa_nodes_ptr;
235   static struct bitmask* _numa_interleave_bitmask;
236   static struct bitmask* _numa_membind_bitmask;
237 
238   static void set_sched_getcpu(sched_getcpu_func_t func) { _sched_getcpu = func; }
239   static void set_numa_node_to_cpus(numa_node_to_cpus_func_t func) { _numa_node_to_cpus = func; }
240   static void set_numa_node_to_cpus_v2(numa_node_to_cpus_v2_func_t func) { _numa_node_to_cpus_v2 = func; }
241   static void set_numa_max_node(numa_max_node_func_t func) { _numa_max_node = func; }
242   static void set_numa_num_configured_nodes(numa_num_configured_nodes_func_t func) { _numa_num_configured_nodes = func; }
243   static void set_numa_available(numa_available_func_t func) { _numa_available = func; }
244   static void set_numa_tonode_memory(numa_tonode_memory_func_t func) { _numa_tonode_memory = func; }
245   static void set_numa_interleave_memory(numa_interleave_memory_func_t func) { _numa_interleave_memory = func; }
246   static void set_numa_interleave_memory_v2(numa_interleave_memory_v2_func_t func) { _numa_interleave_memory_v2 = func; }
247   static void set_numa_set_bind_policy(numa_set_bind_policy_func_t func) { _numa_set_bind_policy = func; }
248   static void set_numa_bitmask_isbitset(numa_bitmask_isbitset_func_t func) { _numa_bitmask_isbitset = func; }
249   static void set_numa_distance(numa_distance_func_t func) { _numa_distance = func; }
250   static void set_numa_get_membind(numa_get_membind_func_t func) { _numa_get_membind = func; }
251   static void set_numa_get_interleave_mask(numa_get_interleave_mask_func_t func) { _numa_get_interleave_mask = func; }
252   static void set_numa_move_pages(numa_move_pages_func_t func) { _numa_move_pages = func; }
253   static void set_numa_set_preferred(numa_set_preferred_func_t func) { _numa_set_preferred = func; }
254   static void set_numa_all_nodes(unsigned long* ptr) { _numa_all_nodes = ptr; }
255   static void set_numa_all_nodes_ptr(struct bitmask **ptr) { _numa_all_nodes_ptr = (ptr == nullptr ? nullptr : *ptr); }
256   static void set_numa_nodes_ptr(struct bitmask **ptr) { _numa_nodes_ptr = (ptr == nullptr ? nullptr : *ptr); }
257   static void set_numa_interleave_bitmask(struct bitmask* ptr)     { _numa_interleave_bitmask = ptr ;   }
258   static void set_numa_membind_bitmask(struct bitmask* ptr)        { _numa_membind_bitmask = ptr ;      }
259   static int sched_getcpu_syscall(void);
260 
261   enum NumaAllocationPolicy{
262     NotInitialized,
263     Membind,
264     Interleave
265   };
266   static NumaAllocationPolicy _current_numa_policy;
267 
268  public:
269   static int sched_getcpu()  { return _sched_getcpu != nullptr ? _sched_getcpu() : -1; }
270   static int numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen);
271   static int numa_max_node() { return _numa_max_node != nullptr ? _numa_max_node() : -1; }
272   static int numa_num_configured_nodes() {
273     return _numa_num_configured_nodes != nullptr ? _numa_num_configured_nodes() : -1;
274   }
275   static int numa_available() { return _numa_available != nullptr ? _numa_available() : -1; }
276   static int numa_tonode_memory(void *start, size_t size, int node) {
277     return _numa_tonode_memory != nullptr ? _numa_tonode_memory(start, size, node) : -1;
278   }
279 
280   static bool is_running_in_interleave_mode() {
281     return _current_numa_policy == Interleave;
282   }
283 
284   static void set_configured_numa_policy(NumaAllocationPolicy numa_policy) {
285     _current_numa_policy = numa_policy;
286   }
287 
288   static NumaAllocationPolicy identify_numa_policy() {
289     for (int node = 0; node <= Linux::numa_max_node(); node++) {
290       if (Linux::_numa_bitmask_isbitset(Linux::_numa_interleave_bitmask, node)) {
291         return Interleave;
292       }
293     }
294     return Membind;
295   }
296 
297   static void numa_interleave_memory(void *start, size_t size) {
298     // Prefer v2 API
299     if (_numa_interleave_memory_v2 != nullptr) {
300       if (is_running_in_interleave_mode()) {
301         _numa_interleave_memory_v2(start, size, _numa_interleave_bitmask);
302       } else if (_numa_membind_bitmask != nullptr) {
303         _numa_interleave_memory_v2(start, size, _numa_membind_bitmask);
304       }
305     } else if (_numa_interleave_memory != nullptr) {
306       _numa_interleave_memory(start, size, _numa_all_nodes);
307     }
308   }
309   static void numa_set_preferred(int node) {
310     if (_numa_set_preferred != nullptr) {
311       _numa_set_preferred(node);
312     }
313   }
314   static void numa_set_bind_policy(int policy) {
315     if (_numa_set_bind_policy != nullptr) {
316       _numa_set_bind_policy(policy);
317     }
318   }
319   static int numa_distance(int node1, int node2) {
320     return _numa_distance != nullptr ? _numa_distance(node1, node2) : -1;
321   }
322   static long numa_move_pages(int pid, unsigned long count, void **pages, const int *nodes, int *status, int flags) {
323     return _numa_move_pages != nullptr ? _numa_move_pages(pid, count, pages, nodes, status, flags) : -1;
324   }
325   static int get_node_by_cpu(int cpu_id);
326   static int get_existing_num_nodes();
327   // Check if numa node is configured (non-zero memory node).
328   static bool is_node_in_configured_nodes(unsigned int n) {
329     if (_numa_bitmask_isbitset != nullptr && _numa_all_nodes_ptr != nullptr) {
330       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
331     } else
332       return false;
333   }
334   // Check if numa node exists in the system (including zero memory nodes).
335   static bool is_node_in_existing_nodes(unsigned int n) {
336     if (_numa_bitmask_isbitset != nullptr && _numa_nodes_ptr != nullptr) {
337       return _numa_bitmask_isbitset(_numa_nodes_ptr, n);
338     } else if (_numa_bitmask_isbitset != nullptr && _numa_all_nodes_ptr != nullptr) {
339       // Not all libnuma API v2 implement numa_nodes_ptr, so it's not possible
340       // to trust the API version for checking its absence. On the other hand,
341       // numa_nodes_ptr found in libnuma 2.0.9 and above is the only way to get
342       // a complete view of all numa nodes in the system, hence numa_nodes_ptr
343       // is used to handle CPU and nodes on architectures (like PowerPC) where
344       // there can exist nodes with CPUs but no memory or vice-versa and the
345       // nodes may be non-contiguous. For most of the architectures, like
346       // x86_64, numa_node_ptr presents the same node set as found in
347       // numa_all_nodes_ptr so it's possible to use numa_all_nodes_ptr as a
348       // substitute.
349       return _numa_bitmask_isbitset(_numa_all_nodes_ptr, n);
350     } else
351       return false;
352   }
353   // Check if node is in bound node set.
354   static bool is_node_in_bound_nodes(int node) {
355     if (_numa_bitmask_isbitset != nullptr) {
356       if (is_running_in_interleave_mode()) {
357         return _numa_bitmask_isbitset(_numa_interleave_bitmask, node);
358       } else {
359         return _numa_membind_bitmask != nullptr ? _numa_bitmask_isbitset(_numa_membind_bitmask, node) : false;
360       }
361     }
362     return false;
363   }
364   // Check if bound to only one numa node.
365   // Returns true if bound to a single numa node, otherwise returns false.
366   static bool is_bound_to_single_node() {
367     int nodes = 0;
368     unsigned int node = 0;
369     unsigned int highest_node_number = 0;
370 
371     if (_numa_membind_bitmask != nullptr && _numa_max_node != nullptr && _numa_bitmask_isbitset != nullptr) {
372       highest_node_number = _numa_max_node();
373     } else {
374       return false;
375     }
376 
377     for (node = 0; node <= highest_node_number; node++) {
378       if (_numa_bitmask_isbitset(_numa_membind_bitmask, node)) {
379         nodes++;
380       }
381     }
382 
383     if (nodes == 1) {
384       return true;
385     } else {
386       return false;
387     }
388   }
389 
390   static const GrowableArray<int>* numa_nindex_to_node() {
391     return _nindex_to_node;
392   }
393 
394   static void* resolve_function_descriptor(void* p);
395 
396 #ifdef __GLIBC__
397   // os::Linux::get_mallinfo() hides the complexity of dealing with mallinfo() or
398   // mallinfo2() from the user. Use this function instead of raw mallinfo/mallinfo2()
399   // to keep the JVM runtime-compatible with different glibc versions.
400   //
401   // mallinfo2() was added with glibc (>2.32). Legacy mallinfo() was deprecated with
402   // 2.33 and may vanish in future glibcs. So we may have both or either one of
403   // them.
404   //
405   // mallinfo2() is functionally equivalent to legacy mallinfo but returns sizes as
406   // 64-bit on 64-bit platforms. Legacy mallinfo uses 32-bit fields. However, legacy
407   // mallinfo is still perfectly fine to use if we know the sizes cannot have wrapped.
408   // For example, if the process virtual size does not exceed 4G, we cannot have
409   // malloc'ed more than 4G, so the results from legacy mallinfo() can still be used.
410   //
411   // os::Linux::get_mallinfo() will always prefer mallinfo2() if found, but will fall back
412   // to legacy mallinfo() if only that is available. In that case, it will return true
413   // in *might_have_wrapped.
414   struct glibc_mallinfo {
415     size_t arena;
416     size_t ordblks;
417     size_t smblks;
418     size_t hblks;
419     size_t hblkhd;
420     size_t usmblks;
421     size_t fsmblks;
422     size_t uordblks;
423     size_t fordblks;
424     size_t keepcost;
425   };
426   static void get_mallinfo(glibc_mallinfo* out, bool* might_have_wrapped);
427 
428   // Calls out to GNU extension malloc_info if available
429   // otherwise does nothing and returns -2.
430   static int malloc_info(FILE* stream);
431 #endif // GLIBC
432 };
433 
434 #endif // OS_LINUX_OS_LINUX_HPP