-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathCacheMap.cpp
7125 lines (6348 loc) · 282 KB
/
CacheMap.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 IBM Corp. and others 2001
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
* or the Apache License, Version 2.0 which accompanies this distribution and
* is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following
* Secondary Licenses when the conditions for such availability set
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
* General Public License, version 2 with the GNU Classpath
* Exception [1] and GNU General Public License, version 2 with the
* OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] https://openjdk.org/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0
*******************************************************************************/
#include "CacheMap.hpp"
#include "Managers.hpp"
#include "ClasspathManagerImpl2.hpp"
#include "ROMClassManagerImpl.hpp"
#include "TimestampManagerImpl.hpp"
#include "CompiledMethodManagerImpl.hpp"
#include "ScopeManagerImpl.hpp"
#include "ByteDataManagerImpl.hpp"
#include "AttachedDataManagerImpl.hpp"
#include "CompositeCacheImpl.hpp"
#include "UnitTest.hpp"
#include "AtomicSupport.hpp"
#include "ut_j9shr.h"
#include "j2sever.h"
#include "j9shrnls.h"
#include "j9comp.h"
#include "j9consts.h"
#include <string.h>
extern "C" {
#include "shrinit.h"
}
/* Trace macros should be used if messages should be affected by verboseLevel */
#define CACHEMAP_TRACE(verboseLevel, nlsFlags, var1) if (_verboseFlags & verboseLevel) j9nls_printf(PORTLIB, nlsFlags, var1)
#define CACHEMAP_TRACE1(verboseLevel, nlsFlags, var1, p1) if (_verboseFlags & verboseLevel) j9nls_printf(PORTLIB, nlsFlags, var1, p1)
#define CACHEMAP_TRACE2(verboseLevel, nlsFlags, var1, p1, p2) if (_verboseFlags & verboseLevel) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2)
#define CACHEMAP_TRACE3(verboseLevel, nlsFlags, var1, p1, p2, p3) if (_verboseFlags & verboseLevel) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3)
#define CACHEMAP_TRACE4(verboseLevel, nlsFlags, var1, p1, p2, p3, p4) if (_verboseFlags & verboseLevel) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3, p4)
/* Print macros should be used if message should always be printed */
#define CACHEMAP_PRINT(nlsFlags, var1) j9nls_printf(PORTLIB, nlsFlags, var1)
#define CACHEMAP_PRINT1(nlsFlags, var1, p1) j9nls_printf(PORTLIB, nlsFlags, var1, p1)
#define CACHEMAP_PRINT2(nlsFlags, var1, p1, p2) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2)
#define CACHEMAP_PRINT3(nlsFlags, var1, p1, p2, p3) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3)
#define CACHEMAP_PRINT4(nlsFlags, var1, p1, p2, p3, p4) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3, p4)
#define CACHEMAP_PRINT5(nlsFlags, var1, p1, p2, p3, p4, p5) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3, p4, p5)
#define CACHEMAP_PRINT6(nlsFlags, var1, p1, p2, p3, p4, p5, p6) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3, p4, p5, p6)
#define CACHEMAP_PRINT7(nlsFlags, var1, p1, p2, p3, p4, p5, p6, p7) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3, p4, p5, p6, p7)
#define CACHEMAP_PRINT8(nlsFlags, var1, p1, p2, p3, p4, p5, p6, p7, p8) j9nls_printf(PORTLIB, nlsFlags, var1, p1, p2, p3, p4, p5, p6, p7, p8)
#define CACHEMAP_FMTPRINT1(nlsFlags, var1, p1) j9nls_printf(PORTLIB, nlsFlags, var1, 1,' ',p1)
static char* formatAttachedDataString(J9VMThread* currentThread, U_8 *attachedData, UDATA attachedDataLength, char *attachedDataStringBuffer, UDATA bufferLength);
static void checkROMClassUTF8SRPs(J9ROMClass *romClass);
/* If you make this sleep a lot longer, it almost eliminates store contention
* because the VMs get out of step with each other, but you delay excessively */
#define WRITE_HASH_WAIT_MAX_MICROS 80000
#define DEFAULT_WRITE_HASH_WAIT_MILLIS 10
#define WRITE_HASH_DEFAULT_MAX_MICROS 20000
#define MARK_STALE_RETRY_TIMES 10
#define VERBOSE_BUFFER_SIZE 255
/* TODO: May want to make this cachelet size configurable */
#define J9SHR_DEFAULT_CACHELET_SIZE (1024 * 1024)
#define J9SHR_NESTED_CACHE_TEMP_NAME "nested_temp"
/* All flags should be false. Don't EVER update the cache when set. */
#define RUNTIME_FLAGS_PREVENT_BLOCK_DATA_UPDATE (J9SHR_RUNTIMEFLAG_DENY_CACHE_UPDATES | J9SHR_RUNTIMEFLAG_BLOCK_SPACE_FULL | J9SHR_RUNTIMEFLAG_AVAILABLE_SPACE_FULL)
#define RUNTIME_FLAGS_PREVENT_AOT_DATA_UPDATE (J9SHR_RUNTIMEFLAG_DENY_CACHE_UPDATES | J9SHR_RUNTIMEFLAG_AOT_SPACE_FULL)
#define RUNTIME_FLAGS_PREVENT_JIT_DATA_UPDATE (J9SHR_RUNTIMEFLAG_DENY_CACHE_UPDATES | J9SHR_RUNTIMEFLAG_JIT_SPACE_FULL)
#define MAX_INT 0x7fffffff
#define FIND_ATTACHED_DATA_RETRY_COUNT 1
#define FIND_ATTACHED_DATA_CORRUPT_WAIT_TIME 1
struct TR_AOTHeader;
/**
* @param currentThread - the currentThread or NULL when called to collect javacore data
*
* Assumes the mutex can't be entered recursively
*/
IDATA
SH_CacheMap::enterLocalMutex(J9VMThread* currentThread, omrthread_monitor_t monitor, const char* name, const char* caller)
{
if (_isAssertEnabled) {
Trc_SHR_Assert_ShouldNotHaveLocalMutex(monitor);
}
/* WARNING - currentThread can be NULL */
return enterReentrantLocalMutex(currentThread, monitor, name, caller);
}
/**
* @param currentThread - the currentThread or NULL when called to collect javacore data
*
* Assumes the mutex can't be entered recursively
*/
IDATA
SH_CacheMap::exitLocalMutex(J9VMThread* currentThread, omrthread_monitor_t monitor, const char* name, const char* caller)
{
if (_isAssertEnabled) {
Trc_SHR_Assert_ShouldHaveLocalMutex(monitor);
}
/* WARNING - currentThread can be NULL */
return exitReentrantLocalMutex(currentThread, monitor, name, caller);
}
/**
* @param currentThread - the currentThread or NULL when called to collect javacore data
*/
IDATA
SH_CacheMap::enterReentrantLocalMutex(J9VMThread* currentThread, omrthread_monitor_t monitor, const char* name, const char* caller)
{
IDATA rc = 0;
/* WARNING - currentThread can be NULL */
Trc_SHR_CM_enterLocalMutex_pre(currentThread, name, caller);
rc = omrthread_monitor_enter(monitor);
Trc_SHR_CM_enterLocalMutex_post(currentThread, name, rc, caller);
return rc;
}
/**
* @param currentThread - the currentThread or NULL when called to collect javacore data
*/
IDATA
SH_CacheMap::exitReentrantLocalMutex(J9VMThread* currentThread, omrthread_monitor_t monitor, const char* name, const char* caller)
{
IDATA rc = 0;
/* WARNING - currentThread can be NULL */
Trc_SHR_CM_exitLocalMutex_pre(currentThread, name, caller);
rc = omrthread_monitor_exit(monitor);
Trc_SHR_CM_exitLocalMutex_post(currentThread, name, rc, caller);
return rc;
}
IDATA
SH_CacheMap::enterRefreshMutex(J9VMThread* currentThread, const char* caller)
{
IDATA rc;
if ((rc = enterReentrantLocalMutex(currentThread, _refreshMutex, "_refreshMutex", caller)) == 0) {
if (1 == ((J9ThreadAbstractMonitor*)_refreshMutex)->count) {
/* nonrecursive enter */
SH_CompositeCacheImpl* ccToUse = _ccHead;
do {
ccToUse->notifyRefreshMutexEntered(currentThread);
ccToUse = ccToUse->getNext();
} while (NULL != ccToUse);
}
}
return rc;
}
IDATA
SH_CacheMap::exitRefreshMutex(J9VMThread* currentThread, const char* caller)
{
IDATA rc;
Trc_SHR_Assert_ShouldHaveLocalMutex(_refreshMutex);
if (1 == ((J9ThreadAbstractMonitor*)_refreshMutex)->count) {
/* nonrecursive exit */
SH_CompositeCacheImpl* ccToUse = _ccHead;
do {
ccToUse->notifyRefreshMutexExited(currentThread);
ccToUse = ccToUse->getNext();
} while (NULL != ccToUse);
}
rc = exitReentrantLocalMutex(currentThread, _refreshMutex, "_refreshMutex", caller);
return rc;
}
/**
* Builds a new SH_CacheMap
* THREADING: Only ever single threaded
*
* @param [in] vm A Java VM
* @param [in] sharedClassConfig
* @param [in] memForConstructor Should be memory of the size from getRequiredConstrBytes
* @param [in] cacheName The name of the cache
* @param [in] cacheTypeRequired The cache type required
*
* @return A pointer to the new CacheMap
*/
SH_CacheMap*
SH_CacheMap::newInstance(J9JavaVM* vm, J9SharedClassConfig* sharedClassConfig, SH_CacheMap* memForConstructor, const char* cacheName, I_32 cacheTypeRequired)
{
SH_CacheMap* newCacheMap = memForConstructor;
I_8 topLayer = 0;
if (NULL != sharedClassConfig) {
/* sharedClassConfig can be null in shrtest */
topLayer = sharedClassConfig->layer;
}
Trc_SHR_CM_newInstance_Entry(vm);
new(newCacheMap) SH_CacheMap();
newCacheMap->initialize(vm, sharedClassConfig, ((BlockPtr)memForConstructor + sizeof(SH_CacheMap)), cacheName, cacheTypeRequired, topLayer, false);
Trc_SHR_CM_newInstance_Exit();
return newCacheMap;
}
/**
* Advise the OS to release resources associated with
* the metadata which have been accessed to date.
*/
void
SH_CacheMap::dontNeedMetadata(J9VMThread* currentThread)
{
Trc_SHR_CM_j9shr_dontNeedMetadata(currentThread);
SH_CompositeCacheImpl* ccToUse = _ccHead;
if (_metadataReleaseCounter >= CM_CACHE_MAX_METADATA_RELEASES) {
return;
}
_metadataReleaseCounter += 1;
do {
ccToUse->dontNeedMetadata(currentThread);
ccToUse = ccToUse->getNext();
} while (NULL != ccToUse);
}
/**
* Builds a new SH_CacheMap for retrieving cache statistics
*
* @param [in] vm A Java VM
* @param [in] memForConstructor Should be memory of the size from getRequiredConstrBytes
* @param [in] cacheName The name of the cache
* @param [in] topLayer the top layer number
*
* @return A pointer to the CacheMapStats
*/
SH_CacheMapStats*
SH_CacheMap::newInstanceForStats(J9JavaVM* vm, SH_CacheMap* memForConstructor, const char* cacheName, I_8 topLayer)
{
SH_CacheMap* newCacheMap = memForConstructor;
Trc_SHR_CM_newInstanceForStats_Entry(vm);
new(newCacheMap) SH_CacheMap();
newCacheMap->initialize(vm, NULL, ((BlockPtr)memForConstructor + sizeof(SH_CacheMap)), cacheName, 0, topLayer, true);
Trc_SHR_CM_newInstanceForStats_Exit();
return newCacheMap;
}
/* THREADING: Only ever single threaded */
void
SH_CacheMap::initialize(J9JavaVM* vm, J9SharedClassConfig* sharedClassConfig, BlockPtr memForConstructor, const char* cacheName, I_32 cacheTypeRequired, I_8 topLayer, bool startupForStats)
{
BlockPtr allocPtr = memForConstructor;
Trc_SHR_CM_initialize_Entry1(UnitTest::unitTest);
_sharedClassConfig = sharedClassConfig;
_portlib = vm->portLibrary;
_cacheCorruptReported = false;
_refreshMutex = NULL;
_writeHashMaxWaitMicros = WRITE_HASH_DEFAULT_MAX_MICROS;
_writeHashSavedMaxWaitMicros = 0;
_writeHashAverageTimeMicros = 0;
_writeHashContendedResetHash = 0;
_bytesRead = 0;
_isAssertEnabled = true;
_metadataReleaseCounter = 0;
_ccPool = NULL;
_managers = SH_Managers::newInstance(vm, (SH_Managers *)allocPtr);
_ccHead = _cc = SH_CompositeCacheImpl::newInstance(vm, sharedClassConfig, (SH_CompositeCacheImpl*)(allocPtr += SH_Managers::getRequiredConstrBytes()), cacheName, cacheTypeRequired, startupForStats, topLayer);
_ccHead->setNext(NULL);
_ccHead->setPrevious(NULL);
_ccTail = _ccHead;
memset(_cacheAddressRangeArray, 0, sizeof(_cacheAddressRangeArray));
_numOfCacheLayers = 0;
_tsm = SH_TimestampManagerImpl::newInstance(vm, (SH_TimestampManagerImpl*)(allocPtr += SH_CompositeCacheImpl::getRequiredConstrBytesWithCommonInfo(false, startupForStats)), sharedClassConfig);
_cpm = SH_ClasspathManagerImpl2::newInstance(vm, this, _tsm, (SH_ClasspathManagerImpl2*)(allocPtr += SH_TimestampManagerImpl::getRequiredConstrBytes()));
_scm = SH_ScopeManagerImpl::newInstance(vm, this, (SH_ScopeManagerImpl*)(allocPtr += SH_ClasspathManagerImpl2::getRequiredConstrBytes()));
_rcm = SH_ROMClassManagerImpl::newInstance(vm, this, _tsm, (SH_ROMClassManagerImpl*)(allocPtr += SH_ScopeManagerImpl::getRequiredConstrBytes()));
_cmm = SH_CompiledMethodManagerImpl::newInstance(vm, this, (SH_CompiledMethodManagerImpl*)(allocPtr += SH_ROMClassManagerImpl::getRequiredConstrBytes()));
_bdm = SH_ByteDataManagerImpl::newInstance(vm, this, (SH_ByteDataManagerImpl*)(allocPtr += SH_CompiledMethodManagerImpl::getRequiredConstrBytes()));
_adm = SH_AttachedDataManagerImpl::newInstance(vm, this, (SH_AttachedDataManagerImpl*)(allocPtr += SH_ByteDataManagerImpl::getRequiredConstrBytes()));
Trc_SHR_CM_initialize_Exit();
}
/**
* Returns memory bytes the constructor requires to build what it needs
* THREADING: Only ever single threaded
*
* @return bytes required
*/
UDATA
SH_CacheMap::getRequiredConstrBytes(bool startupForStats)
{
UDATA reqBytes = 0;
reqBytes += SH_CompositeCacheImpl::getRequiredConstrBytesWithCommonInfo(false, startupForStats);
reqBytes += SH_TimestampManagerImpl::getRequiredConstrBytes();
reqBytes += SH_ClasspathManagerImpl2::getRequiredConstrBytes();
reqBytes += SH_ROMClassManagerImpl::getRequiredConstrBytes();
reqBytes += SH_ScopeManagerImpl::getRequiredConstrBytes();
reqBytes += SH_CompiledMethodManagerImpl::getRequiredConstrBytes();
reqBytes += SH_ByteDataManagerImpl::getRequiredConstrBytes();
reqBytes += SH_AttachedDataManagerImpl::getRequiredConstrBytes();
reqBytes += SH_Managers::getRequiredConstrBytes();
reqBytes += sizeof(SH_CacheMap);
return reqBytes;
}
/**
* Clean up resources
*
* THREADING: Only ever single threaded
*/
void
SH_CacheMap::cleanup(J9VMThread* currentThread)
{
SH_Manager* walkManager;
SH_Managers::ManagerWalkState state;
SH_CompositeCacheImpl* theCC = _ccHead;
PORT_ACCESS_FROM_PORT(_portlib);
Trc_SHR_CM_cleanup_Entry(currentThread);
walkManager = managers()->startDo(currentThread, 0, &state);
while (walkManager) {
walkManager->cleanup(currentThread);
walkManager = managers()->nextDo(&state);
}
while (theCC) {
SH_CompositeCacheImpl* nextCC = theCC->getNext();
theCC->cleanup(currentThread);
if (_ccHead != theCC) {
/* _ccHead is deallocated together with sharedClassConfig in j9shr_shutdown() */
j9mem_free_memory(theCC);
}
theCC = nextCC;
}
if (_sharedClassConfig) {
this->resetCacheDescriptorList(currentThread, _sharedClassConfig);
}
if (_refreshMutex) {
omrthread_monitor_destroy(_refreshMutex);
_refreshMutex = NULL;
}
if (_ccPool) {
pool_kill(_ccPool);
}
Trc_SHR_CM_cleanup_Exit(currentThread);
}
/* Walks the ROMClasses in the ROMClass segment performing a quick check to make
* sure that the walk produces sensible results. Any failure in the walk should result
* in a corrupted cache behaviour. */
UDATA
SH_CacheMap::sanityWalkROMClassSegment(J9VMThread* currentThread, SH_CompositeCacheImpl* cache)
{
U_8 *walk, *prev, *endOfROMSegment;
PORT_ACCESS_FROM_PORT(_portlib);
Trc_SHR_CM_sanityWalkROMClassSegment_Entry(currentThread);
endOfROMSegment = (U_8*)cache->getSegmentAllocPtr();
walk = (U_8*)cache->getBaseAddress();
while (walk < endOfROMSegment) {
prev = walk;
walk = walk + ((J9ROMClass*)walk)->romSize;
/* Simply check that we didn't walk backwards, zero or beyond the end of the segment */
if ((walk <= prev) || (walk > endOfROMSegment)) {
Trc_SHR_CM_sanityWalkROMClassSegment_ExitBad(currentThread, prev, walk);
CACHEMAP_TRACE1(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_READ_CORRUPT_ROMCLASS, walk);
cache->setCorruptCache(currentThread, ROMCLASS_CORRUPT, (UDATA)walk);
return 0;
}
}
Trc_SHR_CM_sanityWalkROMClassSegment_ExitOK(currentThread);
return 1;
}
/**
* Start up this CacheMap. Should be called after initialization.
* Sets up access to (or creates) the shared cache and registers it as a ROMClassSegment with the vm.
* THREADING: Only ever single threaded
*
* @param [in] currentThread The current thread
* @param [in] piconfig The shared class pre-init config
* @param [in] rootName The name of the cache to connect to
* @param [in] cacheDirName The location of the cache file(s)
* @param [in] cacheMemoryUT Used for unit testing. If provided, cache is built into this buffer.
* @param [out] cacheHasIntegrity Set to true if the cache is new or has been crc integrity checked, false otherwise
*
* @return 0 on success or -1 for failure
*/
IDATA
SH_CacheMap::startup(J9VMThread* currentThread, J9SharedClassPreinitConfig* piconfig, const char* rootName, const char* cacheDirName, UDATA cacheDirPerm, BlockPtr cacheMemoryUT, bool* cacheHasIntegrity)
{
IDATA itemsRead = 0;
IDATA rc = 0;
const char* fnName = "startup";
J9JavaVM* vm = currentThread->javaVM;
IDATA deleteRC = 1;
PORT_ACCESS_FROM_PORT(_portlib);
SH_CompositeCacheImpl* ccToUse = _ccHead;
SH_CompositeCacheImpl* ccNext = NULL;
SH_CompositeCacheImpl* ccPrevious = NULL;
bool isCacheUniqueIdStored = false;
_actualSize = (U_32)piconfig->sharedClassCacheSize;
Trc_SHR_CM_startup_Entry(currentThread, rootName, _actualSize);
if (_sharedClassConfig) {
_runtimeFlags = &(_sharedClassConfig->runtimeFlags);
_verboseFlags = _sharedClassConfig->verboseFlags;
}
_cacheName = rootName; /* Store the original name as the cache name */
_cacheDir = cacheDirName;
if (*_runtimeFlags & J9SHR_RUNTIMEFLAG_ENABLE_READONLY) {
/* If running readonly, we can't recreate a cache after we delete it, so disable autopunt */
*_runtimeFlags &= ~J9SHR_RUNTIMEFLAG_AUTOKILL_DIFF_BUILDID;
}
if (omrthread_monitor_init(&_refreshMutex, 0)) {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_CREATE_REFRESH_MUTEX);
Trc_SHR_CM_startup_Exit5(currentThread);
return -1;
}
/* _ccHead->startup will set the _actualSize to the real cache size */
U_32 cacheFileSize = 0;
bool doRetry = false;
U_64* runtimeFlags = _runtimeFlags;
char cacheUniqueID[J9SHR_UNIQUE_CACHE_ID_BUFSIZE];
memset(cacheUniqueID, 0, sizeof(cacheUniqueID));
do {
IDATA tryCntr = 0;
bool isCcHead = (ccToUse == _ccHead);
bool storeToCcHead = (ccPrevious == _ccHead);
const char* cacheUniqueIDPtr = NULL;
/* start up _ccHead (the top layer cache) and then statrt its pre-requiste cache (ccNext). Contine to startup ccNext and its pre-requiste cache, util there is no more pre-requiste cache.
* _ccHead -------------> ccNext ---------> ccNext --------> ........---------> ccTail
* (top layer) (middle layer) (middle layer) ........ (layer 0)
*/
if (!isCcHead) {
runtimeFlags = &_sharedClassConfig->readOnlyCacheRuntimeFlags;
}
do {
++tryCntr;
doRetry = false;
if ((rc == CC_STARTUP_SOFT_RESET) && (deleteRC == -1)) {
/* If we've tried SOFT_RESET the first time around and the delete failed,
* remove AUTOKILL so that we start up with the existing cache */
*runtimeFlags &= ~J9SHR_RUNTIMEFLAG_AUTOKILL_DIFF_BUILDID;
}
rc = ccToUse->startup(currentThread, piconfig, cacheMemoryUT, runtimeFlags, _verboseFlags, _cacheName, cacheDirName, cacheDirPerm, &_actualSize, &_localCrashCntr, true, cacheHasIntegrity);
if (rc == CC_STARTUP_OK) {
if (sanityWalkROMClassSegment(currentThread, ccToUse) == 0) {
rc = CC_STARTUP_CORRUPT;
goto error;
}
if (!isCcHead) {
if (NULL == appendCacheDescriptorList(currentThread, _sharedClassConfig, ccToUse)) {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_ALLOC_DESCRIPTOR);
Trc_SHR_CM_startup_Exit12(currentThread);
return -1;
}
}
UDATA idLen = 0;
bool isReadOnly = ccToUse->isRunningReadOnly();
char cacheDirBuf[J9SH_MAXPATH];
U_32 cacheType = J9_ARE_ALL_BITS_SET(*runtimeFlags, J9SHR_RUNTIMEFLAG_ENABLE_PERSISTENT_CACHE) ? J9PORT_SHR_CACHE_TYPE_PERSISTENT : J9PORT_SHR_CACHE_TYPE_NONPERSISTENT;
SH_OSCache::getCacheDir(vm, cacheDirName, cacheDirBuf, J9SH_MAXPATH, cacheType, false);
if (storeToCcHead && !isCacheUniqueIdStored && !ccPrevious->isRunningReadOnly()) {
if (ccPrevious->enterWriteMutex(currentThread, false, fnName) == 0) {
storeCacheUniqueID(currentThread, cacheDirBuf, ccToUse->getCreateTime(), ccToUse->getMetadataBytes(), ccToUse->getClassesBytes(), ccToUse->getLineNumberTableBytes(), ccToUse->getLocalVariableTableBytes(), &cacheUniqueIDPtr, &idLen);
Trc_SHR_Assert_True(idLen < sizeof(cacheUniqueID));
memcpy(cacheUniqueID, cacheUniqueIDPtr, idLen);
cacheUniqueID[idLen] = 0;
ccPrevious->exitWriteMutex(currentThread, fnName);
} else {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_ENTER_WRITE_MUTEX_STARTUP);
Trc_SHR_CM_startup_Exit7(currentThread);
return -1;
}
}
if (ccToUse->enterWriteMutex(currentThread, false, fnName) == 0) {
if (false == isCcHead) {
if (strlen(cacheUniqueID) > 0) {
if (false == ccToUse->verifyCacheUniqueID(currentThread, cacheUniqueID)) {
/* modification to a low layer cache has been detected */
if (_ccHead->isNewCache()) {
CACHEMAP_TRACE4(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_NEW_LAYER_CACHE_DESTROYED, _ccHead->getLayer(), ccToUse->getLayer(), cacheUniqueID, ccToUse->getCacheUniqueID(currentThread));
_ccHead->deleteCache(currentThread, true);
}
CACHEMAP_TRACE1(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_VERIFY_CACHE_ID_FAILED, cacheUniqueID);
ccToUse->exitWriteMutex(currentThread, fnName);
Trc_SHR_CM_startup_Exit8(currentThread);
return -1;
}
}
}
if (J9_ARE_ANY_BITS_SET(*runtimeFlags, J9SHR_RUNTIMEFLAG_ENABLE_REDUCE_STORE_CONTENTION) && !isReadOnly) {
ccToUse->setWriteHash(currentThread, 0); /* Initialize to zero so that peek will work */
}
IDATA preqRC = getPrereqCache(currentThread, cacheDirBuf, ccToUse, false, &cacheUniqueIDPtr, &idLen, &isCacheUniqueIdStored);
if (0 > preqRC) {
if (CM_CACHE_CORRUPT == preqRC) {
rc = CC_STARTUP_CORRUPT;
SH_Managers::ManagerWalkState state;
SH_Manager* walkManager = managers()->startDo(currentThread, 0, &state);
while (walkManager) {
walkManager->cleanup(currentThread);
walkManager = managers()->nextDo(&state);
}
ccToUse->exitWriteMutex(currentThread, fnName);
goto error;
}
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_GET_PREREQ_CACHE_FAILED);
ccToUse->exitWriteMutex(currentThread, fnName);
Trc_SHR_CM_startup_Exit9(currentThread, preqRC);
return -1;
} else if (1 == preqRC) {
UDATA reqBytes = SH_CompositeCacheImpl::getRequiredConstrBytesWithCommonInfo(false, false);
SH_CompositeCacheImpl* allocPtr = (SH_CompositeCacheImpl*)j9mem_allocate_memory(reqBytes, J9MEM_CATEGORY_CLASSES);
if (NULL == allocPtr) {
ccToUse->exitWriteMutex(currentThread, fnName);
CACHEMAP_TRACE1(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_MEMORY_ALLOC_FAILED, reqBytes);
Trc_SHR_CM_startup_Exit10(currentThread);
return -1;
}
if (0 == _sharedClassConfig->readOnlyCacheRuntimeFlags) {
_sharedClassConfig->readOnlyCacheRuntimeFlags = (_sharedClassConfig->runtimeFlags | J9SHR_RUNTIMEFLAG_ENABLE_READONLY);
_sharedClassConfig->readOnlyCacheRuntimeFlags &= ~J9SHR_RUNTIMEFLAG_AUTOKILL_DIFF_BUILDID;
_readOnlyCacheRuntimeFlags = &_sharedClassConfig->readOnlyCacheRuntimeFlags;
}
I_8 preLayer = 0;
const char* cacheName = _cacheName;
char cacheNameBuf[USER_SPECIFIED_CACHE_NAME_MAXLEN];
if (isCacheUniqueIdStored) {
Trc_SHR_Assert_True(idLen < sizeof(cacheUniqueID));
memcpy(cacheUniqueID, cacheUniqueIDPtr, idLen);
cacheUniqueID[idLen] = 0;
SH_OSCache::getCacheNameAndLayerFromUnqiueID(vm, cacheUniqueID, idLen, cacheNameBuf, USER_SPECIFIED_CACHE_NAME_MAXLEN, &preLayer);
cacheName = cacheNameBuf;
} else {
/**
* The CacheUniqueID of the pre-requisite cache is not stored when a new layer of cache is created (using createLayer or layer=<num> option).
* Thus, we get the CacheUniqueID of the current cache and decrement the layer number by 1 to get the cacheName and layer number of the pre-requisite cache.
*/
preLayer = _sharedClassConfig->layer - 1;
}
ccNext = SH_CompositeCacheImpl::newInstance(vm, _sharedClassConfig, allocPtr, cacheName, cacheType, false, preLayer);
ccNext->setNext(NULL);
ccNext->setPrevious(ccToUse);
ccToUse->setNext(ccNext);
ccPrevious = ccToUse;
_ccTail = ccNext;
} else {
/* no prereq cache, do nothing */
}
ccToUse->exitWriteMutex(currentThread, fnName);
} else {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_ENTER_WRITE_MUTEX_STARTUP);
Trc_SHR_CM_startup_Exit7(currentThread);
return -1;
}
}
error:
if (CC_STARTUP_OK != rc) {
if (isCcHead) {
cacheFileSize = _ccHead->getTotalSize();
}
handleStartupError(currentThread, ccToUse, rc, *runtimeFlags, _verboseFlags, &doRetry, &deleteRC);
if (isCcHead && doRetry) {
if (cacheFileSize > 0) {
/* If we're recreating, make the new cache the same size as the old
* Cache may be corrupt, so don't rely on values in the cache header to determine size */
piconfig->sharedClassCacheSize = cacheFileSize;
}
}
}
} while (doRetry && (tryCntr < 2));
ccToUse = ccToUse->getNext();
} while (NULL != ccToUse && CC_STARTUP_OK == rc);
if (CC_STARTUP_OK != rc) {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_TO_START_UP);
Trc_SHR_CM_startup_Exit11(currentThread);
if (CC_STARTUP_NO_CACHE == rc) {
return -2;
}
return rc;
}
setCacheAddressRangeArray();
ccToUse = _ccTail;
if (J9_ARE_ALL_BITS_SET(*runtimeFlags, J9SHR_RUNTIMEFLAG_ENABLE_STATS)) {
if (UnitTest::CORRUPT_CACHE_TEST != UnitTest::unitTest) {
Trc_SHR_Assert_True(J9_ARE_ALL_BITS_SET(*runtimeFlags, J9SHR_RUNTIMEFLAG_ENABLE_READONLY));
}
if (J9_ARE_ALL_BITS_SET(vm->sharedCacheAPI->printStatsOptions, PRINTSTATS_SHOW_TOP_LAYER_ONLY)) {
ccToUse = _ccHead;
}
}
do {
if (ccToUse == _ccHead) {
runtimeFlags = _runtimeFlags;
} else {
runtimeFlags = _readOnlyCacheRuntimeFlags;
}
bool isReadOnly = ccToUse->isRunningReadOnly();
/* THREADING: We want the cache mutex here as we are reading all available data. Don't want updates happening as we read. */
if (ccToUse->enterWriteMutex(currentThread, false, fnName) == 0) {
/* populate the hashtables */
itemsRead = readCache(currentThread, ccToUse, -1, false);
ccToUse->protectPartiallyFilledPages(currentThread);
/* Two reasons for moving the code to check for full cache from SH_CompositeCacheImpl::startup()
* to SH_CacheMap::startup():
* - While marking cache full, last unsused pages are also protected, which ideally should be done
* after protecting pages belonging to ROMClass area and metadata area.
* - Secondly, when setting cache full flags, the code expects to be holding the write mutex, which is not done in
* SH_CompositeCacheImpl::startup().
*
* Do not call fillCacheIfNearlyFull() in readonly mode, as we cannot write anything to cache.
*/
if (!isReadOnly) {
ccToUse->fillCacheIfNearlyFull(currentThread);
}
ccToUse->exitWriteMutex(currentThread, fnName);
if (CM_READ_CACHE_FAILED == itemsRead) {
Trc_SHR_CM_startup_Exit6(currentThread);
return -1;
}
if (CM_CACHE_CORRUPT == itemsRead) {
Trc_SHR_CM_startup_Exit13(currentThread);
rc = CC_STARTUP_CORRUPT;
SH_Managers::ManagerWalkState state;
SH_Manager* walkManager = managers()->startDo(currentThread, 0, &state);
while (walkManager) {
walkManager->cleanup(currentThread);
walkManager = managers()->nextDo(&state);
}
}
} else {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_ENTER_WRITE_MUTEX_STARTUP);
Trc_SHR_CM_startup_Exit7(currentThread);
return -1;
}
if (CC_STARTUP_OK == rc) {
if (isReadOnly) {
*runtimeFlags |= J9SHR_RUNTIMEFLAG_ENABLE_READONLY;
/* If running read-only, treat the cache as full */
ccToUse->markReadOnlyCacheFull();
}
ccToUse = ccToUse->getPrevious();
}
} while (NULL != ccToUse && CC_STARTUP_OK == rc);
if (rc != CC_STARTUP_OK) {
handleStartupError(currentThread, ccToUse, rc, *runtimeFlags, _verboseFlags, &doRetry, &deleteRC);
Trc_SHR_CM_startup_Exit1(currentThread);
return -1;
}
if (!initializeROMSegmentList(currentThread)) {
CACHEMAP_TRACE(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_FAILED_CREATE_ROMIMAGE);
Trc_SHR_CM_startup_Exit4(currentThread);
return -1;
}
updateROMSegmentList(currentThread, false, false);
Trc_SHR_CM_startup_ExitOK(currentThread);
return 0;
}
/**
* Handle the SH_CompositeCacheImpl start up error
*
* @param [in] currentThread The current thread
* @param [in] ccToUse The SH_CompositeCacheImpl that was being started up
* @param [in] errorCode The SH_CompositeCacheImpl startup error code
* @param [in] runtimeFlags The runtime flags
* @param [in] verboseFlags Flags controlling the verbose output
* @param [out] doRetry Whether to retry starting up the cache
* @param [out] deleteRC 0 if cache is successful deleted, -1 otherwise.
*/
void
SH_CacheMap::handleStartupError(J9VMThread* currentThread, SH_CompositeCacheImpl* ccToUse, IDATA errorCode, U_64 runtimeFlags, UDATA verboseFlags, bool *doRetry, IDATA *deleteRC)
{
if (errorCode == CC_STARTUP_CORRUPT) {
reportCorruptCache(currentThread, ccToUse);
}
if (J9_ARE_NO_BITS_SET(runtimeFlags, J9SHR_RUNTIMEFLAG_ENABLE_STATS | J9SHR_RUNTIMEFLAG_FAKE_CORRUPTION)
&& (false == ccToUse->isRunningReadOnly())
) {
/* If the cache is readonly do not delete it or call cleanup().
* Cleanup is already called during j9shr_shutdown().
* Destroy and clean up only are needed if a 2nd cache is to be opened.
*
* If the cache is being opened to display stats then do not delete it.
*/
if ((errorCode == CC_STARTUP_CORRUPT) || (errorCode == CC_STARTUP_RESET) || (errorCode == CC_STARTUP_SOFT_RESET)) {
/* If SOFT_RESET, suppress verbose unless "verbose" is explicitly set
* This will ensure that if the VM can't destroy the cache, we don't get unwanted error messages */
*deleteRC = ccToUse->deleteCache(currentThread, (errorCode == CC_STARTUP_SOFT_RESET) && !(verboseFlags & J9SHR_VERBOSEFLAG_ENABLE_VERBOSE));
ccToUse->cleanup(currentThread);
if (0 == *deleteRC) {
if (errorCode == CC_STARTUP_CORRUPT) {
/* Recovering from a corrupted cache, clear the flags which prevent access */
resetCorruptState(currentThread, FALSE);
}
}
if (J9_ARE_NO_BITS_SET(runtimeFlags, J9SHR_RUNTIMEFLAG_RESTORE_CHECK)) {
/* If the restored cache is corrupted, return CC_STARTUP_CORRUPT and do not retry,
* as retry will create another empty cache that is not restored from the snapshot
*/
if ((0 == *deleteRC) || (errorCode == CC_STARTUP_SOFT_RESET)) {
/* If we deleted the cache, or in the case of SOFT_RESET, even if we failed to delete the cache, retry */
Trc_SHR_Assert_True(ccToUse == _ccHead);
*_runtimeFlags &= ~J9SHR_RUNTIMEFLAG_DO_NOT_CREATE_CACHE;
*doRetry = true;
}
}
}
}
}
/* Assume cc is initialized OK */
/* THREADING: Only ever single threaded */
/* Creates a new ROMClass memory segment and adds it to the avl tree */
J9MemorySegment*
SH_CacheMap::addNewROMImageSegment(J9VMThread* currentThread, U_8* segmentBase, U_8* segmentEnd)
{
J9MemorySegment* romSegment = NULL;
J9JavaVM* vm = currentThread->javaVM;
UDATA type = MEMORY_TYPE_ROM_CLASS | MEMORY_TYPE_ROM | MEMORY_TYPE_FIXEDSIZE;
Trc_SHR_CM_addNewROMImageSegment_Entry(currentThread, segmentBase, segmentEnd);
if ((romSegment = createNewSegment(currentThread, type, vm->classMemorySegments, segmentBase, segmentBase, segmentEnd, segmentBase)) != NULL) {
avl_insert(&vm->classMemorySegments->avlTreeData, (J9AVLTreeNode *) romSegment);
}
Trc_SHR_CM_addNewROMImageSegment_Exit(currentThread, romSegment);
return romSegment;
}
J9MemorySegment*
SH_CacheMap::createNewSegment(J9VMThread* currentThread, UDATA type, J9MemorySegmentList* segmentList,
U_8* baseAddress, U_8* heapBase, U_8* heapTop, U_8* heapAlloc)
{
J9MemorySegment* romSegment = NULL;
J9JavaVM* vm = currentThread->javaVM;
Trc_SHR_CM_createNewSegment_Entry(currentThread, type, segmentList, baseAddress, heapBase, heapTop, heapAlloc);
if ((romSegment = vm->internalVMFunctions->allocateMemorySegmentListEntry(segmentList)) != NULL) {
romSegment->type = type;
romSegment->size = (heapTop - baseAddress);
romSegment->baseAddress = baseAddress;
romSegment->heapBase = heapBase;
romSegment->heapTop = heapTop;
romSegment->heapAlloc = heapAlloc;
romSegment->classLoader = vm->systemClassLoader;
}
Trc_SHR_CM_createNewSegment_Exit(currentThread, romSegment);
return romSegment;
}
/**
* Updates the heapAlloc of the current ROMClass segment and creates a new segment if this is required.
* Should be called whenever a cache update has occurred or after a ROMClass has been added to the cache
* THREADING: The only time that hasClassSegmentMutex can be false is if the caller does not hold the write mutex
* storeROMClass prereq that the class segment mutex is held.
* Therefore, we can enter the write mutex if we have the class segment mutex, but NOT vice-versa.
*
* @param [in] currentThread The current thread
* @param [in] hasClassSegmentMutex Whether the currrent thread has ClassSegmentMutex
* @param [in] topLayerOnly Whether update romClass segment for top layer cache only
*/
void
SH_CacheMap::updateROMSegmentList(J9VMThread* currentThread, bool hasClassSegmentMutex, bool topLayerOnly)
{
SH_CompositeCacheImpl* cache = _ccHead;
#if defined(J9VM_THR_PREEMPTIVE)
omrthread_monitor_t classSegmentMutex = currentThread->javaVM->classMemorySegments->segmentMutex;
#endif
#if defined(J9VM_THR_PREEMPTIVE)
if (!hasClassSegmentMutex) {
Trc_SHR_Assert_ShouldNotHaveLocalMutex(classSegmentMutex);
Trc_SHR_Assert_False(_ccHead->hasWriteMutex(currentThread));
Trc_SHR_Assert_False(_ccHead->hasReadMutex(currentThread));
enterLocalMutex(currentThread, classSegmentMutex, "class segment mutex", "updateROMSegmentList");
} else {
Trc_SHR_Assert_ShouldHaveLocalMutex(classSegmentMutex);
}
#endif
while (cache) {
if (cache->isStarted()) {
updateROMSegmentListForCache(currentThread, cache);
}
if (topLayerOnly) {
break;
}
cache = cache->getNext();
}
#if defined(J9VM_THR_PREEMPTIVE)
if (!hasClassSegmentMutex) {
exitLocalMutex(currentThread, classSegmentMutex, "class segment mutex", "updateROMSegmentList");
}
#endif
}
/**
* THREADING: Should have class segment mutex
* @param[in] forCache The current supercache, if there are no cachelets. A started cachelet, otherwise.
*/
void
SH_CacheMap::updateROMSegmentListForCache(J9VMThread* currentThread, SH_CompositeCacheImpl* forCache)
{
J9JavaVM* vm = currentThread->javaVM;
U_8 *currentSegAlloc, *cacheAlloc;
J9MemorySegment* currentSegment = forCache->getCurrentROMSegment();
PORT_ACCESS_FROM_PORT(_portlib);
Trc_SHR_CM_updateROMSegmentList_Entry(currentThread, currentSegment);
/* TODO: I think we need a pass/fail return value from this */
if (currentSegment == NULL) {
if ((currentSegment = addNewROMImageSegment(currentThread, (U_8*)forCache->getBaseAddress(), (U_8*)forCache->getCacheLastEffectiveAddress())) == NULL) {
Trc_SHR_CM_updateROMSegmentList_addFirstSegmentFailed(currentThread, forCache, forCache->getBaseAddress(), forCache->getCacheLastEffectiveAddress());
return;
}
forCache->setCurrentROMSegment(currentSegment);
}
currentSegAlloc = currentSegment->heapAlloc;
cacheAlloc = (U_8*)forCache->getSegmentAllocPtr();
/* If there is a cache update which is not reflected in the current ROMClass segment... */
if (currentSegAlloc < cacheAlloc) {
U_8* currentROMClass = currentSegAlloc;
UDATA currentSegSize = currentSegment->heapAlloc - currentSegment->heapBase;
UDATA maxSegmentSize = vm->romClassAllocationIncrement;
/* Walk ROMClasses to the limit of cacheAlloc */
while (currentROMClass < cacheAlloc) {
UDATA currentROMSize = ((J9ROMClass*)currentROMClass)->romSize;
/* If the current segment is about to become too large, try to create a new one */
if ((currentSegSize + currentROMSize) > maxSegmentSize) {
J9MemorySegment* newSegment;
/* Failure to create a new segment is fairly fatal - j9mem_allocate_memory would have failed. Continue to use existing segment. */
if ((newSegment = addNewROMImageSegment(currentThread, currentROMClass, (U_8*)forCache->getCacheLastEffectiveAddress()))) {
/* Now that we know the limits of the current segment, set these fields */
currentSegment->heapTop = currentROMClass;
currentSegment->heapAlloc = currentROMClass;
currentSegment->size = (currentSegment->heapTop - currentSegment->heapBase);
forCache->setCurrentROMSegment(newSegment);
currentSegment = newSegment;
currentSegSize = 0;
} else {
Trc_SHR_CM_updateROMSegmentList_addSegmentFailed(currentThread, forCache,
currentROMClass, forCache->getCacheLastEffectiveAddress(), currentSegment);
}
} else if (currentROMSize <= 0) {
CACHEMAP_TRACE1(J9SHR_VERBOSEFLAG_ENABLE_VERBOSE_DEFAULT, J9NLS_ERROR, J9NLS_SHRC_CM_READ_CORRUPT_ROMCLASS, currentROMClass);
forCache->setCorruptCache(currentThread, ROMCLASS_CORRUPT, (UDATA)currentROMClass);
reportCorruptCache(currentThread, forCache);
break;
}
currentSegSize += currentROMSize;
currentROMClass += currentROMSize;
}
currentSegment->heapAlloc = cacheAlloc;
VM_AtomicSupport::writeBarrier();
Trc_SHR_CM_updateROMSegmentList_NewHeapAlloc(currentThread, currentSegment, cacheAlloc);
}
Trc_SHR_CM_updateROMSegmentList_Exit(currentThread, currentSegment);
}
/**
* Assume cc is initialized OK
* @retval 1 success
* @retval 0 failure
*/
UDATA
SH_CacheMap::initializeROMSegmentList(J9VMThread* currentThread)
{
J9JavaVM* vm = currentThread->javaVM;
UDATA result = 1;
J9SharedClassConfig* config;
U_8 *cacheBase, *cacheDebugAreaStart;
BlockPtr firstROMClassAddress;
omrthread_monitor_t classSegmentMutex = vm->classMemorySegments->segmentMutex;
omrthread_monitor_t memorySegmentMutex = vm->memorySegments->segmentMutex;
Trc_SHR_Assert_ShouldNotHaveLocalMutex(classSegmentMutex);
Trc_SHR_Assert_True(_sharedClassConfig != NULL);
Trc_SHR_CM_initializeROMSegmentList_Entry(currentThread);
cacheBase = (U_8*)_ccHead->getBaseAddress();
firstROMClassAddress = _ccHead->getFirstROMClassAddress();
/* Subtract sizeof(ShcItemHdr) from end address, because when the cache is mapped
* to the end of memory, and -Xscdmx0 is used, then cacheDebugAreaStart may equal NULL
*/
cacheDebugAreaStart = (U_8*)_ccHead->getClassDebugDataStartAddress() - sizeof(ShcItemHdr);
config = _sharedClassConfig;
if (config->configMonitor) {
enterLocalMutex(currentThread, config->configMonitor, "config monitor", "initializeROMSegmentList");
}
/* config->cacheDescriptorList always refers to the current supercache */
if (config->cacheDescriptorList->cacheStartAddress) {
Trc_SHR_Assert_True(config->cacheDescriptorList->cacheStartAddress == _ccHead->getCacheHeaderAddress());
} else {
config->cacheDescriptorList->cacheStartAddress = _ccHead->getCacheHeaderAddress();
}
Trc_SHR_Assert_True(config->cacheDescriptorList->cacheStartAddress != NULL);
config->cacheDescriptorList->romclassStartAddress = firstROMClassAddress;
config->cacheDescriptorList->metadataStartAddress = cacheDebugAreaStart;
config->cacheDescriptorList->cacheSizeBytes = _ccHead->getCacheMemorySize();
config->cacheDescriptorList->osPageSizeInHeader = _ccHead->getOSPageSizeInHeader();
#if defined(J9VM_THR_PREEMPTIVE)
if (memorySegmentMutex) {
enterLocalMutex(currentThread, memorySegmentMutex, "memory segment mutex", "initializeROMSegmentList");
}
#endif