1 /*
  2  * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_CDS_DUMPALLOCSTATS_HPP
 26 #define SHARE_CDS_DUMPALLOCSTATS_HPP
 27 
 28 #include "classfile/compactHashtable.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "memory/metaspaceClosureType.hpp"
 31 
 32 // This is for dumping detailed statistics for the allocations
 33 // in the shared spaces.
 34 class DumpAllocStats : public StackObj {
 35 public:
 36 
 37 #define DUMPED_OBJ_TYPES_DO(f) \
 38   METASPACE_CLOSURE_TYPES_DO(f) \
 39   f(SymbolHashentry) \
 40   f(SymbolBucket) \
 41   f(StringHashentry) \
 42   f(StringBucket) \
 43   f(CppVTables) \
 44   f(Gap) \
 45   f(Other)
 46 
 47 #define DUMPED_TYPE_DECLARE(name) name ## Type,
 48 #define DUMPED_TYPE_NAME_CASE(name) case name ## Type: return #name;
 49 
 50   enum Type {
 51     // Types are MetaspaceObj::ClassType, MetaspaceObj::SymbolType, etc
 52     DUMPED_OBJ_TYPES_DO(DUMPED_TYPE_DECLARE)
 53     _number_of_types
 54   };
 55 
 56   static const char* type_name(Type type) {
 57     switch(type) {
 58     DUMPED_OBJ_TYPES_DO(DUMPED_TYPE_NAME_CASE)
 59     default:
 60       ShouldNotReachHere();
 61       return nullptr;
 62     }
 63   }
 64 
 65   CompactHashtableStats _symbol_stats;
 66   CompactHashtableStats _string_stats;
 67 
 68   int _counts[2][_number_of_types];
 69   int _bytes [2][_number_of_types];
 70 
 71   int _num_field_cp_entries;
 72   int _num_field_cp_entries_archived;
 73   int _num_field_cp_entries_reverted;
 74   int _num_indy_cp_entries;
 75   int _num_indy_cp_entries_archived;
 76   int _num_indy_cp_entries_reverted;
 77   int _num_klass_cp_entries;
 78   int _num_klass_cp_entries_archived;
 79   int _num_klass_cp_entries_reverted;
 80   int _num_method_cp_entries;
 81   int _num_method_cp_entries_archived;
 82   int _num_method_cp_entries_reverted;
 83 
 84 public:
 85   enum { RO = 0, RW = 1 };
 86 
 87   DumpAllocStats() {
 88     memset(_counts, 0, sizeof(_counts));
 89     memset(_bytes,  0, sizeof(_bytes));
 90     _num_field_cp_entries           = 0;
 91     _num_field_cp_entries_archived  = 0;
 92     _num_field_cp_entries_reverted  = 0;
 93     _num_indy_cp_entries            = 0;
 94     _num_indy_cp_entries_archived   = 0;
 95     _num_indy_cp_entries_reverted   = 0;
 96     _num_klass_cp_entries           = 0;
 97     _num_klass_cp_entries_archived  = 0;
 98     _num_klass_cp_entries_reverted  = 0;
 99     _num_method_cp_entries          = 0;
100     _num_method_cp_entries_archived = 0;
101     _num_method_cp_entries_reverted = 0;
102   };
103 
104   CompactHashtableStats* symbol_stats() { return &_symbol_stats; }
105   CompactHashtableStats* string_stats() { return &_string_stats; }
106 
107   void record(MetaspaceClosureType type, int byte_size, bool read_only) {
108     int t = (int)type;
109     assert(t >= 0 && t < (int)MetaspaceClosureType::_number_of_types, "sanity");
110     int which = (read_only) ? RO : RW;
111     _counts[which][t] ++;
112     _bytes [which][t] += byte_size;
113   }
114 
115   void record_gap(int byte_size) {
116     _counts[RW][GapType] += 1;
117     _bytes [RW][GapType] += byte_size;
118   }
119 
120   void record_other_type(int byte_size, bool read_only) {
121     int which = (read_only) ? RO : RW;
122     _counts[which][OtherType] += 1;
123     _bytes [which][OtherType] += byte_size;
124   }
125 
126   void record_cpp_vtables(int byte_size) {
127     _counts[RW][CppVTablesType] += 1;
128     _bytes[RW][CppVTablesType] += byte_size;
129   }
130 
131   void record_field_cp_entry(bool archived, bool reverted) {
132     _num_field_cp_entries ++;
133     _num_field_cp_entries_archived += archived ? 1 : 0;
134     _num_field_cp_entries_reverted += reverted ? 1 : 0;
135   }
136 
137   void record_indy_cp_entry(bool archived, bool reverted) {
138     _num_indy_cp_entries ++;
139     _num_indy_cp_entries_archived += archived ? 1 : 0;
140     _num_indy_cp_entries_reverted += reverted ? 1 : 0;
141   }
142 
143   void record_klass_cp_entry(bool archived, bool reverted) {
144     _num_klass_cp_entries ++;
145     _num_klass_cp_entries_archived += archived ? 1 : 0;
146     _num_klass_cp_entries_reverted += reverted ? 1 : 0;
147   }
148 
149   void record_method_cp_entry(bool archived, bool reverted) {
150     _num_method_cp_entries ++;
151     _num_method_cp_entries_archived += archived ? 1 : 0;
152     _num_method_cp_entries_reverted += reverted ? 1 : 0;
153   }
154 
155   void print_stats(int ro_all, int rw_all);
156 };
157 
158 #endif // SHARE_CDS_DUMPALLOCSTATS_HPP