6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "precompiled.hpp"
27 #include "code/codeHeapState.hpp"
28 #include "compiler/compileBroker.hpp"
29 #include "runtime/sweeper.hpp"
30
31 // -------------------------
32 // | General Description |
33 // -------------------------
34 // The CodeHeap state analytics are divided in two parts.
35 // The first part examines the entire CodeHeap and aggregates all
36 // information that is believed useful/important.
37 //
38 // Aggregation condenses the information of a piece of the CodeHeap
39 // (4096 bytes by default) into an analysis granule. These granules
40 // contain enough detail to gain initial insight while keeping the
41 // internal structure sizes in check.
42 //
43 // The second part, which consists of several, independent steps,
44 // prints the previously collected information with emphasis on
45 // various aspects.
56 // The CodeHeap state analytics do have some memory footprint.
57 // The "aggregate" step allocates some data structures to hold the aggregated
58 // information for later output. These data structures live until they are
59 // explicitly discarded (function "discard") or until the VM terminates.
60 // There is one exception: the function "all" does not leave any data
61 // structures allocated.
62 //
63 // Requests for real-time, on-the-fly analysis can be issued via
64 // jcmd <pid> Compiler.CodeHeap_Analytics [<function>] [<granularity>]
65 //
66 // If you are (only) interested in how the CodeHeap looks like after running
67 // a sample workload, you can use the command line option
68 // -XX:+PrintCodeHeapAnalytics
69 // It will cause a full analysis to be written to tty. In addition, a full
70 // analysis will be written the first time a "CodeCache full" condition is
71 // detected.
72 //
73 // The command line option produces output identical to the jcmd function
74 // jcmd <pid> Compiler.CodeHeap_Analytics all 4096
75 // ---------------------------------------------------------------------------------
76
77 // With this declaration macro, it is possible to switch between
78 // - direct output into an argument-passed outputStream and
79 // - buffered output into a bufferedStream with subsequent flush
80 // of the filled buffer to the outputStream.
81 #define USE_STRINGSTREAM
82 #define HEX32_FORMAT "0x%x" // just a helper format string used below multiple times
83 //
84 // Writing to a bufferedStream buffer first has a significant advantage:
85 // It uses noticeably less cpu cycles and reduces (when wirting to a
86 // network file) the required bandwidth by at least a factor of ten.
87 // That clearly makes up for the increased code complexity.
88 #if defined(USE_STRINGSTREAM)
89 #define STRINGSTREAM_DECL(_anyst, _outst) \
90 /* _anyst name of the stream as used in the code */ \
91 /* _outst stream where final output will go to */ \
92 ResourceMark rm; \
93 bufferedStream _sstobj = bufferedStream(4*K); \
94 bufferedStream* _sstbuf = &_sstobj; \
95 outputStream* _outbuf = _outst; \
96 bufferedStream* _anyst = &_sstobj; /* any stream. Use this to just print - no buffer flush. */
97
98 #define STRINGSTREAM_FLUSH(termString) \
99 _sstbuf->print("%s", termString); \
100 _outbuf->print("%s", _sstbuf->as_string()); \
101 _sstbuf->reset();
102
103 #define STRINGSTREAM_FLUSH_LOCKED(termString) \
104 { ttyLocker ttyl;/* keep this output block together */\
105 STRINGSTREAM_FLUSH(termString) \
106 }
107 #else
108 #define STRINGSTREAM_DECL(_anyst, _outst) \
109 outputStream* _outbuf = _outst; \
110 outputStream* _anyst = _outst; /* any stream. Use this to just print - no buffer flush. */
111
112 #define STRINGSTREAM_FLUSH(termString) \
113 _outbuf->print("%s", termString);
114
115 #define STRINGSTREAM_FLUSH_LOCKED(termString) \
116 _outbuf->print("%s", termString);
117 #endif
118
119 const char blobTypeChar[] = {' ', 'C', 'N', 'I', 'X', 'Z', 'U', 'R', '?', 'D', 'T', 'E', 'S', 'A', 'M', 'B', 'L' };
120 const char* blobTypeName[] = {"noType"
121 , "nMethod (under construction)"
122 , "nMethod (active)"
123 , "nMethod (inactive)"
124 , "nMethod (deopt)"
125 , "nMethod (zombie)"
126 , "nMethod (unloaded)"
127 , "runtime stub"
128 , "ricochet stub"
129 , "deopt stub"
130 , "uncommon trap stub"
131 , "exception stub"
132 , "safepoint stub"
133 , "adapter blob"
134 , "MH adapter blob"
135 , "buffer blob"
136 , "lastType"
137 };
444 unsigned int nBlocks_used = 0;
445 unsigned int nBlocks_zomb = 0;
446 unsigned int nBlocks_disconn = 0;
447 unsigned int nBlocks_notentr = 0;
448
449 //---< max & min of TopSizeArray >---
450 // it is sufficient to have these sizes as 32bit unsigned ints.
451 // The CodeHeap is limited in size to 4GB. Furthermore, the sizes
452 // are stored in _segment_size units, scaling them down by a factor of 64 (at least).
453 unsigned int currMax = 0;
454 unsigned int currMin = 0;
455 unsigned int currMin_ix = 0;
456 unsigned long total_iterations = 0;
457
458 bool done = false;
459 const int min_granules = 256;
460 const int max_granules = 512*K; // limits analyzable CodeHeap (with segment_granules) to 32M..128M
461 // results in StatArray size of 24M (= max_granules * 48 Bytes per element)
462 // For a 1GB CodeHeap, the granule size must be at least 2kB to not violate the max_granles limit.
463 const char* heapName = get_heapName(heap);
464 STRINGSTREAM_DECL(ast, out)
465
466 if (!initialization_complete) {
467 memset(CodeHeapStatArray, 0, sizeof(CodeHeapStatArray));
468 initialization_complete = true;
469
470 printBox(ast, '=', "C O D E H E A P A N A L Y S I S (general remarks)", NULL);
471 ast->print_cr(" The code heap analysis function provides deep insights into\n"
472 " the inner workings and the internal state of the Java VM's\n"
473 " code cache - the place where all the JVM generated machine\n"
474 " code is stored.\n"
475 " \n"
476 " This function is designed and provided for support engineers\n"
477 " to help them understand and solve issues in customer systems.\n"
478 " It is not intended for use and interpretation by other persons.\n"
479 " \n");
480 STRINGSTREAM_FLUSH("")
481 }
482 get_HeapStatGlobals(out, heapName);
483
484
485 // Since we are (and must be) analyzing the CodeHeap contents under the CodeCache_lock,
486 // all heap information is "constant" and can be safely extracted/calculated before we
487 // enter the while() loop. Actually, the loop will only be iterated once.
488 char* low_bound = heap->low_boundary();
489 size_t size = heap->capacity();
490 size_t res_size = heap->max_capacity();
491 seg_size = heap->segment_size();
492 log2_seg_size = seg_size == 0 ? 0 : exact_log2(seg_size); // This is a global static value.
493
494 if (seg_size == 0) {
495 printBox(ast, '-', "Heap not fully initialized yet, segment size is zero for segment ", heapName);
496 STRINGSTREAM_FLUSH("")
497 return;
498 }
499
500 if (!CodeCache_lock->owned_by_self()) {
501 printBox(ast, '-', "aggregate function called without holding the CodeCache_lock for ", heapName);
502 STRINGSTREAM_FLUSH("")
503 return;
504 }
505
506 // Calculate granularity of analysis (and output).
507 // The CodeHeap is managed (allocated) in segments (units) of CodeCacheSegmentSize.
508 // The CodeHeap can become fairly large, in particular in productive real-life systems.
509 //
510 // It is often neither feasible nor desirable to aggregate the data with the highest possible
511 // level of detail, i.e. inspecting and printing each segment on its own.
512 //
513 // The granularity parameter allows to specify the level of detail available in the analysis.
514 // It must be a positive multiple of the segment size and should be selected such that enough
515 // detail is provided while, at the same time, the printed output does not explode.
516 //
517 // By manipulating the granularity value, we enforce that at least min_granules units
518 // of analysis are available. We also enforce an upper limit of max_granules units to
519 // keep the amount of allocated storage in check.
520 //
521 // Finally, we adjust the granularity such that each granule covers at most 64k-1 segments.
522 // This is necessary to prevent an unsigned short overflow while accumulating space information.
538 granularity = granularity & (~(seg_size - 1)); // must be multiple of seg_size
539 if (granularity>>log2_seg_size >= (1L<<sizeof(unsigned short)*8)) {
540 granularity = ((1L<<(sizeof(unsigned short)*8))-1)<<log2_seg_size; // Limit: (64k-1) * seg_size
541 }
542 segment_granules = granularity == seg_size;
543 size_t granules = (size + (granularity-1))/granularity;
544
545 printBox(ast, '=', "C O D E H E A P A N A L Y S I S (used blocks) for segment ", heapName);
546 ast->print_cr(" The aggregate step takes an aggregated snapshot of the CodeHeap.\n"
547 " Subsequent print functions create their output based on this snapshot.\n"
548 " The CodeHeap is a living thing, and every effort has been made for the\n"
549 " collected data to be consistent. Only the method names and signatures\n"
550 " are retrieved at print time. That may lead to rare cases where the\n"
551 " name of a method is no longer available, e.g. because it was unloaded.\n");
552 ast->print_cr(" CodeHeap committed size " SIZE_FORMAT "K (" SIZE_FORMAT "M), reserved size " SIZE_FORMAT "K (" SIZE_FORMAT "M), %d%% occupied.",
553 size/(size_t)K, size/(size_t)M, res_size/(size_t)K, res_size/(size_t)M, (unsigned int)(100.0*size/res_size));
554 ast->print_cr(" CodeHeap allocation segment size is " SIZE_FORMAT " bytes. This is the smallest possible granularity.", seg_size);
555 ast->print_cr(" CodeHeap (committed part) is mapped to " SIZE_FORMAT " granules of size " SIZE_FORMAT " bytes.", granules, granularity);
556 ast->print_cr(" Each granule takes " SIZE_FORMAT " bytes of C heap, that is " SIZE_FORMAT "K in total for statistics data.", sizeof(StatElement), (sizeof(StatElement)*granules)/(size_t)K);
557 ast->print_cr(" The number of granules is limited to %dk, requiring a granules size of at least %d bytes for a 1GB heap.", (unsigned int)(max_granules/K), (unsigned int)(G/max_granules));
558 STRINGSTREAM_FLUSH("\n")
559
560
561 while (!done) {
562 //---< reset counters with every aggregation >---
563 nBlocks_t1 = 0;
564 nBlocks_t2 = 0;
565 nBlocks_alive = 0;
566 nBlocks_dead = 0;
567 nBlocks_inconstr = 0;
568 nBlocks_unloaded = 0;
569 nBlocks_stub = 0;
570
571 nBlocks_free = 0;
572 nBlocks_used = 0;
573 nBlocks_zomb = 0;
574 nBlocks_disconn = 0;
575 nBlocks_notentr = 0;
576
577 //---< discard old arrays if size does not match >---
578 if (granules != alloc_granules) {
627 // This is a diagnostic function. It is not supposed to tear down the VM.
628 if ((char*)h < low_bound) {
629 insane = true; ast->print_cr("Sanity check: HeapBlock @%p below low bound (%p)", (char*)h, low_bound);
630 }
631 if ((char*)h > (low_bound + res_size)) {
632 insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside reserved range (%p)", (char*)h, low_bound + res_size);
633 }
634 if ((char*)h > (low_bound + size)) {
635 insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside used range (%p)", (char*)h, low_bound + size);
636 }
637 if (ix_end >= granules) {
638 insane = true; ast->print_cr("Sanity check: end index (%d) out of bounds (" SIZE_FORMAT ")", ix_end, granules);
639 }
640 if (size != heap->capacity()) {
641 insane = true; ast->print_cr("Sanity check: code heap capacity has changed (" SIZE_FORMAT "K to " SIZE_FORMAT "K)", size/(size_t)K, heap->capacity()/(size_t)K);
642 }
643 if (ix_beg > ix_end) {
644 insane = true; ast->print_cr("Sanity check: end index (%d) lower than begin index (%d)", ix_end, ix_beg);
645 }
646 if (insane) {
647 STRINGSTREAM_FLUSH("")
648 continue;
649 }
650
651 if (h->free()) {
652 nBlocks_free++;
653 freeSpace += hb_bytelen;
654 if (hb_bytelen > maxFreeSize) {
655 maxFreeSize = hb_bytelen;
656 maxFreeBlock = h;
657 }
658 } else {
659 update_SizeDistArray(out, hb_len);
660 nBlocks_used++;
661 usedSpace += hb_bytelen;
662 CodeBlob* cb = (CodeBlob*)heap->find_start(h);
663 if (cb != NULL) {
664 cbType = get_cbType(cb);
665 if (cb->is_nmethod()) {
666 compile_id = ((nmethod*)cb)->compile_id();
667 comp_lvl = (CompLevel)((nmethod*)cb)->comp_level();
1016 ast->cr();
1017 ast->print_cr("latest allocated compilation id = %d", latest_compilation_id);
1018 ast->print_cr("highest observed compilation id = %d", highest_compilation_id);
1019 ast->print_cr("Building TopSizeList iterations = %ld", total_iterations);
1020 ast->cr();
1021
1022 int reset_val = NMethodSweeper::hotness_counter_reset_val();
1023 double reverse_free_ratio = (res_size > size) ? (double)res_size/(double)(res_size-size) : (double)res_size;
1024 printBox(ast, '-', "Method hotness information at time of this analysis", NULL);
1025 ast->print_cr("Highest possible method temperature: %12d", reset_val);
1026 ast->print_cr("Threshold for method to be considered 'cold': %12.3f", -reset_val + reverse_free_ratio * NmethodSweepActivity);
1027 if (n_methods > 0) {
1028 avgTemp = hotnessAccumulator/n_methods;
1029 ast->print_cr("min. hotness = %6d", minTemp);
1030 ast->print_cr("avg. hotness = %6d", avgTemp);
1031 ast->print_cr("max. hotness = %6d", maxTemp);
1032 } else {
1033 avgTemp = 0;
1034 ast->print_cr("No hotness data available");
1035 }
1036 STRINGSTREAM_FLUSH("\n")
1037
1038 // This loop is intentionally printing directly to "out".
1039 // It should not print anything, anyway.
1040 out->print("Verifying collected data...");
1041 size_t granule_segs = granule_size>>log2_seg_size;
1042 for (unsigned int ix = 0; ix < granules; ix++) {
1043 if (StatArray[ix].t1_count > granule_segs) {
1044 out->print_cr("t1_count[%d] = %d", ix, StatArray[ix].t1_count);
1045 }
1046 if (StatArray[ix].t2_count > granule_segs) {
1047 out->print_cr("t2_count[%d] = %d", ix, StatArray[ix].t2_count);
1048 }
1049 if (StatArray[ix].tx_count > granule_segs) {
1050 out->print_cr("tx_count[%d] = %d", ix, StatArray[ix].tx_count);
1051 }
1052 if (StatArray[ix].stub_count > granule_segs) {
1053 out->print_cr("stub_count[%d] = %d", ix, StatArray[ix].stub_count);
1054 }
1055 if (StatArray[ix].dead_count > granule_segs) {
1056 out->print_cr("dead_count[%d] = %d", ix, StatArray[ix].dead_count);
1098 }
1099 }
1100 }
1101 out->print_cr("...done\n\n");
1102 } else {
1103 // insane heap state detected. Analysis data incomplete. Just throw it away.
1104 discard_StatArray(out);
1105 discard_TopSizeArray(out);
1106 }
1107 }
1108
1109
1110 done = false;
1111 while (!done && (nBlocks_free > 0)) {
1112
1113 printBox(ast, '=', "C O D E H E A P A N A L Y S I S (free blocks) for segment ", heapName);
1114 ast->print_cr(" The aggregate step collects information about all free blocks in CodeHeap.\n"
1115 " Subsequent print functions create their output based on this snapshot.\n");
1116 ast->print_cr(" Free space in %s is distributed over %d free blocks.", heapName, nBlocks_free);
1117 ast->print_cr(" Each free block takes " SIZE_FORMAT " bytes of C heap for statistics data, that is " SIZE_FORMAT "K in total.", sizeof(FreeBlk), (sizeof(FreeBlk)*nBlocks_free)/K);
1118 STRINGSTREAM_FLUSH("\n")
1119
1120 //----------------------------------------
1121 //-- Prepare the FreeArray of FreeBlks --
1122 //----------------------------------------
1123
1124 //---< discard old array if size does not match >---
1125 if (nBlocks_free != alloc_freeBlocks) {
1126 discard_FreeArray(out);
1127 }
1128
1129 prepare_FreeArray(out, nBlocks_free, heapName);
1130 if (FreeArray == NULL) {
1131 done = true;
1132 continue;
1133 }
1134
1135 //----------------------------------------
1136 //-- Collect all FreeBlks in FreeArray --
1137 //----------------------------------------
1138
1139 unsigned int ix = 0;
1140 FreeBlock* cur = heap->freelist();
1141
1142 while (cur != NULL) {
1143 if (ix < alloc_freeBlocks) { // don't index out of bounds if _freelist has more blocks than anticipated
1144 FreeArray[ix].start = cur;
1145 FreeArray[ix].len = (unsigned int)(cur->length()<<log2_seg_size);
1146 FreeArray[ix].index = ix;
1147 }
1148 cur = cur->link();
1149 ix++;
1150 }
1151 if (ix != alloc_freeBlocks) {
1152 ast->print_cr("Free block count mismatch. Expected %d free blocks, but found %d.", alloc_freeBlocks, ix);
1153 ast->print_cr("I will update the counter and retry data collection");
1154 STRINGSTREAM_FLUSH("\n")
1155 nBlocks_free = ix;
1156 continue;
1157 }
1158 done = true;
1159 }
1160
1161 if (!done || (nBlocks_free == 0)) {
1162 if (nBlocks_free == 0) {
1163 printBox(ast, '-', "no free blocks found in ", heapName);
1164 } else if (!done) {
1165 ast->print_cr("Free block count mismatch could not be resolved.");
1166 ast->print_cr("Try to run \"aggregate\" function to update counters");
1167 }
1168 STRINGSTREAM_FLUSH("")
1169
1170 //---< discard old array and update global values >---
1171 discard_FreeArray(out);
1172 set_HeapStatGlobals(out, heapName);
1173 return;
1174 }
1175
1176 //---< calculate and fill remaining fields >---
1177 if (FreeArray != NULL) {
1178 // This loop is intentionally printing directly to "out".
1179 // It should not print anything, anyway.
1180 for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1181 size_t lenSum = 0;
1182 FreeArray[ix].gap = (unsigned int)((address)FreeArray[ix+1].start - ((address)FreeArray[ix].start + FreeArray[ix].len));
1183 for (HeapBlock *h = heap->next_block(FreeArray[ix].start); (h != NULL) && (h != FreeArray[ix+1].start); h = heap->next_block(h)) {
1184 CodeBlob *cb = (CodeBlob*)(heap->find_start(h));
1185 if ((cb != NULL) && !cb->is_nmethod()) {
1186 FreeArray[ix].stubs_in_gap = true;
1187 }
1188 FreeArray[ix].n_gapBlocks++;
1189 lenSum += h->length()<<log2_seg_size;
1190 if (((address)h < ((address)FreeArray[ix].start+FreeArray[ix].len)) || (h >= FreeArray[ix+1].start)) {
1191 out->print_cr("unsorted occupied CodeHeap block found @ %p, gap interval [%p, %p)", h, (address)FreeArray[ix].start+FreeArray[ix].len, FreeArray[ix+1].start);
1192 }
1193 }
1194 if (lenSum != FreeArray[ix].gap) {
1195 out->print_cr("Length mismatch for gap between FreeBlk[%d] and FreeBlk[%d]. Calculated: %d, accumulated: %d.", ix, ix+1, FreeArray[ix].gap, (unsigned int)lenSum);
1196 }
1197 }
1198 }
1199 set_HeapStatGlobals(out, heapName);
1200
1201 printBox(ast, '=', "C O D E H E A P A N A L Y S I S C O M P L E T E for segment ", heapName);
1202 STRINGSTREAM_FLUSH("\n")
1203 }
1204
1205
1206 void CodeHeapState::print_usedSpace(outputStream* out, CodeHeap* heap) {
1207 if (!initialization_complete) {
1208 return;
1209 }
1210
1211 const char* heapName = get_heapName(heap);
1212 get_HeapStatGlobals(out, heapName);
1213
1214 if ((StatArray == NULL) || (TopSizeArray == NULL) || (used_topSizeBlocks == 0)) {
1215 return;
1216 }
1217 STRINGSTREAM_DECL(ast, out)
1218
1219 {
1220 printBox(ast, '=', "U S E D S P A C E S T A T I S T I C S for ", heapName);
1221 ast->print_cr("Note: The Top%d list of the largest used blocks associates method names\n"
1222 " and other identifying information with the block size data.\n"
1223 "\n"
1224 " Method names are dynamically retrieved from the code cache at print time.\n"
1225 " Due to the living nature of the code cache and because the CodeCache_lock\n"
1226 " is not continuously held, the displayed name might be wrong or no name\n"
1227 " might be found at all. The likelihood for that to happen increases\n"
1228 " over time passed between analysis and print step.\n", used_topSizeBlocks);
1229 STRINGSTREAM_FLUSH_LOCKED("\n")
1230 }
1231
1232 //----------------------------
1233 //-- Print Top Used Blocks --
1234 //----------------------------
1235 {
1236 char* low_bound = heap->low_boundary();
1237 bool have_CodeCache_lock = CodeCache_lock->owned_by_self();
1238
1239 printBox(ast, '-', "Largest Used Blocks in ", heapName);
1240 print_blobType_legend(ast);
1241
1242 ast->fill_to(51);
1243 ast->print("%4s", "blob");
1244 ast->fill_to(56);
1245 ast->print("%9s", "compiler");
1246 ast->fill_to(66);
1247 ast->print_cr("%6s", "method");
1248 ast->print_cr("%18s %13s %17s %4s %9s %5s %s", "Addr(module) ", "offset", "size", "type", " type lvl", " temp", "Name");
1249 STRINGSTREAM_FLUSH_LOCKED("")
1250
1251 //---< print Top Ten Used Blocks >---
1252 if (used_topSizeBlocks > 0) {
1253 unsigned int printed_topSizeBlocks = 0;
1254 for (unsigned int i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
1255 printed_topSizeBlocks++;
1256 nmethod* nm = NULL;
1257 const char* blob_name = "unnamed blob or blob name unavailable";
1258 // heap->find_start() is safe. Only works on _segmap.
1259 // Returns NULL or void*. Returned CodeBlob may be uninitialized.
1260 HeapBlock* heapBlock = TopSizeArray[i].start;
1261 CodeBlob* this_blob = (CodeBlob*)(heap->find_start(heapBlock));
1262 bool blob_is_safe = blob_access_is_safe(this_blob, NULL);
1263 if (blob_is_safe) {
1264 //---< access these fields only if we own the CodeCache_lock >---
1265 if (have_CodeCache_lock) {
1266 blob_name = this_blob->name();
1267 nm = this_blob->as_nmethod_or_null();
1268 }
1269 //---< blob address >---
1307 ast->print("%5d", nm->hotness_counter());
1308 //---< name and signature >---
1309 ast->fill_to(67+6);
1310 if (nm->is_not_installed()) {
1311 ast->print(" not (yet) installed method ");
1312 }
1313 if (nm->is_zombie()) {
1314 ast->print(" zombie method ");
1315 }
1316 ast->print("%s", blob_name);
1317 } else {
1318 //---< block size in hex >---
1319 ast->print(PTR32_FORMAT, (unsigned int)(TopSizeArray[i].len<<log2_seg_size));
1320 ast->print("(" SIZE_FORMAT_W(4) "K)", (TopSizeArray[i].len<<log2_seg_size)/K);
1321 //---< no compiler information >---
1322 ast->fill_to(56);
1323 //---< name and signature >---
1324 ast->fill_to(67+6);
1325 ast->print("%s", blob_name);
1326 }
1327 STRINGSTREAM_FLUSH_LOCKED("\n")
1328 }
1329 if (used_topSizeBlocks != printed_topSizeBlocks) {
1330 ast->print_cr("used blocks: %d, printed blocks: %d", used_topSizeBlocks, printed_topSizeBlocks);
1331 STRINGSTREAM_FLUSH("")
1332 for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1333 ast->print_cr(" TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1334 STRINGSTREAM_FLUSH("")
1335 }
1336 }
1337 STRINGSTREAM_FLUSH_LOCKED("\n\n")
1338 }
1339 }
1340
1341 //-----------------------------
1342 //-- Print Usage Histogram --
1343 //-----------------------------
1344
1345 if (SizeDistributionArray != NULL) {
1346 unsigned long total_count = 0;
1347 unsigned long total_size = 0;
1348 const unsigned long pctFactor = 200;
1349
1350 for (unsigned int i = 0; i < nSizeDistElements; i++) {
1351 total_count += SizeDistributionArray[i].count;
1352 total_size += SizeDistributionArray[i].lenSum;
1353 }
1354
1355 if ((total_count > 0) && (total_size > 0)) {
1356 printBox(ast, '-', "Block count histogram for ", heapName);
1357 ast->print_cr("Note: The histogram indicates how many blocks (as a percentage\n"
1358 " of all blocks) have a size in the given range.\n"
1359 " %ld characters are printed per percentage point.\n", pctFactor/100);
1360 ast->print_cr("total size of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1361 ast->print_cr("total number of all blocks: %7ld\n", total_count);
1362 STRINGSTREAM_FLUSH_LOCKED("")
1363
1364 ast->print_cr("[Size Range)------avg.-size-+----count-+");
1365 for (unsigned int i = 0; i < nSizeDistElements; i++) {
1366 if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1367 ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1368 ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1369 ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1370 );
1371 } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1372 ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1373 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1374 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1375 );
1376 } else {
1377 ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1378 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1379 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1380 );
1381 }
1382 ast->print(" %8d | %8d |",
1383 SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1384 SizeDistributionArray[i].count);
1385
1386 unsigned int percent = pctFactor*SizeDistributionArray[i].count/total_count;
1387 for (unsigned int j = 1; j <= percent; j++) {
1388 ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1389 }
1390 ast->cr();
1391 }
1392 ast->print_cr("----------------------------+----------+\n\n");
1393 STRINGSTREAM_FLUSH_LOCKED("\n")
1394
1395 printBox(ast, '-', "Contribution per size range to total size for ", heapName);
1396 ast->print_cr("Note: The histogram indicates how much space (as a percentage of all\n"
1397 " occupied space) is used by the blocks in the given size range.\n"
1398 " %ld characters are printed per percentage point.\n", pctFactor/100);
1399 ast->print_cr("total size of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1400 ast->print_cr("total number of all blocks: %7ld\n", total_count);
1401 STRINGSTREAM_FLUSH_LOCKED("")
1402
1403 ast->print_cr("[Size Range)------avg.-size-+----count-+");
1404 for (unsigned int i = 0; i < nSizeDistElements; i++) {
1405 if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1406 ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1407 ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1408 ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1409 );
1410 } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1411 ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1412 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1413 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1414 );
1415 } else {
1416 ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1417 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1418 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1419 );
1420 }
1421 ast->print(" %8d | %8d |",
1422 SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1423 SizeDistributionArray[i].count);
1424
1425 unsigned int percent = pctFactor*(unsigned long)SizeDistributionArray[i].lenSum/total_size;
1426 for (unsigned int j = 1; j <= percent; j++) {
1427 ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1428 }
1429 ast->cr();
1430 }
1431 ast->print_cr("----------------------------+----------+");
1432 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1433 }
1434 }
1435 }
1436
1437
1438 void CodeHeapState::print_freeSpace(outputStream* out, CodeHeap* heap) {
1439 if (!initialization_complete) {
1440 return;
1441 }
1442
1443 const char* heapName = get_heapName(heap);
1444 get_HeapStatGlobals(out, heapName);
1445
1446 if ((StatArray == NULL) || (FreeArray == NULL) || (alloc_granules == 0)) {
1447 return;
1448 }
1449 STRINGSTREAM_DECL(ast, out)
1450
1451 {
1452 printBox(ast, '=', "F R E E S P A C E S T A T I S T I C S for ", heapName);
1453 ast->print_cr("Note: in this context, a gap is the occupied space between two free blocks.\n"
1454 " Those gaps are of interest if there is a chance that they become\n"
1455 " unoccupied, e.g. by class unloading. Then, the two adjacent free\n"
1456 " blocks, together with the now unoccupied space, form a new, large\n"
1457 " free block.");
1458 STRINGSTREAM_FLUSH_LOCKED("\n")
1459 }
1460
1461 {
1462 printBox(ast, '-', "List of all Free Blocks in ", heapName);
1463 STRINGSTREAM_FLUSH_LOCKED("")
1464
1465 unsigned int ix = 0;
1466 for (ix = 0; ix < alloc_freeBlocks-1; ix++) {
1467 ast->print(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT ",", p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1468 ast->fill_to(38);
1469 ast->print("Gap[%4d..%4d]: " HEX32_FORMAT " bytes,", ix, ix+1, FreeArray[ix].gap);
1470 ast->fill_to(71);
1471 ast->print("block count: %6d", FreeArray[ix].n_gapBlocks);
1472 if (FreeArray[ix].stubs_in_gap) {
1473 ast->print(" !! permanent gap, contains stubs and/or blobs !!");
1474 }
1475 STRINGSTREAM_FLUSH_LOCKED("\n")
1476 }
1477 ast->print_cr(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT, p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1478 STRINGSTREAM_FLUSH_LOCKED("\n\n")
1479 }
1480
1481
1482 //-----------------------------------------
1483 //-- Find and Print Top Ten Free Blocks --
1484 //-----------------------------------------
1485
1486 //---< find Top Ten Free Blocks >---
1487 const unsigned int nTop = 10;
1488 unsigned int currMax10 = 0;
1489 struct FreeBlk* FreeTopTen[nTop];
1490 memset(FreeTopTen, 0, sizeof(FreeTopTen));
1491
1492 for (unsigned int ix = 0; ix < alloc_freeBlocks; ix++) {
1493 if (FreeArray[ix].len > currMax10) { // larger than the ten largest found so far
1494 unsigned int currSize = FreeArray[ix].len;
1495
1496 unsigned int iy;
1497 for (iy = 0; iy < nTop && FreeTopTen[iy] != NULL; iy++) {
1498 if (FreeTopTen[iy]->len < currSize) {
1502 FreeTopTen[iy] = &FreeArray[ix]; // insert new free block
1503 if (FreeTopTen[nTop-1] != NULL) {
1504 currMax10 = FreeTopTen[nTop-1]->len;
1505 }
1506 break; // done with this, check next free block
1507 }
1508 }
1509 if (iy >= nTop) {
1510 ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1511 currSize, currMax10);
1512 continue;
1513 }
1514 if (FreeTopTen[iy] == NULL) {
1515 FreeTopTen[iy] = &FreeArray[ix];
1516 if (iy == (nTop-1)) {
1517 currMax10 = currSize;
1518 }
1519 }
1520 }
1521 }
1522 STRINGSTREAM_FLUSH_LOCKED("")
1523
1524 {
1525 printBox(ast, '-', "Top Ten Free Blocks in ", heapName);
1526
1527 //---< print Top Ten Free Blocks >---
1528 for (unsigned int iy = 0; (iy < nTop) && (FreeTopTen[iy] != NULL); iy++) {
1529 ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTen[iy]->index, FreeTopTen[iy]->len);
1530 ast->fill_to(39);
1531 if (FreeTopTen[iy]->index == (alloc_freeBlocks-1)) {
1532 ast->print("last free block in list.");
1533 } else {
1534 ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTen[iy]->gap);
1535 ast->fill_to(63);
1536 ast->print("#blocks (in gap) %d", FreeTopTen[iy]->n_gapBlocks);
1537 }
1538 ast->cr();
1539 }
1540 STRINGSTREAM_FLUSH_LOCKED("\n\n")
1541 }
1542
1543
1544 //--------------------------------------------------------
1545 //-- Find and Print Top Ten Free-Occupied-Free Triples --
1546 //--------------------------------------------------------
1547
1548 //---< find and print Top Ten Triples (Free-Occupied-Free) >---
1549 currMax10 = 0;
1550 struct FreeBlk *FreeTopTenTriple[nTop];
1551 memset(FreeTopTenTriple, 0, sizeof(FreeTopTenTriple));
1552
1553 for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1554 // If there are stubs in the gap, this gap will never become completely free.
1555 // The triple will thus never merge to one free block.
1556 unsigned int lenTriple = FreeArray[ix].len + (FreeArray[ix].stubs_in_gap ? 0 : FreeArray[ix].gap + FreeArray[ix+1].len);
1557 FreeArray[ix].len = lenTriple;
1558 if (lenTriple > currMax10) { // larger than the ten largest found so far
1559
1560 unsigned int iy;
1561 for (iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1566 FreeTopTenTriple[iy] = &FreeArray[ix];
1567 if (FreeTopTenTriple[nTop-1] != NULL) {
1568 currMax10 = FreeTopTenTriple[nTop-1]->len;
1569 }
1570 break;
1571 }
1572 }
1573 if (iy == nTop) {
1574 ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1575 lenTriple, currMax10);
1576 continue;
1577 }
1578 if (FreeTopTenTriple[iy] == NULL) {
1579 FreeTopTenTriple[iy] = &FreeArray[ix];
1580 if (iy == (nTop-1)) {
1581 currMax10 = lenTriple;
1582 }
1583 }
1584 }
1585 }
1586 STRINGSTREAM_FLUSH_LOCKED("")
1587
1588 {
1589 printBox(ast, '-', "Top Ten Free-Occupied-Free Triples in ", heapName);
1590 ast->print_cr(" Use this information to judge how likely it is that a large(r) free block\n"
1591 " might get created by code cache sweeping.\n"
1592 " If all the occupied blocks can be swept, the three free blocks will be\n"
1593 " merged into one (much larger) free block. That would reduce free space\n"
1594 " fragmentation.\n");
1595
1596 //---< print Top Ten Free-Occupied-Free Triples >---
1597 for (unsigned int iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1598 ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTenTriple[iy]->index, FreeTopTenTriple[iy]->len);
1599 ast->fill_to(39);
1600 ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTenTriple[iy]->gap);
1601 ast->fill_to(63);
1602 ast->print("#blocks (in gap) %d", FreeTopTenTriple[iy]->n_gapBlocks);
1603 ast->cr();
1604 }
1605 STRINGSTREAM_FLUSH_LOCKED("\n\n")
1606 }
1607 }
1608
1609
1610 void CodeHeapState::print_count(outputStream* out, CodeHeap* heap) {
1611 if (!initialization_complete) {
1612 return;
1613 }
1614
1615 const char* heapName = get_heapName(heap);
1616 get_HeapStatGlobals(out, heapName);
1617
1618 if ((StatArray == NULL) || (alloc_granules == 0)) {
1619 return;
1620 }
1621 STRINGSTREAM_DECL(ast, out)
1622
1623 unsigned int granules_per_line = 32;
1624 char* low_bound = heap->low_boundary();
1625
1626 {
1627 printBox(ast, '=', "B L O C K C O U N T S for ", heapName);
1628 ast->print_cr(" Each granule contains an individual number of heap blocks. Large blocks\n"
1629 " may span multiple granules and are counted for each granule they touch.\n");
1630 if (segment_granules) {
1631 ast->print_cr(" You have selected granule size to be as small as segment size.\n"
1632 " As a result, each granule contains exactly one block (or a part of one block)\n"
1633 " or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1634 " Occupied granules show their BlobType character, see legend.\n");
1635 print_blobType_legend(ast);
1636 }
1637 STRINGSTREAM_FLUSH_LOCKED("")
1638 }
1639
1640 {
1641 if (segment_granules) {
1642 printBox(ast, '-', "Total (all types) count for granule size == segment size", NULL);
1643 STRINGSTREAM_FLUSH_LOCKED("")
1644
1645 granules_per_line = 128;
1646 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1647 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1648 print_blobType_single(ast, StatArray[ix].type);
1649 }
1650 } else {
1651 printBox(ast, '-', "Total (all tiers) count, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1652 STRINGSTREAM_FLUSH_LOCKED("")
1653
1654 granules_per_line = 128;
1655 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1656 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1657 unsigned int count = StatArray[ix].t1_count + StatArray[ix].t2_count + StatArray[ix].tx_count
1658 + StatArray[ix].stub_count + StatArray[ix].dead_count;
1659 print_count_single(ast, count);
1660 }
1661 }
1662 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1663 }
1664
1665 {
1666 if (nBlocks_t1 > 0) {
1667 printBox(ast, '-', "Tier1 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1668 STRINGSTREAM_FLUSH_LOCKED("")
1669
1670 granules_per_line = 128;
1671 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1672 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1673 if (segment_granules && StatArray[ix].t1_count > 0) {
1674 print_blobType_single(ast, StatArray[ix].type);
1675 } else {
1676 print_count_single(ast, StatArray[ix].t1_count);
1677 }
1678 }
1679 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1680 } else {
1681 ast->print("No Tier1 nMethods found in CodeHeap.");
1682 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1683 }
1684 }
1685
1686 {
1687 if (nBlocks_t2 > 0) {
1688 printBox(ast, '-', "Tier2 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1689 STRINGSTREAM_FLUSH_LOCKED("")
1690
1691 granules_per_line = 128;
1692 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1693 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1694 if (segment_granules && StatArray[ix].t2_count > 0) {
1695 print_blobType_single(ast, StatArray[ix].type);
1696 } else {
1697 print_count_single(ast, StatArray[ix].t2_count);
1698 }
1699 }
1700 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1701 } else {
1702 ast->print("No Tier2 nMethods found in CodeHeap.");
1703 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1704 }
1705 }
1706
1707 {
1708 if (nBlocks_alive > 0) {
1709 printBox(ast, '-', "not_used/not_entrant/not_installed nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1710 STRINGSTREAM_FLUSH_LOCKED("")
1711
1712 granules_per_line = 128;
1713 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1714 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1715 if (segment_granules && StatArray[ix].tx_count > 0) {
1716 print_blobType_single(ast, StatArray[ix].type);
1717 } else {
1718 print_count_single(ast, StatArray[ix].tx_count);
1719 }
1720 }
1721 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1722 } else {
1723 ast->print("No not_used/not_entrant nMethods found in CodeHeap.");
1724 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1725 }
1726 }
1727
1728 {
1729 if (nBlocks_stub > 0) {
1730 printBox(ast, '-', "Stub & Blob count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1731 STRINGSTREAM_FLUSH_LOCKED("")
1732
1733 granules_per_line = 128;
1734 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1735 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1736 if (segment_granules && StatArray[ix].stub_count > 0) {
1737 print_blobType_single(ast, StatArray[ix].type);
1738 } else {
1739 print_count_single(ast, StatArray[ix].stub_count);
1740 }
1741 }
1742 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1743 } else {
1744 ast->print("No Stubs and Blobs found in CodeHeap.");
1745 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1746 }
1747 }
1748
1749 {
1750 if (nBlocks_dead > 0) {
1751 printBox(ast, '-', "Dead nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1752 STRINGSTREAM_FLUSH_LOCKED("")
1753
1754 granules_per_line = 128;
1755 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1756 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1757 if (segment_granules && StatArray[ix].dead_count > 0) {
1758 print_blobType_single(ast, StatArray[ix].type);
1759 } else {
1760 print_count_single(ast, StatArray[ix].dead_count);
1761 }
1762 }
1763 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1764 } else {
1765 ast->print("No dead nMethods found in CodeHeap.");
1766 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1767 }
1768 }
1769
1770 {
1771 if (!segment_granules) { // Prevent totally redundant printouts
1772 printBox(ast, '-', "Count by tier (combined, no dead blocks): <#t1>:<#t2>:<#s>, 0x0..0xf. '*' indicates >= 16 blocks", NULL);
1773 STRINGSTREAM_FLUSH_LOCKED("")
1774
1775 granules_per_line = 24;
1776 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1777 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1778
1779 print_count_single(ast, StatArray[ix].t1_count);
1780 ast->print(":");
1781 print_count_single(ast, StatArray[ix].t2_count);
1782 ast->print(":");
1783 if (segment_granules && StatArray[ix].stub_count > 0) {
1784 print_blobType_single(ast, StatArray[ix].type);
1785 } else {
1786 print_count_single(ast, StatArray[ix].stub_count);
1787 }
1788 ast->print(" ");
1789 }
1790 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1791 }
1792 }
1793 }
1794
1795
1796 void CodeHeapState::print_space(outputStream* out, CodeHeap* heap) {
1797 if (!initialization_complete) {
1798 return;
1799 }
1800
1801 const char* heapName = get_heapName(heap);
1802 get_HeapStatGlobals(out, heapName);
1803
1804 if ((StatArray == NULL) || (alloc_granules == 0)) {
1805 return;
1806 }
1807 STRINGSTREAM_DECL(ast, out)
1808
1809 unsigned int granules_per_line = 32;
1810 char* low_bound = heap->low_boundary();
1811
1812 {
1813 printBox(ast, '=', "S P A C E U S A G E & F R A G M E N T A T I O N for ", heapName);
1814 ast->print_cr(" The heap space covered by one granule is occupied to a various extend.\n"
1815 " The granule occupancy is displayed by one decimal digit per granule.\n");
1816 if (segment_granules) {
1817 ast->print_cr(" You have selected granule size to be as small as segment size.\n"
1818 " As a result, each granule contains exactly one block (or a part of one block)\n"
1819 " or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1820 " Occupied granules show their BlobType character, see legend.\n");
1821 print_blobType_legend(ast);
1822 } else {
1823 ast->print_cr(" These digits represent a fill percentage range (see legend).\n");
1824 print_space_legend(ast);
1825 }
1826 STRINGSTREAM_FLUSH_LOCKED("")
1827 }
1828
1829 {
1830 if (segment_granules) {
1831 printBox(ast, '-', "Total (all types) space consumption for granule size == segment size", NULL);
1832 STRINGSTREAM_FLUSH_LOCKED("")
1833
1834 granules_per_line = 128;
1835 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1836 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1837 print_blobType_single(ast, StatArray[ix].type);
1838 }
1839 } else {
1840 printBox(ast, '-', "Total (all types) space consumption. ' ' indicates empty, '*' indicates full.", NULL);
1841 STRINGSTREAM_FLUSH_LOCKED("")
1842
1843 granules_per_line = 128;
1844 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1845 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1846 unsigned int space = StatArray[ix].t1_space + StatArray[ix].t2_space + StatArray[ix].tx_space
1847 + StatArray[ix].stub_space + StatArray[ix].dead_space;
1848 print_space_single(ast, space);
1849 }
1850 }
1851 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1852 }
1853
1854 {
1855 if (nBlocks_t1 > 0) {
1856 printBox(ast, '-', "Tier1 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1857 STRINGSTREAM_FLUSH_LOCKED("")
1858
1859 granules_per_line = 128;
1860 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1861 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1862 if (segment_granules && StatArray[ix].t1_space > 0) {
1863 print_blobType_single(ast, StatArray[ix].type);
1864 } else {
1865 print_space_single(ast, StatArray[ix].t1_space);
1866 }
1867 }
1868 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1869 } else {
1870 ast->print("No Tier1 nMethods found in CodeHeap.");
1871 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1872 }
1873 }
1874
1875 {
1876 if (nBlocks_t2 > 0) {
1877 printBox(ast, '-', "Tier2 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1878 STRINGSTREAM_FLUSH_LOCKED("")
1879
1880 granules_per_line = 128;
1881 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1882 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1883 if (segment_granules && StatArray[ix].t2_space > 0) {
1884 print_blobType_single(ast, StatArray[ix].type);
1885 } else {
1886 print_space_single(ast, StatArray[ix].t2_space);
1887 }
1888 }
1889 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1890 } else {
1891 ast->print("No Tier2 nMethods found in CodeHeap.");
1892 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1893 }
1894 }
1895
1896 {
1897 if (nBlocks_alive > 0) {
1898 printBox(ast, '-', "not_used/not_entrant/not_installed space consumption. ' ' indicates empty, '*' indicates full", NULL);
1899
1900 granules_per_line = 128;
1901 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1902 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1903 if (segment_granules && StatArray[ix].tx_space > 0) {
1904 print_blobType_single(ast, StatArray[ix].type);
1905 } else {
1906 print_space_single(ast, StatArray[ix].tx_space);
1907 }
1908 }
1909 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1910 } else {
1911 ast->print("No Tier2 nMethods found in CodeHeap.");
1912 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1913 }
1914 }
1915
1916 {
1917 if (nBlocks_stub > 0) {
1918 printBox(ast, '-', "Stub and Blob space consumption. ' ' indicates empty, '*' indicates full", NULL);
1919 STRINGSTREAM_FLUSH_LOCKED("")
1920
1921 granules_per_line = 128;
1922 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1923 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1924 if (segment_granules && StatArray[ix].stub_space > 0) {
1925 print_blobType_single(ast, StatArray[ix].type);
1926 } else {
1927 print_space_single(ast, StatArray[ix].stub_space);
1928 }
1929 }
1930 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1931 } else {
1932 ast->print("No Stubs and Blobs found in CodeHeap.");
1933 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1934 }
1935 }
1936
1937 {
1938 if (nBlocks_dead > 0) {
1939 printBox(ast, '-', "Dead space consumption. ' ' indicates empty, '*' indicates full", NULL);
1940 STRINGSTREAM_FLUSH_LOCKED("")
1941
1942 granules_per_line = 128;
1943 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1944 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1945 print_space_single(ast, StatArray[ix].dead_space);
1946 }
1947 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1948 } else {
1949 ast->print("No dead nMethods found in CodeHeap.");
1950 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
1951 }
1952 }
1953
1954 {
1955 if (!segment_granules) { // Prevent totally redundant printouts
1956 printBox(ast, '-', "Space consumption by tier (combined): <t1%>:<t2%>:<s%>. ' ' indicates empty, '*' indicates full", NULL);
1957 STRINGSTREAM_FLUSH_LOCKED("")
1958
1959 granules_per_line = 24;
1960 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1961 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1962
1963 if (segment_granules && StatArray[ix].t1_space > 0) {
1964 print_blobType_single(ast, StatArray[ix].type);
1965 } else {
1966 print_space_single(ast, StatArray[ix].t1_space);
1967 }
1968 ast->print(":");
1969 if (segment_granules && StatArray[ix].t2_space > 0) {
1970 print_blobType_single(ast, StatArray[ix].type);
1971 } else {
1972 print_space_single(ast, StatArray[ix].t2_space);
1973 }
1974 ast->print(":");
1975 if (segment_granules && StatArray[ix].stub_space > 0) {
1976 print_blobType_single(ast, StatArray[ix].type);
1977 } else {
1978 print_space_single(ast, StatArray[ix].stub_space);
1979 }
1980 ast->print(" ");
1981 }
1982 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
1983 }
1984 }
1985 }
1986
1987 void CodeHeapState::print_age(outputStream* out, CodeHeap* heap) {
1988 if (!initialization_complete) {
1989 return;
1990 }
1991
1992 const char* heapName = get_heapName(heap);
1993 get_HeapStatGlobals(out, heapName);
1994
1995 if ((StatArray == NULL) || (alloc_granules == 0)) {
1996 return;
1997 }
1998 STRINGSTREAM_DECL(ast, out)
1999
2000 unsigned int granules_per_line = 32;
2001 char* low_bound = heap->low_boundary();
2002
2003 {
2004 printBox(ast, '=', "M E T H O D A G E by CompileID for ", heapName);
2005 ast->print_cr(" The age of a compiled method in the CodeHeap is not available as a\n"
2006 " time stamp. Instead, a relative age is deducted from the method's compilation ID.\n"
2007 " Age information is available for tier1 and tier2 methods only. There is no\n"
2008 " age information for stubs and blobs, because they have no compilation ID assigned.\n"
2009 " Information for the youngest method (highest ID) in the granule is printed.\n"
2010 " Refer to the legend to learn how method age is mapped to the displayed digit.");
2011 print_age_legend(ast);
2012 STRINGSTREAM_FLUSH_LOCKED("")
2013 }
2014
2015 {
2016 printBox(ast, '-', "Age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2017 STRINGSTREAM_FLUSH_LOCKED("")
2018
2019 granules_per_line = 128;
2020 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2021 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2022 unsigned int age1 = StatArray[ix].t1_age;
2023 unsigned int age2 = StatArray[ix].t2_age;
2024 unsigned int agex = StatArray[ix].tx_age;
2025 unsigned int age = age1 > age2 ? age1 : age2;
2026 age = age > agex ? age : agex;
2027 print_age_single(ast, age);
2028 }
2029 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2030 }
2031
2032 {
2033 if (nBlocks_t1 > 0) {
2034 printBox(ast, '-', "Tier1 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2035 STRINGSTREAM_FLUSH_LOCKED("")
2036
2037 granules_per_line = 128;
2038 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2039 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2040 print_age_single(ast, StatArray[ix].t1_age);
2041 }
2042 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2043 } else {
2044 ast->print("No Tier1 nMethods found in CodeHeap.");
2045 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2046 }
2047 }
2048
2049 {
2050 if (nBlocks_t2 > 0) {
2051 printBox(ast, '-', "Tier2 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2052 STRINGSTREAM_FLUSH_LOCKED("")
2053
2054 granules_per_line = 128;
2055 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2056 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2057 print_age_single(ast, StatArray[ix].t2_age);
2058 }
2059 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2060 } else {
2061 ast->print("No Tier2 nMethods found in CodeHeap.");
2062 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2063 }
2064 }
2065
2066 {
2067 if (nBlocks_alive > 0) {
2068 printBox(ast, '-', "not_used/not_entrant/not_installed age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2069 STRINGSTREAM_FLUSH_LOCKED("")
2070
2071 granules_per_line = 128;
2072 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2073 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2074 print_age_single(ast, StatArray[ix].tx_age);
2075 }
2076 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2077 } else {
2078 ast->print("No Tier2 nMethods found in CodeHeap.");
2079 STRINGSTREAM_FLUSH_LOCKED("\n\n\n")
2080 }
2081 }
2082
2083 {
2084 if (!segment_granules) { // Prevent totally redundant printouts
2085 printBox(ast, '-', "age distribution by tier <a1>:<a2>. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2086 STRINGSTREAM_FLUSH_LOCKED("")
2087
2088 granules_per_line = 32;
2089 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2090 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2091 print_age_single(ast, StatArray[ix].t1_age);
2092 ast->print(":");
2093 print_age_single(ast, StatArray[ix].t2_age);
2094 ast->print(" ");
2095 }
2096 STRINGSTREAM_FLUSH_LOCKED("|\n\n\n")
2097 }
2098 }
2099 }
2100
2101
2102 void CodeHeapState::print_names(outputStream* out, CodeHeap* heap) {
2103 if (!initialization_complete) {
2104 return;
2105 }
2106
2107 const char* heapName = get_heapName(heap);
2108 get_HeapStatGlobals(out, heapName);
2109
2110 if ((StatArray == NULL) || (alloc_granules == 0)) {
2111 return;
2112 }
2113 STRINGSTREAM_DECL(ast, out)
2114
2115 unsigned int granules_per_line = 128;
2116 char* low_bound = heap->low_boundary();
2117 CodeBlob* last_blob = NULL;
2118 bool name_in_addr_range = true;
2119 bool have_CodeCache_lock = CodeCache_lock->owned_by_self();
2120
2121 //---< print at least 128K per block (i.e. between headers) >---
2122 if (granules_per_line*granule_size < 128*K) {
2123 granules_per_line = (unsigned int)((128*K)/granule_size);
2124 }
2125
2126 printBox(ast, '=', "M E T H O D N A M E S for ", heapName);
2127 ast->print_cr(" Method names are dynamically retrieved from the code cache at print time.\n"
2128 " Due to the living nature of the code heap and because the CodeCache_lock\n"
2129 " is not continuously held, the displayed name might be wrong or no name\n"
2130 " might be found at all. The likelihood for that to happen increases\n"
2131 " over time passed between aggregtion and print steps.\n");
2132 STRINGSTREAM_FLUSH_LOCKED("")
2133
2134 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2135 //---< print a new blob on a new line >---
2136 if (ix%granules_per_line == 0) {
2137 if (!name_in_addr_range) {
2138 ast->print_cr("No methods, blobs, or stubs found in this address range");
2139 }
2140 name_in_addr_range = false;
2141
2142 size_t end_ix = (ix+granules_per_line <= alloc_granules) ? ix+granules_per_line : alloc_granules;
2143 ast->cr();
2144 ast->print_cr("--------------------------------------------------------------------");
2145 ast->print_cr("Address range [" INTPTR_FORMAT "," INTPTR_FORMAT "), " SIZE_FORMAT "k", p2i(low_bound+ix*granule_size), p2i(low_bound + end_ix*granule_size), (end_ix - ix)*granule_size/(size_t)K);
2146 ast->print_cr("--------------------------------------------------------------------");
2147 STRINGSTREAM_FLUSH_LOCKED("")
2148 }
2149 // Only check granule if it contains at least one blob.
2150 unsigned int nBlobs = StatArray[ix].t1_count + StatArray[ix].t2_count + StatArray[ix].tx_count +
2151 StatArray[ix].stub_count + StatArray[ix].dead_count;
2152 if (nBlobs > 0 ) {
2153 for (unsigned int is = 0; is < granule_size; is+=(unsigned int)seg_size) {
2154 // heap->find_start() is safe. Only works on _segmap.
2155 // Returns NULL or void*. Returned CodeBlob may be uninitialized.
2156 char* this_seg = low_bound + ix*granule_size + is;
2157 CodeBlob* this_blob = (CodeBlob*)(heap->find_start(this_seg));
2158 bool blob_is_safe = blob_access_is_safe(this_blob, NULL);
2159 // blob could have been flushed, freed, and merged.
2160 // this_blob < last_blob is an indicator for that.
2161 if (blob_is_safe && (this_blob > last_blob)) {
2162 last_blob = this_blob;
2163
2164 //---< get type and name >---
2165 blobType cbType = noType;
2166 if (segment_granules) {
2167 cbType = (blobType)StatArray[ix].type;
2175 //---< access these fields only if we own the CodeCache_lock >---
2176 const char* blob_name = "<unavailable>";
2177 nmethod* nm = NULL;
2178 if (have_CodeCache_lock) {
2179 blob_name = this_blob->name();
2180 nm = this_blob->as_nmethod_or_null();
2181 // this_blob->name() could return NULL if no name was given to CTOR. Inlined, maybe invisible on stack
2182 if ((blob_name == NULL) || !os::is_readable_pointer(blob_name)) {
2183 blob_name = "<unavailable>";
2184 }
2185 }
2186
2187 //---< print table header for new print range >---
2188 if (!name_in_addr_range) {
2189 name_in_addr_range = true;
2190 ast->fill_to(51);
2191 ast->print("%9s", "compiler");
2192 ast->fill_to(61);
2193 ast->print_cr("%6s", "method");
2194 ast->print_cr("%18s %13s %17s %9s %5s %18s %s", "Addr(module) ", "offset", "size", " type lvl", " temp", "blobType ", "Name");
2195 STRINGSTREAM_FLUSH_LOCKED("")
2196 }
2197
2198 //---< print line prefix (address and offset from CodeHeap start) >---
2199 ast->print(INTPTR_FORMAT, p2i(this_blob));
2200 ast->fill_to(19);
2201 ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
2202 ast->fill_to(33);
2203
2204 // access nmethod and Method fields only if we own the CodeCache_lock.
2205 // This fact is implicitly transported via nm != NULL.
2206 if (CompiledMethod::nmethod_access_is_safe(nm)) {
2207 Method* method = nm->method();
2208 ResourceMark rm;
2209 //---< collect all data to locals as quickly as possible >---
2210 unsigned int total_size = nm->total_size();
2211 int hotness = nm->hotness_counter();
2212 bool get_name = (cbType == nMethod_inuse) || (cbType == nMethod_notused);
2213 //---< nMethod size in hex >---
2214 ast->print(PTR32_FORMAT, total_size);
2215 ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
2231 Symbol* methName = method->name();
2232 const char* methNameS = (methName == NULL) ? NULL : methName->as_C_string();
2233 methNameS = (methNameS == NULL) ? "<method name unavailable>" : methNameS;
2234 Symbol* methSig = method->signature();
2235 const char* methSigS = (methSig == NULL) ? NULL : methSig->as_C_string();
2236 methSigS = (methSigS == NULL) ? "<method signature unavailable>" : methSigS;
2237 ast->print("%s", methNameS);
2238 ast->print("%s", methSigS);
2239 } else {
2240 ast->print("%s", blob_name);
2241 }
2242 } else if (blob_is_safe) {
2243 ast->fill_to(62+6);
2244 ast->print("%s", blobTypeName[cbType]);
2245 ast->fill_to(82+6);
2246 ast->print("%s", blob_name);
2247 } else {
2248 ast->fill_to(62+6);
2249 ast->print("<stale blob>");
2250 }
2251 STRINGSTREAM_FLUSH_LOCKED("\n")
2252 } else if (!blob_is_safe && (this_blob != last_blob) && (this_blob != NULL)) {
2253 last_blob = this_blob;
2254 STRINGSTREAM_FLUSH_LOCKED("\n")
2255 }
2256 }
2257 } // nBlobs > 0
2258 }
2259 STRINGSTREAM_FLUSH_LOCKED("\n\n")
2260 }
2261
2262
2263 void CodeHeapState::printBox(outputStream* ast, const char border, const char* text1, const char* text2) {
2264 unsigned int lineLen = 1 + 2 + 2 + 1;
2265 char edge, frame;
2266
2267 if (text1 != NULL) {
2268 lineLen += (unsigned int)strlen(text1); // text1 is much shorter than MAX_INT chars.
2269 }
2270 if (text2 != NULL) {
2271 lineLen += (unsigned int)strlen(text2); // text2 is much shorter than MAX_INT chars.
2272 }
2273 if (border == '-') {
2274 edge = '+';
2275 frame = '|';
2276 } else {
2277 edge = border;
2278 frame = border;
2279 }
2376 if (ix > 0) {
2377 ast->print("|");
2378 }
2379 ast->cr();
2380 assert(out == ast, "must use the same stream!");
2381
2382 ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2383 ast->fill_to(19);
2384 ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2385 }
2386 }
2387
2388 void CodeHeapState::print_line_delim(outputStream* out, bufferedStream* ast, char* low_bound, unsigned int ix, unsigned int gpl) {
2389 assert(out != ast, "must not use the same stream!");
2390 if (ix % gpl == 0) {
2391 if (ix > 0) {
2392 ast->print("|");
2393 }
2394 ast->cr();
2395
2396 { // can't use STRINGSTREAM_FLUSH_LOCKED("") here.
2397 ttyLocker ttyl;
2398 out->print("%s", ast->as_string());
2399 ast->reset();
2400 }
2401
2402 ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2403 ast->fill_to(19);
2404 ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2405 }
2406 }
2407
2408 CodeHeapState::blobType CodeHeapState::get_cbType(CodeBlob* cb) {
2409 if ((cb != NULL) && os::is_readable_pointer(cb)) {
2410 if (cb->is_runtime_stub()) return runtimeStub;
2411 if (cb->is_deoptimization_stub()) return deoptimizationStub;
2412 if (cb->is_uncommon_trap_stub()) return uncommonTrapStub;
2413 if (cb->is_exception_stub()) return exceptionStub;
2414 if (cb->is_safepoint_stub()) return safepointStub;
2415 if (cb->is_adapter_blob()) return adapterBlob;
2416 if (cb->is_method_handles_adapter_blob()) return mh_adapterBlob;
|
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 // With this declaration macro, it is possible to switch between
27 // - direct output into an argument-passed outputStream and
28 // - buffered output into a bufferedStream with subsequent flush
29 // of the filled buffer to the outputStream.
30 #define USE_BUFFEREDSTREAM
31
32 #include "precompiled.hpp"
33 #include "code/codeHeapState.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "runtime/sweeper.hpp"
36
37 // -------------------------
38 // | General Description |
39 // -------------------------
40 // The CodeHeap state analytics are divided in two parts.
41 // The first part examines the entire CodeHeap and aggregates all
42 // information that is believed useful/important.
43 //
44 // Aggregation condenses the information of a piece of the CodeHeap
45 // (4096 bytes by default) into an analysis granule. These granules
46 // contain enough detail to gain initial insight while keeping the
47 // internal structure sizes in check.
48 //
49 // The second part, which consists of several, independent steps,
50 // prints the previously collected information with emphasis on
51 // various aspects.
62 // The CodeHeap state analytics do have some memory footprint.
63 // The "aggregate" step allocates some data structures to hold the aggregated
64 // information for later output. These data structures live until they are
65 // explicitly discarded (function "discard") or until the VM terminates.
66 // There is one exception: the function "all" does not leave any data
67 // structures allocated.
68 //
69 // Requests for real-time, on-the-fly analysis can be issued via
70 // jcmd <pid> Compiler.CodeHeap_Analytics [<function>] [<granularity>]
71 //
72 // If you are (only) interested in how the CodeHeap looks like after running
73 // a sample workload, you can use the command line option
74 // -XX:+PrintCodeHeapAnalytics
75 // It will cause a full analysis to be written to tty. In addition, a full
76 // analysis will be written the first time a "CodeCache full" condition is
77 // detected.
78 //
79 // The command line option produces output identical to the jcmd function
80 // jcmd <pid> Compiler.CodeHeap_Analytics all 4096
81 // ---------------------------------------------------------------------------------
82 //
83 // There are instances when composing an output line or a small set of
84 // output lines out of many tty->print() calls creates significant overhead.
85 // Writing to a bufferedStream buffer first has a significant advantage:
86 // It uses noticeably less cpu cycles and reduces (when writing to a
87 // network file) the required bandwidth by at least a factor of ten. Observed on MacOS.
88 // That clearly makes up for the increased code complexity.
89 //
90 // Conversion of existing code is easy and straightforward, if the code already
91 // uses a parameterized output destination, e.g. "outputStream st".
92 // - rename the formal parameter to any other name, e.g. out_st.
93 // - at a suitable place in your code, insert
94 // BUFFEREDSTEAM_DECL(buf_st, out_st)
95 // This will provide all the declarations necessary. After that, all
96 // buf_st->print() (and the like) calls will be directed to a bufferedStream object.
97 // Once a block of output (a line or a small set of lines) is composed, insert
98 // BUFFEREDSTREAM_FLUSH(termstring)
99 // to flush the bufferedStream to the final destination out_st. termstring is just
100 // an arbitrary string (e.g. "\n") which is appended to the bufferedStream before
101 // being written to out_st. Be aware that the last character written MUST be a '\n'.
102 // Otherwise, buf_st->position() does not correspond to out_st->position() any longer.
103 // BUFFEREDSTREAM_FLUSH_LOCKED(termstring)
104 // does the same thing, protected by the ttyLocker lock.
105 // BUFFEREDSTREAM_FLUSH_IF(termstring, remSize)
106 // does a flush only if the remaining buffer space is less than remSize.
107 //
108 // To activate, #define USE_BUFFERED_STREAM before including this header.
109 // If not activated, output will directly go to the originally used outputStream
110 // with no additional overhead.
111 //
112 #if defined(USE_BUFFEREDSTREAM)
113 // All necessary declarations to print via a bufferedStream
114 // This macro must be placed before any other BUFFEREDSTREAM*
115 // macro in the function.
116 #define BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, _capa) \
117 ResourceMark _rm; \
118 /* _anyst name of the stream as used in the code */ \
119 /* _outst stream where final output will go to */ \
120 /* _capa allocated capacity of stream buffer */ \
121 size_t _nflush = 0; \
122 size_t _nforcedflush = 0; \
123 size_t _nsavedflush = 0; \
124 size_t _nlockedflush = 0; \
125 size_t _nflush_bytes = 0; \
126 size_t _capacity = _capa; \
127 bufferedStream _sstobj = bufferedStream(_capa); \
128 bufferedStream* _sstbuf = &_sstobj; \
129 outputStream* _outbuf = _outst; \
130 bufferedStream* _anyst = &_sstobj; /* any stream. Use this to just print - no buffer flush. */
131
132 // Same as above, but with fixed buffer size.
133 #define BUFFEREDSTREAM_DECL(_anyst, _outst) \
134 BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, 4*K);
135
136 // Flush the buffer contents unconditionally.
137 // No action if the buffer is empty.
138 #define BUFFEREDSTREAM_FLUSH(_termString) \
139 if (((_termString) != NULL) && (strlen(_termString) > 0)){\
140 _sstbuf->print("%s", _termString); \
141 } \
142 if (_sstbuf != _outbuf) { \
143 if (_sstbuf->size() != 0) { \
144 _nforcedflush++; _nflush_bytes += _sstbuf->size(); \
145 _outbuf->print("%s", _sstbuf->as_string()); \
146 _sstbuf->reset(); \
147 } \
148 }
149
150 // Flush the buffer contents if the remaining capacity is
151 // less than the given threshold.
152 #define BUFFEREDSTREAM_FLUSH_IF(_termString, _remSize) \
153 if (((_termString) != NULL) && (strlen(_termString) > 0)){\
154 _sstbuf->print("%s", _termString); \
155 } \
156 if (_sstbuf != _outbuf) { \
157 if ((_capacity - _sstbuf->size()) < (size_t)(_remSize)){\
158 _nflush++; _nforcedflush--; \
159 BUFFEREDSTREAM_FLUSH("") \
160 } else { \
161 _nsavedflush++; \
162 } \
163 }
164
165 // Flush the buffer contents if the remaining capacity is less
166 // than the calculated threshold (256 bytes + capacity/16)
167 // That should suffice for all reasonably sized output lines.
168 #define BUFFEREDSTREAM_FLUSH_AUTO(_termString) \
169 BUFFEREDSTREAM_FLUSH_IF(_termString, 256+(_capacity>>4))
170
171 #define BUFFEREDSTREAM_FLUSH_LOCKED(_termString) \
172 { ttyLocker ttyl;/* keep this output block together */ \
173 _nlockedflush++; \
174 BUFFEREDSTREAM_FLUSH(_termString) \
175 }
176
177 // #define BUFFEREDSTREAM_FLUSH_STAT() \
178 // if (_sstbuf != _outbuf) { \
179 // _outbuf->print_cr("%ld flushes (buffer full), %ld forced, %ld locked, %ld bytes total, %ld flushes saved", _nflush, _nforcedflush, _nlockedflush, _nflush_bytes, _nsavedflush); \
180 // }
181
182 #define BUFFEREDSTREAM_FLUSH_STAT()
183 #else
184 #define BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, _capa) \
185 size_t _capacity = _capa; \
186 outputStream* _outbuf = _outst; \
187 outputStream* _anyst = _outst; /* any stream. Use this to just print - no buffer flush. */
188
189 #define BUFFEREDSTREAM_DECL(_anyst, _outst) \
190 BUFFEREDSTREAM_DECL_SIZE(_anyst, _outst, 4*K)
191
192 #define BUFFEREDSTREAM_FLUSH(_termString) \
193 if (((_termString) != NULL) && (strlen(_termString) > 0)){\
194 _outbuf->print("%s", _termString); \
195 }
196
197 #define BUFFEREDSTREAM_FLUSH_IF(_termString, _remSize) \
198 BUFFEREDSTREAM_FLUSH(_termString)
199
200 #define BUFFEREDSTREAM_FLUSH_AUTO(_termString) \
201 BUFFEREDSTREAM_FLUSH(_termString)
202
203 #define BUFFEREDSTREAM_FLUSH_LOCKED(_termString) \
204 BUFFEREDSTREAM_FLUSH(_termString)
205
206 #define BUFFEREDSTREAM_FLUSH_STAT()
207 #endif
208 #define HEX32_FORMAT "0x%x" // just a helper format string used below multiple times
209
210 const char blobTypeChar[] = {' ', 'C', 'N', 'I', 'X', 'Z', 'U', 'R', '?', 'D', 'T', 'E', 'S', 'A', 'M', 'B', 'L' };
211 const char* blobTypeName[] = {"noType"
212 , "nMethod (under construction)"
213 , "nMethod (active)"
214 , "nMethod (inactive)"
215 , "nMethod (deopt)"
216 , "nMethod (zombie)"
217 , "nMethod (unloaded)"
218 , "runtime stub"
219 , "ricochet stub"
220 , "deopt stub"
221 , "uncommon trap stub"
222 , "exception stub"
223 , "safepoint stub"
224 , "adapter blob"
225 , "MH adapter blob"
226 , "buffer blob"
227 , "lastType"
228 };
535 unsigned int nBlocks_used = 0;
536 unsigned int nBlocks_zomb = 0;
537 unsigned int nBlocks_disconn = 0;
538 unsigned int nBlocks_notentr = 0;
539
540 //---< max & min of TopSizeArray >---
541 // it is sufficient to have these sizes as 32bit unsigned ints.
542 // The CodeHeap is limited in size to 4GB. Furthermore, the sizes
543 // are stored in _segment_size units, scaling them down by a factor of 64 (at least).
544 unsigned int currMax = 0;
545 unsigned int currMin = 0;
546 unsigned int currMin_ix = 0;
547 unsigned long total_iterations = 0;
548
549 bool done = false;
550 const int min_granules = 256;
551 const int max_granules = 512*K; // limits analyzable CodeHeap (with segment_granules) to 32M..128M
552 // results in StatArray size of 24M (= max_granules * 48 Bytes per element)
553 // For a 1GB CodeHeap, the granule size must be at least 2kB to not violate the max_granles limit.
554 const char* heapName = get_heapName(heap);
555 BUFFEREDSTREAM_DECL(ast, out)
556
557 if (!initialization_complete) {
558 memset(CodeHeapStatArray, 0, sizeof(CodeHeapStatArray));
559 initialization_complete = true;
560
561 printBox(ast, '=', "C O D E H E A P A N A L Y S I S (general remarks)", NULL);
562 ast->print_cr(" The code heap analysis function provides deep insights into\n"
563 " the inner workings and the internal state of the Java VM's\n"
564 " code cache - the place where all the JVM generated machine\n"
565 " code is stored.\n"
566 " \n"
567 " This function is designed and provided for support engineers\n"
568 " to help them understand and solve issues in customer systems.\n"
569 " It is not intended for use and interpretation by other persons.\n"
570 " \n");
571 BUFFEREDSTREAM_FLUSH("")
572 }
573 get_HeapStatGlobals(out, heapName);
574
575
576 // Since we are (and must be) analyzing the CodeHeap contents under the CodeCache_lock,
577 // all heap information is "constant" and can be safely extracted/calculated before we
578 // enter the while() loop. Actually, the loop will only be iterated once.
579 char* low_bound = heap->low_boundary();
580 size_t size = heap->capacity();
581 size_t res_size = heap->max_capacity();
582 seg_size = heap->segment_size();
583 log2_seg_size = seg_size == 0 ? 0 : exact_log2(seg_size); // This is a global static value.
584
585 if (seg_size == 0) {
586 printBox(ast, '-', "Heap not fully initialized yet, segment size is zero for segment ", heapName);
587 BUFFEREDSTREAM_FLUSH("")
588 return;
589 }
590
591 if (!CodeCache_lock->owned_by_self()) {
592 printBox(ast, '-', "aggregate function called without holding the CodeCache_lock for ", heapName);
593 BUFFEREDSTREAM_FLUSH("")
594 return;
595 }
596
597 // Calculate granularity of analysis (and output).
598 // The CodeHeap is managed (allocated) in segments (units) of CodeCacheSegmentSize.
599 // The CodeHeap can become fairly large, in particular in productive real-life systems.
600 //
601 // It is often neither feasible nor desirable to aggregate the data with the highest possible
602 // level of detail, i.e. inspecting and printing each segment on its own.
603 //
604 // The granularity parameter allows to specify the level of detail available in the analysis.
605 // It must be a positive multiple of the segment size and should be selected such that enough
606 // detail is provided while, at the same time, the printed output does not explode.
607 //
608 // By manipulating the granularity value, we enforce that at least min_granules units
609 // of analysis are available. We also enforce an upper limit of max_granules units to
610 // keep the amount of allocated storage in check.
611 //
612 // Finally, we adjust the granularity such that each granule covers at most 64k-1 segments.
613 // This is necessary to prevent an unsigned short overflow while accumulating space information.
629 granularity = granularity & (~(seg_size - 1)); // must be multiple of seg_size
630 if (granularity>>log2_seg_size >= (1L<<sizeof(unsigned short)*8)) {
631 granularity = ((1L<<(sizeof(unsigned short)*8))-1)<<log2_seg_size; // Limit: (64k-1) * seg_size
632 }
633 segment_granules = granularity == seg_size;
634 size_t granules = (size + (granularity-1))/granularity;
635
636 printBox(ast, '=', "C O D E H E A P A N A L Y S I S (used blocks) for segment ", heapName);
637 ast->print_cr(" The aggregate step takes an aggregated snapshot of the CodeHeap.\n"
638 " Subsequent print functions create their output based on this snapshot.\n"
639 " The CodeHeap is a living thing, and every effort has been made for the\n"
640 " collected data to be consistent. Only the method names and signatures\n"
641 " are retrieved at print time. That may lead to rare cases where the\n"
642 " name of a method is no longer available, e.g. because it was unloaded.\n");
643 ast->print_cr(" CodeHeap committed size " SIZE_FORMAT "K (" SIZE_FORMAT "M), reserved size " SIZE_FORMAT "K (" SIZE_FORMAT "M), %d%% occupied.",
644 size/(size_t)K, size/(size_t)M, res_size/(size_t)K, res_size/(size_t)M, (unsigned int)(100.0*size/res_size));
645 ast->print_cr(" CodeHeap allocation segment size is " SIZE_FORMAT " bytes. This is the smallest possible granularity.", seg_size);
646 ast->print_cr(" CodeHeap (committed part) is mapped to " SIZE_FORMAT " granules of size " SIZE_FORMAT " bytes.", granules, granularity);
647 ast->print_cr(" Each granule takes " SIZE_FORMAT " bytes of C heap, that is " SIZE_FORMAT "K in total for statistics data.", sizeof(StatElement), (sizeof(StatElement)*granules)/(size_t)K);
648 ast->print_cr(" The number of granules is limited to %dk, requiring a granules size of at least %d bytes for a 1GB heap.", (unsigned int)(max_granules/K), (unsigned int)(G/max_granules));
649 BUFFEREDSTREAM_FLUSH("\n")
650
651
652 while (!done) {
653 //---< reset counters with every aggregation >---
654 nBlocks_t1 = 0;
655 nBlocks_t2 = 0;
656 nBlocks_alive = 0;
657 nBlocks_dead = 0;
658 nBlocks_inconstr = 0;
659 nBlocks_unloaded = 0;
660 nBlocks_stub = 0;
661
662 nBlocks_free = 0;
663 nBlocks_used = 0;
664 nBlocks_zomb = 0;
665 nBlocks_disconn = 0;
666 nBlocks_notentr = 0;
667
668 //---< discard old arrays if size does not match >---
669 if (granules != alloc_granules) {
718 // This is a diagnostic function. It is not supposed to tear down the VM.
719 if ((char*)h < low_bound) {
720 insane = true; ast->print_cr("Sanity check: HeapBlock @%p below low bound (%p)", (char*)h, low_bound);
721 }
722 if ((char*)h > (low_bound + res_size)) {
723 insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside reserved range (%p)", (char*)h, low_bound + res_size);
724 }
725 if ((char*)h > (low_bound + size)) {
726 insane = true; ast->print_cr("Sanity check: HeapBlock @%p outside used range (%p)", (char*)h, low_bound + size);
727 }
728 if (ix_end >= granules) {
729 insane = true; ast->print_cr("Sanity check: end index (%d) out of bounds (" SIZE_FORMAT ")", ix_end, granules);
730 }
731 if (size != heap->capacity()) {
732 insane = true; ast->print_cr("Sanity check: code heap capacity has changed (" SIZE_FORMAT "K to " SIZE_FORMAT "K)", size/(size_t)K, heap->capacity()/(size_t)K);
733 }
734 if (ix_beg > ix_end) {
735 insane = true; ast->print_cr("Sanity check: end index (%d) lower than begin index (%d)", ix_end, ix_beg);
736 }
737 if (insane) {
738 BUFFEREDSTREAM_FLUSH("")
739 continue;
740 }
741
742 if (h->free()) {
743 nBlocks_free++;
744 freeSpace += hb_bytelen;
745 if (hb_bytelen > maxFreeSize) {
746 maxFreeSize = hb_bytelen;
747 maxFreeBlock = h;
748 }
749 } else {
750 update_SizeDistArray(out, hb_len);
751 nBlocks_used++;
752 usedSpace += hb_bytelen;
753 CodeBlob* cb = (CodeBlob*)heap->find_start(h);
754 if (cb != NULL) {
755 cbType = get_cbType(cb);
756 if (cb->is_nmethod()) {
757 compile_id = ((nmethod*)cb)->compile_id();
758 comp_lvl = (CompLevel)((nmethod*)cb)->comp_level();
1107 ast->cr();
1108 ast->print_cr("latest allocated compilation id = %d", latest_compilation_id);
1109 ast->print_cr("highest observed compilation id = %d", highest_compilation_id);
1110 ast->print_cr("Building TopSizeList iterations = %ld", total_iterations);
1111 ast->cr();
1112
1113 int reset_val = NMethodSweeper::hotness_counter_reset_val();
1114 double reverse_free_ratio = (res_size > size) ? (double)res_size/(double)(res_size-size) : (double)res_size;
1115 printBox(ast, '-', "Method hotness information at time of this analysis", NULL);
1116 ast->print_cr("Highest possible method temperature: %12d", reset_val);
1117 ast->print_cr("Threshold for method to be considered 'cold': %12.3f", -reset_val + reverse_free_ratio * NmethodSweepActivity);
1118 if (n_methods > 0) {
1119 avgTemp = hotnessAccumulator/n_methods;
1120 ast->print_cr("min. hotness = %6d", minTemp);
1121 ast->print_cr("avg. hotness = %6d", avgTemp);
1122 ast->print_cr("max. hotness = %6d", maxTemp);
1123 } else {
1124 avgTemp = 0;
1125 ast->print_cr("No hotness data available");
1126 }
1127 BUFFEREDSTREAM_FLUSH("\n")
1128
1129 // This loop is intentionally printing directly to "out".
1130 // It should not print anything, anyway.
1131 out->print("Verifying collected data...");
1132 size_t granule_segs = granule_size>>log2_seg_size;
1133 for (unsigned int ix = 0; ix < granules; ix++) {
1134 if (StatArray[ix].t1_count > granule_segs) {
1135 out->print_cr("t1_count[%d] = %d", ix, StatArray[ix].t1_count);
1136 }
1137 if (StatArray[ix].t2_count > granule_segs) {
1138 out->print_cr("t2_count[%d] = %d", ix, StatArray[ix].t2_count);
1139 }
1140 if (StatArray[ix].tx_count > granule_segs) {
1141 out->print_cr("tx_count[%d] = %d", ix, StatArray[ix].tx_count);
1142 }
1143 if (StatArray[ix].stub_count > granule_segs) {
1144 out->print_cr("stub_count[%d] = %d", ix, StatArray[ix].stub_count);
1145 }
1146 if (StatArray[ix].dead_count > granule_segs) {
1147 out->print_cr("dead_count[%d] = %d", ix, StatArray[ix].dead_count);
1189 }
1190 }
1191 }
1192 out->print_cr("...done\n\n");
1193 } else {
1194 // insane heap state detected. Analysis data incomplete. Just throw it away.
1195 discard_StatArray(out);
1196 discard_TopSizeArray(out);
1197 }
1198 }
1199
1200
1201 done = false;
1202 while (!done && (nBlocks_free > 0)) {
1203
1204 printBox(ast, '=', "C O D E H E A P A N A L Y S I S (free blocks) for segment ", heapName);
1205 ast->print_cr(" The aggregate step collects information about all free blocks in CodeHeap.\n"
1206 " Subsequent print functions create their output based on this snapshot.\n");
1207 ast->print_cr(" Free space in %s is distributed over %d free blocks.", heapName, nBlocks_free);
1208 ast->print_cr(" Each free block takes " SIZE_FORMAT " bytes of C heap for statistics data, that is " SIZE_FORMAT "K in total.", sizeof(FreeBlk), (sizeof(FreeBlk)*nBlocks_free)/K);
1209 BUFFEREDSTREAM_FLUSH("\n")
1210
1211 //----------------------------------------
1212 //-- Prepare the FreeArray of FreeBlks --
1213 //----------------------------------------
1214
1215 //---< discard old array if size does not match >---
1216 if (nBlocks_free != alloc_freeBlocks) {
1217 discard_FreeArray(out);
1218 }
1219
1220 prepare_FreeArray(out, nBlocks_free, heapName);
1221 if (FreeArray == NULL) {
1222 done = true;
1223 continue;
1224 }
1225
1226 //----------------------------------------
1227 //-- Collect all FreeBlks in FreeArray --
1228 //----------------------------------------
1229
1230 unsigned int ix = 0;
1231 FreeBlock* cur = heap->freelist();
1232
1233 while (cur != NULL) {
1234 if (ix < alloc_freeBlocks) { // don't index out of bounds if _freelist has more blocks than anticipated
1235 FreeArray[ix].start = cur;
1236 FreeArray[ix].len = (unsigned int)(cur->length()<<log2_seg_size);
1237 FreeArray[ix].index = ix;
1238 }
1239 cur = cur->link();
1240 ix++;
1241 }
1242 if (ix != alloc_freeBlocks) {
1243 ast->print_cr("Free block count mismatch. Expected %d free blocks, but found %d.", alloc_freeBlocks, ix);
1244 ast->print_cr("I will update the counter and retry data collection");
1245 BUFFEREDSTREAM_FLUSH("\n")
1246 nBlocks_free = ix;
1247 continue;
1248 }
1249 done = true;
1250 }
1251
1252 if (!done || (nBlocks_free == 0)) {
1253 if (nBlocks_free == 0) {
1254 printBox(ast, '-', "no free blocks found in ", heapName);
1255 } else if (!done) {
1256 ast->print_cr("Free block count mismatch could not be resolved.");
1257 ast->print_cr("Try to run \"aggregate\" function to update counters");
1258 }
1259 BUFFEREDSTREAM_FLUSH("")
1260
1261 //---< discard old array and update global values >---
1262 discard_FreeArray(out);
1263 set_HeapStatGlobals(out, heapName);
1264 return;
1265 }
1266
1267 //---< calculate and fill remaining fields >---
1268 if (FreeArray != NULL) {
1269 // This loop is intentionally printing directly to "out".
1270 // It should not print anything, anyway.
1271 for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1272 size_t lenSum = 0;
1273 FreeArray[ix].gap = (unsigned int)((address)FreeArray[ix+1].start - ((address)FreeArray[ix].start + FreeArray[ix].len));
1274 for (HeapBlock *h = heap->next_block(FreeArray[ix].start); (h != NULL) && (h != FreeArray[ix+1].start); h = heap->next_block(h)) {
1275 CodeBlob *cb = (CodeBlob*)(heap->find_start(h));
1276 if ((cb != NULL) && !cb->is_nmethod()) {
1277 FreeArray[ix].stubs_in_gap = true;
1278 }
1279 FreeArray[ix].n_gapBlocks++;
1280 lenSum += h->length()<<log2_seg_size;
1281 if (((address)h < ((address)FreeArray[ix].start+FreeArray[ix].len)) || (h >= FreeArray[ix+1].start)) {
1282 out->print_cr("unsorted occupied CodeHeap block found @ %p, gap interval [%p, %p)", h, (address)FreeArray[ix].start+FreeArray[ix].len, FreeArray[ix+1].start);
1283 }
1284 }
1285 if (lenSum != FreeArray[ix].gap) {
1286 out->print_cr("Length mismatch for gap between FreeBlk[%d] and FreeBlk[%d]. Calculated: %d, accumulated: %d.", ix, ix+1, FreeArray[ix].gap, (unsigned int)lenSum);
1287 }
1288 }
1289 }
1290 set_HeapStatGlobals(out, heapName);
1291
1292 printBox(ast, '=', "C O D E H E A P A N A L Y S I S C O M P L E T E for segment ", heapName);
1293 BUFFEREDSTREAM_FLUSH("\n")
1294 }
1295
1296
1297 void CodeHeapState::print_usedSpace(outputStream* out, CodeHeap* heap) {
1298 if (!initialization_complete) {
1299 return;
1300 }
1301
1302 const char* heapName = get_heapName(heap);
1303 get_HeapStatGlobals(out, heapName);
1304
1305 if ((StatArray == NULL) || (TopSizeArray == NULL) || (used_topSizeBlocks == 0)) {
1306 return;
1307 }
1308 BUFFEREDSTREAM_DECL(ast, out)
1309
1310 {
1311 printBox(ast, '=', "U S E D S P A C E S T A T I S T I C S for ", heapName);
1312 ast->print_cr("Note: The Top%d list of the largest used blocks associates method names\n"
1313 " and other identifying information with the block size data.\n"
1314 "\n"
1315 " Method names are dynamically retrieved from the code cache at print time.\n"
1316 " Due to the living nature of the code cache and because the CodeCache_lock\n"
1317 " is not continuously held, the displayed name might be wrong or no name\n"
1318 " might be found at all. The likelihood for that to happen increases\n"
1319 " over time passed between analysis and print step.\n", used_topSizeBlocks);
1320 BUFFEREDSTREAM_FLUSH_LOCKED("\n")
1321 }
1322
1323 //----------------------------
1324 //-- Print Top Used Blocks --
1325 //----------------------------
1326 {
1327 char* low_bound = heap->low_boundary();
1328 bool have_CodeCache_lock = CodeCache_lock->owned_by_self();
1329
1330 printBox(ast, '-', "Largest Used Blocks in ", heapName);
1331 print_blobType_legend(ast);
1332
1333 ast->fill_to(51);
1334 ast->print("%4s", "blob");
1335 ast->fill_to(56);
1336 ast->print("%9s", "compiler");
1337 ast->fill_to(66);
1338 ast->print_cr("%6s", "method");
1339 ast->print_cr("%18s %13s %17s %4s %9s %5s %s", "Addr(module) ", "offset", "size", "type", " type lvl", " temp", "Name");
1340 BUFFEREDSTREAM_FLUSH_LOCKED("")
1341
1342 //---< print Top Ten Used Blocks >---
1343 if (used_topSizeBlocks > 0) {
1344 unsigned int printed_topSizeBlocks = 0;
1345 for (unsigned int i = 0; i != tsbStopper; i = TopSizeArray[i].index) {
1346 printed_topSizeBlocks++;
1347 nmethod* nm = NULL;
1348 const char* blob_name = "unnamed blob or blob name unavailable";
1349 // heap->find_start() is safe. Only works on _segmap.
1350 // Returns NULL or void*. Returned CodeBlob may be uninitialized.
1351 HeapBlock* heapBlock = TopSizeArray[i].start;
1352 CodeBlob* this_blob = (CodeBlob*)(heap->find_start(heapBlock));
1353 bool blob_is_safe = blob_access_is_safe(this_blob, NULL);
1354 if (blob_is_safe) {
1355 //---< access these fields only if we own the CodeCache_lock >---
1356 if (have_CodeCache_lock) {
1357 blob_name = this_blob->name();
1358 nm = this_blob->as_nmethod_or_null();
1359 }
1360 //---< blob address >---
1398 ast->print("%5d", nm->hotness_counter());
1399 //---< name and signature >---
1400 ast->fill_to(67+6);
1401 if (nm->is_not_installed()) {
1402 ast->print(" not (yet) installed method ");
1403 }
1404 if (nm->is_zombie()) {
1405 ast->print(" zombie method ");
1406 }
1407 ast->print("%s", blob_name);
1408 } else {
1409 //---< block size in hex >---
1410 ast->print(PTR32_FORMAT, (unsigned int)(TopSizeArray[i].len<<log2_seg_size));
1411 ast->print("(" SIZE_FORMAT_W(4) "K)", (TopSizeArray[i].len<<log2_seg_size)/K);
1412 //---< no compiler information >---
1413 ast->fill_to(56);
1414 //---< name and signature >---
1415 ast->fill_to(67+6);
1416 ast->print("%s", blob_name);
1417 }
1418 ast->cr();
1419 BUFFEREDSTREAM_FLUSH_AUTO("")
1420 }
1421 if (used_topSizeBlocks != printed_topSizeBlocks) {
1422 ast->print_cr("used blocks: %d, printed blocks: %d", used_topSizeBlocks, printed_topSizeBlocks);
1423 for (unsigned int i = 0; i < alloc_topSizeBlocks; i++) {
1424 ast->print_cr(" TopSizeArray[%d].index = %d, len = %d", i, TopSizeArray[i].index, TopSizeArray[i].len);
1425 BUFFEREDSTREAM_FLUSH_AUTO("")
1426 }
1427 }
1428 BUFFEREDSTREAM_FLUSH("\n\n")
1429 }
1430 }
1431
1432 //-----------------------------
1433 //-- Print Usage Histogram --
1434 //-----------------------------
1435
1436 if (SizeDistributionArray != NULL) {
1437 unsigned long total_count = 0;
1438 unsigned long total_size = 0;
1439 const unsigned long pctFactor = 200;
1440
1441 for (unsigned int i = 0; i < nSizeDistElements; i++) {
1442 total_count += SizeDistributionArray[i].count;
1443 total_size += SizeDistributionArray[i].lenSum;
1444 }
1445
1446 if ((total_count > 0) && (total_size > 0)) {
1447 printBox(ast, '-', "Block count histogram for ", heapName);
1448 ast->print_cr("Note: The histogram indicates how many blocks (as a percentage\n"
1449 " of all blocks) have a size in the given range.\n"
1450 " %ld characters are printed per percentage point.\n", pctFactor/100);
1451 ast->print_cr("total size of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1452 ast->print_cr("total number of all blocks: %7ld\n", total_count);
1453 BUFFEREDSTREAM_FLUSH_LOCKED("")
1454
1455 ast->print_cr("[Size Range)------avg.-size-+----count-+");
1456 for (unsigned int i = 0; i < nSizeDistElements; i++) {
1457 if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1458 ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1459 ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1460 ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1461 );
1462 } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1463 ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1464 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1465 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1466 );
1467 } else {
1468 ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1469 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1470 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1471 );
1472 }
1473 ast->print(" %8d | %8d |",
1474 SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1475 SizeDistributionArray[i].count);
1476
1477 unsigned int percent = pctFactor*SizeDistributionArray[i].count/total_count;
1478 for (unsigned int j = 1; j <= percent; j++) {
1479 ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1480 }
1481 ast->cr();
1482 BUFFEREDSTREAM_FLUSH_AUTO("")
1483 }
1484 ast->print_cr("----------------------------+----------+");
1485 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1486
1487 printBox(ast, '-', "Contribution per size range to total size for ", heapName);
1488 ast->print_cr("Note: The histogram indicates how much space (as a percentage of all\n"
1489 " occupied space) is used by the blocks in the given size range.\n"
1490 " %ld characters are printed per percentage point.\n", pctFactor/100);
1491 ast->print_cr("total size of all blocks: %7ldM", (total_size<<log2_seg_size)/M);
1492 ast->print_cr("total number of all blocks: %7ld\n", total_count);
1493 BUFFEREDSTREAM_FLUSH_LOCKED("")
1494
1495 ast->print_cr("[Size Range)------avg.-size-+----count-+");
1496 for (unsigned int i = 0; i < nSizeDistElements; i++) {
1497 if (SizeDistributionArray[i].rangeStart<<log2_seg_size < K) {
1498 ast->print("[" SIZE_FORMAT_W(5) " .." SIZE_FORMAT_W(5) " ): "
1499 ,(size_t)(SizeDistributionArray[i].rangeStart<<log2_seg_size)
1500 ,(size_t)(SizeDistributionArray[i].rangeEnd<<log2_seg_size)
1501 );
1502 } else if (SizeDistributionArray[i].rangeStart<<log2_seg_size < M) {
1503 ast->print("[" SIZE_FORMAT_W(5) "K.." SIZE_FORMAT_W(5) "K): "
1504 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/K
1505 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/K
1506 );
1507 } else {
1508 ast->print("[" SIZE_FORMAT_W(5) "M.." SIZE_FORMAT_W(5) "M): "
1509 ,(SizeDistributionArray[i].rangeStart<<log2_seg_size)/M
1510 ,(SizeDistributionArray[i].rangeEnd<<log2_seg_size)/M
1511 );
1512 }
1513 ast->print(" %8d | %8d |",
1514 SizeDistributionArray[i].count > 0 ? (SizeDistributionArray[i].lenSum<<log2_seg_size)/SizeDistributionArray[i].count : 0,
1515 SizeDistributionArray[i].count);
1516
1517 unsigned int percent = pctFactor*(unsigned long)SizeDistributionArray[i].lenSum/total_size;
1518 for (unsigned int j = 1; j <= percent; j++) {
1519 ast->print("%c", (j%((pctFactor/100)*10) == 0) ? ('0'+j/(((unsigned int)pctFactor/100)*10)) : '*');
1520 }
1521 ast->cr();
1522 BUFFEREDSTREAM_FLUSH_AUTO("")
1523 }
1524 ast->print_cr("----------------------------+----------+");
1525 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1526 }
1527 }
1528 }
1529
1530
1531 void CodeHeapState::print_freeSpace(outputStream* out, CodeHeap* heap) {
1532 if (!initialization_complete) {
1533 return;
1534 }
1535
1536 const char* heapName = get_heapName(heap);
1537 get_HeapStatGlobals(out, heapName);
1538
1539 if ((StatArray == NULL) || (FreeArray == NULL) || (alloc_granules == 0)) {
1540 return;
1541 }
1542 BUFFEREDSTREAM_DECL(ast, out)
1543
1544 {
1545 printBox(ast, '=', "F R E E S P A C E S T A T I S T I C S for ", heapName);
1546 ast->print_cr("Note: in this context, a gap is the occupied space between two free blocks.\n"
1547 " Those gaps are of interest if there is a chance that they become\n"
1548 " unoccupied, e.g. by class unloading. Then, the two adjacent free\n"
1549 " blocks, together with the now unoccupied space, form a new, large\n"
1550 " free block.");
1551 BUFFEREDSTREAM_FLUSH_LOCKED("\n")
1552 }
1553
1554 {
1555 printBox(ast, '-', "List of all Free Blocks in ", heapName);
1556
1557 unsigned int ix = 0;
1558 for (ix = 0; ix < alloc_freeBlocks-1; ix++) {
1559 ast->print(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT ",", p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1560 ast->fill_to(38);
1561 ast->print("Gap[%4d..%4d]: " HEX32_FORMAT " bytes,", ix, ix+1, FreeArray[ix].gap);
1562 ast->fill_to(71);
1563 ast->print("block count: %6d", FreeArray[ix].n_gapBlocks);
1564 if (FreeArray[ix].stubs_in_gap) {
1565 ast->print(" !! permanent gap, contains stubs and/or blobs !!");
1566 }
1567 ast->cr();
1568 BUFFEREDSTREAM_FLUSH_AUTO("")
1569 }
1570 ast->print_cr(INTPTR_FORMAT ": Len[%4d] = " HEX32_FORMAT, p2i(FreeArray[ix].start), ix, FreeArray[ix].len);
1571 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
1572 }
1573
1574
1575 //-----------------------------------------
1576 //-- Find and Print Top Ten Free Blocks --
1577 //-----------------------------------------
1578
1579 //---< find Top Ten Free Blocks >---
1580 const unsigned int nTop = 10;
1581 unsigned int currMax10 = 0;
1582 struct FreeBlk* FreeTopTen[nTop];
1583 memset(FreeTopTen, 0, sizeof(FreeTopTen));
1584
1585 for (unsigned int ix = 0; ix < alloc_freeBlocks; ix++) {
1586 if (FreeArray[ix].len > currMax10) { // larger than the ten largest found so far
1587 unsigned int currSize = FreeArray[ix].len;
1588
1589 unsigned int iy;
1590 for (iy = 0; iy < nTop && FreeTopTen[iy] != NULL; iy++) {
1591 if (FreeTopTen[iy]->len < currSize) {
1595 FreeTopTen[iy] = &FreeArray[ix]; // insert new free block
1596 if (FreeTopTen[nTop-1] != NULL) {
1597 currMax10 = FreeTopTen[nTop-1]->len;
1598 }
1599 break; // done with this, check next free block
1600 }
1601 }
1602 if (iy >= nTop) {
1603 ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1604 currSize, currMax10);
1605 continue;
1606 }
1607 if (FreeTopTen[iy] == NULL) {
1608 FreeTopTen[iy] = &FreeArray[ix];
1609 if (iy == (nTop-1)) {
1610 currMax10 = currSize;
1611 }
1612 }
1613 }
1614 }
1615 BUFFEREDSTREAM_FLUSH_AUTO("")
1616
1617 {
1618 printBox(ast, '-', "Top Ten Free Blocks in ", heapName);
1619
1620 //---< print Top Ten Free Blocks >---
1621 for (unsigned int iy = 0; (iy < nTop) && (FreeTopTen[iy] != NULL); iy++) {
1622 ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTen[iy]->index, FreeTopTen[iy]->len);
1623 ast->fill_to(39);
1624 if (FreeTopTen[iy]->index == (alloc_freeBlocks-1)) {
1625 ast->print("last free block in list.");
1626 } else {
1627 ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTen[iy]->gap);
1628 ast->fill_to(63);
1629 ast->print("#blocks (in gap) %d", FreeTopTen[iy]->n_gapBlocks);
1630 }
1631 ast->cr();
1632 BUFFEREDSTREAM_FLUSH_AUTO("")
1633 }
1634 }
1635 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
1636
1637
1638 //--------------------------------------------------------
1639 //-- Find and Print Top Ten Free-Occupied-Free Triples --
1640 //--------------------------------------------------------
1641
1642 //---< find and print Top Ten Triples (Free-Occupied-Free) >---
1643 currMax10 = 0;
1644 struct FreeBlk *FreeTopTenTriple[nTop];
1645 memset(FreeTopTenTriple, 0, sizeof(FreeTopTenTriple));
1646
1647 for (unsigned int ix = 0; ix < alloc_freeBlocks-1; ix++) {
1648 // If there are stubs in the gap, this gap will never become completely free.
1649 // The triple will thus never merge to one free block.
1650 unsigned int lenTriple = FreeArray[ix].len + (FreeArray[ix].stubs_in_gap ? 0 : FreeArray[ix].gap + FreeArray[ix+1].len);
1651 FreeArray[ix].len = lenTriple;
1652 if (lenTriple > currMax10) { // larger than the ten largest found so far
1653
1654 unsigned int iy;
1655 for (iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1660 FreeTopTenTriple[iy] = &FreeArray[ix];
1661 if (FreeTopTenTriple[nTop-1] != NULL) {
1662 currMax10 = FreeTopTenTriple[nTop-1]->len;
1663 }
1664 break;
1665 }
1666 }
1667 if (iy == nTop) {
1668 ast->print_cr("Internal logic error. New Max10 = %d detected, but could not be merged. Old Max10 = %d",
1669 lenTriple, currMax10);
1670 continue;
1671 }
1672 if (FreeTopTenTriple[iy] == NULL) {
1673 FreeTopTenTriple[iy] = &FreeArray[ix];
1674 if (iy == (nTop-1)) {
1675 currMax10 = lenTriple;
1676 }
1677 }
1678 }
1679 }
1680 BUFFEREDSTREAM_FLUSH_AUTO("")
1681
1682 {
1683 printBox(ast, '-', "Top Ten Free-Occupied-Free Triples in ", heapName);
1684 ast->print_cr(" Use this information to judge how likely it is that a large(r) free block\n"
1685 " might get created by code cache sweeping.\n"
1686 " If all the occupied blocks can be swept, the three free blocks will be\n"
1687 " merged into one (much larger) free block. That would reduce free space\n"
1688 " fragmentation.\n");
1689
1690 //---< print Top Ten Free-Occupied-Free Triples >---
1691 for (unsigned int iy = 0; (iy < nTop) && (FreeTopTenTriple[iy] != NULL); iy++) {
1692 ast->print("Pos %3d: Block %4d - size " HEX32_FORMAT ",", iy+1, FreeTopTenTriple[iy]->index, FreeTopTenTriple[iy]->len);
1693 ast->fill_to(39);
1694 ast->print("Gap (to next) " HEX32_FORMAT ",", FreeTopTenTriple[iy]->gap);
1695 ast->fill_to(63);
1696 ast->print("#blocks (in gap) %d", FreeTopTenTriple[iy]->n_gapBlocks);
1697 ast->cr();
1698 BUFFEREDSTREAM_FLUSH_AUTO("")
1699 }
1700 }
1701 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
1702 }
1703
1704
1705 void CodeHeapState::print_count(outputStream* out, CodeHeap* heap) {
1706 if (!initialization_complete) {
1707 return;
1708 }
1709
1710 const char* heapName = get_heapName(heap);
1711 get_HeapStatGlobals(out, heapName);
1712
1713 if ((StatArray == NULL) || (alloc_granules == 0)) {
1714 return;
1715 }
1716 BUFFEREDSTREAM_DECL(ast, out)
1717
1718 unsigned int granules_per_line = 32;
1719 char* low_bound = heap->low_boundary();
1720
1721 {
1722 printBox(ast, '=', "B L O C K C O U N T S for ", heapName);
1723 ast->print_cr(" Each granule contains an individual number of heap blocks. Large blocks\n"
1724 " may span multiple granules and are counted for each granule they touch.\n");
1725 if (segment_granules) {
1726 ast->print_cr(" You have selected granule size to be as small as segment size.\n"
1727 " As a result, each granule contains exactly one block (or a part of one block)\n"
1728 " or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1729 " Occupied granules show their BlobType character, see legend.\n");
1730 print_blobType_legend(ast);
1731 }
1732 BUFFEREDSTREAM_FLUSH_LOCKED("")
1733 }
1734
1735 {
1736 if (segment_granules) {
1737 printBox(ast, '-', "Total (all types) count for granule size == segment size", NULL);
1738
1739 granules_per_line = 128;
1740 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1741 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1742 print_blobType_single(ast, StatArray[ix].type);
1743 }
1744 } else {
1745 printBox(ast, '-', "Total (all tiers) count, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1746
1747 granules_per_line = 128;
1748 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1749 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1750 unsigned int count = StatArray[ix].t1_count + StatArray[ix].t2_count + StatArray[ix].tx_count
1751 + StatArray[ix].stub_count + StatArray[ix].dead_count;
1752 print_count_single(ast, count);
1753 }
1754 }
1755 BUFFEREDSTREAM_FLUSH_LOCKED("|\n\n\n")
1756 }
1757
1758 {
1759 if (nBlocks_t1 > 0) {
1760 printBox(ast, '-', "Tier1 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1761
1762 granules_per_line = 128;
1763 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1764 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1765 if (segment_granules && StatArray[ix].t1_count > 0) {
1766 print_blobType_single(ast, StatArray[ix].type);
1767 } else {
1768 print_count_single(ast, StatArray[ix].t1_count);
1769 }
1770 }
1771 ast->print("|");
1772 } else {
1773 ast->print("No Tier1 nMethods found in CodeHeap.");
1774 }
1775 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1776 }
1777
1778 {
1779 if (nBlocks_t2 > 0) {
1780 printBox(ast, '-', "Tier2 nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1781
1782 granules_per_line = 128;
1783 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1784 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1785 if (segment_granules && StatArray[ix].t2_count > 0) {
1786 print_blobType_single(ast, StatArray[ix].type);
1787 } else {
1788 print_count_single(ast, StatArray[ix].t2_count);
1789 }
1790 }
1791 ast->print("|");
1792 } else {
1793 ast->print("No Tier2 nMethods found in CodeHeap.");
1794 }
1795 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1796 }
1797
1798 {
1799 if (nBlocks_alive > 0) {
1800 printBox(ast, '-', "not_used/not_entrant/not_installed nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1801
1802 granules_per_line = 128;
1803 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1804 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1805 if (segment_granules && StatArray[ix].tx_count > 0) {
1806 print_blobType_single(ast, StatArray[ix].type);
1807 } else {
1808 print_count_single(ast, StatArray[ix].tx_count);
1809 }
1810 }
1811 ast->print("|");
1812 } else {
1813 ast->print("No not_used/not_entrant nMethods found in CodeHeap.");
1814 }
1815 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1816 }
1817
1818 {
1819 if (nBlocks_stub > 0) {
1820 printBox(ast, '-', "Stub & Blob count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1821
1822 granules_per_line = 128;
1823 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1824 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1825 if (segment_granules && StatArray[ix].stub_count > 0) {
1826 print_blobType_single(ast, StatArray[ix].type);
1827 } else {
1828 print_count_single(ast, StatArray[ix].stub_count);
1829 }
1830 }
1831 ast->print("|");
1832 } else {
1833 ast->print("No Stubs and Blobs found in CodeHeap.");
1834 }
1835 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1836 }
1837
1838 {
1839 if (nBlocks_dead > 0) {
1840 printBox(ast, '-', "Dead nMethod count only, 0x1..0xf. '*' indicates >= 16 blocks, ' ' indicates empty", NULL);
1841
1842 granules_per_line = 128;
1843 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1844 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1845 if (segment_granules && StatArray[ix].dead_count > 0) {
1846 print_blobType_single(ast, StatArray[ix].type);
1847 } else {
1848 print_count_single(ast, StatArray[ix].dead_count);
1849 }
1850 }
1851 ast->print("|");
1852 } else {
1853 ast->print("No dead nMethods found in CodeHeap.");
1854 }
1855 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1856 }
1857
1858 {
1859 if (!segment_granules) { // Prevent totally redundant printouts
1860 printBox(ast, '-', "Count by tier (combined, no dead blocks): <#t1>:<#t2>:<#s>, 0x0..0xf. '*' indicates >= 16 blocks", NULL);
1861
1862 granules_per_line = 24;
1863 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1864 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1865
1866 print_count_single(ast, StatArray[ix].t1_count);
1867 ast->print(":");
1868 print_count_single(ast, StatArray[ix].t2_count);
1869 ast->print(":");
1870 if (segment_granules && StatArray[ix].stub_count > 0) {
1871 print_blobType_single(ast, StatArray[ix].type);
1872 } else {
1873 print_count_single(ast, StatArray[ix].stub_count);
1874 }
1875 ast->print(" ");
1876 }
1877 BUFFEREDSTREAM_FLUSH_LOCKED("|\n\n\n")
1878 }
1879 }
1880 }
1881
1882
1883 void CodeHeapState::print_space(outputStream* out, CodeHeap* heap) {
1884 if (!initialization_complete) {
1885 return;
1886 }
1887
1888 const char* heapName = get_heapName(heap);
1889 get_HeapStatGlobals(out, heapName);
1890
1891 if ((StatArray == NULL) || (alloc_granules == 0)) {
1892 return;
1893 }
1894 BUFFEREDSTREAM_DECL(ast, out)
1895
1896 unsigned int granules_per_line = 32;
1897 char* low_bound = heap->low_boundary();
1898
1899 {
1900 printBox(ast, '=', "S P A C E U S A G E & F R A G M E N T A T I O N for ", heapName);
1901 ast->print_cr(" The heap space covered by one granule is occupied to a various extend.\n"
1902 " The granule occupancy is displayed by one decimal digit per granule.\n");
1903 if (segment_granules) {
1904 ast->print_cr(" You have selected granule size to be as small as segment size.\n"
1905 " As a result, each granule contains exactly one block (or a part of one block)\n"
1906 " or is displayed as empty (' ') if it's BlobType does not match the selection.\n"
1907 " Occupied granules show their BlobType character, see legend.\n");
1908 print_blobType_legend(ast);
1909 } else {
1910 ast->print_cr(" These digits represent a fill percentage range (see legend).\n");
1911 print_space_legend(ast);
1912 }
1913 BUFFEREDSTREAM_FLUSH_LOCKED("")
1914 }
1915
1916 {
1917 if (segment_granules) {
1918 printBox(ast, '-', "Total (all types) space consumption for granule size == segment size", NULL);
1919
1920 granules_per_line = 128;
1921 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1922 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1923 print_blobType_single(ast, StatArray[ix].type);
1924 }
1925 } else {
1926 printBox(ast, '-', "Total (all types) space consumption. ' ' indicates empty, '*' indicates full.", NULL);
1927
1928 granules_per_line = 128;
1929 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1930 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1931 unsigned int space = StatArray[ix].t1_space + StatArray[ix].t2_space + StatArray[ix].tx_space
1932 + StatArray[ix].stub_space + StatArray[ix].dead_space;
1933 print_space_single(ast, space);
1934 }
1935 }
1936 BUFFEREDSTREAM_FLUSH_LOCKED("|\n\n\n")
1937 }
1938
1939 {
1940 if (nBlocks_t1 > 0) {
1941 printBox(ast, '-', "Tier1 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1942
1943 granules_per_line = 128;
1944 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1945 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1946 if (segment_granules && StatArray[ix].t1_space > 0) {
1947 print_blobType_single(ast, StatArray[ix].type);
1948 } else {
1949 print_space_single(ast, StatArray[ix].t1_space);
1950 }
1951 }
1952 ast->print("|");
1953 } else {
1954 ast->print("No Tier1 nMethods found in CodeHeap.");
1955 }
1956 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1957 }
1958
1959 {
1960 if (nBlocks_t2 > 0) {
1961 printBox(ast, '-', "Tier2 space consumption. ' ' indicates empty, '*' indicates full", NULL);
1962
1963 granules_per_line = 128;
1964 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1965 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1966 if (segment_granules && StatArray[ix].t2_space > 0) {
1967 print_blobType_single(ast, StatArray[ix].type);
1968 } else {
1969 print_space_single(ast, StatArray[ix].t2_space);
1970 }
1971 }
1972 ast->print("|");
1973 } else {
1974 ast->print("No Tier2 nMethods found in CodeHeap.");
1975 }
1976 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1977 }
1978
1979 {
1980 if (nBlocks_alive > 0) {
1981 printBox(ast, '-', "not_used/not_entrant/not_installed space consumption. ' ' indicates empty, '*' indicates full", NULL);
1982
1983 granules_per_line = 128;
1984 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
1985 print_line_delim(out, ast, low_bound, ix, granules_per_line);
1986 if (segment_granules && StatArray[ix].tx_space > 0) {
1987 print_blobType_single(ast, StatArray[ix].type);
1988 } else {
1989 print_space_single(ast, StatArray[ix].tx_space);
1990 }
1991 }
1992 ast->print("|");
1993 } else {
1994 ast->print("No Tier2 nMethods found in CodeHeap.");
1995 }
1996 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
1997 }
1998
1999 {
2000 if (nBlocks_stub > 0) {
2001 printBox(ast, '-', "Stub and Blob space consumption. ' ' indicates empty, '*' indicates full", NULL);
2002
2003 granules_per_line = 128;
2004 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2005 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2006 if (segment_granules && StatArray[ix].stub_space > 0) {
2007 print_blobType_single(ast, StatArray[ix].type);
2008 } else {
2009 print_space_single(ast, StatArray[ix].stub_space);
2010 }
2011 }
2012 ast->print("|");
2013 } else {
2014 ast->print("No Stubs and Blobs found in CodeHeap.");
2015 }
2016 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2017 }
2018
2019 {
2020 if (nBlocks_dead > 0) {
2021 printBox(ast, '-', "Dead space consumption. ' ' indicates empty, '*' indicates full", NULL);
2022
2023 granules_per_line = 128;
2024 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2025 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2026 print_space_single(ast, StatArray[ix].dead_space);
2027 }
2028 ast->print("|");
2029 } else {
2030 ast->print("No dead nMethods found in CodeHeap.");
2031 }
2032 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2033 }
2034
2035 {
2036 if (!segment_granules) { // Prevent totally redundant printouts
2037 printBox(ast, '-', "Space consumption by tier (combined): <t1%>:<t2%>:<s%>. ' ' indicates empty, '*' indicates full", NULL);
2038
2039 granules_per_line = 24;
2040 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2041 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2042
2043 if (segment_granules && StatArray[ix].t1_space > 0) {
2044 print_blobType_single(ast, StatArray[ix].type);
2045 } else {
2046 print_space_single(ast, StatArray[ix].t1_space);
2047 }
2048 ast->print(":");
2049 if (segment_granules && StatArray[ix].t2_space > 0) {
2050 print_blobType_single(ast, StatArray[ix].type);
2051 } else {
2052 print_space_single(ast, StatArray[ix].t2_space);
2053 }
2054 ast->print(":");
2055 if (segment_granules && StatArray[ix].stub_space > 0) {
2056 print_blobType_single(ast, StatArray[ix].type);
2057 } else {
2058 print_space_single(ast, StatArray[ix].stub_space);
2059 }
2060 ast->print(" ");
2061 }
2062 ast->print("|");
2063 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2064 }
2065 }
2066 }
2067
2068 void CodeHeapState::print_age(outputStream* out, CodeHeap* heap) {
2069 if (!initialization_complete) {
2070 return;
2071 }
2072
2073 const char* heapName = get_heapName(heap);
2074 get_HeapStatGlobals(out, heapName);
2075
2076 if ((StatArray == NULL) || (alloc_granules == 0)) {
2077 return;
2078 }
2079 BUFFEREDSTREAM_DECL(ast, out)
2080
2081 unsigned int granules_per_line = 32;
2082 char* low_bound = heap->low_boundary();
2083
2084 {
2085 printBox(ast, '=', "M E T H O D A G E by CompileID for ", heapName);
2086 ast->print_cr(" The age of a compiled method in the CodeHeap is not available as a\n"
2087 " time stamp. Instead, a relative age is deducted from the method's compilation ID.\n"
2088 " Age information is available for tier1 and tier2 methods only. There is no\n"
2089 " age information for stubs and blobs, because they have no compilation ID assigned.\n"
2090 " Information for the youngest method (highest ID) in the granule is printed.\n"
2091 " Refer to the legend to learn how method age is mapped to the displayed digit.");
2092 print_age_legend(ast);
2093 BUFFEREDSTREAM_FLUSH_LOCKED("")
2094 }
2095
2096 {
2097 printBox(ast, '-', "Age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2098
2099 granules_per_line = 128;
2100 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2101 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2102 unsigned int age1 = StatArray[ix].t1_age;
2103 unsigned int age2 = StatArray[ix].t2_age;
2104 unsigned int agex = StatArray[ix].tx_age;
2105 unsigned int age = age1 > age2 ? age1 : age2;
2106 age = age > agex ? age : agex;
2107 print_age_single(ast, age);
2108 }
2109 ast->print("|");
2110 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2111 }
2112
2113 {
2114 if (nBlocks_t1 > 0) {
2115 printBox(ast, '-', "Tier1 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2116
2117 granules_per_line = 128;
2118 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2119 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2120 print_age_single(ast, StatArray[ix].t1_age);
2121 }
2122 ast->print("|");
2123 } else {
2124 ast->print("No Tier1 nMethods found in CodeHeap.");
2125 }
2126 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2127 }
2128
2129 {
2130 if (nBlocks_t2 > 0) {
2131 printBox(ast, '-', "Tier2 age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2132
2133 granules_per_line = 128;
2134 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2135 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2136 print_age_single(ast, StatArray[ix].t2_age);
2137 }
2138 ast->print("|");
2139 } else {
2140 ast->print("No Tier2 nMethods found in CodeHeap.");
2141 }
2142 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2143 }
2144
2145 {
2146 if (nBlocks_alive > 0) {
2147 printBox(ast, '-', "not_used/not_entrant/not_installed age distribution. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2148
2149 granules_per_line = 128;
2150 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2151 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2152 print_age_single(ast, StatArray[ix].tx_age);
2153 }
2154 ast->print("|");
2155 } else {
2156 ast->print("No Tier2 nMethods found in CodeHeap.");
2157 }
2158 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2159 }
2160
2161 {
2162 if (!segment_granules) { // Prevent totally redundant printouts
2163 printBox(ast, '-', "age distribution by tier <a1>:<a2>. '0' indicates youngest 1/256, '8': oldest half, ' ': no age information", NULL);
2164
2165 granules_per_line = 32;
2166 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2167 print_line_delim(out, ast, low_bound, ix, granules_per_line);
2168 print_age_single(ast, StatArray[ix].t1_age);
2169 ast->print(":");
2170 print_age_single(ast, StatArray[ix].t2_age);
2171 ast->print(" ");
2172 }
2173 ast->print("|");
2174 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n\n")
2175 }
2176 }
2177 }
2178
2179
2180 void CodeHeapState::print_names(outputStream* out, CodeHeap* heap) {
2181 if (!initialization_complete) {
2182 return;
2183 }
2184
2185 const char* heapName = get_heapName(heap);
2186 get_HeapStatGlobals(out, heapName);
2187
2188 if ((StatArray == NULL) || (alloc_granules == 0)) {
2189 return;
2190 }
2191 BUFFEREDSTREAM_DECL(ast, out)
2192
2193 unsigned int granules_per_line = 128;
2194 char* low_bound = heap->low_boundary();
2195 CodeBlob* last_blob = NULL;
2196 bool name_in_addr_range = true;
2197 bool have_CodeCache_lock = CodeCache_lock->owned_by_self();
2198
2199 //---< print at least 128K per block (i.e. between headers) >---
2200 if (granules_per_line*granule_size < 128*K) {
2201 granules_per_line = (unsigned int)((128*K)/granule_size);
2202 }
2203
2204 printBox(ast, '=', "M E T H O D N A M E S for ", heapName);
2205 ast->print_cr(" Method names are dynamically retrieved from the code cache at print time.\n"
2206 " Due to the living nature of the code heap and because the CodeCache_lock\n"
2207 " is not continuously held, the displayed name might be wrong or no name\n"
2208 " might be found at all. The likelihood for that to happen increases\n"
2209 " over time passed between aggregtion and print steps.\n");
2210 BUFFEREDSTREAM_FLUSH_LOCKED("")
2211
2212 for (unsigned int ix = 0; ix < alloc_granules; ix++) {
2213 //---< print a new blob on a new line >---
2214 if (ix%granules_per_line == 0) {
2215 if (!name_in_addr_range) {
2216 ast->print_cr("No methods, blobs, or stubs found in this address range");
2217 }
2218 name_in_addr_range = false;
2219
2220 size_t end_ix = (ix+granules_per_line <= alloc_granules) ? ix+granules_per_line : alloc_granules;
2221 ast->cr();
2222 ast->print_cr("--------------------------------------------------------------------");
2223 ast->print_cr("Address range [" INTPTR_FORMAT "," INTPTR_FORMAT "), " SIZE_FORMAT "k", p2i(low_bound+ix*granule_size), p2i(low_bound + end_ix*granule_size), (end_ix - ix)*granule_size/(size_t)K);
2224 ast->print_cr("--------------------------------------------------------------------");
2225 BUFFEREDSTREAM_FLUSH_AUTO("")
2226 }
2227 // Only check granule if it contains at least one blob.
2228 unsigned int nBlobs = StatArray[ix].t1_count + StatArray[ix].t2_count + StatArray[ix].tx_count +
2229 StatArray[ix].stub_count + StatArray[ix].dead_count;
2230 if (nBlobs > 0 ) {
2231 for (unsigned int is = 0; is < granule_size; is+=(unsigned int)seg_size) {
2232 // heap->find_start() is safe. Only works on _segmap.
2233 // Returns NULL or void*. Returned CodeBlob may be uninitialized.
2234 char* this_seg = low_bound + ix*granule_size + is;
2235 CodeBlob* this_blob = (CodeBlob*)(heap->find_start(this_seg));
2236 bool blob_is_safe = blob_access_is_safe(this_blob, NULL);
2237 // blob could have been flushed, freed, and merged.
2238 // this_blob < last_blob is an indicator for that.
2239 if (blob_is_safe && (this_blob > last_blob)) {
2240 last_blob = this_blob;
2241
2242 //---< get type and name >---
2243 blobType cbType = noType;
2244 if (segment_granules) {
2245 cbType = (blobType)StatArray[ix].type;
2253 //---< access these fields only if we own the CodeCache_lock >---
2254 const char* blob_name = "<unavailable>";
2255 nmethod* nm = NULL;
2256 if (have_CodeCache_lock) {
2257 blob_name = this_blob->name();
2258 nm = this_blob->as_nmethod_or_null();
2259 // this_blob->name() could return NULL if no name was given to CTOR. Inlined, maybe invisible on stack
2260 if ((blob_name == NULL) || !os::is_readable_pointer(blob_name)) {
2261 blob_name = "<unavailable>";
2262 }
2263 }
2264
2265 //---< print table header for new print range >---
2266 if (!name_in_addr_range) {
2267 name_in_addr_range = true;
2268 ast->fill_to(51);
2269 ast->print("%9s", "compiler");
2270 ast->fill_to(61);
2271 ast->print_cr("%6s", "method");
2272 ast->print_cr("%18s %13s %17s %9s %5s %18s %s", "Addr(module) ", "offset", "size", " type lvl", " temp", "blobType ", "Name");
2273 BUFFEREDSTREAM_FLUSH_AUTO("")
2274 }
2275
2276 //---< print line prefix (address and offset from CodeHeap start) >---
2277 ast->print(INTPTR_FORMAT, p2i(this_blob));
2278 ast->fill_to(19);
2279 ast->print("(+" PTR32_FORMAT ")", (unsigned int)((char*)this_blob-low_bound));
2280 ast->fill_to(33);
2281
2282 // access nmethod and Method fields only if we own the CodeCache_lock.
2283 // This fact is implicitly transported via nm != NULL.
2284 if (CompiledMethod::nmethod_access_is_safe(nm)) {
2285 Method* method = nm->method();
2286 ResourceMark rm;
2287 //---< collect all data to locals as quickly as possible >---
2288 unsigned int total_size = nm->total_size();
2289 int hotness = nm->hotness_counter();
2290 bool get_name = (cbType == nMethod_inuse) || (cbType == nMethod_notused);
2291 //---< nMethod size in hex >---
2292 ast->print(PTR32_FORMAT, total_size);
2293 ast->print("(" SIZE_FORMAT_W(4) "K)", total_size/K);
2309 Symbol* methName = method->name();
2310 const char* methNameS = (methName == NULL) ? NULL : methName->as_C_string();
2311 methNameS = (methNameS == NULL) ? "<method name unavailable>" : methNameS;
2312 Symbol* methSig = method->signature();
2313 const char* methSigS = (methSig == NULL) ? NULL : methSig->as_C_string();
2314 methSigS = (methSigS == NULL) ? "<method signature unavailable>" : methSigS;
2315 ast->print("%s", methNameS);
2316 ast->print("%s", methSigS);
2317 } else {
2318 ast->print("%s", blob_name);
2319 }
2320 } else if (blob_is_safe) {
2321 ast->fill_to(62+6);
2322 ast->print("%s", blobTypeName[cbType]);
2323 ast->fill_to(82+6);
2324 ast->print("%s", blob_name);
2325 } else {
2326 ast->fill_to(62+6);
2327 ast->print("<stale blob>");
2328 }
2329 ast->cr();
2330 BUFFEREDSTREAM_FLUSH_AUTO("")
2331 } else if (!blob_is_safe && (this_blob != last_blob) && (this_blob != NULL)) {
2332 last_blob = this_blob;
2333 }
2334 }
2335 } // nBlobs > 0
2336 }
2337 BUFFEREDSTREAM_FLUSH_LOCKED("\n\n")
2338 }
2339
2340
2341 void CodeHeapState::printBox(outputStream* ast, const char border, const char* text1, const char* text2) {
2342 unsigned int lineLen = 1 + 2 + 2 + 1;
2343 char edge, frame;
2344
2345 if (text1 != NULL) {
2346 lineLen += (unsigned int)strlen(text1); // text1 is much shorter than MAX_INT chars.
2347 }
2348 if (text2 != NULL) {
2349 lineLen += (unsigned int)strlen(text2); // text2 is much shorter than MAX_INT chars.
2350 }
2351 if (border == '-') {
2352 edge = '+';
2353 frame = '|';
2354 } else {
2355 edge = border;
2356 frame = border;
2357 }
2454 if (ix > 0) {
2455 ast->print("|");
2456 }
2457 ast->cr();
2458 assert(out == ast, "must use the same stream!");
2459
2460 ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2461 ast->fill_to(19);
2462 ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2463 }
2464 }
2465
2466 void CodeHeapState::print_line_delim(outputStream* out, bufferedStream* ast, char* low_bound, unsigned int ix, unsigned int gpl) {
2467 assert(out != ast, "must not use the same stream!");
2468 if (ix % gpl == 0) {
2469 if (ix > 0) {
2470 ast->print("|");
2471 }
2472 ast->cr();
2473
2474 // can't use BUFFEREDSTREAM_FLUSH_IF("", 512) here.
2475 // can't use this expression. bufferedStream::capacity() does not exist.
2476 // if ((ast->capacity() - ast->size()) < 512) {
2477 // Assume instead that default bufferedStream capacity (4K) was used.
2478 if (ast->size() > 3*K) {
2479 ttyLocker ttyl;
2480 out->print("%s", ast->as_string());
2481 ast->reset();
2482 }
2483
2484 ast->print(INTPTR_FORMAT, p2i(low_bound + ix*granule_size));
2485 ast->fill_to(19);
2486 ast->print("(+" PTR32_FORMAT "): |", (unsigned int)(ix*granule_size));
2487 }
2488 }
2489
2490 CodeHeapState::blobType CodeHeapState::get_cbType(CodeBlob* cb) {
2491 if ((cb != NULL) && os::is_readable_pointer(cb)) {
2492 if (cb->is_runtime_stub()) return runtimeStub;
2493 if (cb->is_deoptimization_stub()) return deoptimizationStub;
2494 if (cb->is_uncommon_trap_stub()) return uncommonTrapStub;
2495 if (cb->is_exception_stub()) return exceptionStub;
2496 if (cb->is_safepoint_stub()) return safepointStub;
2497 if (cb->is_adapter_blob()) return adapterBlob;
2498 if (cb->is_method_handles_adapter_blob()) return mh_adapterBlob;
|