- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathcompilationMemoryStatistic.cpp
1101 lines (958 loc) · 36.8 KB
/
compilationMemoryStatistic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Red Hat, Inc. and/or its affiliates.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include"code/nmethod.hpp"
#include"compiler/abstractCompiler.hpp"
#include"compiler/compilationMemStatInternals.inline.hpp"
#include"compiler/compilerDefinitions.inline.hpp"
#include"compiler/compilerDirectives.hpp"
#include"compiler/compilerOracle.hpp"
#include"compiler/compilerThread.hpp"
#include"compiler/compileTask.hpp"
#include"logging/log.hpp"
#include"logging/logStream.hpp"
#include"memory/arena.hpp"
#include"nmt/nmtCommon.hpp"
#include"oops/method.inline.hpp"
#include"oops/symbol.hpp"
#include"runtime/atomic.hpp"
#include"runtime/mutexLocker.hpp"
#include"runtime/os.hpp"
#include"utilities/checkedCast.hpp"
#include"utilities/debug.hpp"
#include"utilities/globalDefinitions.hpp"
#include"utilities/ostream.hpp"
#ifdef COMPILER1
#include"c1/c1_Compilation.hpp"
#endif
#ifdef COMPILER2
#include"opto/compile.hpp"
#include"opto/node.hpp"// compile.hpp is not self-contained
#endif
staticconstchar* phase_trc_id_to_string(int phase_trc_id) {
returnCOMPILER2_PRESENT(Phase::get_phase_trace_id_text((Phase::PhaseTraceId)phase_trc_id))
NOT_COMPILER2("");
}
// If crash option on memlimit is enabled and an oom occurred, the stats for the
// first offending compilation.
static ArenaStatCounter* volatile _arenastat_oom_crash = nullptr;
// Arena-chunk stamping
unionchunkstamp_t {
uint64_t raw;
struct {
uint32_t tracked;
uint16_t arena_tag;
uint16_t phase_id;
};
};
STATIC_ASSERT(sizeof(chunkstamp_t) == sizeof(chunkstamp_t::raw));
ArenaCounterTable::ArenaCounterTable() {
memset(_v, 0, sizeof(_v));
}
voidArenaCounterTable::copy_from(const ArenaCounterTable& other) {
memcpy(_v, other._v, sizeof(_v));
}
voidArenaCounterTable::summarize(size_t out[arena_tag_max]) const {
memset(out, 0, arena_tag_max * sizeof(size_t));
for (int i = 0; i < phase_trc_id_max; i++) {
for (int j = 0; j < arena_tag_max; j++) {
out[j] += _v[i][j];
}
}
}
voidArenaCounterTable::print_on(outputStream* st) const {
bool header_printed = false;
for (int phase_trc_id = 0; phase_trc_id < phase_trc_id_max; phase_trc_id++) {
size_t sum = 0;
for (int arena_tag = 0; arena_tag < arena_tag_max; arena_tag++) {
sum += at(phase_trc_id, arena_tag);
}
if (sum > 0) { // omit phases that did not contribute to allocation load
if (!header_printed) {
st->print("%-24s %10s", "Phase", "Total");
for (int arena_tag = 0; arena_tag < arena_tag_max; arena_tag++) {
st->print("%10s", Arena::tag_name[arena_tag]);
}
st->cr();
header_printed = true;
}
st->print("%-24s ", phase_trc_id_to_string(phase_trc_id));
st->print("%10zu", sum);
for (int arena_tag = 0; arena_tag < arena_tag_max; arena_tag++) {
constsize_t v = at(phase_trc_id, arena_tag);
st->print("%10zu", v);
}
st->cr();
}
}
}
// When reporting phase footprint movements, if phase-local peak over start as well over end
// was larger than this threshold, we report it.
staticconstexprsize_t significant_peak_threshold = M;
FootprintTimeline::FootprintTimeline() {
DEBUG_ONLY(_inbetween_phases = true;)
}
voidFootprintTimeline::copy_from(const FootprintTimeline& other) {
_fifo.copy_from(other._fifo);
DEBUG_ONLY(_inbetween_phases = other._inbetween_phases;)
}
voidFootprintTimeline::print_on(outputStream* st) const {
constint start_indent = st->indentation();
if (!_fifo.empty()) {
// .123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789
st->print_cr("Phase seq. number Bytes Nodes");
unsigned from = 0;
if (_fifo.lost() > 0) {
st->print_cr(" (" UINT64_FORMAT " older entries lost)", _fifo.lost());
}
int last_level = 0;
int last_num = 0; // see if we are regressing
auto printer = [&](const Entry& e) {
int col = start_indent;
check_phase_trace_id(e.info.id);
st->print("%*s", e.level,
((e.level < last_level) ? "<" : ((e.level > last_level) ? ">" : ""))
);
last_level = e.level;
st->print("%d ", e.info.num);
if (e.info.num < last_num) {
st->print("(cont.) ");
}
last_num = e.info.num;
col += 15; st->fill_to(col);
st->print("%24s", e.info.text);
col += 25; st->fill_to(col);
char tmp[64];
os::snprintf(tmp, sizeof(tmp), "%9zu (%+zd)", e._bytes.cur, e._bytes.end_delta());
st->print("%s ", tmp); // end
col += 21; st->fill_to(col);
os::snprintf(tmp, sizeof(tmp), "%6u (%+d)", e._live_nodes.cur, e._live_nodes.end_delta());
st->print("%s ", tmp); // end
if (e._bytes.temporary_peak_size() > significant_peak_threshold) {
col += 20; st->fill_to(col);
st->print(" significant temporary peak: %zu (%+zd)", e._bytes.peak, (ssize_t)e._bytes.peak - e._bytes.start); // peak
}
st->cr();
};
_fifo.iterate_all(printer);
}
}
voidFootprintTimeline::on_phase_end(size_t cur_abs, unsigned cur_nodes) {
const Entry& old = _fifo.current();
// One last counter update in old phase:
// We see all allocations, so cur_abs given should correspond to our topmost cur.
// But the same does not hold for nodes, since we only get updated when node allocation
// would cause a new arena chunk to be born. Node allocations that don't cause arena
// chunks (the vast majority) fly by us.
assert(old._bytes.cur == cur_abs, "miscount");
on_footprint_change(cur_abs, cur_nodes);
// Close old, open new entry
_fifo.advance();
DEBUG_ONLY(_inbetween_phases = true;)
}
voidFootprintTimeline::on_phase_start(PhaseInfo info, size_t cur_abs, unsigned cur_nodes, int level) {
if (!_fifo.empty() && _fifo.last().info.id == info.id && _fifo.last().level == level) {
// Two phases with the same id are collapsed if they were not interleaved by another phase
_fifo.revert();
// We now just continue bookkeeping into the last entry
} else {
// seed current entry
Entry& e = _fifo.current();
e._bytes.init(cur_abs);
e._live_nodes.init(cur_nodes);
e.info = info;
e.level = level;
}
DEBUG_ONLY(_inbetween_phases = false;)
}
FullMethodName::FullMethodName() : _k(nullptr), _m(nullptr), _s(nullptr) {}
FullMethodName::FullMethodName(const Method* m) :
_k(m->klass_name()), _m(m->name()), _s(m->signature()) {};
FullMethodName::FullMethodName(const FullMethodName& o) : _k(o._k), _m(o._m), _s(o._s) {}
FullMethodName& FullMethodName::operator=(const FullMethodName& o) {
_k = o._k; _m = o._m; _s = o._s;
return *this;
}
voidFullMethodName::make_permanent() {
_k->make_permanent();
_m->make_permanent();
_s->make_permanent();
}
voidFullMethodName::print_on(outputStream* st) const {
char tmp[1024];
st->print_raw(_k->as_C_string(tmp, sizeof(tmp)));
st->print_raw("::");
st->print_raw(_m->as_C_string(tmp, sizeof(tmp)));
st->put('(');
st->print_raw(_s->as_C_string(tmp, sizeof(tmp)));
st->put(')');
}
char* FullMethodName::as_C_string(char* buf, size_t len) const {
stringStream ss(buf, len);
print_on(&ss);
return buf;
}
bool FullMethodName::operator== (const FullMethodName& b) const {
return _k == b._k && _m == b._m && _s == b._s;
}
#ifdef ASSERT
boolFullMethodName::is_test_class() const {
char tmp[1024];
_k->as_C_string(tmp, sizeof(tmp));
returnstrstr(tmp, "CompileCommandPrintMemStat") != nullptr ||
strstr(tmp, "CompileCommandMemLimit") != nullptr;
}
#endif// ASSERT
ArenaStatCounter::ArenaStatCounter(const CompileTask* task, size_t limit) :
_fmn(task->method()),
_should_print_memstat(task->directive()->should_print_memstat()),
_should_crash_on_memlimit(task->directive()->should_crash_at_mem_limit()),
_current(0), _peak(0), _live_nodes_current(0), _live_nodes_at_global_peak(0),
_limit(limit), _hit_limit(false), _limit_in_process(false),
_phase_counter(0), _comp_type(task->compiler()->type()), _comp_id(task->compile_id())
DEBUG_ONLY(COMMA _is_test_class(false))
{
_fmn.make_permanent();
#ifdef ASSERT
// If the class name matches the JTreg test class, we are in test mode and
// will do some test allocations to test the statistic
_is_test_class = _fmn.is_test_class();
#endif// ASSERT
}
voidArenaStatCounter::on_phase_start(PhaseInfo info) {
// Update node counter
_live_nodes_current = retrieve_live_node_count();
// For the timeline, when nesting TracePhase happens, we maintain the illusion of a flat succession of
// separate phases. Thus, { TracePhase p1; { TracePhase p2; }} will be seen as:
// P1 starts -> P1 ends -> P2 starts -> P2 ends -> P1 starts -> P1 ends
// In other words, when a child phase interrupts a parent phase, it "ends" the parent phase, which will
// be "restarted" when the child phase ends.
// This is the only way to get a per-phase timeline that makes any sort of sense.
if (!_phase_info_stack.empty()) {
_timeline.on_phase_end(_current, _live_nodes_current);
}
_phase_info_stack.push(info);
_timeline.on_phase_start(info, _current, _live_nodes_current, _phase_info_stack.depth());
}
voidArenaStatCounter::on_phase_end() {
PhaseInfo top = _phase_info_stack.top();
_phase_info_stack.pop();
_live_nodes_current = retrieve_live_node_count();
_timeline.on_phase_end(_current, _live_nodes_current);
if (!_phase_info_stack.empty()) {
// "restart" parent phase in timeline
_timeline.on_phase_start(_phase_info_stack.top(), _current, _live_nodes_current, _phase_info_stack.depth());
}
}
intArenaStatCounter::retrieve_live_node_count() const {
int result = 0;
#ifdef COMPILER2
if (_comp_type == compiler_c2) {
// Update C2 node count
// Careful, Compile::current() may be null in a short time window when Compile itself
// is still being constructed.
if (Compile::current() != nullptr) {
result = Compile::current()->live_nodes();
}
}
#endif// COMPILER2
return result;
}
// Account an arena allocation. Returns true if new peak reached.
boolArenaStatCounter::on_arena_chunk_allocation(size_t size, int arena_tag, uint64_t* stamp) {
bool rc = false;
constsize_t old_current = _current;
_current += size;
assert(_current >= old_current, "Overflow");
constint phase_trc_id = _phase_info_stack.top().id;
_counters_current.add(size, phase_trc_id, arena_tag);
_live_nodes_current = retrieve_live_node_count();
_timeline.on_footprint_change(_current, _live_nodes_current);
// Did we reach a global peak?
if (_current > _peak) {
_peak = _current;
// snapshot all current counters
_counters_at_global_peak.copy_from(_counters_current);
// snapshot live nodes
_live_nodes_at_global_peak = _live_nodes_current;
// Did we hit the memory limit?
if (!_hit_limit && _limit > 0 && _peak > _limit) {
_hit_limit = true;
}
// report peak back
rc = true;
}
// calculate arena chunk stamp
chunkstamp_t cs;
cs.tracked = 1;
cs.arena_tag = checked_cast<uint16_t>(arena_tag);
cs.phase_id = checked_cast<uint16_t>(_phase_info_stack.top().id);
*stamp = cs.raw;
return rc;
}
voidArenaStatCounter::on_arena_chunk_deallocation(size_t size, uint64_t stamp) {
assert(_current >= size, "Underflow (%zu %zu)", size, _current);
// Extract tag and phase id from stamp
chunkstamp_t cs;
cs.raw = stamp;
assert(cs.tracked == 1, "Sanity");
constint arena_tag = cs.arena_tag;
assert(arena_tag >= 0 && arena_tag < arena_tag_max, "Arena Tag OOB (%d)", arena_tag_max);
constintphase_trc_id(cs.phase_id);
assert(phase_trc_id >= 0 && phase_trc_id < phase_trc_id_max, "Phase trace id OOB (%d)", phase_trc_id);
_current -= size;
_counters_current.sub(size, phase_trc_id, arena_tag);
_live_nodes_current = retrieve_live_node_count();
_timeline.on_footprint_change(_current, _live_nodes_current);
}
// Used for logging, not for the report table generated with jcmd Compiler.memory
voidArenaStatCounter::print_peak_state_on(outputStream* st) const {
st->print("Total Usage: %zu ", _peak);
if (_peak > 0) {
#ifdef COMPILER1
// C1: print allocations broken down by arena types
if (_comp_type == CompilerType::compiler_c1) {
st->print("[");
size_t sums[arena_tag_max];
_counters_at_global_peak.summarize(sums);
bool print_comma = false;
for (int i = 0; i < arena_tag_max; i++) {
if (sums[i] > 0) {
if (print_comma) {
st->print_raw(", ");
}
st->print("%s %zu", Arena::tag_name[i], sums[i]);
print_comma = true;
}
}
st->print_cr("]");
}
#endif// COMPILER1
#ifdef COMPILER2
// C2: print counters and timeline on multiple lines, indented
if (_comp_type == CompilerType::compiler_c2) {
streamIndentor si(st, 4);
st->cr();
st->print_cr("--- Arena Usage by Arena Type and compilation phase, at arena usage peak of %zu ---", _peak);
{
streamIndentor si(st, 4);
_counters_at_global_peak.print_on(st);
}
st->print_cr("--- Allocation timelime by phase ---");
{
streamIndentor si(st, 4);
_timeline.print_on(st);
}
st->print_cr("---");
}
#endif
} else {
st->cr();
}
}
classMemStatEntry : publicCHeapObj<mtCompiler> {
FullMethodName _fmn;
CompilerType _comp_type;
int _comp_id;
double _time;
// Compiling thread. Only for diagnostic purposes. Thread may not be alive anymore.
const Thread* _thread;
// active limit for this compilation, if any
size_t _limit;
// true if the compilation hit the limit
bool _hit_limit;
// result as reported by compiler
constchar* _result;
// Bytes total at global peak
size_t _peak;
// Bytes per arena tag.
size_t _peak_composition_per_arena_tag[arena_tag_max];
// Number of live nodes at global peak (C2 only)
unsigned _live_nodes_at_global_peak;
structDetails {
ArenaCounterTable counters_at_global_peak;
FootprintTimeline timeline;
};
Details* _detail_stats;
MemStatEntry(const MemStatEntry& e); // deny
public:
MemStatEntry()
: _comp_type(compiler_none), _comp_id(-1),
_time(0), _thread(nullptr), _limit(0), _hit_limit(false),
_result(nullptr), _peak(0), _live_nodes_at_global_peak(0),
_detail_stats(nullptr) {
}
~MemStatEntry() {
clean_details();
}
voidset_comp_id(int comp_id) { _comp_id = comp_id; }
voidset_comptype(CompilerType comptype) { _comp_type = comptype; }
voidset_current_time() { _time = os::elapsedTime(); }
voidset_current_thread() { _thread = Thread::current(); }
voidset_limit(size_t limit) { _limit = limit; }
voidset_from_state(const ArenaStatCounter* state, bool store_details) {
_fmn = state->fmn();
_comp_type = state->comp_type();
_comp_id = state->comp_id();
_limit = state->limit();
_hit_limit = state->hit_limit();
_peak = state->peak();
_live_nodes_at_global_peak = state->live_nodes_at_global_peak();
state->counters_at_global_peak().summarize(_peak_composition_per_arena_tag);
#ifdef COMPILER2
assert(_detail_stats == nullptr, "should have been cleaned");
if (store_details) {
_detail_stats = NEW_C_HEAP_OBJ(Details, mtCompiler);
_detail_stats->counters_at_global_peak.copy_from(state->counters_at_global_peak());
_detail_stats->timeline.copy_from(state->timeline());
}
#endif// COMPILER2
}
voidclean_details() {
if (_detail_stats != nullptr) {
FREE_C_HEAP_ARRAY(Details, _detail_stats);
_detail_stats = nullptr;
}
}
voidreset() {
clean_details();
_comp_type = CompilerType::compiler_none;
_comp_id = -1;
_limit = _peak = 0;
_live_nodes_at_global_peak = 0;
memset(_peak_composition_per_arena_tag, 0, sizeof(_peak_composition_per_arena_tag));
}
voidset_result(constchar* s) { _result = s; }
size_tpeak() const { return _peak; }
boolis_c1() const { return _comp_type == CompilerType::compiler_c1; }
boolis_c2() const { return _comp_type == CompilerType::compiler_c2; }
staticvoidprint_legend(outputStream* st) {
#defineLEGEND_KEY_FMT"%11s"
st->print_cr("Legend:");
st->print_cr("" LEGEND_KEY_FMT ": %s", "ctype", "compiler type");
st->print_cr("" LEGEND_KEY_FMT ": %s", "total", "peak memory allocated via arenas while compiling");
for (int tag = 0; tag < arena_tag_max; tag++) {
st->print_cr("" LEGEND_KEY_FMT ": %s", Arena::tag_name[tag], Arena::tag_desc[tag]);
}
st->print_cr("" LEGEND_KEY_FMT ": %s", "#nodes", "...how many nodes (c2 only)");
st->print_cr("" LEGEND_KEY_FMT ": %s", "result", "Result reported by compiler");
st->print_cr("" LEGEND_KEY_FMT ": %s", "limit", "memory limit; followed by \"*\" if the limit was hit");
st->print_cr("" LEGEND_KEY_FMT ": %s", "time", "timestamp");
st->print_cr("" LEGEND_KEY_FMT ": %s", "id", "compile id");
st->print_cr("" LEGEND_KEY_FMT ": %s", "thread", "compiler thread");
#undef LEGEND_KEY_FMT
}
staticvoidprint_header(outputStream* st) {
st->print("%-6s", "ctyp");
#defineSIZE_FMT"%-10s"
st->print(SIZE_FMT, "total");
for (int tag = 0; tag < arena_tag_max; tag++) {
st->print(SIZE_FMT, Arena::tag_name[tag]);
}
#defineHDR_FMT1"%-8s%-8s%-10s%-8s"
#defineHDR_FMT2"%-6s%-19s%s"
st->print(HDR_FMT1, "#nodes", "result", "limit", "time");
st->print(HDR_FMT2, "id", "thread", "method");
}
voidprint_brief_oneline(outputStream* st) const {
int col = st->indentation();
// Type
st->print("%2s ", compilertype2name(_comp_type));
col += 6; st->fill_to(col);
// Total
size_t v = _peak;
st->print("%zu ", v);
col += 10; st->fill_to(col);
for (int tag = 0; tag < arena_tag_max; tag++) {
v = _peak_composition_per_arena_tag[tag];
st->print("%zu ", v);
col += 10; st->fill_to(col);
}
// Number of Nodes when memory peaked
if (_live_nodes_at_global_peak > 0) {
st->print("%u ", _live_nodes_at_global_peak);
} else {
st->print("-");
}
col += 8; st->fill_to(col);
// result?
st->print("%s ", _result ? _result : "");
col += 8; st->fill_to(col);
// Limit
if (_limit > 0) {
st->print("%zu%s ", _limit, _hit_limit ? "*" : "");
} else {
st->print("-");
}
col += 10; st->fill_to(col);
// TimeStamp
st->print("%.3f ", _time);
col += 8; st->fill_to(col);
// Compile ID
st->print("%d ", _comp_id);
col += 6; st->fill_to(col);
// Thread
st->print(PTR_FORMAT "", p2i(_thread));
// MethodName
char buf[1024];
st->print("%s ", _fmn.as_C_string(buf, sizeof(buf)));
st->cr();
}
voidprint_detailed(outputStream* st) const {
int col = 0;
constexprint indent1 = 40;
constexprint indent2 = 50;
char buf[1024];
st->print_cr("Method : %s", _fmn.as_C_string(buf, sizeof(buf)));
st->print_cr("Compiler : %2s", compilertype2name(_comp_type));
st->print( "Arena Usage at peak : %zu", _peak);
if (_peak > M) {
st->print(" (%.2fM)", ((double)_peak/(double)M));
}
st->cr();
if (_comp_type == CompilerType::compiler_c2) {
st->print_cr("Nodes at peak : %u", _live_nodes_at_global_peak);
}
st->print_cr("Compile ID : %d", _comp_id);
st->print( "Result : %s", _result);
if (strcmp(_result, "oom") == 0) {
st->print(" (memory limit was: %zu)", _limit);
}
st->cr();
st->print_cr("Thread : " PTR_FORMAT, p2i(_thread));
st->print_cr("Timestamp : %.3f", _time);
if (_detail_stats != nullptr) {
st->cr();
st->print_cr("Arena Usage by Arena Type and compilation phase, at arena usage peak of %zu:", _peak);
_detail_stats->counters_at_global_peak.print_on(st);
st->cr();
st->print_cr("Allocation timelime by phase:");
_detail_stats->timeline.print_on(st);
} else {
st->cr();
st->print_cr("Arena Usage by Arena Type, at arena usage peak of %zu:", _peak);
for (int tag = 0; tag < arena_tag_max; tag++) {
constsize_t v = _peak_composition_per_arena_tag[tag];
if (v > 0) {
st->print_cr("%-36s: %zu ", Arena::tag_desc[tag], v);
}
}
}
}
};
classMemStatStore : publicCHeapObj<mtCompiler> {
// Total number of entries. Reaching this limit, we discard the least interesting (smallest allocation size) first.
staticconstexprint max_entries = 64;
struct {
size_t s; MemStatEntry* e;
} _entries[max_entries];
structiteration_result { unsigned num, num_c1, num_c2, num_filtered_out; };
template<typename F>
voiditerate_sorted_filtered(F f, size_t minsize, int max_num_printed, iteration_result& result) const {
assert_lock_strong(NMTCompilationCostHistory_lock);
constunsigned stop_after = max_num_printed == -1 ? UINT_MAX : (unsigned)max_num_printed;
result.num = result.num_c1 = result.num_c2 = result.num_filtered_out = 0;
for (int i = 0; i < max_entries && _entries[i].e != nullptr && result.num < stop_after; i++) {
if (_entries[i].s >= minsize) {
f(_entries[i].e);
result.num++;
result.num_c1 += _entries[i].e->is_c1() ? 1 : 0;
result.num_c2 += _entries[i].e->is_c2() ? 1 : 0;
} else {
result.num_filtered_out++;
}
}
}
voidprint_footer(outputStream* st, size_t minsize, const iteration_result& result) const {
if (result.num > 0) {
st->print_cr("Total: %u (C1: %u, C2: %u)", result.num, result.num_c1, result.num_c2);
} else {
st->print_cr("No entries.");
}
if (result.num_filtered_out > 0) {
st->print_cr(" (%d compilations smaller than %zu omitted)", result.num_filtered_out, minsize);
}
}
public:
MemStatStore() {
memset(_entries, 0, sizeof(_entries));
}
voidadd(const ArenaStatCounter* state, constchar* result) {
constsize_t size = state->peak();
// search insert point
int i = 0;
while (i < max_entries && _entries[i].s > size) {
i++;
}
if (i == max_entries) {
return;
}
MemStatEntry* e = _entries[max_entries - 1].e; // recycle last one
if (e == nullptr) {
e = newMemStatEntry();
}
memmove(_entries + i + 1, _entries + i, sizeof(_entries[0]) * (max_entries - i - 1));
e->reset();
e->set_current_time();
e->set_current_thread();
e->set_result(result);
// Since we don't have phases in C1, for now we just avoid saving details for C1.
constbool save_details = state->comp_type() == CompilerType::compiler_c2;
e->set_from_state(state, save_details);
_entries[i].s = e->peak();
_entries[i].e = e;
}
voidprint_table(outputStream* st, bool legend, size_t minsize, int max_num_printed) const {
assert_lock_strong(NMTCompilationCostHistory_lock);
if (legend) {
MemStatEntry::print_legend(st);
st->cr();
}
MemStatEntry::print_header(st);
st->cr();
iteration_result itres;
auto printer = [&](const MemStatEntry* e) {
e->print_brief_oneline(st);
};
iterate_sorted_filtered(printer, minsize, max_num_printed, itres);
print_footer(st, minsize, itres);
}
voidprint_details(outputStream* st, size_t minsize, int max_num_printed) const {
assert_lock_strong(NMTCompilationCostHistory_lock);
iteration_result itres;
auto printer = [&](const MemStatEntry* e) {
e->print_detailed(st);
st->cr();
st->print_cr("------------------------");
st->cr();
};
iterate_sorted_filtered(printer, minsize, max_num_printed, itres);
}
};
bool CompilationMemoryStatistic::_enabled = false;
static MemStatStore* _the_store = nullptr;
voidCompilationMemoryStatistic::initialize() {
assert(_enabled == false && _the_store == nullptr, "Only once");
_the_store = new MemStatStore;
_enabled = true;
log_info(compilation, alloc)("Compilation memory statistic enabled");
}
voidCompilationMemoryStatistic::on_start_compilation(const DirectiveSet* directive) {
assert(enabled(), "Not enabled?");
assert(directive->should_collect_memstat(), "Don't call if not needed");
CompilerThread* const th = Thread::current()->as_Compiler_thread();
CompileTask* const task = th->task();
constsize_t limit = directive->mem_limit();
// Create new ArenaStat object and hook it into the thread
assert(th->arena_stat() == nullptr, "Sanity");
ArenaStatCounter* const arena_stat = newArenaStatCounter(task, limit);
th->set_arenastat(arena_stat);
// Start a "root" phase
PhaseInfo info;
info.id = phase_trc_id_none;
info.num = 0;
info.text = "(outside)";
arena_stat->on_phase_start(info);
}
voidCompilationMemoryStatistic::on_end_compilation() {
assert(enabled(), "Not enabled?");
CompilerThread* const th = Thread::current()->as_Compiler_thread();
ArenaStatCounter* const arena_stat = th->arena_stat();
if (arena_stat == nullptr) { // not started
return;
}
// Mark end of compilation by clearing out the arena state object in the CompilerThread.
// Do this before the final "phase end".
th->set_arenastat(nullptr);
// End final outer phase.
arena_stat->on_phase_end();
CompileTask* const task = th->task();
assert(task->compile_id() == arena_stat->comp_id(), "Different compilation?");
const Method* const m = th->task()->method();
const DirectiveSet* directive = th->task()->directive();
assert(directive->should_collect_memstat(), "Should only be called if memstat is enabled for this method");
constbool print = directive->should_print_memstat();
// Store memory used in task, for later processing by JFR
task->set_arena_bytes(arena_stat->peak());
// Store result (ok, failed, oom...)
// For this to work, we must call on_end_compilation() at a point where
// Compile|Compilation already handed over the failure string to ciEnv,
// but ciEnv must still be alive.
constchar* result = "ok"; // ok
const ciEnv* const env = th->env();
if (env) {
constchar* const failure_reason = env->failure_reason();
if (failure_reason != nullptr) {
result = (strcmp(failure_reason, failure_reason_memlimit()) == 0) ? "oom" : "err";
}
}
{
MutexLocker ml(NMTCompilationCostHistory_lock, Mutex::_no_safepoint_check_flag);
assert(_the_store != nullptr, "not initialized");
_the_store->add(arena_stat, result);
}
if (print) {
// Pre-assemble string to prevent tearing
stringStream ss;
StreamAutoIndentor sai(&ss);
ss.print("%s (%d) (%s) Arena usage ", compilertype2name(arena_stat->comp_type()), arena_stat->comp_id(), result);
arena_stat->fmn().print_on(&ss);
ss.print_raw(": ");
arena_stat->print_peak_state_on(&ss);
tty->print_raw(ss.base());
}
delete arena_stat;
}
staticvoidinform_compilation_about_oom(CompilerType ct) {
// Inform C1 or C2 that an OOM happened. They will take delayed action
// and abort the compilation in progress. Note that this is not instantaneous,
// since the compiler has to actively bailout, which may take a while, during
// which memory usage may rise further.
//
// The mechanism differs slightly between C1 and C2:
// - With C1, we directly set the bailout string, which will cause C1 to
// bailout at the typical BAILOUT places.
// - With C2, the corresponding mechanism would be the failure string; but
// bailout paths in C2 are not complete and therefore it is dangerous to
// set the failure string at - for C2 - seemingly random places. Instead,
// upon OOM C2 sets the failure string next time it checks the node limit.
if (ciEnv::current() != nullptr) {
void* compiler_data = ciEnv::current()->compiler_data();
#ifdef COMPILER1
if (ct == compiler_c1) {
Compilation* C = static_cast<Compilation*>(compiler_data);
if (C != nullptr) {
C->bailout(CompilationMemoryStatistic::failure_reason_memlimit());
C->set_oom();
}
}
#endif
#ifdef COMPILER2
if (ct == compiler_c2) {
Compile* C = static_cast<Compile*>(compiler_data);
if (C != nullptr) {
C->set_oom();
}
}
#endif// COMPILER2
}
}
voidCompilationMemoryStatistic::on_arena_chunk_allocation(size_t size, int arena_tag, uint64_t* stamp) {
assert(enabled(), "Not enabled?");
assert(arena_tag >= 0 && arena_tag < arena_tag_max, "Arena Tag OOB (%d)", arena_tag_max);
CompilerThread* const th = Thread::current()->as_Compiler_thread();
ArenaStatCounter* const arena_stat = th->arena_stat();
if (arena_stat == nullptr || // not started
arena_stat->limit_in_process()) { // limit handling in process, prevent recursion
return;
}
// Compiler can be slow to bailout, so we may hit memlimit more than once
constbool hit_limit_before = arena_stat->hit_limit();
if (arena_stat->on_arena_chunk_allocation(size, arena_tag, stamp)) { // new peak?
// Limit handling
if (arena_stat->hit_limit()) {
char name[1024] = "";
const CompilerType ct = arena_stat->comp_type();
constbool print = arena_stat->should_print_memstat();
constbool crash = arena_stat->should_crash_on_memlimit();
arena_stat->set_limit_in_process(true); // prevent recursive limit hits
arena_stat->fmn().as_C_string(name, sizeof(name));
inform_compilation_about_oom(ct);
if (crash) {
// Store this ArenaStat. If other threads also run into OOMs, let them sleep.
// We will never return, so the global store will not contain this info. We will
// print the stored ArenaStat in hs-err (see print_error_report)
if (Atomic::cmpxchg(&_arenastat_oom_crash, (ArenaStatCounter*) nullptr, arena_stat) != nullptr) {
os::infinite_sleep();
}
}
stringStream short_msg;
// We print to tty if either print is enabled or if we are to crash on MemLimit hit.
// If printing/crashing are not enabled, we just quietly abort the compilation. The
// compilation is marked as "oom" in the compilation memory result store.
if (print || crash) {
short_msg.print("%s (%d) %s: ", compilertype2name(ct), arena_stat->comp_id(), name);
short_msg.print("Hit MemLimit %s- limit: %zu now: %zu",
(hit_limit_before ? "again " : ""),
arena_stat->limit(), arena_stat->peak());
tty->print_raw(short_msg.base());
tty->cr();
}
if (crash) {
// Before crashing, if C2, end current phase. That causes its info (which is the most important) to
// be added to the phase timeline.
if (arena_stat->comp_type() == CompilerType::compiler_c2) {
arena_stat->on_phase_end();
}
// print extended message to tty (mirrors the one that should show up in the hs-err file, just for good measure)
tty->print_cr("Details:");
arena_stat->print_peak_state_on(tty);
tty->cr();
// abort VM
report_fatal(OOM_HOTSPOT_ARENA, __FILE__, __LINE__, "%s", short_msg.base());
}
arena_stat->set_limit_in_process(false);
} // end Limit handling
}
}
voidCompilationMemoryStatistic::on_arena_chunk_deallocation(size_t size, uint64_t stamp) {
assert(enabled(), "Not enabled?");
CompilerThread* const th = Thread::current()->as_Compiler_thread();
ArenaStatCounter* const arena_stat = th->arena_stat();
if (arena_stat == nullptr) { // not started
return;
}
if (arena_stat->limit_in_process()) {
return; // avoid recursion on limit hit
}
arena_stat->on_arena_chunk_deallocation(size, stamp);
}
voidCompilationMemoryStatistic::on_phase_start(int phase_trc_id, constchar* text) {
assert(enabled(), "Not enabled?");
assert(phase_trc_id >= 0 && phase_trc_id < phase_trc_id_max, "Phase trace id OOB (%d)", phase_trc_id);
CompilerThread* const th = Thread::current()->as_Compiler_thread();
ArenaStatCounter* const arena_stat = th->arena_stat();
if (arena_stat == nullptr) { // not started
return;
}
PhaseInfo info;
info.id = phase_trc_id;
info.num = arena_stat->advance_phase_counter();
info.text = text;
arena_stat->on_phase_start(info);
}
voidCompilationMemoryStatistic::on_phase_end() {
assert(enabled(), "Not enabled?");
CompilerThread* const th = Thread::current()->as_Compiler_thread();
ArenaStatCounter* const arena_stat = th->arena_stat();
if (arena_stat == nullptr) { // not started
return;
}
arena_stat->on_phase_end();
}
staticboolcheck_before_reporting(outputStream* st) {
if (!CompilationMemoryStatistic::enabled()) {
st->print_cr("Compilation memory statistics disabled.");
returnfalse;
}
if (_the_store == nullptr) {
st->print_cr("Compilation memory statistics not yet initialized. ");
returnfalse;
}
returntrue;
}
boolCompilationMemoryStatistic::in_oom_crash() {
returnAtomic::load(&_arenastat_oom_crash) != nullptr;
}
voidCompilationMemoryStatistic::print_error_report(outputStream* st) {
if (!check_before_reporting(st)) {