From a37de62d9ddadf1490ee59bd03224e8cea70a75b Mon Sep 17 00:00:00 2001 From: Ioi Lam Date: Sun, 11 Dec 2022 02:58:59 +0000 Subject: [PATCH] 8298126: Print statistics for objects in CDS archive heap Reviewed-by: ccheung --- src/hotspot/share/cds/heapShared.cpp | 55 ++++++++++++++++++++++++++++ src/hotspot/share/cds/heapShared.hpp | 9 +++++ 2 files changed, 64 insertions(+) diff --git a/src/hotspot/share/cds/heapShared.cpp b/src/hotspot/share/cds/heapShared.cpp index 6a37a50f127..9da6f85c766 100644 --- a/src/hotspot/share/cds/heapShared.cpp +++ b/src/hotspot/share/cds/heapShared.cpp @@ -84,6 +84,11 @@ bool HeapShared::_disable_writing = false; DumpedInternedStrings *HeapShared::_dumped_interned_strings = NULL; GrowableArrayCHeap* HeapShared::_native_pointers = NULL; +size_t HeapShared::_alloc_count[HeapShared::ALLOC_STAT_SLOTS]; +size_t HeapShared::_alloc_size[HeapShared::ALLOC_STAT_SLOTS]; +size_t HeapShared::_total_obj_count; +size_t HeapShared::_total_obj_size; + #ifndef PRODUCT #define ARCHIVE_TEST_FIELD_NAME "archivedObjects" static Array* _archived_ArchiveHeapTestClass = NULL; @@ -301,6 +306,7 @@ oop HeapShared::archive_object(oop obj) { oop archived_oop = cast_to_oop(G1CollectedHeap::heap()->archive_mem_allocate(len)); if (archived_oop != NULL) { + count_allocation(len); Copy::aligned_disjoint_words(cast_from_oop(obj), cast_from_oop(archived_oop), len); // Reinitialize markword to remove age/marking/locking/etc. // @@ -586,6 +592,7 @@ void HeapShared::copy_roots() { roots()->obj_at_put(i, _pending_roots->at(i)); } log_info(cds)("archived obj roots[%d] = " SIZE_FORMAT " words, klass = %p, obj = %p", length, size, k, mem); + count_allocation(roots()->size()); } // @@ -831,6 +838,9 @@ void HeapShared::write_subgraph_info_table() { _archived_ArchiveHeapTestClass = array; } #endif + if (log_is_enabled(Info, cds, heap)) { + print_stats(); + } } void HeapShared::serialize_root(SerializeClosure* soc) { @@ -1858,4 +1868,49 @@ ResourceBitMap HeapShared::calculate_ptrmap(MemRegion region) { } } +void HeapShared::count_allocation(size_t size) { + _total_obj_count ++; + _total_obj_size += size; + for (int i = 0; i < ALLOC_STAT_SLOTS; i++) { + if (size <= (size_t(1) << i)) { + _alloc_count[i] ++; + _alloc_size[i] += size; + return; + } + } +} + +static double avg_size(size_t size, size_t count) { + double avg = 0; + if (count > 0) { + avg = double(size * HeapWordSize) / double(count); + } + return avg; +} + +void HeapShared::print_stats() { + size_t huge_count = _total_obj_count; + size_t huge_size = _total_obj_size; + + for (int i = 0; i < ALLOC_STAT_SLOTS; i++) { + size_t byte_size_limit = (size_t(1) << i) * HeapWordSize; + size_t count = _alloc_count[i]; + size_t size = _alloc_size[i]; + log_info(cds, heap)(SIZE_FORMAT_W(8) " objects are <= " SIZE_FORMAT_W(-6) + " bytes (total " SIZE_FORMAT_W(8) " bytes, avg %8.1f bytes)", + count, byte_size_limit, size * HeapWordSize, avg_size(size, count)); + huge_count -= count; + huge_size -= size; + } + + log_info(cds, heap)(SIZE_FORMAT_W(8) " huge objects (total " SIZE_FORMAT_W(8) " bytes" + ", avg %8.1f bytes)", + huge_count, huge_size * HeapWordSize, + avg_size(huge_size, huge_count)); + log_info(cds, heap)(SIZE_FORMAT_W(8) " total objects (total " SIZE_FORMAT_W(8) " bytes" + ", avg %8.1f bytes)", + _total_obj_count, _total_obj_size * HeapWordSize, + avg_size(_total_obj_size, _total_obj_count)); +} + #endif // INCLUDE_CDS_JAVA_HEAP diff --git a/src/hotspot/share/cds/heapShared.hpp b/src/hotspot/share/cds/heapShared.hpp index 9990ab825b3..067897bd821 100644 --- a/src/hotspot/share/cds/heapShared.hpp +++ b/src/hotspot/share/cds/heapShared.hpp @@ -162,6 +162,15 @@ private: static DumpedInternedStrings *_dumped_interned_strings; static GrowableArrayCHeap* _native_pointers; + // statistics + constexpr static int ALLOC_STAT_SLOTS = 16; + static size_t _alloc_count[ALLOC_STAT_SLOTS]; + static size_t _alloc_size[ALLOC_STAT_SLOTS]; + static size_t _total_obj_count; + static size_t _total_obj_size; // in HeapWords + + static void count_allocation(size_t size); + static void print_stats(); public: static unsigned oop_hash(oop const& p); static unsigned string_oop_hash(oop const& string) {