Bug Summary

File:jdk/src/hotspot/os/linux/os_linux.cpp
Warning:line 5227, column 15
Value stored to 'ret' during its initialization is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name os_linux.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mthread-model posix -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/libjvm/objs/precompiled -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D _GNU_SOURCE -D _REENTRANT -D LIBC=gnu -D LINUX -D VM_LITTLE_ENDIAN -D _LP64=1 -D ASSERT -D CHECK_UNHANDLED_OOPS -D TARGET_ARCH_x86 -D INCLUDE_SUFFIX_OS=_linux -D INCLUDE_SUFFIX_CPU=_x86 -D INCLUDE_SUFFIX_COMPILER=_gcc -D TARGET_COMPILER_gcc -D AMD64 -D HOTSPOT_LIB_ARCH="amd64" -D COMPILER1 -D COMPILER2 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc/adfiles -I /home/daniel/Projects/java/jdk/src/hotspot/share -I /home/daniel/Projects/java/jdk/src/hotspot/os/linux -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix -I /home/daniel/Projects/java/jdk/src/hotspot/cpu/x86 -I /home/daniel/Projects/java/jdk/src/hotspot/os_cpu/linux_x86 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc -I /home/daniel/Projects/java/jdk/src/hotspot/share/precompiled -I /home/daniel/Projects/java/jdk/src/hotspot/share/include -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix/include -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/support/modules_include/java.base/linux -I /home/daniel/Projects/java/jdk/src/java.base/share/native/libjimage -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc/adfiles -I /home/daniel/Projects/java/jdk/src/hotspot/share -I /home/daniel/Projects/java/jdk/src/hotspot/os/linux -I /home/daniel/Projects/java/jdk/src/hotspot/os/posix -I /home/daniel/Projects/java/jdk/src/hotspot/cpu/x86 -I /home/daniel/Projects/java/jdk/src/hotspot/os_cpu/linux_x86 -I /home/daniel/Projects/java/jdk/build/linux-x86_64-server-fastdebug/hotspot/variant-server/gensrc -D _FORTIFY_SOURCE=2 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-format-zero-length -Wno-unused-parameter -Wno-unused -Wno-parentheses -Wno-comment -Wno-unknown-pragmas -Wno-address -Wno-delete-non-virtual-dtor -Wno-char-subscripts -Wno-array-bounds -Wno-int-in-bool-context -Wno-ignored-qualifiers -Wno-missing-field-initializers -Wno-implicit-fallthrough -Wno-empty-body -Wno-strict-overflow -Wno-sequence-point -Wno-maybe-uninitialized -Wno-misleading-indentation -Wno-cast-function-type -Wno-shift-negative-value -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /home/daniel/Projects/java/jdk/make/hotspot -ferror-limit 19 -fmessage-length 0 -fvisibility hidden -stack-protector 1 -fno-rtti -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /home/daniel/Projects/java/scan/2021-12-21-193737-8510-1 -x c++ /home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp
1/*
2 * Copyright (c) 1999, 2021, 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// no precompiled headers
26#include "jvm.h"
27#include "classfile/vmSymbols.hpp"
28#include "code/icBuffer.hpp"
29#include "code/vtableStubs.hpp"
30#include "compiler/compileBroker.hpp"
31#include "compiler/disassembler.hpp"
32#include "interpreter/interpreter.hpp"
33#include "jvmtifiles/jvmti.h"
34#include "logging/log.hpp"
35#include "logging/logStream.hpp"
36#include "memory/allocation.inline.hpp"
37#include "oops/oop.inline.hpp"
38#include "os_linux.inline.hpp"
39#include "os_posix.inline.hpp"
40#include "os_share_linux.hpp"
41#include "osContainer_linux.hpp"
42#include "prims/jniFastGetField.hpp"
43#include "prims/jvm_misc.hpp"
44#include "runtime/arguments.hpp"
45#include "runtime/atomic.hpp"
46#include "runtime/globals.hpp"
47#include "runtime/globals_extension.hpp"
48#include "runtime/interfaceSupport.inline.hpp"
49#include "runtime/init.hpp"
50#include "runtime/java.hpp"
51#include "runtime/javaCalls.hpp"
52#include "runtime/mutexLocker.hpp"
53#include "runtime/objectMonitor.hpp"
54#include "runtime/osThread.hpp"
55#include "runtime/perfMemory.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/threadSMR.hpp"
62#include "runtime/timer.hpp"
63#include "runtime/vm_version.hpp"
64#include "signals_posix.hpp"
65#include "semaphore_posix.hpp"
66#include "services/memTracker.hpp"
67#include "services/runtimeService.hpp"
68#include "utilities/align.hpp"
69#include "utilities/decoder.hpp"
70#include "utilities/defaultStream.hpp"
71#include "utilities/events.hpp"
72#include "utilities/elfFile.hpp"
73#include "utilities/growableArray.hpp"
74#include "utilities/macros.hpp"
75#include "utilities/powerOfTwo.hpp"
76#include "utilities/vmError.hpp"
77
78// put OS-includes here
79# include <sys/types.h>
80# include <sys/mman.h>
81# include <sys/stat.h>
82# include <sys/select.h>
83# include <pthread.h>
84# include <signal.h>
85# include <endian.h>
86# include <errno(*__errno_location ()).h>
87# include <dlfcn.h>
88# include <stdio.h>
89# include <unistd.h>
90# include <sys/resource.h>
91# include <pthread.h>
92# include <sys/stat.h>
93# include <sys/time.h>
94# include <sys/times.h>
95# include <sys/utsname.h>
96# include <sys/socket.h>
97# include <pwd.h>
98# include <poll.h>
99# include <fcntl.h>
100# include <string.h>
101# include <syscall.h>
102# include <sys/sysinfo.h>
103# include <sys/ipc.h>
104# include <sys/shm.h>
105# include <link.h>
106# include <stdint.h>
107# include <inttypes.h>
108# include <sys/ioctl.h>
109# include <linux/elf-em.h>
110#ifdef __GLIBC__2
111# include <malloc.h>
112#endif
113
114#ifndef _GNU_SOURCE1
115 #define _GNU_SOURCE1
116 #include <sched.h>
117 #undef _GNU_SOURCE1
118#else
119 #include <sched.h>
120#endif
121
122// if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling
123// getrusage() is prepared to handle the associated failure.
124#ifndef RUSAGE_THREADRUSAGE_THREAD
125 #define RUSAGE_THREADRUSAGE_THREAD (1) /* only the calling thread */
126#endif
127
128#define MAX_PATH(2 * K) (2 * K)
129
130#define MAX_SECS100000000 100000000
131
132// for timer info max values which include all bits
133#define ALL_64_BITS(0xFFFFFFFFFFFFFFFFLL) CONST64(0xFFFFFFFFFFFFFFFF)(0xFFFFFFFFFFFFFFFFLL)
134
135#ifdef MUSL_LIBC
136// dlvsym is not a part of POSIX
137// and musl libc doesn't implement it.
138static void *dlvsym(void *handle,
139 const char *symbol,
140 const char *version) {
141 // load the latest version of symbol
142 return dlsym(handle, symbol);
143}
144#endif
145
146enum CoredumpFilterBit {
147 FILE_BACKED_PVT_BIT = 1 << 2,
148 FILE_BACKED_SHARED_BIT = 1 << 3,
149 LARGEPAGES_BIT = 1 << 6,
150 DAX_SHARED_BIT = 1 << 8
151};
152
153////////////////////////////////////////////////////////////////////////////////
154// global variables
155julong os::Linux::_physical_memory = 0;
156
157address os::Linux::_initial_thread_stack_bottom = NULL__null;
158uintptr_t os::Linux::_initial_thread_stack_size = 0;
159
160int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL__null;
161int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL__null;
162pthread_t os::Linux::_main_thread;
163int os::Linux::_page_size = -1;
164bool os::Linux::_supports_fast_thread_cpu_time = false;
165const char * os::Linux::_libc_version = NULL__null;
166const char * os::Linux::_libpthread_version = NULL__null;
167size_t os::Linux::_default_large_page_size = 0;
168
169#ifdef __GLIBC__2
170os::Linux::mallinfo_func_t os::Linux::_mallinfo = NULL__null;
171os::Linux::mallinfo2_func_t os::Linux::_mallinfo2 = NULL__null;
172#endif // __GLIBC__
173
174static int clock_tics_per_sec = 100;
175
176// If the VM might have been created on the primordial thread, we need to resolve the
177// primordial thread stack bounds and check if the current thread might be the
178// primordial thread in places. If we know that the primordial thread is never used,
179// such as when the VM was created by one of the standard java launchers, we can
180// avoid this
181static bool suppress_primordial_thread_resolution = false;
182
183// utility functions
184
185julong os::available_memory() {
186 return Linux::available_memory();
187}
188
189julong os::Linux::available_memory() {
190 // values in struct sysinfo are "unsigned long"
191 struct sysinfo si;
192 julong avail_mem;
193
194 if (OSContainer::is_containerized()) {
195 jlong mem_limit, mem_usage;
196 if ((mem_limit = OSContainer::memory_limit_in_bytes()) < 1) {
197 log_debug(os, container)(!(LogImpl<(LogTag::_os), (LogTag::_container), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Debug))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_container), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Debug>
("container memory limit %s: " JLONG_FORMAT"%" "l" "d" ", using host value",
198 mem_limit == OSCONTAINER_ERROR(-2) ? "failed" : "unlimited", mem_limit);
199 }
200 if (mem_limit > 0 && (mem_usage = OSContainer::memory_usage_in_bytes()) < 1) {
201 log_debug(os, container)(!(LogImpl<(LogTag::_os), (LogTag::_container), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Debug))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_container), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Debug>
("container memory usage failed: " JLONG_FORMAT"%" "l" "d" ", using host value", mem_usage);
202 }
203 if (mem_limit > 0 && mem_usage > 0 ) {
204 avail_mem = mem_limit > mem_usage ? (julong)mem_limit - (julong)mem_usage : 0;
205 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("available container memory: " JULONG_FORMAT"%" "l" "u", avail_mem);
206 return avail_mem;
207 }
208 }
209
210 sysinfo(&si);
211 avail_mem = (julong)si.freeram * si.mem_unit;
212 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("available memory: " JULONG_FORMAT"%" "l" "u", avail_mem);
213 return avail_mem;
214}
215
216julong os::physical_memory() {
217 jlong phys_mem = 0;
218 if (OSContainer::is_containerized()) {
219 jlong mem_limit;
220 if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {
221 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("total container memory: " JLONG_FORMAT"%" "l" "d", mem_limit);
222 return mem_limit;
223 }
224 log_debug(os, container)(!(LogImpl<(LogTag::_os), (LogTag::_container), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Debug))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_container), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Debug>
("container memory limit %s: " JLONG_FORMAT"%" "l" "d" ", using host value",
225 mem_limit == OSCONTAINER_ERROR(-2) ? "failed" : "unlimited", mem_limit);
226 }
227
228 phys_mem = Linux::physical_memory();
229 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("total system memory: " JLONG_FORMAT"%" "l" "d", phys_mem);
230 return phys_mem;
231}
232
233static uint64_t initial_total_ticks = 0;
234static uint64_t initial_steal_ticks = 0;
235static bool has_initial_tick_info = false;
236
237static void next_line(FILE *f) {
238 int c;
239 do {
240 c = fgetc(f);
241 } while (c != '\n' && c != EOF(-1));
242}
243
244bool os::Linux::get_tick_information(CPUPerfTicks* pticks, int which_logical_cpu) {
245 FILE* fh;
246 uint64_t userTicks, niceTicks, systemTicks, idleTicks;
247 // since at least kernel 2.6 : iowait: time waiting for I/O to complete
248 // irq: time servicing interrupts; softirq: time servicing softirqs
249 uint64_t iowTicks = 0, irqTicks = 0, sirqTicks= 0;
250 // steal (since kernel 2.6.11): time spent in other OS when running in a virtualized environment
251 uint64_t stealTicks = 0;
252 // guest (since kernel 2.6.24): time spent running a virtual CPU for guest OS under the
253 // control of the Linux kernel
254 uint64_t guestNiceTicks = 0;
255 int logical_cpu = -1;
256 const int required_tickinfo_count = (which_logical_cpu == -1) ? 4 : 5;
257 int n;
258
259 memset(pticks, 0, sizeof(CPUPerfTicks));
260
261 if ((fh = fopen("/proc/stat", "r")) == NULL__null) {
262 return false;
263 }
264
265 if (which_logical_cpu == -1) {
266 n = fscanf(fh, "cpu " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " "
267 UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " "
268 UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " ",
269 &userTicks, &niceTicks, &systemTicks, &idleTicks,
270 &iowTicks, &irqTicks, &sirqTicks,
271 &stealTicks, &guestNiceTicks);
272 } else {
273 // Move to next line
274 next_line(fh);
275
276 // find the line for requested cpu faster to just iterate linefeeds?
277 for (int i = 0; i < which_logical_cpu; i++) {
278 next_line(fh);
279 }
280
281 n = fscanf(fh, "cpu%u " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " "
282 UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " "
283 UINT64_FORMAT"%" "l" "u" " " UINT64_FORMAT"%" "l" "u" " ",
284 &logical_cpu, &userTicks, &niceTicks,
285 &systemTicks, &idleTicks, &iowTicks, &irqTicks, &sirqTicks,
286 &stealTicks, &guestNiceTicks);
287 }
288
289 fclose(fh);
290 if (n < required_tickinfo_count || logical_cpu != which_logical_cpu) {
291 return false;
292 }
293 pticks->used = userTicks + niceTicks;
294 pticks->usedKernel = systemTicks + irqTicks + sirqTicks;
295 pticks->total = userTicks + niceTicks + systemTicks + idleTicks +
296 iowTicks + irqTicks + sirqTicks + stealTicks + guestNiceTicks;
297
298 if (n > required_tickinfo_count + 3) {
299 pticks->steal = stealTicks;
300 pticks->has_steal_ticks = true;
301 } else {
302 pticks->steal = 0;
303 pticks->has_steal_ticks = false;
304 }
305
306 return true;
307}
308
309#ifndef SYS_gettid186
310// i386: 224, ia64: 1105, amd64: 186, sparc: 143
311 #ifdef __ia64__
312 #define SYS_gettid186 1105
313 #else
314 #ifdef __i386__
315 #define SYS_gettid186 224
316 #else
317 #ifdef __amd64__1
318 #define SYS_gettid186 186
319 #else
320 #ifdef __sparc__
321 #define SYS_gettid186 143
322 #else
323 #error define gettid for the arch
324 #endif
325 #endif
326 #endif
327 #endif
328#endif
329
330
331// pid_t gettid()
332//
333// Returns the kernel thread id of the currently running thread. Kernel
334// thread id is used to access /proc.
335pid_t os::Linux::gettid() {
336 int rslt = syscall(SYS_gettid186);
337 assert(rslt != -1, "must be.")do { if (!(rslt != -1)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 337, "assert(" "rslt != -1" ") failed", "must be."); ::breakpoint
(); } } while (0)
; // old linuxthreads implementation?
338 return (pid_t)rslt;
339}
340
341// Most versions of linux have a bug where the number of processors are
342// determined by looking at the /proc file system. In a chroot environment,
343// the system call returns 1.
344static bool unsafe_chroot_detected = false;
345static const char *unstable_chroot_error = "/proc file system not found.\n"
346 "Java may be unstable running multithreaded in a chroot "
347 "environment on Linux when /proc filesystem is not mounted.";
348
349void os::Linux::initialize_system_info() {
350 set_processor_count(sysconf(_SC_NPROCESSORS_CONF_SC_NPROCESSORS_CONF));
351 if (processor_count() == 1) {
352 pid_t pid = os::Linux::gettid();
353 char fname[32];
354 jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
355 FILE *fp = fopen(fname, "r");
356 if (fp == NULL__null) {
357 unsafe_chroot_detected = true;
358 } else {
359 fclose(fp);
360 }
361 }
362 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE_SC_PAGESIZE);
363 assert(processor_count() > 0, "linux error")do { if (!(processor_count() > 0)) { (*g_assert_poison) = 'X'
;; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 363, "assert(" "processor_count() > 0" ") failed", "linux error"
); ::breakpoint(); } } while (0)
;
364}
365
366void os::init_system_properties_values() {
367 // The next steps are taken in the product version:
368 //
369 // Obtain the JAVA_HOME value from the location of libjvm.so.
370 // This library should be located at:
371 // <JAVA_HOME>/lib/{client|server}/libjvm.so.
372 //
373 // If "/jre/lib/" appears at the right place in the path, then we
374 // assume libjvm.so is installed in a JDK and we use this path.
375 //
376 // Otherwise exit with message: "Could not create the Java virtual machine."
377 //
378 // The following extra steps are taken in the debugging version:
379 //
380 // If "/jre/lib/" does NOT appear at the right place in the path
381 // instead of exit check for $JAVA_HOME environment variable.
382 //
383 // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
384 // then we append a fake suffix "hotspot/libjvm.so" to this path so
385 // it looks like libjvm.so is installed there
386 // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
387 //
388 // Otherwise exit.
389 //
390 // Important note: if the location of libjvm.so changes this
391 // code needs to be changed accordingly.
392
393 // See ld(1):
394 // The linker uses the following search paths to locate required
395 // shared libraries:
396 // 1: ...
397 // ...
398 // 7: The default directories, normally /lib and /usr/lib.
399#ifndef OVERRIDE_LIBPATH
400 #if defined(_LP641)
401 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
402 #else
403 #define DEFAULT_LIBPATH "/lib:/usr/lib"
404 #endif
405#else
406 #define DEFAULT_LIBPATH OVERRIDE_LIBPATH
407#endif
408
409// Base path of extensions installed on the system.
410#define SYS_EXT_DIR "/usr/java/packages"
411#define EXTENSIONS_DIR "/lib/ext"
412
413 // Buffer that fits several sprintfs.
414 // Note that the space for the colon and the trailing null are provided
415 // by the nulls included by the sizeof operator.
416 const size_t bufsize =
417 MAX2((size_t)MAXPATHLEN4096, // For dll_dir & friends.
418 (size_t)MAXPATHLEN4096 + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR)); // extensions dir
419 char *buf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal)(char*) (AllocateHeap((bufsize) * sizeof(char), mtInternal));
420
421 // sysclasspath, java_home, dll_dir
422 {
423 char *pslash;
424 os::jvm_path(buf, bufsize);
425
426 // Found the full path to libjvm.so.
427 // Now cut the path to <java_home>/jre if we can.
428 pslash = strrchr(buf, '/');
429 if (pslash != NULL__null) {
430 *pslash = '\0'; // Get rid of /libjvm.so.
431 }
432 pslash = strrchr(buf, '/');
433 if (pslash != NULL__null) {
434 *pslash = '\0'; // Get rid of /{client|server|hotspot}.
435 }
436 Arguments::set_dll_dir(buf);
437
438 if (pslash != NULL__null) {
439 pslash = strrchr(buf, '/');
440 if (pslash != NULL__null) {
441 *pslash = '\0'; // Get rid of /lib.
442 }
443 }
444 Arguments::set_java_home(buf);
445 if (!set_boot_path('/', ':')) {
446 vm_exit_during_initialization("Failed setting boot class path.", NULL__null);
447 }
448 }
449
450 // Where to look for native libraries.
451 //
452 // Note: Due to a legacy implementation, most of the library path
453 // is set in the launcher. This was to accomodate linking restrictions
454 // on legacy Linux implementations (which are no longer supported).
455 // Eventually, all the library path setting will be done here.
456 //
457 // However, to prevent the proliferation of improperly built native
458 // libraries, the new path component /usr/java/packages is added here.
459 // Eventually, all the library path setting will be done here.
460 {
461 // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
462 // should always exist (until the legacy problem cited above is
463 // addressed).
464 const char *v = ::getenv("LD_LIBRARY_PATH");
465 const char *v_colon = ":";
466 if (v == NULL__null) { v = ""; v_colon = ""; }
467 // That's +1 for the colon and +1 for the trailing '\0'.
468 char *ld_library_path = NEW_C_HEAP_ARRAY(char,(char*) (AllocateHeap((strlen(v) + 1 + sizeof(SYS_EXT_DIR) + sizeof
("/lib/") + sizeof(DEFAULT_LIBPATH) + 1) * sizeof(char), mtInternal
))
469 strlen(v) + 1 +(char*) (AllocateHeap((strlen(v) + 1 + sizeof(SYS_EXT_DIR) + sizeof
("/lib/") + sizeof(DEFAULT_LIBPATH) + 1) * sizeof(char), mtInternal
))
470 sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1,(char*) (AllocateHeap((strlen(v) + 1 + sizeof(SYS_EXT_DIR) + sizeof
("/lib/") + sizeof(DEFAULT_LIBPATH) + 1) * sizeof(char), mtInternal
))
471 mtInternal)(char*) (AllocateHeap((strlen(v) + 1 + sizeof(SYS_EXT_DIR) + sizeof
("/lib/") + sizeof(DEFAULT_LIBPATH) + 1) * sizeof(char), mtInternal
))
;
472 sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon);
473 Arguments::set_library_path(ld_library_path);
474 FREE_C_HEAP_ARRAY(char, ld_library_path)FreeHeap((char*)(ld_library_path));
475 }
476
477 // Extensions directories.
478 sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
479 Arguments::set_ext_dirs(buf);
480
481 FREE_C_HEAP_ARRAY(char, buf)FreeHeap((char*)(buf));
482
483#undef DEFAULT_LIBPATH
484#undef SYS_EXT_DIR
485#undef EXTENSIONS_DIR
486}
487
488////////////////////////////////////////////////////////////////////////////////
489// breakpoint support
490
491void os::breakpoint() {
492 BREAKPOINT::breakpoint();
493}
494
495extern "C" void breakpoint() {
496 // use debugger to set breakpoint here
497}
498
499//////////////////////////////////////////////////////////////////////////////
500// detecting pthread library
501
502void os::Linux::libpthread_init() {
503 // Save glibc and pthread version strings.
504#if !defined(_CS_GNU_LIBC_VERSION_CS_GNU_LIBC_VERSION) || \
505 !defined(_CS_GNU_LIBPTHREAD_VERSION_CS_GNU_LIBPTHREAD_VERSION)
506 #error "glibc too old (< 2.3.2)"
507#endif
508
509#ifdef MUSL_LIBC
510 // confstr() from musl libc returns EINVAL for
511 // _CS_GNU_LIBC_VERSION and _CS_GNU_LIBPTHREAD_VERSION
512 os::Linux::set_libc_version("musl - unknown");
513 os::Linux::set_libpthread_version("musl - unknown");
514#else
515 size_t n = confstr(_CS_GNU_LIBC_VERSION_CS_GNU_LIBC_VERSION, NULL__null, 0);
516 assert(n > 0, "cannot retrieve glibc version")do { if (!(n > 0)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 516, "assert(" "n > 0" ") failed", "cannot retrieve glibc version"
); ::breakpoint(); } } while (0)
;
517 char *str = (char *)malloc(n, mtInternal);
518 confstr(_CS_GNU_LIBC_VERSION_CS_GNU_LIBC_VERSION, str, n);
519 os::Linux::set_libc_version(str);
520
521 n = confstr(_CS_GNU_LIBPTHREAD_VERSION_CS_GNU_LIBPTHREAD_VERSION, NULL__null, 0);
522 assert(n > 0, "cannot retrieve pthread version")do { if (!(n > 0)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 522, "assert(" "n > 0" ") failed", "cannot retrieve pthread version"
); ::breakpoint(); } } while (0)
;
523 str = (char *)malloc(n, mtInternal);
524 confstr(_CS_GNU_LIBPTHREAD_VERSION_CS_GNU_LIBPTHREAD_VERSION, str, n);
525 os::Linux::set_libpthread_version(str);
526#endif
527}
528
529/////////////////////////////////////////////////////////////////////////////
530// thread stack expansion
531
532// os::Linux::manually_expand_stack() takes care of expanding the thread
533// stack. Note that this is normally not needed: pthread stacks allocate
534// thread stack using mmap() without MAP_NORESERVE, so the stack is already
535// committed. Therefore it is not necessary to expand the stack manually.
536//
537// Manually expanding the stack was historically needed on LinuxThreads
538// thread stacks, which were allocated with mmap(MAP_GROWSDOWN). Nowadays
539// it is kept to deal with very rare corner cases:
540//
541// For one, user may run the VM on an own implementation of threads
542// whose stacks are - like the old LinuxThreads - implemented using
543// mmap(MAP_GROWSDOWN).
544//
545// Also, this coding may be needed if the VM is running on the primordial
546// thread. Normally we avoid running on the primordial thread; however,
547// user may still invoke the VM on the primordial thread.
548//
549// The following historical comment describes the details about running
550// on a thread stack allocated with mmap(MAP_GROWSDOWN):
551
552
553// Force Linux kernel to expand current thread stack. If "bottom" is close
554// to the stack guard, caller should block all signals.
555//
556// MAP_GROWSDOWN:
557// A special mmap() flag that is used to implement thread stacks. It tells
558// kernel that the memory region should extend downwards when needed. This
559// allows early versions of LinuxThreads to only mmap the first few pages
560// when creating a new thread. Linux kernel will automatically expand thread
561// stack as needed (on page faults).
562//
563// However, because the memory region of a MAP_GROWSDOWN stack can grow on
564// demand, if a page fault happens outside an already mapped MAP_GROWSDOWN
565// region, it's hard to tell if the fault is due to a legitimate stack
566// access or because of reading/writing non-exist memory (e.g. buffer
567// overrun). As a rule, if the fault happens below current stack pointer,
568// Linux kernel does not expand stack, instead a SIGSEGV is sent to the
569// application (see Linux kernel fault.c).
570//
571// This Linux feature can cause SIGSEGV when VM bangs thread stack for
572// stack overflow detection.
573//
574// Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do
575// not use MAP_GROWSDOWN.
576//
577// To get around the problem and allow stack banging on Linux, we need to
578// manually expand thread stack after receiving the SIGSEGV.
579//
580// There are two ways to expand thread stack to address "bottom", we used
581// both of them in JVM before 1.5:
582// 1. adjust stack pointer first so that it is below "bottom", and then
583// touch "bottom"
584// 2. mmap() the page in question
585//
586// Now alternate signal stack is gone, it's harder to use 2. For instance,
587// if current sp is already near the lower end of page 101, and we need to
588// call mmap() to map page 100, it is possible that part of the mmap() frame
589// will be placed in page 100. When page 100 is mapped, it is zero-filled.
590// That will destroy the mmap() frame and cause VM to crash.
591//
592// The following code works by adjusting sp first, then accessing the "bottom"
593// page to force a page fault. Linux kernel will then automatically expand the
594// stack mapping.
595//
596// _expand_stack_to() assumes its frame size is less than page size, which
597// should always be true if the function is not inlined.
598
599static void NOINLINE__attribute__ ((noinline)) _expand_stack_to(address bottom) {
600 address sp;
601 size_t size;
602 volatile char *p;
603
604 // Adjust bottom to point to the largest address within the same page, it
605 // gives us a one-page buffer if alloca() allocates slightly more memory.
606 bottom = (address)align_down((uintptr_t)bottom, os::Linux::page_size());
607 bottom += os::Linux::page_size() - 1;
608
609 // sp might be slightly above current stack pointer; if that's the case, we
610 // will alloca() a little more space than necessary, which is OK. Don't use
611 // os::current_stack_pointer(), as its result can be slightly below current
612 // stack pointer, causing us to not alloca enough to reach "bottom".
613 sp = (address)&sp;
614
615 if (sp > bottom) {
616 size = sp - bottom;
617 p = (volatile char *)alloca(size)__builtin_alloca (size);
618 assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?")do { if (!(p != __null && p <= (volatile char *)bottom
)) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 618, "assert(" "p != __null && p <= (volatile char *)bottom"
") failed", "alloca problem?"); ::breakpoint(); } } while (0
)
;
619 p[0] = '\0';
620 }
621}
622
623void os::Linux::expand_stack_to(address bottom) {
624 _expand_stack_to(bottom);
625}
626
627bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
628 assert(t!=NULL, "just checking")do { if (!(t!=__null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 628, "assert(" "t!=__null" ") failed", "just checking"); ::
breakpoint(); } } while (0)
;
629 assert(t->osthread()->expanding_stack(), "expand should be set")do { if (!(t->osthread()->expanding_stack())) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 629, "assert(" "t->osthread()->expanding_stack()" ") failed"
, "expand should be set"); ::breakpoint(); } } while (0)
;
630
631 if (t->is_in_usable_stack(addr)) {
632 sigset_t mask_all, old_sigset;
633 sigfillset(&mask_all);
634 pthread_sigmask(SIG_SETMASK2, &mask_all, &old_sigset);
635 _expand_stack_to(addr);
636 pthread_sigmask(SIG_SETMASK2, &old_sigset, NULL__null);
637 return true;
638 }
639 return false;
640}
641
642//////////////////////////////////////////////////////////////////////////////
643// create new thread
644
645// Thread start routine for all newly created threads
646static void *thread_native_entry(Thread *thread) {
647
648 thread->record_stack_base_and_size();
649
650#ifndef __GLIBC__2
651 // Try to randomize the cache line index of hot stack frames.
652 // This helps when threads of the same stack traces evict each other's
653 // cache lines. The threads can be either from the same JVM instance, or
654 // from different JVM instances. The benefit is especially true for
655 // processors with hyperthreading technology.
656 // This code is not needed anymore in glibc because it has MULTI_PAGE_ALIASING
657 // and we did not see any degradation in performance without `alloca()`.
658 static int counter = 0;
659 int pid = os::current_process_id();
660 int random = ((pid ^ counter++) & 7) * 128;
661 void *stackmem = alloca(random != 0 ? random : 1)__builtin_alloca (random != 0 ? random : 1); // ensure we allocate > 0
662 // Ensure the alloca result is used in a way that prevents the compiler from eliding it.
663 *(char *)stackmem = 1;
664#endif
665
666 thread->initialize_thread_current();
667
668 OSThread* osthread = thread->osthread();
669 Monitor* sync = osthread->startThread_lock();
670
671 osthread->set_thread_id(os::current_thread_id());
672
673 if (UseNUMA) {
674 int lgrp_id = os::numa_get_group_id();
675 if (lgrp_id != -1) {
676 thread->set_lgrp_id(lgrp_id);
677 }
678 }
679 // initialize signal mask for this thread
680 PosixSignals::hotspot_sigmask(thread);
681
682 // initialize floating point control register
683 os::Linux::init_thread_fpu_state();
684
685 // handshaking with parent thread
686 {
687 MutexLocker ml(sync, Mutex::_no_safepoint_check_flag);
688
689 // notify parent thread
690 osthread->set_state(INITIALIZED);
691 sync->notify_all();
692
693 // wait until os::start_thread()
694 while (osthread->get_state() == INITIALIZED) {
695 sync->wait_without_safepoint_check();
696 }
697 }
698
699 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Thread is alive (tid: " UINTX_FORMAT"%" "l" "u" ", pthread id: " UINTX_FORMAT"%" "l" "u" ").",
700 os::current_thread_id(), (uintx) pthread_self());
701
702 assert(osthread->pthread_id() != 0, "pthread_id was not set as expected")do { if (!(osthread->pthread_id() != 0)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 702, "assert(" "osthread->pthread_id() != 0" ") failed",
"pthread_id was not set as expected"); ::breakpoint(); } } while
(0)
;
703
704 // call one more level start routine
705 thread->call_run();
706
707 // Note: at this point the thread object may already have deleted itself.
708 // Prevent dereferencing it from here on out.
709 thread = NULL__null;
710
711 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Thread finished (tid: " UINTX_FORMAT"%" "l" "u" ", pthread id: " UINTX_FORMAT"%" "l" "u" ").",
712 os::current_thread_id(), (uintx) pthread_self());
713
714 return 0;
715}
716
717// On Linux, glibc places static TLS blocks (for __thread variables) on
718// the thread stack. This decreases the stack size actually available
719// to threads.
720//
721// For large static TLS sizes, this may cause threads to malfunction due
722// to insufficient stack space. This is a well-known issue in glibc:
723// http://sourceware.org/bugzilla/show_bug.cgi?id=11787.
724//
725// As a workaround, we call a private but assumed-stable glibc function,
726// __pthread_get_minstack() to obtain the minstack size and derive the
727// static TLS size from it. We then increase the user requested stack
728// size by this TLS size.
729//
730// Due to compatibility concerns, this size adjustment is opt-in and
731// controlled via AdjustStackSizeForTLS.
732typedef size_t (*GetMinStack)(const pthread_attr_t *attr);
733
734GetMinStack _get_minstack_func = NULL__null;
735
736static void get_minstack_init() {
737 _get_minstack_func =
738 (GetMinStack)dlsym(RTLD_DEFAULT((void *) 0), "__pthread_get_minstack");
739 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Lookup of __pthread_get_minstack %s",
740 _get_minstack_func == NULL__null ? "failed" : "succeeded");
741}
742
743// Returns the size of the static TLS area glibc puts on thread stacks.
744// The value is cached on first use, which occurs when the first thread
745// is created during VM initialization.
746static size_t get_static_tls_area_size(const pthread_attr_t *attr) {
747 size_t tls_size = 0;
748 if (_get_minstack_func != NULL__null) {
749 // Obtain the pthread minstack size by calling __pthread_get_minstack.
750 size_t minstack_size = _get_minstack_func(attr);
751
752 // Remove non-TLS area size included in minstack size returned
753 // by __pthread_get_minstack() to get the static TLS size.
754 // In glibc before 2.27, minstack size includes guard_size.
755 // In glibc 2.27 and later, guard_size is automatically added
756 // to the stack size by pthread_create and is no longer included
757 // in minstack size. In both cases, the guard_size is taken into
758 // account, so there is no need to adjust the result for that.
759 //
760 // Although __pthread_get_minstack() is a private glibc function,
761 // it is expected to have a stable behavior across future glibc
762 // versions while glibc still allocates the static TLS blocks off
763 // the stack. Following is glibc 2.28 __pthread_get_minstack():
764 //
765 // size_t
766 // __pthread_get_minstack (const pthread_attr_t *attr)
767 // {
768 // return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
769 // }
770 //
771 //
772 // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN'
773 // if check is done for precaution.
774 if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN16384) {
775 tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN16384;
776 }
777 }
778
779 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Stack size adjustment for TLS is " SIZE_FORMAT"%" "l" "u",
780 tls_size);
781 return tls_size;
782}
783
784bool os::create_thread(Thread* thread, ThreadType thr_type,
785 size_t req_stack_size) {
786 assert(thread->osthread() == NULL, "caller responsible")do { if (!(thread->osthread() == __null)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 786, "assert(" "thread->osthread() == __null" ") failed"
, "caller responsible"); ::breakpoint(); } } while (0)
;
787
788 // Allocate the OSThread object
789 OSThread* osthread = new OSThread();
790 if (osthread == NULL__null) {
791 return false;
792 }
793
794 // set the correct thread state
795 osthread->set_thread_type(thr_type);
796
797 // Initial state is ALLOCATED but not INITIALIZED
798 osthread->set_state(ALLOCATED);
799
800 thread->set_osthread(osthread);
801
802 // init thread attributes
803 pthread_attr_t attr;
804 pthread_attr_init(&attr);
805 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHEDPTHREAD_CREATE_DETACHED);
806
807 // Calculate stack size if it's not specified by caller.
808 size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
809 // In glibc versions prior to 2.7 the guard size mechanism
810 // is not implemented properly. The posix standard requires adding
811 // the size of the guard pages to the stack size, instead Linux
812 // takes the space out of 'stacksize'. Thus we adapt the requested
813 // stack_size by the size of the guard pages to mimick proper
814 // behaviour. However, be careful not to end up with a size
815 // of zero due to overflow. Don't add the guard page in that case.
816 size_t guard_size = os::Linux::default_guard_size(thr_type);
817 // Configure glibc guard page. Must happen before calling
818 // get_static_tls_area_size(), which uses the guard_size.
819 pthread_attr_setguardsize(&attr, guard_size);
820
821 size_t stack_adjust_size = 0;
822 if (AdjustStackSizeForTLS) {
823 // Adjust the stack_size for on-stack TLS - see get_static_tls_area_size().
824 stack_adjust_size += get_static_tls_area_size(&attr);
825 } else {
826 stack_adjust_size += guard_size;
827 }
828
829 stack_adjust_size = align_up(stack_adjust_size, os::vm_page_size());
830 if (stack_size <= SIZE_MAX(18446744073709551615UL) - stack_adjust_size) {
831 stack_size += stack_adjust_size;
832 }
833 assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned")do { if (!(is_aligned(stack_size, os::vm_page_size()))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 833, "assert(" "is_aligned(stack_size, os::vm_page_size())"
") failed", "stack_size not aligned"); ::breakpoint(); } } while
(0)
;
834
835 int status = pthread_attr_setstacksize(&attr, stack_size);
836 if (status != 0) {
837 // pthread_attr_setstacksize() function can fail
838 // if the stack size exceeds a system-imposed limit.
839 assert_status(status == EINVAL, status, "pthread_attr_setstacksize")do { if (!(status == 22)) { (*g_assert_poison) = 'X';; report_vm_status_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 839, "assert(" "status == 22" ") failed", status, "pthread_attr_setstacksize"
); ::breakpoint(); } } while (0)
;
840 log_warning(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Warning))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Warning>
("The %sthread stack size specified is invalid: " SIZE_FORMAT"%" "l" "u" "k",
841 (thr_type == compiler_thread) ? "compiler " : ((thr_type == java_thread) ? "" : "VM "),
842 stack_size / K);
843 thread->set_osthread(NULL__null);
844 delete osthread;
845 return false;
846 }
847
848 ThreadState state;
849
850 {
851 ResourceMark rm;
852 pthread_t tid;
853 int ret = 0;
854 int limit = 3;
855 do {
856 ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
857 } while (ret == EAGAIN11 && limit-- > 0);
858
859 char buf[64];
860 if (ret == 0) {
861 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Thread \"%s\" started (pthread id: " UINTX_FORMAT"%" "l" "u" ", attributes: %s). ",
862 thread->name(), (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
863 } else {
864 log_warning(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Warning))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Warning>
("Failed to start thread \"%s\" - pthread_create failed (%s) for attributes: %s.",
865 thread->name(), os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
866 // Log some OS information which might explain why creating the thread failed.
867 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
868 LogStream st(Log(os, thread)LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>
::info());
869 os::Posix::print_rlimit_info(&st);
870 os::print_memory_info(&st);
871 os::Linux::print_proc_sys_info(&st);
872 os::Linux::print_container_info(&st);
873 }
874
875 pthread_attr_destroy(&attr);
876
877 if (ret != 0) {
878 // Need to clean up stuff we've allocated so far
879 thread->set_osthread(NULL__null);
880 delete osthread;
881 return false;
882 }
883
884 // Store pthread info into the OSThread
885 osthread->set_pthread_id(tid);
886
887 // Wait until child thread is either initialized or aborted
888 {
889 Monitor* sync_with_child = osthread->startThread_lock();
890 MutexLocker ml(sync_with_child, Mutex::_no_safepoint_check_flag);
891 while ((state = osthread->get_state()) == ALLOCATED) {
892 sync_with_child->wait_without_safepoint_check();
893 }
894 }
895 }
896
897 // The thread is returned suspended (in state INITIALIZED),
898 // and is started higher up in the call chain
899 assert(state == INITIALIZED, "race condition")do { if (!(state == INITIALIZED)) { (*g_assert_poison) = 'X';
; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 899, "assert(" "state == INITIALIZED" ") failed", "race condition"
); ::breakpoint(); } } while (0)
;
900 return true;
901}
902
903/////////////////////////////////////////////////////////////////////////////
904// attach existing thread
905
906// bootstrap the main thread
907bool os::create_main_thread(JavaThread* thread) {
908 assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread")do { if (!(os::Linux::_main_thread == pthread_self())) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 908, "assert(" "os::Linux::_main_thread == pthread_self()" ") failed"
, "should be called inside main thread"); ::breakpoint(); } }
while (0)
;
909 return create_attached_thread(thread);
910}
911
912bool os::create_attached_thread(JavaThread* thread) {
913#ifdef ASSERT1
914 thread->verify_not_published();
915#endif
916
917 // Allocate the OSThread object
918 OSThread* osthread = new OSThread();
919
920 if (osthread == NULL__null) {
921 return false;
922 }
923
924 // Store pthread info into the OSThread
925 osthread->set_thread_id(os::Linux::gettid());
926 osthread->set_pthread_id(::pthread_self());
927
928 // initialize floating point control register
929 os::Linux::init_thread_fpu_state();
930
931 // Initial thread state is RUNNABLE
932 osthread->set_state(RUNNABLE);
933
934 thread->set_osthread(osthread);
935
936 if (UseNUMA) {
937 int lgrp_id = os::numa_get_group_id();
938 if (lgrp_id != -1) {
939 thread->set_lgrp_id(lgrp_id);
940 }
941 }
942
943 if (os::is_primordial_thread()) {
944 // If current thread is primordial thread, its stack is mapped on demand,
945 // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
946 // the entire stack region to avoid SEGV in stack banging.
947 // It is also useful to get around the heap-stack-gap problem on SuSE
948 // kernel (see 4821821 for details). We first expand stack to the top
949 // of yellow zone, then enable stack yellow zone (order is significant,
950 // enabling yellow zone first will crash JVM on SuSE Linux), so there
951 // is no gap between the last two virtual memory regions.
952
953 StackOverflow* overflow_state = thread->stack_overflow_state();
954 address addr = overflow_state->stack_reserved_zone_base();
955 assert(addr != NULL, "initialization problem?")do { if (!(addr != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 955, "assert(" "addr != __null" ") failed", "initialization problem?"
); ::breakpoint(); } } while (0)
;
956 assert(overflow_state->stack_available(addr) > 0, "stack guard should not be enabled")do { if (!(overflow_state->stack_available(addr) > 0)) {
(*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 956, "assert(" "overflow_state->stack_available(addr) > 0"
") failed", "stack guard should not be enabled"); ::breakpoint
(); } } while (0)
;
957
958 osthread->set_expanding_stack();
959 os::Linux::manually_expand_stack(thread, addr);
960 osthread->clear_expanding_stack();
961 }
962
963 // initialize signal mask for this thread
964 // and save the caller's signal mask
965 PosixSignals::hotspot_sigmask(thread);
966
967 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Thread attached (tid: " UINTX_FORMAT"%" "l" "u" ", pthread id: " UINTX_FORMAT"%" "l" "u" ").",
968 os::current_thread_id(), (uintx) pthread_self());
969
970 return true;
971}
972
973void os::pd_start_thread(Thread* thread) {
974 OSThread * osthread = thread->osthread();
975 assert(osthread->get_state() != INITIALIZED, "just checking")do { if (!(osthread->get_state() != INITIALIZED)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 975, "assert(" "osthread->get_state() != INITIALIZED" ") failed"
, "just checking"); ::breakpoint(); } } while (0)
;
976 Monitor* sync_with_child = osthread->startThread_lock();
977 MutexLocker ml(sync_with_child, Mutex::_no_safepoint_check_flag);
978 sync_with_child->notify();
979}
980
981// Free Linux resources related to the OSThread
982void os::free_thread(OSThread* osthread) {
983 assert(osthread != NULL, "osthread not set")do { if (!(osthread != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 983, "assert(" "osthread != __null" ") failed", "osthread not set"
); ::breakpoint(); } } while (0)
;
984
985 // We are told to free resources of the argument thread,
986 // but we can only really operate on the current thread.
987 assert(Thread::current()->osthread() == osthread,do { if (!(Thread::current()->osthread() == osthread)) { (
*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 988, "assert(" "Thread::current()->osthread() == osthread"
") failed", "os::free_thread but not current thread"); ::breakpoint
(); } } while (0)
988 "os::free_thread but not current thread")do { if (!(Thread::current()->osthread() == osthread)) { (
*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 988, "assert(" "Thread::current()->osthread() == osthread"
") failed", "os::free_thread but not current thread"); ::breakpoint
(); } } while (0)
;
989
990#ifdef ASSERT1
991 sigset_t current;
992 sigemptyset(&current);
993 pthread_sigmask(SIG_SETMASK2, NULL__null, &current);
994 assert(!sigismember(&current, PosixSignals::SR_signum), "SR signal should not be blocked!")do { if (!(!sigismember(&current, PosixSignals::SR_signum
))) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 994, "assert(" "!sigismember(&current, PosixSignals::SR_signum)"
") failed", "SR signal should not be blocked!"); ::breakpoint
(); } } while (0)
;
995#endif
996
997 // Restore caller's signal mask
998 sigset_t sigmask = osthread->caller_sigmask();
999 pthread_sigmask(SIG_SETMASK2, &sigmask, NULL__null);
1000
1001 delete osthread;
1002}
1003
1004//////////////////////////////////////////////////////////////////////////////
1005// primordial thread
1006
1007// Check if current thread is the primordial thread, similar to Solaris thr_main.
1008bool os::is_primordial_thread(void) {
1009 if (suppress_primordial_thread_resolution) {
1010 return false;
1011 }
1012 char dummy;
1013 // If called before init complete, thread stack bottom will be null.
1014 // Can be called if fatal error occurs before initialization.
1015 if (os::Linux::initial_thread_stack_bottom() == NULL__null) return false;
1016 assert(os::Linux::initial_thread_stack_bottom() != NULL &&do { if (!(os::Linux::initial_thread_stack_bottom() != __null
&& os::Linux::initial_thread_stack_size() != 0)) { (
*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1018, "assert(" "os::Linux::initial_thread_stack_bottom() != __null && os::Linux::initial_thread_stack_size() != 0"
") failed", "os::init did not locate primordial thread's stack region"
); ::breakpoint(); } } while (0)
1017 os::Linux::initial_thread_stack_size() != 0,do { if (!(os::Linux::initial_thread_stack_bottom() != __null
&& os::Linux::initial_thread_stack_size() != 0)) { (
*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1018, "assert(" "os::Linux::initial_thread_stack_bottom() != __null && os::Linux::initial_thread_stack_size() != 0"
") failed", "os::init did not locate primordial thread's stack region"
); ::breakpoint(); } } while (0)
1018 "os::init did not locate primordial thread's stack region")do { if (!(os::Linux::initial_thread_stack_bottom() != __null
&& os::Linux::initial_thread_stack_size() != 0)) { (
*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1018, "assert(" "os::Linux::initial_thread_stack_bottom() != __null && os::Linux::initial_thread_stack_size() != 0"
") failed", "os::init did not locate primordial thread's stack region"
); ::breakpoint(); } } while (0)
;
1019 if ((address)&dummy >= os::Linux::initial_thread_stack_bottom() &&
1020 (address)&dummy < os::Linux::initial_thread_stack_bottom() +
1021 os::Linux::initial_thread_stack_size()) {
1022 return true;
1023 } else {
1024 return false;
1025 }
1026}
1027
1028// Find the virtual memory area that contains addr
1029static bool find_vma(address addr, address* vma_low, address* vma_high) {
1030 FILE *fp = fopen("/proc/self/maps", "r");
1031 if (fp) {
1032 address low, high;
1033 while (!feof(fp)) {
1034 if (fscanf(fp, "%p-%p", &low, &high) == 2) {
1035 if (low <= addr && addr < high) {
1036 if (vma_low) *vma_low = low;
1037 if (vma_high) *vma_high = high;
1038 fclose(fp);
1039 return true;
1040 }
1041 }
1042 for (;;) {
1043 int ch = fgetc(fp);
1044 if (ch == EOF(-1) || ch == (int)'\n') break;
1045 }
1046 }
1047 fclose(fp);
1048 }
1049 return false;
1050}
1051
1052// Locate primordial thread stack. This special handling of primordial thread stack
1053// is needed because pthread_getattr_np() on most (all?) Linux distros returns
1054// bogus value for the primordial process thread. While the launcher has created
1055// the VM in a new thread since JDK 6, we still have to allow for the use of the
1056// JNI invocation API from a primordial thread.
1057void os::Linux::capture_initial_stack(size_t max_size) {
1058
1059 // max_size is either 0 (which means accept OS default for thread stacks) or
1060 // a user-specified value known to be at least the minimum needed. If we
1061 // are actually on the primordial thread we can make it appear that we have a
1062 // smaller max_size stack by inserting the guard pages at that location. But we
1063 // cannot do anything to emulate a larger stack than what has been provided by
1064 // the OS or threading library. In fact if we try to use a stack greater than
1065 // what is set by rlimit then we will crash the hosting process.
1066
1067 // Maximum stack size is the easy part, get it from RLIMIT_STACK.
1068 // If this is "unlimited" then it will be a huge value.
1069 struct rlimit rlim;
1070 getrlimit(RLIMIT_STACKRLIMIT_STACK, &rlim);
1071 size_t stack_size = rlim.rlim_cur;
1072
1073 // 6308388: a bug in ld.so will relocate its own .data section to the
1074 // lower end of primordial stack; reduce ulimit -s value a little bit
1075 // so we won't install guard page on ld.so's data section.
1076 // But ensure we don't underflow the stack size - allow 1 page spare
1077 if (stack_size >= (size_t)(3 * page_size())) {
1078 stack_size -= 2 * page_size();
1079 }
1080
1081 // Try to figure out where the stack base (top) is. This is harder.
1082 //
1083 // When an application is started, glibc saves the initial stack pointer in
1084 // a global variable "__libc_stack_end", which is then used by system
1085 // libraries. __libc_stack_end should be pretty close to stack top. The
1086 // variable is available since the very early days. However, because it is
1087 // a private interface, it could disappear in the future.
1088 //
1089 // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar
1090 // to __libc_stack_end, it is very close to stack top, but isn't the real
1091 // stack top. Note that /proc may not exist if VM is running as a chroot
1092 // program, so reading /proc/<pid>/stat could fail. Also the contents of
1093 // /proc/<pid>/stat could change in the future (though unlikely).
1094 //
1095 // We try __libc_stack_end first. If that doesn't work, look for
1096 // /proc/<pid>/stat. If neither of them works, we use current stack pointer
1097 // as a hint, which should work well in most cases.
1098
1099 uintptr_t stack_start;
1100
1101 // try __libc_stack_end first
1102 uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT((void *) 0), "__libc_stack_end");
1103 if (p && *p) {
1104 stack_start = *p;
1105 } else {
1106 // see if we can get the start_stack field from /proc/self/stat
1107 FILE *fp;
1108 int pid;
1109 char state;
1110 int ppid;
1111 int pgrp;
1112 int session;
1113 int nr;
1114 int tpgrp;
1115 unsigned long flags;
1116 unsigned long minflt;
1117 unsigned long cminflt;
1118 unsigned long majflt;
1119 unsigned long cmajflt;
1120 unsigned long utime;
1121 unsigned long stime;
1122 long cutime;
1123 long cstime;
1124 long prio;
1125 long nice;
1126 long junk;
1127 long it_real;
1128 uintptr_t start;
1129 uintptr_t vsize;
1130 intptr_t rss;
1131 uintptr_t rsslim;
1132 uintptr_t scodes;
1133 uintptr_t ecode;
1134 int i;
1135
1136 // Figure what the primordial thread stack base is. Code is inspired
1137 // by email from Hans Boehm. /proc/self/stat begins with current pid,
1138 // followed by command name surrounded by parentheses, state, etc.
1139 char stat[2048];
1140 int statlen;
1141
1142 fp = fopen("/proc/self/stat", "r");
1143 if (fp) {
1144 statlen = fread(stat, 1, 2047, fp);
1145 stat[statlen] = '\0';
1146 fclose(fp);
1147
1148 // Skip pid and the command string. Note that we could be dealing with
1149 // weird command names, e.g. user could decide to rename java launcher
1150 // to "java 1.4.2 :)", then the stat file would look like
1151 // 1234 (java 1.4.2 :)) R ... ...
1152 // We don't really need to know the command string, just find the last
1153 // occurrence of ")" and then start parsing from there. See bug 4726580.
1154 char * s = strrchr(stat, ')');
1155
1156 i = 0;
1157 if (s) {
1158 // Skip blank chars
1159 do { s++; } while (s && isspace(*s));
1160
1161#define _UFM UINTX_FORMAT"%" "l" "u"
1162#define _DFM INTX_FORMAT"%" "l" "d"
1163
1164 // 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
1165 // 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8
1166 i = sscanf(s, "%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld " _UFM _UFM _DFM _UFM _UFM _UFM _UFM,
1167 &state, // 3 %c
1168 &ppid, // 4 %d
1169 &pgrp, // 5 %d
1170 &session, // 6 %d
1171 &nr, // 7 %d
1172 &tpgrp, // 8 %d
1173 &flags, // 9 %lu
1174 &minflt, // 10 %lu
1175 &cminflt, // 11 %lu
1176 &majflt, // 12 %lu
1177 &cmajflt, // 13 %lu
1178 &utime, // 14 %lu
1179 &stime, // 15 %lu
1180 &cutime, // 16 %ld
1181 &cstime, // 17 %ld
1182 &prio, // 18 %ld
1183 &nice, // 19 %ld
1184 &junk, // 20 %ld
1185 &it_real, // 21 %ld
1186 &start, // 22 UINTX_FORMAT
1187 &vsize, // 23 UINTX_FORMAT
1188 &rss, // 24 INTX_FORMAT
1189 &rsslim, // 25 UINTX_FORMAT
1190 &scodes, // 26 UINTX_FORMAT
1191 &ecode, // 27 UINTX_FORMAT
1192 &stack_start); // 28 UINTX_FORMAT
1193 }
1194
1195#undef _UFM
1196#undef _DFM
1197
1198 if (i != 28 - 2) {
1199 assert(false, "Bad conversion from /proc/self/stat")do { if (!(false)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1199, "assert(" "false" ") failed", "Bad conversion from /proc/self/stat"
); ::breakpoint(); } } while (0)
;
1200 // product mode - assume we are the primordial thread, good luck in the
1201 // embedded case.
1202 warning("Can't detect primordial thread stack location - bad conversion");
1203 stack_start = (uintptr_t) &rlim;
1204 }
1205 } else {
1206 // For some reason we can't open /proc/self/stat (for example, running on
1207 // FreeBSD with a Linux emulator, or inside chroot), this should work for
1208 // most cases, so don't abort:
1209 warning("Can't detect primordial thread stack location - no /proc/self/stat");
1210 stack_start = (uintptr_t) &rlim;
1211 }
1212 }
1213
1214 // Now we have a pointer (stack_start) very close to the stack top, the
1215 // next thing to do is to figure out the exact location of stack top. We
1216 // can find out the virtual memory area that contains stack_start by
1217 // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
1218 // and its upper limit is the real stack top. (again, this would fail if
1219 // running inside chroot, because /proc may not exist.)
1220
1221 uintptr_t stack_top;
1222 address low, high;
1223 if (find_vma((address)stack_start, &low, &high)) {
1224 // success, "high" is the true stack top. (ignore "low", because initial
1225 // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
1226 stack_top = (uintptr_t)high;
1227 } else {
1228 // failed, likely because /proc/self/maps does not exist
1229 warning("Can't detect primordial thread stack location - find_vma failed");
1230 // best effort: stack_start is normally within a few pages below the real
1231 // stack top, use it as stack top, and reduce stack size so we won't put
1232 // guard page outside stack.
1233 stack_top = stack_start;
1234 stack_size -= 16 * page_size();
1235 }
1236
1237 // stack_top could be partially down the page so align it
1238 stack_top = align_up(stack_top, page_size());
1239
1240 // Allowed stack value is minimum of max_size and what we derived from rlimit
1241 if (max_size > 0) {
1242 _initial_thread_stack_size = MIN2(max_size, stack_size);
1243 } else {
1244 // Accept the rlimit max, but if stack is unlimited then it will be huge, so
1245 // clamp it at 8MB as we do on Solaris
1246 _initial_thread_stack_size = MIN2(stack_size, 8*M);
1247 }
1248 _initial_thread_stack_size = align_down(_initial_thread_stack_size, page_size());
1249 _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;
1250
1251 assert(_initial_thread_stack_bottom < (address)stack_top, "overflow!")do { if (!(_initial_thread_stack_bottom < (address)stack_top
)) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1251, "assert(" "_initial_thread_stack_bottom < (address)stack_top"
") failed", "overflow!"); ::breakpoint(); } } while (0)
;
1252
1253 if (log_is_enabled(Info, os, thread)(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))
) {
1254 // See if we seem to be on primordial process thread
1255 bool primordial = uintptr_t(&rlim) > uintptr_t(_initial_thread_stack_bottom) &&
1256 uintptr_t(&rlim) < stack_top;
1257
1258 log_info(os, thread)(!(LogImpl<(LogTag::_os), (LogTag::_thread), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::_thread), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("Capturing initial stack in %s thread: req. size: " SIZE_FORMAT"%" "l" "u" "K, actual size: "
1259 SIZE_FORMAT"%" "l" "u" "K, top=" INTPTR_FORMAT"0x%016" "l" "x" ", bottom=" INTPTR_FORMAT"0x%016" "l" "x",
1260 primordial ? "primordial" : "user", max_size / K, _initial_thread_stack_size / K,
1261 stack_top, intptr_t(_initial_thread_stack_bottom));
1262 }
1263}
1264
1265////////////////////////////////////////////////////////////////////////////////
1266// time support
1267double os::elapsedVTime() {
1268 struct rusage usage;
1269 int retval = getrusage(RUSAGE_THREADRUSAGE_THREAD, &usage);
1270 if (retval == 0) {
1271 return (double) (usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) + (double) (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / (1000 * 1000);
1272 } else {
1273 // better than nothing, but not much
1274 return elapsedTime();
1275 }
1276}
1277
1278void os::Linux::fast_thread_clock_init() {
1279 if (!UseLinuxPosixThreadCPUClocks) {
1280 return;
1281 }
1282 clockid_t clockid;
1283 struct timespec tp;
1284 int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
1285 (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT((void *) 0), "pthread_getcpuclockid");
1286
1287 // Switch to using fast clocks for thread cpu time if
1288 // the clock_getres() returns 0 error code.
1289 // Note, that some kernels may support the current thread
1290 // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
1291 // returned by the pthread_getcpuclockid().
1292 // If the fast Posix clocks are supported then the clock_getres()
1293 // must return at least tp.tv_sec == 0 which means a resolution
1294 // better than 1 sec. This is extra check for reliability.
1295
1296 if (pthread_getcpuclockid_func &&
1297 pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
1298 clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
1299 _supports_fast_thread_cpu_time = true;
1300 _pthread_getcpuclockid = pthread_getcpuclockid_func;
1301 }
1302}
1303
1304// thread_id is kernel thread id (similar to Solaris LWP id)
1305intx os::current_thread_id() { return os::Linux::gettid(); }
1306int os::current_process_id() {
1307 return ::getpid();
1308}
1309
1310// DLL functions
1311
1312const char* os::dll_file_extension() { return ".so"; }
1313
1314// This must be hard coded because it's the system's temporary
1315// directory not the java application's temp directory, ala java.io.tmpdir.
1316const char* os::get_temp_directory() { return "/tmp"; }
1317
1318// check if addr is inside libjvm.so
1319bool os::address_is_in_vm(address addr) {
1320 static address libjvm_base_addr;
1321 Dl_info dlinfo;
1322
1323 if (libjvm_base_addr == NULL__null) {
1324 if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm)((void *)((address_word)(os::address_is_in_vm))), &dlinfo) != 0) {
1325 libjvm_base_addr = (address)dlinfo.dli_fbase;
1326 }
1327 assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm")do { if (!(libjvm_base_addr !=__null)) { (*g_assert_poison) =
'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1327, "assert(" "libjvm_base_addr !=__null" ") failed", "Cannot obtain base address for libjvm"
); ::breakpoint(); } } while (0)
;
1328 }
1329
1330 if (dladdr((void *)addr, &dlinfo) != 0) {
1331 if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
1332 }
1333
1334 return false;
1335}
1336
1337bool os::dll_address_to_function_name(address addr, char *buf,
1338 int buflen, int *offset,
1339 bool demangle) {
1340 // buf is not optional, but offset is optional
1341 assert(buf != NULL, "sanity check")do { if (!(buf != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1341, "assert(" "buf != __null" ") failed", "sanity check")
; ::breakpoint(); } } while (0)
;
1342
1343 Dl_info dlinfo;
1344
1345 if (dladdr((void*)addr, &dlinfo) != 0) {
1346 // see if we have a matching symbol
1347 if (dlinfo.dli_saddr != NULL__null && dlinfo.dli_sname != NULL__null) {
1348 if (!(demangle && Decoder::demangle(dlinfo.dli_sname, buf, buflen))) {
1349 jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
1350 }
1351 if (offset != NULL__null) *offset = addr - (address)dlinfo.dli_saddr;
1352 return true;
1353 }
1354 // no matching symbol so try for just file info
1355 if (dlinfo.dli_fname != NULL__null && dlinfo.dli_fbase != NULL__null) {
1356 if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
1357 buf, buflen, offset, dlinfo.dli_fname, demangle)) {
1358 return true;
1359 }
1360 }
1361 }
1362
1363 buf[0] = '\0';
1364 if (offset != NULL__null) *offset = -1;
1365 return false;
1366}
1367
1368struct _address_to_library_name {
1369 address addr; // input : memory address
1370 size_t buflen; // size of fname
1371 char* fname; // output: library name
1372 address base; // library base addr
1373};
1374
1375static int address_to_library_name_callback(struct dl_phdr_info *info,
1376 size_t size, void *data) {
1377 int i;
1378 bool found = false;
1379 address libbase = NULL__null;
1380 struct _address_to_library_name * d = (struct _address_to_library_name *)data;
1381
1382 // iterate through all loadable segments
1383 for (i = 0; i < info->dlpi_phnum; i++) {
1384 address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
1385 if (info->dlpi_phdr[i].p_type == PT_LOAD1) {
1386 // base address of a library is the lowest address of its loaded
1387 // segments.
1388 if (libbase == NULL__null || libbase > segbase) {
1389 libbase = segbase;
1390 }
1391 // see if 'addr' is within current segment
1392 if (segbase <= d->addr &&
1393 d->addr < segbase + info->dlpi_phdr[i].p_memsz) {
1394 found = true;
1395 }
1396 }
1397 }
1398
1399 // dlpi_name is NULL or empty if the ELF file is executable, return 0
1400 // so dll_address_to_library_name() can fall through to use dladdr() which
1401 // can figure out executable name from argv[0].
1402 if (found && info->dlpi_name && info->dlpi_name[0]) {
1403 d->base = libbase;
1404 if (d->fname) {
1405 jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
1406 }
1407 return 1;
1408 }
1409 return 0;
1410}
1411
1412bool os::dll_address_to_library_name(address addr, char* buf,
1413 int buflen, int* offset) {
1414 // buf is not optional, but offset is optional
1415 assert(buf != NULL, "sanity check")do { if (!(buf != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1415, "assert(" "buf != __null" ") failed", "sanity check")
; ::breakpoint(); } } while (0)
;
1416
1417 Dl_info dlinfo;
1418 struct _address_to_library_name data;
1419
1420 // There is a bug in old glibc dladdr() implementation that it could resolve
1421 // to wrong library name if the .so file has a base address != NULL. Here
1422 // we iterate through the program headers of all loaded libraries to find
1423 // out which library 'addr' really belongs to. This workaround can be
1424 // removed once the minimum requirement for glibc is moved to 2.3.x.
1425 data.addr = addr;
1426 data.fname = buf;
1427 data.buflen = buflen;
1428 data.base = NULL__null;
1429 int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
1430
1431 if (rslt) {
1432 // buf already contains library name
1433 if (offset) *offset = addr - data.base;
1434 return true;
1435 }
1436 if (dladdr((void*)addr, &dlinfo) != 0) {
1437 if (dlinfo.dli_fname != NULL__null) {
1438 jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
1439 }
1440 if (dlinfo.dli_fbase != NULL__null && offset != NULL__null) {
1441 *offset = addr - (address)dlinfo.dli_fbase;
1442 }
1443 return true;
1444 }
1445
1446 buf[0] = '\0';
1447 if (offset) *offset = -1;
1448 return false;
1449}
1450
1451// Loads .dll/.so and
1452// in case of error it checks if .dll/.so was built for the
1453// same architecture as Hotspot is running on
1454
1455
1456// Remember the stack's state. The Linux dynamic linker will change
1457// the stack to 'executable' at most once, so we must safepoint only once.
1458bool os::Linux::_stack_is_executable = false;
1459
1460// VM operation that loads a library. This is necessary if stack protection
1461// of the Java stacks can be lost during loading the library. If we
1462// do not stop the Java threads, they can stack overflow before the stacks
1463// are protected again.
1464class VM_LinuxDllLoad: public VM_Operation {
1465 private:
1466 const char *_filename;
1467 char *_ebuf;
1468 int _ebuflen;
1469 void *_lib;
1470 public:
1471 VM_LinuxDllLoad(const char *fn, char *ebuf, int ebuflen) :
1472 _filename(fn), _ebuf(ebuf), _ebuflen(ebuflen), _lib(NULL__null) {}
1473 VMOp_Type type() const { return VMOp_LinuxDllLoad; }
1474 void doit() {
1475 _lib = os::Linux::dll_load_in_vmthread(_filename, _ebuf, _ebuflen);
1476 os::Linux::_stack_is_executable = true;
1477 }
1478 void* loaded_library() { return _lib; }
1479};
1480
1481void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
1482 void * result = NULL__null;
1483 bool load_attempted = false;
1484
1485 log_info(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("attempting shared library load of %s", filename);
1486
1487 // Check whether the library to load might change execution rights
1488 // of the stack. If they are changed, the protection of the stack
1489 // guard pages will be lost. We need a safepoint to fix this.
1490 //
1491 // See Linux man page execstack(8) for more info.
1492 if (os::uses_stack_guard_pages() && !os::Linux::_stack_is_executable) {
1493 if (!ElfFile::specifies_noexecstack(filename)) {
1494 if (!is_init_completed()) {
1495 os::Linux::_stack_is_executable = true;
1496 // This is OK - No Java threads have been created yet, and hence no
1497 // stack guard pages to fix.
1498 //
1499 // Dynamic loader will make all stacks executable after
1500 // this function returns, and will not do that again.
1501 assert(Threads::number_of_threads() == 0, "no Java threads should exist yet.")do { if (!(Threads::number_of_threads() == 0)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1501, "assert(" "Threads::number_of_threads() == 0" ") failed"
, "no Java threads should exist yet."); ::breakpoint(); } } while
(0)
;
1502 } else {
1503 warning("You have loaded library %s which might have disabled stack guard. "
1504 "The VM will try to fix the stack guard now.\n"
1505 "It's highly recommended that you fix the library with "
1506 "'execstack -c <libfile>', or link it with '-z noexecstack'.",
1507 filename);
1508
1509 JavaThread *jt = JavaThread::current();
1510 if (jt->thread_state() != _thread_in_native) {
1511 // This happens when a compiler thread tries to load a hsdis-<arch>.so file
1512 // that requires ExecStack. Cannot enter safe point. Let's give up.
1513 warning("Unable to fix stack guard. Giving up.");
1514 } else {
1515 if (!LoadExecStackDllInVMThread) {
1516 // This is for the case where the DLL has an static
1517 // constructor function that executes JNI code. We cannot
1518 // load such DLLs in the VMThread.
1519 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);
1520 }
1521
1522 ThreadInVMfromNative tiv(jt);
1523 debug_only(VMNativeEntryWrapper vew;)VMNativeEntryWrapper vew;
1524
1525 VM_LinuxDllLoad op(filename, ebuf, ebuflen);
1526 VMThread::execute(&op);
1527 if (LoadExecStackDllInVMThread) {
1528 result = op.loaded_library();
1529 }
1530 load_attempted = true;
1531 }
1532 }
1533 }
1534 }
1535
1536 if (!load_attempted) {
1537 result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);
1538 }
1539
1540 if (result != NULL__null) {
1541 // Successful loading
1542 return result;
1543 }
1544
1545 Elf32_Ehdr elf_head;
1546 int diag_msg_max_length=ebuflen-strlen(ebuf);
1547 char* diag_msg_buf=ebuf+strlen(ebuf);
1548
1549 if (diag_msg_max_length==0) {
1550 // No more space in ebuf for additional diagnostics message
1551 return NULL__null;
1552 }
1553
1554
1555 int file_descriptor= ::open(filename, O_RDONLY00 | O_NONBLOCK04000);
1556
1557 if (file_descriptor < 0) {
1558 // Can't open library, report dlerror() message
1559 return NULL__null;
1560 }
1561
1562 bool failed_to_read_elf_head=
1563 (sizeof(elf_head)!=
1564 (::read(file_descriptor, &elf_head,sizeof(elf_head))));
1565
1566 ::close(file_descriptor);
1567 if (failed_to_read_elf_head) {
1568 // file i/o error - report dlerror() msg
1569 return NULL__null;
1570 }
1571
1572 if (elf_head.e_ident[EI_DATA5] != LITTLE_ENDIAN_ONLY(ELFDATA2LSB)1 BIG_ENDIAN_ONLY(ELFDATA2MSB)) {
1573 // handle invalid/out of range endianness values
1574 if (elf_head.e_ident[EI_DATA5] == 0 || elf_head.e_ident[EI_DATA5] > 2) {
1575 return NULL__null;
1576 }
1577
1578#if defined(VM_LITTLE_ENDIAN1)
1579 // VM is LE, shared object BE
1580 elf_head.e_machine = be16toh(elf_head.e_machine)(__extension__ ({ unsigned short int __v, __x = (unsigned short
int) (elf_head.e_machine); if (__builtin_constant_p (__x)) __v
= ((unsigned short int) ((((__x) >> 8) & 0xff) | (
((__x) & 0xff) << 8))); else __asm__ ("rorw $8, %w0"
: "=r" (__v) : "0" (__x) : "cc"); __v; }))
;
1581#else
1582 // VM is BE, shared object LE
1583 elf_head.e_machine = le16toh(elf_head.e_machine)__uint16_identity (elf_head.e_machine);
1584#endif
1585 }
1586
1587 typedef struct {
1588 Elf32_Half code; // Actual value as defined in elf.h
1589 Elf32_Half compat_class; // Compatibility of archs at VM's sense
1590 unsigned char elf_class; // 32 or 64 bit
1591 unsigned char endianness; // MSB or LSB
1592 char* name; // String representation
1593 } arch_t;
1594
1595#ifndef EM_AARCH64183
1596 #define EM_AARCH64183 183 /* ARM AARCH64 */
1597#endif
1598#ifndef EM_RISCV243
1599 #define EM_RISCV243 243 /* RISC-V */
1600#endif
1601#ifndef EM_LOONGARCH258
1602 #define EM_LOONGARCH258 258 /* LoongArch */
1603#endif
1604
1605 static const arch_t arch_array[]={
1606 {EM_3863, EM_3863, ELFCLASS321, ELFDATA2LSB1, (char*)"IA 32"},
1607 {EM_4866, EM_3863, ELFCLASS321, ELFDATA2LSB1, (char*)"IA 32"},
1608 {EM_IA_6450, EM_IA_6450, ELFCLASS642, ELFDATA2LSB1, (char*)"IA 64"},
1609 {EM_X86_6462, EM_X86_6462, ELFCLASS642, ELFDATA2LSB1, (char*)"AMD 64"},
1610 {EM_SPARC2, EM_SPARC2, ELFCLASS321, ELFDATA2MSB2, (char*)"Sparc 32"},
1611 {EM_SPARC32PLUS18, EM_SPARC2, ELFCLASS321, ELFDATA2MSB2, (char*)"Sparc 32"},
1612 {EM_SPARCV943, EM_SPARCV943, ELFCLASS642, ELFDATA2MSB2, (char*)"Sparc v9 64"},
1613 {EM_PPC20, EM_PPC20, ELFCLASS321, ELFDATA2MSB2, (char*)"Power PC 32"},
1614#if defined(VM_LITTLE_ENDIAN1)
1615 {EM_PPC6421, EM_PPC6421, ELFCLASS642, ELFDATA2LSB1, (char*)"Power PC 64 LE"},
1616 {EM_SH42, EM_SH42, ELFCLASS321, ELFDATA2LSB1, (char*)"SuperH"},
1617#else
1618 {EM_PPC6421, EM_PPC6421, ELFCLASS642, ELFDATA2MSB2, (char*)"Power PC 64"},
1619 {EM_SH42, EM_SH42, ELFCLASS321, ELFDATA2MSB2, (char*)"SuperH BE"},
1620#endif
1621 {EM_ARM40, EM_ARM40, ELFCLASS321, ELFDATA2LSB1, (char*)"ARM"},
1622 // we only support 64 bit z architecture
1623 {EM_S39022, EM_S39022, ELFCLASS642, ELFDATA2MSB2, (char*)"IBM System/390"},
1624 {EM_ALPHA0x9026, EM_ALPHA0x9026, ELFCLASS642, ELFDATA2LSB1, (char*)"Alpha"},
1625 {EM_MIPS_RS3_LE10, EM_MIPS_RS3_LE10, ELFCLASS321, ELFDATA2LSB1, (char*)"MIPSel"},
1626 {EM_MIPS8, EM_MIPS8, ELFCLASS321, ELFDATA2MSB2, (char*)"MIPS"},
1627 {EM_PARISC15, EM_PARISC15, ELFCLASS321, ELFDATA2MSB2, (char*)"PARISC"},
1628 {EM_68K4, EM_68K4, ELFCLASS321, ELFDATA2MSB2, (char*)"M68k"},
1629 {EM_AARCH64183, EM_AARCH64183, ELFCLASS642, ELFDATA2LSB1, (char*)"AARCH64"},
1630 {EM_RISCV243, EM_RISCV243, ELFCLASS642, ELFDATA2LSB1, (char*)"RISC-V"},
1631 {EM_LOONGARCH258, EM_LOONGARCH258, ELFCLASS642, ELFDATA2LSB1, (char*)"LoongArch"},
1632 };
1633
1634#if (defined IA32)
1635 static Elf32_Half running_arch_code=EM_3863;
1636#elif (defined AMD641) || (defined X32)
1637 static Elf32_Half running_arch_code=EM_X86_6462;
1638#elif (defined IA64)
1639 static Elf32_Half running_arch_code=EM_IA_6450;
1640#elif (defined __sparc) && (defined _LP641)
1641 static Elf32_Half running_arch_code=EM_SPARCV943;
1642#elif (defined __sparc) && (!defined _LP641)
1643 static Elf32_Half running_arch_code=EM_SPARC2;
1644#elif (defined __powerpc64__)
1645 static Elf32_Half running_arch_code=EM_PPC6421;
1646#elif (defined __powerpc__)
1647 static Elf32_Half running_arch_code=EM_PPC20;
1648#elif (defined AARCH64)
1649 static Elf32_Half running_arch_code=EM_AARCH64183;
1650#elif (defined ARM)
1651 static Elf32_Half running_arch_code=EM_ARM40;
1652#elif (defined S390)
1653 static Elf32_Half running_arch_code=EM_S39022;
1654#elif (defined ALPHA)
1655 static Elf32_Half running_arch_code=EM_ALPHA0x9026;
1656#elif (defined MIPSEL)
1657 static Elf32_Half running_arch_code=EM_MIPS_RS3_LE10;
1658#elif (defined PARISC)
1659 static Elf32_Half running_arch_code=EM_PARISC15;
1660#elif (defined MIPS)
1661 static Elf32_Half running_arch_code=EM_MIPS8;
1662#elif (defined M68K)
1663 static Elf32_Half running_arch_code=EM_68K4;
1664#elif (defined SH)
1665 static Elf32_Half running_arch_code=EM_SH42;
1666#elif (defined RISCV)
1667 static Elf32_Half running_arch_code=EM_RISCV243;
1668#elif (defined LOONGARCH)
1669 static Elf32_Half running_arch_code=EM_LOONGARCH258;
1670#else
1671 #error Method os::dll_load requires that one of following is defined:\
1672 AARCH64, ALPHA, ARM, AMD641, IA32, IA64, LOONGARCH, M68K, MIPS, MIPSEL, PARISC, __powerpc__, __powerpc64__, RISCV, S390, SH, __sparc
1673#endif
1674
1675 // Identify compatibility class for VM's architecture and library's architecture
1676 // Obtain string descriptions for architectures
1677
1678 arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS4], elf_head.e_ident[EI_DATA5], NULL__null};
1679 int running_arch_index=-1;
1680
1681 for (unsigned int i=0; i < ARRAY_SIZE(arch_array)sizeof(array_size_impl(arch_array)); i++) {
1682 if (running_arch_code == arch_array[i].code) {
1683 running_arch_index = i;
1684 }
1685 if (lib_arch.code == arch_array[i].code) {
1686 lib_arch.compat_class = arch_array[i].compat_class;
1687 lib_arch.name = arch_array[i].name;
1688 }
1689 }
1690
1691 assert(running_arch_index != -1,do { if (!(running_arch_index != -1)) { (*g_assert_poison) = 'X'
;; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1692, "assert(" "running_arch_index != -1" ") failed", "Didn't find running architecture code (running_arch_code) in arch_array"
); ::breakpoint(); } } while (0)
1692 "Didn't find running architecture code (running_arch_code) in arch_array")do { if (!(running_arch_index != -1)) { (*g_assert_poison) = 'X'
;; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 1692, "assert(" "running_arch_index != -1" ") failed", "Didn't find running architecture code (running_arch_code) in arch_array"
); ::breakpoint(); } } while (0)
;
1693 if (running_arch_index == -1) {
1694 // Even though running architecture detection failed
1695 // we may still continue with reporting dlerror() message
1696 return NULL__null;
1697 }
1698
1699 if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
1700 if (lib_arch.name != NULL__null) {
1701 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1702 " (Possible cause: can't load %s .so on a %s platform)",
1703 lib_arch.name, arch_array[running_arch_index].name);
1704 } else {
1705 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1706 " (Possible cause: can't load this .so (machine code=0x%x) on a %s platform)",
1707 lib_arch.code, arch_array[running_arch_index].name);
1708 }
1709 return NULL__null;
1710 }
1711
1712 if (lib_arch.endianness != arch_array[running_arch_index].endianness) {
1713 ::snprintf(diag_msg_buf, diag_msg_max_length-1, " (Possible cause: endianness mismatch)");
1714 return NULL__null;
1715 }
1716
1717 // ELF file class/capacity : 0 - invalid, 1 - 32bit, 2 - 64bit
1718 if (lib_arch.elf_class > 2 || lib_arch.elf_class < 1) {
1719 ::snprintf(diag_msg_buf, diag_msg_max_length-1, " (Possible cause: invalid ELF file class)");
1720 return NULL__null;
1721 }
1722
1723 if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
1724 ::snprintf(diag_msg_buf, diag_msg_max_length-1,
1725 " (Possible cause: architecture word width mismatch, can't load %d-bit .so on a %d-bit platform)",
1726 (int) lib_arch.elf_class * 32, arch_array[running_arch_index].elf_class * 32);
1727 return NULL__null;
1728 }
1729
1730 return NULL__null;
1731}
1732
1733void * os::Linux::dlopen_helper(const char *filename, char *ebuf,
1734 int ebuflen) {
1735 void * result = ::dlopen(filename, RTLD_LAZY0x00001);
1736 if (result == NULL__null) {
1737 const char* error_report = ::dlerror();
1738 if (error_report == NULL__null) {
1739 error_report = "dlerror returned no error description";
1740 }
1741 if (ebuf != NULL__null && ebuflen > 0) {
1742 ::strncpy(ebuf, error_report, ebuflen-1);
1743 ebuf[ebuflen-1]='\0';
1744 }
1745 Events::log(NULL__null, "Loading shared library %s failed, %s", filename, error_report);
1746 log_info(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("shared library load of %s failed, %s", filename, error_report);
1747 } else {
1748 Events::log(NULL__null, "Loaded shared library %s", filename);
1749 log_info(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("shared library load of %s was successful", filename);
1750 }
1751 return result;
1752}
1753
1754void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf,
1755 int ebuflen) {
1756 void * result = NULL__null;
1757 if (LoadExecStackDllInVMThread) {
1758 result = dlopen_helper(filename, ebuf, ebuflen);
1759 }
1760
1761 // Since 7019808, libjvm.so is linked with -noexecstack. If the VM loads a
1762 // library that requires an executable stack, or which does not have this
1763 // stack attribute set, dlopen changes the stack attribute to executable. The
1764 // read protection of the guard pages gets lost.
1765 //
1766 // Need to check _stack_is_executable again as multiple VM_LinuxDllLoad
1767 // may have been queued at the same time.
1768
1769 if (!_stack_is_executable) {
1770 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1771 StackOverflow* overflow_state = jt->stack_overflow_state();
1772 if (!overflow_state->stack_guard_zone_unused() && // Stack not yet fully initialized
1773 overflow_state->stack_guards_enabled()) { // No pending stack overflow exceptions
1774 if (!os::guard_memory((char *)jt->stack_end(), StackOverflow::stack_guard_zone_size())) {
1775 warning("Attempt to reguard stack yellow zone failed.");
1776 }
1777 }
1778 }
1779 }
1780
1781 return result;
1782}
1783
1784static bool _print_ascii_file(const char* filename, outputStream* st, const char* hdr = NULL__null) {
1785 int fd = ::open(filename, O_RDONLY00);
1786 if (fd == -1) {
1787 return false;
1788 }
1789
1790 if (hdr != NULL__null) {
1791 st->print_cr("%s", hdr);
1792 }
1793
1794 char buf[33];
1795 int bytes;
1796 buf[32] = '\0';
1797 while ((bytes = ::read(fd, buf, sizeof(buf)-1)) > 0) {
1798 st->print_raw(buf, bytes);
1799 }
1800
1801 ::close(fd);
1802
1803 return true;
1804}
1805
1806static void _print_ascii_file_h(const char* header, const char* filename, outputStream* st, bool same_line = true) {
1807 st->print("%s:%c", header, same_line ? ' ' : '\n');
1808 if (!_print_ascii_file(filename, st)) {
1809 st->print_cr("<Not Available>");
1810 }
1811}
1812
1813void os::print_dll_info(outputStream *st) {
1814 st->print_cr("Dynamic libraries:");
1815
1816 char fname[32];
1817 pid_t pid = os::Linux::gettid();
1818
1819 jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);
1820
1821 if (!_print_ascii_file(fname, st)) {
1822 st->print_cr("Can not get library information for pid = %d", pid);
1823 }
1824}
1825
1826struct loaded_modules_info_param {
1827 os::LoadedModulesCallbackFunc callback;
1828 void *param;
1829};
1830
1831static int dl_iterate_callback(struct dl_phdr_info *info, size_t size, void *data) {
1832 if ((info->dlpi_name == NULL__null) || (*info->dlpi_name == '\0')) {
1833 return 0;
1834 }
1835
1836 struct loaded_modules_info_param *callback_param = reinterpret_cast<struct loaded_modules_info_param *>(data);
1837 address base = NULL__null;
1838 address top = NULL__null;
1839 for (int idx = 0; idx < info->dlpi_phnum; idx++) {
1840 const ElfW(Phdr)Elf64_Phdr *phdr = info->dlpi_phdr + idx;
1841 if (phdr->p_type == PT_LOAD1) {
1842 address raw_phdr_base = reinterpret_cast<address>(info->dlpi_addr + phdr->p_vaddr);
1843
1844 address phdr_base = align_down(raw_phdr_base, phdr->p_align);
1845 if ((base == NULL__null) || (base > phdr_base)) {
1846 base = phdr_base;
1847 }
1848
1849 address phdr_top = align_up(raw_phdr_base + phdr->p_memsz, phdr->p_align);
1850 if ((top == NULL__null) || (top < phdr_top)) {
1851 top = phdr_top;
1852 }
1853 }
1854 }
1855
1856 return callback_param->callback(info->dlpi_name, base, top, callback_param->param);
1857}
1858
1859int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {
1860 struct loaded_modules_info_param callback_param = {callback, param};
1861 return dl_iterate_phdr(&dl_iterate_callback, &callback_param);
1862}
1863
1864void os::print_os_info_brief(outputStream* st) {
1865 os::Linux::print_distro_info(st);
1866
1867 os::Posix::print_uname_info(st);
1868
1869 os::Linux::print_libversion_info(st);
1870
1871}
1872
1873void os::print_os_info(outputStream* st) {
1874 st->print_cr("OS:");
1875
1876 os::Linux::print_distro_info(st);
1877
1878 os::Posix::print_uname_info(st);
1879
1880 os::Linux::print_uptime_info(st);
1881
1882 // Print warning if unsafe chroot environment detected
1883 if (unsafe_chroot_detected) {
1884 st->print_cr("WARNING!! %s", unstable_chroot_error);
1885 }
1886
1887 os::Linux::print_libversion_info(st);
1888
1889 os::Posix::print_rlimit_info(st);
1890
1891 os::Posix::print_load_average(st);
1892 st->cr();
1893
1894 os::Linux::print_system_memory_info(st);
1895 st->cr();
1896
1897 os::Linux::print_process_memory_info(st);
1898 st->cr();
1899
1900 os::Linux::print_proc_sys_info(st);
1901 st->cr();
1902
1903 if (os::Linux::print_ld_preload_file(st)) {
1904 st->cr();
1905 }
1906
1907 if (os::Linux::print_container_info(st)) {
1908 st->cr();
1909 }
1910
1911 VM_Version::print_platform_virtualization_info(st);
1912
1913 os::Linux::print_steal_info(st);
1914}
1915
1916// Try to identify popular distros.
1917// Most Linux distributions have a /etc/XXX-release file, which contains
1918// the OS version string. Newer Linux distributions have a /etc/lsb-release
1919// file that also contains the OS version string. Some have more than one
1920// /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and
1921// /etc/redhat-release.), so the order is important.
1922// Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have
1923// their own specific XXX-release file as well as a redhat-release file.
1924// Because of this the XXX-release file needs to be searched for before the
1925// redhat-release file.
1926// Since Red Hat and SuSE have an lsb-release file that is not very descriptive the
1927// search for redhat-release / SuSE-release needs to be before lsb-release.
1928// Since the lsb-release file is the new standard it needs to be searched
1929// before the older style release files.
1930// Searching system-release (Red Hat) and os-release (other Linuxes) are a
1931// next to last resort. The os-release file is a new standard that contains
1932// distribution information and the system-release file seems to be an old
1933// standard that has been replaced by the lsb-release and os-release files.
1934// Searching for the debian_version file is the last resort. It contains
1935// an informative string like "6.0.6" or "wheezy/sid". Because of this
1936// "Debian " is printed before the contents of the debian_version file.
1937
1938const char* distro_files[] = {
1939 "/etc/oracle-release",
1940 "/etc/mandriva-release",
1941 "/etc/mandrake-release",
1942 "/etc/sun-release",
1943 "/etc/redhat-release",
1944 "/etc/SuSE-release",
1945 "/etc/lsb-release",
1946 "/etc/turbolinux-release",
1947 "/etc/gentoo-release",
1948 "/etc/ltib-release",
1949 "/etc/angstrom-version",
1950 "/etc/system-release",
1951 "/etc/os-release",
1952 NULL__null };
1953
1954void os::Linux::print_distro_info(outputStream* st) {
1955 for (int i = 0;; i++) {
1956 const char* file = distro_files[i];
1957 if (file == NULL__null) {
1958 break; // done
1959 }
1960 // If file prints, we found it.
1961 if (_print_ascii_file(file, st)) {
1962 return;
1963 }
1964 }
1965
1966 if (file_exists("/etc/debian_version")) {
1967 st->print("Debian ");
1968 _print_ascii_file("/etc/debian_version", st);
1969 } else {
1970 st->print_cr("Linux");
1971 }
1972}
1973
1974static void parse_os_info_helper(FILE* fp, char* distro, size_t length, bool get_first_line) {
1975 char buf[256];
1976 while (fgets(buf, sizeof(buf), fp)) {
1977 // Edit out extra stuff in expected format
1978 if (strstr(buf, "DISTRIB_DESCRIPTION=") != NULL__null || strstr(buf, "PRETTY_NAME=") != NULL__null) {
1979 char* ptr = strstr(buf, "\""); // the name is in quotes
1980 if (ptr != NULL__null) {
1981 ptr++; // go beyond first quote
1982 char* nl = strchr(ptr, '\"');
1983 if (nl != NULL__null) *nl = '\0';
1984 strncpy(distro, ptr, length);
1985 } else {
1986 ptr = strstr(buf, "=");
1987 ptr++; // go beyond equals then
1988 char* nl = strchr(ptr, '\n');
1989 if (nl != NULL__null) *nl = '\0';
1990 strncpy(distro, ptr, length);
1991 }
1992 return;
1993 } else if (get_first_line) {
1994 char* nl = strchr(buf, '\n');
1995 if (nl != NULL__null) *nl = '\0';
1996 strncpy(distro, buf, length);
1997 return;
1998 }
1999 }
2000 // print last line and close
2001 char* nl = strchr(buf, '\n');
2002 if (nl != NULL__null) *nl = '\0';
2003 strncpy(distro, buf, length);
2004}
2005
2006static void parse_os_info(char* distro, size_t length, const char* file) {
2007 FILE* fp = fopen(file, "r");
2008 if (fp != NULL__null) {
2009 // if suse format, print out first line
2010 bool get_first_line = (strcmp(file, "/etc/SuSE-release") == 0);
2011 parse_os_info_helper(fp, distro, length, get_first_line);
2012 fclose(fp);
2013 }
2014}
2015
2016void os::get_summary_os_info(char* buf, size_t buflen) {
2017 for (int i = 0;; i++) {
2018 const char* file = distro_files[i];
2019 if (file == NULL__null) {
2020 break; // ran out of distro_files
2021 }
2022 if (file_exists(file)) {
2023 parse_os_info(buf, buflen, file);
2024 return;
2025 }
2026 }
2027 // special case for debian
2028 if (file_exists("/etc/debian_version")) {
2029 strncpy(buf, "Debian ", buflen);
2030 if (buflen > 7) {
2031 parse_os_info(&buf[7], buflen-7, "/etc/debian_version");
2032 }
2033 } else {
2034 strncpy(buf, "Linux", buflen);
2035 }
2036}
2037
2038void os::Linux::print_libversion_info(outputStream* st) {
2039 // libc, pthread
2040 st->print("libc: ");
2041 st->print("%s ", os::Linux::libc_version());
2042 st->print("%s ", os::Linux::libpthread_version());
2043 st->cr();
2044}
2045
2046void os::Linux::print_proc_sys_info(outputStream* st) {
2047 _print_ascii_file_h("/proc/sys/kernel/threads-max (system-wide limit on the number of threads)",
2048 "/proc/sys/kernel/threads-max", st);
2049 _print_ascii_file_h("/proc/sys/vm/max_map_count (maximum number of memory map areas a process may have)",
2050 "/proc/sys/vm/max_map_count", st);
2051 _print_ascii_file_h("/proc/sys/kernel/pid_max (system-wide limit on number of process identifiers)",
2052 "/proc/sys/kernel/pid_max", st);
2053}
2054
2055void os::Linux::print_system_memory_info(outputStream* st) {
2056 _print_ascii_file_h("/proc/meminfo", "/proc/meminfo", st, false);
2057 st->cr();
2058
2059 // some information regarding THPs; for details see
2060 // https://www.kernel.org/doc/Documentation/vm/transhuge.txt
2061 _print_ascii_file_h("/sys/kernel/mm/transparent_hugepage/enabled",
2062 "/sys/kernel/mm/transparent_hugepage/enabled", st);
2063 _print_ascii_file_h("/sys/kernel/mm/transparent_hugepage/defrag (defrag/compaction efforts parameter)",
2064 "/sys/kernel/mm/transparent_hugepage/defrag", st);
2065}
2066
2067bool os::Linux::query_process_memory_info(os::Linux::meminfo_t* info) {
2068 FILE* f = ::fopen("/proc/self/status", "r");
2069 const int num_values = sizeof(os::Linux::meminfo_t) / sizeof(size_t);
2070 int num_found = 0;
2071 char buf[256];
2072 info->vmsize = info->vmpeak = info->vmrss = info->vmhwm = info->vmswap =
2073 info->rssanon = info->rssfile = info->rssshmem = -1;
2074 if (f != NULL__null) {
2075 while (::fgets(buf, sizeof(buf), f) != NULL__null && num_found < num_values) {
2076 if ( (info->vmsize == -1 && sscanf(buf, "VmSize: " SSIZE_FORMAT"%" "l" "d" " kB", &info->vmsize) == 1) ||
2077 (info->vmpeak == -1 && sscanf(buf, "VmPeak: " SSIZE_FORMAT"%" "l" "d" " kB", &info->vmpeak) == 1) ||
2078 (info->vmswap == -1 && sscanf(buf, "VmSwap: " SSIZE_FORMAT"%" "l" "d" " kB", &info->vmswap) == 1) ||
2079 (info->vmhwm == -1 && sscanf(buf, "VmHWM: " SSIZE_FORMAT"%" "l" "d" " kB", &info->vmhwm) == 1) ||
2080 (info->vmrss == -1 && sscanf(buf, "VmRSS: " SSIZE_FORMAT"%" "l" "d" " kB", &info->vmrss) == 1) ||
2081 (info->rssanon == -1 && sscanf(buf, "RssAnon: " SSIZE_FORMAT"%" "l" "d" " kB", &info->rssanon) == 1) || // Needs Linux 4.5
2082 (info->rssfile == -1 && sscanf(buf, "RssFile: " SSIZE_FORMAT"%" "l" "d" " kB", &info->rssfile) == 1) || // Needs Linux 4.5
2083 (info->rssshmem == -1 && sscanf(buf, "RssShmem: " SSIZE_FORMAT"%" "l" "d" " kB", &info->rssshmem) == 1) // Needs Linux 4.5
2084 )
2085 {
2086 num_found ++;
2087 }
2088 }
2089 fclose(f);
2090 return true;
2091 }
2092 return false;
2093}
2094
2095void os::Linux::print_process_memory_info(outputStream* st) {
2096
2097 st->print_cr("Process Memory:");
2098
2099 // Print virtual and resident set size; peak values; swap; and for
2100 // rss its components if the kernel is recent enough.
2101 meminfo_t info;
2102 if (query_process_memory_info(&info)) {
2103 st->print_cr("Virtual Size: " SSIZE_FORMAT"%" "l" "d" "K (peak: " SSIZE_FORMAT"%" "l" "d" "K)", info.vmsize, info.vmpeak);
2104 st->print("Resident Set Size: " SSIZE_FORMAT"%" "l" "d" "K (peak: " SSIZE_FORMAT"%" "l" "d" "K)", info.vmrss, info.vmhwm);
2105 if (info.rssanon != -1) { // requires kernel >= 4.5
2106 st->print(" (anon: " SSIZE_FORMAT"%" "l" "d" "K, file: " SSIZE_FORMAT"%" "l" "d" "K, shmem: " SSIZE_FORMAT"%" "l" "d" "K)",
2107 info.rssanon, info.rssfile, info.rssshmem);
2108 }
2109 st->cr();
2110 if (info.vmswap != -1) { // requires kernel >= 2.6.34
2111 st->print_cr("Swapped out: " SSIZE_FORMAT"%" "l" "d" "K", info.vmswap);
2112 }
2113 } else {
2114 st->print_cr("Could not open /proc/self/status to get process memory related information");
2115 }
2116
2117 // Print glibc outstanding allocations.
2118 // (note: there is no implementation of mallinfo for muslc)
2119#ifdef __GLIBC__2
2120 size_t total_allocated = 0;
2121 bool might_have_wrapped = false;
2122 if (_mallinfo2 != NULL__null) {
2123 struct glibc_mallinfo2 mi = _mallinfo2();
2124 total_allocated = mi.uordblks;
2125 } else if (_mallinfo != NULL__null) {
2126 // mallinfo is an old API. Member names mean next to nothing and, beyond that, are int.
2127 // So values may have wrapped around. Still useful enough to see how much glibc thinks
2128 // we allocated.
2129 struct glibc_mallinfo mi = _mallinfo();
2130 total_allocated = (size_t)(unsigned)mi.uordblks;
2131 // Since mallinfo members are int, glibc values may have wrapped. Warn about this.
2132 might_have_wrapped = (info.vmrss * K) > UINT_MAX(2147483647 *2U +1U) && (info.vmrss * K) > (total_allocated + UINT_MAX(2147483647 *2U +1U));
2133 }
2134 if (_mallinfo2 != NULL__null || _mallinfo != NULL__null) {
2135 st->print_cr("C-Heap outstanding allocations: " SIZE_FORMAT"%" "l" "u" "K%s",
2136 total_allocated / K,
2137 might_have_wrapped ? " (may have wrapped)" : "");
2138 }
2139#endif // __GLIBC__
2140
2141}
2142
2143bool os::Linux::print_ld_preload_file(outputStream* st) {
2144 return _print_ascii_file("/etc/ld.so.preload", st, "/etc/ld.so.preload:");
2145}
2146
2147void os::Linux::print_uptime_info(outputStream* st) {
2148 struct sysinfo sinfo;
2149 int ret = sysinfo(&sinfo);
2150 if (ret == 0) {
2151 os::print_dhm(st, "OS uptime:", (long) sinfo.uptime);
2152 }
2153}
2154
2155bool os::Linux::print_container_info(outputStream* st) {
2156 if (!OSContainer::is_containerized()) {
2157 st->print_cr("container information not found.");
2158 return false;
2159 }
2160
2161 st->print_cr("container (cgroup) information:");
2162
2163 const char *p_ct = OSContainer::container_type();
2164 st->print_cr("container_type: %s", p_ct != NULL__null ? p_ct : "not supported");
2165
2166 char *p = OSContainer::cpu_cpuset_cpus();
2167 st->print_cr("cpu_cpuset_cpus: %s", p != NULL__null ? p : "not supported");
2168 free(p);
2169
2170 p = OSContainer::cpu_cpuset_memory_nodes();
2171 st->print_cr("cpu_memory_nodes: %s", p != NULL__null ? p : "not supported");
2172 free(p);
2173
2174 int i = OSContainer::active_processor_count();
2175 st->print("active_processor_count: ");
2176 if (i > 0) {
2177 st->print_cr("%d", i);
2178 } else {
2179 st->print_cr("not supported");
2180 }
2181
2182 i = OSContainer::cpu_quota();
2183 st->print("cpu_quota: ");
2184 if (i > 0) {
2185 st->print_cr("%d", i);
2186 } else {
2187 st->print_cr("%s", i == OSCONTAINER_ERROR(-2) ? "not supported" : "no quota");
2188 }
2189
2190 i = OSContainer::cpu_period();
2191 st->print("cpu_period: ");
2192 if (i > 0) {
2193 st->print_cr("%d", i);
2194 } else {
2195 st->print_cr("%s", i == OSCONTAINER_ERROR(-2) ? "not supported" : "no period");
2196 }
2197
2198 i = OSContainer::cpu_shares();
2199 st->print("cpu_shares: ");
2200 if (i > 0) {
2201 st->print_cr("%d", i);
2202 } else {
2203 st->print_cr("%s", i == OSCONTAINER_ERROR(-2) ? "not supported" : "no shares");
2204 }
2205
2206 jlong j = OSContainer::memory_limit_in_bytes();
2207 st->print("memory_limit_in_bytes: ");
2208 if (j > 0) {
2209 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2210 } else {
2211 st->print_cr("%s", j == OSCONTAINER_ERROR(-2) ? "not supported" : "unlimited");
2212 }
2213
2214 j = OSContainer::memory_and_swap_limit_in_bytes();
2215 st->print("memory_and_swap_limit_in_bytes: ");
2216 if (j > 0) {
2217 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2218 } else {
2219 st->print_cr("%s", j == OSCONTAINER_ERROR(-2) ? "not supported" : "unlimited");
2220 }
2221
2222 j = OSContainer::memory_soft_limit_in_bytes();
2223 st->print("memory_soft_limit_in_bytes: ");
2224 if (j > 0) {
2225 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2226 } else {
2227 st->print_cr("%s", j == OSCONTAINER_ERROR(-2) ? "not supported" : "unlimited");
2228 }
2229
2230 j = OSContainer::OSContainer::memory_usage_in_bytes();
2231 st->print("memory_usage_in_bytes: ");
2232 if (j > 0) {
2233 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2234 } else {
2235 st->print_cr("%s", j == OSCONTAINER_ERROR(-2) ? "not supported" : "unlimited");
2236 }
2237
2238 j = OSContainer::OSContainer::memory_max_usage_in_bytes();
2239 st->print("memory_max_usage_in_bytes: ");
2240 if (j > 0) {
2241 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2242 } else {
2243 st->print_cr("%s", j == OSCONTAINER_ERROR(-2) ? "not supported" : "unlimited");
2244 }
2245
2246 j = OSContainer::OSContainer::pids_max();
2247 st->print("maximum number of tasks: ");
2248 if (j > 0) {
2249 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2250 } else {
2251 st->print_cr("%s", j == OSCONTAINER_ERROR(-2) ? "not supported" : "unlimited");
2252 }
2253
2254 j = OSContainer::OSContainer::pids_current();
2255 st->print("current number of tasks: ");
2256 if (j > 0) {
2257 st->print_cr(JLONG_FORMAT"%" "l" "d", j);
2258 } else {
2259 if (j == OSCONTAINER_ERROR(-2)) {
2260 st->print_cr("not supported");
2261 }
2262 }
2263
2264 return true;
2265}
2266
2267void os::Linux::print_steal_info(outputStream* st) {
2268 if (has_initial_tick_info) {
2269 CPUPerfTicks pticks;
2270 bool res = os::Linux::get_tick_information(&pticks, -1);
2271
2272 if (res && pticks.has_steal_ticks) {
2273 uint64_t steal_ticks_difference = pticks.steal - initial_steal_ticks;
2274 uint64_t total_ticks_difference = pticks.total - initial_total_ticks;
2275 double steal_ticks_perc = 0.0;
2276 if (total_ticks_difference != 0) {
2277 steal_ticks_perc = (double) steal_ticks_difference / total_ticks_difference;
2278 }
2279 st->print_cr("Steal ticks since vm start: " UINT64_FORMAT"%" "l" "u", steal_ticks_difference);
2280 st->print_cr("Steal ticks percentage since vm start:%7.3f", steal_ticks_perc);
2281 }
2282 }
2283}
2284
2285void os::print_memory_info(outputStream* st) {
2286
2287 st->print("Memory:");
2288 st->print(" %dk page", os::vm_page_size()>>10);
2289
2290 // values in struct sysinfo are "unsigned long"
2291 struct sysinfo si;
2292 sysinfo(&si);
2293
2294 st->print(", physical " UINT64_FORMAT"%" "l" "u" "k",
2295 os::physical_memory() >> 10);
2296 st->print("(" UINT64_FORMAT"%" "l" "u" "k free)",
2297 os::available_memory() >> 10);
2298 st->print(", swap " UINT64_FORMAT"%" "l" "u" "k",
2299 ((jlong)si.totalswap * si.mem_unit) >> 10);
2300 st->print("(" UINT64_FORMAT"%" "l" "u" "k free)",
2301 ((jlong)si.freeswap * si.mem_unit) >> 10);
2302 st->cr();
2303 st->print("Page Sizes: ");
2304 _page_sizes.print_on(st);
2305 st->cr();
2306}
2307
2308// Print the first "model name" line and the first "flags" line
2309// that we find and nothing more. We assume "model name" comes
2310// before "flags" so if we find a second "model name", then the
2311// "flags" field is considered missing.
2312static bool print_model_name_and_flags(outputStream* st, char* buf, size_t buflen) {
2313#if defined(IA32) || defined(AMD641)
2314 // Other platforms have less repetitive cpuinfo files
2315 FILE *fp = fopen("/proc/cpuinfo", "r");
2316 if (fp) {
2317 bool model_name_printed = false;
2318 while (!feof(fp)) {
2319 if (fgets(buf, buflen, fp)) {
2320 // Assume model name comes before flags
2321 if (strstr(buf, "model name") != NULL__null) {
2322 if (!model_name_printed) {
2323 st->print_raw("CPU Model and flags from /proc/cpuinfo:\n");
2324 st->print_raw(buf);
2325 model_name_printed = true;
2326 } else {
2327 // model name printed but not flags? Odd, just return
2328 fclose(fp);
2329 return true;
2330 }
2331 }
2332 // print the flags line too
2333 if (strstr(buf, "flags") != NULL__null) {
2334 st->print_raw(buf);
2335 fclose(fp);
2336 return true;
2337 }
2338 }
2339 }
2340 fclose(fp);
2341 }
2342#endif // x86 platforms
2343 return false;
2344}
2345
2346// additional information about CPU e.g. available frequency ranges
2347static void print_sys_devices_cpu_info(outputStream* st, char* buf, size_t buflen) {
2348 _print_ascii_file_h("Online cpus", "/sys/devices/system/cpu/online", st);
2349 _print_ascii_file_h("Offline cpus", "/sys/devices/system/cpu/offline", st);
2350
2351 if (ExtensiveErrorReports) {
2352 // cache related info (cpu 0, should be similar for other CPUs)
2353 for (unsigned int i=0; i < 10; i++) { // handle max. 10 cache entries
2354 char hbuf_level[60];
2355 char hbuf_type[60];
2356 char hbuf_size[60];
2357 char hbuf_coherency_line_size[80];
2358 snprintf(hbuf_level, 60, "/sys/devices/system/cpu/cpu0/cache/index%u/level", i);
2359 snprintf(hbuf_type, 60, "/sys/devices/system/cpu/cpu0/cache/index%u/type", i);
2360 snprintf(hbuf_size, 60, "/sys/devices/system/cpu/cpu0/cache/index%u/size", i);
2361 snprintf(hbuf_coherency_line_size, 80, "/sys/devices/system/cpu/cpu0/cache/index%u/coherency_line_size", i);
2362 if (os::file_exists(hbuf_level)) {
2363 _print_ascii_file_h("cache level", hbuf_level, st);
2364 _print_ascii_file_h("cache type", hbuf_type, st);
2365 _print_ascii_file_h("cache size", hbuf_size, st);
2366 _print_ascii_file_h("cache coherency line size", hbuf_coherency_line_size, st);
2367 }
2368 }
2369 }
2370
2371 // we miss the cpufreq entries on Power and s390x
2372#if defined(IA32) || defined(AMD641)
2373 _print_ascii_file_h("BIOS frequency limitation", "/sys/devices/system/cpu/cpu0/cpufreq/bios_limit", st);
2374 _print_ascii_file_h("Frequency switch latency (ns)", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_transition_latency", st);
2375 _print_ascii_file_h("Available cpu frequencies", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies", st);
2376 // min and max should be in the Available range but still print them (not all info might be available for all kernels)
2377 if (ExtensiveErrorReports) {
2378 _print_ascii_file_h("Maximum cpu frequency", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", st);
2379 _print_ascii_file_h("Minimum cpu frequency", "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq", st);
2380 _print_ascii_file_h("Current cpu frequency", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", st);
2381 }
2382 // governors are power schemes, see https://wiki.archlinux.org/index.php/CPU_frequency_scaling
2383 if (ExtensiveErrorReports) {
2384 _print_ascii_file_h("Available governors", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", st);
2385 }
2386 _print_ascii_file_h("Current governor", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", st);
2387 // Core performance boost, see https://www.kernel.org/doc/Documentation/cpu-freq/boost.txt
2388 // Raise operating frequency of some cores in a multi-core package if certain conditions apply, e.g.
2389 // whole chip is not fully utilized
2390 _print_ascii_file_h("Core performance/turbo boost", "/sys/devices/system/cpu/cpufreq/boost", st);
2391#endif
2392}
2393
2394void os::pd_print_cpu_info(outputStream* st, char* buf, size_t buflen) {
2395 // Only print the model name if the platform provides this as a summary
2396 if (!print_model_name_and_flags(st, buf, buflen)) {
2397 _print_ascii_file_h("/proc/cpuinfo", "/proc/cpuinfo", st, false);
2398 }
2399 st->cr();
2400 print_sys_devices_cpu_info(st, buf, buflen);
2401}
2402
2403#if defined(AMD641) || defined(IA32) || defined(X32)
2404const char* search_string = "model name";
2405#elif defined(M68K)
2406const char* search_string = "CPU";
2407#elif defined(PPC64)
2408const char* search_string = "cpu";
2409#elif defined(S390)
2410const char* search_string = "machine =";
2411#elif defined(SPARC)
2412const char* search_string = "cpu";
2413#else
2414const char* search_string = "Processor";
2415#endif
2416
2417// Parses the cpuinfo file for string representing the model name.
2418void os::get_summary_cpu_info(char* cpuinfo, size_t length) {
2419 FILE* fp = fopen("/proc/cpuinfo", "r");
2420 if (fp != NULL__null) {
2421 while (!feof(fp)) {
2422 char buf[256];
2423 if (fgets(buf, sizeof(buf), fp)) {
2424 char* start = strstr(buf, search_string);
2425 if (start != NULL__null) {
2426 char *ptr = start + strlen(search_string);
2427 char *end = buf + strlen(buf);
2428 while (ptr != end) {
2429 // skip whitespace and colon for the rest of the name.
2430 if (*ptr != ' ' && *ptr != '\t' && *ptr != ':') {
2431 break;
2432 }
2433 ptr++;
2434 }
2435 if (ptr != end) {
2436 // reasonable string, get rid of newline and keep the rest
2437 char* nl = strchr(buf, '\n');
2438 if (nl != NULL__null) *nl = '\0';
2439 strncpy(cpuinfo, ptr, length);
2440 fclose(fp);
2441 return;
2442 }
2443 }
2444 }
2445 }
2446 fclose(fp);
2447 }
2448 // cpuinfo not found or parsing failed, just print generic string. The entire
2449 // /proc/cpuinfo file will be printed later in the file (or enough of it for x86)
2450#if defined(AARCH64)
2451 strncpy(cpuinfo, "AArch64", length);
2452#elif defined(AMD641)
2453 strncpy(cpuinfo, "x86_64", length);
2454#elif defined(ARM) // Order wrt. AARCH64 is relevant!
2455 strncpy(cpuinfo, "ARM", length);
2456#elif defined(IA32)
2457 strncpy(cpuinfo, "x86_32", length);
2458#elif defined(IA64)
2459 strncpy(cpuinfo, "IA64", length);
2460#elif defined(PPC)
2461 strncpy(cpuinfo, "PPC64", length);
2462#elif defined(S390)
2463 strncpy(cpuinfo, "S390", length);
2464#elif defined(SPARC)
2465 strncpy(cpuinfo, "sparcv9", length);
2466#elif defined(ZERO_LIBARCH)
2467 strncpy(cpuinfo, ZERO_LIBARCH, length);
2468#else
2469 strncpy(cpuinfo, "unknown", length);
2470#endif
2471}
2472
2473static char saved_jvm_path[MAXPATHLEN4096] = {0};
2474
2475// Find the full path to the current module, libjvm.so
2476void os::jvm_path(char *buf, jint buflen) {
2477 // Error checking.
2478 if (buflen < MAXPATHLEN4096) {
2479 assert(false, "must use a large-enough buffer")do { if (!(false)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2479, "assert(" "false" ") failed", "must use a large-enough buffer"
); ::breakpoint(); } } while (0)
;
2480 buf[0] = '\0';
2481 return;
2482 }
2483 // Lazy resolve the path to current module.
2484 if (saved_jvm_path[0] != 0) {
2485 strcpy(buf, saved_jvm_path);
2486 return;
2487 }
2488
2489 char dli_fname[MAXPATHLEN4096];
2490 dli_fname[0] = '\0';
2491 bool ret = dll_address_to_library_name(
2492 CAST_FROM_FN_PTR(address, os::jvm_path)((address)((address_word)(os::jvm_path))),
2493 dli_fname, sizeof(dli_fname), NULL__null);
2494 assert(ret, "cannot locate libjvm")do { if (!(ret)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2494, "assert(" "ret" ") failed", "cannot locate libjvm"); ::
breakpoint(); } } while (0)
;
2495 char *rp = NULL__null;
2496 if (ret && dli_fname[0] != '\0') {
2497 rp = os::Posix::realpath(dli_fname, buf, buflen);
2498 }
2499 if (rp == NULL__null) {
2500 return;
2501 }
2502
2503 if (Arguments::sun_java_launcher_is_altjvm()) {
2504 // Support for the java launcher's '-XXaltjvm=<path>' option. Typical
2505 // value for buf is "<JAVA_HOME>/jre/lib/<vmtype>/libjvm.so".
2506 // If "/jre/lib/" appears at the right place in the string, then
2507 // assume we are installed in a JDK and we're done. Otherwise, check
2508 // for a JAVA_HOME environment variable and fix up the path so it
2509 // looks like libjvm.so is installed there (append a fake suffix
2510 // hotspot/libjvm.so).
2511 const char *p = buf + strlen(buf) - 1;
2512 for (int count = 0; p > buf && count < 5; ++count) {
2513 for (--p; p > buf && *p != '/'; --p)
2514 /* empty */ ;
2515 }
2516
2517 if (strncmp(p, "/jre/lib/", 9) != 0) {
2518 // Look for JAVA_HOME in the environment.
2519 char* java_home_var = ::getenv("JAVA_HOME");
2520 if (java_home_var != NULL__null && java_home_var[0] != 0) {
2521 char* jrelib_p;
2522 int len;
2523
2524 // Check the current module name "libjvm.so".
2525 p = strrchr(buf, '/');
2526 if (p == NULL__null) {
2527 return;
2528 }
2529 assert(strstr(p, "/libjvm") == p, "invalid library name")do { if (!(strstr(p, "/libjvm") == p)) { (*g_assert_poison) =
'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2529, "assert(" "strstr(p, \"/libjvm\") == p" ") failed", "invalid library name"
); ::breakpoint(); } } while (0)
;
2530
2531 rp = os::Posix::realpath(java_home_var, buf, buflen);
2532 if (rp == NULL__null) {
2533 return;
2534 }
2535
2536 // determine if this is a legacy image or modules image
2537 // modules image doesn't have "jre" subdirectory
2538 len = strlen(buf);
2539 assert(len < buflen, "Ran out of buffer room")do { if (!(len < buflen)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2539, "assert(" "len < buflen" ") failed", "Ran out of buffer room"
); ::breakpoint(); } } while (0)
;
2540 jrelib_p = buf + len;
2541 snprintf(jrelib_p, buflen-len, "/jre/lib");
2542 if (0 != access(buf, F_OK0)) {
2543 snprintf(jrelib_p, buflen-len, "/lib");
2544 }
2545
2546 if (0 == access(buf, F_OK0)) {
2547 // Use current module name "libjvm.so"
2548 len = strlen(buf);
2549 snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");
2550 } else {
2551 // Go back to path of .so
2552 rp = os::Posix::realpath(dli_fname, buf, buflen);
2553 if (rp == NULL__null) {
2554 return;
2555 }
2556 }
2557 }
2558 }
2559 }
2560
2561 strncpy(saved_jvm_path, buf, MAXPATHLEN4096);
2562 saved_jvm_path[MAXPATHLEN4096 - 1] = '\0';
2563}
2564
2565void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
2566 // no prefix required, not even "_"
2567}
2568
2569void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
2570 // no suffix required
2571}
2572
2573////////////////////////////////////////////////////////////////////////////////
2574// Virtual Memory
2575
2576int os::vm_page_size() {
2577 // Seems redundant as all get out
2578 assert(os::Linux::page_size() != -1, "must call os::init")do { if (!(os::Linux::page_size() != -1)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2578, "assert(" "os::Linux::page_size() != -1" ") failed", "must call os::init"
); ::breakpoint(); } } while (0)
;
2579 return os::Linux::page_size();
2580}
2581
2582// Solaris allocates memory by pages.
2583int os::vm_allocation_granularity() {
2584 assert(os::Linux::page_size() != -1, "must call os::init")do { if (!(os::Linux::page_size() != -1)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2584, "assert(" "os::Linux::page_size() != -1" ") failed", "must call os::init"
); ::breakpoint(); } } while (0)
;
2585 return os::Linux::page_size();
2586}
2587
2588// Rationale behind this function:
2589// current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable
2590// mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get
2591// samples for JITted code. Here we create private executable mapping over the code cache
2592// and then we can use standard (well, almost, as mapping can change) way to provide
2593// info for the reporting script by storing timestamp and location of symbol
2594void linux_wrap_code(char* base, size_t size) {
2595 static volatile jint cnt = 0;
2596
2597 if (!UseOprofile) {
2598 return;
2599 }
2600
2601 char buf[PATH_MAX4096+1];
2602 int num = Atomic::add(&cnt, 1);
2603
2604 snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d",
2605 os::get_temp_directory(), os::current_process_id(), num);
2606 unlink(buf);
2607
2608 int fd = ::open(buf, O_CREAT0100 | O_RDWR02, S_IRWXU(0400|0200|0100));
2609
2610 if (fd != -1) {
2611 off_t rv = ::lseek(fd, size-2, SEEK_SET0);
2612 if (rv != (off_t)-1) {
2613 if (::write(fd, "", 1) == 1) {
2614 mmap(base, size,
2615 PROT_READ0x1|PROT_WRITE0x2|PROT_EXEC0x4,
2616 MAP_PRIVATE0x02|MAP_FIXED0x10|MAP_NORESERVE0x04000, fd, 0);
2617 }
2618 }
2619 ::close(fd);
2620 unlink(buf);
2621 }
2622}
2623
2624static bool recoverable_mmap_error(int err) {
2625 // See if the error is one we can let the caller handle. This
2626 // list of errno values comes from JBS-6843484. I can't find a
2627 // Linux man page that documents this specific set of errno
2628 // values so while this list currently matches Solaris, it may
2629 // change as we gain experience with this failure mode.
2630 switch (err) {
2631 case EBADF9:
2632 case EINVAL22:
2633 case ENOTSUP95:
2634 // let the caller deal with these errors
2635 return true;
2636
2637 default:
2638 // Any remaining errors on this OS can cause our reserved mapping
2639 // to be lost. That can cause confusion where different data
2640 // structures think they have the same memory mapped. The worst
2641 // scenario is if both the VM and a library think they have the
2642 // same memory mapped.
2643 return false;
2644 }
2645}
2646
2647static void warn_fail_commit_memory(char* addr, size_t size, bool exec,
2648 int err) {
2649 warning("INFO: os::commit_memory(" PTR_FORMAT"0x%016" "l" "x" ", " SIZE_FORMAT"%" "l" "u"
2650 ", %d) failed; error='%s' (errno=%d)", p2i(addr), size, exec,
2651 os::strerror(err), err);
2652}
2653
2654static void warn_fail_commit_memory(char* addr, size_t size,
2655 size_t alignment_hint, bool exec,
2656 int err) {
2657 warning("INFO: os::commit_memory(" PTR_FORMAT"0x%016" "l" "x" ", " SIZE_FORMAT"%" "l" "u"
2658 ", " SIZE_FORMAT"%" "l" "u" ", %d) failed; error='%s' (errno=%d)", p2i(addr), size,
2659 alignment_hint, exec, os::strerror(err), err);
2660}
2661
2662// NOTE: Linux kernel does not really reserve the pages for us.
2663// All it does is to check if there are enough free pages
2664// left at the time of mmap(). This could be a potential
2665// problem.
2666int os::Linux::commit_memory_impl(char* addr, size_t size, bool exec) {
2667 int prot = exec ? PROT_READ0x1|PROT_WRITE0x2|PROT_EXEC0x4 : PROT_READ0x1|PROT_WRITE0x2;
2668 uintptr_t res = (uintptr_t) ::mmap(addr, size, prot,
2669 MAP_PRIVATE0x02|MAP_FIXED0x10|MAP_ANONYMOUS0x20, -1, 0);
2670 if (res != (uintptr_t) MAP_FAILED((void *) -1)) {
2671 if (UseNUMAInterleaving) {
2672 numa_make_global(addr, size);
2673 }
2674 return 0;
2675 }
2676
2677 int err = errno(*__errno_location ()); // save errno from mmap() call above
2678
2679 if (!recoverable_mmap_error(err)) {
2680 warn_fail_commit_memory(addr, size, exec, err);
2681 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory.")do { report_vm_out_of_memory("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2681, size, OOM_MMAP_ERROR, "committing reserved memory.");
::breakpoint(); } while (0)
;
2682 }
2683
2684 return err;
2685}
2686
2687bool os::pd_commit_memory(char* addr, size_t size, bool exec) {
2688 return os::Linux::commit_memory_impl(addr, size, exec) == 0;
2689}
2690
2691void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,
2692 const char* mesg) {
2693 assert(mesg != NULL, "mesg must be specified")do { if (!(mesg != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2693, "assert(" "mesg != __null" ") failed", "mesg must be specified"
); ::breakpoint(); } } while (0)
;
2694 int err = os::Linux::commit_memory_impl(addr, size, exec);
2695 if (err != 0) {
2696 // the caller wants all commit errors to exit with the specified mesg:
2697 warn_fail_commit_memory(addr, size, exec, err);
2698 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg)do { report_vm_out_of_memory("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2698, size, OOM_MMAP_ERROR, "%s", mesg); ::breakpoint(); } while
(0)
;
2699 }
2700}
2701
2702// Define MAP_HUGETLB here so we can build HotSpot on old systems.
2703#ifndef MAP_HUGETLB0x40000
2704 #define MAP_HUGETLB0x40000 0x40000
2705#endif
2706
2707// If mmap flags are set with MAP_HUGETLB and the system supports multiple
2708// huge page sizes, flag bits [26:31] can be used to encode the log2 of the
2709// desired huge page size. Otherwise, the system's default huge page size will be used.
2710// See mmap(2) man page for more info (since Linux 3.8).
2711// https://lwn.net/Articles/533499/
2712#ifndef MAP_HUGE_SHIFT26
2713 #define MAP_HUGE_SHIFT26 26
2714#endif
2715
2716// Define MADV_HUGEPAGE here so we can build HotSpot on old systems.
2717#ifndef MADV_HUGEPAGE14
2718 #define MADV_HUGEPAGE14 14
2719#endif
2720
2721int os::Linux::commit_memory_impl(char* addr, size_t size,
2722 size_t alignment_hint, bool exec) {
2723 int err = os::Linux::commit_memory_impl(addr, size, exec);
2724 if (err == 0) {
2725 realign_memory(addr, size, alignment_hint);
2726 }
2727 return err;
2728}
2729
2730bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,
2731 bool exec) {
2732 return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0;
2733}
2734
2735void os::pd_commit_memory_or_exit(char* addr, size_t size,
2736 size_t alignment_hint, bool exec,
2737 const char* mesg) {
2738 assert(mesg != NULL, "mesg must be specified")do { if (!(mesg != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2738, "assert(" "mesg != __null" ") failed", "mesg must be specified"
); ::breakpoint(); } } while (0)
;
2739 int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec);
2740 if (err != 0) {
2741 // the caller wants all commit errors to exit with the specified mesg:
2742 warn_fail_commit_memory(addr, size, alignment_hint, exec, err);
2743 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg)do { report_vm_out_of_memory("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 2743, size, OOM_MMAP_ERROR, "%s", mesg); ::breakpoint(); } while
(0)
;
2744 }
2745}
2746
2747void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2748 if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) {
2749 // We don't check the return value: madvise(MADV_HUGEPAGE) may not
2750 // be supported or the memory may already be backed by huge pages.
2751 ::madvise(addr, bytes, MADV_HUGEPAGE14);
2752 }
2753}
2754
2755void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {
2756 // This method works by doing an mmap over an existing mmaping and effectively discarding
2757 // the existing pages. However it won't work for SHM-based large pages that cannot be
2758 // uncommitted at all. We don't do anything in this case to avoid creating a segment with
2759 // small pages on top of the SHM segment. This method always works for small pages, so we
2760 // allow that in any case.
2761 if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) {
2762 commit_memory(addr, bytes, alignment_hint, !ExecMem);
2763 }
2764}
2765
2766void os::numa_make_global(char *addr, size_t bytes) {
2767 Linux::numa_interleave_memory(addr, bytes);
2768}
2769
2770// Define for numa_set_bind_policy(int). Setting the argument to 0 will set the
2771// bind policy to MPOL_PREFERRED for the current thread.
2772#define USE_MPOL_PREFERRED0 0
2773
2774void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
2775 // To make NUMA and large pages more robust when both enabled, we need to ease
2776 // the requirements on where the memory should be allocated. MPOL_BIND is the
2777 // default policy and it will force memory to be allocated on the specified
2778 // node. Changing this to MPOL_PREFERRED will prefer to allocate the memory on
2779 // the specified node, but will not force it. Using this policy will prevent
2780 // getting SIGBUS when trying to allocate large pages on NUMA nodes with no
2781 // free large pages.
2782 Linux::numa_set_bind_policy(USE_MPOL_PREFERRED0);
2783 Linux::numa_tonode_memory(addr, bytes, lgrp_hint);
2784}
2785
2786bool os::numa_topology_changed() { return false; }
2787
2788size_t os::numa_get_groups_num() {
2789 // Return just the number of nodes in which it's possible to allocate memory
2790 // (in numa terminology, configured nodes).
2791 return Linux::numa_num_configured_nodes();
2792}
2793
2794int os::numa_get_group_id() {
2795 int cpu_id = Linux::sched_getcpu();
2796 if (cpu_id != -1) {
2797 int lgrp_id = Linux::get_node_by_cpu(cpu_id);
2798 if (lgrp_id != -1) {
2799 return lgrp_id;
2800 }
2801 }
2802 return 0;
2803}
2804
2805int os::numa_get_group_id_for_address(const void* address) {
2806 void** pages = const_cast<void**>(&address);
2807 int id = -1;
2808
2809 if (os::Linux::numa_move_pages(0, 1, pages, NULL__null, &id, 0) == -1) {
2810 return -1;
2811 }
2812 if (id < 0) {
2813 return -1;
2814 }
2815 return id;
2816}
2817
2818int os::Linux::get_existing_num_nodes() {
2819 int node;
2820 int highest_node_number = Linux::numa_max_node();
2821 int num_nodes = 0;
2822
2823 // Get the total number of nodes in the system including nodes without memory.
2824 for (node = 0; node <= highest_node_number; node++) {
2825 if (is_node_in_existing_nodes(node)) {
2826 num_nodes++;
2827 }
2828 }
2829 return num_nodes;
2830}
2831
2832size_t os::numa_get_leaf_groups(int *ids, size_t size) {
2833 int highest_node_number = Linux::numa_max_node();
2834 size_t i = 0;
2835
2836 // Map all node ids in which it is possible to allocate memory. Also nodes are
2837 // not always consecutively available, i.e. available from 0 to the highest
2838 // node number. If the nodes have been bound explicitly using numactl membind,
2839 // then allocate memory from those nodes only.
2840 for (int node = 0; node <= highest_node_number; node++) {
2841 if (Linux::is_node_in_bound_nodes((unsigned int)node)) {
2842 ids[i++] = node;
2843 }
2844 }
2845 return i;
2846}
2847
2848bool os::get_page_info(char *start, page_info* info) {
2849 return false;
2850}
2851
2852char *os::scan_pages(char *start, char* end, page_info* page_expected,
2853 page_info* page_found) {
2854 return end;
2855}
2856
2857
2858int os::Linux::sched_getcpu_syscall(void) {
2859 unsigned int cpu = 0;
2860 int retval = -1;
2861
2862#if defined(IA32)
2863 #ifndef SYS_getcpu309
2864 #define SYS_getcpu309 318
2865 #endif
2866 retval = syscall(SYS_getcpu309, &cpu, NULL__null, NULL__null);
2867#elif defined(AMD641)
2868// Unfortunately we have to bring all these macros here from vsyscall.h
2869// to be able to compile on old linuxes.
2870 #define __NR_vgetcpu2 2
2871 #define VSYSCALL_START(-10UL << 20) (-10UL << 20)
2872 #define VSYSCALL_SIZE1024 1024
2873 #define VSYSCALL_ADDR(vsyscall_nr)((-10UL << 20)+1024*(vsyscall_nr)) (VSYSCALL_START(-10UL << 20)+VSYSCALL_SIZE1024*(vsyscall_nr))
2874 typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache);
2875 vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu)((-10UL << 20)+1024*(2));
2876 retval = vgetcpu(&cpu, NULL__null, NULL__null);
2877#endif
2878
2879 return (retval == -1) ? retval : cpu;
2880}
2881
2882void os::Linux::sched_getcpu_init() {
2883 // sched_getcpu() should be in libc.
2884 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,(reinterpret_cast<sched_getcpu_func_t>(dlsym(((void *) 0
), "sched_getcpu")))
2885 dlsym(RTLD_DEFAULT, "sched_getcpu"))(reinterpret_cast<sched_getcpu_func_t>(dlsym(((void *) 0
), "sched_getcpu")))
);
2886
2887 // If it's not, try a direct syscall.
2888 if (sched_getcpu() == -1) {
2889 set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,(reinterpret_cast<sched_getcpu_func_t>((void*)&sched_getcpu_syscall
))
2890 (void*)&sched_getcpu_syscall)(reinterpret_cast<sched_getcpu_func_t>((void*)&sched_getcpu_syscall
))
);
2891 }
2892
2893 if (sched_getcpu() == -1) {
2894 vm_exit_during_initialization("getcpu(2) system call not supported by kernel");
2895 }
2896}
2897
2898// Something to do with the numa-aware allocator needs these symbols
2899extern "C" JNIEXPORT__attribute__((visibility("default"))) void numa_warn(int number, char *where, ...) { }
2900extern "C" JNIEXPORT__attribute__((visibility("default"))) void numa_error(char *where) { }
2901
2902// Handle request to load libnuma symbol version 1.1 (API v1). If it fails
2903// load symbol from base version instead.
2904void* os::Linux::libnuma_dlsym(void* handle, const char *name) {
2905 void *f = dlvsym(handle, name, "libnuma_1.1");
2906 if (f == NULL__null) {
2907 f = dlsym(handle, name);
2908 }
2909 return f;
2910}
2911
2912// Handle request to load libnuma symbol version 1.2 (API v2) only.
2913// Return NULL if the symbol is not defined in this particular version.
2914void* os::Linux::libnuma_v2_dlsym(void* handle, const char* name) {
2915 return dlvsym(handle, name, "libnuma_1.2");
2916}
2917
2918// Check numa dependent syscalls
2919static bool numa_syscall_check() {
2920 // NUMA APIs depend on several syscalls. E.g., get_mempolicy is required for numa_get_membind and
2921 // numa_get_interleave_mask. But these dependent syscalls can be unsupported for various reasons.
2922 // Especially in dockers, get_mempolicy is not allowed with the default configuration. So it's necessary
2923 // to check whether the syscalls are available. Currently, only get_mempolicy is checked since checking
2924 // others like mbind would cause unexpected side effects.
2925#ifdef SYS_get_mempolicy239
2926 int dummy = 0;
2927 if (syscall(SYS_get_mempolicy239, &dummy, NULL__null, 0, (void*)&dummy, 3) == -1) {
2928 return false;
2929 }
2930#endif
2931
2932 return true;
2933}
2934
2935bool os::Linux::libnuma_init() {
2936 // Requires sched_getcpu() and numa dependent syscalls support
2937 if ((sched_getcpu() != -1) && numa_syscall_check()) {
2938 void *handle = dlopen("libnuma.so.1", RTLD_LAZY0x00001);
2939 if (handle != NULL__null) {
2940 set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,(reinterpret_cast<numa_node_to_cpus_func_t>(libnuma_dlsym
(handle, "numa_node_to_cpus")))
2941 libnuma_dlsym(handle, "numa_node_to_cpus"))(reinterpret_cast<numa_node_to_cpus_func_t>(libnuma_dlsym
(handle, "numa_node_to_cpus")))
);
2942 set_numa_node_to_cpus_v2(CAST_TO_FN_PTR(numa_node_to_cpus_v2_func_t,(reinterpret_cast<numa_node_to_cpus_v2_func_t>(libnuma_v2_dlsym
(handle, "numa_node_to_cpus")))
2943 libnuma_v2_dlsym(handle, "numa_node_to_cpus"))(reinterpret_cast<numa_node_to_cpus_v2_func_t>(libnuma_v2_dlsym
(handle, "numa_node_to_cpus")))
);
2944 set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,(reinterpret_cast<numa_max_node_func_t>(libnuma_dlsym(handle
, "numa_max_node")))
2945 libnuma_dlsym(handle, "numa_max_node"))(reinterpret_cast<numa_max_node_func_t>(libnuma_dlsym(handle
, "numa_max_node")))
);
2946 set_numa_num_configured_nodes(CAST_TO_FN_PTR(numa_num_configured_nodes_func_t,(reinterpret_cast<numa_num_configured_nodes_func_t>(libnuma_dlsym
(handle, "numa_num_configured_nodes")))
2947 libnuma_dlsym(handle, "numa_num_configured_nodes"))(reinterpret_cast<numa_num_configured_nodes_func_t>(libnuma_dlsym
(handle, "numa_num_configured_nodes")))
);
2948 set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,(reinterpret_cast<numa_available_func_t>(libnuma_dlsym(
handle, "numa_available")))
2949 libnuma_dlsym(handle, "numa_available"))(reinterpret_cast<numa_available_func_t>(libnuma_dlsym(
handle, "numa_available")))
);
2950 set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,(reinterpret_cast<numa_tonode_memory_func_t>(libnuma_dlsym
(handle, "numa_tonode_memory")))
2951 libnuma_dlsym(handle, "numa_tonode_memory"))(reinterpret_cast<numa_tonode_memory_func_t>(libnuma_dlsym
(handle, "numa_tonode_memory")))
);
2952 set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,(reinterpret_cast<numa_interleave_memory_func_t>(libnuma_dlsym
(handle, "numa_interleave_memory")))
2953 libnuma_dlsym(handle, "numa_interleave_memory"))(reinterpret_cast<numa_interleave_memory_func_t>(libnuma_dlsym
(handle, "numa_interleave_memory")))
);
2954 set_numa_interleave_memory_v2(CAST_TO_FN_PTR(numa_interleave_memory_v2_func_t,(reinterpret_cast<numa_interleave_memory_v2_func_t>(libnuma_v2_dlsym
(handle, "numa_interleave_memory")))
2955 libnuma_v2_dlsym(handle, "numa_interleave_memory"))(reinterpret_cast<numa_interleave_memory_v2_func_t>(libnuma_v2_dlsym
(handle, "numa_interleave_memory")))
);
2956 set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t,(reinterpret_cast<numa_set_bind_policy_func_t>(libnuma_dlsym
(handle, "numa_set_bind_policy")))
2957 libnuma_dlsym(handle, "numa_set_bind_policy"))(reinterpret_cast<numa_set_bind_policy_func_t>(libnuma_dlsym
(handle, "numa_set_bind_policy")))
);
2958 set_numa_bitmask_isbitset(CAST_TO_FN_PTR(numa_bitmask_isbitset_func_t,(reinterpret_cast<numa_bitmask_isbitset_func_t>(libnuma_dlsym
(handle, "numa_bitmask_isbitset")))
2959 libnuma_dlsym(handle, "numa_bitmask_isbitset"))(reinterpret_cast<numa_bitmask_isbitset_func_t>(libnuma_dlsym
(handle, "numa_bitmask_isbitset")))
);
2960 set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,(reinterpret_cast<numa_distance_func_t>(libnuma_dlsym(handle
, "numa_distance")))
2961 libnuma_dlsym(handle, "numa_distance"))(reinterpret_cast<numa_distance_func_t>(libnuma_dlsym(handle
, "numa_distance")))
);
2962 set_numa_get_membind(CAST_TO_FN_PTR(numa_get_membind_func_t,(reinterpret_cast<numa_get_membind_func_t>(libnuma_v2_dlsym
(handle, "numa_get_membind")))
2963 libnuma_v2_dlsym(handle, "numa_get_membind"))(reinterpret_cast<numa_get_membind_func_t>(libnuma_v2_dlsym
(handle, "numa_get_membind")))
);
2964 set_numa_get_interleave_mask(CAST_TO_FN_PTR(numa_get_interleave_mask_func_t,(reinterpret_cast<numa_get_interleave_mask_func_t>(libnuma_v2_dlsym
(handle, "numa_get_interleave_mask")))
2965 libnuma_v2_dlsym(handle, "numa_get_interleave_mask"))(reinterpret_cast<numa_get_interleave_mask_func_t>(libnuma_v2_dlsym
(handle, "numa_get_interleave_mask")))
);
2966 set_numa_move_pages(CAST_TO_FN_PTR(numa_move_pages_func_t,(reinterpret_cast<numa_move_pages_func_t>(libnuma_dlsym
(handle, "numa_move_pages")))
2967 libnuma_dlsym(handle, "numa_move_pages"))(reinterpret_cast<numa_move_pages_func_t>(libnuma_dlsym
(handle, "numa_move_pages")))
);
2968 set_numa_set_preferred(CAST_TO_FN_PTR(numa_set_preferred_func_t,(reinterpret_cast<numa_set_preferred_func_t>(libnuma_dlsym
(handle, "numa_set_preferred")))
2969 libnuma_dlsym(handle, "numa_set_preferred"))(reinterpret_cast<numa_set_preferred_func_t>(libnuma_dlsym
(handle, "numa_set_preferred")))
);
2970
2971 if (numa_available() != -1) {
2972 set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
2973 set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));
2974 set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));
2975 set_numa_interleave_bitmask(_numa_get_interleave_mask());
2976 set_numa_membind_bitmask(_numa_get_membind());
2977 // Create an index -> node mapping, since nodes are not always consecutive
2978 _nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, mtInternal);
2979 rebuild_nindex_to_node_map();
2980 // Create a cpu -> node mapping
2981 _cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, mtInternal);
2982 rebuild_cpu_to_node_map();
2983 return true;
2984 }
2985 }
2986 }
2987 return false;
2988}
2989
2990size_t os::Linux::default_guard_size(os::ThreadType thr_type) {
2991 // Creating guard page is very expensive. Java thread has HotSpot
2992 // guard pages, only enable glibc guard page for non-Java threads.
2993 // (Remember: compiler thread is a Java thread, too!)
2994 return ((thr_type == java_thread || thr_type == compiler_thread) ? 0 : page_size());
2995}
2996
2997void os::Linux::rebuild_nindex_to_node_map() {
2998 int highest_node_number = Linux::numa_max_node();
2999
3000 nindex_to_node()->clear();
3001 for (int node = 0; node <= highest_node_number; node++) {
3002 if (Linux::is_node_in_existing_nodes(node)) {
3003 nindex_to_node()->append(node);
3004 }
3005 }
3006}
3007
3008// rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.
3009// The table is later used in get_node_by_cpu().
3010void os::Linux::rebuild_cpu_to_node_map() {
3011 const size_t NCPUS = 32768; // Since the buffer size computation is very obscure
3012 // in libnuma (possible values are starting from 16,
3013 // and continuing up with every other power of 2, but less
3014 // than the maximum number of CPUs supported by kernel), and
3015 // is a subject to change (in libnuma version 2 the requirements
3016 // are more reasonable) we'll just hardcode the number they use
3017 // in the library.
3018 const size_t BitsPerCLong = sizeof(long) * CHAR_BIT8;
3019
3020 size_t cpu_num = processor_count();
3021 size_t cpu_map_size = NCPUS / BitsPerCLong;
3022 size_t cpu_map_valid_size =
3023 MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size);
3024
3025 cpu_to_node()->clear();
3026 cpu_to_node()->at_grow(cpu_num - 1);
3027
3028 size_t node_num = get_existing_num_nodes();
3029
3030 int distance = 0;
3031 int closest_distance = INT_MAX2147483647;
3032 int closest_node = 0;
3033 unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size, mtInternal)(unsigned long*) (AllocateHeap((cpu_map_size) * sizeof(unsigned
long), mtInternal))
;
3034 for (size_t i = 0; i < node_num; i++) {
3035 // Check if node is configured (not a memory-less node). If it is not, find
3036 // the closest configured node. Check also if node is bound, i.e. it's allowed
3037 // to allocate memory from the node. If it's not allowed, map cpus in that node
3038 // to the closest node from which memory allocation is allowed.
3039 if (!is_node_in_configured_nodes(nindex_to_node()->at(i)) ||
3040 !is_node_in_bound_nodes(nindex_to_node()->at(i))) {
3041 closest_distance = INT_MAX2147483647;
3042 // Check distance from all remaining nodes in the system. Ignore distance
3043 // from itself, from another non-configured node, and from another non-bound
3044 // node.
3045 for (size_t m = 0; m < node_num; m++) {
3046 if (m != i &&
3047 is_node_in_configured_nodes(nindex_to_node()->at(m)) &&
3048 is_node_in_bound_nodes(nindex_to_node()->at(m))) {
3049 distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));
3050 // If a closest node is found, update. There is always at least one
3051 // configured and bound node in the system so there is always at least
3052 // one node close.
3053 if (distance != 0 && distance < closest_distance) {
3054 closest_distance = distance;
3055 closest_node = nindex_to_node()->at(m);
3056 }
3057 }
3058 }
3059 } else {
3060 // Current node is already a configured node.
3061 closest_node = nindex_to_node()->at(i);
3062 }
3063
3064 // Get cpus from the original node and map them to the closest node. If node
3065 // is a configured node (not a memory-less node), then original node and
3066 // closest node are the same.
3067 if (numa_node_to_cpus(nindex_to_node()->at(i), cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
3068 for (size_t j = 0; j < cpu_map_valid_size; j++) {
3069 if (cpu_map[j] != 0) {
3070 for (size_t k = 0; k < BitsPerCLong; k++) {
3071 if (cpu_map[j] & (1UL << k)) {
3072 int cpu_index = j * BitsPerCLong + k;
3073
3074#ifndef PRODUCT
3075 if (UseDebuggerErgo1 && cpu_index >= (int)cpu_num) {
3076 // Some debuggers limit the processor count without
3077 // intercepting the NUMA APIs. Just fake the values.
3078 cpu_index = 0;
3079 }
3080#endif
3081
3082 cpu_to_node()->at_put(cpu_index, closest_node);
3083 }
3084 }
3085 }
3086 }
3087 }
3088 }
3089 FREE_C_HEAP_ARRAY(unsigned long, cpu_map)FreeHeap((char*)(cpu_map));
3090}
3091
3092int os::Linux::numa_node_to_cpus(int node, unsigned long *buffer, int bufferlen) {
3093 // use the latest version of numa_node_to_cpus if available
3094 if (_numa_node_to_cpus_v2 != NULL__null) {
3095
3096 // libnuma bitmask struct
3097 struct bitmask {
3098 unsigned long size; /* number of bits in the map */
3099 unsigned long *maskp;
3100 };
3101
3102 struct bitmask mask;
3103 mask.maskp = (unsigned long *)buffer;
3104 mask.size = bufferlen * 8;
3105 return _numa_node_to_cpus_v2(node, &mask);
3106 } else if (_numa_node_to_cpus != NULL__null) {
3107 return _numa_node_to_cpus(node, buffer, bufferlen);
3108 }
3109 return -1;
3110}
3111
3112int os::Linux::get_node_by_cpu(int cpu_id) {
3113 if (cpu_to_node() != NULL__null && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {
3114 return cpu_to_node()->at(cpu_id);
3115 }
3116 return -1;
3117}
3118
3119GrowableArray<int>* os::Linux::_cpu_to_node;
3120GrowableArray<int>* os::Linux::_nindex_to_node;
3121os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;
3122os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;
3123os::Linux::numa_node_to_cpus_v2_func_t os::Linux::_numa_node_to_cpus_v2;
3124os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;
3125os::Linux::numa_num_configured_nodes_func_t os::Linux::_numa_num_configured_nodes;
3126os::Linux::numa_available_func_t os::Linux::_numa_available;
3127os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;
3128os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;
3129os::Linux::numa_interleave_memory_v2_func_t os::Linux::_numa_interleave_memory_v2;
3130os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;
3131os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;
3132os::Linux::numa_distance_func_t os::Linux::_numa_distance;
3133os::Linux::numa_get_membind_func_t os::Linux::_numa_get_membind;
3134os::Linux::numa_get_interleave_mask_func_t os::Linux::_numa_get_interleave_mask;
3135os::Linux::numa_move_pages_func_t os::Linux::_numa_move_pages;
3136os::Linux::numa_set_preferred_func_t os::Linux::_numa_set_preferred;
3137os::Linux::NumaAllocationPolicy os::Linux::_current_numa_policy;
3138unsigned long* os::Linux::_numa_all_nodes;
3139struct bitmask* os::Linux::_numa_all_nodes_ptr;
3140struct bitmask* os::Linux::_numa_nodes_ptr;
3141struct bitmask* os::Linux::_numa_interleave_bitmask;
3142struct bitmask* os::Linux::_numa_membind_bitmask;
3143
3144bool os::pd_uncommit_memory(char* addr, size_t size, bool exec) {
3145 uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE0x0,
3146 MAP_PRIVATE0x02|MAP_FIXED0x10|MAP_NORESERVE0x04000|MAP_ANONYMOUS0x20, -1, 0);
3147 return res != (uintptr_t) MAP_FAILED((void *) -1);
3148}
3149
3150static address get_stack_commited_bottom(address bottom, size_t size) {
3151 address nbot = bottom;
3152 address ntop = bottom + size;
3153
3154 size_t page_sz = os::vm_page_size();
3155 unsigned pages = size / page_sz;
3156
3157 unsigned char vec[1];
3158 unsigned imin = 1, imax = pages + 1, imid;
3159 int mincore_return_value = 0;
3160
3161 assert(imin <= imax, "Unexpected page size")do { if (!(imin <= imax)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3161, "assert(" "imin <= imax" ") failed", "Unexpected page size"
); ::breakpoint(); } } while (0)
;
3162
3163 while (imin < imax) {
3164 imid = (imax + imin) / 2;
3165 nbot = ntop - (imid * page_sz);
3166
3167 // Use a trick with mincore to check whether the page is mapped or not.
3168 // mincore sets vec to 1 if page resides in memory and to 0 if page
3169 // is swapped output but if page we are asking for is unmapped
3170 // it returns -1,ENOMEM
3171 mincore_return_value = mincore(nbot, page_sz, vec);
3172
3173 if (mincore_return_value == -1) {
3174 // Page is not mapped go up
3175 // to find first mapped page
3176 if (errno(*__errno_location ()) != EAGAIN11) {
3177 assert(errno == ENOMEM, "Unexpected mincore errno")do { if (!((*__errno_location ()) == 12)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3177, "assert(" "(*__errno_location ()) == 12" ") failed", "Unexpected mincore errno"
); ::breakpoint(); } } while (0)
;
3178 imax = imid;
3179 }
3180 } else {
3181 // Page is mapped go down
3182 // to find first not mapped page
3183 imin = imid + 1;
3184 }
3185 }
3186
3187 nbot = nbot + page_sz;
3188
3189 // Adjust stack bottom one page up if last checked page is not mapped
3190 if (mincore_return_value == -1) {
3191 nbot = nbot + page_sz;
3192 }
3193
3194 return nbot;
3195}
3196
3197bool os::committed_in_range(address start, size_t size, address& committed_start, size_t& committed_size) {
3198 int mincore_return_value;
3199 const size_t stripe = 1024; // query this many pages each time
3200 unsigned char vec[stripe + 1];
3201 // set a guard
3202 vec[stripe] = 'X';
3203
3204 const size_t page_sz = os::vm_page_size();
3205 size_t pages = size / page_sz;
3206
3207 assert(is_aligned(start, page_sz), "Start address must be page aligned")do { if (!(is_aligned(start, page_sz))) { (*g_assert_poison) =
'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3207, "assert(" "is_aligned(start, page_sz)" ") failed", "Start address must be page aligned"
); ::breakpoint(); } } while (0)
;
3208 assert(is_aligned(size, page_sz), "Size must be page aligned")do { if (!(is_aligned(size, page_sz))) { (*g_assert_poison) =
'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3208, "assert(" "is_aligned(size, page_sz)" ") failed", "Size must be page aligned"
); ::breakpoint(); } } while (0)
;
3209
3210 committed_start = NULL__null;
3211
3212 int loops = (pages + stripe - 1) / stripe;
3213 int committed_pages = 0;
3214 address loop_base = start;
3215 bool found_range = false;
3216
3217 for (int index = 0; index < loops && !found_range; index ++) {
3218 assert(pages > 0, "Nothing to do")do { if (!(pages > 0)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3218, "assert(" "pages > 0" ") failed", "Nothing to do")
; ::breakpoint(); } } while (0)
;
3219 int pages_to_query = (pages >= stripe) ? stripe : pages;
3220 pages -= pages_to_query;
3221
3222 // Get stable read
3223 while ((mincore_return_value = mincore(loop_base, pages_to_query * page_sz, vec)) == -1 && errno(*__errno_location ()) == EAGAIN11);
3224
3225 // During shutdown, some memory goes away without properly notifying NMT,
3226 // E.g. ConcurrentGCThread/WatcherThread can exit without deleting thread object.
3227 // Bailout and return as not committed for now.
3228 if (mincore_return_value == -1 && errno(*__errno_location ()) == ENOMEM12) {
3229 return false;
3230 }
3231
3232 assert(vec[stripe] == 'X', "overflow guard")do { if (!(vec[stripe] == 'X')) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3232, "assert(" "vec[stripe] == 'X'" ") failed", "overflow guard"
); ::breakpoint(); } } while (0)
;
3233 assert(mincore_return_value == 0, "Range must be valid")do { if (!(mincore_return_value == 0)) { (*g_assert_poison) =
'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3233, "assert(" "mincore_return_value == 0" ") failed", "Range must be valid"
); ::breakpoint(); } } while (0)
;
3234 // Process this stripe
3235 for (int vecIdx = 0; vecIdx < pages_to_query; vecIdx ++) {
3236 if ((vec[vecIdx] & 0x01) == 0) { // not committed
3237 // End of current contiguous region
3238 if (committed_start != NULL__null) {
3239 found_range = true;
3240 break;
3241 }
3242 } else { // committed
3243 // Start of region
3244 if (committed_start == NULL__null) {
3245 committed_start = loop_base + page_sz * vecIdx;
3246 }
3247 committed_pages ++;
3248 }
3249 }
3250
3251 loop_base += pages_to_query * page_sz;
3252 }
3253
3254 if (committed_start != NULL__null) {
3255 assert(committed_pages > 0, "Must have committed region")do { if (!(committed_pages > 0)) { (*g_assert_poison) = 'X'
;; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3255, "assert(" "committed_pages > 0" ") failed", "Must have committed region"
); ::breakpoint(); } } while (0)
;
3256 assert(committed_pages <= int(size / page_sz), "Can not commit more than it has")do { if (!(committed_pages <= int(size / page_sz))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3256, "assert(" "committed_pages <= int(size / page_sz)"
") failed", "Can not commit more than it has"); ::breakpoint
(); } } while (0)
;
3257 assert(committed_start >= start && committed_start < start + size, "Out of range")do { if (!(committed_start >= start && committed_start
< start + size)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3257, "assert(" "committed_start >= start && committed_start < start + size"
") failed", "Out of range"); ::breakpoint(); } } while (0)
;
3258 committed_size = page_sz * committed_pages;
3259 return true;
3260 } else {
3261 assert(committed_pages == 0, "Should not have committed region")do { if (!(committed_pages == 0)) { (*g_assert_poison) = 'X';
; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3261, "assert(" "committed_pages == 0" ") failed", "Should not have committed region"
); ::breakpoint(); } } while (0)
;
3262 return false;
3263 }
3264}
3265
3266
3267// Linux uses a growable mapping for the stack, and if the mapping for
3268// the stack guard pages is not removed when we detach a thread the
3269// stack cannot grow beyond the pages where the stack guard was
3270// mapped. If at some point later in the process the stack expands to
3271// that point, the Linux kernel cannot expand the stack any further
3272// because the guard pages are in the way, and a segfault occurs.
3273//
3274// However, it's essential not to split the stack region by unmapping
3275// a region (leaving a hole) that's already part of the stack mapping,
3276// so if the stack mapping has already grown beyond the guard pages at
3277// the time we create them, we have to truncate the stack mapping.
3278// So, we need to know the extent of the stack mapping when
3279// create_stack_guard_pages() is called.
3280
3281// We only need this for stacks that are growable: at the time of
3282// writing thread stacks don't use growable mappings (i.e. those
3283// creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
3284// only applies to the main thread.
3285
3286// If the (growable) stack mapping already extends beyond the point
3287// where we're going to put our guard pages, truncate the mapping at
3288// that point by munmap()ping it. This ensures that when we later
3289// munmap() the guard pages we don't leave a hole in the stack
3290// mapping. This only affects the main/primordial thread
3291
3292bool os::pd_create_stack_guard_pages(char* addr, size_t size) {
3293 if (os::is_primordial_thread()) {
3294 // As we manually grow stack up to bottom inside create_attached_thread(),
3295 // it's likely that os::Linux::initial_thread_stack_bottom is mapped and
3296 // we don't need to do anything special.
3297 // Check it first, before calling heavy function.
3298 uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();
3299 unsigned char vec[1];
3300
3301 if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {
3302 // Fallback to slow path on all errors, including EAGAIN
3303 assert((uintptr_t)addr >= stack_extent,do { if (!((uintptr_t)addr >= stack_extent)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3305, "assert(" "(uintptr_t)addr >= stack_extent" ") failed"
, "Sanity: addr should be larger than extent, " "0x%016" "l" "x"
" >= " "0x%016" "l" "x", p2i(addr), stack_extent); ::breakpoint
(); } } while (0)
3304 "Sanity: addr should be larger than extent, " PTR_FORMAT " >= " PTR_FORMAT,do { if (!((uintptr_t)addr >= stack_extent)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3305, "assert(" "(uintptr_t)addr >= stack_extent" ") failed"
, "Sanity: addr should be larger than extent, " "0x%016" "l" "x"
" >= " "0x%016" "l" "x", p2i(addr), stack_extent); ::breakpoint
(); } } while (0)
3305 p2i(addr), stack_extent)do { if (!((uintptr_t)addr >= stack_extent)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3305, "assert(" "(uintptr_t)addr >= stack_extent" ") failed"
, "Sanity: addr should be larger than extent, " "0x%016" "l" "x"
" >= " "0x%016" "l" "x", p2i(addr), stack_extent); ::breakpoint
(); } } while (0)
;
3306 stack_extent = (uintptr_t) get_stack_commited_bottom(
3307 os::Linux::initial_thread_stack_bottom(),
3308 (size_t)addr - stack_extent);
3309 }
3310
3311 if (stack_extent < (uintptr_t)addr) {
3312 ::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));
3313 }
3314 }
3315
3316 return os::commit_memory(addr, size, !ExecMem);
3317}
3318
3319// If this is a growable mapping, remove the guard pages entirely by
3320// munmap()ping them. If not, just call uncommit_memory(). This only
3321// affects the main/primordial thread, but guard against future OS changes.
3322// It's safe to always unmap guard pages for primordial thread because we
3323// always place it right after end of the mapped region.
3324
3325bool os::remove_stack_guard_pages(char* addr, size_t size) {
3326 uintptr_t stack_extent, stack_base;
3327
3328 if (os::is_primordial_thread()) {
3329 return ::munmap(addr, size) == 0;
3330 }
3331
3332 return os::uncommit_memory(addr, size);
3333}
3334
3335// 'requested_addr' is only treated as a hint, the return value may or
3336// may not start from the requested address. Unlike Linux mmap(), this
3337// function returns NULL to indicate failure.
3338static char* anon_mmap(char* requested_addr, size_t bytes) {
3339 // MAP_FIXED is intentionally left out, to leave existing mappings intact.
3340 const int flags = MAP_PRIVATE0x02 | MAP_NORESERVE0x04000 | MAP_ANONYMOUS0x20;
3341
3342 // Map reserved/uncommitted pages PROT_NONE so we fail early if we
3343 // touch an uncommitted page. Otherwise, the read/write might
3344 // succeed if we have enough swap space to back the physical page.
3345 char* addr = (char*)::mmap(requested_addr, bytes, PROT_NONE0x0, flags, -1, 0);
3346
3347 return addr == MAP_FAILED((void *) -1) ? NULL__null : addr;
3348}
3349
3350// Allocate (using mmap, NO_RESERVE, with small pages) at either a given request address
3351// (req_addr != NULL) or with a given alignment.
3352// - bytes shall be a multiple of alignment.
3353// - req_addr can be NULL. If not NULL, it must be a multiple of alignment.
3354// - alignment sets the alignment at which memory shall be allocated.
3355// It must be a multiple of allocation granularity.
3356// Returns address of memory or NULL. If req_addr was not NULL, will only return
3357// req_addr or NULL.
3358static char* anon_mmap_aligned(char* req_addr, size_t bytes, size_t alignment) {
3359 size_t extra_size = bytes;
3360 if (req_addr == NULL__null && alignment > 0) {
3361 extra_size += alignment;
3362 }
3363
3364 char* start = anon_mmap(req_addr, extra_size);
3365 if (start != NULL__null) {
3366 if (req_addr != NULL__null) {
3367 if (start != req_addr) {
3368 ::munmap(start, extra_size);
3369 start = NULL__null;
3370 }
3371 } else {
3372 char* const start_aligned = align_up(start, alignment);
3373 char* const end_aligned = start_aligned + bytes;
3374 char* const end = start + extra_size;
3375 if (start_aligned > start) {
3376 ::munmap(start, start_aligned - start);
3377 }
3378 if (end_aligned < end) {
3379 ::munmap(end_aligned, end - end_aligned);
3380 }
3381 start = start_aligned;
3382 }
3383 }
3384 return start;
3385}
3386
3387static int anon_munmap(char * addr, size_t size) {
3388 return ::munmap(addr, size) == 0;
3389}
3390
3391char* os::pd_reserve_memory(size_t bytes, bool exec) {
3392 return anon_mmap(NULL__null, bytes);
3393}
3394
3395bool os::pd_release_memory(char* addr, size_t size) {
3396 return anon_munmap(addr, size);
3397}
3398
3399#ifdef CAN_SHOW_REGISTERS_ON_ASSERT
3400extern char* g_assert_poison; // assertion poison page address
3401#endif
3402
3403static bool linux_mprotect(char* addr, size_t size, int prot) {
3404 // Linux wants the mprotect address argument to be page aligned.
3405 char* bottom = (char*)align_down((intptr_t)addr, os::Linux::page_size());
3406
3407 // According to SUSv3, mprotect() should only be used with mappings
3408 // established by mmap(), and mmap() always maps whole pages. Unaligned
3409 // 'addr' likely indicates problem in the VM (e.g. trying to change
3410 // protection of malloc'ed or statically allocated memory). Check the
3411 // caller if you hit this assert.
3412 assert(addr == bottom, "sanity check")do { if (!(addr == bottom)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3412, "assert(" "addr == bottom" ") failed", "sanity check"
); ::breakpoint(); } } while (0)
;
3413
3414 size = align_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());
3415 // Don't log anything if we're executing in the poison page signal handling
3416 // context. It can lead to reentrant use of other parts of the VM code.
3417#ifdef CAN_SHOW_REGISTERS_ON_ASSERT
3418 if (addr != g_assert_poison)
3419#endif
3420 Events::log(NULL__null, "Protecting memory [" INTPTR_FORMAT"0x%016" "l" "x" "," INTPTR_FORMAT"0x%016" "l" "x" "] with protection modes %x", p2i(bottom), p2i(bottom+size), prot);
3421 return ::mprotect(bottom, size, prot) == 0;
3422}
3423
3424// Set protections specified
3425bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
3426 bool is_committed) {
3427 unsigned int p = 0;
3428 switch (prot) {
3429 case MEM_PROT_NONE: p = PROT_NONE0x0; break;
3430 case MEM_PROT_READ: p = PROT_READ0x1; break;
3431 case MEM_PROT_RW: p = PROT_READ0x1|PROT_WRITE0x2; break;
3432 case MEM_PROT_RWX: p = PROT_READ0x1|PROT_WRITE0x2|PROT_EXEC0x4; break;
3433 default:
3434 ShouldNotReachHere()do { (*g_assert_poison) = 'X';; report_should_not_reach_here(
"/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3434); ::breakpoint(); } while (0)
;
3435 }
3436 // is_committed is unused.
3437 return linux_mprotect(addr, bytes, p);
3438}
3439
3440bool os::guard_memory(char* addr, size_t size) {
3441 return linux_mprotect(addr, size, PROT_NONE0x0);
3442}
3443
3444bool os::unguard_memory(char* addr, size_t size) {
3445 return linux_mprotect(addr, size, PROT_READ0x1|PROT_WRITE0x2);
3446}
3447
3448bool os::Linux::transparent_huge_pages_sanity_check(bool warn,
3449 size_t page_size) {
3450 bool result = false;
3451 void *p = mmap(NULL__null, page_size * 2, PROT_READ0x1|PROT_WRITE0x2,
3452 MAP_ANONYMOUS0x20|MAP_PRIVATE0x02,
3453 -1, 0);
3454 if (p != MAP_FAILED((void *) -1)) {
3455 void *aligned_p = align_up(p, page_size);
3456
3457 result = madvise(aligned_p, page_size, MADV_HUGEPAGE14) == 0;
3458
3459 munmap(p, page_size * 2);
3460 }
3461
3462 if (warn && !result) {
3463 warning("TransparentHugePages is not supported by the operating system.");
3464 }
3465
3466 return result;
3467}
3468
3469int os::Linux::hugetlbfs_page_size_flag(size_t page_size) {
3470 if (page_size != default_large_page_size()) {
3471 return (exact_log2(page_size) << MAP_HUGE_SHIFT26);
3472 }
3473 return 0;
3474}
3475
3476bool os::Linux::hugetlbfs_sanity_check(bool warn, size_t page_size) {
3477 // Include the page size flag to ensure we sanity check the correct page size.
3478 int flags = MAP_ANONYMOUS0x20 | MAP_PRIVATE0x02 | MAP_HUGETLB0x40000 | hugetlbfs_page_size_flag(page_size);
3479 void *p = mmap(NULL__null, page_size, PROT_READ0x1|PROT_WRITE0x2, flags, -1, 0);
3480
3481 if (p != MAP_FAILED((void *) -1)) {
3482 // Mapping succeeded, sanity check passed.
3483 munmap(p, page_size);
3484 return true;
3485 } else {
3486 log_info(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Info))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Info>
("Large page size (" SIZE_FORMAT"%" "l" "u" "%s) failed sanity check, "
3487 "checking if smaller large page sizes are usable",
3488 byte_size_in_exact_unit(page_size),
3489 exact_unit_for_byte_size(page_size));
3490 for (size_t page_size_ = _page_sizes.next_smaller(page_size);
3491 page_size_ != (size_t)os::vm_page_size();
3492 page_size_ = _page_sizes.next_smaller(page_size_)) {
3493 flags = MAP_ANONYMOUS0x20 | MAP_PRIVATE0x02 | MAP_HUGETLB0x40000 | hugetlbfs_page_size_flag(page_size_);
3494 p = mmap(NULL__null, page_size_, PROT_READ0x1|PROT_WRITE0x2, flags, -1, 0);
3495 if (p != MAP_FAILED((void *) -1)) {
3496 // Mapping succeeded, sanity check passed.
3497 munmap(p, page_size_);
3498 log_info(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Info))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Info>
("Large page size (" SIZE_FORMAT"%" "l" "u" "%s) passed sanity check",
3499 byte_size_in_exact_unit(page_size_),
3500 exact_unit_for_byte_size(page_size_));
3501 return true;
3502 }
3503 }
3504 }
3505
3506 if (warn) {
3507 warning("HugeTLBFS is not configured or not supported by the operating system.");
3508 }
3509
3510 return false;
3511}
3512
3513bool os::Linux::shm_hugetlbfs_sanity_check(bool warn, size_t page_size) {
3514 // Try to create a large shared memory segment.
3515 int shmid = shmget(IPC_PRIVATE((__key_t) 0), page_size, SHM_HUGETLB04000|IPC_CREAT01000|SHM_R0400|SHM_W0200);
3516 if (shmid == -1) {
3517 // Possible reasons for shmget failure:
3518 // 1. shmmax is too small for the request.
3519 // > check shmmax value: cat /proc/sys/kernel/shmmax
3520 // > increase shmmax value: echo "new_value" > /proc/sys/kernel/shmmax
3521 // 2. not enough large page memory.
3522 // > check available large pages: cat /proc/meminfo
3523 // > increase amount of large pages:
3524 // sysctl -w vm.nr_hugepages=new_value
3525 // > For more information regarding large pages please refer to:
3526 // https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
3527 if (warn) {
3528 warning("Large pages using UseSHM are not configured on this system.");
3529 }
3530 return false;
3531 }
3532 // Managed to create a segment, now delete it.
3533 shmctl(shmid, IPC_RMID0, NULL__null);
3534 return true;
3535}
3536
3537// From the coredump_filter documentation:
3538//
3539// - (bit 0) anonymous private memory
3540// - (bit 1) anonymous shared memory
3541// - (bit 2) file-backed private memory
3542// - (bit 3) file-backed shared memory
3543// - (bit 4) ELF header pages in file-backed private memory areas (it is
3544// effective only if the bit 2 is cleared)
3545// - (bit 5) hugetlb private memory
3546// - (bit 6) hugetlb shared memory
3547// - (bit 7) dax private memory
3548// - (bit 8) dax shared memory
3549//
3550static void set_coredump_filter(CoredumpFilterBit bit) {
3551 FILE *f;
3552 long cdm;
3553
3554 if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL__null) {
3555 return;
3556 }
3557
3558 if (fscanf(f, "%lx", &cdm) != 1) {
3559 fclose(f);
3560 return;
3561 }
3562
3563 long saved_cdm = cdm;
3564 rewind(f);
3565 cdm |= bit;
3566
3567 if (cdm != saved_cdm) {
3568 fprintf(f, "%#lx", cdm);
3569 }
3570
3571 fclose(f);
3572}
3573
3574// Large page support
3575
3576static size_t _large_page_size = 0;
3577
3578static size_t scan_default_large_page_size() {
3579 size_t default_large_page_size = 0;
3580
3581 // large_page_size on Linux is used to round up heap size. x86 uses either
3582 // 2M or 4M page, depending on whether PAE (Physical Address Extensions)
3583 // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
3584 // page as large as 1G.
3585 //
3586 // Here we try to figure out page size by parsing /proc/meminfo and looking
3587 // for a line with the following format:
3588 // Hugepagesize: 2048 kB
3589 //
3590 // If we can't determine the value (e.g. /proc is not mounted, or the text
3591 // format has been changed), we'll set largest page size to 0
3592
3593 FILE *fp = fopen("/proc/meminfo", "r");
3594 if (fp) {
3595 while (!feof(fp)) {
3596 int x = 0;
3597 char buf[16];
3598 if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
3599 if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
3600 default_large_page_size = x * K;
3601 break;
3602 }
3603 } else {
3604 // skip to next line
3605 for (;;) {
3606 int ch = fgetc(fp);
3607 if (ch == EOF(-1) || ch == (int)'\n') break;
3608 }
3609 }
3610 }
3611 fclose(fp);
3612 }
3613
3614 return default_large_page_size;
3615}
3616
3617static os::PageSizes scan_multiple_page_support() {
3618 // Scan /sys/kernel/mm/hugepages
3619 // to discover the available page sizes
3620 const char* sys_hugepages = "/sys/kernel/mm/hugepages";
3621 os::PageSizes page_sizes;
3622
3623 DIR *dir = opendir(sys_hugepages);
3624
3625 struct dirent *entry;
3626 size_t page_size;
3627 while ((entry = readdir(dir)) != NULL__null) {
3628 if (entry->d_type == DT_DIRDT_DIR &&
3629 sscanf(entry->d_name, "hugepages-%zukB", &page_size) == 1) {
3630 // The kernel is using kB, hotspot uses bytes
3631 // Add each found Large Page Size to page_sizes
3632 page_sizes.add(page_size * K);
3633 }
3634 }
3635 closedir(dir);
3636
3637 LogTarget(Debug, pagesize)LogTargetImpl<LogLevel::Debug, (LogTag::_pagesize), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG), (LogTag::__NO_TAG)>
lt;
3638 if (lt.is_enabled()) {
3639 LogStream ls(lt);
3640 ls.print("Large Page sizes: ");
3641 page_sizes.print_on(&ls);
3642 }
3643
3644 return page_sizes;
3645}
3646
3647size_t os::Linux::default_large_page_size() {
3648 return _default_large_page_size;
3649}
3650
3651void warn_no_large_pages_configured() {
3652 if (!FLAG_IS_DEFAULT(UseLargePages)(JVMFlag::is_default(Flag_UseLargePages_enum))) {
3653 log_warning(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Warning))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Warning>
("UseLargePages disabled, no large pages configured and available on the system.");
3654 }
3655}
3656
3657bool os::Linux::setup_large_page_type(size_t page_size) {
3658 if (FLAG_IS_DEFAULT(UseHugeTLBFS)(JVMFlag::is_default(Flag_UseHugeTLBFS_enum)) &&
3659 FLAG_IS_DEFAULT(UseSHM)(JVMFlag::is_default(Flag_UseSHM_enum)) &&
3660 FLAG_IS_DEFAULT(UseTransparentHugePages)(JVMFlag::is_default(Flag_UseTransparentHugePages_enum))) {
3661
3662 // The type of large pages has not been specified by the user.
3663
3664 // Try UseHugeTLBFS and then UseSHM.
3665 UseHugeTLBFS = UseSHM = true;
3666
3667 // Don't try UseTransparentHugePages since there are known
3668 // performance issues with it turned on. This might change in the future.
3669 UseTransparentHugePages = false;
3670 }
3671
3672 if (UseTransparentHugePages) {
3673 bool warn_on_failure = !FLAG_IS_DEFAULT(UseTransparentHugePages)(JVMFlag::is_default(Flag_UseTransparentHugePages_enum));
3674 if (transparent_huge_pages_sanity_check(warn_on_failure, page_size)) {
3675 UseHugeTLBFS = false;
3676 UseSHM = false;
3677 return true;
3678 }
3679 UseTransparentHugePages = false;
3680 }
3681
3682 if (UseHugeTLBFS) {
3683 bool warn_on_failure = !FLAG_IS_DEFAULT(UseHugeTLBFS)(JVMFlag::is_default(Flag_UseHugeTLBFS_enum));
3684 if (hugetlbfs_sanity_check(warn_on_failure, page_size)) {
3685 UseSHM = false;
3686 return true;
3687 }
3688 UseHugeTLBFS = false;
3689 }
3690
3691 if (UseSHM) {
3692 bool warn_on_failure = !FLAG_IS_DEFAULT(UseSHM)(JVMFlag::is_default(Flag_UseSHM_enum));
3693 if (shm_hugetlbfs_sanity_check(warn_on_failure, page_size)) {
3694 return true;
3695 }
3696 UseSHM = false;
3697 }
3698
3699 warn_no_large_pages_configured();
3700 return false;
3701}
3702
3703void os::large_page_init() {
3704 // 1) Handle the case where we do not want to use huge pages and hence
3705 // there is no need to scan the OS for related info
3706 if (!UseLargePages &&
3707 !UseTransparentHugePages &&
3708 !UseHugeTLBFS &&
3709 !UseSHM) {
3710 // Not using large pages.
3711 return;
3712 }
3713
3714 if (!FLAG_IS_DEFAULT(UseLargePages)(JVMFlag::is_default(Flag_UseLargePages_enum)) && !UseLargePages) {
3715 // The user explicitly turned off large pages.
3716 // Ignore the rest of the large pages flags.
3717 UseTransparentHugePages = false;
3718 UseHugeTLBFS = false;
3719 UseSHM = false;
3720 return;
3721 }
3722
3723 // 2) Scan OS info
3724 size_t default_large_page_size = scan_default_large_page_size();
3725 os::Linux::_default_large_page_size = default_large_page_size;
3726 if (default_large_page_size == 0) {
3727 // No large pages configured, return.
3728 warn_no_large_pages_configured();
3729 UseLargePages = false;
3730 UseTransparentHugePages = false;
3731 UseHugeTLBFS = false;
3732 UseSHM = false;
3733 return;
3734 }
3735 os::PageSizes all_large_pages = scan_multiple_page_support();
3736
3737 // 3) Consistency check and post-processing
3738
3739 // It is unclear if /sys/kernel/mm/hugepages/ and /proc/meminfo could disagree. Manually
3740 // re-add the default page size to the list of page sizes to be sure.
3741 all_large_pages.add(default_large_page_size);
3742
3743 // Check LargePageSizeInBytes matches an available page size and if so set _large_page_size
3744 // using LargePageSizeInBytes as the maximum allowed large page size. If LargePageSizeInBytes
3745 // doesn't match an available page size set _large_page_size to default_large_page_size
3746 // and use it as the maximum.
3747 if (FLAG_IS_DEFAULT(LargePageSizeInBytes)(JVMFlag::is_default(Flag_LargePageSizeInBytes_enum)) ||
3748 LargePageSizeInBytes == 0 ||
3749 LargePageSizeInBytes == default_large_page_size) {
3750 _large_page_size = default_large_page_size;
3751 log_info(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Info))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Info>
("Using the default large page size: " SIZE_FORMAT"%" "l" "u" "%s",
3752 byte_size_in_exact_unit(_large_page_size),
3753 exact_unit_for_byte_size(_large_page_size));
3754 } else {
3755 if (all_large_pages.contains(LargePageSizeInBytes)) {
3756 _large_page_size = LargePageSizeInBytes;
3757 log_info(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Info))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Info>
("Overriding default large page size (" SIZE_FORMAT"%" "l" "u" "%s) "
3758 "using LargePageSizeInBytes: " SIZE_FORMAT"%" "l" "u" "%s",
3759 byte_size_in_exact_unit(default_large_page_size),
3760 exact_unit_for_byte_size(default_large_page_size),
3761 byte_size_in_exact_unit(_large_page_size),
3762 exact_unit_for_byte_size(_large_page_size));
3763 } else {
3764 _large_page_size = default_large_page_size;
3765 log_info(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Info))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Info>
("LargePageSizeInBytes is not a valid large page size (" SIZE_FORMAT"%" "l" "u" "%s) "
3766 "using the default large page size: " SIZE_FORMAT"%" "l" "u" "%s",
3767 byte_size_in_exact_unit(LargePageSizeInBytes),
3768 exact_unit_for_byte_size(LargePageSizeInBytes),
3769 byte_size_in_exact_unit(_large_page_size),
3770 exact_unit_for_byte_size(_large_page_size));
3771 }
3772 }
3773
3774 // Populate _page_sizes with large page sizes less than or equal to
3775 // _large_page_size.
3776 for (size_t page_size = _large_page_size; page_size != 0;
3777 page_size = all_large_pages.next_smaller(page_size)) {
3778 _page_sizes.add(page_size);
3779 }
3780
3781 LogTarget(Info, pagesize)LogTargetImpl<LogLevel::Info, (LogTag::_pagesize), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG), (LogTag::__NO_TAG)>
lt;
3782 if (lt.is_enabled()) {
3783 LogStream ls(lt);
3784 ls.print("Usable page sizes: ");
3785 _page_sizes.print_on(&ls);
3786 }
3787
3788 // Now determine the type of large pages to use:
3789 UseLargePages = os::Linux::setup_large_page_type(_large_page_size);
3790
3791 set_coredump_filter(LARGEPAGES_BIT);
3792}
3793
3794#ifndef SHM_HUGETLB04000
3795 #define SHM_HUGETLB04000 04000
3796#endif
3797
3798#define shm_warning_format(format, ...)do { if (UseLargePages && (!(JVMFlag::is_default(Flag_UseLargePages_enum
)) || !(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::
is_default(Flag_LargePageSizeInBytes_enum)))) { warning(format
, ...); } } while (0)
\
3799 do { \
3800 if (UseLargePages && \
3801 (!FLAG_IS_DEFAULT(UseLargePages)(JVMFlag::is_default(Flag_UseLargePages_enum)) || \
3802 !FLAG_IS_DEFAULT(UseSHM)(JVMFlag::is_default(Flag_UseSHM_enum)) || \
3803 !FLAG_IS_DEFAULT(LargePageSizeInBytes)(JVMFlag::is_default(Flag_LargePageSizeInBytes_enum)))) { \
3804 warning(format, __VA_ARGS__); \
3805 } \
3806 } while (0)
3807
3808#define shm_warning(str)do { if (UseLargePages && (!(JVMFlag::is_default(Flag_UseLargePages_enum
)) || !(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::
is_default(Flag_LargePageSizeInBytes_enum)))) { warning("%s",
str); } } while (0)
shm_warning_format("%s", str)do { if (UseLargePages && (!(JVMFlag::is_default(Flag_UseLargePages_enum
)) || !(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::
is_default(Flag_LargePageSizeInBytes_enum)))) { warning("%s",
str); } } while (0)
3809
3810#define shm_warning_with_errno(str)do { int err = (*__errno_location ()); do { if (UseLargePages
&& (!(JVMFlag::is_default(Flag_UseLargePages_enum)) ||
!(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::is_default
(Flag_LargePageSizeInBytes_enum)))) { warning(str " (error = %d)"
, err); } } while (0); } while (0)
\
3811 do { \
3812 int err = errno(*__errno_location ()); \
3813 shm_warning_format(str " (error = %d)", err)do { if (UseLargePages && (!(JVMFlag::is_default(Flag_UseLargePages_enum
)) || !(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::
is_default(Flag_LargePageSizeInBytes_enum)))) { warning(str " (error = %d)"
, err); } } while (0)
; \
3814 } while (0)
3815
3816static char* shmat_with_alignment(int shmid, size_t bytes, size_t alignment) {
3817 assert(is_aligned(bytes, alignment), "Must be divisible by the alignment")do { if (!(is_aligned(bytes, alignment))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3817, "assert(" "is_aligned(bytes, alignment)" ") failed", "Must be divisible by the alignment"
); ::breakpoint(); } } while (0)
;
3818
3819 if (!is_aligned(alignment, SHMLBA(__getpagesize ()))) {
3820 assert(false, "Code below assumes that alignment is at least SHMLBA aligned")do { if (!(false)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3820, "assert(" "false" ") failed", "Code below assumes that alignment is at least SHMLBA aligned"
); ::breakpoint(); } } while (0)
;
3821 return NULL__null;
3822 }
3823
3824 // To ensure that we get 'alignment' aligned memory from shmat,
3825 // we pre-reserve aligned virtual memory and then attach to that.
3826
3827 char* pre_reserved_addr = anon_mmap_aligned(NULL__null /* req_addr */, bytes, alignment);
3828 if (pre_reserved_addr == NULL__null) {
3829 // Couldn't pre-reserve aligned memory.
3830 shm_warning("Failed to pre-reserve aligned memory for shmat.")do { if (UseLargePages && (!(JVMFlag::is_default(Flag_UseLargePages_enum
)) || !(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::
is_default(Flag_LargePageSizeInBytes_enum)))) { warning("%s",
"Failed to pre-reserve aligned memory for shmat."); } } while
(0)
;
3831 return NULL__null;
3832 }
3833
3834 // SHM_REMAP is needed to allow shmat to map over an existing mapping.
3835 char* addr = (char*)shmat(shmid, pre_reserved_addr, SHM_REMAP040000);
3836
3837 if ((intptr_t)addr == -1) {
3838 int err = errno(*__errno_location ());
3839 shm_warning_with_errno("Failed to attach shared memory.")do { int err = (*__errno_location ()); do { if (UseLargePages
&& (!(JVMFlag::is_default(Flag_UseLargePages_enum)) ||
!(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::is_default
(Flag_LargePageSizeInBytes_enum)))) { warning("Failed to attach shared memory."
" (error = %d)", err); } } while (0); } while (0)
;
3840
3841 assert(err != EACCES, "Unexpected error")do { if (!(err != 13)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3841, "assert(" "err != 13" ") failed", "Unexpected error")
; ::breakpoint(); } } while (0)
;
3842 assert(err != EIDRM, "Unexpected error")do { if (!(err != 43)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3842, "assert(" "err != 43" ") failed", "Unexpected error")
; ::breakpoint(); } } while (0)
;
3843 assert(err != EINVAL, "Unexpected error")do { if (!(err != 22)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3843, "assert(" "err != 22" ") failed", "Unexpected error")
; ::breakpoint(); } } while (0)
;
3844
3845 // Since we don't know if the kernel unmapped the pre-reserved memory area
3846 // we can't unmap it, since that would potentially unmap memory that was
3847 // mapped from other threads.
3848 return NULL__null;
3849 }
3850
3851 return addr;
3852}
3853
3854static char* shmat_at_address(int shmid, char* req_addr) {
3855 if (!is_aligned(req_addr, SHMLBA(__getpagesize ()))) {
3856 assert(false, "Requested address needs to be SHMLBA aligned")do { if (!(false)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3856, "assert(" "false" ") failed", "Requested address needs to be SHMLBA aligned"
); ::breakpoint(); } } while (0)
;
3857 return NULL__null;
3858 }
3859
3860 char* addr = (char*)shmat(shmid, req_addr, 0);
3861
3862 if ((intptr_t)addr == -1) {
3863 shm_warning_with_errno("Failed to attach shared memory.")do { int err = (*__errno_location ()); do { if (UseLargePages
&& (!(JVMFlag::is_default(Flag_UseLargePages_enum)) ||
!(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::is_default
(Flag_LargePageSizeInBytes_enum)))) { warning("Failed to attach shared memory."
" (error = %d)", err); } } while (0); } while (0)
;
3864 return NULL__null;
3865 }
3866
3867 return addr;
3868}
3869
3870static char* shmat_large_pages(int shmid, size_t bytes, size_t alignment, char* req_addr) {
3871 // If a req_addr has been provided, we assume that the caller has already aligned the address.
3872 if (req_addr != NULL__null) {
3873 assert(is_aligned(req_addr, os::large_page_size()), "Must be divisible by the large page size")do { if (!(is_aligned(req_addr, os::large_page_size()))) { (*
g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3873, "assert(" "is_aligned(req_addr, os::large_page_size())"
") failed", "Must be divisible by the large page size"); ::breakpoint
(); } } while (0)
;
3874 assert(is_aligned(req_addr, alignment), "Must be divisible by given alignment")do { if (!(is_aligned(req_addr, alignment))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3874, "assert(" "is_aligned(req_addr, alignment)" ") failed"
, "Must be divisible by given alignment"); ::breakpoint(); } }
while (0)
;
3875 return shmat_at_address(shmid, req_addr);
3876 }
3877
3878 // Since shmid has been setup with SHM_HUGETLB, shmat will automatically
3879 // return large page size aligned memory addresses when req_addr == NULL.
3880 // However, if the alignment is larger than the large page size, we have
3881 // to manually ensure that the memory returned is 'alignment' aligned.
3882 if (alignment > os::large_page_size()) {
3883 assert(is_aligned(alignment, os::large_page_size()), "Must be divisible by the large page size")do { if (!(is_aligned(alignment, os::large_page_size()))) { (
*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3883, "assert(" "is_aligned(alignment, os::large_page_size())"
") failed", "Must be divisible by the large page size"); ::breakpoint
(); } } while (0)
;
3884 return shmat_with_alignment(shmid, bytes, alignment);
3885 } else {
3886 return shmat_at_address(shmid, NULL__null);
3887 }
3888}
3889
3890char* os::Linux::reserve_memory_special_shm(size_t bytes, size_t alignment,
3891 char* req_addr, bool exec) {
3892 // "exec" is passed in but not used. Creating the shared image for
3893 // the code cache doesn't have an SHM_X executable permission to check.
3894 assert(UseLargePages && UseSHM, "only for SHM large pages")do { if (!(UseLargePages && UseSHM)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3894, "assert(" "UseLargePages && UseSHM" ") failed"
, "only for SHM large pages"); ::breakpoint(); } } while (0)
;
3895 assert(is_aligned(req_addr, os::large_page_size()), "Unaligned address")do { if (!(is_aligned(req_addr, os::large_page_size()))) { (*
g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3895, "assert(" "is_aligned(req_addr, os::large_page_size())"
") failed", "Unaligned address"); ::breakpoint(); } } while (
0)
;
3896 assert(is_aligned(req_addr, alignment), "Unaligned address")do { if (!(is_aligned(req_addr, alignment))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3896, "assert(" "is_aligned(req_addr, alignment)" ") failed"
, "Unaligned address"); ::breakpoint(); } } while (0)
;
3897
3898 if (!is_aligned(bytes, os::large_page_size())) {
3899 return NULL__null; // Fallback to small pages.
3900 }
3901
3902 // Create a large shared memory region to attach to based on size.
3903 // Currently, size is the total size of the heap.
3904 int shmid = shmget(IPC_PRIVATE((__key_t) 0), bytes, SHM_HUGETLB04000|IPC_CREAT01000|SHM_R0400|SHM_W0200);
3905 if (shmid == -1) {
3906 // Possible reasons for shmget failure:
3907 // 1. shmmax is too small for the request.
3908 // > check shmmax value: cat /proc/sys/kernel/shmmax
3909 // > increase shmmax value: echo "new_value" > /proc/sys/kernel/shmmax
3910 // 2. not enough large page memory.
3911 // > check available large pages: cat /proc/meminfo
3912 // > increase amount of large pages:
3913 // sysctl -w vm.nr_hugepages=new_value
3914 // > For more information regarding large pages please refer to:
3915 // https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
3916 // Note 1: different Linux may use different name for this property,
3917 // e.g. on Redhat AS-3 it is "hugetlb_pool".
3918 // Note 2: it's possible there's enough physical memory available but
3919 // they are so fragmented after a long run that they can't
3920 // coalesce into large pages. Try to reserve large pages when
3921 // the system is still "fresh".
3922 shm_warning_with_errno("Failed to reserve shared memory.")do { int err = (*__errno_location ()); do { if (UseLargePages
&& (!(JVMFlag::is_default(Flag_UseLargePages_enum)) ||
!(JVMFlag::is_default(Flag_UseSHM_enum)) || !(JVMFlag::is_default
(Flag_LargePageSizeInBytes_enum)))) { warning("Failed to reserve shared memory."
" (error = %d)", err); } } while (0); } while (0)
;
3923 return NULL__null;
3924 }
3925
3926 // Attach to the region.
3927 char* addr = shmat_large_pages(shmid, bytes, alignment, req_addr);
3928
3929 // Remove shmid. If shmat() is successful, the actual shared memory segment
3930 // will be deleted when it's detached by shmdt() or when the process
3931 // terminates. If shmat() is not successful this will remove the shared
3932 // segment immediately.
3933 shmctl(shmid, IPC_RMID0, NULL__null);
3934
3935 return addr;
3936}
3937
3938static void warn_on_commit_special_failure(char* req_addr, size_t bytes,
3939 size_t page_size, int error) {
3940 assert(error == ENOMEM, "Only expect to fail if no memory is available")do { if (!(error == 12)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3940, "assert(" "error == 12" ") failed", "Only expect to fail if no memory is available"
); ::breakpoint(); } } while (0)
;
3941
3942 bool warn_on_failure = UseLargePages &&
3943 (!FLAG_IS_DEFAULT(UseLargePages)(JVMFlag::is_default(Flag_UseLargePages_enum)) ||
3944 !FLAG_IS_DEFAULT(UseHugeTLBFS)(JVMFlag::is_default(Flag_UseHugeTLBFS_enum)) ||
3945 !FLAG_IS_DEFAULT(LargePageSizeInBytes)(JVMFlag::is_default(Flag_LargePageSizeInBytes_enum)));
3946
3947 if (warn_on_failure) {
3948 char msg[128];
3949 jio_snprintf(msg, sizeof(msg), "Failed to reserve and commit memory. req_addr: "
3950 PTR_FORMAT"0x%016" "l" "x" " bytes: " SIZE_FORMAT"%" "l" "u" " page size: "
3951 SIZE_FORMAT"%" "l" "u" " (errno = %d).",
3952 req_addr, bytes, page_size, error);
3953 warning("%s", msg);
3954 }
3955}
3956
3957bool os::Linux::commit_memory_special(size_t bytes,
3958 size_t page_size,
3959 char* req_addr,
3960 bool exec) {
3961 assert(UseLargePages && UseHugeTLBFS, "Should only get here when HugeTLBFS large pages are used")do { if (!(UseLargePages && UseHugeTLBFS)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3961, "assert(" "UseLargePages && UseHugeTLBFS" ") failed"
, "Should only get here when HugeTLBFS large pages are used")
; ::breakpoint(); } } while (0)
;
3962 assert(is_aligned(bytes, page_size), "Unaligned size")do { if (!(is_aligned(bytes, page_size))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3962, "assert(" "is_aligned(bytes, page_size)" ") failed", "Unaligned size"
); ::breakpoint(); } } while (0)
;
3963 assert(is_aligned(req_addr, page_size), "Unaligned address")do { if (!(is_aligned(req_addr, page_size))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3963, "assert(" "is_aligned(req_addr, page_size)" ") failed"
, "Unaligned address"); ::breakpoint(); } } while (0)
;
3964 assert(req_addr != NULL, "Must have a requested address for special mappings")do { if (!(req_addr != __null)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3964, "assert(" "req_addr != __null" ") failed", "Must have a requested address for special mappings"
); ::breakpoint(); } } while (0)
;
3965
3966 int prot = exec ? PROT_READ0x1|PROT_WRITE0x2|PROT_EXEC0x4 : PROT_READ0x1|PROT_WRITE0x2;
3967 int flags = MAP_PRIVATE0x02|MAP_ANONYMOUS0x20|MAP_FIXED0x10;
3968
3969 // For large pages additional flags are required.
3970 if (page_size > (size_t) os::vm_page_size()) {
3971 flags |= MAP_HUGETLB0x40000 | hugetlbfs_page_size_flag(page_size);
3972 }
3973 char* addr = (char*)::mmap(req_addr, bytes, prot, flags, -1, 0);
3974
3975 if (addr == MAP_FAILED((void *) -1)) {
3976 warn_on_commit_special_failure(req_addr, bytes, page_size, errno(*__errno_location ()));
3977 return false;
3978 }
3979
3980 log_debug(pagesize)(!(LogImpl<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag
::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::
__NO_TAG)>::is_level(LogLevel::Debug))) ? (void)0 : LogImpl
<(LogTag::_pagesize), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::write<LogLevel::Debug>
("Commit special mapping: " PTR_FORMAT"0x%016" "l" "x" ", size=" SIZE_FORMAT"%" "l" "u" "%s, page size="
3981 SIZE_FORMAT"%" "l" "u" "%s",
3982 p2i(addr), byte_size_in_exact_unit(bytes),
3983 exact_unit_for_byte_size(bytes),
3984 byte_size_in_exact_unit(page_size),
3985 exact_unit_for_byte_size(page_size));
3986 assert(is_aligned(addr, page_size), "Must be")do { if (!(is_aligned(addr, page_size))) { (*g_assert_poison)
= 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3986, "assert(" "is_aligned(addr, page_size)" ") failed", "Must be"
); ::breakpoint(); } } while (0)
;
3987 return true;
3988}
3989
3990char* os::Linux::reserve_memory_special_huge_tlbfs(size_t bytes,
3991 size_t alignment,
3992 size_t page_size,
3993 char* req_addr,
3994 bool exec) {
3995 assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages")do { if (!(UseLargePages && UseHugeTLBFS)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3995, "assert(" "UseLargePages && UseHugeTLBFS" ") failed"
, "only for Huge TLBFS large pages"); ::breakpoint(); } } while
(0)
;
3996 assert(is_aligned(req_addr, alignment), "Must be")do { if (!(is_aligned(req_addr, alignment))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3996, "assert(" "is_aligned(req_addr, alignment)" ") failed"
, "Must be"); ::breakpoint(); } } while (0)
;
3997 assert(is_aligned(req_addr, page_size), "Must be")do { if (!(is_aligned(req_addr, page_size))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3997, "assert(" "is_aligned(req_addr, page_size)" ") failed"
, "Must be"); ::breakpoint(); } } while (0)
;
3998 assert(is_aligned(alignment, os::vm_allocation_granularity()), "Must be")do { if (!(is_aligned(alignment, os::vm_allocation_granularity
()))) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3998, "assert(" "is_aligned(alignment, os::vm_allocation_granularity())"
") failed", "Must be"); ::breakpoint(); } } while (0)
;
3999 assert(_page_sizes.contains(page_size), "Must be a valid page size")do { if (!(_page_sizes.contains(page_size))) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 3999, "assert(" "_page_sizes.contains(page_size)" ") failed"
, "Must be a valid page size"); ::breakpoint(); } } while (0)
;
4000 assert(page_size > (size_t)os::vm_page_size(), "Must be a large page size")do { if (!(page_size > (size_t)os::vm_page_size())) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4000, "assert(" "page_size > (size_t)os::vm_page_size()"
") failed", "Must be a large page size"); ::breakpoint(); } }
while (0)
;
4001 assert(bytes >= page_size, "Shouldn't allocate large pages for small sizes")do { if (!(bytes >= page_size)) { (*g_assert_poison) = 'X'
;; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4001, "assert(" "bytes >= page_size" ") failed", "Shouldn't allocate large pages for small sizes"
); ::breakpoint(); } } while (0)
;
4002
4003 // We only end up here when at least 1 large page can be used.
4004 // If the size is not a multiple of the large page size, we
4005 // will mix the type of pages used, but in a decending order.
4006 // Start off by reserving a range of the given size that is
4007 // properly aligned. At this point no pages are committed. If
4008 // a requested address is given it will be used and it must be
4009 // aligned to both the large page size and the given alignment.
4010 // The larger of the two will be used.
4011 size_t required_alignment = MAX(page_size, alignment)(((page_size)>(alignment))?(page_size):(alignment));
4012 char* const aligned_start = anon_mmap_aligned(req_addr, bytes, required_alignment);
4013 if (aligned_start == NULL__null) {
4014 return NULL__null;
4015 }
4016
4017 // First commit using large pages.
4018 size_t large_bytes = align_down(bytes, page_size);
4019 bool large_committed = commit_memory_special(large_bytes, page_size, aligned_start, exec);
4020
4021 if (large_committed && bytes == large_bytes) {
4022 // The size was large page aligned so no additional work is
4023 // needed even if the commit failed.
4024 return aligned_start;
4025 }
4026
4027 // The requested size requires some small pages as well.
4028 char* small_start = aligned_start + large_bytes;
4029 size_t small_size = bytes - large_bytes;
4030 if (!large_committed) {
4031 // Failed to commit large pages, so we need to unmap the
4032 // reminder of the orinal reservation.
4033 ::munmap(small_start, small_size);
4034 return NULL__null;
4035 }
4036
4037 // Commit the remaining bytes using small pages.
4038 bool small_committed = commit_memory_special(small_size, os::vm_page_size(), small_start, exec);
4039 if (!small_committed) {
4040 // Failed to commit the remaining size, need to unmap
4041 // the large pages part of the reservation.
4042 ::munmap(aligned_start, large_bytes);
4043 return NULL__null;
4044 }
4045 return aligned_start;
4046}
4047
4048char* os::pd_reserve_memory_special(size_t bytes, size_t alignment, size_t page_size,
4049 char* req_addr, bool exec) {
4050 assert(UseLargePages, "only for large pages")do { if (!(UseLargePages)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4050, "assert(" "UseLargePages" ") failed", "only for large pages"
); ::breakpoint(); } } while (0)
;
4051
4052 char* addr;
4053 if (UseSHM) {
4054 // No support for using specific page sizes with SHM.
4055 addr = os::Linux::reserve_memory_special_shm(bytes, alignment, req_addr, exec);
4056 } else {
4057 assert(UseHugeTLBFS, "must be")do { if (!(UseHugeTLBFS)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4057, "assert(" "UseHugeTLBFS" ") failed", "must be"); ::breakpoint
(); } } while (0)
;
4058 addr = os::Linux::reserve_memory_special_huge_tlbfs(bytes, alignment, page_size, req_addr, exec);
4059 }
4060
4061 if (addr != NULL__null) {
4062 if (UseNUMAInterleaving) {
4063 numa_make_global(addr, bytes);
4064 }
4065 }
4066
4067 return addr;
4068}
4069
4070bool os::Linux::release_memory_special_shm(char* base, size_t bytes) {
4071 // detaching the SHM segment will also delete it, see reserve_memory_special_shm()
4072 return shmdt(base) == 0;
4073}
4074
4075bool os::Linux::release_memory_special_huge_tlbfs(char* base, size_t bytes) {
4076 return pd_release_memory(base, bytes);
4077}
4078
4079bool os::pd_release_memory_special(char* base, size_t bytes) {
4080 assert(UseLargePages, "only for large pages")do { if (!(UseLargePages)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4080, "assert(" "UseLargePages" ") failed", "only for large pages"
); ::breakpoint(); } } while (0)
;
4081 bool res;
4082
4083 if (UseSHM) {
4084 res = os::Linux::release_memory_special_shm(base, bytes);
4085 } else {
4086 assert(UseHugeTLBFS, "must be")do { if (!(UseHugeTLBFS)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4086, "assert(" "UseHugeTLBFS" ") failed", "must be"); ::breakpoint
(); } } while (0)
;
4087 res = os::Linux::release_memory_special_huge_tlbfs(base, bytes);
4088 }
4089 return res;
4090}
4091
4092size_t os::large_page_size() {
4093 return _large_page_size;
4094}
4095
4096// With SysV SHM the entire memory region must be allocated as shared
4097// memory.
4098// HugeTLBFS allows application to commit large page memory on demand.
4099// However, when committing memory with HugeTLBFS fails, the region
4100// that was supposed to be committed will lose the old reservation
4101// and allow other threads to steal that memory region. Because of this
4102// behavior we can't commit HugeTLBFS memory.
4103bool os::can_commit_large_page_memory() {
4104 return UseTransparentHugePages;
4105}
4106
4107bool os::can_execute_large_page_memory() {
4108 return UseTransparentHugePages || UseHugeTLBFS;
4109}
4110
4111char* os::pd_attempt_map_memory_to_file_at(char* requested_addr, size_t bytes, int file_desc) {
4112 assert(file_desc >= 0, "file_desc is not valid")do { if (!(file_desc >= 0)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4112, "assert(" "file_desc >= 0" ") failed", "file_desc is not valid"
); ::breakpoint(); } } while (0)
;
4113 char* result = pd_attempt_reserve_memory_at(requested_addr, bytes, !ExecMem);
4114 if (result != NULL__null) {
4115 if (replace_existing_mapping_with_file_mapping(result, bytes, file_desc) == NULL__null) {
4116 vm_exit_during_initialization(err_msg("Error in mapping Java heap at the given filesystem directory"));
4117 }
4118 }
4119 return result;
4120}
4121
4122// Reserve memory at an arbitrary address, only if that area is
4123// available (and not reserved for something else).
4124
4125char* os::pd_attempt_reserve_memory_at(char* requested_addr, size_t bytes, bool exec) {
4126 // Assert only that the size is a multiple of the page size, since
4127 // that's all that mmap requires, and since that's all we really know
4128 // about at this low abstraction level. If we need higher alignment,
4129 // we can either pass an alignment to this method or verify alignment
4130 // in one of the methods further up the call chain. See bug 5044738.
4131 assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block")do { if (!(bytes % os::vm_page_size() == 0)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4131, "assert(" "bytes % os::vm_page_size() == 0" ") failed"
, "reserving unexpected size block"); ::breakpoint(); } } while
(0)
;
4132
4133 // Repeatedly allocate blocks until the block is allocated at the
4134 // right spot.
4135
4136 // Linux mmap allows caller to pass an address as hint; give it a try first,
4137 // if kernel honors the hint then we can return immediately.
4138 char * addr = anon_mmap(requested_addr, bytes);
4139 if (addr == requested_addr) {
4140 return requested_addr;
4141 }
4142
4143 if (addr != NULL__null) {
4144 // mmap() is successful but it fails to reserve at the requested address
4145 anon_munmap(addr, bytes);
4146 }
4147
4148 return NULL__null;
4149}
4150
4151// Sleep forever; naked call to OS-specific sleep; use with CAUTION
4152void os::infinite_sleep() {
4153 while (true) { // sleep forever ...
4154 ::sleep(100); // ... 100 seconds at a time
4155 }
4156}
4157
4158// Used to convert frequent JVM_Yield() to nops
4159bool os::dont_yield() {
4160 return DontYieldALot;
4161}
4162
4163// Linux CFS scheduler (since 2.6.23) does not guarantee sched_yield(2) will
4164// actually give up the CPU. Since skip buddy (v2.6.28):
4165//
4166// * Sets the yielding task as skip buddy for current CPU's run queue.
4167// * Picks next from run queue, if empty, picks a skip buddy (can be the yielding task).
4168// * Clears skip buddies for this run queue (yielding task no longer a skip buddy).
4169//
4170// An alternative is calling os::naked_short_nanosleep with a small number to avoid
4171// getting re-scheduled immediately.
4172//
4173void os::naked_yield() {
4174 sched_yield();
4175}
4176
4177////////////////////////////////////////////////////////////////////////////////
4178// thread priority support
4179
4180// Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER
4181// only supports dynamic priority, static priority must be zero. For real-time
4182// applications, Linux supports SCHED_RR which allows static priority (1-99).
4183// However, for large multi-threaded applications, SCHED_RR is not only slower
4184// than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out
4185// of 5 runs - Sep 2005).
4186//
4187// The following code actually changes the niceness of kernel-thread/LWP. It
4188// has an assumption that setpriority() only modifies one kernel-thread/LWP,
4189// not the entire user process, and user level threads are 1:1 mapped to kernel
4190// threads. It has always been the case, but could change in the future. For
4191// this reason, the code should not be used as default (ThreadPriorityPolicy=0).
4192// It is only used when ThreadPriorityPolicy=1 and may require system level permission
4193// (e.g., root privilege or CAP_SYS_NICE capability).
4194
4195int os::java_to_os_priority[CriticalPriority + 1] = {
4196 19, // 0 Entry should never be used
4197
4198 4, // 1 MinPriority
4199 3, // 2
4200 2, // 3
4201
4202 1, // 4
4203 0, // 5 NormPriority
4204 -1, // 6
4205
4206 -2, // 7
4207 -3, // 8
4208 -4, // 9 NearMaxPriority
4209
4210 -5, // 10 MaxPriority
4211
4212 -5 // 11 CriticalPriority
4213};
4214
4215static int prio_init() {
4216 if (ThreadPriorityPolicy == 1) {
4217 if (geteuid() != 0) {
4218 if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)(JVMFlag::is_default(Flag_ThreadPriorityPolicy_enum)) && !FLAG_IS_JIMAGE_RESOURCE(ThreadPriorityPolicy)(JVMFlag::is_jimage_resource(Flag_ThreadPriorityPolicy_enum))) {
4219 warning("-XX:ThreadPriorityPolicy=1 may require system level permission, " \
4220 "e.g., being the root user. If the necessary permission is not " \
4221 "possessed, changes to priority will be silently ignored.");
4222 }
4223 }
4224 }
4225 if (UseCriticalJavaThreadPriority) {
4226 os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];
4227 }
4228 return 0;
4229}
4230
4231OSReturn os::set_native_priority(Thread* thread, int newpri) {
4232 if (!UseThreadPriorities || ThreadPriorityPolicy == 0) return OS_OK;
4233
4234 int ret = setpriority(PRIO_PROCESSPRIO_PROCESS, thread->osthread()->thread_id(), newpri);
4235 return (ret == 0) ? OS_OK : OS_ERR;
4236}
4237
4238OSReturn os::get_native_priority(const Thread* const thread,
4239 int *priority_ptr) {
4240 if (!UseThreadPriorities || ThreadPriorityPolicy == 0) {
4241 *priority_ptr = java_to_os_priority[NormPriority];
4242 return OS_OK;
4243 }
4244
4245 errno(*__errno_location ()) = 0;
4246 *priority_ptr = getpriority(PRIO_PROCESSPRIO_PROCESS, thread->osthread()->thread_id());
4247 return (*priority_ptr != -1 || errno(*__errno_location ()) == 0 ? OS_OK : OS_ERR);
4248}
4249
4250// This is the fastest way to get thread cpu time on Linux.
4251// Returns cpu time (user+sys) for any thread, not only for current.
4252// POSIX compliant clocks are implemented in the kernels 2.6.16+.
4253// It might work on 2.6.10+ with a special kernel/glibc patch.
4254// For reference, please, see IEEE Std 1003.1-2004:
4255// http://www.unix.org/single_unix_specification
4256
4257jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
4258 struct timespec tp;
4259 int status = clock_gettime(clockid, &tp);
4260 assert(status == 0, "clock_gettime error: %s", os::strerror(errno))do { if (!(status == 0)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4260, "assert(" "status == 0" ") failed", "clock_gettime error: %s"
, os::strerror((*__errno_location ()))); ::breakpoint(); } } while
(0)
;
4261 return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;
4262}
4263
4264// Determine if the vmid is the parent pid for a child in a PID namespace.
4265// Return the namespace pid if so, otherwise -1.
4266int os::Linux::get_namespace_pid(int vmid) {
4267 char fname[24];
4268 int retpid = -1;
4269
4270 snprintf(fname, sizeof(fname), "/proc/%d/status", vmid);
4271 FILE *fp = fopen(fname, "r");
4272
4273 if (fp) {
4274 int pid, nspid;
4275 int ret;
4276 while (!feof(fp) && !ferror(fp)) {
4277 ret = fscanf(fp, "NSpid: %d %d", &pid, &nspid);
4278 if (ret == 1) {
4279 break;
4280 }
4281 if (ret == 2) {
4282 retpid = nspid;
4283 break;
4284 }
4285 for (;;) {
4286 int ch = fgetc(fp);
4287 if (ch == EOF(-1) || ch == (int)'\n') break;
4288 }
4289 }
4290 fclose(fp);
4291 }
4292 return retpid;
4293}
4294
4295extern void report_error(char* file_name, int line_no, char* title,
4296 char* format, ...);
4297
4298// Some linux distributions (notably: Alpine Linux) include the
4299// grsecurity in the kernel. Of particular interest from a JVM perspective
4300// is PaX (https://pax.grsecurity.net/), which adds some security features
4301// related to page attributes. Specifically, the MPROTECT PaX functionality
4302// (https://pax.grsecurity.net/docs/mprotect.txt) prevents dynamic
4303// code generation by disallowing a (previously) writable page to be
4304// marked as executable. This is, of course, exactly what HotSpot does
4305// for both JIT compiled method, as well as for stubs, adapters, etc.
4306//
4307// Instead of crashing "lazily" when trying to make a page executable,
4308// this code probes for the presence of PaX and reports the failure
4309// eagerly.
4310static void check_pax(void) {
4311 // Zero doesn't generate code dynamically, so no need to perform the PaX check
4312#ifndef ZERO
4313 size_t size = os::Linux::page_size();
4314
4315 void* p = ::mmap(NULL__null, size, PROT_WRITE0x2, MAP_PRIVATE0x02|MAP_ANONYMOUS0x20, -1, 0);
4316 if (p == MAP_FAILED((void *) -1)) {
4317 log_debug(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Debug))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Debug>
("os_linux.cpp: check_pax: mmap failed (%s)" , os::strerror(errno(*__errno_location ())));
4318 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "failed to allocate memory for PaX check.")do { report_vm_out_of_memory("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4318, size, OOM_MMAP_ERROR, "failed to allocate memory for PaX check."
); ::breakpoint(); } while (0)
;
4319 }
4320
4321 int res = ::mprotect(p, size, PROT_WRITE0x2|PROT_EXEC0x4);
4322 if (res == -1) {
4323 log_debug(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Debug))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Debug>
("os_linux.cpp: check_pax: mprotect failed (%s)" , os::strerror(errno(*__errno_location ())));
4324 vm_exit_during_initialization(
4325 "Failed to mark memory page as executable - check if grsecurity/PaX is enabled");
4326 }
4327
4328 ::munmap(p, size);
4329#endif
4330}
4331
4332// this is called _before_ most of the global arguments have been parsed
4333void os::init(void) {
4334 char dummy; // used to get a guess on initial stack address
4335
4336 clock_tics_per_sec = sysconf(_SC_CLK_TCK_SC_CLK_TCK);
4337
4338 Linux::set_page_size(sysconf(_SC_PAGESIZE_SC_PAGESIZE));
4339 if (Linux::page_size() == -1) {
4340 fatal("os_linux.cpp: os::init: sysconf failed (%s)",do { (*g_assert_poison) = 'X';; report_fatal(INTERNAL_ERROR, "/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4341, "os_linux.cpp: os::init: sysconf failed (%s)", os::strerror
((*__errno_location ()))); ::breakpoint(); } while (0)
4341 os::strerror(errno))do { (*g_assert_poison) = 'X';; report_fatal(INTERNAL_ERROR, "/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4341, "os_linux.cpp: os::init: sysconf failed (%s)", os::strerror
((*__errno_location ()))); ::breakpoint(); } while (0)
;
4342 }
4343 _page_sizes.add(Linux::page_size());
4344
4345 Linux::initialize_system_info();
4346
4347#ifdef __GLIBC__2
4348 Linux::_mallinfo = CAST_TO_FN_PTR(Linux::mallinfo_func_t, dlsym(RTLD_DEFAULT, "mallinfo"))(reinterpret_cast<Linux::mallinfo_func_t>(dlsym(((void *
) 0), "mallinfo")))
;
4349 Linux::_mallinfo2 = CAST_TO_FN_PTR(Linux::mallinfo2_func_t, dlsym(RTLD_DEFAULT, "mallinfo2"))(reinterpret_cast<Linux::mallinfo2_func_t>(dlsym(((void
*) 0), "mallinfo2")))
;
4350#endif // __GLIBC__
4351
4352 os::Linux::CPUPerfTicks pticks;
4353 bool res = os::Linux::get_tick_information(&pticks, -1);
4354
4355 if (res && pticks.has_steal_ticks) {
4356 has_initial_tick_info = true;
4357 initial_total_ticks = pticks.total;
4358 initial_steal_ticks = pticks.steal;
4359 }
4360
4361 // _main_thread points to the thread that created/loaded the JVM.
4362 Linux::_main_thread = pthread_self();
4363
4364 // retrieve entry point for pthread_setname_np
4365 Linux::_pthread_setname_np =
4366 (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT((void *) 0), "pthread_setname_np");
4367
4368 check_pax();
4369
4370 os::Posix::init();
4371}
4372
4373// To install functions for atexit system call
4374extern "C" {
4375 static void perfMemory_exit_helper() {
4376 perfMemory_exit();
4377 }
4378}
4379
4380void os::pd_init_container_support() {
4381 OSContainer::init();
4382}
4383
4384void os::Linux::numa_init() {
4385
4386 // Java can be invoked as
4387 // 1. Without numactl and heap will be allocated/configured on all nodes as
4388 // per the system policy.
4389 // 2. With numactl --interleave:
4390 // Use numa_get_interleave_mask(v2) API to get nodes bitmask. The same
4391 // API for membind case bitmask is reset.
4392 // Interleave is only hint and Kernel can fallback to other nodes if
4393 // no memory is available on the target nodes.
4394 // 3. With numactl --membind:
4395 // Use numa_get_membind(v2) API to get nodes bitmask. The same API for
4396 // interleave case returns bitmask of all nodes.
4397 // numa_all_nodes_ptr holds bitmask of all nodes.
4398 // numa_get_interleave_mask(v2) and numa_get_membind(v2) APIs returns correct
4399 // bitmask when externally configured to run on all or fewer nodes.
4400
4401 if (!Linux::libnuma_init()) {
4402 FLAG_SET_ERGO(UseNUMA, false)(Flag_UseNUMA_set((false), JVMFlagOrigin::ERGONOMIC));
4403 FLAG_SET_ERGO(UseNUMAInterleaving, false)(Flag_UseNUMAInterleaving_set((false), JVMFlagOrigin::ERGONOMIC
))
; // Also depends on libnuma.
4404 } else {
4405 if ((Linux::numa_max_node() < 1) || Linux::is_bound_to_single_node()) {
4406 // If there's only one node (they start from 0) or if the process
4407 // is bound explicitly to a single node using membind, disable NUMA
4408 UseNUMA = false;
4409 } else {
4410 LogTarget(Info,os)LogTargetImpl<LogLevel::Info, (LogTag::_os), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
, (LogTag::__NO_TAG)>
log;
4411 LogStream ls(log);
4412
4413 Linux::set_configured_numa_policy(Linux::identify_numa_policy());
4414
4415 struct bitmask* bmp = Linux::_numa_membind_bitmask;
4416 const char* numa_mode = "membind";
4417
4418 if (Linux::is_running_in_interleave_mode()) {
4419 bmp = Linux::_numa_interleave_bitmask;
4420 numa_mode = "interleave";
4421 }
4422
4423 ls.print("UseNUMA is enabled and invoked in '%s' mode."
4424 " Heap will be configured using NUMA memory nodes:", numa_mode);
4425
4426 for (int node = 0; node <= Linux::numa_max_node(); node++) {
4427 if (Linux::_numa_bitmask_isbitset(bmp, node)) {
4428 ls.print(" %d", node);
4429 }
4430 }
4431 }
4432 }
4433
4434 // When NUMA requested, not-NUMA-aware allocations default to interleaving.
4435 if (UseNUMA && !UseNUMAInterleaving) {
4436 FLAG_SET_ERGO_IF_DEFAULT(UseNUMAInterleaving, true)do { if ((JVMFlag::is_default(Flag_UseNUMAInterleaving_enum))
) { (Flag_UseNUMAInterleaving_set((true), JVMFlagOrigin::ERGONOMIC
)); } } while (0)
;
4437 }
4438
4439 if (UseParallelGC && UseNUMA && UseLargePages && !can_commit_large_page_memory()) {
4440 // With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way
4441 // we can make the adaptive lgrp chunk resizing work. If the user specified both
4442 // UseNUMA and UseLargePages (or UseSHM/UseHugeTLBFS) on the command line - warn
4443 // and disable adaptive resizing.
4444 if (UseAdaptiveSizePolicy || UseAdaptiveNUMAChunkSizing) {
4445 warning("UseNUMA is not fully compatible with SHM/HugeTLBFS large pages, "
4446 "disabling adaptive resizing (-XX:-UseAdaptiveSizePolicy -XX:-UseAdaptiveNUMAChunkSizing)");
4447 UseAdaptiveSizePolicy = false;
4448 UseAdaptiveNUMAChunkSizing = false;
4449 }
4450 }
4451}
4452
4453// this is called _after_ the global arguments have been parsed
4454jint os::init_2(void) {
4455
4456 // This could be set after os::Posix::init() but all platforms
4457 // have to set it the same so we have to mirror Solaris.
4458 DEBUG_ONLY(os::set_mutex_init_done();)os::set_mutex_init_done();
4459
4460 os::Posix::init_2();
4461
4462 Linux::fast_thread_clock_init();
4463
4464 if (PosixSignals::init() == JNI_ERR(-1)) {
4465 return JNI_ERR(-1);
4466 }
4467
4468 if (AdjustStackSizeForTLS) {
4469 get_minstack_init();
4470 }
4471
4472 // Check and sets minimum stack sizes against command line options
4473 if (Posix::set_minimum_stack_sizes() == JNI_ERR(-1)) {
4474 return JNI_ERR(-1);
4475 }
4476
4477#if defined(IA32) && !defined(ZERO)
4478 // Need to ensure we've determined the process's initial stack to
4479 // perform the workaround
4480 Linux::capture_initial_stack(JavaThread::stack_size_at_create());
4481 workaround_expand_exec_shield_cs_limit();
4482#else
4483 suppress_primordial_thread_resolution = Arguments::created_by_java_launcher();
4484 if (!suppress_primordial_thread_resolution) {
4485 Linux::capture_initial_stack(JavaThread::stack_size_at_create());
4486 }
4487#endif
4488
4489 Linux::libpthread_init();
4490 Linux::sched_getcpu_init();
4491 log_info(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("HotSpot is running with %s, %s",
4492 Linux::libc_version(), Linux::libpthread_version());
4493
4494 if (UseNUMA || UseNUMAInterleaving) {
4495 Linux::numa_init();
4496 }
4497
4498 if (MaxFDLimit) {
4499 // set the number of file descriptors to max. print out error
4500 // if getrlimit/setrlimit fails but continue regardless.
4501 struct rlimit nbr_files;
4502 int status = getrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &nbr_files);
4503 if (status != 0) {
4504 log_info(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("os::init_2 getrlimit failed: %s", os::strerror(errno(*__errno_location ())));
4505 } else {
4506 nbr_files.rlim_cur = nbr_files.rlim_max;
4507 status = setrlimit(RLIMIT_NOFILERLIMIT_NOFILE, &nbr_files);
4508 if (status != 0) {
4509 log_info(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Info))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Info>
("os::init_2 setrlimit failed: %s", os::strerror(errno(*__errno_location ())));
4510 }
4511 }
4512 }
4513
4514 // at-exit methods are called in the reverse order of their registration.
4515 // atexit functions are called on return from main or as a result of a
4516 // call to exit(3C). There can be only 32 of these functions registered
4517 // and atexit() does not set errno.
4518
4519 if (PerfAllowAtExitRegistration) {
4520 // only register atexit functions if PerfAllowAtExitRegistration is set.
4521 // atexit functions can be delayed until process exit time, which
4522 // can be problematic for embedded VM situations. Embedded VMs should
4523 // call DestroyJavaVM() to assure that VM resources are released.
4524
4525 // note: perfMemory_exit_helper atexit function may be removed in
4526 // the future if the appropriate cleanup code can be added to the
4527 // VM_Exit VMOperation's doit method.
4528 if (atexit(perfMemory_exit_helper) != 0) {
4529 warning("os::init_2 atexit(perfMemory_exit_helper) failed");
4530 }
4531 }
4532
4533 // initialize thread priority policy
4534 prio_init();
4535
4536 if (!FLAG_IS_DEFAULT(AllocateHeapAt)(JVMFlag::is_default(Flag_AllocateHeapAt_enum))) {
4537 set_coredump_filter(DAX_SHARED_BIT);
4538 }
4539
4540 if (DumpPrivateMappingsInCore) {
4541 set_coredump_filter(FILE_BACKED_PVT_BIT);
4542 }
4543
4544 if (DumpSharedMappingsInCore) {
4545 set_coredump_filter(FILE_BACKED_SHARED_BIT);
4546 }
4547
4548 if (DumpPerfMapAtExit && FLAG_IS_DEFAULT(UseCodeCacheFlushing)(JVMFlag::is_default(Flag_UseCodeCacheFlushing_enum))) {
4549 // Disable code cache flushing to ensure the map file written at
4550 // exit contains all nmethods generated during execution.
4551 FLAG_SET_DEFAULT(UseCodeCacheFlushing, false)((UseCodeCacheFlushing) = (false));
4552 }
4553
4554 return JNI_OK0;
4555}
4556
4557// older glibc versions don't have this macro (which expands to
4558// an optimized bit-counting function) so we have to roll our own
4559#ifndef CPU_COUNT
4560
4561static int _cpu_count(const cpu_set_t* cpus) {
4562 int count = 0;
4563 // only look up to the number of configured processors
4564 for (int i = 0; i < os::processor_count(); i++) {
4565 if (CPU_ISSET(i, cpus)(__extension__ ({ size_t __cpu = (i); __cpu / 8 < (sizeof (
cpu_set_t)) ? ((((const __cpu_mask *) ((cpus)->__bits))[((
__cpu) / (8 * sizeof (__cpu_mask)))] & ((__cpu_mask) 1 <<
((__cpu) % (8 * sizeof (__cpu_mask)))))) != 0 : 0; }))
) {
4566 count++;
4567 }
4568 }
4569 return count;
4570}
4571
4572#define CPU_COUNT(cpus)__sched_cpucount (sizeof (cpu_set_t), cpus) _cpu_count(cpus)
4573
4574#endif // CPU_COUNT
4575
4576// Get the current number of available processors for this process.
4577// This value can change at any time during a process's lifetime.
4578// sched_getaffinity gives an accurate answer as it accounts for cpusets.
4579// If it appears there may be more than 1024 processors then we do a
4580// dynamic check - see 6515172 for details.
4581// If anything goes wrong we fallback to returning the number of online
4582// processors - which can be greater than the number available to the process.
4583int os::Linux::active_processor_count() {
4584 cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors
4585 cpu_set_t* cpus_p = &cpus;
4586 int cpus_size = sizeof(cpu_set_t);
4587
4588 int configured_cpus = os::processor_count(); // upper bound on available cpus
4589 int cpu_count = 0;
4590
4591// old build platforms may not support dynamic cpu sets
4592#ifdef CPU_ALLOC
4593
4594 // To enable easy testing of the dynamic path on different platforms we
4595 // introduce a diagnostic flag: UseCpuAllocPath
4596 if (configured_cpus >= CPU_SETSIZE1024 || UseCpuAllocPath) {
4597 // kernel may use a mask bigger than cpu_set_t
4598 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: using dynamic path %s"
4599 "- configured processors: %d",
4600 UseCpuAllocPath ? "(forced) " : "",
4601 configured_cpus);
4602 cpus_p = CPU_ALLOC(configured_cpus)__sched_cpualloc (configured_cpus);
4603 if (cpus_p != NULL__null) {
4604 cpus_size = CPU_ALLOC_SIZE(configured_cpus)((((configured_cpus) + (8 * sizeof (__cpu_mask)) - 1) / (8 * sizeof
(__cpu_mask))) * sizeof (__cpu_mask))
;
4605 // zero it just to be safe
4606 CPU_ZERO_S(cpus_size, cpus_p)do __builtin_memset (cpus_p, '\0', cpus_size); while (0);
4607 }
4608 else {
4609 // failed to allocate so fallback to online cpus
4610 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN);
4611 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: "
4612 "CPU_ALLOC failed (%s) - using "
4613 "online processor count: %d",
4614 os::strerror(errno(*__errno_location ())), online_cpus);
4615 return online_cpus;
4616 }
4617 }
4618 else {
4619 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: using static path - configured processors: %d",
4620 configured_cpus);
4621 }
4622#else // CPU_ALLOC
4623// these stubs won't be executed
4624#define CPU_COUNT_S(size, cpus)__sched_cpucount (size, cpus) -1
4625#define CPU_FREE(cpus)__sched_cpufree (cpus)
4626
4627 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: only static path available - configured processors: %d",
4628 configured_cpus);
4629#endif // CPU_ALLOC
4630
4631 // pid 0 means the current thread - which we have to assume represents the process
4632 if (sched_getaffinity(0, cpus_size, cpus_p) == 0) {
4633 if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
4634 cpu_count = CPU_COUNT_S(cpus_size, cpus_p)__sched_cpucount (cpus_size, cpus_p);
4635 }
4636 else {
4637 cpu_count = CPU_COUNT(cpus_p)__sched_cpucount (sizeof (cpu_set_t), cpus_p);
4638 }
4639 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: sched_getaffinity processor count: %d", cpu_count);
4640 }
4641 else {
4642 cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN_SC_NPROCESSORS_ONLN);
4643 warning("sched_getaffinity failed (%s)- using online processor count (%d) "
4644 "which may exceed available processors", os::strerror(errno(*__errno_location ())), cpu_count);
4645 }
4646
4647 if (cpus_p != &cpus) { // can only be true when CPU_ALLOC used
4648 CPU_FREE(cpus_p)__sched_cpufree (cpus_p);
4649 }
4650
4651 assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check")do { if (!(cpu_count > 0 && cpu_count <= os::processor_count
())) { (*g_assert_poison) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4651, "assert(" "cpu_count > 0 && cpu_count <= os::processor_count()"
") failed", "sanity check"); ::breakpoint(); } } while (0)
;
4652 return cpu_count;
4653}
4654
4655// Determine the active processor count from one of
4656// three different sources:
4657//
4658// 1. User option -XX:ActiveProcessorCount
4659// 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)
4660// 3. extracted from cgroup cpu subsystem (shares and quotas)
4661//
4662// Option 1, if specified, will always override.
4663// If the cgroup subsystem is active and configured, we
4664// will return the min of the cgroup and option 2 results.
4665// This is required since tools, such as numactl, that
4666// alter cpu affinity do not update cgroup subsystem
4667// cpuset configuration files.
4668int os::active_processor_count() {
4669 // User has overridden the number of active processors
4670 if (ActiveProcessorCount > 0) {
4671 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: "
4672 "active processor count set by user : %d",
4673 ActiveProcessorCount);
4674 return ActiveProcessorCount;
4675 }
4676
4677 int active_cpus;
4678 if (OSContainer::is_containerized()) {
4679 active_cpus = OSContainer::active_processor_count();
4680 log_trace(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Trace))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Trace>
("active_processor_count: determined by OSContainer: %d",
4681 active_cpus);
4682 } else {
4683 active_cpus = os::Linux::active_processor_count();
4684 }
4685
4686 return active_cpus;
4687}
4688
4689static bool should_warn_invalid_processor_id() {
4690 if (os::processor_count() == 1) {
4691 // Don't warn if we only have one processor
4692 return false;
4693 }
4694
4695 static volatile int warn_once = 1;
4696
4697 if (Atomic::load(&warn_once) == 0 ||
4698 Atomic::xchg(&warn_once, 0) == 0) {
4699 // Don't warn more than once
4700 return false;
4701 }
4702
4703 return true;
4704}
4705
4706uint os::processor_id() {
4707 const int id = Linux::sched_getcpu();
4708
4709 if (id < processor_count()) {
4710 return (uint)id;
4711 }
4712
4713 // Some environments (e.g. openvz containers and the rr debugger) incorrectly
4714 // report a processor id that is higher than the number of processors available.
4715 // This is problematic, for example, when implementing CPU-local data structures,
4716 // where the processor id is used to index into an array of length processor_count().
4717 // If this happens we return 0 here. This is is safe since we always have at least
4718 // one processor, but it's not optimal for performance if we're actually executing
4719 // in an environment with more than one processor.
4720 if (should_warn_invalid_processor_id()) {
4721 log_warning(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Warning))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Warning>
("Invalid processor id reported by the operating system "
4722 "(got processor id %d, valid processor id range is 0-%d)",
4723 id, processor_count() - 1);
4724 log_warning(os)(!(LogImpl<(LogTag::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG)
>::is_level(LogLevel::Warning))) ? (void)0 : LogImpl<(LogTag
::_os), (LogTag::__NO_TAG), (LogTag::__NO_TAG), (LogTag::__NO_TAG
), (LogTag::__NO_TAG), (LogTag::__NO_TAG)>::write<LogLevel
::Warning>
("Falling back to assuming processor id is 0. "
4725 "This could have a negative impact on performance.");
4726 }
4727
4728 return 0;
4729}
4730
4731void os::set_native_thread_name(const char *name) {
4732 if (Linux::_pthread_setname_np) {
4733 char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
4734 snprintf(buf, sizeof(buf), "%s", name);
4735 buf[sizeof(buf) - 1] = '\0';
4736 const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
4737 // ERANGE should not happen; all other errors should just be ignored.
4738 assert(rc != ERANGE, "pthread_setname_np failed")do { if (!(rc != 34)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 4738, "assert(" "rc != 34" ") failed", "pthread_setname_np failed"
); ::breakpoint(); } } while (0)
;
4739 }
4740}
4741
4742////////////////////////////////////////////////////////////////////////////////
4743// debug support
4744
4745bool os::find(address addr, outputStream* st) {
4746 Dl_info dlinfo;
4747 memset(&dlinfo, 0, sizeof(dlinfo));
4748 if (dladdr(addr, &dlinfo) != 0) {
4749 st->print(PTR_FORMAT"0x%016" "l" "x" ": ", p2i(addr));
4750 if (dlinfo.dli_sname != NULL__null && dlinfo.dli_saddr != NULL__null) {
4751 st->print("%s+" PTR_FORMAT"0x%016" "l" "x", dlinfo.dli_sname,
4752 p2i(addr) - p2i(dlinfo.dli_saddr));
4753 } else if (dlinfo.dli_fbase != NULL__null) {
4754 st->print("<offset " PTR_FORMAT"0x%016" "l" "x" ">", p2i(addr) - p2i(dlinfo.dli_fbase));
4755 } else {
4756 st->print("<absolute address>");
4757 }
4758 if (dlinfo.dli_fname != NULL__null) {
4759 st->print(" in %s", dlinfo.dli_fname);
4760 }
4761 if (dlinfo.dli_fbase != NULL__null) {
4762 st->print(" at " PTR_FORMAT"0x%016" "l" "x", p2i(dlinfo.dli_fbase));
4763 }
4764 st->cr();
4765
4766 if (Verbose) {
4767 // decode some bytes around the PC
4768 address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());
4769 address end = clamp_address_in_page(addr+40, addr, os::vm_page_size());
4770 address lowest = (address) dlinfo.dli_sname;
4771 if (!lowest) lowest = (address) dlinfo.dli_fbase;
4772 if (begin < lowest) begin = lowest;
4773 Dl_info dlinfo2;
4774 if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr
4775 && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin) {
4776 end = (address) dlinfo2.dli_saddr;
4777 }
4778 Disassembler::decode(begin, end, st);
4779 }
4780 return true;
4781 }
4782 return false;
4783}
4784
4785////////////////////////////////////////////////////////////////////////////////
4786// misc
4787
4788// This does not do anything on Linux. This is basically a hook for being
4789// able to use structured exception handling (thread-local exception filters)
4790// on, e.g., Win32.
4791void
4792os::os_exception_wrapper(java_call_t f, JavaValue* value, const methodHandle& method,
4793 JavaCallArguments* args, JavaThread* thread) {
4794 f(value, method, args, thread);
4795}
4796
4797void os::print_statistics() {
4798}
4799
4800bool os::message_box(const char* title, const char* message) {
4801 int i;
4802 fdStream err(defaultStream::error_fd());
4803 for (i = 0; i < 78; i++) err.print_raw("=");
4804 err.cr();
4805 err.print_raw_cr(title);
4806 for (i = 0; i < 78; i++) err.print_raw("-");
4807 err.cr();
4808 err.print_raw_cr(message);
4809 for (i = 0; i < 78; i++) err.print_raw("=");
4810 err.cr();
4811
4812 char buf[16];
4813 // Prevent process from exiting upon "read error" without consuming all CPU
4814 while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
4815
4816 return buf[0] == 'y' || buf[0] == 'Y';
4817}
4818
4819// Is a (classpath) directory empty?
4820bool os::dir_is_empty(const char* path) {
4821 DIR *dir = NULL__null;
4822 struct dirent *ptr;
4823
4824 dir = opendir(path);
4825 if (dir == NULL__null) return true;
4826
4827 // Scan the directory
4828 bool result = true;
4829 while (result && (ptr = readdir(dir)) != NULL__null) {
4830 if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
4831 result = false;
4832 }
4833 }
4834 closedir(dir);
4835 return result;
4836}
4837
4838// This code originates from JDK's sysOpen and open64_w
4839// from src/solaris/hpi/src/system_md.c
4840
4841int os::open(const char *path, int oflag, int mode) {
4842 if (strlen(path) > MAX_PATH(2 * K) - 1) {
4843 errno(*__errno_location ()) = ENAMETOOLONG36;
4844 return -1;
4845 }
4846
4847 // All file descriptors that are opened in the Java process and not
4848 // specifically destined for a subprocess should have the close-on-exec
4849 // flag set. If we don't set it, then careless 3rd party native code
4850 // might fork and exec without closing all appropriate file descriptors
4851 // (e.g. as we do in closeDescriptors in UNIXProcess.c), and this in
4852 // turn might:
4853 //
4854 // - cause end-of-file to fail to be detected on some file
4855 // descriptors, resulting in mysterious hangs, or
4856 //
4857 // - might cause an fopen in the subprocess to fail on a system
4858 // suffering from bug 1085341.
4859 //
4860 // (Yes, the default setting of the close-on-exec flag is a Unix
4861 // design flaw)
4862 //
4863 // See:
4864 // 1085341: 32-bit stdio routines should support file descriptors >255
4865 // 4843136: (process) pipe file descriptor from Runtime.exec not being closed
4866 // 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9
4867 //
4868 // Modern Linux kernels (after 2.6.23 2007) support O_CLOEXEC with open().
4869 // O_CLOEXEC is preferable to using FD_CLOEXEC on an open file descriptor
4870 // because it saves a system call and removes a small window where the flag
4871 // is unset. On ancient Linux kernels the O_CLOEXEC flag will be ignored
4872 // and we fall back to using FD_CLOEXEC (see below).
4873#ifdef O_CLOEXEC02000000
4874 oflag |= O_CLOEXEC02000000;
4875#endif
4876
4877 int fd = ::open64(path, oflag, mode);
4878 if (fd == -1) return -1;
4879
4880 //If the open succeeded, the file might still be a directory
4881 {
4882 struct stat64 buf64;
4883 int ret = ::fstat64(fd, &buf64);
4884 int st_mode = buf64.st_mode;
4885
4886 if (ret != -1) {
4887 if ((st_mode & S_IFMT0170000) == S_IFDIR0040000) {
4888 errno(*__errno_location ()) = EISDIR21;
4889 ::close(fd);
4890 return -1;
4891 }
4892 } else {
4893 ::close(fd);
4894 return -1;
4895 }
4896 }
4897
4898#ifdef FD_CLOEXEC1
4899 // Validate that the use of the O_CLOEXEC flag on open above worked.
4900 // With recent kernels, we will perform this check exactly once.
4901 static sig_atomic_t O_CLOEXEC_is_known_to_work = 0;
4902 if (!O_CLOEXEC_is_known_to_work) {
4903 int flags = ::fcntl(fd, F_GETFD1);
4904 if (flags != -1) {
4905 if ((flags & FD_CLOEXEC1) != 0)
4906 O_CLOEXEC_is_known_to_work = 1;
4907 else
4908 ::fcntl(fd, F_SETFD2, flags | FD_CLOEXEC1);
4909 }
4910 }
4911#endif
4912
4913 return fd;
4914}
4915
4916
4917// create binary file, rewriting existing file if required
4918int os::create_binary_file(const char* path, bool rewrite_existing) {
4919 int oflags = O_WRONLY01 | O_CREAT0100;
4920 oflags |= rewrite_existing ? O_TRUNC01000 : O_EXCL0200;
4921 return ::open64(path, oflags, S_IREAD0400 | S_IWRITE0200);
4922}
4923
4924// return current position of file pointer
4925jlong os::current_file_offset(int fd) {
4926 return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR1);
4927}
4928
4929// move file pointer to the specified offset
4930jlong os::seek_to_file_offset(int fd, jlong offset) {
4931 return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET0);
4932}
4933
4934// This code originates from JDK's sysAvailable
4935// from src/solaris/hpi/src/native_threads/src/sys_api_td.c
4936
4937int os::available(int fd, jlong *bytes) {
4938 jlong cur, end;
4939 int mode;
4940 struct stat64 buf64;
4941
4942 if (::fstat64(fd, &buf64) >= 0) {
4943 mode = buf64.st_mode;
4944 if (S_ISCHR(mode)((((mode)) & 0170000) == (0020000)) || S_ISFIFO(mode)((((mode)) & 0170000) == (0010000)) || S_ISSOCK(mode)((((mode)) & 0170000) == (0140000))) {
4945 int n;
4946 if (::ioctl(fd, FIONREAD0x541B, &n) >= 0) {
4947 *bytes = n;
4948 return 1;
4949 }
4950 }
4951 }
4952 if ((cur = ::lseek64(fd, 0L, SEEK_CUR1)) == -1) {
4953 return 0;
4954 } else if ((end = ::lseek64(fd, 0L, SEEK_END2)) == -1) {
4955 return 0;
4956 } else if (::lseek64(fd, cur, SEEK_SET0) == -1) {
4957 return 0;
4958 }
4959 *bytes = end - cur;
4960 return 1;
4961}
4962
4963// Map a block of memory.
4964char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,
4965 char *addr, size_t bytes, bool read_only,
4966 bool allow_exec) {
4967 int prot;
4968 int flags = MAP_PRIVATE0x02;
4969
4970 if (read_only) {
4971 prot = PROT_READ0x1;
4972 } else {
4973 prot = PROT_READ0x1 | PROT_WRITE0x2;
4974 }
4975
4976 if (allow_exec) {
4977 prot |= PROT_EXEC0x4;
4978 }
4979
4980 if (addr != NULL__null) {
4981 flags |= MAP_FIXED0x10;
4982 }
4983
4984 char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
4985 fd, file_offset);
4986 if (mapped_address == MAP_FAILED((void *) -1)) {
4987 return NULL__null;
4988 }
4989 return mapped_address;
4990}
4991
4992
4993// Remap a block of memory.
4994char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,
4995 char *addr, size_t bytes, bool read_only,
4996 bool allow_exec) {
4997 // same as map_memory() on this OS
4998 return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
4999 allow_exec);
5000}
5001
5002
5003// Unmap a block of memory.
5004bool os::pd_unmap_memory(char* addr, size_t bytes) {
5005 return munmap(addr, bytes) == 0;
5006}
5007
5008static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
5009
5010static jlong fast_cpu_time(Thread *thread) {
5011 clockid_t clockid;
5012 int rc = os::Linux::pthread_getcpuclockid(thread->osthread()->pthread_id(),
5013 &clockid);
5014 if (rc == 0) {
5015 return os::Linux::fast_thread_cpu_time(clockid);
5016 } else {
5017 // It's possible to encounter a terminated native thread that failed
5018 // to detach itself from the VM - which should result in ESRCH.
5019 assert_status(rc == ESRCH, rc, "pthread_getcpuclockid failed")do { if (!(rc == 3)) { (*g_assert_poison) = 'X';; report_vm_status_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5019, "assert(" "rc == 3" ") failed", rc, "pthread_getcpuclockid failed"
); ::breakpoint(); } } while (0)
;
5020 return -1;
5021 }
5022}
5023
5024// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
5025// are used by JVM M&M and JVMTI to get user+sys or user CPU time
5026// of a thread.
5027//
5028// current_thread_cpu_time() and thread_cpu_time(Thread*) returns
5029// the fast estimate available on the platform.
5030
5031jlong os::current_thread_cpu_time() {
5032 if (os::Linux::supports_fast_thread_cpu_time()) {
5033 return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID3);
5034 } else {
5035 // return user + sys since the cost is the same
5036 return slow_thread_cpu_time(Thread::current(), true /* user + sys */);
5037 }
5038}
5039
5040jlong os::thread_cpu_time(Thread* thread) {
5041 // consistent with what current_thread_cpu_time() returns
5042 if (os::Linux::supports_fast_thread_cpu_time()) {
5043 return fast_cpu_time(thread);
5044 } else {
5045 return slow_thread_cpu_time(thread, true /* user + sys */);
5046 }
5047}
5048
5049jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
5050 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5051 return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID3);
5052 } else {
5053 return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
5054 }
5055}
5056
5057jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5058 if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
5059 return fast_cpu_time(thread);
5060 } else {
5061 return slow_thread_cpu_time(thread, user_sys_cpu_time);
5062 }
5063}
5064
5065// -1 on error.
5066static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
5067 pid_t tid = thread->osthread()->thread_id();
5068 char *s;
5069 char stat[2048];
5070 int statlen;
5071 char proc_name[64];
5072 int count;
5073 long sys_time, user_time;
5074 char cdummy;
5075 int idummy;
5076 long ldummy;
5077 FILE *fp;
5078
5079 snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);
5080 fp = fopen(proc_name, "r");
5081 if (fp == NULL__null) return -1;
5082 statlen = fread(stat, 1, 2047, fp);
5083 stat[statlen] = '\0';
5084 fclose(fp);
5085
5086 // Skip pid and the command string. Note that we could be dealing with
5087 // weird command names, e.g. user could decide to rename java launcher
5088 // to "java 1.4.2 :)", then the stat file would look like
5089 // 1234 (java 1.4.2 :)) R ... ...
5090 // We don't really need to know the command string, just find the last
5091 // occurrence of ")" and then start parsing from there. See bug 4726580.
5092 s = strrchr(stat, ')');
5093 if (s == NULL__null) return -1;
5094
5095 // Skip blank chars
5096 do { s++; } while (s && isspace(*s));
5097
5098 count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
5099 &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,
5100 &ldummy, &ldummy, &ldummy, &ldummy, &ldummy,
5101 &user_time, &sys_time);
5102 if (count != 13) return -1;
5103 if (user_sys_cpu_time) {
5104 return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
5105 } else {
5106 return (jlong)user_time * (1000000000 / clock_tics_per_sec);
5107 }
5108}
5109
5110void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
5111 info_ptr->max_value = ALL_64_BITS(0xFFFFFFFFFFFFFFFFLL); // will not wrap in less than 64 bits
5112 info_ptr->may_skip_backward = false; // elapsed time not wall time
5113 info_ptr->may_skip_forward = false; // elapsed time not wall time
5114 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned
5115}
5116
5117void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
5118 info_ptr->max_value = ALL_64_BITS(0xFFFFFFFFFFFFFFFFLL); // will not wrap in less than 64 bits
5119 info_ptr->may_skip_backward = false; // elapsed time not wall time
5120 info_ptr->may_skip_forward = false; // elapsed time not wall time
5121 info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned
5122}
5123
5124bool os::is_thread_cpu_time_supported() {
5125 return true;
5126}
5127
5128// System loadavg support. Returns -1 if load average cannot be obtained.
5129// Linux doesn't yet have a (official) notion of processor sets,
5130// so just return the system wide load average.
5131int os::loadavg(double loadavg[], int nelem) {
5132 return ::getloadavg(loadavg, nelem);
5133}
5134
5135void os::pause() {
5136 char filename[MAX_PATH(2 * K)];
5137 if (PauseAtStartupFile && PauseAtStartupFile[0]) {
5138 jio_snprintf(filename, MAX_PATH(2 * K), "%s", PauseAtStartupFile);
5139 } else {
5140 jio_snprintf(filename, MAX_PATH(2 * K), "./vm.paused.%d", current_process_id());
5141 }
5142
5143 int fd = ::open(filename, O_WRONLY01 | O_CREAT0100 | O_TRUNC01000, 0666);
5144 if (fd != -1) {
5145 struct stat buf;
5146 ::close(fd);
5147 while (::stat(filename, &buf) == 0) {
5148 (void)::poll(NULL__null, 0, 100);
5149 }
5150 } else {
5151 jio_fprintf(stderrstderr,
5152 "Could not open pause file '%s', continuing immediately.\n", filename);
5153 }
5154}
5155
5156// Get the default path to the core file
5157// Returns the length of the string
5158int os::get_core_path(char* buffer, size_t bufferSize) {
5159 /*
5160 * Max length of /proc/sys/kernel/core_pattern is 128 characters.
5161 * See https://www.kernel.org/doc/Documentation/sysctl/kernel.txt
5162 */
5163 const int core_pattern_len = 129;
5164 char core_pattern[core_pattern_len] = {0};
5165
5166 int core_pattern_file = ::open("/proc/sys/kernel/core_pattern", O_RDONLY00);
5167 if (core_pattern_file == -1) {
5168 return -1;
5169 }
5170
5171 ssize_t ret = ::read(core_pattern_file, core_pattern, core_pattern_len);
5172 ::close(core_pattern_file);
5173 if (ret <= 0 || ret >= core_pattern_len || core_pattern[0] == '\n') {
5174 return -1;
5175 }
5176 if (core_pattern[ret-1] == '\n') {
5177 core_pattern[ret-1] = '\0';
5178 } else {
5179 core_pattern[ret] = '\0';
5180 }
5181
5182 // Replace the %p in the core pattern with the process id. NOTE: we do this
5183 // only if the pattern doesn't start with "|", and we support only one %p in
5184 // the pattern.
5185 char *pid_pos = strstr(core_pattern, "%p");
5186 const char* tail = (pid_pos != NULL__null) ? (pid_pos + 2) : ""; // skip over the "%p"
5187 int written;
5188
5189 if (core_pattern[0] == '/') {
5190 if (pid_pos != NULL__null) {
5191 *pid_pos = '\0';
5192 written = jio_snprintf(buffer, bufferSize, "%s%d%s", core_pattern,
5193 current_process_id(), tail);
5194 } else {
5195 written = jio_snprintf(buffer, bufferSize, "%s", core_pattern);
5196 }
5197 } else {
5198 char cwd[PATH_MAX4096];
5199
5200 const char* p = get_current_directory(cwd, PATH_MAX4096);
5201 if (p == NULL__null) {
5202 return -1;
5203 }
5204
5205 if (core_pattern[0] == '|') {
5206 written = jio_snprintf(buffer, bufferSize,
5207 "\"%s\" (or dumping to %s/core.%d)",
5208 &core_pattern[1], p, current_process_id());
5209 } else if (pid_pos != NULL__null) {
5210 *pid_pos = '\0';
5211 written = jio_snprintf(buffer, bufferSize, "%s/%s%d%s", p, core_pattern,
5212 current_process_id(), tail);
5213 } else {
5214 written = jio_snprintf(buffer, bufferSize, "%s/%s", p, core_pattern);
5215 }
5216 }
5217
5218 if (written < 0) {
5219 return -1;
5220 }
5221
5222 if (((size_t)written < bufferSize) && (pid_pos == NULL__null) && (core_pattern[0] != '|')) {
5223 int core_uses_pid_file = ::open("/proc/sys/kernel/core_uses_pid", O_RDONLY00);
5224
5225 if (core_uses_pid_file != -1) {
5226 char core_uses_pid = 0;
5227 ssize_t ret = ::read(core_uses_pid_file, &core_uses_pid, 1);
Value stored to 'ret' during its initialization is never read
5228 ::close(core_uses_pid_file);
5229
5230 if (core_uses_pid == '1') {
5231 jio_snprintf(buffer + written, bufferSize - written,
5232 ".%d", current_process_id());
5233 }
5234 }
5235 }
5236
5237 return strlen(buffer);
5238}
5239
5240bool os::start_debugging(char *buf, int buflen) {
5241 int len = (int)strlen(buf);
5242 char *p = &buf[len];
5243
5244 jio_snprintf(p, buflen-len,
5245 "\n\n"
5246 "Do you want to debug the problem?\n\n"
5247 "To debug, run 'gdb /proc/%d/exe %d'; then switch to thread " UINTX_FORMAT"%" "l" "u" " (" INTPTR_FORMAT"0x%016" "l" "x" ")\n"
5248 "Enter 'yes' to launch gdb automatically (PATH must include gdb)\n"
5249 "Otherwise, press RETURN to abort...",
5250 os::current_process_id(), os::current_process_id(),
5251 os::current_thread_id(), os::current_thread_id());
5252
5253 bool yes = os::message_box("Unexpected Error", buf);
5254
5255 if (yes) {
5256 // yes, user asked VM to launch debugger
5257 jio_snprintf(buf, sizeof(char)*buflen, "gdb /proc/%d/exe %d",
5258 os::current_process_id(), os::current_process_id());
5259
5260 os::fork_and_exec(buf);
5261 yes = false;
5262 }
5263 return yes;
5264}
5265
5266
5267// Java/Compiler thread:
5268//
5269// Low memory addresses
5270// P0 +------------------------+
5271// | |\ Java thread created by VM does not have glibc
5272// | glibc guard page | - guard page, attached Java thread usually has
5273// | |/ 1 glibc guard page.
5274// P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
5275// | |\
5276// | HotSpot Guard Pages | - red, yellow and reserved pages
5277// | |/
5278// +------------------------+ StackOverflow::stack_reserved_zone_base()
5279// | |\
5280// | Normal Stack | -
5281// | |/
5282// P2 +------------------------+ Thread::stack_base()
5283//
5284// Non-Java thread:
5285//
5286// Low memory addresses
5287// P0 +------------------------+
5288// | |\
5289// | glibc guard page | - usually 1 page
5290// | |/
5291// P1 +------------------------+ Thread::stack_base() - Thread::stack_size()
5292// | |\
5293// | Normal Stack | -
5294// | |/
5295// P2 +------------------------+ Thread::stack_base()
5296//
5297// ** P1 (aka bottom) and size (P2 = P1 - size) are the address and stack size
5298// returned from pthread_attr_getstack().
5299// ** Due to NPTL implementation error, linux takes the glibc guard page out
5300// of the stack size given in pthread_attr. We work around this for
5301// threads created by the VM. (We adapt bottom to be P1 and size accordingly.)
5302//
5303#ifndef ZERO
5304static void current_stack_region(address * bottom, size_t * size) {
5305 if (os::is_primordial_thread()) {
5306 // primordial thread needs special handling because pthread_getattr_np()
5307 // may return bogus value.
5308 *bottom = os::Linux::initial_thread_stack_bottom();
5309 *size = os::Linux::initial_thread_stack_size();
5310 } else {
5311 pthread_attr_t attr;
5312
5313 int rslt = pthread_getattr_np(pthread_self(), &attr);
5314
5315 // JVM needs to know exact stack location, abort if it fails
5316 if (rslt != 0) {
5317 if (rslt == ENOMEM12) {
5318 vm_exit_out_of_memory(0, OOM_MMAP_ERROR, "pthread_getattr_np")do { report_vm_out_of_memory("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5318, 0, OOM_MMAP_ERROR, "pthread_getattr_np"); ::breakpoint
(); } while (0)
;
5319 } else {
5320 fatal("pthread_getattr_np failed with error = %d", rslt)do { (*g_assert_poison) = 'X';; report_fatal(INTERNAL_ERROR, "/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5320, "pthread_getattr_np failed with error = %d", rslt); ::
breakpoint(); } while (0)
;
5321 }
5322 }
5323
5324 if (pthread_attr_getstack(&attr, (void **)bottom, size) != 0) {
5325 fatal("Cannot locate current stack attributes!")do { (*g_assert_poison) = 'X';; report_fatal(INTERNAL_ERROR, "/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5325, "Cannot locate current stack attributes!"); ::breakpoint
(); } while (0)
;
5326 }
5327
5328 // Work around NPTL stack guard error.
5329 size_t guard_size = 0;
5330 rslt = pthread_attr_getguardsize(&attr, &guard_size);
5331 if (rslt != 0) {
5332 fatal("pthread_attr_getguardsize failed with error = %d", rslt)do { (*g_assert_poison) = 'X';; report_fatal(INTERNAL_ERROR, "/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5332, "pthread_attr_getguardsize failed with error = %d", rslt
); ::breakpoint(); } while (0)
;
5333 }
5334 *bottom += guard_size;
5335 *size -= guard_size;
5336
5337 pthread_attr_destroy(&attr);
5338
5339 }
5340 assert(os::current_stack_pointer() >= *bottom &&do { if (!(os::current_stack_pointer() >= *bottom &&
os::current_stack_pointer() < *bottom + *size)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5341, "assert(" "os::current_stack_pointer() >= *bottom && os::current_stack_pointer() < *bottom + *size"
") failed", "just checking"); ::breakpoint(); } } while (0)
5341 os::current_stack_pointer() < *bottom + *size, "just checking")do { if (!(os::current_stack_pointer() >= *bottom &&
os::current_stack_pointer() < *bottom + *size)) { (*g_assert_poison
) = 'X';; report_vm_error("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5341, "assert(" "os::current_stack_pointer() >= *bottom && os::current_stack_pointer() < *bottom + *size"
") failed", "just checking"); ::breakpoint(); } } while (0)
;
5342}
5343
5344address os::current_stack_base() {
5345 address bottom;
5346 size_t size;
5347 current_stack_region(&bottom, &size);
5348 return (bottom + size);
5349}
5350
5351size_t os::current_stack_size() {
5352 // This stack size includes the usable stack and HotSpot guard pages
5353 // (for the threads that have Hotspot guard pages).
5354 address bottom;
5355 size_t size;
5356 current_stack_region(&bottom, &size);
5357 return size;
5358}
5359#endif
5360
5361static inline struct timespec get_mtime(const char* filename) {
5362 struct stat st;
5363 int ret = os::stat(filename, &st);
5364 assert(ret == 0, "failed to stat() file '%s': %s", filename, os::strerror(errno))do { if (!(ret == 0)) { (*g_assert_poison) = 'X';; report_vm_error
("/home/daniel/Projects/java/jdk/src/hotspot/os/linux/os_linux.cpp"
, 5364, "assert(" "ret == 0" ") failed", "failed to stat() file '%s': %s"
, filename, os::strerror((*__errno_location ()))); ::breakpoint
(); } } while (0)
;
5365 return st.st_mtim;
5366}
5367
5368int os::compare_file_modified_times(const char* file1, const char* file2) {
5369 struct timespec filetime1 = get_mtime(file1);
5370 struct timespec filetime2 = get_mtime(file2);
5371 int diff = filetime1.tv_sec - filetime2.tv_sec;
5372 if (diff == 0) {
5373 return filetime1.tv_nsec - filetime2.tv_nsec;
5374 }
5375 return diff;
5376}
5377
5378bool os::supports_map_sync() {
5379 return true;
5380}
5381
5382void os::print_memory_mappings(char* addr, size_t bytes, outputStream* st) {
5383 unsigned long long start = (unsigned long long)addr;
5384 unsigned long long end = start + bytes;
5385 FILE* f = ::fopen("/proc/self/maps", "r");
5386 int num_found = 0;
5387 if (f != NULL__null) {
5388 st->print("Range [%llx-%llx) contains: ", start, end);
5389 char line[512];
5390 while(fgets(line, sizeof(line), f) == line) {
5391 unsigned long long a1 = 0;
5392 unsigned long long a2 = 0;
5393 if (::sscanf(line, "%llx-%llx", &a1, &a2) == 2) {
5394 // Lets print out every range which touches ours.
5395 if ((a1 >= start && a1 < end) || // left leg in
5396 (a2 >= start && a2 < end) || // right leg in
5397 (a1 < start && a2 >= end)) { // superimposition
5398 num_found ++;
5399 st->print("%s", line); // line includes \n
5400 }
5401 }
5402 }
5403 ::fclose(f);
5404 if (num_found == 0) {
5405 st->print("nothing.");
5406 }
5407 st->cr();
5408 }
5409}