32
32
#include " classfile/javaClasses.inline.hpp"
33
33
#include " classfile/stringTable.hpp"
34
34
#include " classfile/vmClasses.hpp"
35
+ #include " compiler/compileBroker.hpp"
35
36
#include " gc/shared/collectedHeap.hpp"
36
37
#include " gc/shared/oopStorage.inline.hpp"
37
38
#include " gc/shared/oopStorageSet.hpp"
@@ -115,6 +116,7 @@ OopStorage* StringTable::_oop_storage;
115
116
116
117
static size_t _current_size = 0 ;
117
118
static volatile size_t _items_count = 0 ;
119
+ DEBUG_ONLY (static bool _disable_interning_during_cds_dump = false );
118
120
119
121
volatile bool _alt_hash = false ;
120
122
@@ -346,6 +348,10 @@ bool StringTable::has_work() {
346
348
return Atomic::load_acquire (&_has_work);
347
349
}
348
350
351
+ size_t StringTable::items_count_acquire () {
352
+ return Atomic::load_acquire (&_items_count);
353
+ }
354
+
349
355
void StringTable::trigger_concurrent_work () {
350
356
// Avoid churn on ServiceThread
351
357
if (!has_work ()) {
@@ -504,6 +510,9 @@ oop StringTable::intern(const char* utf8_string, TRAPS) {
504
510
}
505
511
506
512
oop StringTable::intern (const StringWrapper& name, TRAPS) {
513
+ assert (!Atomic::load_acquire (&_disable_interning_during_cds_dump),
514
+ " All threads that may intern strings should have been stopped before CDS starts copying the interned string table" );
515
+
507
516
// shared table always uses java_lang_String::hash_code
508
517
unsigned int hash = hash_wrapped_string (name);
509
518
oop found_string = lookup_shared (name, hash);
@@ -793,7 +802,7 @@ void StringTable::verify() {
793
802
}
794
803
795
804
// Verification and comp
796
- class VerifyCompStrings : StackObj {
805
+ class StringTable :: VerifyCompStrings : StackObj {
797
806
static unsigned string_hash (oop const & str) {
798
807
return java_lang_String::hash_code_noupdate (str);
799
808
}
@@ -805,7 +814,7 @@ class VerifyCompStrings : StackObj {
805
814
string_hash, string_equals> _table;
806
815
public:
807
816
size_t _errors;
808
- VerifyCompStrings () : _table(unsigned (_items_count / 8 ) + 1 , 0 /* do not resize */ ), _errors(0 ) {}
817
+ VerifyCompStrings () : _table(unsigned (items_count_acquire() / 8 ) + 1 , 0 /* do not resize */ ), _errors(0 ) {}
809
818
bool operator ()(WeakHandle* val) {
810
819
oop s = val->resolve ();
811
820
if (s == nullptr ) {
@@ -939,20 +948,31 @@ oop StringTable::lookup_shared(const jchar* name, int len) {
939
948
return _shared_table.lookup (wrapped_name, java_lang_String::hash_code (name, len), 0 );
940
949
}
941
950
942
- // This is called BEFORE we enter the CDS safepoint. We can allocate heap objects.
943
- // This should be called when we know no more strings will be added (which will be easy
944
- // to guarantee because CDS runs with a single Java thread. See JDK-8253495.)
951
+ // This is called BEFORE we enter the CDS safepoint. We can still allocate Java object arrays to
952
+ // be used by the shared strings table.
945
953
void StringTable::allocate_shared_strings_array (TRAPS) {
946
954
if (!CDSConfig::is_dumping_heap ()) {
947
955
return ;
948
956
}
949
- assert (CDSConfig::allow_only_single_java_thread (), " No more interned strings can be added" );
950
957
951
- if (_items_count > (size_t )max_jint) {
952
- fatal (" Too many strings to be archived: %zu" , _items_count);
958
+ CompileBroker::wait_for_no_active_tasks ();
959
+
960
+ precond (CDSConfig::allow_only_single_java_thread ());
961
+
962
+ // At this point, no more strings will be added:
963
+ // - There's only a single Java thread (this thread). It no longer executes Java bytecodes
964
+ // so JIT compilation will eventually stop.
965
+ // - CompileBroker has no more active tasks, so all JIT requests have been processed.
966
+
967
+ // This flag will be cleared after intern table dumping has completed, so we can run the
968
+ // compiler again (for future AOT method compilation, etc).
969
+ DEBUG_ONLY (Atomic::release_store (&_disable_interning_during_cds_dump, true ));
970
+
971
+ if (items_count_acquire () > (size_t )max_jint) {
972
+ fatal (" Too many strings to be archived: %zu" , items_count_acquire ());
953
973
}
954
974
955
- int total = (int )_items_count ;
975
+ int total = (int )items_count_acquire () ;
956
976
size_t single_array_size = objArrayOopDesc::object_size (total);
957
977
958
978
log_info (aot)(" allocated string table for %d strings" , total);
@@ -972,7 +992,7 @@ void StringTable::allocate_shared_strings_array(TRAPS) {
972
992
// This can only happen if you have an extremely large number of classes that
973
993
// refer to more than 16384 * 16384 = 26M interned strings! Not a practical concern
974
994
// but bail out for safety.
975
- log_error (aot)(" Too many strings to be archived: %zu" , _items_count );
995
+ log_error (aot)(" Too many strings to be archived: %zu" , items_count_acquire () );
976
996
MetaspaceShared::unrecoverable_writing_error ();
977
997
}
978
998
@@ -1070,7 +1090,7 @@ oop StringTable::init_shared_strings_array() {
1070
1090
1071
1091
void StringTable::write_shared_table () {
1072
1092
_shared_table.reset ();
1073
- CompactHashtableWriter writer ((int )_items_count , ArchiveBuilder::string_stats ());
1093
+ CompactHashtableWriter writer ((int )items_count_acquire () , ArchiveBuilder::string_stats ());
1074
1094
1075
1095
int index = 0 ;
1076
1096
auto copy_into_shared_table = [&] (WeakHandle* val) {
@@ -1084,6 +1104,8 @@ void StringTable::write_shared_table() {
1084
1104
};
1085
1105
_local_table->do_safepoint_scan (copy_into_shared_table);
1086
1106
writer.dump (&_shared_table, " string" );
1107
+
1108
+ DEBUG_ONLY (Atomic::release_store (&_disable_interning_during_cds_dump, false ));
1087
1109
}
1088
1110
1089
1111
void StringTable::set_shared_strings_array_index (int root_index) {
0 commit comments