2
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "cds/cdsConfig.hpp"
27
#include "classfile/javaClasses.hpp"
28
#include "classfile/moduleEntry.hpp"
29
#include "classfile/systemDictionary.hpp"
30
#include "classfile/vmClasses.hpp"
31
#include "classfile/vmSymbols.hpp"
32
#include "code/codeCache.hpp"
33
#include "code/vtableStubs.hpp"
34
#include "gc/shared/gcVMOperations.hpp"
35
#include "interpreter/interpreter.hpp"
37
#include "logging/log.hpp"
38
#include "logging/logStream.hpp"
39
#include "memory/allocation.inline.hpp"
40
#include "memory/resourceArea.hpp"
41
#include "memory/universe.hpp"
42
#include "nmt/mallocHeader.inline.hpp"
43
#include "nmt/mallocTracker.hpp"
44
#include "nmt/memTracker.inline.hpp"
45
#include "nmt/nmtCommon.hpp"
46
#include "nmt/nmtPreInit.hpp"
47
#include "oops/compressedKlass.inline.hpp"
48
#include "oops/oop.inline.hpp"
49
#include "prims/jvm_misc.hpp"
50
#include "prims/jvmtiAgent.hpp"
51
#include "runtime/arguments.hpp"
52
#include "runtime/atomic.hpp"
53
#include "runtime/frame.inline.hpp"
54
#include "runtime/handles.inline.hpp"
55
#include "runtime/interfaceSupport.inline.hpp"
56
#include "runtime/java.hpp"
57
#include "runtime/javaCalls.hpp"
58
#include "runtime/javaThread.hpp"
59
#include "runtime/jniHandles.hpp"
60
#include "runtime/mutexLocker.hpp"
61
#include "runtime/os.inline.hpp"
62
#include "runtime/osThread.hpp"
63
#include "runtime/safefetch.hpp"
64
#include "runtime/sharedRuntime.hpp"
65
#include "runtime/threadCrashProtection.hpp"
66
#include "runtime/threadSMR.hpp"
67
#include "runtime/vmOperations.hpp"
68
#include "runtime/vm_version.hpp"
69
#include "sanitizers/address.hpp"
70
#include "services/attachListener.hpp"
71
#include "services/threadService.hpp"
72
#include "utilities/align.hpp"
73
#include "utilities/checkedCast.hpp"
74
#include "utilities/count_trailing_zeros.hpp"
75
#include "utilities/defaultStream.hpp"
76
#include "utilities/events.hpp"
77
#include "utilities/fastrand.hpp"
78
#include "utilities/macros.hpp"
79
#include "utilities/powerOfTwo.hpp"
82
#include "osContainer_linux.hpp"
92
OSThread* os::_starting_thread = nullptr;
93
volatile unsigned int os::_rand_seed = 1234567;
94
int os::_processor_count = 0;
95
int os::_initial_active_processor_count = 0;
96
os::PageSizes os::_page_sizes;
98
DEBUG_ONLY(bool os::_mutex_init_done = false;)
100
int os::snprintf(char* buf, size_t len, const char* fmt, ...) {
103
int result = os::vsnprintf(buf, len, fmt, args);
108
int os::snprintf_checked(char* buf, size_t len, const char* fmt, ...) {
111
int result = os::vsnprintf(buf, len, fmt, args);
113
assert(result >= 0, "os::snprintf error");
114
assert(static_cast<size_t>(result) < len, "os::snprintf truncated");
118
int os::vsnprintf(char* buf, size_t len, const char* fmt, va_list args) {
119
ALLOW_C_FUNCTION(::vsnprintf, int result = ::vsnprintf(buf, len, fmt, args);)
120
// If an encoding error occurred (result < 0) then it's not clear
121
// whether the buffer is NUL terminated, so ensure it is.
122
if ((result < 0) && (len > 0)) {
128
// Fill in buffer with current local time as an ISO-8601 string.
129
// E.g., YYYY-MM-DDThh:mm:ss.mmm+zzzz.
130
// Returns buffer, or null if it failed.
131
char* os::iso8601_time(char* buffer, size_t buffer_length, bool utc) {
132
const jlong now = javaTimeMillis();
133
return os::iso8601_time(now, buffer, buffer_length, utc);
136
// Fill in buffer with an ISO-8601 string corresponding to the given javaTimeMillis value
137
// E.g., yyyy-mm-ddThh:mm:ss-zzzz.
138
// Returns buffer, or null if it failed.
139
// This would mostly be a call to
140
// strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....)
141
// except that on Windows the %z behaves badly, so we do it ourselves.
142
// Also, people wanted milliseconds on there,
143
// and strftime doesn't do milliseconds.
144
char* os::iso8601_time(jlong milliseconds_since_19700101, char* buffer, size_t buffer_length, bool utc) {
145
// Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0"
147
// Sanity check the arguments
148
if (buffer == nullptr) {
149
assert(false, "null buffer");
152
if (buffer_length < os::iso8601_timestamp_size) {
153
assert(false, "buffer_length too small");
156
const int milliseconds_per_second = 1000;
157
const time_t seconds_since_19700101 =
158
milliseconds_since_19700101 / milliseconds_per_second;
159
const int milliseconds_after_second =
160
checked_cast<int>(milliseconds_since_19700101 % milliseconds_per_second);
161
// Convert the time value to a tm and timezone variable
162
struct tm time_struct;
164
if (gmtime_pd(&seconds_since_19700101, &time_struct) == nullptr) {
165
assert(false, "Failed gmtime_pd");
169
if (localtime_pd(&seconds_since_19700101, &time_struct) == nullptr) {
170
assert(false, "Failed localtime_pd");
175
const time_t seconds_per_minute = 60;
176
const time_t minutes_per_hour = 60;
177
const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
179
// No offset when dealing with UTC
180
time_t UTC_to_local = 0;
182
#if (defined(_ALLBSD_SOURCE) || defined(_GNU_SOURCE)) && !defined(AIX)
183
UTC_to_local = -(time_struct.tm_gmtoff);
184
#elif defined(_WINDOWS)
186
_get_timezone(&zone);
187
UTC_to_local = static_cast<time_t>(zone);
189
UTC_to_local = timezone;
192
// tm_gmtoff already includes adjustment for daylight saving
193
#if !defined(_ALLBSD_SOURCE) && !defined(_GNU_SOURCE)
194
// If daylight savings time is in effect,
195
// we are 1 hour East of our time zone
196
if (time_struct.tm_isdst > 0) {
197
UTC_to_local = UTC_to_local - seconds_per_hour;
202
// Compute the time zone offset.
203
// localtime_pd() sets timezone to the difference (in seconds)
204
// between UTC and local time.
205
// ISO 8601 says we need the difference between local time and UTC,
206
// we change the sign of the localtime_pd() result.
207
const time_t local_to_UTC = -(UTC_to_local);
208
// Then we have to figure out if if we are ahead (+) or behind (-) UTC.
209
char sign_local_to_UTC = '+';
210
time_t abs_local_to_UTC = local_to_UTC;
211
if (local_to_UTC < 0) {
212
sign_local_to_UTC = '-';
213
abs_local_to_UTC = -(abs_local_to_UTC);
215
// Convert time zone offset seconds to hours and minutes.
216
const int zone_hours = static_cast<int>(abs_local_to_UTC / seconds_per_hour);
218
static_cast<int>((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute);
220
// Print an ISO 8601 date and time stamp into the buffer
221
const int year = 1900 + time_struct.tm_year;
222
const int month = 1 + time_struct.tm_mon;
223
const int printed = jio_snprintf(buffer, buffer_length,
224
"%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d",
231
milliseconds_after_second,
236
assert(false, "Failed jio_printf");
242
OSReturn os::set_priority(Thread* thread, ThreadPriority p) {
243
debug_only(Thread::check_for_dangling_thread_pointer(thread);)
245
if ((p >= MinPriority && p <= MaxPriority) ||
246
(p == CriticalPriority && thread->is_ConcurrentGC_thread())) {
247
int priority = java_to_os_priority[p];
248
return set_native_priority(thread, priority);
250
assert(false, "Should not happen");
255
// The mapping from OS priority back to Java priority may be inexact because
256
// Java priorities can map M:1 with native priorities. If you want the definite
257
// Java priority then use JavaThread::java_priority()
258
OSReturn os::get_priority(const Thread* const thread, ThreadPriority& priority) {
261
OSReturn ret = get_native_priority(thread, &os_prio);
262
if (ret != OS_OK) return ret;
264
if (java_to_os_priority[MaxPriority] > java_to_os_priority[MinPriority]) {
265
for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] > os_prio; p--) ;
267
// niceness values are in reverse order
268
for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] < os_prio; p--) ;
270
priority = (ThreadPriority)p;
274
bool os::dll_build_name(char* buffer, size_t size, const char* fname) {
275
int n = jio_snprintf(buffer, size, "%s%s%s", JNI_LIB_PREFIX, fname, JNI_LIB_SUFFIX);
280
// Helper for dll_locate_lib.
281
// Pass buffer and printbuffer as we already printed the path to buffer
282
// when we called get_current_directory. This way we avoid another buffer
284
static bool conc_path_file_and_check(char *buffer, char *printbuffer, size_t printbuflen,
285
const char* pname, char lastchar, const char* fname) {
287
// Concatenate path and file name, but don't print double path separators.
288
const char *filesep = (WINDOWS_ONLY(lastchar == ':' ||) lastchar == os::file_separator()[0]) ?
289
"" : os::file_separator();
290
int ret = jio_snprintf(printbuffer, printbuflen, "%s%s%s", pname, filesep, fname);
291
// Check whether file exists.
294
return os::stat(buffer, &statbuf) == 0;
299
// Frees all memory allocated on the heap for the
300
// supplied array of arrays of chars (a), where n
301
// is the number of elements in the array.
302
static void free_array_of_char_arrays(char** a, size_t n) {
305
if (a[n] != nullptr) {
306
FREE_C_HEAP_ARRAY(char, a[n]);
309
FREE_C_HEAP_ARRAY(char*, a);
312
bool os::dll_locate_lib(char *buffer, size_t buflen,
313
const char* pname, const char* fname) {
316
size_t fullfnamelen = strlen(JNI_LIB_PREFIX) + strlen(fname) + strlen(JNI_LIB_SUFFIX);
317
char* fullfname = NEW_C_HEAP_ARRAY(char, fullfnamelen + 1, mtInternal);
318
if (dll_build_name(fullfname, fullfnamelen + 1, fname)) {
319
const size_t pnamelen = pname ? strlen(pname) : 0;
322
// If no path given, use current working directory.
323
const char* p = get_current_directory(buffer, buflen);
325
const size_t plen = strlen(buffer);
326
const char lastchar = buffer[plen - 1];
327
retval = conc_path_file_and_check(buffer, &buffer[plen], buflen - plen,
328
"", lastchar, fullfname);
330
} else if (strchr(pname, *os::path_separator()) != nullptr) {
331
// A list of paths. Search for the path that contains the library.
333
char** pelements = split_path(pname, &n, fullfnamelen);
334
if (pelements != nullptr) {
335
for (size_t i = 0; i < n; i++) {
336
char* path = pelements[i];
337
// Really shouldn't be null, but check can't hurt.
338
size_t plen = (path == nullptr) ? 0 : strlen(path);
340
continue; // Skip the empty path values.
342
const char lastchar = path[plen - 1];
343
retval = conc_path_file_and_check(buffer, buffer, buflen, path, lastchar, fullfname);
346
// Release the storage allocated by split_path.
347
free_array_of_char_arrays(pelements, n);
351
const char lastchar = pname[pnamelen-1];
352
retval = conc_path_file_and_check(buffer, buffer, buflen, pname, lastchar, fullfname);
356
FREE_C_HEAP_ARRAY(char*, fullfname);
360
// --------------------- sun.misc.Signal (optional) ---------------------
363
// SIGBREAK is sent by the keyboard to query the VM state
365
#define SIGBREAK SIGQUIT
368
// sigexitnum_pd is a platform-specific special signal used for terminating the Signal thread.
371
static void signal_thread_entry(JavaThread* thread, TRAPS) {
372
os::set_priority(thread, NearMaxPriority);
376
// FIXME : Currently we have not decided what should be the status
377
// for this java thread blocked here. Once we decide about
378
// that we should fix this.
379
sig = os::signal_wait();
381
if (sig == os::sigexitnum_pd()) {
382
// Terminate the signal thread
389
// Check if the signal is a trigger to start the Attach Listener - in that
390
// case don't print stack traces.
391
if (!DisableAttachMechanism) {
392
// Attempt to transit state to AL_INITIALIZING.
393
AttachListenerState cur_state = AttachListener::transit_state(AL_INITIALIZING, AL_NOT_INITIALIZED);
394
if (cur_state == AL_INITIALIZING) {
395
// Attach Listener has been started to initialize. Ignore this signal.
397
} else if (cur_state == AL_NOT_INITIALIZED) {
398
// Start to initialize.
399
if (AttachListener::is_init_trigger()) {
400
// Attach Listener has been initialized.
401
// Accept subsequent request.
404
// Attach Listener could not be started.
405
// So we need to transit the state to AL_NOT_INITIALIZED.
406
AttachListener::set_state(AL_NOT_INITIALIZED);
408
} else if (AttachListener::check_socket_file()) {
409
// Attach Listener has been started, but unix domain socket file
410
// does not exist. So restart Attach Listener.
415
// Print stack traces
416
// Any SIGBREAK operations added here should make sure to flush
417
// the output stream (e.g. tty->flush()) after output. See 4803766.
418
// Each module also prints an extra carriage return after its output.
419
VM_PrintThreads op(tty, PrintConcurrentLocks, false /* no extended info */, true /* print JNI handle info */);
420
VMThread::execute(&op);
421
VM_FindDeadlocks op1(tty);
422
VMThread::execute(&op1);
423
Universe::print_heap_at_SIGBREAK();
424
if (PrintClassHistogram) {
425
VM_GC_HeapInspection op1(tty, true /* force full GC before heap inspection */);
426
VMThread::execute(&op1);
428
if (JvmtiExport::should_post_data_dump()) {
429
JvmtiExport::post_data_dump();
434
// Dispatch the signal to java
435
HandleMark hm(THREAD);
436
Klass* klass = SystemDictionary::resolve_or_null(vmSymbols::jdk_internal_misc_Signal(), THREAD);
437
if (klass != nullptr) {
438
JavaValue result(T_VOID);
439
JavaCallArguments args;
441
JavaCalls::call_static(
444
vmSymbols::dispatch_name(),
445
vmSymbols::int_void_signature(),
450
if (HAS_PENDING_EXCEPTION) {
451
// tty is initialized early so we don't expect it to be null, but
452
// if it is we can't risk doing an initialization that might
453
// trigger additional out-of-memory conditions
454
if (tty != nullptr) {
455
char klass_name[256];
456
char tmp_sig_name[16];
457
const char* sig_name = "UNKNOWN";
458
InstanceKlass::cast(PENDING_EXCEPTION->klass())->
459
name()->as_klass_external_name(klass_name, 256);
460
if (os::exception_name(sig, tmp_sig_name, 16) != nullptr)
461
sig_name = tmp_sig_name;
462
warning("Exception %s occurred dispatching signal %s to handler"
463
"- the VM may need to be forcibly terminated",
464
klass_name, sig_name );
466
CLEAR_PENDING_EXCEPTION;
473
void os::init_before_ergo() {
474
initialize_initial_active_processor_count();
475
// We need to initialize large page support here because ergonomics takes some
476
// decisions depending on large page support and the calculated large page size.
479
StackOverflow::initialize_stack_zone_sizes();
481
// VM version initialization identifies some characteristics of the
482
// platform that are used during ergonomic decisions.
483
VM_Version::init_before_ergo();
486
void os::initialize_jdk_signal_support(TRAPS) {
487
if (!ReduceSignalUsage) {
488
// Setup JavaThread for processing signals
489
const char* name = "Signal Dispatcher";
490
Handle thread_oop = JavaThread::create_system_thread_object(name, CHECK);
492
JavaThread* thread = new JavaThread(&signal_thread_entry);
493
JavaThread::vm_exit_on_osthread_failure(thread);
495
JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NearMaxPriority);
500
void os::terminate_signal_thread() {
501
if (!ReduceSignalUsage)
502
signal_notify(sigexitnum_pd());
506
// --------------------- loading libraries ---------------------
508
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
509
extern struct JavaVM_ main_vm;
511
static void* _native_java_library = nullptr;
513
void* os::native_java_library() {
514
if (_native_java_library == nullptr) {
515
char buffer[JVM_MAXPATHLEN];
519
if (dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
521
_native_java_library = dll_load(buffer, ebuf, sizeof(ebuf));
523
if (_native_java_library == nullptr) {
524
vm_exit_during_initialization("Unable to load native library", ebuf);
527
#if defined(__OpenBSD__)
528
// Work-around OpenBSD's lack of $ORIGIN support by pre-loading libnet.so
530
if (dll_locate_lib(buffer, sizeof(buffer), Arguments::get_dll_dir(),
532
dll_load(buffer, ebuf, sizeof(ebuf));
536
return _native_java_library;
540
* Support for finding Agent_On(Un)Load/Attach<_lib_name> if it exists.
541
* If check_lib == true then we are looking for an
542
* Agent_OnLoad_lib_name or Agent_OnAttach_lib_name function to determine if
543
* this library is statically linked into the image.
544
* If check_lib == false then we will look for the appropriate symbol in the
545
* executable if agent_lib->is_static_lib() == true or in the shared library
546
* referenced by 'handle'.
548
void* os::find_agent_function(JvmtiAgent *agent_lib, bool check_lib,
549
const char *syms[], size_t syms_len) {
550
assert(agent_lib != nullptr, "sanity check");
551
const char *lib_name;
552
void *handle = agent_lib->os_lib();
553
void *entryName = nullptr;
554
char *agent_function_name;
557
// If checking then use the agent name otherwise test is_static_lib() to
558
// see how to process this lookup
559
lib_name = ((check_lib || agent_lib->is_static_lib()) ? agent_lib->name() : nullptr);
560
for (i = 0; i < syms_len; i++) {
561
agent_function_name = build_agent_function_name(syms[i], lib_name, agent_lib->is_absolute_path());
562
if (agent_function_name == nullptr) {
565
entryName = dll_lookup(handle, agent_function_name);
566
FREE_C_HEAP_ARRAY(char, agent_function_name);
567
if (entryName != nullptr) {
574
// See if the passed in agent is statically linked into the VM image.
575
bool os::find_builtin_agent(JvmtiAgent* agent, const char *syms[],
581
assert(agent != nullptr, "sanity check");
582
if (agent->name() == nullptr) {
585
proc_handle = get_default_process_handle();
586
// Check for Agent_OnLoad/Attach_lib_name function
587
save_handle = agent->os_lib();
588
// We want to look in this process' symbol table.
589
agent->set_os_lib(proc_handle);
590
ret = find_agent_function(agent, true, syms, syms_len);
591
if (ret != nullptr) {
592
// Found an entry point like Agent_OnLoad_lib_name so we have a static agent
593
agent->set_static_lib();
597
agent->set_os_lib(save_handle);
601
// --------------------- heap allocation utilities ---------------------
603
char *os::strdup(const char *str, MEMFLAGS flags) {
604
size_t size = strlen(str);
605
char *dup_str = (char *)malloc(size + 1, flags);
606
if (dup_str == nullptr) return nullptr;
607
strcpy(dup_str, str);
611
char* os::strdup_check_oom(const char* str, MEMFLAGS flags) {
612
char* p = os::strdup(str, flags);
614
vm_exit_out_of_memory(strlen(str) + 1, OOM_MALLOC_ERROR, "os::strdup_check_oom");
620
static void check_crash_protection() {
621
assert(!ThreadCrashProtection::is_crash_protected(Thread::current_or_null()),
622
"not allowed when crash protection is set");
624
static void break_if_ptr_caught(void* ptr) {
625
if (p2i(ptr) == (intptr_t)MallocCatchPtr) {
626
log_warning(malloc, free)("ptr caught: " PTR_FORMAT, p2i(ptr));
632
void* os::malloc(size_t size, MEMFLAGS flags) {
633
return os::malloc(size, flags, CALLER_PC);
636
void* os::malloc(size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
638
// Special handling for NMT preinit phase before arguments are parsed
640
if (NMTPreInit::handle_malloc(&rc, size)) {
641
// No need to fill with 0 because CDS static dumping doesn't use these
642
// early allocations.
646
DEBUG_ONLY(check_crash_protection());
648
// On malloc(0), implementations of malloc(3) have the choice to return either
649
// null or a unique non-null pointer. To unify libc behavior across our platforms
650
// we chose the latter.
651
size = MAX2((size_t)1, size);
653
// Observe MallocLimit
654
if (MemTracker::check_exceeds_limit(size, memflags)) {
658
const size_t outer_size = size + MemTracker::overhead_per_malloc();
660
// Check for overflow.
661
if (outer_size < size) {
665
ALLOW_C_FUNCTION(::malloc, void* const outer_ptr = ::malloc(outer_size);)
666
if (outer_ptr == nullptr) {
670
void* const inner_ptr = MemTracker::record_malloc((address)outer_ptr, size, memflags, stack);
672
if (CDSConfig::is_dumping_static_archive()) {
673
// Need to deterministically fill all the alignment gaps in C++ structures.
674
::memset(inner_ptr, 0, size);
676
DEBUG_ONLY(::memset(inner_ptr, uninitBlockPad, size);)
678
DEBUG_ONLY(break_if_ptr_caught(inner_ptr);)
682
void* os::realloc(void *memblock, size_t size, MEMFLAGS flags) {
683
return os::realloc(memblock, size, flags, CALLER_PC);
686
void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, const NativeCallStack& stack) {
688
// Special handling for NMT preinit phase before arguments are parsed
690
if (NMTPreInit::handle_realloc(&rc, memblock, size, memflags)) {
694
if (memblock == nullptr) {
695
return os::malloc(size, memflags, stack);
698
DEBUG_ONLY(check_crash_protection());
700
// On realloc(p, 0), implementers of realloc(3) have the choice to return either
701
// null or a unique non-null pointer. To unify libc behavior across our platforms
702
// we chose the latter.
703
size = MAX2((size_t)1, size);
705
if (MemTracker::enabled()) {
706
// NMT realloc handling
708
const size_t new_outer_size = size + MemTracker::overhead_per_malloc();
710
// Handle size overflow.
711
if (new_outer_size < size) {
715
const size_t old_size = MallocTracker::malloc_header(memblock)->size();
717
// Observe MallocLimit
718
if ((size > old_size) && MemTracker::check_exceeds_limit(size - old_size, memflags)) {
722
// Perform integrity checks on and mark the old block as dead *before* calling the real realloc(3) since it
723
// may invalidate the old block, including its header.
724
MallocHeader* header = MallocHeader::resolve_checked(memblock);
725
assert(memflags == header->flags(), "weird NMT flags mismatch (new:\"%s\" != old:\"%s\")\n",
726
NMTUtil::flag_to_name(memflags), NMTUtil::flag_to_name(header->flags()));
727
const MallocHeader::FreeInfo free_info = header->free_info();
729
header->mark_block_as_dead();
732
ALLOW_C_FUNCTION(::realloc, void* const new_outer_ptr = ::realloc(header, new_outer_size);)
734
if (new_outer_ptr == nullptr) {
735
// realloc(3) failed and the block still exists.
736
// We have however marked it as dead, revert this change.
740
// realloc(3) succeeded, variable header now points to invalid memory and we need to deaccount the old block.
741
MemTracker::deaccount(free_info);
743
// After a successful realloc(3), we account the resized block with its new size
745
void* const new_inner_ptr = MemTracker::record_malloc(new_outer_ptr, size, memflags, stack);
748
assert(old_size == free_info.size, "Sanity");
749
if (old_size < size) {
750
// We also zap the newly extended region.
751
::memset((char*)new_inner_ptr + old_size, uninitBlockPad, size - old_size);
760
ALLOW_C_FUNCTION(::realloc, rc = ::realloc(memblock, size);)
767
DEBUG_ONLY(break_if_ptr_caught(rc);)
772
void os::free(void *memblock) {
774
// Special handling for NMT preinit phase before arguments are parsed
775
if (NMTPreInit::handle_free(memblock)) {
779
if (memblock == nullptr) {
783
DEBUG_ONLY(break_if_ptr_caught(memblock);)
785
// When NMT is enabled this checks for heap overwrites, then deaccounts the old block.
786
void* const old_outer_ptr = MemTracker::record_free(memblock);
788
ALLOW_C_FUNCTION(::free, ::free(old_outer_ptr);)
791
void os::init_random(unsigned int initval) {
792
_rand_seed = initval;
796
int os::next_random(unsigned int rand_seed) {
797
/* standard, well-known linear congruential random generator with
798
* next_rand = (16807*seed) mod (2**31-1)
800
* (1) "Random Number Generators: Good Ones Are Hard to Find",
801
* S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
802
* (2) "Two Fast Implementations of the 'Minimal Standard' Random
803
* Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88.
805
const unsigned int a = 16807;
806
const unsigned int m = 2147483647;
807
const int q = m / a; assert(q == 127773, "weird math");
808
const int r = m % a; assert(r == 2836, "weird math");
810
// compute az=2^31p+q
811
unsigned int lo = a * (rand_seed & 0xFFFF);
812
unsigned int hi = a * (rand_seed >> 16);
813
lo += (hi & 0x7FFF) << 16;
815
// if q overflowed, ignore the overflow and increment q
822
// if (p+q) overflowed, ignore the overflow and increment (p+q)
831
// Make updating the random seed thread safe.
833
unsigned int seed = _rand_seed;
834
unsigned int rand = next_random(seed);
835
if (Atomic::cmpxchg(&_rand_seed, seed, rand, memory_order_relaxed) == seed) {
836
return static_cast<int>(rand);
841
// The INITIALIZED state is distinguished from the SUSPENDED state because the
842
// conditions in which a thread is first started are different from those in which
843
// a suspension is resumed. These differences make it hard for us to apply the
844
// tougher checks when starting threads that we want to do when resuming them.
845
// However, when start_thread is called as a result of Thread.start, on a Java
846
// thread, the operation is synchronized on the Java Thread object. So there
847
// cannot be a race to start the thread and hence for the thread to exit while
848
// we are working on it. Non-Java threads that start Java threads either have
849
// to do so in a context in which races are impossible, or should do appropriate
852
void os::start_thread(Thread* thread) {
853
OSThread* osthread = thread->osthread();
854
osthread->set_state(RUNNABLE);
855
pd_start_thread(thread);
858
void os::abort(bool dump_core) {
859
abort(dump_core && CreateCoredumpOnCrash, nullptr, nullptr);
862
//---------------------------------------------------------------------------
863
// Helper functions for fatal error handler
865
bool os::print_function_and_library_name(outputStream* st,
867
char* buf, int buflen,
870
bool strip_arguments) {
871
// If no scratch buffer given, allocate one here on stack.
872
// (used during error handling; its a coin toss, really, if on-stack allocation
873
// is worse than (raw) C-heap allocation in that case).
876
p = (char*)::alloca(O_BUFLEN);
880
bool have_function_name = dll_address_to_function_name(addr, p, buflen,
882
bool is_function_descriptor = false;
883
#ifdef HAVE_FUNCTION_DESCRIPTORS
884
// When we deal with a function descriptor instead of a real code pointer, try to
885
// resolve it. There is a small chance that a random pointer given to this function
886
// may just happen to look like a valid descriptor, but this is rare and worth the
887
// risk to see resolved function names. But we will print a little suffix to mark
888
// this as a function descriptor for the reader (see below).
889
if (!have_function_name && os::is_readable_pointer(addr)) {
890
address addr2 = (address)os::resolve_function_descriptor(addr);
891
if ((have_function_name = is_function_descriptor =
892
dll_address_to_function_name(addr2, p, buflen, &offset, demangle))) {
896
#endif // HAVE_FUNCTION_DESCRIPTORS
898
if (have_function_name) {
899
// Print function name, optionally demangled
900
if (demangle && strip_arguments) {
901
char* args_start = strchr(p, '(');
902
if (args_start != nullptr) {
906
// Print offset. Omit printing if offset is zero, which makes the output
907
// more readable if we print function pointers.
911
st->print("%s+%d", p, offset);
914
st->print(PTR_FORMAT, p2i(addr));
918
const bool have_library_name = dll_address_to_library_name(addr, p, buflen, &offset);
919
if (have_library_name) {
922
char* p2 = strrchr(p, os::file_separator()[0]);
927
st->print(" in %s", p);
928
if (!have_function_name) { // Omit offset if we already printed the function offset
929
st->print("+%d", offset);
933
// Write a trailing marker if this was a function descriptor
934
if (have_function_name && is_function_descriptor) {
935
st->print_raw(" (FD)");
938
return have_function_name || have_library_name;
941
ATTRIBUTE_NO_ASAN static bool read_safely_from(const uintptr_t* p, uintptr_t* result) {
942
DEBUG_ONLY(*result = 0xAAAA;)
943
const uintptr_t errval = 0x1717;
944
uintptr_t i = (uintptr_t)SafeFetchN((intptr_t*)p, errval);
946
i = (uintptr_t)SafeFetchN((intptr_t*)p, ~errval);
951
(*result) = (uintptr_t)i;
955
// Helper for os::print_hex_dump
956
static void print_ascii_form(stringStream& ascii_form, uint64_t value, int unitsize) {
959
uint8_t c[sizeof(v)];
961
for (int i = 0; i < unitsize; i++) {
962
const int idx = LITTLE_ENDIAN_ONLY(i) BIG_ENDIAN_ONLY(sizeof(u.v) - unitsize + i);
963
const uint8_t c = u.c[idx];
964
ascii_form.put(isprint(c) && isascii(c) ? c : '.');
968
// Helper for os::print_hex_dump
969
static void print_hex_location(outputStream* st, const_address p, int unitsize, stringStream& ascii_form) {
970
assert(is_aligned(p, unitsize), "Unaligned");
971
const uintptr_t* pa = (const uintptr_t*) align_down(p, sizeof(intptr_t));
973
// Special handling for printing qwords on 32-bit platforms
975
uintptr_t i1 = 0, i2 = 0;
976
if (read_safely_from(pa, &i1) &&
977
read_safely_from(pa + 1, &i2)) {
978
const uint64_t value =
979
LITTLE_ENDIAN_ONLY((((uint64_t)i2) << 32) | i1)
980
BIG_ENDIAN_ONLY((((uint64_t)i1) << 32) | i2);
981
st->print("%016" FORMAT64_MODIFIER "x", value);
982
print_ascii_form(ascii_form, value, unitsize);
984
st->print_raw("????????????????");
988
#endif // 32-bit, qwords
990
if (read_safely_from(pa, &i)) {
991
// bytes: CA FE BA BE DE AD C0 DE
992
// bytoff: 0 1 2 3 4 5 6 7
993
// LE bits: 0 8 16 24 32 40 48 56
994
// BE bits: 56 48 40 32 24 16 8 0
995
const int offset = (int)(p - (const_address)pa);
996
const int bitoffset =
997
LITTLE_ENDIAN_ONLY(offset * BitsPerByte)
998
BIG_ENDIAN_ONLY((int)((sizeof(intptr_t) - unitsize - offset) * BitsPerByte));
999
const int bitfieldsize = unitsize * BitsPerByte;
1000
uintptr_t value = bitfield(i, bitoffset, bitfieldsize);
1002
case 1: st->print("%02x", (u1)value); break;
1003
case 2: st->print("%04x", (u2)value); break;
1004
case 4: st->print("%08x", (u4)value); break;
1005
case 8: st->print("%016" FORMAT64_MODIFIER "x", (u8)value); break;
1007
print_ascii_form(ascii_form, value, unitsize);
1010
case 1: st->print_raw("??"); break;
1011
case 2: st->print_raw("????"); break;
1012
case 4: st->print_raw("????????"); break;
1013
case 8: st->print_raw("????????????????"); break;
1018
void os::print_hex_dump(outputStream* st, const_address start, const_address end, int unitsize,
1019
bool print_ascii, int bytes_per_line, const_address logical_start) {
1020
constexpr int max_bytes_per_line = 64;
1021
assert(unitsize == 1 || unitsize == 2 || unitsize == 4 || unitsize == 8, "just checking");
1022
assert(bytes_per_line > 0 && bytes_per_line <= max_bytes_per_line &&
1023
is_power_of_2(bytes_per_line), "invalid bytes_per_line");
1025
start = align_down(start, unitsize);
1026
logical_start = align_down(logical_start, unitsize);
1027
bytes_per_line = align_up(bytes_per_line, 8);
1030
const int cols_per_line = bytes_per_line / unitsize;
1032
const_address p = start;
1033
const_address logical_p = logical_start;
1035
stringStream ascii_form;
1037
// Print out the addresses as if we were starting from logical_start.
1040
st->print(PTR_FORMAT ": ", p2i(logical_p));
1042
print_hex_location(st, p, unitsize, ascii_form);
1044
logical_p += unitsize;
1046
if (cols >= cols_per_line) {
1047
if (print_ascii && !ascii_form.is_empty()) {
1048
st->print(" %s", ascii_form.base());
1058
if (cols > 0) { // did not print a full line
1060
// indent last ascii part to match that of full lines
1061
const int size_of_printed_unit = unitsize * 2;
1062
const int space_left = (cols_per_line - cols) * (size_of_printed_unit + 1);
1064
st->print(" %s", ascii_form.base());
1070
void os::print_dhm(outputStream* st, const char* startStr, long sec) {
1071
long days = sec/86400;
1072
long hours = (sec/3600) - (days * 24);
1073
long minutes = (sec/60) - (days * 1440) - (hours * 60);
1074
if (startStr == nullptr) startStr = "";
1075
st->print_cr("%s %ld days %ld:%02ld hours", startStr, days, hours, minutes);
1078
void os::print_tos(outputStream* st, address sp) {
1079
st->print_cr("Top of Stack: (sp=" PTR_FORMAT ")", p2i(sp));
1080
print_hex_dump(st, sp, sp + 512, sizeof(intptr_t));
1083
void os::print_instructions(outputStream* st, address pc, int unitsize) {
1084
st->print_cr("Instructions: (pc=" PTR_FORMAT ")", p2i(pc));
1085
print_hex_dump(st, pc - 256, pc + 256, unitsize, /* print_ascii=*/false);
1088
void os::print_environment_variables(outputStream* st, const char** env_list) {
1090
st->print_cr("Environment Variables:");
1092
for (int i = 0; env_list[i] != nullptr; i++) {
1093
char *envvar = ::getenv(env_list[i]);
1094
if (envvar != nullptr) {
1095
st->print("%s", env_list[i]);
1097
st->print("%s", envvar);
1098
// Use separate cr() printing to avoid unnecessary buffer operations that might cause truncation.
1105
void os::print_register_info(outputStream* st, const void* context) {
1106
int continuation = 0;
1107
print_register_info(st, context, continuation);
1110
void os::print_cpu_info(outputStream* st, char* buf, size_t buflen) {
1113
#if defined(__APPLE__) && !defined(ZERO)
1114
if (VM_Version::is_cpu_emulated()) {
1115
st->print(" (EMULATED)");
1118
st->print(" total %d", os::processor_count());
1119
// It's not safe to query number of active processors after crash
1120
// st->print("(active %d)", os::active_processor_count()); but we can
1121
// print the initial number of active processors.
1122
// We access the raw value here because the assert in the accessor will
1123
// fail if the crash occurs before initialization of this value.
1124
st->print(" (initial active %d)", _initial_active_processor_count);
1125
st->print(" %s", VM_Version::features_string());
1127
pd_print_cpu_info(st, buf, buflen);
1130
// Print a one line string summarizing the cpu, number of cores, memory, and operating system version
1131
void os::print_summary_info(outputStream* st, char* buf, size_t buflen) {
1132
st->print("Host: ");
1134
if (get_host_name(buf, buflen)) {
1135
st->print("%s, ", buf);
1138
get_summary_cpu_info(buf, buflen);
1139
st->print("%s, ", buf);
1140
size_t mem = physical_memory()/G;
1141
if (mem == 0) { // for low memory systems
1142
mem = physical_memory()/M;
1143
st->print("%d cores, " SIZE_FORMAT "M, ", processor_count(), mem);
1145
st->print("%d cores, " SIZE_FORMAT "G, ", processor_count(), mem);
1147
get_summary_os_info(buf, buflen);
1152
static constexpr int secs_per_day = 86400;
1153
static constexpr int secs_per_hour = 3600;
1154
static constexpr int secs_per_min = 60;
1156
void os::print_date_and_time(outputStream *st, char* buf, size_t buflen) {
1160
char* timestring = ctime(&tloc); // ctime adds newline.
1161
// edit out the newline
1162
char* nl = strchr(timestring, '\n');
1163
if (nl != nullptr) {
1168
if (localtime_pd(&tloc, &tz) != nullptr) {
1170
size_t n = ::wcsftime(w_buf, 80, L"%Z", &tz);
1172
::wcstombs(buf, w_buf, buflen);
1173
st->print("Time: %s %s", timestring, buf);
1175
st->print("Time: %s", timestring);
1178
st->print("Time: %s", timestring);
1181
double t = os::elapsedTime();
1182
st->print(" elapsed time: ");
1183
print_elapsed_time(st, t);
1187
void os::print_elapsed_time(outputStream* st, double time) {
1188
// NOTE: a crash using printf("%f",...) on Linux was historically noted here.
1189
int eltime = (int)time; // elapsed time in seconds
1190
int eltimeFraction = (int) ((time - eltime) * 1000000);
1192
// print elapsed time in a human-readable format:
1193
int eldays = eltime / secs_per_day;
1194
int day_secs = eldays * secs_per_day;
1195
int elhours = (eltime - day_secs) / secs_per_hour;
1196
int hour_secs = elhours * secs_per_hour;
1197
int elmins = (eltime - day_secs - hour_secs) / secs_per_min;
1198
int minute_secs = elmins * secs_per_min;
1199
int elsecs = (eltime - day_secs - hour_secs - minute_secs);
1200
st->print("%d.%06d seconds (%dd %dh %dm %ds)", eltime, eltimeFraction, eldays, elhours, elmins, elsecs);
1204
// Check if pointer can be read from (4-byte read access).
1205
// Helps to prove validity of a non-null pointer.
1206
// Returns true in very early stages of VM life when stub is not yet generated.
1207
bool os::is_readable_pointer(const void* p) {
1208
int* const aligned = (int*) align_down((intptr_t)p, 4);
1209
int cafebabe = 0xcafebabe; // tester value 1
1210
int deadbeef = 0xdeadbeef; // tester value 2
1211
return (SafeFetch32(aligned, cafebabe) != cafebabe) || (SafeFetch32(aligned, deadbeef) != deadbeef);
1214
bool os::is_readable_range(const void* from, const void* to) {
1215
if ((uintptr_t)from >= (uintptr_t)to) return false;
1216
for (uintptr_t p = align_down((uintptr_t)from, min_page_size()); p < (uintptr_t)to; p += min_page_size()) {
1217
if (!is_readable_pointer((const void*)p)) {
1225
// moved from debug.cpp (used to be find()) but still called from there
1226
// The verbose parameter is only set by the debug code in one case
1227
void os::print_location(outputStream* st, intptr_t x, bool verbose) {
1228
address addr = (address)x;
1229
// Handle null first, so later checks don't need to protect against it.
1230
if (addr == nullptr) {
1231
st->print_cr("0x0 is null");
1235
// Check if addr points into a code blob.
1236
CodeBlob* b = CodeCache::find_blob(addr);
1238
b->dump_for_addr(addr, st, verbose);
1242
// Check if addr points into Java heap.
1243
if (Universe::heap()->print_location(st, addr)) {
1249
bool accessible = is_readable_pointer(addr);
1251
// Check if addr is a JNI handle.
1252
if (align_down((intptr_t)addr, sizeof(intptr_t)) != 0 && accessible) {
1253
if (JNIHandles::is_global_handle((jobject) addr)) {
1254
st->print_cr(INTPTR_FORMAT " is a global jni handle", p2i(addr));
1257
if (JNIHandles::is_weak_global_handle((jobject) addr)) {
1258
st->print_cr(INTPTR_FORMAT " is a weak global jni handle", p2i(addr));
1263
// Check if addr belongs to a Java thread.
1264
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
1265
// If the addr is a java thread print information about that.
1266
if (addr == (address)thread) {
1268
thread->print_on(st);
1270
st->print_cr(INTPTR_FORMAT " is a thread", p2i(addr));
1274
// If the addr is in the stack region for this thread then report that
1275
// and print thread info
1276
if (thread->is_in_full_stack(addr)) {
1277
st->print_cr(INTPTR_FORMAT " is pointing into the stack for thread: "
1278
INTPTR_FORMAT, p2i(addr), p2i(thread));
1279
if (verbose) thread->print_on(st);
1284
// Check if in metaspace and print types that have vptrs
1285
if (Metaspace::contains(addr)) {
1286
if (Klass::is_valid((Klass*)addr)) {
1287
st->print_cr(INTPTR_FORMAT " is a pointer to class: ", p2i(addr));
1288
((Klass*)addr)->print_on(st);
1289
} else if (Method::is_valid_method((const Method*)addr)) {
1290
((Method*)addr)->print_value_on(st);
1293
// Use addr->print() from the debugger instead (not here)
1294
st->print_cr(INTPTR_FORMAT " is pointing into metadata", p2i(addr));
1299
// Compressed klass needs to be decoded first.
1301
if (UseCompressedClassPointers && ((uintptr_t)addr &~ (uintptr_t)max_juint) == 0) {
1302
narrowKlass narrow_klass = (narrowKlass)(uintptr_t)addr;
1303
Klass* k = CompressedKlassPointers::decode_without_asserts(narrow_klass);
1305
if (Klass::is_valid(k)) {
1306
st->print_cr(UINT32_FORMAT " is a compressed pointer to class: " INTPTR_FORMAT, narrow_klass, p2i((HeapWord*)k));
1313
// Still nothing? If NMT is enabled, we can ask what it thinks...
1314
if (MemTracker::print_containing_region(addr, st)) {
1318
// Try an OS specific find
1319
if (os::find(addr, st)) {
1324
st->print(INTPTR_FORMAT " points into unknown readable memory:", p2i(addr));
1325
if (is_aligned(addr, sizeof(intptr_t))) {
1326
st->print(" " PTR_FORMAT " |", *(intptr_t*)addr);
1328
for (address p = addr; p < align_up(addr + 1, sizeof(intptr_t)); ++p) {
1329
st->print(" %02x", *(u1*)p);
1335
#endif // !INCLUDE_ASAN
1337
st->print_cr(INTPTR_FORMAT " is an unknown value", p2i(addr));
1341
static bool is_pointer_bad(intptr_t* ptr) {
1342
return !is_aligned(ptr, sizeof(uintptr_t)) || !os::is_readable_pointer(ptr);
1345
// Looks like all platforms can use the same function to check if C
1346
// stack is walkable beyond current frame.
1347
// Returns true if this is not the case, i.e. the frame is possibly
1348
// the first C frame on the stack.
1349
bool os::is_first_C_frame(frame* fr) {
1352
return true; // native stack isn't walkable on windows this way.
1354
// Load up sp, fp, sender sp and sender fp, check for reasonable values.
1355
// Check usp first, because if that's bad the other accessors may fault
1356
// on some architectures. Ditto ufp second, etc.
1358
if (is_pointer_bad(fr->sp())) return true;
1360
uintptr_t ufp = (uintptr_t)fr->fp();
1361
if (is_pointer_bad(fr->fp())) return true;
1363
uintptr_t old_sp = (uintptr_t)fr->sender_sp();
1364
if ((uintptr_t)fr->sender_sp() == (uintptr_t)-1 || is_pointer_bad(fr->sender_sp())) return true;
1366
uintptr_t old_fp = (uintptr_t)fr->link_or_null();
1367
if (old_fp == 0 || old_fp == (uintptr_t)-1 || old_fp == ufp ||
1368
is_pointer_bad(fr->link_or_null())) return true;
1370
// stack grows downwards; if old_fp is below current fp or if the stack
1371
// frame is too large, either the stack is corrupted or fp is not saved
1372
// on stack (i.e. on x86, ebp may be used as general register). The stack
1373
// is not walkable beyond current frame.
1374
if (old_fp < ufp) return true;
1375
if (old_fp - ufp > 64 * K) return true;
1380
// Set up the boot classpath.
1382
char* os::format_boot_path(const char* format_string,
1387
assert((fileSep == '/' && pathSep == ':') ||
1388
(fileSep == '\\' && pathSep == ';'), "unexpected separator chars");
1390
// Scan the format string to determine the length of the actual
1391
// boot classpath, and handle platform dependencies as well.
1392
int formatted_path_len = 0;
1394
for (p = format_string; *p != 0; ++p) {
1395
if (*p == '%') formatted_path_len += home_len - 1;
1396
++formatted_path_len;
1399
char* formatted_path = NEW_C_HEAP_ARRAY(char, formatted_path_len + 1, mtInternal);
1401
// Create boot classpath from format, substituting separator chars and
1402
// java home directory.
1403
char* q = formatted_path;
1404
for (p = format_string; *p != 0; ++p) {
1422
assert((q - formatted_path) == formatted_path_len, "formatted_path size botched");
1423
return formatted_path;
1426
// This function is a proxy to fopen, it tries to add a non standard flag ('e' or 'N')
1427
// that ensures automatic closing of the file on exec. If it can not find support in
1428
// the underlying c library, it will make an extra system call (fcntl) to ensure automatic
1429
// closing of the file on exec.
1430
FILE* os::fopen(const char* path, const char* mode) {
1431
char modified_mode[20];
1432
assert(strlen(mode) + 1 < sizeof(modified_mode), "mode chars plus one extra must fit in buffer");
1433
os::snprintf_checked(modified_mode, sizeof(modified_mode), "%s" LINUX_ONLY("e") BSD_ONLY("e") WINDOWS_ONLY("N"), mode);
1434
FILE* file = ::fopen(path, modified_mode);
1436
#if !(defined LINUX || defined BSD || defined _WINDOWS)
1437
// assume fcntl FD_CLOEXEC support as a backup solution when 'e' or 'N'
1438
// is not supported as mode in fopen
1439
if (file != nullptr) {
1440
int fd = fileno(file);
1442
int fd_flags = fcntl(fd, F_GETFD);
1443
if (fd_flags != -1) {
1444
fcntl(fd, F_SETFD, fd_flags | FD_CLOEXEC);
1453
bool os::set_boot_path(char fileSep, char pathSep) {
1454
const char* home = Arguments::get_java_home();
1455
int home_len = (int)strlen(home);
1459
// modular image if "modules" jimage exists
1460
char* jimage = format_boot_path("%/lib/" MODULES_IMAGE_NAME, home, home_len, fileSep, pathSep);
1461
if (jimage == nullptr) return false;
1462
bool has_jimage = (os::stat(jimage, &st) == 0);
1464
Arguments::set_boot_class_path(jimage, true);
1465
FREE_C_HEAP_ARRAY(char, jimage);
1468
FREE_C_HEAP_ARRAY(char, jimage);
1470
// check if developer build with exploded modules
1471
char* base_classes = format_boot_path("%/modules/" JAVA_BASE_NAME, home, home_len, fileSep, pathSep);
1472
if (base_classes == nullptr) return false;
1473
if (os::stat(base_classes, &st) == 0) {
1474
Arguments::set_boot_class_path(base_classes, false);
1475
FREE_C_HEAP_ARRAY(char, base_classes);
1478
FREE_C_HEAP_ARRAY(char, base_classes);
1483
bool os::file_exists(const char* filename) {
1484
struct stat statbuf;
1485
if (filename == nullptr || strlen(filename) == 0) {
1488
return os::stat(filename, &statbuf) == 0;
1491
bool os::write(int fd, const void *buf, size_t nBytes) {
1494
while (nBytes > 0) {
1495
res = pd_write(fd, buf, nBytes);
1496
if (res == OS_ERR) {
1499
buf = (void *)((char *)buf + res);
1507
// Splits a path, based on its separator, the number of
1508
// elements is returned back in "elements".
1509
// file_name_length is used as a modifier for each path's
1510
// length when compared to JVM_MAXPATHLEN. So if you know
1511
// each returned path will have something appended when
1512
// in use, you can pass the length of that in
1513
// file_name_length, to ensure we detect if any path
1514
// exceeds the maximum path length once prepended onto
1515
// the sub-path/file name.
1516
// It is the callers responsibility to:
1517
// a> check the value of "elements", which may be 0.
1518
// b> ignore any empty path elements
1519
// c> free up the data.
1520
char** os::split_path(const char* path, size_t* elements, size_t file_name_length) {
1521
*elements = (size_t)0;
1522
if (path == nullptr || strlen(path) == 0 || file_name_length == (size_t)nullptr) {
1525
const char psepchar = *os::path_separator();
1526
char* inpath = NEW_C_HEAP_ARRAY(char, strlen(path) + 1, mtInternal);
1527
strcpy(inpath, path);
1529
char* p = strchr(inpath, psepchar);
1530
// Get a count of elements to allocate memory
1531
while (p != nullptr) {
1534
p = strchr(p, psepchar);
1537
char** opath = NEW_C_HEAP_ARRAY(char*, count, mtInternal);
1539
// do the actual splitting
1541
for (size_t i = 0 ; i < count ; i++) {
1542
size_t len = strcspn(p, os::path_separator());
1543
if (len + file_name_length > JVM_MAXPATHLEN) {
1544
// release allocated storage before exiting the vm
1545
free_array_of_char_arrays(opath, i++);
1546
vm_exit_during_initialization("The VM tried to use a path that exceeds the maximum path length for "
1547
"this system. Review path-containing parameters and properties, such as "
1548
"sun.boot.library.path, to identify potential sources for this path.");
1550
// allocate the string and add terminator storage
1551
char* s = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
1557
FREE_C_HEAP_ARRAY(char, inpath);
1562
// Returns true if the current stack pointer is above the stack shadow
1563
// pages, false otherwise.
1564
bool os::stack_shadow_pages_available(Thread *thread, const methodHandle& method, address sp) {
1565
if (!thread->is_Java_thread()) return false;
1566
// Check if we have StackShadowPages above the guard zone. This parameter
1567
// is dependent on the depth of the maximum VM call stack possible from
1568
// the handler for stack overflow. 'instanceof' in the stack overflow
1569
// handler or a println uses at least 8k stack of VM and native code
1571
const int framesize_in_bytes =
1572
Interpreter::size_top_interpreter_activation(method()) * wordSize;
1574
address limit = JavaThread::cast(thread)->stack_overflow_state()->shadow_zone_safe_limit();
1575
return sp > (limit + framesize_in_bytes);
1578
size_t os::page_size_for_region(size_t region_size, size_t min_pages, bool must_be_aligned) {
1579
assert(min_pages > 0, "sanity");
1580
if (UseLargePages) {
1581
const size_t max_page_size = region_size / min_pages;
1583
for (size_t page_size = page_sizes().largest(); page_size != 0;
1584
page_size = page_sizes().next_smaller(page_size)) {
1585
if (page_size <= max_page_size) {
1586
if (!must_be_aligned || is_aligned(region_size, page_size)) {
1593
return vm_page_size();
1596
size_t os::page_size_for_region_aligned(size_t region_size, size_t min_pages) {
1597
return page_size_for_region(region_size, min_pages, true);
1600
size_t os::page_size_for_region_unaligned(size_t region_size, size_t min_pages) {
1601
return page_size_for_region(region_size, min_pages, false);
1605
#define MAX_PATH (2 * K)
1609
char filename[MAX_PATH];
1610
if (PauseAtStartupFile && PauseAtStartupFile[0]) {
1611
jio_snprintf(filename, MAX_PATH, "%s", PauseAtStartupFile);
1613
jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
1616
int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
1620
while (::stat(filename, &buf) == 0) {
1621
#if defined(_WINDOWS)
1624
(void)::poll(nullptr, 0, 100);
1629
"Could not open pause file '%s', continuing immediately.\n", filename);
1633
static const char* errno_to_string (int e, bool short_text) {
1634
#define ALL_SHARED_ENUMS(X) \
1635
X(E2BIG, "Argument list too long") \
1636
X(EACCES, "Permission denied") \
1637
X(EADDRINUSE, "Address in use") \
1638
X(EADDRNOTAVAIL, "Address not available") \
1639
X(EAFNOSUPPORT, "Address family not supported") \
1640
X(EAGAIN, "Resource unavailable, try again") \
1641
X(EALREADY, "Connection already in progress") \
1642
X(EBADF, "Bad file descriptor") \
1643
X(EBADMSG, "Bad message") \
1644
X(EBUSY, "Device or resource busy") \
1645
X(ECANCELED, "Operation canceled") \
1646
X(ECHILD, "No child processes") \
1647
X(ECONNABORTED, "Connection aborted") \
1648
X(ECONNREFUSED, "Connection refused") \
1649
X(ECONNRESET, "Connection reset") \
1650
X(EDEADLK, "Resource deadlock would occur") \
1651
X(EDESTADDRREQ, "Destination address required") \
1652
X(EDOM, "Mathematics argument out of domain of function") \
1653
X(EEXIST, "File exists") \
1654
X(EFAULT, "Bad address") \
1655
X(EFBIG, "File too large") \
1656
X(EHOSTUNREACH, "Host is unreachable") \
1657
X(EIDRM, "Identifier removed") \
1658
X(EILSEQ, "Illegal byte sequence") \
1659
X(EINPROGRESS, "Operation in progress") \
1660
X(EINTR, "Interrupted function") \
1661
X(EINVAL, "Invalid argument") \
1662
X(EIO, "I/O error") \
1663
X(EISCONN, "Socket is connected") \
1664
X(EISDIR, "Is a directory") \
1665
X(ELOOP, "Too many levels of symbolic links") \
1666
X(EMFILE, "Too many open files") \
1667
X(EMLINK, "Too many links") \
1668
X(EMSGSIZE, "Message too large") \
1669
X(ENAMETOOLONG, "Filename too long") \
1670
X(ENETDOWN, "Network is down") \
1671
X(ENETRESET, "Connection aborted by network") \
1672
X(ENETUNREACH, "Network unreachable") \
1673
X(ENFILE, "Too many files open in system") \
1674
X(ENOBUFS, "No buffer space available") \
1675
X(ENODATA, "No message is available on the STREAM head read queue") \
1676
X(ENODEV, "No such device") \
1677
X(ENOENT, "No such file or directory") \
1678
X(ENOEXEC, "Executable file format error") \
1679
X(ENOLCK, "No locks available") \
1680
X(ENOLINK, "Reserved") \
1681
X(ENOMEM, "Not enough space") \
1682
X(ENOMSG, "No message of the desired type") \
1683
X(ENOPROTOOPT, "Protocol not available") \
1684
X(ENOSPC, "No space left on device") \
1685
X(ENOSR, "No STREAM resources") \
1686
X(ENOSTR, "Not a STREAM") \
1687
X(ENOSYS, "Function not supported") \
1688
X(ENOTCONN, "The socket is not connected") \
1689
X(ENOTDIR, "Not a directory") \
1690
X(ENOTEMPTY, "Directory not empty") \
1691
X(ENOTSOCK, "Not a socket") \
1692
X(ENOTSUP, "Not supported") \
1693
X(ENOTTY, "Inappropriate I/O control operation") \
1694
X(ENXIO, "No such device or address") \
1695
X(EOPNOTSUPP, "Operation not supported on socket") \
1696
X(EOVERFLOW, "Value too large to be stored in data type") \
1697
X(EPERM, "Operation not permitted") \
1698
X(EPIPE, "Broken pipe") \
1699
X(EPROTO, "Protocol error") \
1700
X(EPROTONOSUPPORT, "Protocol not supported") \
1701
X(EPROTOTYPE, "Protocol wrong type for socket") \
1702
X(ERANGE, "Result too large") \
1703
X(EROFS, "Read-only file system") \
1704
X(ESPIPE, "Invalid seek") \
1705
X(ESRCH, "No such process") \
1706
X(ETIME, "Stream ioctl() timeout") \
1707
X(ETIMEDOUT, "Connection timed out") \
1708
X(ETXTBSY, "Text file busy") \
1709
X(EWOULDBLOCK, "Operation would block") \
1710
X(EXDEV, "Cross-device link")
1712
#define DEFINE_ENTRY(e, text) { e, #e, text },
1714
static const struct {
1716
const char* short_text;
1717
const char* long_text;
1720
ALL_SHARED_ENUMS(DEFINE_ENTRY)
1722
// The following enums are not defined on all platforms.
1724
DEFINE_ENTRY(ESTALE, "Reserved")
1727
DEFINE_ENTRY(EDQUOT, "Reserved")
1730
DEFINE_ENTRY(EMULTIHOP, "Reserved")
1734
{ -1, "Unknown errno", "Unknown error" }
1742
while (table[i].v != -1 && table[i].v != e) {
1746
return short_text ? table[i].short_text : table[i].long_text;
1750
const char* os::strerror(int e) {
1751
return errno_to_string(e, false);
1754
const char* os::errno_name(int e) {
1755
return errno_to_string(e, true);
1758
// create binary file, rewriting existing file if required
1759
int os::create_binary_file(const char* path, bool rewrite_existing) {
1760
int oflags = O_WRONLY | O_CREAT WINDOWS_ONLY(| O_BINARY);
1761
oflags |= rewrite_existing ? O_TRUNC : O_EXCL;
1762
return ::open(path, oflags, S_IREAD | S_IWRITE);
1765
#define trace_page_size_params(size) byte_size_in_exact_unit(size), exact_unit_for_byte_size(size)
1767
void os::trace_page_sizes(const char* str,
1768
const size_t region_min_size,
1769
const size_t region_max_size,
1772
const size_t page_size) {
1774
log_info(pagesize)("%s: "
1775
" min=" SIZE_FORMAT "%s"
1776
" max=" SIZE_FORMAT "%s"
1778
" size=" SIZE_FORMAT "%s"
1779
" page_size=" SIZE_FORMAT "%s",
1781
trace_page_size_params(region_min_size),
1782
trace_page_size_params(region_max_size),
1784
trace_page_size_params(size),
1785
trace_page_size_params(page_size));
1788
void os::trace_page_sizes_for_requested_size(const char* str,
1789
const size_t requested_size,
1790
const size_t requested_page_size,
1793
const size_t page_size) {
1795
log_info(pagesize)("%s:"
1796
" req_size=" SIZE_FORMAT "%s"
1797
" req_page_size=" SIZE_FORMAT "%s"
1799
" size=" SIZE_FORMAT "%s"
1800
" page_size=" SIZE_FORMAT "%s",
1802
trace_page_size_params(requested_size),
1803
trace_page_size_params(requested_page_size),
1805
trace_page_size_params(size),
1806
trace_page_size_params(page_size));
1810
// This is the working definition of a server class machine:
1811
// >= 2 physical CPU's and >=2GB of memory, with some fuzz
1812
// because the graphics memory (?) sometimes masks physical memory.
1813
// If you want to change the definition of a server class machine
1814
// on some OS or platform, e.g., >=4GB on Windows platforms,
1815
// then you'll have to parameterize this method based on that state,
1816
// as was done for logical processors here, or replicate and
1817
// specialize this method for each platform. (Or fix os to have
1818
// some inheritance structure and use subclassing. Sigh.)
1819
// If you want some platform to always or never behave as a server
1820
// class machine, change the setting of AlwaysActAsServerClassMachine
1821
// and NeverActAsServerClassMachine in globals*.hpp.
1822
bool os::is_server_class_machine() {
1823
// First check for the early returns
1824
if (NeverActAsServerClassMachine) {
1827
if (AlwaysActAsServerClassMachine) {
1830
// Then actually look at the machine
1831
bool result = false;
1832
const unsigned int server_processors = 2;
1833
const julong server_memory = 2UL * G;
1834
// We seem not to get our full complement of memory.
1835
// We allow some part (1/8?) of the memory to be "missing",
1836
// based on the sizes of DIMMs, and maybe graphics cards.
1837
const julong missing_memory = 256UL * M;
1839
/* Is this a server class machine? */
1840
if ((os::active_processor_count() >= (int)server_processors) &&
1841
(os::physical_memory() >= (server_memory - missing_memory))) {
1842
const unsigned int logical_processors =
1843
VM_Version::logical_processors_per_package();
1844
if (logical_processors > 1) {
1845
const unsigned int physical_packages =
1846
os::active_processor_count() / logical_processors;
1847
if (physical_packages >= server_processors) {
1857
void os::initialize_initial_active_processor_count() {
1858
assert(_initial_active_processor_count == 0, "Initial active processor count already set.");
1859
_initial_active_processor_count = active_processor_count();
1860
log_debug(os)("Initial active processor count set to %d" , _initial_active_processor_count);
1863
bool os::create_stack_guard_pages(char* addr, size_t bytes) {
1864
return os::pd_create_stack_guard_pages(addr, bytes);
1867
char* os::reserve_memory(size_t bytes, bool executable, MEMFLAGS flags) {
1868
char* result = pd_reserve_memory(bytes, executable);
1869
if (result != nullptr) {
1870
MemTracker::record_virtual_memory_reserve(result, bytes, CALLER_PC, flags);
1871
log_debug(os, map)("Reserved " RANGEFMT, RANGEFMTARGS(result, bytes));
1873
log_info(os, map)("Reserve failed (%zu bytes)", bytes);
1878
char* os::attempt_reserve_memory_at(char* addr, size_t bytes, bool executable, MEMFLAGS flag) {
1879
char* result = SimulateFullAddressSpace ? nullptr : pd_attempt_reserve_memory_at(addr, bytes, executable);
1880
if (result != nullptr) {
1881
MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC, flag);
1882
log_debug(os, map)("Reserved " RANGEFMT, RANGEFMTARGS(result, bytes));
1884
log_info(os, map)("Attempt to reserve " RANGEFMT " failed",
1885
RANGEFMTARGS(addr, bytes));
1891
static void print_points(const char* s, unsigned* points, unsigned num) {
1893
for (unsigned i = 0; i < num; i ++) {
1894
ss.print("%u ", points[i]);
1896
log_trace(os, map)("%s, %u Points: %s", s, num, ss.base());
1900
// Helper for os::attempt_reserve_memory_between
1901
// Given an array of things, shuffle them (Fisher-Yates)
1902
template <typename T>
1903
static void shuffle_fisher_yates(T* arr, unsigned num, FastRandom& frand) {
1904
for (unsigned i = num - 1; i >= 1; i--) {
1905
unsigned j = frand.next() % i;
1906
swap(arr[i], arr[j]);
1910
// Helper for os::attempt_reserve_memory_between
1911
// Given an array of things, do a hemisphere split such that the resulting
1912
// order is: [first, last, first + 1, last - 1, ...]
1913
template <typename T>
1914
static void hemi_split(T* arr, unsigned num) {
1915
T* tmp = (T*)::alloca(sizeof(T) * num);
1916
for (unsigned i = 0; i < num; i++) {
1919
for (unsigned i = 0; i < num; i++) {
1920
arr[i] = is_even(i) ? tmp[i / 2] : tmp[num - (i / 2) - 1];
1924
// Given an address range [min, max), attempts to reserve memory within this area, with the given alignment.
1925
// If randomize is true, the location will be randomized.
1926
char* os::attempt_reserve_memory_between(char* min, char* max, size_t bytes, size_t alignment, bool randomize) {
1928
// Please keep the following constants in sync with the companion gtests:
1930
// Number of mmap attemts we will undertake.
1931
constexpr unsigned max_attempts = 32;
1933
// In randomization mode: We require a minimum number of possible attach points for
1934
// randomness. Below that we refuse to reserve anything.
1935
constexpr unsigned min_random_value_range = 16;
1937
// In randomization mode: If the possible value range is below this threshold, we
1938
// use a total shuffle without regard for address space fragmentation, otherwise
1939
// we attempt to minimize fragmentation.
1940
constexpr unsigned total_shuffle_threshold = 1024;
1942
#define ARGSFMT "range [" PTR_FORMAT "-" PTR_FORMAT "), size " SIZE_FORMAT_X ", alignment " SIZE_FORMAT_X ", randomize: %d"
1943
#define ARGSFMTARGS p2i(min), p2i(max), bytes, alignment, randomize
1945
log_debug(os, map) ("reserve_between (" ARGSFMT ")", ARGSFMTARGS);
1947
assert(is_power_of_2(alignment), "alignment invalid (" ARGSFMT ")", ARGSFMTARGS);
1948
assert(alignment < SIZE_MAX / 2, "alignment too large (" ARGSFMT ")", ARGSFMTARGS);
1949
assert(is_aligned(bytes, os::vm_page_size()), "size not page aligned (" ARGSFMT ")", ARGSFMTARGS);
1950
assert(max >= min, "invalid range (" ARGSFMT ")", ARGSFMTARGS);
1952
char* const absolute_max = (char*)(NOT_LP64(G * 3) LP64_ONLY(G * 128 * 1024));
1953
char* const absolute_min = (char*) os::vm_min_address();
1955
// AIX is the only platform that uses System V shm for reserving virtual memory.
1956
// In this case, the required alignment of the allocated size (64K) and the alignment
1957
// of possible start points of the memory region (256M) differ.
1958
// This is not reflected by os_allocation_granularity().
1959
// The logic here is dual to the one in pd_reserve_memory in os_aix.cpp
1960
const size_t system_allocation_granularity =
1961
AIX_ONLY((!os::Aix::supports_64K_mmap_pages() && os::vm_page_size() == 64*K) ? 256*M : ) os::vm_allocation_granularity();
1963
const size_t alignment_adjusted = MAX2(alignment, system_allocation_granularity);
1965
// Calculate first and last possible attach points:
1966
char* const lo_att = align_up(MAX2(absolute_min, min), alignment_adjusted);
1967
if (lo_att == nullptr) {
1968
return nullptr; // overflow
1971
char* const hi_end = MIN2(max, absolute_max);
1972
if ((uintptr_t)hi_end < bytes) {
1973
return nullptr; // no need to go on
1975
char* const hi_att = align_down(hi_end - bytes, alignment_adjusted);
1977
return nullptr; // overflow
1980
// no possible attach points
1981
if (hi_att < lo_att) {
1985
char* result = nullptr;
1987
const size_t num_attach_points = (size_t)((hi_att - lo_att) / alignment_adjusted) + 1;
1988
assert(num_attach_points > 0, "Sanity");
1990
// If this fires, the input range is too large for the given alignment (we work
1991
// with int below to keep things simple). Since alignment is bound to page size,
1992
// and the lowest page size is 4K, this gives us a minimum of 4K*4G=8TB address
1994
assert(num_attach_points <= UINT_MAX,
1995
"Too many possible attach points - range too large or alignment too small (" ARGSFMT ")", ARGSFMTARGS);
1997
const unsigned num_attempts = MIN2((unsigned)num_attach_points, max_attempts);
1998
unsigned points[max_attempts];
2003
if (num_attach_points < min_random_value_range) {
2007
// We pre-calc the attach points:
2008
// 1 We divide the attach range into equidistant sections and calculate an attach
2009
// point within each section.
2010
// 2 We wiggle those attach points around within their section (depends on attach
2011
// point granularity)
2012
// 3 Should that not be enough to get effective randomization, shuffle all
2014
// 4 Otherwise, re-order them to get an optimized probing sequence.
2015
const unsigned stepsize = (unsigned)num_attach_points / num_attempts;
2016
const unsigned half = num_attempts / 2;
2018
// 1+2: pre-calc points
2019
for (unsigned i = 0; i < num_attempts; i++) {
2020
const unsigned deviation = stepsize > 1 ? (frand.next() % stepsize) : 0;
2021
points[i] = (i * stepsize) + deviation;
2024
if (num_attach_points < total_shuffle_threshold) {
2026
// The numeber of possible attach points is too low for the "wiggle" from
2027
// point 2 to be enough to provide randomization. In that case, shuffle
2028
// all attach points at the cost of possible fragmentation (e.g. if we
2029
// end up mapping into the middle of the range).
2030
shuffle_fisher_yates(points, num_attempts, frand);
2033
// We have a large enough number of attach points to satisfy the randomness
2034
// goal without. In that case, we optimize probing by sorting the attach
2035
// points: We attempt outermost points first, then work ourselves up to
2036
// the middle. That reduces address space fragmentation. We also alternate
2037
// hemispheres, which increases the chance of successfull mappings if the
2038
// previous mapping had been blocked by large maps.
2039
hemi_split(points, num_attempts);
2041
} // end: randomized
2044
// Non-randomized. We just attempt to reserve by probing sequentially. We
2045
// alternate between hemispheres, working ourselves up to the middle.
2046
const int stepsize = (unsigned)num_attach_points / num_attempts;
2047
for (unsigned i = 0; i < num_attempts; i++) {
2048
points[i] = (i * stepsize);
2050
hemi_split(points, num_attempts);
2054
// Print + check all pre-calculated attach points
2055
print_points("before reserve", points, num_attempts);
2056
for (unsigned i = 0; i < num_attempts; i++) {
2057
assert(points[i] < num_attach_points, "Candidate attach point %d out of range (%u, num_attach_points: %zu) " ARGSFMT,
2058
i, points[i], num_attach_points, ARGSFMTARGS);
2063
for (unsigned i = 0; result == nullptr && i < num_attempts; i++) {
2064
const unsigned candidate_offset = points[i];
2065
char* const candidate = lo_att + candidate_offset * alignment_adjusted;
2066
assert(candidate <= hi_att, "Invalid offset %u (" ARGSFMT ")", candidate_offset, ARGSFMTARGS);
2067
result = SimulateFullAddressSpace ? nullptr : os::pd_attempt_reserve_memory_at(candidate, bytes, false);
2069
log_trace(os, map)("Failed to attach at " PTR_FORMAT, p2i(candidate));
2073
// Sanity checks, logging, NMT stuff:
2074
if (result != nullptr) {
2075
#define ERRFMT "result: " PTR_FORMAT " " ARGSFMT
2076
#define ERRFMTARGS p2i(result), ARGSFMTARGS
2077
assert(result >= min, "OOB min (" ERRFMT ")", ERRFMTARGS);
2078
assert((result + bytes) <= max, "OOB max (" ERRFMT ")", ERRFMTARGS);
2079
assert(result >= (char*)os::vm_min_address(), "OOB vm.map min (" ERRFMT ")", ERRFMTARGS);
2080
assert((result + bytes) <= absolute_max, "OOB vm.map max (" ERRFMT ")", ERRFMTARGS);
2081
assert(is_aligned(result, alignment), "alignment invalid (" ERRFMT ")", ERRFMTARGS);
2082
log_trace(os, map)(ERRFMT, ERRFMTARGS);
2083
log_debug(os, map)("successfully attached at " PTR_FORMAT, p2i(result));
2084
MemTracker::record_virtual_memory_reserve((address)result, bytes, CALLER_PC);
2086
log_debug(os, map)("failed to attach anywhere in [" PTR_FORMAT "-" PTR_FORMAT ")", p2i(min), p2i(max));
2095
static void assert_nonempty_range(const char* addr, size_t bytes) {
2096
assert(addr != nullptr && bytes > 0, "invalid range [" PTR_FORMAT ", " PTR_FORMAT ")",
2097
p2i(addr), p2i(addr) + bytes);
2100
julong os::used_memory() {
2102
if (OSContainer::is_containerized()) {
2103
jlong mem_usage = OSContainer::memory_usage_in_bytes();
2104
if (mem_usage > 0) {
2109
return os::physical_memory() - os::available_memory();
2113
bool os::commit_memory(char* addr, size_t bytes, bool executable) {
2114
assert_nonempty_range(addr, bytes);
2115
bool res = pd_commit_memory(addr, bytes, executable);
2117
MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
2118
log_debug(os, map)("Committed " RANGEFMT, RANGEFMTARGS(addr, bytes));
2120
log_info(os, map)("Failed to commit " RANGEFMT, RANGEFMTARGS(addr, bytes));
2125
bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
2127
assert_nonempty_range(addr, size);
2128
bool res = os::pd_commit_memory(addr, size, alignment_hint, executable);
2130
MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
2131
log_debug(os, map)("Committed " RANGEFMT, RANGEFMTARGS(addr, size));
2133
log_info(os, map)("Failed to commit " RANGEFMT, RANGEFMTARGS(addr, size));
2138
void os::commit_memory_or_exit(char* addr, size_t bytes, bool executable,
2140
assert_nonempty_range(addr, bytes);
2141
pd_commit_memory_or_exit(addr, bytes, executable, mesg);
2142
MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
2145
void os::commit_memory_or_exit(char* addr, size_t size, size_t alignment_hint,
2146
bool executable, const char* mesg) {
2147
assert_nonempty_range(addr, size);
2148
os::pd_commit_memory_or_exit(addr, size, alignment_hint, executable, mesg);
2149
MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
2152
bool os::uncommit_memory(char* addr, size_t bytes, bool executable) {
2153
assert_nonempty_range(addr, bytes);
2155
if (MemTracker::enabled()) {
2157
res = pd_uncommit_memory(addr, bytes, executable);
2159
MemTracker::record_virtual_memory_uncommit((address)addr, bytes);
2162
res = pd_uncommit_memory(addr, bytes, executable);
2166
log_debug(os, map)("Uncommitted " RANGEFMT, RANGEFMTARGS(addr, bytes));
2168
log_info(os, map)("Failed to uncommit " RANGEFMT, RANGEFMTARGS(addr, bytes));
2174
bool os::release_memory(char* addr, size_t bytes) {
2175
assert_nonempty_range(addr, bytes);
2177
if (MemTracker::enabled()) {
2179
res = pd_release_memory(addr, bytes);
2181
MemTracker::record_virtual_memory_release((address)addr, bytes);
2184
res = pd_release_memory(addr, bytes);
2187
log_info(os, map)("Failed to release " RANGEFMT, RANGEFMTARGS(addr, bytes));
2189
log_debug(os, map)("Released " RANGEFMT, RANGEFMTARGS(addr, bytes));
2194
// Prints all mappings
2195
void os::print_memory_mappings(outputStream* st) {
2196
os::print_memory_mappings(nullptr, SIZE_MAX, st);
2199
// Pretouching must use a store, not just a load. On many OSes loads from
2200
// fresh memory would be satisfied from a single mapped page containing all
2201
// zeros. We need to store something to each page to get them backed by
2202
// their own memory, which is the effect we want here. An atomic add of
2203
// zero is used instead of a simple store, allowing the memory to be used
2204
// while pretouch is in progress, rather than requiring users of the memory
2205
// to wait until the entire range has been touched. This is technically
2206
// a UB data race, but doesn't cause any problems for us.
2207
void os::pretouch_memory(void* start, void* end, size_t page_size) {
2208
assert(start <= end, "invalid range: " PTR_FORMAT " -> " PTR_FORMAT, p2i(start), p2i(end));
2209
assert(is_power_of_2(page_size), "page size misaligned: %zu", page_size);
2210
assert(page_size >= sizeof(int), "page size too small: %zu", page_size);
2212
// We're doing concurrent-safe touch and memory state has page
2213
// granularity, so we can touch anywhere in a page. Touch at the
2214
// beginning of each page to simplify iteration.
2215
void* first = align_down(start, page_size);
2216
void* last = align_down(static_cast<char*>(end) - 1, page_size);
2217
assert(first <= last, "invariant");
2218
const size_t pd_page_size = pd_pretouch_memory(first, last, page_size);
2219
if (pd_page_size > 0) {
2220
// Iterate from first page through last (inclusive), being careful to
2221
// avoid overflow if the last page abuts the end of the address range.
2222
last = align_down(static_cast<char*>(end) - 1, pd_page_size);
2223
for (char* cur = static_cast<char*>(first); /* break */; cur += pd_page_size) {
2224
Atomic::add(reinterpret_cast<int*>(cur), 0, memory_order_relaxed);
2225
if (cur >= last) break;
2231
char* os::map_memory_to_file(size_t bytes, int file_desc, MEMFLAGS flag) {
2232
// Could have called pd_reserve_memory() followed by replace_existing_mapping_with_file_mapping(),
2233
// but AIX may use SHM in which case its more trouble to detach the segment and remap memory to the file.
2234
// On all current implementations null is interpreted as any available address.
2235
char* result = os::map_memory_to_file(nullptr /* addr */, bytes, file_desc);
2236
if (result != nullptr) {
2237
MemTracker::record_virtual_memory_reserve_and_commit(result, bytes, CALLER_PC, flag);
2242
char* os::attempt_map_memory_to_file_at(char* addr, size_t bytes, int file_desc, MEMFLAGS flag) {
2243
char* result = pd_attempt_map_memory_to_file_at(addr, bytes, file_desc);
2244
if (result != nullptr) {
2245
MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC, flag);
2250
char* os::map_memory(int fd, const char* file_name, size_t file_offset,
2251
char *addr, size_t bytes, bool read_only,
2252
bool allow_exec, MEMFLAGS flags) {
2253
char* result = pd_map_memory(fd, file_name, file_offset, addr, bytes, read_only, allow_exec);
2254
if (result != nullptr) {
2255
MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, CALLER_PC, flags);
2260
bool os::unmap_memory(char *addr, size_t bytes) {
2262
if (MemTracker::enabled()) {
2264
result = pd_unmap_memory(addr, bytes);
2266
MemTracker::record_virtual_memory_release((address)addr, bytes);
2269
result = pd_unmap_memory(addr, bytes);
2274
void os::disclaim_memory(char *addr, size_t bytes) {
2275
pd_disclaim_memory(addr, bytes);
2278
void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
2279
pd_realign_memory(addr, bytes, alignment_hint);
2282
char* os::reserve_memory_special(size_t size, size_t alignment, size_t page_size,
2283
char* addr, bool executable) {
2285
assert(is_aligned(addr, alignment), "Unaligned request address");
2287
char* result = pd_reserve_memory_special(size, alignment, page_size, addr, executable);
2288
if (result != nullptr) {
2289
// The memory is committed
2290
MemTracker::record_virtual_memory_reserve_and_commit((address)result, size, CALLER_PC);
2291
log_debug(os, map)("Reserved and committed " RANGEFMT, RANGEFMTARGS(result, size));
2293
log_info(os, map)("Reserve and commit failed (%zu bytes)", size);
2299
bool os::release_memory_special(char* addr, size_t bytes) {
2301
if (MemTracker::enabled()) {
2303
res = pd_release_memory_special(addr, bytes);
2305
MemTracker::record_virtual_memory_release((address)addr, bytes);
2308
res = pd_release_memory_special(addr, bytes);
2313
// Convenience wrapper around naked_short_sleep to allow for longer sleep
2314
// times. Only for use by non-JavaThreads.
2315
void os::naked_sleep(jlong millis) {
2316
assert(!Thread::current()->is_Java_thread(), "not for use by JavaThreads");
2317
const jlong limit = 999;
2318
while (millis > limit) {
2319
naked_short_sleep(limit);
2322
naked_short_sleep(millis);
2326
////// Implementation of PageSizes
2328
void os::PageSizes::add(size_t page_size) {
2329
assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
2333
bool os::PageSizes::contains(size_t page_size) const {
2334
assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
2335
return (_v & page_size) != 0;
2338
size_t os::PageSizes::next_smaller(size_t page_size) const {
2339
assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
2340
size_t v2 = _v & (page_size - 1);
2344
return round_down_power_of_2(v2);
2347
size_t os::PageSizes::next_larger(size_t page_size) const {
2348
assert(is_power_of_2(page_size), "page_size must be a power of 2: " SIZE_FORMAT_X, page_size);
2349
if (page_size == max_power_of_2<size_t>()) { // Shift by 32/64 would be UB
2352
// Remove current and smaller page sizes
2353
size_t v2 = _v & ~(page_size + (page_size - 1));
2357
return (size_t)1 << count_trailing_zeros(v2);
2360
size_t os::PageSizes::largest() const {
2361
const size_t max = max_power_of_2<size_t>();
2362
if (contains(max)) {
2365
return next_smaller(max);
2368
size_t os::PageSizes::smallest() const {
2369
// Strictly speaking the set should not contain sizes < os::vm_page_size().
2370
// But this is not enforced.
2371
return next_larger(1);
2374
void os::PageSizes::print_on(outputStream* st) const {
2376
for (size_t sz = smallest(); sz != 0; sz = next_larger(sz)) {
2380
st->print_raw(", ");
2383
st->print(SIZE_FORMAT "k", sz / K);
2384
} else if (sz < G) {
2385
st->print(SIZE_FORMAT "M", sz / M);
2387
st->print(SIZE_FORMAT "G", sz / G);
2395
// Check minimum allowable stack sizes for thread creation and to initialize
2396
// the java system classes, including StackOverflowError - depends on page
2398
// The space needed for frames during startup is platform dependent. It
2399
// depends on word size, platform calling conventions, C frame layout and
2400
// interpreter/C1/C2 design decisions. Therefore this is given in a
2401
// platform (os/cpu) dependent constant.
2402
// To this, space for guard mechanisms is added, which depends on the
2403
// page size which again depends on the concrete system the VM is running
2404
// on. Space for libc guard pages is not included in this size.
2405
jint os::set_minimum_stack_sizes() {
2407
_java_thread_min_stack_allowed = _java_thread_min_stack_allowed +
2408
StackOverflow::stack_guard_zone_size() +
2409
StackOverflow::stack_shadow_zone_size();
2411
_java_thread_min_stack_allowed = align_up(_java_thread_min_stack_allowed, vm_page_size());
2412
_java_thread_min_stack_allowed = MAX2(_java_thread_min_stack_allowed, _os_min_stack_allowed);
2414
size_t stack_size_in_bytes = ThreadStackSize * K;
2415
if (stack_size_in_bytes != 0 &&
2416
stack_size_in_bytes < _java_thread_min_stack_allowed) {
2417
// The '-Xss' and '-XX:ThreadStackSize=N' options both set
2418
// ThreadStackSize so we go with "Java thread stack size" instead
2419
// of "ThreadStackSize" to be more friendly.
2420
tty->print_cr("\nThe Java thread stack size specified is too small. "
2421
"Specify at least " SIZE_FORMAT "k",
2422
_java_thread_min_stack_allowed / K);
2426
// Make the stack size a multiple of the page size so that
2427
// the yellow/red zones can be guarded.
2428
JavaThread::set_stack_size_at_create(align_up(stack_size_in_bytes, vm_page_size()));
2430
// Reminder: a compiler thread is a Java thread.
2431
_compiler_thread_min_stack_allowed = _compiler_thread_min_stack_allowed +
2432
StackOverflow::stack_guard_zone_size() +
2433
StackOverflow::stack_shadow_zone_size();
2435
_compiler_thread_min_stack_allowed = align_up(_compiler_thread_min_stack_allowed, vm_page_size());
2436
_compiler_thread_min_stack_allowed = MAX2(_compiler_thread_min_stack_allowed, _os_min_stack_allowed);
2438
stack_size_in_bytes = CompilerThreadStackSize * K;
2439
if (stack_size_in_bytes != 0 &&
2440
stack_size_in_bytes < _compiler_thread_min_stack_allowed) {
2441
tty->print_cr("\nThe CompilerThreadStackSize specified is too small. "
2442
"Specify at least " SIZE_FORMAT "k",
2443
_compiler_thread_min_stack_allowed / K);
2447
_vm_internal_thread_min_stack_allowed = align_up(_vm_internal_thread_min_stack_allowed, vm_page_size());
2448
_vm_internal_thread_min_stack_allowed = MAX2(_vm_internal_thread_min_stack_allowed, _os_min_stack_allowed);
2450
stack_size_in_bytes = VMThreadStackSize * K;
2451
if (stack_size_in_bytes != 0 &&
2452
stack_size_in_bytes < _vm_internal_thread_min_stack_allowed) {
2453
tty->print_cr("\nThe VMThreadStackSize specified is too small. "
2454
"Specify at least " SIZE_FORMAT "k",
2455
_vm_internal_thread_min_stack_allowed / K);