-
Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathOMRCodeCacheManager.cpp
1269 lines (1072 loc) · 46.6 KB
/
OMRCodeCacheManager.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) 2000, 2020 IBM Corp. and others
*
* 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 http://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] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "runtime/CodeCacheManager.hpp"
#include <algorithm>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "env/FrontEnd.hpp"
#include "control/Options.hpp"
#include "control/Options_inlines.hpp"
#include "env/IO.hpp"
#include "env/defines.h"
#include "env/CompilerEnv.hpp"
#include "env/jittypes.h"
#include "env/VerboseLog.hpp"
#include "il/DataTypes.hpp"
#include "infra/Assert.hpp"
#include "infra/CriticalSection.hpp"
#include "infra/Monitor.hpp"
#include "omrformatconsts.h"
#include "runtime/CodeCache.hpp"
#include "runtime/CodeCacheManager.hpp"
#include "runtime/CodeCacheMemorySegment.hpp"
#include "runtime/CodeCacheConfig.hpp"
#include "runtime/Runtime.hpp"
#if (HOST_OS == OMR_LINUX)
#include <elf.h>
#include <unistd.h>
#include "codegen/ELFGenerator.hpp"
TR::CodeCacheSymbolContainer * OMR::CodeCacheManager::_symbolContainer = NULL;
#endif //HOST_OS == OMR_LINUX
OMR::CodeCacheManager::CodeCacheManager(TR::RawAllocator rawAllocator) :
_rawAllocator(rawAllocator),
_initialized(false),
_codeCacheFull(false),
_currTotalUsedInBytes(0),
_maxUsedInBytes(0)
{
}
TR::CodeCacheManager *
OMR::CodeCacheManager::self()
{
return static_cast<TR::CodeCacheManager *>(this);
}
OMR::CodeCacheManager::CacheListCriticalSection::CacheListCriticalSection(TR::CodeCacheManager *mgr)
: CriticalSection(mgr->cacheListMutex())
{
}
OMR::CodeCacheManager::RepositoryMonitorCriticalSection::RepositoryMonitorCriticalSection(TR::CodeCacheManager *mgr)
: CriticalSection(mgr->_codeCacheRepositoryMonitor)
{
}
OMR::CodeCacheManager::UsageMonitorCriticalSection::UsageMonitorCriticalSection(TR::CodeCacheManager *mgr)
: CriticalSection(mgr->_usageMonitor)
{
}
TR::CodeCache *
OMR::CodeCacheManager::initialize(
bool allocateMonolithicCodeCache,
uint32_t numberOfCodeCachesToCreateAtStartup)
{
TR_ASSERT(!self()->initialized(), "cannot initialize code cache manager more than once");
#if (HOST_OS == OMR_LINUX)
_elfRelocatableGenerator = NULL;
_elfExecutableGenerator = NULL;
if (_symbolContainer == NULL){
TR::CodeCacheSymbolContainer * symbolContainer = static_cast<TR::CodeCacheSymbolContainer *>(self()->getMemory(sizeof(TR::CodeCacheSymbolContainer)));
symbolContainer->_head = NULL;
symbolContainer->_tail = NULL;
symbolContainer->_numSymbols = 0; //does not include UNDEF symbol
symbolContainer->_totalSymbolNameLength = 1; //precount for the UNDEF symbol
_symbolContainer = symbolContainer;
}
#endif // HOST_OS == OMR_LINUX
TR::CodeCacheConfig &config = self()->codeCacheConfig();
if (allocateMonolithicCodeCache)
{
size_t size = config.codeCacheTotalKB() * 1024;
if (self()->allocateCodeCacheRepository(size))
{
// Success. In this case we want to pre-allocate just one code cache
if (config.canChangeNumCodeCaches())
{
// code caches will all be nearby each other in the repository so no need to allocate more than 1 up front
numberOfCodeCachesToCreateAtStartup = 1;
}
}
else
{
// when we use 1GB pages we can fail allocation for many reasons
// the next attempt should be done using 4K pages
if (config.largeCodePageSize() >= 0x40000000)
config._largeCodePageSize = 0x1000;
// print a messages and fall back on the old mechanism
if (config.verboseCodeCache())
{
TR_VerboseLog::writeLineLocked(TR_Vlog_FAILURE, "failed to allocate codeCacheRepository of size %u KB", (uint32_t)(config.codeCacheTotalKB()));
}
}
}
mcc_printf("mcc_initialize: initializing %d code cache(s)\n", numberOfCodeCachesToCreateAtStartup);
mcc_printf("mcc_initialize: code cache size = %" OMR_PRIuSIZE " kb\n", config.codeCacheKB());
mcc_printf("mcc_initialize: code cache pad size = %" OMR_PRIuSIZE " kb\n", config.codeCachePadKB());
mcc_printf("mcc_initialize: code cache total size = %" OMR_PRIuSIZE " kb\n", config.codeCacheTotalKB());
// Initialize the list of code caches
//
_codeCacheList._head = NULL;
_codeCacheList._mutex = TR::Monitor::create("JIT-CodeCacheListMutex");
if (_codeCacheList._mutex == NULL)
return NULL;
if (!(_usageMonitor = TR::Monitor::create("CodeCacheUsageMonitor")))
return NULL;
#if defined(TR_HOST_POWER)
#define REACHEABLE_RANGE_KB (32*1024)
#elif defined(TR_HOST_ARM64)
#define REACHEABLE_RANGE_KB (128*1024)
#else
#define REACHEABLE_RANGE_KB (2048*1024)
#endif
config._needsMethodTrampolines =
!(config.trampolineCodeSize() == 0
|| config.maxNumberOfCodeCaches() == 1
#if !defined(TR_HOST_POWER)
|| (!TR::Options::getCmdLineOptions()->getOption(TR_StressTrampolines) &&
self()->usingRepository() &&
config.codeCacheTotalKB() <= REACHEABLE_RANGE_KB)
#endif
);
_lowCodeCacheSpaceThresholdReached = false;
_initialized = true;
int32_t cachesCreatedOnInit = std::min<int32_t>(config.maxNumberOfCodeCaches(), numberOfCodeCachesToCreateAtStartup);
TR::CodeCache *codeCache = NULL;
for (int32_t i = 0; i < cachesCreatedOnInit; i++)
{
// These code caches must not be reserved. A value of -2 for reservingCompThreadID
// instructs allocate() routine to not reserve the code caches
//
codeCache = self()->allocateCodeCacheFromNewSegment(config.codeCacheKB() << 10, -2);
}
_curNumberOfCodeCaches = cachesCreatedOnInit;
return codeCache;
}
void
OMR::CodeCacheManager::destroy()
{
#if (HOST_OS == OMR_LINUX)
// if code cache should be written out as shared object, do that now before destroying anything
if (_elfRelocatableGenerator)
{
TR_ASSERT(_elfRelocatableGenerator->emitELF((const char*)_objectFileName,
_relocatableSymbolContainer->_head,
_relocatableSymbolContainer->_numSymbols,
_relocatableSymbolContainer->_totalSymbolNameLength,
_relocations->_head,
_relocations->_numRelocations),
"Failed to write code cache symbols to executable ELF file.");
}
if (_elfExecutableGenerator)
{
pid_t jvmPid = getpid();
static const int maxElfFilenameSize = 15 + sizeof(jvmPid)* 3; // "written to file: /tmp/perf-%d.jit, where d is the pid"
char elfFilename[maxElfFilenameSize] = { 0 };
int numCharsWritten = snprintf(elfFilename, maxElfFilenameSize, "/tmp/perf-%d.jit", jvmPid);
if (numCharsWritten > 0 && numCharsWritten < maxElfFilenameSize)
{
TR_ASSERT(_elfExecutableGenerator->emitELF((const char*) &elfFilename,
_symbolContainer->_head,
_symbolContainer->_numSymbols,
_symbolContainer->_totalSymbolNameLength),
"Failed to write code cache symbols to relocatable ELF file.");
}
}
#endif // HOST_OS == OMR_LINUX
TR::CodeCache *codeCache = self()->getFirstCodeCache();
while (codeCache != NULL)
{
TR::CodeCache *nextCache = codeCache->next();
codeCache->destroy(self());
self()->freeMemory(codeCache);
codeCache = nextCache;
}
if (self()->usingRepository())
{
self()->freeCodeCacheSegment(_codeCacheRepositorySegment);
}
_initialized = false;
}
void *
OMR::CodeCacheManager::getMemory(size_t sizeInBytes)
{
return _rawAllocator.allocate(sizeInBytes, std::nothrow);
}
void
OMR::CodeCacheManager::freeMemory(void *memoryToFree)
{
_rawAllocator.deallocate(memoryToFree);
}
TR::CodeCache *
OMR::CodeCacheManager::allocateCodeCacheObject(TR::CodeCacheMemorySegment *codeCacheSegment,
size_t codeCacheSize)
{
TR::CodeCache *codeCache = static_cast<TR::CodeCache *>(self()->getMemory(sizeof(TR::CodeCache)));
if (codeCache)
{
new (codeCache) TR::CodeCache();
if (!codeCache->initialize(self(), codeCacheSegment, codeCacheSize))
{
self()->freeMemory(codeCache);
codeCache = NULL;
}
}
return codeCache;
}
// Start managing the given code cache
//
void
OMR::CodeCacheManager::addCodeCache(TR::CodeCache *codeCache)
{
CacheListCriticalSection updateCacheList(self());
/* add it to the linked list */
codeCache->linkTo(_codeCacheList._head);
FLUSH_MEMORY(true); // Insure codeCache contents are globally visible before adding it to the list!
_codeCacheList._head = codeCache;
_curNumberOfCodeCaches++;
}
void
OMR::CodeCacheManager::unreserveCodeCache(TR::CodeCache *codeCache)
{
if (!codeCache)
return;
CacheListCriticalSection scanCacheList(self());
codeCache->unreserve();
}
// The size estimate is just that a guess. We should reserve a code cache that has at least
// that much space available. If sizeEstimate is 0, then there is no estimate.
// compThreadID is the ID of the compilation thread requesting the reservation
// A compThreadID of -1 means unknown. This ID will be written into the code cache
// The ID of the thread that last reserved the cache will remain written after the
// reservation is over. This will allow us to implement some affinity.
TR::CodeCache *
OMR::CodeCacheManager::reserveCodeCache(bool compilationCodeAllocationsMustBeContiguous,
size_t sizeEstimate,
int32_t compThreadID,
int32_t *numReserved)
{
int32_t numCachesAlreadyReserved = 0;
TR::CodeCache *codeCache = NULL;
// Scan the list of code caches; must acquire a mutex
//
{
CacheListCriticalSection scanCacheList(self());
for (codeCache = self()->getFirstCodeCache(); codeCache; codeCache = codeCache->next())
{
if (!codeCache->isReserved()) // we cannot touch the reserved ones
{
TR_YesNoMaybe almostFull = codeCache->almostFull();
if (almostFull == TR_no || (almostFull == TR_maybe && !compilationCodeAllocationsMustBeContiguous))
{
// Is the free space big enough?
if (sizeEstimate == 0 || // If size estimate is not given we'll blindly pick anything
codeCache->getFreeContiguousSpace() >= sizeEstimate ||
codeCache->getSizeOfLargestFreeWarmBlock() >= sizeEstimate // we don't know yet the warm/cold requirements
) // so check only for warm part
{
codeCache->reserve(compThreadID);
break;
}
}
}
else // code cache is reserved
{
numCachesAlreadyReserved++;
}
} // end for
}
*numReserved = numCachesAlreadyReserved;
if (codeCache)
{
TR_ASSERT(codeCache->isReserved(), "cache must be reserved\n");
return codeCache;
}
// No existing code cache is available; try to allocate a new one
if (self()->canAddNewCodeCache())
{
TR::CodeCacheConfig &config = self()->codeCacheConfig();
codeCache = self()->allocateCodeCacheFromNewSegment(config.codeCacheKB() << 10, compThreadID);
}
else
{
if (numCachesAlreadyReserved > 0)
self()->setHasFailedCodeCacheAllocation();
}
if (codeCache)
{
TR_ASSERT(codeCache->isReserved(), "cache must be reserved\n");
}
else
{
if (numCachesAlreadyReserved == 0)
{
// Cannot reserve a cache and there are no other reserved caches, so
// no chance to find space for this request. Must declare code cache full.
self()->setCodeCacheFull();
}
}
return codeCache;
}
//------------------------------ getNewCodeCache -----------------------------
// Searches for a code cache that hasn't been used before. If not found it
// tries to allocate a new code cache. If that fails too, it returns NULL
// Parameter: the ID of the compilation thread that executes the routine
// Side effects: the new cache is reserved
//----------------------------------------------------------------------------
TR::CodeCache *
OMR::CodeCacheManager::getNewCodeCache(int32_t reservingCompThreadID)
{
TR::CodeCache *codeCache = NULL;
if (self()->canAddNewCodeCache())
{
TR::CodeCacheConfig &config = self()->codeCacheConfig();
codeCache = self()->allocateCodeCacheFromNewSegment(config.codeCacheKB() << 10, reservingCompThreadID);
}
return codeCache;
}
// Allocate code memory with separate warm and cold sections.
// Returns the address of the warm section; the "coldCode" slot is filled in with
// the address of the cold section. The sizes may increase due to alignment and headers
//
uint8_t *
OMR::CodeCacheManager::allocateCodeMemory(size_t warmCodeSize,
size_t coldCodeSize,
TR::CodeCache **codeCache_pp,
uint8_t ** coldCode,
bool needsToBeContiguous,
bool isMethodHeaderNeeded)
{
uint8_t *methodBlockAddress;
TR_ASSERT((*codeCache_pp)->isReserved(), "Code cache must be reserved"); // MCT
TR::CodeCacheConfig &config = self()->codeCacheConfig();
methodBlockAddress = self()->allocateCodeMemoryWithRetries(warmCodeSize,
coldCodeSize,
codeCache_pp,
(int32_t) config.codeCacheMethodBodyAllocRetries(),
coldCode,
needsToBeContiguous,
isMethodHeaderNeeded);
_lastCache = *codeCache_pp;
if (config.doSanityChecks() && (*codeCache_pp))
(*codeCache_pp)->checkForErrors();
return methodBlockAddress;
}
void
OMR::CodeCacheManager::performSizeAdjustments(size_t &warmCodeSize,
size_t &coldCodeSize,
bool needsToBeContiguous,
bool isMethodHeaderNeeded)
{
TR::CodeCacheConfig config = self()->codeCacheConfig();
size_t round = config.codeCacheAlignment() - 1;
if (needsToBeContiguous && coldCodeSize)
{
warmCodeSize += coldCodeSize;
coldCodeSize=0;
}
// reserve space for code cache header and align to the required boundary
if (warmCodeSize)
{
if (isMethodHeaderNeeded)
warmCodeSize += sizeof(CodeCacheMethodHeader);
warmCodeSize = (warmCodeSize + round) & ~round;
}
if (coldCodeSize)
{
if (isMethodHeaderNeeded)
coldCodeSize += sizeof(CodeCacheMethodHeader);
coldCodeSize = (coldCodeSize + round) & ~round;
}
}
// Find a code cache containing the given address
//
TR::CodeCache *
OMR::CodeCacheManager::findCodeCacheFromPC(void *inCacheAddress)
{
TR::CodeCache *codeCache = self()->getFirstCodeCache();
if (!codeCache)
return NULL;
/* scan all code caches to see if they encompass inCacheAddress */
while (codeCache)
{
/* helperTop is heapTop */
if ((uint8_t *) inCacheAddress >= codeCache->getCodeBase() &&
(uint8_t *) inCacheAddress <= codeCache->getHelperTop())
return codeCache;
codeCache = codeCache->next();
}
/* nothing found */
return NULL;
}
// Trampoline Lookup
// Find the trampoline for the given method in the code cache containing the
// callingPC.
//
OMR::CodeCacheTrampolineCode *
OMR::CodeCacheManager::findMethodTrampoline(TR_OpaqueMethodBlock *method, void *callingPC)
{
TR::CodeCacheConfig &config = self()->codeCacheConfig();
if (!config.needsMethodTrampolines())
return NULL;
TR::CodeCache *codeCache = self()->findCodeCacheFromPC(callingPC);
if (!codeCache)
return NULL;
return codeCache->findTrampoline(method);
}
intptr_t
OMR::CodeCacheManager::findHelperTrampoline(int32_t helperIndex, void *callSite)
{
/* does the platform need trampolines at all? */
TR::CodeCacheConfig &config = self()->codeCacheConfig();
if (!config.trampolineCodeSize())
return 0;
TR::CodeCache *codeCache = self()->findCodeCacheFromPC(callSite);
if (!codeCache)
return 0;
return (intptr_t)codeCache->findTrampoline(helperIndex);
}
// Synchronize temporary trampolines in all code caches
//
void
OMR::CodeCacheManager::synchronizeTrampolines()
{
TR::CodeCacheConfig &config = self()->codeCacheConfig();
if (!config.needsMethodTrampolines())
return;
{
CacheListCriticalSection scanCacheList(self());
for (TR::CodeCache *codeCache = self()->getFirstCodeCache(); codeCache; codeCache = codeCache->next())
{
/* does the platform need temporary trampolines? */
if (codeCache->tempTrampolinesMax())
codeCache->syncTempTrampolines();
}
}
}
// Trampoline Replacement / Patching
// Replace permanent trampoline code with updated target address
//
OMR::CodeCacheTrampolineCode *
OMR::CodeCacheManager::replaceTrampoline(TR_OpaqueMethodBlock *method,
void *callSite,
void *oldTrampoline,
void *oldTargetPC,
void *newTargetPC,
bool needSync)
{
TR::CodeCacheConfig &config = self()->codeCacheConfig();
if (!config.needsMethodTrampolines())
return NULL;
TR::CodeCache *codeCache = self()->findCodeCacheFromPC(callSite);
return codeCache->replaceTrampoline(method, oldTrampoline, oldTargetPC, newTargetPC, needSync);
}
// Is there space and are we allowed to allocate a new code cache?
//
bool
OMR::CodeCacheManager::canAddNewCodeCache()
{
TR::CodeCacheConfig & config = self()->codeCacheConfig();
// is there space for another code cache?
if (config._allowedToGrowCache && _curNumberOfCodeCaches < config._maxNumberOfCodeCaches)
return true;
if (config.verboseCodeCache())
{
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE, "CodeCache maximum allocated");
}
return false;
}
// Allocate code memory with separate warm and cold sections.
// Returns the address of the warm section; the "coldCode" slot is filled in with
// the address of the cold section.
// This is a private method used by the above allocation methods that allows the
// allocation to be retried if the code cache becomes full.
//
uint8_t *
OMR::CodeCacheManager::allocateCodeMemoryWithRetries(size_t warmCodeSize,
size_t coldCodeSize,
TR::CodeCache **codeCache_pp,
int32_t allocationRetries,
uint8_t ** coldCode,
bool needsToBeContiguous,
bool isMethodHeaderNeeded)
{
uint8_t * warmCodeAddress = NULL;
TR::CodeCache *codeCache;
TR::CodeCache *originalCodeCache = *codeCache_pp; // diagnostic
/* prevent infinite recursion on allocation requests larger than possible code cache size etc */
if (allocationRetries-- < 0)
return NULL;
codeCache = *codeCache_pp;
TR_ASSERT(codeCache->isReserved(), "Code cache must be reserved retries=%d", allocationRetries);
int32_t compThreadID = codeCache->getReservingCompThreadID(); // read the ID of the comp thread
// Try to allocate into the suggested code cache
warmCodeAddress = codeCache->allocateCodeMemory(warmCodeSize,
coldCodeSize,
coldCode,
needsToBeContiguous,
isMethodHeaderNeeded);
if (warmCodeAddress)
return warmCodeAddress;
if (codeCache->almostFull() == TR_no)
codeCache->setAlmostFull(TR_maybe);
// Let's scan the list of caches to find one that has enough space
// However, for the last trial allocate a cache
int32_t numCachesVisited = 0;
int32_t numCachesAlreadyReserved = 0;
if (allocationRetries > 0)
{
// scope for cache list critical section
{
CacheListCriticalSection scanCacheList(self());
for (codeCache = self()->getFirstCodeCache(); codeCache; codeCache = codeCache->next())
{
numCachesVisited++;
// Our current cache is reserved, so we cannot find it again
if (!codeCache->isReserved())
{
if (codeCache->almostFull() != TR_yes)
{
// How about the size
size_t warmSize = warmCodeSize;
size_t coldSize = coldCodeSize;
// note side effect on warmSize and coldSize from performSizeAdjustments
self()->performSizeAdjustments(warmSize,
coldSize,
needsToBeContiguous,
isMethodHeaderNeeded);
// TODO: should we check the free blocks first?
if (codeCache->getFreeContiguousSpace() > warmSize + coldSize)
{
codeCache->reserve(compThreadID);
break;
}
}
}
else // This cache is reserved; would it be OK though?
{
numCachesAlreadyReserved++;
}
}
}
if (codeCache) // found an appropriate code cache
{
TR_ASSERT((*codeCache_pp)->isReserved(), "Original code cache must have been reserved"); // MCT
TR_ASSERT(codeCache->isReserved(), "Selected code cache must have been reserved"); // MCT
(*codeCache_pp)->unreserve();
#ifdef MCT_DEBUG
fprintf(stderr, "cache %p reset reservation in allocateCodeMemory after searching\n", *codeCache_pp);
#endif
*codeCache_pp = codeCache; // code cache has been switched // MCT
TR::CodeCacheConfig & config = self()->codeCacheConfig();
if (config.verboseCodeCache())
{
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE, "Switching TR::CodeCache to %p @ " POINTER_PRINTF_FORMAT "-" POINTER_PRINTF_FORMAT,
codeCache, codeCache->getCodeBase(), codeCache->getCodeTop());
}
if (needsToBeContiguous) // We should abort the compilation if we change the cache during a compilation that needs contiguous allocations
return NULL;
/* try allocating in the new cache */
warmCodeAddress = self()->allocateCodeMemoryWithRetries(warmCodeSize,
coldCodeSize,
codeCache_pp,
allocationRetries,
coldCode,
needsToBeContiguous,
isMethodHeaderNeeded);
return warmCodeAddress;
}
}
// We didn't find an appropriate code cache and must allocate a new code cache
// Let's see if there actually is a code cache that is free
//fprintf(stderr, "Will have to allocate a code cache; retries=%d cachesVisited=%d cachedRserved=%d\n",
// allocationRetries, numCachesVisited, numCachesAlreadyReserved);
#ifdef MCT_DEBUG
fprintf(stderr, "Cache %p has %u bytes free. We requested %u bytes\n",
*codeCache_pp, (*codeCache_pp)->getFreeContiguousSpace(), warmCodeSize+coldCodeSize);
#endif
codeCache = *codeCache_pp; // MCT
if (!self()->canAddNewCodeCache())
{
if (numCachesAlreadyReserved > 1) // 1 is for my own reserved cache
{
// Here we don't declare code cache full, because we may still have the chance to
// find some space in one of the other caches that are currently reserved
self()->setHasFailedCodeCacheAllocation();
}
else // If one allocation request cannot be satisfied by any code cache
{ // in the system, then we must declare that code caches have become full
self()->setCodeCacheFull();
}
return NULL;
}
/* Calculate the code cache segment size */
TR::CodeCacheConfig &config = self()->codeCacheConfig();
size_t allocateSize = 2*(warmCodeSize +
coldCodeSize +
config.numRuntimeHelpers() * config.trampolineCodeSize() +
config.ccPreLoadedCodeSize());
// TODO: move these jit config fields into private jit config?
size_t segmentSize = config.codeCacheKB() << 10;
if (segmentSize < allocateSize)
{
size_t round = config.codeCacheAlignment() - 1;
segmentSize = (allocateSize + round) & (~round);
}
/* Create a new code cache structure and initialize it */
codeCache = self()->allocateCodeCacheFromNewSegment(segmentSize, compThreadID);
if (!codeCache)
{
self()->setCodeCacheFull();
return NULL;
}
// Unreserve the original cache
TR_ASSERT((*codeCache_pp)->isReserved(), "Code cache must be reserved. Original code cache=%p pp=%p\n", originalCodeCache, *codeCache_pp); // MCT
(*codeCache_pp)->unreserve();
#ifdef MCT_DEBUG
fprintf(stderr, "cache %p reset reservation in allocateCodeMemory after new code cache allocation. New cache=%p needsToBeContiguous=%d allocationRetries=%d\n",
*codeCache_pp, codeCache, needsToBeContiguous, allocationRetries);
#endif
TR_ASSERT(codeCache->isReserved(), "Allocated code cache must have been reserved"); // MCT
*codeCache_pp = codeCache;
if (needsToBeContiguous) // We should abort the compilation if code allocations needs to be contiguous
return NULL;
/* try allocating in the new cache */
warmCodeAddress = self()->allocateCodeMemoryWithRetries(warmCodeSize,
coldCodeSize,
codeCache_pp,
allocationRetries,
coldCode,
needsToBeContiguous,
isMethodHeaderNeeded);
return warmCodeAddress;
}
TR::CodeCacheMemorySegment *
OMR::CodeCacheManager::getNewCodeCacheMemorySegment(
size_t segmentSize,
size_t & codeCacheSizeAllocated)
{
TR::CodeCacheMemorySegment *codeCacheSegment;
if (self()->usingRepository())
{
codeCacheSegment = self()->carveCodeCacheSpaceFromRepository(segmentSize, codeCacheSizeAllocated);
if (!codeCacheSegment)
{
mcc_printf("CodeCache::allocate : code cache repository exhausted\n");
TR::CodeCacheConfig & config = self()->codeCacheConfig();
if (config.verboseCodeCache())
TR_VerboseLog::writeLineLocked(TR_Vlog_FAILURE, "code cache repository exhausted");
return NULL;
}
}
else // old path
{
codeCacheSegment = self()->allocateCodeCacheSegment(segmentSize, codeCacheSizeAllocated, NULL);
if (!codeCacheSegment)
{
mcc_printf("CodeCache::allocate : codeCacheSegment is NULL\n");
TR::CodeCacheConfig & config = self()->codeCacheConfig();
if (config.verboseCodeCache())
TR_VerboseLog::writeLineLocked(TR_Vlog_FAILURE, "cannot allocate code cache segment");
// TODO: add trace point
return NULL;
}
}
return codeCacheSegment;
}
TR::CodeCache *
OMR::CodeCacheManager::allocateRepositoryCodeCache()
{
TR::CodeCache *cache = static_cast<TR::CodeCache *>(self()->getMemory(sizeof(TR::CodeCache)));
return cache;
}
TR::CodeCacheMemorySegment *
OMR::CodeCacheManager::allocateCodeCacheRepository(size_t repositorySize)
{
TR::CodeCacheConfig &config = self()->codeCacheConfig();
size_t codeCacheSizeAllocated;
if (!(_codeCacheRepositoryMonitor = TR::Monitor::create("CodeCacheRepositoryMonitor")))
return NULL;
void *startAddress = self()->chooseCacheStartAddress(repositorySize);
_codeCacheRepositorySegment = self()->allocateCodeCacheSegment(repositorySize,
codeCacheSizeAllocated,
startAddress);
if (_codeCacheRepositorySegment)
{
_repositoryCodeCache = self()->allocateRepositoryCodeCache();
new (_repositoryCodeCache) CodeCache();
// The VM expects the first entry in the segment to be a pointer to
// a TR::CodeCache structure and the first two entries in the cache
// to be warmCodeAlloc and coldCodeAlloc.
uint8_t * start = _codeCacheRepositorySegment->segmentAlloc();
*((TR::CodeCache**)start) = self()->getRepositoryCodeCacheAddress();
_codeCacheRepositorySegment->adjustAlloc(sizeof(TR::CodeCache*)); // jump over the pointer we setup
self()->repositoryCodeCacheCreated();
// The difference between coldCodeAlloc and warmCodeAlloc must give us the free space
_repositoryCodeCache->setWarmCodeAlloc(0);
_repositoryCodeCache->setColdCodeAlloc((uint8_t*)(_codeCacheRepositorySegment->segmentTop() - _codeCacheRepositorySegment->segmentAlloc()));
if (config.verboseCodeCache())
{
TR_VerboseLog::writeLineLocked(TR_Vlog_CODECACHE, "allocateCodeCacheRepository: size=%u heapBase=%p heapAlloc=%p heapTop=%p",
codeCacheSizeAllocated,
_codeCacheRepositorySegment->segmentBase(),
_codeCacheRepositorySegment->segmentAlloc(),
_codeCacheRepositorySegment->segmentTop());
}
}
return _codeCacheRepositorySegment;
}
void
OMR::CodeCacheManager::repositoryCodeCacheCreated()
{
#if (HOST_OS == OMR_LINUX)
TR::CodeCacheConfig &config = self()->codeCacheConfig();
if (config.emitExecutableELF())
self()->initializeExecutableELFGenerator();
if (config.emitRelocatableELF())
{
self()->initializeRelocatableELFGenerator();
}
#endif // HOST_OS == OMR_LINUX
}
void
OMR::CodeCacheManager::registerCompiledMethod(const char *sig, uint8_t *startPC, uint32_t codeSize)
{
#if (HOST_OS == OMR_LINUX)
TR::CodeCacheSymbol *newSymbol = static_cast<TR::CodeCacheSymbol *> (self()->getMemory(sizeof(TR::CodeCacheSymbol)));
uint32_t nameLength = strlen(sig) + 1;
char *name = static_cast<char *> (self()->getMemory(nameLength * sizeof(char)));
memcpy(name, sig, nameLength);
newSymbol->_name = name;
newSymbol->_nameLength = nameLength;
newSymbol->_start = startPC;
newSymbol->_size = codeSize;
newSymbol->_next = NULL;
if(_symbolContainer->_head){
_symbolContainer->_tail->_next = newSymbol;
_symbolContainer->_tail = newSymbol;
} else {
_symbolContainer->_head = newSymbol;
_symbolContainer->_tail = newSymbol;
}
_symbolContainer->_numSymbols++;
_symbolContainer->_totalSymbolNameLength += nameLength;
if (_elfRelocatableGenerator){
TR::CodeCacheSymbol *newRelocSymbol = static_cast<TR::CodeCacheSymbol *> (self()->getMemory(sizeof(TR::CodeCacheSymbol)));
memcpy(newRelocSymbol, newSymbol, sizeof(TR::CodeCacheSymbol));
newRelocSymbol->_next = NULL;
if(_relocatableSymbolContainer->_head){
_relocatableSymbolContainer->_tail->_next = newRelocSymbol;
_relocatableSymbolContainer->_tail = newRelocSymbol;
} else {
_relocatableSymbolContainer->_head = newRelocSymbol;
_relocatableSymbolContainer->_tail = newRelocSymbol;
}
_relocatableSymbolContainer->_numSymbols++;
_relocatableSymbolContainer->_totalSymbolNameLength += nameLength;
}
#endif // HOST_OS == OMR_LINUX
}
void
OMR::CodeCacheManager::registerStaticRelocation(const TR::StaticRelocation &relocation)
{
#if (HOST_OS == OMR_LINUX)
if (_elfRelocatableGenerator)
{
const char * const symbolName(relocation.symbol());
uint32_t nameLength = strlen(symbolName) + 1;
char *name = static_cast<char *>(self()->getMemory(nameLength * sizeof(char)));
memcpy(name, symbolName, nameLength);
TR::CodeCacheSymbol *newRelocSymbol = static_cast<TR::CodeCacheSymbol *> (self()->getMemory(sizeof(TR::CodeCacheSymbol)));
newRelocSymbol->_name = name;
newRelocSymbol->_nameLength = nameLength;
newRelocSymbol->_start = 0;
newRelocSymbol->_size = 0;
newRelocSymbol->_next = NULL;
if(_relocatableSymbolContainer->_head){
_relocatableSymbolContainer->_tail->_next = newRelocSymbol;
_relocatableSymbolContainer->_tail = newRelocSymbol;
} else {
_relocatableSymbolContainer->_head = newRelocSymbol;
_relocatableSymbolContainer->_tail = newRelocSymbol;
}
_relocatableSymbolContainer->_numSymbols++;
_relocatableSymbolContainer->_totalSymbolNameLength += nameLength;
uint32_t symbolNumber = static_cast<uint32_t>(_relocatableSymbolContainer->_numSymbols - 1); //symbol index in the linked list
uint32_t relocationType = _resolver.resolveRelocationType(relocation);
TR::CodeCacheRelocationInfo *newRelocation = static_cast<TR::CodeCacheRelocationInfo *> (self()->getMemory(sizeof(TR::CodeCacheRelocationInfo)));
newRelocation->_location = relocation.location();
newRelocation->_type = relocationType;
newRelocation->_symbol = symbolNumber; //symbol index along the linked list
if(_relocations->_head){
_relocations->_tail->_next = newRelocation;
_relocations->_tail = newRelocation;
} else{
_relocations->_head = newRelocation;
_relocations->_tail = newRelocation;
}
_relocations->_numRelocations++;
}
#endif
}
void *
OMR::CodeCacheManager::chooseCacheStartAddress(size_t repositorySize)
{
// default: NULL means don't try to pick a starting address
return NULL;
}
void
OMR::CodeCacheManager::increaseCurrTotalUsedInBytes(size_t size)
{
self()->decreaseFreeSpaceInCodeCacheRepository(size);
{
UsageMonitorCriticalSection updateCodeCacheUsage(self());
_currTotalUsedInBytes += size;
_maxUsedInBytes = std::max(_maxUsedInBytes, _currTotalUsedInBytes);
}
}
void
OMR::CodeCacheManager::decreaseCurrTotalUsedInBytes(size_t size)
{
self()->increaseFreeSpaceInCodeCacheRepository(size);
{
UsageMonitorCriticalSection updateCodeCacheUsage(self());
_currTotalUsedInBytes = (size < _currTotalUsedInBytes) ? (_currTotalUsedInBytes - size) : 0;
}
}
void
OMR::CodeCacheManager::increaseFreeSpaceInCodeCacheRepository(size_t size)
{