/
JITClientSession.cpp
708 lines (647 loc) · 27 KB
/
JITClientSession.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
/*******************************************************************************
* Copyright (c) 2019, 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 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] 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/JITClientSession.hpp"
#include "control/CompilationRuntime.hpp" // for CompilationInfo
#include "control/MethodToBeCompiled.hpp" // for TR_MethodToBeCompiled
#include "control/JITServerHelpers.hpp"
#include "net/ServerStream.hpp" // for JITServer::ServerStream
#include "runtime/RuntimeAssumptions.hpp" // for TR_AddressSet
ClientSessionData::ClientSessionData(uint64_t clientUID, uint32_t seqNo) :
_clientUID(clientUID), _expectedSeqNo(seqNo), _maxReceivedSeqNo(seqNo), _OOSequenceEntryList(NULL),
_chTableClassMap(decltype(_chTableClassMap)::allocator_type(TR::Compiler->persistentAllocator())),
_romClassMap(decltype(_romClassMap)::allocator_type(TR::Compiler->persistentAllocator())),
_J9MethodMap(decltype(_J9MethodMap)::allocator_type(TR::Compiler->persistentAllocator())),
_classByNameMap(decltype(_classByNameMap)::allocator_type(TR::Compiler->persistentAllocator())),
_classChainDataMap(decltype(_classChainDataMap)::allocator_type(TR::Compiler->persistentAllocator())),
_constantPoolToClassMap(decltype(_constantPoolToClassMap)::allocator_type(TR::Compiler->persistentAllocator())),
_unloadedClassAddresses(NULL),
_requestUnloadedClasses(true),
_staticFinalDataMap(decltype(_staticFinalDataMap)::allocator_type(TR::Compiler->persistentAllocator())),
_rtResolve(false),
_registeredJ2IThunksMap(decltype(_registeredJ2IThunksMap)::allocator_type(TR::Compiler->persistentAllocator())),
_registeredInvokeExactJ2IThunksSet(decltype(_registeredInvokeExactJ2IThunksSet)::allocator_type(TR::Compiler->persistentAllocator()))
{
updateTimeOfLastAccess();
_javaLangClassPtr = NULL;
_inUse = 1;
_numActiveThreads = 0;
_romMapMonitor = TR::Monitor::create("JIT-JITServerROMMapMonitor");
_classMapMonitor = TR::Monitor::create("JIT-JITServerClassMapMonitor");
_classChainDataMapMonitor = TR::Monitor::create("JIT-JITServerClassChainDataMapMonitor");
_sequencingMonitor = TR::Monitor::create("JIT-JITServerSequencingMonitor");
_constantPoolMapMonitor = TR::Monitor::create("JIT-JITServerConstantPoolMonitor");
_vmInfo = NULL;
_staticMapMonitor = TR::Monitor::create("JIT-JITServerStaticMapMonitor");
_markedForDeletion = false;
_thunkSetMonitor = TR::Monitor::create("JIT-JITServerThunkSetMonitor");
}
ClientSessionData::~ClientSessionData()
{
clearCaches();
_romMapMonitor->destroy();
_classMapMonitor->destroy();
_classChainDataMapMonitor->destroy();
_sequencingMonitor->destroy();
_constantPoolMapMonitor->destroy();
if (_unloadedClassAddresses)
{
_unloadedClassAddresses->destroy();
jitPersistentFree(_unloadedClassAddresses);
}
_staticMapMonitor->destroy();
if (_vmInfo)
{
destroyJ9SharedClassCacheDescriptorList();
jitPersistentFree(_vmInfo);
}
_thunkSetMonitor->destroy();
}
void
ClientSessionData::updateTimeOfLastAccess()
{
PORT_ACCESS_FROM_PORT(TR::Compiler->portLib);
_timeOfLastAccess = j9time_current_time_millis();
}
void
ClientSessionData::processUnloadedClasses(JITServer::ServerStream *stream, const std::vector<TR_OpaqueClassBlock*> &classes)
{
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "Server will process a list of %u unloaded classes for clientUID %llu", (unsigned)classes.size(), (unsigned long long)_clientUID);
//Vector to hold the list of the unloaded classes and the corresponding data needed for purging the Caches
std::vector<ClassUnloadedData> unloadedClasses;
unloadedClasses.reserve(classes.size());
bool updateUnloadedClasses = true;
if (_requestUnloadedClasses)
{
stream->write(JITServer::MessageType::getUnloadedClassRanges, JITServer::Void());
auto response = stream->read<std::vector<TR_AddressRange>, int32_t>();
auto unloadedClassRanges = std::get<0>(response);
auto maxRanges = std::get<1>(response);
{
OMR::CriticalSection getUnloadedClasses(getROMMapMonitor());
if (!_unloadedClassAddresses)
_unloadedClassAddresses = new (PERSISTENT_NEW) TR_AddressSet(trPersistentMemory, maxRanges);
_unloadedClassAddresses->setRanges(unloadedClassRanges);
_requestUnloadedClasses = false;
}
updateUnloadedClasses = false;
}
{
OMR::CriticalSection processUnloadedClasses(getROMMapMonitor());
for (auto clazz : classes)
{
if (updateUnloadedClasses)
_unloadedClassAddresses->add((uintptr_t)clazz);
auto it = _romClassMap.find((J9Class*)clazz);
if (it == _romClassMap.end())
{
//Class is not cached so this entry will be used to delete the entry from caches by value.
ClassLoaderStringPair key;
unloadedClasses.push_back({ clazz, key, NULL, false });
continue; // unloaded class was never cached
}
J9ConstantPool *cp = it->second._constantPool;
J9ROMClass *romClass = it->second._romClass;
J9UTF8 *clazzName = NNSRP_GET(romClass->className, J9UTF8 *);
std::string className((const char *)clazzName->data, (int32_t)clazzName->length);
J9ClassLoader * cl = (J9ClassLoader *)(it->second._classLoader);
ClassLoaderStringPair key = { cl, className };
//Class is cached, so retain the data to be used for purging the caches.
unloadedClasses.push_back({ clazz, key, cp, true });
J9Method *methods = it->second._methodsOfClass;
// delete all the cached J9Methods belonging to this unloaded class
for (size_t i = 0; i < romClass->romMethodCount; i++)
{
J9Method *j9method = methods + i;
auto iter = _J9MethodMap.find(j9method);
if (iter != _J9MethodMap.end())
{
IPTable_t *ipDataHT = iter->second._IPData;
if (ipDataHT)
{
for (auto& entryIt : *ipDataHT)
{
auto entryPtr = entryIt.second;
if (entryPtr)
jitPersistentFree(entryPtr);
}
ipDataHT->~IPTable_t();
jitPersistentFree(ipDataHT);
iter->second._IPData = NULL;
}
_J9MethodMap.erase(j9method);
}
}
it->second.freeClassInfo();
_romClassMap.erase(it);
}
}
// remove the class chain data from the cache for the unloaded class.
{
OMR::CriticalSection processUnloadedClasses(getClassChainDataMapMonitor());
for (auto clazz : classes)
_classChainDataMap.erase((J9Class*)clazz);
}
// purge Class by name cache
{
OMR::CriticalSection classMapCS(getClassMapMonitor());
purgeCache(&unloadedClasses, getClassByNameMap(), &ClassUnloadedData::_pair);
}
// purge Constant pool to class cache
{
OMR::CriticalSection constantPoolToClassMap(getConstantPoolMonitor());
purgeCache(&unloadedClasses, getConstantPoolToClassMap(), &ClassUnloadedData::_cp);
}
}
TR_IPBytecodeHashTableEntry*
ClientSessionData::getCachedIProfilerInfo(TR_OpaqueMethodBlock *method, uint32_t byteCodeIndex, bool *methodInfoPresent)
{
*methodInfoPresent = false;
TR_IPBytecodeHashTableEntry *ipEntry = NULL;
OMR::CriticalSection getRemoteROMClass(getROMMapMonitor());
// check whether info about j9method is cached
auto & j9methodMap = getJ9MethodMap();
auto it = j9methodMap.find((J9Method*)method);
if (it != j9methodMap.end())
{
// check whether j9method data has any IP information
auto iProfilerMap = it->second._IPData;
if (iProfilerMap)
{
*methodInfoPresent = true;
// check whether desired bcindex is cached
auto ipData = iProfilerMap->find(byteCodeIndex);
if (ipData != iProfilerMap->end())
{
ipEntry = ipData->second;
}
}
}
else
{
// Very unlikely scenario because the optimizer will have created ResolvedJ9Method
// whose constructor would have fetched and cached the j9method info
TR_ASSERT(false, "profilingSample: asking about j9method=%p but this is not present in the J9MethodMap", method);
}
return ipEntry;
}
bool
ClientSessionData::cacheIProfilerInfo(TR_OpaqueMethodBlock *method, uint32_t byteCodeIndex, TR_IPBytecodeHashTableEntry *entry, bool isCompiled)
{
OMR::CriticalSection getRemoteROMClass(getROMMapMonitor());
// check whether info about j9method exists
auto & j9methodMap = getJ9MethodMap();
auto it = j9methodMap.find((J9Method*)method);
if (it != j9methodMap.end())
{
IPTable_t *iProfilerMap = it->second._IPData;
if (!iProfilerMap)
{
// Check and update if method is compiled when collecting profiling data
if (isCompiled)
it->second._isCompiledWhenProfiling = true;
// allocate a new iProfiler map
iProfilerMap = new (PERSISTENT_NEW) IPTable_t(IPTable_t::allocator_type(TR::Compiler->persistentAllocator()));
if (iProfilerMap)
{
it->second._IPData = iProfilerMap;
// entry could be null; this means that the method has no IProfiler info
if (entry)
iProfilerMap->insert({ byteCodeIndex, entry });
return true;
}
}
else
{
if (entry)
iProfilerMap->insert({ byteCodeIndex, entry });
return true;
}
}
else
{
// JITServer TODO: count how many times we cannot cache. There should be very few instances if at all.
}
return false; // false means that caching attempt failed
}
void
ClientSessionData::printStats()
{
PORT_ACCESS_FROM_PORT(TR::Compiler->portLib);
j9tty_printf(PORTLIB, "\tNum cached ROM classes: %d\n", _romClassMap.size());
j9tty_printf(PORTLIB, "\tNum cached ROM methods: %d\n", _J9MethodMap.size());
size_t total = 0;
for (auto& it : _romClassMap)
total += it.second._romClass->romSize;
j9tty_printf(PORTLIB, "\tTotal size of cached ROM classes + methods: %d bytes\n", total);
}
ClientSessionData::ClassInfo::ClassInfo() :
_romClass(NULL),
_remoteRomClass(NULL),
_methodsOfClass(NULL),
_baseComponentClass(NULL),
_numDimensions(0),
_parentClass(NULL),
_interfaces(NULL),
_classHasFinalFields(false),
_classDepthAndFlags(0),
_classInitialized(false),
_byteOffsetToLockword(0),
_leafComponentClass(NULL),
_classLoader(NULL),
_hostClass(NULL),
_componentClass(NULL),
_arrayClass(NULL),
_totalInstanceSize(0),
_constantPool(NULL),
_classFlags(0),
_classChainOffsetOfIdentifyingLoaderForClazz(0),
_remoteROMStringsCache(decltype(_remoteROMStringsCache)::allocator_type(TR::Compiler->persistentAllocator())),
_fieldOrStaticNameCache(decltype(_fieldOrStaticNameCache)::allocator_type(TR::Compiler->persistentAllocator())),
_classOfStaticCache(decltype(_classOfStaticCache)::allocator_type(TR::Compiler->persistentAllocator())),
_constantClassPoolCache(decltype(_constantClassPoolCache)::allocator_type(TR::Compiler->persistentAllocator())),
_fieldAttributesCache(decltype(_fieldAttributesCache)::allocator_type(TR::Compiler->persistentAllocator())),
_staticAttributesCache(decltype(_staticAttributesCache)::allocator_type(TR::Compiler->persistentAllocator())),
_fieldAttributesCacheAOT(decltype(_fieldAttributesCacheAOT)::allocator_type(TR::Compiler->persistentAllocator())),
_staticAttributesCacheAOT(decltype(_fieldAttributesCacheAOT)::allocator_type(TR::Compiler->persistentAllocator())),
_jitFieldsCache(decltype(_jitFieldsCache)::allocator_type(TR::Compiler->persistentAllocator())),
_fieldOrStaticDeclaringClassCache(decltype(_fieldOrStaticDeclaringClassCache)::allocator_type(TR::Compiler->persistentAllocator())),
_fieldOrStaticDefiningClassCache(decltype(_fieldOrStaticDefiningClassCache)::allocator_type(TR::Compiler->persistentAllocator())),
_J9MethodNameCache(decltype(_J9MethodNameCache)::allocator_type(TR::Compiler->persistentAllocator()))
{
}
void
ClientSessionData::ClassInfo::freeClassInfo()
{
TR_Memory::jitPersistentFree(_romClass);
// free cached _interfaces
_interfaces->~PersistentVector<TR_OpaqueClassBlock *>();
jitPersistentFree(_interfaces);
}
ClientSessionData::VMInfo *
ClientSessionData::getOrCacheVMInfo(JITServer::ServerStream *stream)
{
if (!_vmInfo)
{
stream->write(JITServer::MessageType::VM_getVMInfo, JITServer::Void());
auto recv = stream->read<VMInfo, std::vector<uintptr_t>, std::vector<uintptr_t> >();
_vmInfo = new (PERSISTENT_NEW) VMInfo(std::get<0>(recv));
_vmInfo->_j9SharedClassCacheDescriptorList = reconstructJ9SharedClassCacheDescriptorList(std::get<1>(recv), std::get<2>(recv));
}
return _vmInfo;
}
J9SharedClassCacheDescriptor *
ClientSessionData::reconstructJ9SharedClassCacheDescriptorList(const std::vector<uintptr_t> &listOfCacheStartAddress, const std::vector<uintptr_t> &listOfCacheSizeBytes)
{
J9SharedClassCacheDescriptor * cur = NULL;
J9SharedClassCacheDescriptor * prev = NULL;
J9SharedClassCacheDescriptor * head = NULL;
for (size_t i = 0; i < listOfCacheStartAddress.size(); i++)
{
cur = new (PERSISTENT_NEW) J9SharedClassCacheDescriptor();
cur->cacheStartAddress = (J9SharedCacheHeader*)(listOfCacheStartAddress[i]);
cur->cacheSizeBytes = listOfCacheSizeBytes[i];
if (prev)
{
prev->next = cur;
cur->previous = prev;
}
else
{
head = cur;
}
prev = cur;
}
if (!head)
return NULL;
head->previous = prev; // assign head's previous to tail
prev->next = head; // assign tail's previous to head
return head;
}
void
ClientSessionData::destroyJ9SharedClassCacheDescriptorList()
{
if (!_vmInfo->_j9SharedClassCacheDescriptorList)
return;
J9SharedClassCacheDescriptor * cur = _vmInfo->_j9SharedClassCacheDescriptorList;
J9SharedClassCacheDescriptor * next = NULL;
_vmInfo->_j9SharedClassCacheDescriptorList->previous->next = NULL; // break the circular links by setting tail's next pointer to be NULL
while (cur)
{
next = cur->next;
jitPersistentFree(cur);
cur = next;
}
_vmInfo->_j9SharedClassCacheDescriptorList = NULL;
}
void
ClientSessionData::clearCaches()
{
{
OMR::CriticalSection clearCache(getClassMapMonitor());
_classByNameMap.clear();
}
OMR::CriticalSection getRemoteROMClass(getROMMapMonitor());
// Free memory for all hashtables with IProfiler info
for (auto& it : _J9MethodMap)
{
IPTable_t *ipDataHT = it.second._IPData;
// It it exists, walk the collection of <pc, TR_IPBytecodeHashTableEntry*> mappings
if (ipDataHT)
{
for (auto& entryIt : *ipDataHT)
{
auto entryPtr = entryIt.second;
if (entryPtr)
jitPersistentFree(entryPtr);
}
ipDataHT->~IPTable_t();
jitPersistentFree(ipDataHT);
it.second._IPData = NULL;
}
}
_J9MethodMap.clear();
// Free memory for j9class info
for (auto& it : _romClassMap)
it.second.freeClassInfo();
_romClassMap.clear();
_classChainDataMap.clear();
// Free CHTable
for (auto& it : _chTableClassMap)
{
TR_PersistentClassInfo *classInfo = it.second;
classInfo->removeSubClasses();
jitPersistentFree(classInfo);
}
_chTableClassMap.clear();
_requestUnloadedClasses = true;
_registeredJ2IThunksMap.clear();
_registeredInvokeExactJ2IThunksSet.clear();
}
void
ClientSessionData::destroy(ClientSessionData *clientSession)
{
clientSession->~ClientSessionData();
TR_PersistentMemory::jitPersistentFree(clientSession);
}
// notifyAndDetachFirstWaitingThread needs to be executed with sequencingMonitor in hand
TR_MethodToBeCompiled *
ClientSessionData::notifyAndDetachFirstWaitingThread()
{
TR_MethodToBeCompiled *entry = _OOSequenceEntryList;
if (entry)
{
entry->getMonitor()->enter();
entry->getMonitor()->notifyAll();
entry->getMonitor()->exit();
_OOSequenceEntryList = entry->_next;
}
return entry;
}
char *
ClientSessionData::ClassInfo::getRemoteROMString(int32_t &len, void *basePtr, std::initializer_list<size_t> offsets)
{
auto offsetFirst = *offsets.begin();
auto offsetSecond = (offsets.size() == 2) ? *(offsets.begin() + 1) : 0;
TR_ASSERT(offsetFirst < (1 << 16) && offsetSecond < (1 << 16), "Offsets are larger than 16 bits");
TR_ASSERT(offsets.size() <= 2, "Number of offsets is greater than 2");
// create a key for hashing into a table of strings
TR_RemoteROMStringKey key;
uint32_t offsetKey = (offsetFirst << 16) + offsetSecond;
key._basePtr = basePtr;
key._offsets = offsetKey;
std::string *cachedStr = NULL;
bool isCached = false;
auto gotStr = _remoteROMStringsCache.find(key);
if (gotStr != _remoteROMStringsCache.end())
{
cachedStr = &(gotStr->second);
isCached = true;
}
// only make a query if a string hasn't been cached
if (!isCached)
{
size_t offsetFromROMClass = (uint8_t*) basePtr - (uint8_t*) _romClass;
std::string offsetsStr((char*) offsets.begin(), offsets.size() * sizeof(size_t));
JITServer::ServerStream *stream = TR::CompilationInfo::getStream();
stream->write(JITServer::MessageType::ClassInfo_getRemoteROMString, _remoteRomClass, offsetFromROMClass, offsetsStr);
cachedStr = &(_remoteROMStringsCache.insert({key, std::get<0>(stream->read<std::string>())}).first->second);
}
len = cachedStr->length();
return &(cachedStr->at(0));
}
// Takes a pointer to some data which is placed statically relative to the rom class,
// as well as a list of offsets to J9SRP fields. The first offset is applied before the first
// SRP is followed.
//
// If at any point while following the chain of SRP pointers we land outside the ROM class,
// then we fall back to getRemoteROMString which follows the same process on the client.
//
// This is a workaround because some data referenced in the ROM constant pool is located outside of
// it, but we cannot easily determine if the reference is outside or not (or even if it is a reference!)
// because the data is untyped.
char *
ClientSessionData::ClassInfo::getROMString(int32_t &len, void *basePtr, std::initializer_list<size_t> offsets)
{
uint8_t *ptr = (uint8_t*) basePtr;
for (size_t offset : offsets)
{
ptr += offset;
if (!JITServerHelpers::isAddressInROMClass(ptr, _romClass))
return getRemoteROMString(len, basePtr, offsets);
ptr = ptr + *(J9SRP*)ptr;
}
if (!JITServerHelpers::isAddressInROMClass(ptr, _romClass))
return getRemoteROMString(len, basePtr, offsets);
char *data = utf8Data((J9UTF8*) ptr, len);
return data;
}
template <typename map, typename key>
void ClientSessionData::purgeCache(std::vector<ClassUnloadedData> *unloadedClasses, map& m, key ClassUnloadedData::*k)
{
ClassUnloadedData *data = unloadedClasses->data();
std::vector<ClassUnloadedData>::iterator it = unloadedClasses->begin();
while (it != unloadedClasses->end())
{
if (it->_cached)
{
m.erase((data->*k));
}
else
{
//If the class is not cached this is the place to iterate the cache(Map) for deleting the entry by value rather then key.
auto itClass = m.begin();
while (itClass != m.end())
{
if (itClass->second == data->_class)
{
m.erase(itClass);
break;
}
++itClass;
}
}
//DO NOT remove the entry from the unloadedClasses as it will be needed to purge other caches.
++it;
++data;
}
}
ClientSessionHT*
ClientSessionHT::allocate()
{
return new (PERSISTENT_NEW) ClientSessionHT();
}
ClientSessionHT::ClientSessionHT() : _clientSessionMap(decltype(_clientSessionMap)::allocator_type(TR::Compiler->persistentAllocator())),
TIME_BETWEEN_PURGES(1000*60*30), // JITServer TODO: this must come from options
OLD_AGE(1000*60*1000) // 1000 minutes
{
PORT_ACCESS_FROM_PORT(TR::Compiler->portLib);
_timeOfLastPurge = j9time_current_time_millis();
_clientSessionMap.reserve(250); // allow room for at least 250 clients
}
// The destructor is currently never called because the server does not exit cleanly
ClientSessionHT::~ClientSessionHT()
{
for (auto iter = _clientSessionMap.begin(); iter != _clientSessionMap.end(); ++iter)
{
ClientSessionData::destroy(iter->second); // delete the client data
_clientSessionMap.erase(iter); // delete the mapping from the hashtable
}
}
// Search the clientSessionHashtable for the given clientUID and return the
// data corresponding to the client.
// If the clientUID does not already exist in the HT, insert a new blank entry.
// Must have compilation monitor in hand when calling this function.
// Side effects: _inUse is incremented on the ClientSessionData
// _expectedSeqNo is populated if a new ClientSessionData is created
// timeOflastAccess is updated with curent time.
ClientSessionData *
ClientSessionHT::findOrCreateClientSession(uint64_t clientUID, uint32_t seqNo, bool *newSessionWasCreated)
{
*newSessionWasCreated = false;
ClientSessionData *clientData = findClientSession(clientUID);
if (!clientData)
{
// alocate a new ClientSessionData object and create a clientUID mapping
clientData = new (PERSISTENT_NEW) ClientSessionData(clientUID, seqNo);
if (clientData)
{
_clientSessionMap[clientUID] = clientData;
*newSessionWasCreated = true;
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "Server allocated data for a new clientUID %llu", (unsigned long long)clientUID);
}
else
{
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "ERROR: Server could not allocate client session data");
// should we throw bad_alloc here?
}
}
return clientData;
}
// Search the clientSessionHashtable for the given clientUID and delete the
// data corresponding to the client. Return true if client data found and deleted.
// Must have compilation monitor in hand when calling this function.
bool
ClientSessionHT::deleteClientSession(uint64_t clientUID, bool forDeletion)
{
ClientSessionData *clientData = NULL;
auto clientDataIt = _clientSessionMap.find(clientUID);
if (clientDataIt != _clientSessionMap.end())
{
clientData = clientDataIt->second;
if (forDeletion)
clientData->markForDeletion();
if ((clientData->getInUse() == 0) && clientData->isMarkedForDeletion())
{
ClientSessionData::destroy(clientData); // delete the client data
_clientSessionMap.erase(clientDataIt); // delete the mapping from the hashtable
return true;
}
}
return false;
}
// Search the clientSessionHashtable for the given clientUID and return the
// data corresponding to the client.
// Must have compilation monitor in hand when calling this function.
// Side effects: _inUse is incremented on the ClientSessionData and the
// timeOflastAccess is updated with curent time.
ClientSessionData *
ClientSessionHT::findClientSession(uint64_t clientUID)
{
ClientSessionData *clientData = NULL;
auto clientDataIt = _clientSessionMap.find(clientUID);
if (clientDataIt != _clientSessionMap.end())
{
// if clientData found in hashtable, update the access time before returning it
clientData = clientDataIt->second;
clientData->incInUse();
clientData->updateTimeOfLastAccess();
}
return clientData;
}
// Purge the old client session data from the hashtable and
// update the timeOfLastPurge.
// Entries with _inUse > 0 must be left alone, though having
// very old entries in use it's a sign of a programming error.
// This routine must be executed with compilation monitor in hand.
void
ClientSessionHT::purgeOldDataIfNeeded()
{
PORT_ACCESS_FROM_PORT(TR::Compiler->portLib);
int64_t crtTime = j9time_current_time_millis();
if (crtTime - _timeOfLastPurge > TIME_BETWEEN_PURGES)
{
// Time for a purge operation.
// Scan the entire table and delete old elements that are not in use
for (auto iter = _clientSessionMap.begin(); iter != _clientSessionMap.end(); ++iter)
{
TR_ASSERT(iter->second->getInUse() >= 0, "_inUse=%d must be positive\n", iter->second->getInUse());
if (iter->second->getInUse() == 0 &&
crtTime - iter->second->getTimeOflastAccess() > OLD_AGE)
{
if (TR::Options::getVerboseOption(TR_VerboseJITServer))
TR_VerboseLog::writeLineLocked(TR_Vlog_JITServer, "Server will purge session data for clientUID %llu", (unsigned long long)iter->first);
ClientSessionData::destroy(iter->second); // delete the client data
_clientSessionMap.erase(iter); // delete the mapping from the hashtable
}
}
_timeOfLastPurge = crtTime;
// JITServer TODO: keep stats on how many elements were purged
}
}
// to print these stats,
// set the env var `TR_PrintJITServerCacheStats=1`
// run the server with `-Xdump:jit:events=user`
// then `kill -3` it when you want to print them
void
ClientSessionHT::printStats()
{
PORT_ACCESS_FROM_PORT(TR::Compiler->portLib);
j9tty_printf(PORTLIB, "Client sessions:\n");
for (auto session : _clientSessionMap)
{
j9tty_printf(PORTLIB, "Session for id %d:\n", session.first);
session.second->printStats();
}
}