forked from openjdk/valhalla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaotCodeCache.cpp
More file actions
1906 lines (1737 loc) · 66.9 KB
/
aotCodeCache.cpp
File metadata and controls
1906 lines (1737 loc) · 66.9 KB
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, 2026, Oracle and/or its affiliates. All rights reserved.
* 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 "asm/macroAssembler.hpp"
#include "cds/aotCacheAccess.hpp"
#include "cds/aotMetaspace.hpp"
#include "cds/cds_globals.hpp"
#include "cds/cdsConfig.hpp"
#include "cds/heapShared.hpp"
#include "ci/ciUtilities.hpp"
#include "classfile/javaAssertions.hpp"
#include "code/aotCodeCache.hpp"
#include "code/codeCache.hpp"
#include "gc/shared/cardTableBarrierSet.hpp"
#include "gc/shared/gcConfig.hpp"
#include "logging/logStream.hpp"
#include "memory/memoryReserver.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/flags/flagSetting.hpp"
#include "runtime/globals_extension.hpp"
#include "runtime/java.hpp"
#include "runtime/mutexLocker.hpp"
#include "runtime/os.inline.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/stubInfo.hpp"
#include "runtime/stubRoutines.hpp"
#include "utilities/copy.hpp"
#ifdef COMPILER1
#include "c1/c1_Runtime1.hpp"
#endif
#ifdef COMPILER2
#include "opto/runtime.hpp"
#endif
#if INCLUDE_G1GC
#include "gc/g1/g1BarrierSetRuntime.hpp"
#include "gc/g1/g1HeapRegion.hpp"
#endif
#if INCLUDE_SHENANDOAHGC
#include "gc/shenandoah/shenandoahRuntime.hpp"
#endif
#if INCLUDE_ZGC
#include "gc/z/zBarrierSetRuntime.hpp"
#endif
#include <errno.h>
#include <sys/stat.h>
const char* aot_code_entry_kind_name[] = {
#define DECL_KIND_STRING(kind) XSTR(kind),
DO_AOTCODEENTRY_KIND(DECL_KIND_STRING)
#undef DECL_KIND_STRING
};
static void report_load_failure() {
if (AbortVMOnAOTCodeFailure) {
vm_exit_during_initialization("Unable to use AOT Code Cache.", nullptr);
}
log_info(aot, codecache, init)("Unable to use AOT Code Cache.");
AOTCodeCache::disable_caching();
}
static void report_store_failure() {
if (AbortVMOnAOTCodeFailure) {
tty->print_cr("Unable to create AOT Code Cache.");
vm_abort(false);
}
log_info(aot, codecache, exit)("Unable to create AOT Code Cache.");
AOTCodeCache::disable_caching();
}
// The sequence of AOT code caching flags and parametters settings.
//
// 1. The initial AOT code caching flags setting is done
// during call to CDSConfig::check_vm_args_consistency().
//
// 2. The earliest AOT code state check done in compilationPolicy_init()
// where we set number of compiler threads for AOT assembly phase.
//
// 3. We determine presence of AOT code in AOT Cache in
// AOTMetaspace::open_static_archive() which is calles
// after compilationPolicy_init() but before codeCache_init().
//
// 4. AOTCodeCache::initialize() is called during universe_init()
// and does final AOT state and flags settings.
//
// 5. Finally AOTCodeCache::init2() is called after universe_init()
// when all GC settings are finalized.
// Next methods determine which action we do with AOT code depending
// on phase of AOT process: assembly or production.
bool AOTCodeCache::is_dumping_adapter() {
return AOTAdapterCaching && is_on_for_dump();
}
bool AOTCodeCache::is_using_adapter() {
return AOTAdapterCaching && is_on_for_use();
}
bool AOTCodeCache::is_dumping_stub() {
return AOTStubCaching && is_on_for_dump();
}
bool AOTCodeCache::is_using_stub() {
return AOTStubCaching && is_on_for_use();
}
// Next methods could be called regardless AOT code cache status.
// Initially they are called during flags parsing and finilized
// in AOTCodeCache::initialize().
void AOTCodeCache::enable_caching() {
FLAG_SET_ERGO_IF_DEFAULT(AOTStubCaching, true);
FLAG_SET_ERGO_IF_DEFAULT(AOTAdapterCaching, true);
}
void AOTCodeCache::disable_caching() {
FLAG_SET_ERGO(AOTStubCaching, false);
FLAG_SET_ERGO(AOTAdapterCaching, false);
}
bool AOTCodeCache::is_caching_enabled() {
return AOTStubCaching || AOTAdapterCaching;
}
static uint32_t encode_id(AOTCodeEntry::Kind kind, int id) {
assert(AOTCodeEntry::is_valid_entry_kind(kind), "invalid AOTCodeEntry kind %d", (int)kind);
// There can be a conflict of id between an Adapter and *Blob, but that should not cause any functional issue
// becasue both id and kind are used to find an entry, and that combination should be unique
if (kind == AOTCodeEntry::Adapter) {
return id;
} else if (kind == AOTCodeEntry::SharedBlob) {
assert(StubInfo::is_shared(static_cast<BlobId>(id)), "not a shared blob id %d", id);
return id;
} else if (kind == AOTCodeEntry::C1Blob) {
assert(StubInfo::is_c1(static_cast<BlobId>(id)), "not a c1 blob id %d", id);
return id;
} else {
// kind must be AOTCodeEntry::C2Blob
assert(StubInfo::is_c2(static_cast<BlobId>(id)), "not a c2 blob id %d", id);
return id;
}
}
static uint _max_aot_code_size = 0;
uint AOTCodeCache::max_aot_code_size() {
return _max_aot_code_size;
}
// It is called from AOTMetaspace::initialize_shared_spaces()
// which is called from universe_init().
// At this point all AOT class linking seetings are finilized
// and AOT cache is open so we can map AOT code region.
void AOTCodeCache::initialize() {
#if defined(ZERO) || !(defined(AMD64) || defined(AARCH64))
log_info(aot, codecache, init)("AOT Code Cache is not supported on this platform.");
disable_caching();
return;
#else
if (FLAG_IS_DEFAULT(AOTCache)) {
log_info(aot, codecache, init)("AOT Code Cache is not used: AOTCache is not specified.");
disable_caching();
return; // AOTCache must be specified to dump and use AOT code
}
// Disable stubs caching until JDK-8357398 is fixed.
FLAG_SET_ERGO(AOTStubCaching, false);
if (VerifyOops) {
// Disable AOT stub caching when VerifyOops flag is on.
// Verify oops code generated a lot of C strings which overflow
// AOT C string table (which has fixed size).
// AOT C string table will be reworked later to handle such cases.
log_info(aot, codecache, init)("AOT Stub Caching is not supported with VerifyOops.");
FLAG_SET_ERGO(AOTStubCaching, false);
if (InlineTypePassFieldsAsArgs) {
log_info(aot, codecache, init)("AOT Adapter Caching is not supported with VerifyOops + InlineTypePassFieldsAsArgs.");
FLAG_SET_ERGO(AOTAdapterCaching, false);
}
}
bool is_dumping = false;
bool is_using = false;
if (CDSConfig::is_dumping_final_static_archive() && CDSConfig::is_dumping_aot_linked_classes()) {
is_dumping = true;
enable_caching();
is_dumping = is_caching_enabled();
} else if (CDSConfig::is_using_archive() && CDSConfig::is_using_aot_linked_classes()) {
enable_caching();
is_using = is_caching_enabled();
} else {
log_info(aot, codecache, init)("AOT Code Cache is not used: AOT Class Linking is not used.");
disable_caching();
return; // nothing to do
}
if (!(is_dumping || is_using)) {
disable_caching();
return; // AOT code caching disabled on command line
}
_max_aot_code_size = AOTCodeMaxSize;
if (!FLAG_IS_DEFAULT(AOTCodeMaxSize)) {
if (!is_aligned(AOTCodeMaxSize, os::vm_allocation_granularity())) {
_max_aot_code_size = align_up(AOTCodeMaxSize, os::vm_allocation_granularity());
log_debug(aot,codecache,init)("Max AOT Code Cache size is aligned up to %uK", (int)(max_aot_code_size()/K));
}
}
size_t aot_code_size = is_using ? AOTCacheAccess::get_aot_code_region_size() : 0;
if (is_using && aot_code_size == 0) {
log_info(aot, codecache, init)("AOT Code Cache is empty");
disable_caching();
return;
}
if (!open_cache(is_dumping, is_using)) {
if (is_using) {
report_load_failure();
} else {
report_store_failure();
}
return;
}
if (is_dumping) {
FLAG_SET_DEFAULT(ForceUnreachable, true);
}
FLAG_SET_DEFAULT(DelayCompilerStubsGeneration, false);
#endif // defined(AMD64) || defined(AARCH64)
}
static AOTCodeCache* opened_cache = nullptr; // Use this until we verify the cache
AOTCodeCache* AOTCodeCache::_cache = nullptr;
DEBUG_ONLY( bool AOTCodeCache::_passed_init2 = false; )
// It is called after universe_init() when all GC settings are finalized.
void AOTCodeCache::init2() {
DEBUG_ONLY( _passed_init2 = true; )
if (opened_cache == nullptr) {
return;
}
if (!opened_cache->verify_config()) {
delete opened_cache;
opened_cache = nullptr;
report_load_failure();
return;
}
// initialize aot runtime constants as appropriate to this runtime
AOTRuntimeConstants::initialize_from_runtime();
// initialize the table of external routines so we can save
// generated code blobs that reference them
AOTCodeAddressTable* table = opened_cache->_table;
assert(table != nullptr, "should be initialized already");
table->init_extrs();
// Now cache and address table are ready for AOT code generation
_cache = opened_cache;
}
bool AOTCodeCache::open_cache(bool is_dumping, bool is_using) {
opened_cache = new AOTCodeCache(is_dumping, is_using);
if (opened_cache->failed()) {
delete opened_cache;
opened_cache = nullptr;
return false;
}
return true;
}
void AOTCodeCache::close() {
if (is_on()) {
delete _cache; // Free memory
_cache = nullptr;
opened_cache = nullptr;
}
}
#define DATA_ALIGNMENT HeapWordSize
AOTCodeCache::AOTCodeCache(bool is_dumping, bool is_using) :
_load_header(nullptr),
_load_buffer(nullptr),
_store_buffer(nullptr),
_C_store_buffer(nullptr),
_write_position(0),
_load_size(0),
_store_size(0),
_for_use(is_using),
_for_dump(is_dumping),
_closing(false),
_failed(false),
_lookup_failed(false),
_table(nullptr),
_load_entries(nullptr),
_search_entries(nullptr),
_store_entries(nullptr),
_C_strings_buf(nullptr),
_store_entries_cnt(0)
{
// Read header at the begining of cache
if (_for_use) {
// Read cache
size_t load_size = AOTCacheAccess::get_aot_code_region_size();
ReservedSpace rs = MemoryReserver::reserve(load_size, mtCode);
if (!rs.is_reserved()) {
log_warning(aot, codecache, init)("Failed to reserved %u bytes of memory for mapping AOT code region into AOT Code Cache", (uint)load_size);
set_failed();
return;
}
if (!AOTCacheAccess::map_aot_code_region(rs)) {
log_warning(aot, codecache, init)("Failed to read/mmap cached code region into AOT Code Cache");
set_failed();
return;
}
_load_size = (uint)load_size;
_load_buffer = (char*)rs.base();
assert(is_aligned(_load_buffer, DATA_ALIGNMENT), "load_buffer is not aligned");
log_debug(aot, codecache, init)("Mapped %u bytes at address " INTPTR_FORMAT " at AOT Code Cache", _load_size, p2i(_load_buffer));
_load_header = (Header*)addr(0);
if (!_load_header->verify(_load_size)) {
set_failed();
return;
}
log_info (aot, codecache, init)("Loaded %u AOT code entries from AOT Code Cache", _load_header->entries_count());
log_debug(aot, codecache, init)(" Adapters: total=%u", _load_header->adapters_count());
log_debug(aot, codecache, init)(" Shared Blobs: total=%u", _load_header->shared_blobs_count());
log_debug(aot, codecache, init)(" C1 Blobs: total=%u", _load_header->C1_blobs_count());
log_debug(aot, codecache, init)(" C2 Blobs: total=%u", _load_header->C2_blobs_count());
log_debug(aot, codecache, init)(" AOT code cache size: %u bytes", _load_header->cache_size());
// Read strings
load_strings();
}
if (_for_dump) {
_C_store_buffer = NEW_C_HEAP_ARRAY(char, max_aot_code_size() + DATA_ALIGNMENT, mtCode);
_store_buffer = align_up(_C_store_buffer, DATA_ALIGNMENT);
// Entries allocated at the end of buffer in reverse (as on stack).
_store_entries = (AOTCodeEntry*)align_up(_C_store_buffer + max_aot_code_size(), DATA_ALIGNMENT);
log_debug(aot, codecache, init)("Allocated store buffer at address " INTPTR_FORMAT " of size %u", p2i(_store_buffer), max_aot_code_size());
}
_table = new AOTCodeAddressTable();
}
void AOTCodeCache::init_early_stubs_table() {
AOTCodeAddressTable* table = addr_table();
if (table != nullptr) {
table->init_early_stubs();
}
}
void AOTCodeCache::init_shared_blobs_table() {
AOTCodeAddressTable* table = addr_table();
if (table != nullptr) {
table->init_shared_blobs();
}
}
void AOTCodeCache::init_early_c1_table() {
AOTCodeAddressTable* table = addr_table();
if (table != nullptr) {
table->init_early_c1();
}
}
AOTCodeCache::~AOTCodeCache() {
if (_closing) {
return; // Already closed
}
// Stop any further access to cache.
_closing = true;
MutexLocker ml(Compile_lock);
if (for_dump()) { // Finalize cache
finish_write();
}
_load_buffer = nullptr;
if (_C_store_buffer != nullptr) {
FREE_C_HEAP_ARRAY(char, _C_store_buffer);
_C_store_buffer = nullptr;
_store_buffer = nullptr;
}
if (_table != nullptr) {
MutexLocker ml(AOTCodeCStrings_lock, Mutex::_no_safepoint_check_flag);
delete _table;
_table = nullptr;
}
}
void AOTCodeCache::Config::record(uint cpu_features_offset) {
_flags = 0;
#ifdef ASSERT
_flags |= debugVM;
#endif
if (UseCompressedOops) {
_flags |= compressedOops;
}
if (UseCompressedClassPointers) {
_flags |= compressedClassPointers;
}
if (UseTLAB) {
_flags |= useTLAB;
}
if (JavaAssertions::systemClassDefault()) {
_flags |= systemClassAssertions;
}
if (JavaAssertions::userClassDefault()) {
_flags |= userClassAssertions;
}
if (EnableContended) {
_flags |= enableContendedPadding;
}
if (RestrictContended) {
_flags |= restrictContendedPadding;
}
_compressedOopShift = CompressedOops::shift();
_compressedOopBase = CompressedOops::base();
_compressedKlassShift = CompressedKlassPointers::shift();
_contendedPaddingWidth = ContendedPaddingWidth;
_gc = (uint)Universe::heap()->kind();
_cpu_features_offset = cpu_features_offset;
}
bool AOTCodeCache::Config::verify_cpu_features(AOTCodeCache* cache) const {
LogStreamHandle(Debug, aot, codecache, init) log;
uint offset = _cpu_features_offset;
uint cpu_features_size = *(uint *)cache->addr(offset);
assert(cpu_features_size == (uint)VM_Version::cpu_features_size(), "must be");
offset += sizeof(uint);
void* cached_cpu_features_buffer = (void *)cache->addr(offset);
if (log.is_enabled()) {
ResourceMark rm; // required for stringStream::as_string()
stringStream ss;
VM_Version::get_cpu_features_name(cached_cpu_features_buffer, ss);
log.print_cr("CPU features recorded in AOTCodeCache: %s", ss.as_string());
}
if (VM_Version::supports_features(cached_cpu_features_buffer)) {
if (log.is_enabled()) {
ResourceMark rm; // required for stringStream::as_string()
stringStream ss;
char* runtime_cpu_features = NEW_RESOURCE_ARRAY(char, VM_Version::cpu_features_size());
VM_Version::store_cpu_features(runtime_cpu_features);
VM_Version::get_missing_features_name(runtime_cpu_features, cached_cpu_features_buffer, ss);
if (!ss.is_empty()) {
log.print_cr("Additional runtime CPU features: %s", ss.as_string());
}
}
} else {
if (log.is_enabled()) {
ResourceMark rm; // required for stringStream::as_string()
stringStream ss;
char* runtime_cpu_features = NEW_RESOURCE_ARRAY(char, VM_Version::cpu_features_size());
VM_Version::store_cpu_features(runtime_cpu_features);
VM_Version::get_missing_features_name(cached_cpu_features_buffer, runtime_cpu_features, ss);
log.print_cr("AOT Code Cache disabled: required cpu features are missing: %s", ss.as_string());
}
return false;
}
return true;
}
bool AOTCodeCache::Config::verify(AOTCodeCache* cache) const {
// First checks affect all cached AOT code
#ifdef ASSERT
if ((_flags & debugVM) == 0) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created by product VM, it can't be used by debug VM");
return false;
}
#else
if ((_flags & debugVM) != 0) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created by debug VM, it can't be used by product VM");
return false;
}
#endif
CollectedHeap::Name aot_gc = (CollectedHeap::Name)_gc;
if (aot_gc != Universe::heap()->kind()) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with different GC: %s vs current %s", GCConfig::hs_err_name(aot_gc), GCConfig::hs_err_name());
return false;
}
if (((_flags & compressedClassPointers) != 0) != UseCompressedClassPointers) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with UseCompressedClassPointers = %s", UseCompressedClassPointers ? "false" : "true");
return false;
}
if (_compressedKlassShift != (uint)CompressedKlassPointers::shift()) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: it was created with CompressedKlassPointers::shift() = %d vs current %d", _compressedKlassShift, CompressedKlassPointers::shift());
return false;
}
// The following checks do not affect AOT code, but can disable
// AOT stub/adapters caching if they are incompatible with runtime settings
// (adapters too as they access oops when buffering scalarized value objects).
if (((_flags & compressedOops) != 0) != UseCompressedOops) {
log_debug(aot, codecache, init)("AOT Stub/Adapter Cache disabled: it was created with UseCompressedOops = %s", UseCompressedOops ? "false" : "true");
AOTStubCaching = false;
if (InlineTypePassFieldsAsArgs) {
AOTAdapterCaching = false;
}
}
if (_compressedOopShift != (uint)CompressedOops::shift()) {
log_debug(aot, codecache, init)("AOT Stub/Adapter Cache disabled: it was created with different CompressedOops::shift(): %d vs current %d", _compressedOopShift, CompressedOops::shift());
AOTStubCaching = false;
if (InlineTypePassFieldsAsArgs) {
AOTAdapterCaching = false;
}
}
// This should be the last check as it only disables AOTStub/AdapterCaching
if ((_compressedOopBase == nullptr || CompressedOops::base() == nullptr) && (_compressedOopBase != CompressedOops::base())) {
log_debug(aot, codecache, init)("AOT Stub/Adapter Cache disabled: incompatible CompressedOops::base(): %p vs current %p", _compressedOopBase, CompressedOops::base());
AOTStubCaching = false;
if (InlineTypePassFieldsAsArgs) {
AOTAdapterCaching = false;
}
}
if (!verify_cpu_features(cache)) {
return false;
}
return true;
}
bool AOTCodeCache::Header::verify(uint load_size) const {
if (_version != AOT_CODE_VERSION) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: different AOT Code version %d vs %d recorded in AOT Code header", AOT_CODE_VERSION, _version);
return false;
}
if (load_size < _cache_size) {
log_debug(aot, codecache, init)("AOT Code Cache disabled: AOT Code Cache size %d < %d recorded in AOT Code header", load_size, _cache_size);
return false;
}
return true;
}
AOTCodeCache* AOTCodeCache::open_for_use() {
if (AOTCodeCache::is_on_for_use()) {
return AOTCodeCache::cache();
}
return nullptr;
}
AOTCodeCache* AOTCodeCache::open_for_dump() {
if (AOTCodeCache::is_on_for_dump()) {
AOTCodeCache* cache = AOTCodeCache::cache();
cache->clear_lookup_failed(); // Reset bit
return cache;
}
return nullptr;
}
void copy_bytes(const char* from, address to, uint size) {
assert((int)size > 0, "sanity");
memcpy(to, from, size);
log_trace(aot, codecache)("Copied %d bytes from " INTPTR_FORMAT " to " INTPTR_FORMAT, size, p2i(from), p2i(to));
}
AOTCodeReader::AOTCodeReader(AOTCodeCache* cache, AOTCodeEntry* entry) {
_cache = cache;
_entry = entry;
_load_buffer = cache->cache_buffer();
_read_position = 0;
_lookup_failed = false;
}
void AOTCodeReader::set_read_position(uint pos) {
if (pos == _read_position) {
return;
}
assert(pos < _cache->load_size(), "offset:%d >= file size:%d", pos, _cache->load_size());
_read_position = pos;
}
bool AOTCodeCache::set_write_position(uint pos) {
if (pos == _write_position) {
return true;
}
if (_store_size < _write_position) {
_store_size = _write_position; // Adjust during write
}
assert(pos < _store_size, "offset:%d >= file size:%d", pos, _store_size);
_write_position = pos;
return true;
}
static char align_buffer[256] = { 0 };
bool AOTCodeCache::align_write() {
// We are not executing code from cache - we copy it by bytes first.
// No need for big alignment (or at all).
uint padding = DATA_ALIGNMENT - (_write_position & (DATA_ALIGNMENT - 1));
if (padding == DATA_ALIGNMENT) {
return true;
}
uint n = write_bytes((const void*)&align_buffer, padding);
if (n != padding) {
return false;
}
log_trace(aot, codecache)("Adjust write alignment in AOT Code Cache");
return true;
}
// Check to see if AOT code cache has required space to store "nbytes" of data
address AOTCodeCache::reserve_bytes(uint nbytes) {
assert(for_dump(), "Code Cache file is not created");
uint new_position = _write_position + nbytes;
if (new_position >= (uint)((char*)_store_entries - _store_buffer)) {
log_warning(aot,codecache)("Failed to ensure %d bytes at offset %d in AOT Code Cache. Increase AOTCodeMaxSize.",
nbytes, _write_position);
set_failed();
report_store_failure();
return nullptr;
}
address buffer = (address)(_store_buffer + _write_position);
log_trace(aot, codecache)("Reserved %d bytes at offset %d in AOT Code Cache", nbytes, _write_position);
_write_position += nbytes;
if (_store_size < _write_position) {
_store_size = _write_position;
}
return buffer;
}
uint AOTCodeCache::write_bytes(const void* buffer, uint nbytes) {
assert(for_dump(), "Code Cache file is not created");
if (nbytes == 0) {
return 0;
}
uint new_position = _write_position + nbytes;
if (new_position >= (uint)((char*)_store_entries - _store_buffer)) {
log_warning(aot, codecache)("Failed to write %d bytes at offset %d to AOT Code Cache. Increase AOTCodeMaxSize.",
nbytes, _write_position);
set_failed();
report_store_failure();
return 0;
}
copy_bytes((const char* )buffer, (address)(_store_buffer + _write_position), nbytes);
log_trace(aot, codecache)("Wrote %d bytes at offset %d to AOT Code Cache", nbytes, _write_position);
_write_position += nbytes;
if (_store_size < _write_position) {
_store_size = _write_position;
}
return nbytes;
}
void* AOTCodeEntry::operator new(size_t x, AOTCodeCache* cache) {
return (void*)(cache->add_entry());
}
static bool check_entry(AOTCodeEntry::Kind kind, uint id, AOTCodeEntry* entry) {
if (entry->kind() == kind) {
assert(entry->id() == id, "sanity");
return true; // Found
}
return false;
}
AOTCodeEntry* AOTCodeCache::find_entry(AOTCodeEntry::Kind kind, uint id) {
assert(_for_use, "sanity");
uint count = _load_header->entries_count();
if (_load_entries == nullptr) {
// Read it
_search_entries = (uint*)addr(_load_header->entries_offset()); // [id, index]
_load_entries = (AOTCodeEntry*)(_search_entries + 2 * count);
log_debug(aot, codecache, init)("Read %d entries table at offset %d from AOT Code Cache", count, _load_header->entries_offset());
}
// Binary search
int l = 0;
int h = count - 1;
while (l <= h) {
int mid = (l + h) >> 1;
int ix = mid * 2;
uint is = _search_entries[ix];
if (is == id) {
int index = _search_entries[ix + 1];
AOTCodeEntry* entry = &(_load_entries[index]);
if (check_entry(kind, id, entry)) {
return entry; // Found
}
// Linear search around to handle id collission
for (int i = mid - 1; i >= l; i--) { // search back
ix = i * 2;
is = _search_entries[ix];
if (is != id) {
break;
}
index = _search_entries[ix + 1];
AOTCodeEntry* entry = &(_load_entries[index]);
if (check_entry(kind, id, entry)) {
return entry; // Found
}
}
for (int i = mid + 1; i <= h; i++) { // search forward
ix = i * 2;
is = _search_entries[ix];
if (is != id) {
break;
}
index = _search_entries[ix + 1];
AOTCodeEntry* entry = &(_load_entries[index]);
if (check_entry(kind, id, entry)) {
return entry; // Found
}
}
break; // Not found match
} else if (is < id) {
l = mid + 1;
} else {
h = mid - 1;
}
}
return nullptr;
}
extern "C" {
static int uint_cmp(const void *i, const void *j) {
uint a = *(uint *)i;
uint b = *(uint *)j;
return a > b ? 1 : a < b ? -1 : 0;
}
}
void AOTCodeCache::store_cpu_features(char*& buffer, uint buffer_size) {
uint* size_ptr = (uint *)buffer;
*size_ptr = buffer_size;
buffer += sizeof(uint);
VM_Version::store_cpu_features(buffer);
log_debug(aot, codecache, exit)("CPU features recorded in AOTCodeCache: %s", VM_Version::features_string());
buffer += buffer_size;
buffer = align_up(buffer, DATA_ALIGNMENT);
}
bool AOTCodeCache::finish_write() {
if (!align_write()) {
return false;
}
uint strings_offset = _write_position;
int strings_count = store_strings();
if (strings_count < 0) {
return false;
}
if (!align_write()) {
return false;
}
uint strings_size = _write_position - strings_offset;
uint entries_count = 0; // Number of entrant (useful) code entries
uint entries_offset = _write_position;
uint store_count = _store_entries_cnt;
if (store_count > 0) {
uint header_size = (uint)align_up(sizeof(AOTCodeCache::Header), DATA_ALIGNMENT);
uint code_count = store_count;
uint search_count = code_count * 2;
uint search_size = search_count * sizeof(uint);
uint entries_size = (uint)align_up(code_count * sizeof(AOTCodeEntry), DATA_ALIGNMENT); // In bytes
// _write_position includes size of code and strings
uint code_alignment = code_count * DATA_ALIGNMENT; // We align_up code size when storing it.
uint cpu_features_size = VM_Version::cpu_features_size();
uint total_cpu_features_size = sizeof(uint) + cpu_features_size; // sizeof(uint) to store cpu_features_size
uint total_size = header_size + _write_position + code_alignment + search_size + entries_size +
align_up(total_cpu_features_size, DATA_ALIGNMENT);
assert(total_size < max_aot_code_size(), "AOT Code size (" UINT32_FORMAT " bytes) is greater than AOTCodeMaxSize(" UINT32_FORMAT " bytes).", total_size, max_aot_code_size());
// Allocate in AOT Cache buffer
char* buffer = (char *)AOTCacheAccess::allocate_aot_code_region(total_size + DATA_ALIGNMENT);
char* start = align_up(buffer, DATA_ALIGNMENT);
char* current = start + header_size; // Skip header
uint cpu_features_offset = current - start;
store_cpu_features(current, cpu_features_size);
assert(is_aligned(current, DATA_ALIGNMENT), "sanity check");
assert(current < start + total_size, "sanity check");
// Create ordered search table for entries [id, index];
uint* search = NEW_C_HEAP_ARRAY(uint, search_count, mtCode);
AOTCodeEntry* entries_address = _store_entries; // Pointer to latest entry
uint adapters_count = 0;
uint shared_blobs_count = 0;
uint C1_blobs_count = 0;
uint C2_blobs_count = 0;
uint max_size = 0;
// AOTCodeEntry entries were allocated in reverse in store buffer.
// Process them in reverse order to cache first code first.
for (int i = store_count - 1; i >= 0; i--) {
entries_address[i].set_next(nullptr); // clear pointers before storing data
uint size = align_up(entries_address[i].size(), DATA_ALIGNMENT);
if (size > max_size) {
max_size = size;
}
copy_bytes((_store_buffer + entries_address[i].offset()), (address)current, size);
entries_address[i].set_offset(current - start); // New offset
current += size;
uint n = write_bytes(&(entries_address[i]), sizeof(AOTCodeEntry));
if (n != sizeof(AOTCodeEntry)) {
FREE_C_HEAP_ARRAY(uint, search);
return false;
}
search[entries_count*2 + 0] = entries_address[i].id();
search[entries_count*2 + 1] = entries_count;
entries_count++;
AOTCodeEntry::Kind kind = entries_address[i].kind();
if (kind == AOTCodeEntry::Adapter) {
adapters_count++;
} else if (kind == AOTCodeEntry::SharedBlob) {
shared_blobs_count++;
} else if (kind == AOTCodeEntry::C1Blob) {
C1_blobs_count++;
} else if (kind == AOTCodeEntry::C2Blob) {
C2_blobs_count++;
}
}
if (entries_count == 0) {
log_info(aot, codecache, exit)("AOT Code Cache was not created: no entires");
FREE_C_HEAP_ARRAY(uint, search);
return true; // Nothing to write
}
assert(entries_count <= store_count, "%d > %d", entries_count, store_count);
// Write strings
if (strings_count > 0) {
copy_bytes((_store_buffer + strings_offset), (address)current, strings_size);
strings_offset = (current - start); // New offset
current += strings_size;
}
uint new_entries_offset = (current - start); // New offset
// Sort and store search table
qsort(search, entries_count, 2*sizeof(uint), uint_cmp);
search_size = 2 * entries_count * sizeof(uint);
copy_bytes((const char*)search, (address)current, search_size);
FREE_C_HEAP_ARRAY(uint, search);
current += search_size;
// Write entries
entries_size = entries_count * sizeof(AOTCodeEntry); // New size
copy_bytes((_store_buffer + entries_offset), (address)current, entries_size);
current += entries_size;
uint size = (current - start);
assert(size <= total_size, "%d > %d", size , total_size);
log_debug(aot, codecache, exit)(" Adapters: total=%u", adapters_count);
log_debug(aot, codecache, exit)(" Shared Blobs: total=%d", shared_blobs_count);
log_debug(aot, codecache, exit)(" C1 Blobs: total=%d", C1_blobs_count);
log_debug(aot, codecache, exit)(" C2 Blobs: total=%d", C2_blobs_count);
log_debug(aot, codecache, exit)(" AOT code cache size: %u bytes, max entry's size: %u bytes", size, max_size);
// Finalize header
AOTCodeCache::Header* header = (AOTCodeCache::Header*)start;
header->init(size, (uint)strings_count, strings_offset,
entries_count, new_entries_offset,
adapters_count, shared_blobs_count,
C1_blobs_count, C2_blobs_count, cpu_features_offset);
log_info(aot, codecache, exit)("Wrote %d AOT code entries to AOT Code Cache", entries_count);
}
return true;
}
//------------------Store/Load AOT code ----------------------
bool AOTCodeCache::store_code_blob(CodeBlob& blob, AOTCodeEntry::Kind entry_kind, uint id, const char* name) {
AOTCodeCache* cache = open_for_dump();
if (cache == nullptr) {
return false;
}
assert(AOTCodeEntry::is_valid_entry_kind(entry_kind), "invalid entry_kind %d", entry_kind);
if (AOTCodeEntry::is_adapter(entry_kind) && !is_dumping_adapter()) {
return false;
}
if (AOTCodeEntry::is_blob(entry_kind) && !is_dumping_stub()) {
return false;
}
log_debug(aot, codecache, stubs)("Writing blob '%s' (id=%u, kind=%s) to AOT Code Cache", name, id, aot_code_entry_kind_name[entry_kind]);
#ifdef ASSERT
LogStreamHandle(Trace, aot, codecache, stubs) log;
if (log.is_enabled()) {
FlagSetting fs(PrintRelocations, true);
blob.print_on(&log);
}
#endif
// we need to take a lock to prevent race between compiler threads generating AOT code
// and the main thread generating adapter
MutexLocker ml(Compile_lock);
if (!is_on()) {
return false; // AOT code cache was already dumped and closed.
}
if (!cache->align_write()) {
return false;
}
uint entry_position = cache->_write_position;
// Write name
uint name_offset = cache->_write_position - entry_position;
uint name_size = (uint)strlen(name) + 1; // Includes '/0'
uint n = cache->write_bytes(name, name_size);
if (n != name_size) {
return false;
}
// Write CodeBlob
if (!cache->align_write()) {
return false;
}
uint blob_offset = cache->_write_position - entry_position;
address archive_buffer = cache->reserve_bytes(blob.size());
if (archive_buffer == nullptr) {
return false;
}
CodeBlob::archive_blob(&blob, archive_buffer);
uint reloc_data_size = blob.relocation_size();
n = cache->write_bytes((address)blob.relocation_begin(), reloc_data_size);
if (n != reloc_data_size) {
return false;
}
bool has_oop_maps = false;
if (blob.oop_maps() != nullptr) {
if (!cache->write_oop_map_set(blob)) {
return false;
}
has_oop_maps = true;
}
#ifndef PRODUCT
// Write asm remarks
if (!cache->write_asm_remarks(blob)) {
return false;
}
if (!cache->write_dbg_strings(blob)) {
return false;
}
#endif /* PRODUCT */
if (!cache->write_relocations(blob)) {
if (!cache->failed()) {
// We may miss an address in AOT table - skip this code blob.
cache->set_write_position(entry_position);
}
return false;
}
uint entry_size = cache->_write_position - entry_position;
AOTCodeEntry* entry = new(cache) AOTCodeEntry(entry_kind, encode_id(entry_kind, id),
entry_position, entry_size, name_offset, name_size,
blob_offset, has_oop_maps, blob.content_begin());
log_debug(aot, codecache, stubs)("Wrote code blob '%s' (id=%u, kind=%s) to AOT Code Cache", name, id, aot_code_entry_kind_name[entry_kind]);
return true;
}
bool AOTCodeCache::store_code_blob(CodeBlob& blob, AOTCodeEntry::Kind entry_kind, BlobId id) {
assert(AOTCodeEntry::is_blob(entry_kind),
"wrong entry kind for blob id %s", StubInfo::name(id));
return store_code_blob(blob, entry_kind, (uint)id, StubInfo::name(id));
}
CodeBlob* AOTCodeCache::load_code_blob(AOTCodeEntry::Kind entry_kind, uint id, const char* name) {
AOTCodeCache* cache = open_for_use();
if (cache == nullptr) {
return nullptr;
}
assert(AOTCodeEntry::is_valid_entry_kind(entry_kind), "invalid entry_kind %d", entry_kind);
if (AOTCodeEntry::is_adapter(entry_kind) && !is_using_adapter()) {
return nullptr;
}
if (AOTCodeEntry::is_blob(entry_kind) && !is_using_stub()) {
return nullptr;
}
log_debug(aot, codecache, stubs)("Reading blob '%s' (id=%u, kind=%s) from AOT Code Cache", name, id, aot_code_entry_kind_name[entry_kind]);
AOTCodeEntry* entry = cache->find_entry(entry_kind, encode_id(entry_kind, id));
if (entry == nullptr) {
return nullptr;