diff --git a/src/hotspot/share/compiler/compilerThread.cpp b/src/hotspot/share/compiler/compilerThread.cpp index 47e3f5a6f49..e212200a47c 100644 --- a/src/hotspot/share/compiler/compilerThread.cpp +++ b/src/hotspot/share/compiler/compilerThread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,7 +32,7 @@ // Create a CompilerThread CompilerThread::CompilerThread(CompileQueue* queue, CompilerCounters* counters) - : JavaThread(&CompilerThread::thread_entry) { + : JavaThread(&CompilerThread::thread_entry, 0, mtCompiler) { _env = nullptr; _log = nullptr; _task = nullptr; @@ -43,9 +43,6 @@ CompilerThread::CompilerThread(CompileQueue* queue, _compiler = nullptr; _arena_stat = CompilationMemoryStatistic::enabled() ? new ArenaStatCounter : nullptr; - // Compiler uses resource area for compilation, let's bias it to mtCompiler - resource_area()->bias_to(mtCompiler); - #ifndef PRODUCT _ideal_graph_printer = nullptr; #endif diff --git a/src/hotspot/share/memory/arena.cpp b/src/hotspot/share/memory/arena.cpp index 6faffe0d20b..0399c6922e3 100644 --- a/src/hotspot/share/memory/arena.cpp +++ b/src/hotspot/share/memory/arena.cpp @@ -209,7 +209,12 @@ void Chunk::next_chop(Chunk* k) { k->_next = nullptr; } -Arena::Arena(MEMFLAGS flag, Tag tag, size_t init_size) : _flags(flag), _tag(tag), _size_in_bytes(0) { +Arena::Arena(MEMFLAGS flag, Tag tag, size_t init_size) : + _flags(flag), _tag(tag), + _size_in_bytes(0), + _first(nullptr), _chunk(nullptr), + _hwm(nullptr), _max(nullptr) +{ init_size = ARENA_ALIGN(init_size); _chunk = ChunkPool::allocate_chunk(init_size, AllocFailStrategy::EXIT_OOM); _first = _chunk; @@ -219,15 +224,6 @@ Arena::Arena(MEMFLAGS flag, Tag tag, size_t init_size) : _flags(flag), _tag(tag) set_size_in_bytes(init_size); } -Arena::Arena(MEMFLAGS flag, Tag tag) : _flags(flag), _tag(tag), _size_in_bytes(0) { - _chunk = ChunkPool::allocate_chunk(Chunk::init_size, AllocFailStrategy::EXIT_OOM); - _first = _chunk; - _hwm = _chunk->bottom(); // Save the cached hwm, max - _max = _chunk->top(); - MemTracker::record_new_arena(flag); - set_size_in_bytes(Chunk::init_size); -} - Arena::~Arena() { destruct_contents(); MemTracker::record_arena_free(_flags); diff --git a/src/hotspot/share/memory/arena.hpp b/src/hotspot/share/memory/arena.hpp index ed441eca851..1ca8a787825 100644 --- a/src/hotspot/share/memory/arena.hpp +++ b/src/hotspot/share/memory/arena.hpp @@ -94,21 +94,23 @@ public: tag_node // C2 Node arena }; +private: + const MEMFLAGS _flags; // Memory tracking flags + const Tag _tag; + size_t _size_in_bytes; // Size of arena (used for native memory tracking) + protected: friend class HandleMark; friend class NoHandleMark; friend class VMStructs; - MEMFLAGS _flags; // Memory tracking flags - const Tag _tag; - uint32_t _init_size; Chunk* _first; // First chunk Chunk* _chunk; // current chunk char* _hwm; // High water mark char* _max; // and max in current chunk + // Get a new Chunk of at least size x void* grow(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); - size_t _size_in_bytes; // Size of arena (used for native memory tracking) void* internal_amalloc(size_t x, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { assert(is_aligned(x, BytesPerWord), "misaligned size"); @@ -124,8 +126,7 @@ protected: public: // Start the chunk_pool cleaner task static void start_chunk_pool_cleaner_task(); - Arena(MEMFLAGS memflag, Tag tag = Tag::tag_other); - Arena(MEMFLAGS memflag, Tag tag, size_t init_size); + Arena(MEMFLAGS memflag, Tag tag = Tag::tag_other, size_t init_size = Chunk::init_size); ~Arena(); void destruct_contents(); char* hwm() const { return _hwm; } diff --git a/src/hotspot/share/memory/resourceArea.cpp b/src/hotspot/share/memory/resourceArea.cpp index 409460709e4..d5a7513ba19 100644 --- a/src/hotspot/share/memory/resourceArea.cpp +++ b/src/hotspot/share/memory/resourceArea.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,17 +30,6 @@ #include "runtime/javaThread.hpp" #include "utilities/vmError.hpp" -void ResourceArea::bias_to(MEMFLAGS new_flags) { - if (new_flags != _flags) { - size_t size = size_in_bytes(); - MemTracker::record_arena_size_change(-ssize_t(size), _flags); - MemTracker::record_arena_free(_flags); - MemTracker::record_new_arena(new_flags); - MemTracker::record_arena_size_change(ssize_t(size), new_flags); - _flags = new_flags; - } -} - #ifdef ASSERT ResourceMark::ResourceMark(ResourceArea* area, Thread* thread) : diff --git a/src/hotspot/share/memory/resourceArea.hpp b/src/hotspot/share/memory/resourceArea.hpp index ba294e33eff..5fd376068c5 100644 --- a/src/hotspot/share/memory/resourceArea.hpp +++ b/src/hotspot/share/memory/resourceArea.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -60,10 +60,6 @@ public: char* allocate_bytes(size_t size, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM); - // Bias this resource area to specific memory type - // (by default, ResourceArea is tagged as mtThread, per-thread general purpose storage) - void bias_to(MEMFLAGS flags); - DEBUG_ONLY(int nesting() const { return _nesting; }) // Capture the state of a ResourceArea needed by a ResourceMark for @@ -81,7 +77,7 @@ public: _chunk(area->_chunk), _hwm(area->_hwm), _max(area->_max), - _size_in_bytes(area->_size_in_bytes) + _size_in_bytes(area->size_in_bytes()) DEBUG_ONLY(COMMA _nesting(area->_nesting)) {} }; diff --git a/src/hotspot/share/prims/jni.cpp b/src/hotspot/share/prims/jni.cpp index b6a4443a8c7..ae040d66138 100644 --- a/src/hotspot/share/prims/jni.cpp +++ b/src/hotspot/share/prims/jni.cpp @@ -1,6 +1,6 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. - * Copyright (c) 2012 Red Hat, Inc. + * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024 Red Hat, Inc. * Copyright (c) 2021, Azul Systems, Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -3775,7 +3775,7 @@ static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool dae // Create a thread and mark it as attaching so it will be skipped by the // ThreadsListEnumerator - see CR 6404306 - JavaThread* thread = new JavaThread(true); + JavaThread* thread = JavaThread::create_attaching_thread(); // Set correct safepoint info. The thread is going to call into Java when // initializing the Java level thread object. Hence, the correct state must diff --git a/src/hotspot/share/runtime/handles.hpp b/src/hotspot/share/runtime/handles.hpp index 7865d32bef2..39e59cc1ef0 100644 --- a/src/hotspot/share/runtime/handles.hpp +++ b/src/hotspot/share/runtime/handles.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -187,7 +187,7 @@ class HandleArea: public Arena { HandleArea* _prev; // link to outer (older) area public: // Constructor - HandleArea(HandleArea* prev) : Arena(mtThread, Tag::tag_ha, Chunk::tiny_size) { + HandleArea(MEMFLAGS flags, HandleArea* prev) : Arena(flags, Tag::tag_ha, Chunk::tiny_size) { debug_only(_handle_mark_nesting = 0); debug_only(_no_handle_mark_nesting = 0); _prev = prev; diff --git a/src/hotspot/share/runtime/javaThread.cpp b/src/hotspot/share/runtime/javaThread.cpp index 70bb47c213c..a3eef07ba0a 100644 --- a/src/hotspot/share/runtime/javaThread.cpp +++ b/src/hotspot/share/runtime/javaThread.cpp @@ -409,9 +409,9 @@ void JavaThread::check_for_valid_safepoint_state() { // A JavaThread is a normal Java thread -JavaThread::JavaThread() : +JavaThread::JavaThread(MEMFLAGS flags) : + Thread(flags), // Initialize fields - _on_thread_list(false), DEBUG_ONLY(_java_call_counter(0) COMMA) _entry_point(nullptr), @@ -525,13 +525,12 @@ JavaThread::JavaThread() : assert(deferred_card_mark().is_empty(), "Default MemRegion ctor"); } -JavaThread::JavaThread(bool is_attaching_via_jni) : JavaThread() { - if (is_attaching_via_jni) { - _jni_attach_state = _attaching_via_jni; - } +JavaThread* JavaThread::create_attaching_thread() { + JavaThread* jt = new JavaThread(); + jt->_jni_attach_state = _attaching_via_jni; + return jt; } - // interrupt support void JavaThread::interrupt() { @@ -634,8 +633,7 @@ void JavaThread::block_if_vm_exited() { } } -JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz) : JavaThread() { - _jni_attach_state = _not_attaching_via_jni; +JavaThread::JavaThread(ThreadFunction entry_point, size_t stack_sz, MEMFLAGS flags) : JavaThread(flags) { set_entry_point(entry_point); // Create the native thread itself. // %note runtime_23 diff --git a/src/hotspot/share/runtime/javaThread.hpp b/src/hotspot/share/runtime/javaThread.hpp index 2541aaded00..755a8268864 100644 --- a/src/hotspot/share/runtime/javaThread.hpp +++ b/src/hotspot/share/runtime/javaThread.hpp @@ -478,11 +478,13 @@ private: public: // Constructor - JavaThread(); // delegating constructor - JavaThread(bool is_attaching_via_jni); // for main thread and JNI attached threads - JavaThread(ThreadFunction entry_point, size_t stack_size = 0); + JavaThread(MEMFLAGS flags = mtThread); // delegating constructor + JavaThread(ThreadFunction entry_point, size_t stack_size = 0, MEMFLAGS flags = mtThread); ~JavaThread(); + // Factory method to create a new JavaThread whose attach state is "is attaching" + static JavaThread* create_attaching_thread(); + #ifdef ASSERT // verify this JavaThread hasn't be published in the Threads::list yet void verify_not_published(); diff --git a/src/hotspot/share/runtime/javaThread.inline.hpp b/src/hotspot/share/runtime/javaThread.inline.hpp index 7b1ad7e17e1..a51a30ae577 100644 --- a/src/hotspot/share/runtime/javaThread.inline.hpp +++ b/src/hotspot/share/runtime/javaThread.inline.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021, Azul Systems, Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * diff --git a/src/hotspot/share/runtime/thread.cpp b/src/hotspot/share/runtime/thread.cpp index d98fcf6f664..e72077adabf 100644 --- a/src/hotspot/share/runtime/thread.cpp +++ b/src/hotspot/share/runtime/thread.cpp @@ -64,7 +64,7 @@ THREAD_LOCAL Thread* Thread::_thr_current = nullptr; DEBUG_ONLY(Thread* Thread::_starting_thread = nullptr;) -Thread::Thread() { +Thread::Thread(MEMFLAGS flags) { DEBUG_ONLY(_run_state = PRE_CALL_RUN;) @@ -78,9 +78,9 @@ Thread::Thread() { // allocated data structures set_osthread(nullptr); - set_resource_area(new (mtThread)ResourceArea()); + set_resource_area(new (flags) ResourceArea(flags)); DEBUG_ONLY(_current_resource_mark = nullptr;) - set_handle_area(new (mtThread) HandleArea(nullptr)); + set_handle_area(new (flags) HandleArea(flags, nullptr)); set_metadata_handles(new (mtClass) GrowableArray(30, mtClass)); set_last_handle_mark(nullptr); DEBUG_ONLY(_missed_ic_stub_refill_verifier = nullptr); diff --git a/src/hotspot/share/runtime/thread.hpp b/src/hotspot/share/runtime/thread.hpp index 44c845986f3..e9fee4d113a 100644 --- a/src/hotspot/share/runtime/thread.hpp +++ b/src/hotspot/share/runtime/thread.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021, Azul Systems, Inc. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * @@ -277,7 +277,7 @@ class Thread: public ThreadShadow { // is waiting to lock public: // Constructor - Thread(); + Thread(MEMFLAGS flag = mtThread); virtual ~Thread() = 0; // Thread is abstract. // Manage Thread::current()