Skip to content

Commit

Permalink
8298126: Print statistics for objects in CDS archive heap
Browse files Browse the repository at this point in the history
Reviewed-by: ccheung
  • Loading branch information
iklam committed Dec 11, 2022
1 parent 8ea2a67 commit a37de62
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/hotspot/share/cds/heapShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ bool HeapShared::_disable_writing = false;
DumpedInternedStrings *HeapShared::_dumped_interned_strings = NULL;
GrowableArrayCHeap<Metadata**, mtClassShared>* 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<char>* _archived_ArchiveHeapTestClass = NULL;
Expand Down Expand Up @@ -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<HeapWord*>(obj), cast_from_oop<HeapWord*>(archived_oop), len);
// Reinitialize markword to remove age/marking/locking/etc.
//
Expand Down Expand Up @@ -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());
}

//
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/hotspot/share/cds/heapShared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ class HeapShared: AllStatic {
static DumpedInternedStrings *_dumped_interned_strings;
static GrowableArrayCHeap<Metadata**, mtClassShared>* _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) {
Expand Down

1 comment on commit a37de62

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.