-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathOMRCodeMetaDataManager.cpp
679 lines (553 loc) · 19.3 KB
/
OMRCodeMetaDataManager.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
/*******************************************************************************
* Copyright (c) 2000, 2019 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/CodeMetaDataManager.hpp"
#include <stdint.h>
#include <string.h>
#include "avl_api.h"
#include "env/TRMemory.hpp"
#include "infra/Assert.hpp"
#include "j9nongenerated.h"
#include "runtime/CodeCache.hpp"
#include "runtime/CodeCacheMemorySegment.hpp"
#include "runtime/CodeMetaDataManager.hpp"
#include "runtime/CodeMetaDataManager_inlines.hpp"
#include "runtime/CodeMetaDataPOD.hpp"
#if !defined(TR_TARGET_POWER) || !defined(__clang__)
#include "AtomicSupport.hpp"
#endif
namespace OMR
{
TR::CodeMetaDataManager *CodeMetaDataManager::_codeMetaDataManager = NULL;
CodeMetaDataManager::CodeMetaDataManager() :
_cachedPC(0),
_cachedHashTable(NULL),
_retrievedMetaDataCache(NULL)
{
_metaDataAVL = self()->allocateMetaDataAVL();
}
bool
CodeMetaDataManager::initializeCodeMetaDataManager()
{
bool initSuccess = false;
if (_codeMetaDataManager)
{
initSuccess = true;
}
else
{
//TR::Monitor *monitor = TR::Monitor::create("JIT-CodeMetaDataManagerMonitor");
//if (monitor)
{
_codeMetaDataManager = new (PERSISTENT_NEW) TR::CodeMetaDataManager();
if (_codeMetaDataManager)
initSuccess = true;
}
}
return initSuccess;
}
// First allocation
//
J9AVLTree *
CodeMetaDataManager::allocateMetaDataAVL()
{
J9AVLTree *metaDataAVLTree;
metaDataAVLTree = (J9AVLTree *) TR_Memory::jitPersistentAlloc(sizeof(J9AVLTree), TR_Memory::CodeMetaDataAVL);
if (!metaDataAVLTree)
return NULL;
metaDataAVLTree->insertionComparator = (intptr_t (*)(J9AVLTree *, J9AVLTreeNode *, J9AVLTreeNode *))OMR::avl_jit_metadata_insertionCompare;
metaDataAVLTree->searchComparator = (intptr_t (*)(J9AVLTree *, uintptr_t, J9AVLTreeNode *))OMR::avl_jit_metadata_searchCompare;
metaDataAVLTree->genericActionHook = NULL;
metaDataAVLTree->flags = 0;
metaDataAVLTree->rootNode = 0;
// Use the OMR AVL structure but TR will manage its own memory
//
metaDataAVLTree->portLibrary = NULL;
return metaDataAVLTree;
}
/**
* Insert metadata into the MetaDataManager.
*
* \note It is important that the range for the metadata be > 0.
* Inserting a zero width range will fail, because lookups
* will fail.
*/
bool
CodeMetaDataManager::insertMetaData(TR::MethodMetaDataPOD *metaData)
{
TR_ASSERT(metaData, "metaData must not be null");
//OMR::CriticalSection insertingMetaData(_monitor);
return self()->insertRange(metaData, metaData->startPC, metaData->endPC);
}
bool
CodeMetaDataManager::containsMetaData(const TR::MethodMetaDataPOD *metaData)
{
// OMR::CriticalSection searchingMetaData(_monitor);
return (metaData && metaData == self()->findMetaDataForPC(metaData->startPC));
}
bool
CodeMetaDataManager::removeMetaData(const TR::MethodMetaDataPOD *metaData)
{
TR_ASSERT(metaData, "metaData must not be null");
//OMR::CriticalSection removingMetaData(_monitor);
bool removeSuccess = false;
if (self()->containsMetaData(metaData))
{
removeSuccess = self()->removeRange(metaData, metaData->startPC, metaData->endPC);
}
_retrievedMetaDataCache = NULL;
return removeSuccess;
}
const TR::MethodMetaDataPOD *
CodeMetaDataManager::findMetaDataForPC(uintptr_t pc)
{
TR_ASSERT(pc != 0, "attempting to query existing MetaData for a NULL PC");
self()->updateCache(pc);
if (!_retrievedMetaDataCache)
{
if (_cachedHashTable)
{
_retrievedMetaDataCache = self()->findMetaDataInHash(_cachedHashTable, pc);
}
}
return _retrievedMetaDataCache;
}
// protected
bool
CodeMetaDataManager::insertRange(
TR::MethodMetaDataPOD *metaData,
uintptr_t startPC,
uintptr_t endPC)
{
bool insertSuccess = false;
self()->updateCache(metaData->startPC);
if (_cachedHashTable)
{
insertSuccess = (self()->insertMetaDataRangeInHash(_cachedHashTable, metaData, startPC, endPC) == 0);
}
return insertSuccess;
}
// protected
bool
CodeMetaDataManager::removeRange(
const TR::MethodMetaDataPOD *metaData,
uintptr_t startPC,
uintptr_t endPC)
{
bool removeSuccess = false;
self()->updateCache(metaData->startPC);
if (_cachedHashTable)
{
removeSuccess = (self()->removeMetaDataRangeFromHash(_cachedHashTable, metaData, startPC, endPC) == 0);
}
return removeSuccess;
}
// protected
void
CodeMetaDataManager::updateCache(uintptr_t currentPC)
{
TR_ASSERT(currentPC > 0, "Attempting to find a code cache's metaData hash table for a NULL PC.");
if (currentPC != _cachedPC)
{
_retrievedMetaDataCache = NULL;
_cachedPC = currentPC;
_cachedHashTable =
static_cast<TR::MetaDataHashTable *>(static_cast<void *>(avl_search(_metaDataAVL, currentPC) ) );
TR_ASSERT(_cachedHashTable, "Either we lost a code cache or we attempted to find a hash table for a non-code cache startPC: Searched for %p", currentPC);
}
}
#undef LOW_BIT_SET
#undef SET_LOW_BIT
#undef REMOVE_LOW_BIT
#undef DETERMINE_BUCKET
#undef BUCKET_SIZE
#undef METHOD_STORE_SIZE
#define LOW_BIT_SET(value) ((uintptr_t) (value) & (uintptr_t) 1)
#define SET_LOW_BIT(value) ((uintptr_t) (value) | (uintptr_t) 1)
#define REMOVE_LOW_BIT(value) ((uintptr_t) (value) & (uintptr_t) -2)
#define DETERMINE_BUCKET(value, start, buckets) (((((uintptr_t)(value) - (uintptr_t)(start)) >> (uintptr_t) 9) * (uintptr_t)sizeof(uintptr_t)) + (uintptr_t)(buckets))
#define BUCKET_SIZE 512
#define METHOD_STORE_SIZE 256
// protected
TR::MethodMetaDataPOD *
CodeMetaDataManager::findMetaDataInHash(
TR::MetaDataHashTable *table,
uintptr_t searchValue)
{
TR::MethodMetaDataPOD *entry, **bucket;
if (searchValue >= table->start && searchValue < table->end)
{
// The search value is in this hash table
//
bucket = (TR::MethodMetaDataPOD **)DETERMINE_BUCKET(searchValue, table->start, table->buckets);
if (*bucket)
{
// The bucket for this search value is not empty
//
if (LOW_BIT_SET(*bucket))
{
// The bucket consists of a single low-tagged TR::MethodMetaDataPOD pointer
//
entry = *bucket;
}
else
{
// The bucket consists of an array of TR::MethodMetaDataPOD pointers,
// the last of which is low-tagged.
// Search all but the last entry in the array
//
bucket = (TR::MethodMetaDataPOD **)(*bucket);
for ( ; ; bucket++)
{
entry = *bucket;
if (LOW_BIT_SET(entry))
break;
if (searchValue >= entry->startPC && searchValue < entry->endPC)
return entry;
}
}
// Search the last (or only) entry in the bucket, which is low-tagged
//
entry = (TR::MethodMetaDataPOD *)REMOVE_LOW_BIT(entry);
if (searchValue >= entry->startPC && searchValue < entry->endPC)
return entry;
}
}
return NULL;
}
// protected
uintptr_t
CodeMetaDataManager::insertMetaDataRangeInHash(
TR::MetaDataHashTable *table,
TR::MethodMetaDataPOD *dataToInsert,
uintptr_t startPC,
uintptr_t endPC)
{
TR::MethodMetaDataPOD **index;
TR::MethodMetaDataPOD **endIndex;
TR::MethodMetaDataPOD **temp;
if ((startPC < table->start) || (endPC > table->end))
{
return 1;
}
// Don't insert zero sized ranges.
if (startPC == endPC)
{
return 1;
}
index = (TR::MethodMetaDataPOD **) DETERMINE_BUCKET(startPC, table->start, table->buckets);
endIndex = (TR::MethodMetaDataPOD **) DETERMINE_BUCKET(endPC, table->start, table->buckets);
do
{
if (*index)
{
temp = self()->insertMetaDataArrayInHash(table, (TR::MethodMetaDataPOD**) *index, dataToInsert, startPC);
if (!temp)
{
return 2;
}
#if !defined(TR_TARGET_POWER) || !defined(__clang__)
VM_AtomicSupport::writeBarrier();
#endif
*index = (TR::MethodMetaDataPOD *) temp;
}
else
{
#if !defined(TR_TARGET_POWER) || !defined(__clang__)
VM_AtomicSupport::writeBarrier();
#endif
*index = (TR::MethodMetaDataPOD *) SET_LOW_BIT(dataToInsert);
}
} while (++index <= endIndex);
return 0;
}
// protected
TR::MethodMetaDataPOD **
CodeMetaDataManager::insertMetaDataArrayInHash(
TR::MetaDataHashTable *table,
TR::MethodMetaDataPOD **array,
TR::MethodMetaDataPOD *dataToInsert,
uintptr_t startPC)
{
TR::MethodMetaDataPOD **returnVal = array;
// If the array pointer is tagged, then it's not a chain, it's a single
// tagged metadata.
//
if (LOW_BIT_SET(array))
{
// There is a single tagged entry in the bucket, not a chain. In this case, we will
// always be allocating a new chain from the current allocate of the method store.
// We'll need 2 entries (one for the new entry and one for the existing tagged entry
// which will also terminate the chain.
// This comparison is safe since currentAllocate and methodStoreEnd will
// always be pointing into the same allocated block.
//
if ((table->currentAllocate + 2) > table->methodStoreEnd)
{
if (self()->allocateMethodStoreInHash(table) == NULL)
{
return NULL;
}
}
returnVal = (TR::MethodMetaDataPOD **) table->currentAllocate;
table->currentAllocate += 2;
returnVal[0] = (TR::MethodMetaDataPOD *)dataToInsert;
returnVal[1] = (TR::MethodMetaDataPOD *)array;
}
else
{
TR::MethodMetaDataPOD **newElement;
// There is already a a chain, so we need to either add to the end of it
// if there is free space, or copy the chain and add to the end of the copy.
//
newElement = array;
do
{
++newElement;
}
while (!LOW_BIT_SET(*(newElement-1)));
// If there is a NULL at the newElement position, then we can just add there.
// It is safe to check *newElement even when the method store is full because
// the method store always has an extra non-NULL entry on the end.
//
if (*newElement == NULL)
{
/** Adding to the end of an existing chain with a free slot after it. To avoid twizzling
* bits, copy the existing chain end forward one slot, and place the new entry in the
* old end slot. A write barrier must be issued before storing the new element, both
* to ensure the metadata is fully visible before it can be seen in the array, and to
* make the new chain end visible.
*/
*newElement = *(newElement - 1);
#if !defined(TR_TARGET_POWER) || !defined(__clang__)
VM_AtomicSupport::writeBarrier();
#endif
*(newElement - 1) = dataToInsert;
// Increase the next allocation point only if the new element is not
// being stored into freed space.
//
if (newElement == (TR::MethodMetaDataPOD **) table->currentAllocate)
{
table->currentAllocate += 1;
}
}
else
{
uintptr_t chainLength = newElement - array;
/** Not enough space to add to the end of the existing chain, so it must be copied
* and extended in free space. There may be enough free space in the current
* method store. Once space is found, copy the chain into it with the new entry at
* the beginning (to avoid twizzling tag bits). There's no need for a write barrier
* here since the new chain is not visible to anyone yet, and the caller of this
* function issues a write barrier before updating the bucket pointer.
*/
// This comparison is safe since currentAllocate and methodStoreEnd will
// always be pointing into the same allocated block.
//
if ((table->currentAllocate + chainLength + 1) > table->methodStoreEnd)
{
if (self()->allocateMethodStoreInHash(table) == NULL)
{
return NULL;
}
}
returnVal = (TR::MethodMetaDataPOD**) table->currentAllocate;
table->currentAllocate += (chainLength + 1);
returnVal[0] = dataToInsert;
memcpy(returnVal + 1, array, chainLength * sizeof(uintptr_t)); /* safe to memcpy since the new array is not yet visible */
}
}
return returnVal;
}
// protected
TR::MethodMetaDataPOD **
CodeMetaDataManager::allocateMethodStoreInHash(TR::MetaDataHashTable *table)
{
// Add 1 slot for link, 1 slot for terminator
//
uintptr_t size = (METHOD_STORE_SIZE + 2) * sizeof(uintptr_t);
uintptr_t *newStore;
newStore = (uintptr_t *) TR_Memory::jitPersistentAlloc(size, TR_Memory::CodeMetaDataAVL);
if (newStore != NULL)
{
memset(newStore, 0, size);
*newStore = (uintptr_t) table->methodStoreStart; // Link to the old store
table->methodStoreStart = (uintptr_t *) newStore;
table->methodStoreEnd = (uintptr_t *) (newStore + METHOD_STORE_SIZE + 1);
table->currentAllocate = (uintptr_t *) (newStore + 1);
// Ensure that the method store is terminated with a non-NULL entry
//
*(table->methodStoreEnd) = (uintptr_t) 0xBAAD076D;
}
return (TR::MethodMetaDataPOD **) newStore;
}
uintptr_t
CodeMetaDataManager::removeMetaDataRangeFromHash(
TR::MetaDataHashTable *table,
const TR::MethodMetaDataPOD *dataToRemove,
uintptr_t startPC,
uintptr_t endPC)
{
TR::MethodMetaDataPOD** index;
TR::MethodMetaDataPOD** endIndex;
TR::MethodMetaDataPOD* temp;
if ((startPC < table->start) || (endPC > table->end))
return (uintptr_t) 1;
index = (TR::MethodMetaDataPOD **) DETERMINE_BUCKET(startPC, table->start, table->buckets);
endIndex = (TR::MethodMetaDataPOD **) DETERMINE_BUCKET(endPC, table->start, table->buckets);
do
{
if (LOW_BIT_SET(*index))
{
if ((TR::MethodMetaDataPOD *)REMOVE_LOW_BIT(*index) == dataToRemove)
*index = 0;
else
return (uintptr_t) 1;
}
else if (*index)
{
temp = (TR::MethodMetaDataPOD *) (self()->removeMetaDataArrayFromHash((TR::MethodMetaDataPOD**) *index, dataToRemove));
if (!temp)
return (uintptr_t) 1;
else if (temp == (TR::MethodMetaDataPOD *) 1)
return (uintptr_t) 2;
else
*index = temp;
}
else
return (uintptr_t) 1;
} while (++index <= endIndex);
return (uintptr_t) 0;
}
TR::MethodMetaDataPOD **
CodeMetaDataManager::removeMetaDataArrayFromHash(
TR::MethodMetaDataPOD **array,
const TR::MethodMetaDataPOD *dataToRemove)
{
TR::MethodMetaDataPOD **index;
uintptr_t count= 0;
uintptr_t size;
uintptr_t removeSpot = 0;
index = array;
while (!LOW_BIT_SET(*index)) /* search for dataToRemove in the array */
{
++count;
if (*index == dataToRemove)
removeSpot = count;
++index;
}
if ((TR::MethodMetaDataPOD*) REMOVE_LOW_BIT(*index) == dataToRemove)
{
*index=0; /* dataToRemove is last pointer in the array. */
--index;
*index = (TR::MethodMetaDataPOD*)SET_LOW_BIT(*index);
}
else if(removeSpot)
{
size = (count-removeSpot+1)*sizeof(uintptr_t); /* dataToRemove is in middle (or start) of the array. */
memmove((void*)(array+removeSpot-1),(void*)(array+removeSpot),size);
*index = 0;
}
else
{
return (TR::MethodMetaDataPOD**) 1; /* We did not find dataToRemove in array */
}
/* tidy the case of the array having contracted to becoming a single element - it can be recycled. */
if (LOW_BIT_SET(*array))
{
// NULL unused slots to allow re-use, and to simplify debugging
//
TR::MethodMetaDataPOD ** rc = (TR::MethodMetaDataPOD**) *array; /* Only one pointer left. Just return the one pointer */
*array = NULL;
return rc;
}
return array;
}
// Call when creating a new code cache
TR::MetaDataHashTable *
CodeMetaDataManager::addCodeCache(
TR::CodeCache *codeCache)
{
TR_ASSERT(codeCache->segment(), "missing code cache segment");
TR::MetaDataHashTable *newTable = self()->allocateCodeMetaDataHash(
(uintptr_t) (codeCache->segment()->segmentBase()),
(uintptr_t) (codeCache->segment()->segmentTop()) );
if (newTable)
{
avl_insert(_metaDataAVL, (J9AVLTreeNode *) newTable);
}
return newTable;
}
// protected, secondary
TR::MetaDataHashTable *
CodeMetaDataManager::allocateCodeMetaDataHash(uintptr_t start, uintptr_t end)
{
TR::MetaDataHashTable *table;
uintptr_t size;
table = (TR::MetaDataHashTable *) TR_Memory::jitPersistentAlloc(sizeof(TR::MetaDataHashTable), TR_Memory::CodeMetaDataAVL);
if (table == NULL)
{
return NULL;
}
memset(table, 0, sizeof(*table));
table->start = start;
table->end = end;
size = DETERMINE_BUCKET(end, start, 0) + sizeof(uintptr_t);
table->buckets = (uintptr_t *) TR_Memory::jitPersistentAlloc(size, TR_Memory::CodeMetaDataAVL);
if (table->buckets == NULL)
{
TR_Memory::jitPersistentFree(table);
return NULL;
}
memset(table->buckets, 0, size);
if (self()->allocateMethodStoreInHash(table) == NULL)
{
TR_Memory::jitPersistentFree(table->buckets);
TR_Memory::jitPersistentFree(table);
return NULL;
}
return table;
}
extern "C"
{
intptr_t
avl_jit_metadata_insertionCompare(J9AVLTree *tree, TR::MetaDataHashTable *insertNode, TR::MetaDataHashTable *walkNode)
{
if (walkNode->start > insertNode->start)
{
return 1;
}
else if (walkNode->start < insertNode->start)
{
return -1;
}
return 0;
}
intptr_t
avl_jit_metadata_searchCompare(J9AVLTree *tree, uintptr_t searchValue, TR::MetaDataHashTable *walkNode)
{
if (searchValue >= walkNode->end)
return -1;
if (searchValue < walkNode->start)
return 1;
return 0;
}
} // extern "C"
}