-
Notifications
You must be signed in to change notification settings - Fork 752
/
Copy pathrossa.cpp
2237 lines (1913 loc) · 85 KB
/
rossa.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, 2021 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WINDOWS
// Undefine the winsockapi because winsock2 defines it. Removes warnings.
#if defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
#undef _WINSOCKAPI_
#endif
#include <winsock2.h>
#include <process.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <pthread.h>
#ifdef J9ZOS390
#include <arpa/inet.h>
#endif
#endif
#ifdef J9ZTPF
#include <tpf/c_eb0eb.h>
#include <tpf/sysapi.h>
#include <tpf/tpfapi.h>
#include <tpf/c_cinfc.h>
#endif
#include "env/annotations/AnnotationBase.hpp"
#define J9_EXTERNAL_TO_VM
#include "codegen/PrivateLinkage.hpp"
#include "control/CompilationRuntime.hpp"
#include "control/CompilationThread.hpp"
#include "control/JitDump.hpp"
#include "control/Recompilation.hpp"
#include "control/RecompilationInfo.hpp"
#include "runtime/ArtifactManager.hpp"
#include "runtime/CodeCache.hpp"
#include "runtime/CodeCacheManager.hpp"
#include "runtime/CodeCacheReclamation.h"
#include "runtime/codertinit.hpp"
#include "runtime/IProfiler.hpp"
#include "runtime/HWProfiler.hpp"
#include "runtime/RelocationRuntime.hpp"
#include "env/PersistentInfo.hpp"
#include "env/ClassLoaderTable.hpp"
#include "env/J2IThunk.hpp"
#include "env/PersistentCHTable.hpp"
#include "env/CompilerEnv.hpp"
#include "env/jittypes.h"
#include "env/ClassTableCriticalSection.hpp"
#include "env/VerboseLog.hpp"
#include "ilgen/IlGeneratorMethodDetails_inlines.hpp"
/* Hardware Profiling */
#if defined(TR_HOST_S390) && defined(BUILD_Z_RUNTIME_INSTRUMENTATION)
#include "z/runtime/ZHWProfiler.hpp"
#elif defined(TR_HOST_POWER)
#include "p/runtime/PPCHWProfiler.hpp"
#endif
#include "control/rossa.h"
#include "control/OptimizationPlan.hpp"
#include "control/CompilationController.hpp"
#include "runtime/IProfiler.hpp"
#define _UTE_STATIC_
#include "env/ut_j9jit.h"
#include "jitprotos.h"
#if defined(J9VM_OPT_SHARED_CLASSES)
#include "j9jitnls.h"
#endif
#include "env/J9SharedCache.hpp"
#ifdef J9VM_OPT_JAVA_CRYPTO_ACCELERATION
#include "runtime/Crypto.hpp"
#endif
#include "infra/Monitor.hpp"
#include "j9.h"
#include "j9cfg.h"
#include "vmaccess.h"
#include "jvminit.h"
#include "j9port.h"
#include "env/exports.h"
#if defined(J9VM_OPT_JITSERVER)
#include "env/JITServerPersistentCHTable.hpp"
#include "net/CommunicationStream.hpp"
#include "net/ClientStream.hpp"
#include "net/LoadSSLLibs.hpp"
#include "runtime/JITClientSession.hpp"
#include "runtime/JITServerAOTCache.hpp"
#include "runtime/JITServerAOTDeserializer.hpp"
#include "runtime/JITServerIProfiler.hpp"
#include "runtime/JITServerSharedROMClassCache.hpp"
#include "runtime/JITServerStatisticsThread.hpp"
#include "runtime/Listener.hpp"
#endif /* defined(J9VM_OPT_JITSERVER) */
extern "C" int32_t encodeCount(int32_t count);
extern "C" {
struct J9RASdumpContext;
}
#if defined(TR_TARGET_X86) && defined(TR_HOST_32BIT)
extern TR_X86CPUIDBuffer *queryX86TargetCPUID(void * javaVM);
#endif
extern void setupCodeCacheParameters(int32_t *, OMR::CodeCacheCodeGenCallbacks *callBacks, int32_t *numHelpers, int32_t *CCPreLoadedCodeSize);
extern "C" void stopInterpreterProfiling(J9JITConfig *jitConfig);
extern "C" void restartInterpreterProfiling();
#ifdef J9VM_JIT_RUNTIME_INSTRUMENTATION
// extern until these functions are added to oti/jitprotos.h
extern "C" UDATA initializeJITRuntimeInstrumentation(J9JavaVM *vm);
#endif
extern void *ppcPicTrampInit(TR_FrontEnd *, TR::PersistentInfo *);
extern TR_Debug *createDebugObject(TR::Compilation *);
bool isQuickstart = false;
#define TRANSLATE_METHODHANDLE_TAKES_FLAGS
TR::Monitor *vpMonitor = 0;
char *compilationErrorNames[]={
"compilationOK", // 0
"compilationFailure", // 1
"compilationRestrictionILNodes", // 2
"compilationRestrictionRecDepth", // 3
"compilationRestrictedMethod", // 4
"compilationExcessiveComplexity", // 5
"compilationNotNeeded", // 6
"compilationSuspended", // 7
"compilationExcessiveSize", // 8
"compilationInterrupted", // 9
"compilationMetaDataFailure", //10
"compilationInProgress", //11
"compilationCHTableCommitFailure",//12
"compilationMaxCallerIndexExceeded",//13
"compilationKilledByClassReplacement",//14
"compilationHeapLimitExceeded", //15
"compilationNeededAtHigherLevel", //16
"compilationAotValidateFieldFailure", //17
"compilationAotStaticFieldReloFailure", //18
"compilationAotClassReloFailure", //19
"compilationAotThunkReloFailure", //20
"compilationAotTrampolineReloFailure", //21
"compilationAotPicTrampolineReloFailure", //22
"compilationAotCacheFullReloFailure", //23
"compilationAotUnknownReloTypeFailure", //24
"compilationCodeReservationFailure", //25
"compilationAotHasInvokehandle", //26
"compilationTrampolineFailure", //27
"compilationRecoverableTrampolineFailure", // 28
"compilationIlGenFailure", // 29
"compilationIllegalCodeCacheSwitch", // 30
"compilationNullSubstituteCodeCache", // 31
"compilationCodeMemoryExhausted", // 32
"compilationGCRPatchFailure", // 33
"compilationAotValidateMethodExitFailure", // 34
"compilationAotValidateMethodEnterFailure", // 35
"compilationAotArrayClassReloFailure", // 36
"compilationLambdaEnforceScorching", // 37
"compilationInternalPointerExceedLimit", // 38
"compilationAotRelocationInterrupted", // 39
"compilationAotClassChainPersistenceFailure", // 40
"compilationLowPhysicalMemory", // 41
"compilationDataCacheError", // 42
"compilationCodeCacheError", // 43
"compilationRecoverableCodeCacheError", // 44
"compilationAotHasInvokeVarHandle", //45
"compilationAotValidateStringCompressionFailure", // 46
"compilationFSDHasInvokeHandle", //47
"compilationVirtualAddressExhaustion", //48
"compilationEnforceProfiling", //49
"compilationSymbolValidationManagerFailure", //50
"compilationAOTNoSupportForAOTFailure", //51
"compilationAOTValidateTMFailure", //52
"compilationILGenUnsupportedValueTypeOperationFailure", //53
"compilationAOTRelocationRecordGenerationFailure", //54
"compilationAotPatchedCPConstant", //55
"compilationAotHasInvokeSpecialInterface", //56
"compilationAotValidateExceptionHookFailure", //57
"compilationAotBlockFrequencyReloFailure", //58
"compilationAotRecompQueuedFlagReloFailure", //59
"compilationAOTValidateOSRFailure", //60
#if defined(J9VM_OPT_JITSERVER)
"compilationStreamFailure", //compilationFirstJITServerFailure=61
"compilationStreamLostMessage", // compilationFirstJITServerFailure+1
"compilationStreamMessageTypeMismatch", //compilationFirstJITServerFailure+2
"compilationStreamVersionIncompatible", //compilationFirstJITServerFailure+3
"compilationStreamInterrupted", //compilationFirstJITServerFailure+4
#endif /* defined(J9VM_OPT_JITSERVER) */
"compilationMaxError",
};
int32_t aggressiveOption = 0;
// Tells the sampler thread whether it is being interrupted to resume it or to
// shut it down.
//
volatile bool shutdownSamplerThread = false; // only set once
extern "C" void *compileMethodHandleThunk(j9object_t methodHandle, j9object_t arg, J9VMThread *vmThread, U_32 flags);
extern "C" J9NameAndSignature newInstancePrototypeNameAndSig;
extern "C" void getOutOfIdleStates(TR::CompilationInfo::TR_SamplerStates expectedState, TR::CompilationInfo *compInfo, const char* reason);
extern "C" IDATA
launchGPU(J9VMThread *vmThread, jobject invokeObject,
J9Method *method, int deviceId,
I_32 gridDimX, I_32 gridDimY, I_32 gridDimZ,
I_32 blockDimX, I_32 blockDimY, I_32 blockDimZ,
void **args);
extern "C" void promoteGPUCompile(J9VMThread *vmThread);
extern "C" int32_t setUpHooks(J9JavaVM * javaVM, J9JITConfig * jitConfig, TR_FrontEnd * vm);
extern "C" int32_t startJITServer(J9JITConfig *jitConfig);
extern "C" int32_t waitJITServerTermination(J9JITConfig *jitConfig);
char *AOTcgDiagOn="1";
// -----------------------------------------------------------------------------
// Method translation
// -----------------------------------------------------------------------------
extern "C" IDATA j9jit_testarossa(struct J9JITConfig * jitConfig, J9VMThread * vmThread, J9Method * method, void * oldStartPC)
{
return j9jit_testarossa_err(jitConfig, vmThread, method, oldStartPC, NULL);
}
extern "C" IDATA
j9jit_testarossa_err(
struct J9JITConfig *jitConfig,
J9VMThread *vmThread,
J9Method *method,
void *oldStartPC,
TR_CompilationErrorCode *compErrCode)
{
// The method is called by the interpreter when a method's invocation count has reached zero.
// Rather than just compiling blindly, pass the controller an event, and let it decide what to do.
//
bool queued = false;
TR_YesNoMaybe async = TR_maybe;
TR_MethodEvent event;
if (oldStartPC)
{
// any recompilation attempt using fixUpMethodCode would go here
J9::PrivateLinkage::LinkageInfo *linkageInfo = J9::PrivateLinkage::LinkageInfo::get(oldStartPC);
TR_PersistentJittedBodyInfo* jbi = TR::Recompilation::getJittedBodyInfoFromPC(oldStartPC);
if (!jbi)
{
return 0;
}
TR_PersistentMethodInfo *pmi = jbi->getMethodInfo();
if (pmi && pmi->hasBeenReplaced()) // HCR
{
// Obsolete method bodies are invalid.
//
TR::Recompilation::fixUpMethodCode(oldStartPC);
jbi->setIsInvalidated();
}
if (jbi->getIsInvalidated())
{
event._eventType = TR_MethodEvent::MethodBodyInvalidated;
async = TR_no;
}
else
{
// Async compilation is disabled or recompilations triggered from jitted code
//
// Check if the method has already been scheduled for compilation and return
// abruptly if so. This will reduce contention on the optimizationPlanMonitor
// when a profiled compilation is followed by a very slow recompilation
// (until the recompilation is over, the java threads will try to trigger
// recompilations again and again. See defect 193193)
//
if (linkageInfo->isBeingCompiled())
{
TR_J9VMBase *fe = TR_J9VMBase::get(jitConfig, vmThread);
if (fe->isAsyncCompilation())
return 0; // early return because the method is queued for compilation
}
// If PersistentJittedBody contains the profile Info and has BlockFrequencyInfo, it will set the
// isQueuedForRecompilation field which can be used by the jitted code at runtime to skip the profiling
// code if it has made request to recompile this method.
if (jbi->getProfileInfo() != NULL && jbi->getProfileInfo()->getBlockFrequencyInfo() != NULL)
jbi->getProfileInfo()->getBlockFrequencyInfo()->setIsQueuedForRecompilation();
event._eventType = TR_MethodEvent::OtherRecompilationTrigger;
}
}
else
{
event._eventType = TR_MethodEvent::InterpreterCounterTripped;
// Experimental code: user may want to artificially delay the compilation
// of methods to gather more IProfiler info
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
if (TR::Options::_compilationDelayTime > 0)
{
if (!TR::CompilationInfo::isJNINative(method))
{
if (compInfo->getPersistentInfo()->getElapsedTime() < 1000 * TR::Options::_compilationDelayTime)
{
// Add 2 to the invocationCount
int32_t count = TR::CompilationInfo::getInvocationCount(method);
if (count >= 0)
{
TR::CompilationInfo::setInvocationCount(method, 2);
return 0;
}
}
}
}
#if defined(J9VM_OPT_JITSERVER)
// Do not allow local compilations in JITServer server mode
if (compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::SERVER)
return 0;
#endif
}
event._j9method = method;
event._oldStartPC = oldStartPC;
event._vmThread = vmThread;
event._classNeedingThunk = 0;
bool newPlanCreated;
IDATA result = 0;
TR_OptimizationPlan *plan = TR::CompilationController::getCompilationStrategy()->processEvent(&event, &newPlanCreated);
// if the controller decides to compile this method, trigger the compilation
if (plan)
{
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
// Check to see if we need to wake up the sampling thread should it be in DEEPIDLE state
if (compInfo->getSamplerState() == TR::CompilationInfo::SAMPLER_DEEPIDLE &&
compInfo->_intervalStats._numFirstTimeCompilationsInInterval >= 1)
getOutOfIdleStates(TR::CompilationInfo::SAMPLER_DEEPIDLE, compInfo, "comp req");
else if (compInfo->getSamplerState() == TR::CompilationInfo::SAMPLER_IDLE &&
compInfo->_intervalStats._numFirstTimeCompilationsInInterval >= TR::Options::_numFirstTimeCompilationsToExitIdleMode)
getOutOfIdleStates(TR::CompilationInfo::SAMPLER_IDLE, compInfo, "comp req");
{ // scope for details
TR::IlGeneratorMethodDetails details(method);
result = (IDATA)compInfo->compileMethod(vmThread, details, oldStartPC, async, compErrCode, &queued, plan);
}
if (!queued && newPlanCreated)
TR_OptimizationPlan::freeOptimizationPlan(plan);
}
else // OOM; Very rare case
{
// Invalidation requests MUST not be ignored even if OOM
if (event._eventType == TR_MethodEvent::MethodBodyInvalidated)
{
// Allocate a plan on the stack and force a synchronous compilation
// so that we wait until the compilation finishes
TR_OptimizationPlan plan;
plan.init(noOpt); // use the cheapest compilation; It's unlikely the compilation will
// succeed because we are running low on native memory
plan.setIsStackAllocated(); // mark that this plan is not dynamically allocated
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
{ // scope for details
TR::IlGeneratorMethodDetails details(method);
result = (IDATA)compInfo->compileMethod(vmThread, details, oldStartPC, async, compErrCode, &queued, &plan);
}
// We should probably prevent any future non-essential compilation
compInfo->getPersistentInfo()->setDisableFurtherCompilation(true);
if (TR::Options::getCmdLineOptions()->getVerboseOption(TR_VerbosePerformance))
{
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF,"t=%6u Disable further compilation due to OOM while creating an optimization plan", (uint32_t)compInfo->getPersistentInfo()->getElapsedTime());
}
}
}
return result;
}
extern "C" IDATA
retranslateWithPreparationForMethodRedefinition(
struct J9JITConfig *jitConfig,
J9VMThread *vmThread,
J9Method *method,
void *oldStartPC)
{
return retranslateWithPreparation(jitConfig, vmThread, method, oldStartPC, TR_PersistentMethodInfo::RecompDueToInlinedMethodRedefinition);
}
extern "C" IDATA
retranslateWithPreparation(
struct J9JITConfig *jitConfig,
J9VMThread *vmThread,
J9Method *method,
void *oldStartPC,
UDATA reason)
{
if (!TR::CompilationInfo::get()->asynchronousCompilation() && !J9::PrivateLinkage::LinkageInfo::get(oldStartPC)->recompilationAttempted())
{
TR::Recompilation::fixUpMethodCode(oldStartPC);
}
TR_PersistentJittedBodyInfo* jbi = TR::Recompilation::getJittedBodyInfoFromPC(oldStartPC);
if (jbi)
{
TR_PersistentMethodInfo *pmi = jbi->getMethodInfo();
if (pmi)
pmi->setReasonForRecompilation(reason);
}
return j9jit_testarossa(jitConfig, vmThread, method, oldStartPC);
}
extern "C" void *
old_translateMethodHandle(J9VMThread *currentThread, j9object_t methodHandle)
{
void *result = compileMethodHandleThunk(methodHandle, NULL, currentThread, 0);
if (result)
{
static char *returnNullFromTranslateMethodHandle = feGetEnv("TR_returnNullFromTranslateMethodHandle");
if (returnNullFromTranslateMethodHandle)
result = NULL;
}
return result;
}
extern "C" void *
translateMethodHandle(J9VMThread *currentThread, j9object_t methodHandle, j9object_t arg, U_32 flags)
{
void *result = compileMethodHandleThunk(methodHandle, arg, currentThread, flags);
if (result)
{
static char *returnNullFromTranslateMethodHandle = feGetEnv("TR_returnNullFromTranslateMethodHandle");
if (returnNullFromTranslateMethodHandle)
result = NULL;
}
return result;
}
// -----------------------------------------------------------------------------
// NewInstanceImpl Thunks
// -----------------------------------------------------------------------------
extern "C" J9Method *
getNewInstancePrototype(J9VMThread * context)
{
J9Method *newInstanceProto = 0;
J9InternalVMFunctions *intFunc = context->javaVM->internalVMFunctions;
J9Class * jlClass = intFunc->internalFindKnownClass(context, J9VMCONSTANTPOOL_JAVALANGCLASS, J9_FINDKNOWNCLASS_FLAG_EXISTING_ONLY);
if (jlClass)
{
newInstanceProto = (J9Method *) intFunc->javaLookupMethod(
context,
jlClass,
(J9ROMNameAndSignature *) &newInstancePrototypeNameAndSig,
0,
J9_LOOK_DIRECT_NAS | J9_LOOK_VIRTUAL | J9_LOOK_NO_JAVA);
}
return newInstanceProto;
}
extern "C" UDATA
j9jit_createNewInstanceThunk_err(
struct J9JITConfig *jitConfig,
J9VMThread *vmThread,
J9Class *classNeedingThunk,
TR_CompilationErrorCode *compErrCode)
{
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
J9Method *method = getNewInstancePrototype(vmThread);
if (!method)
{
*compErrCode = compilationFailure;
return 0;
}
#if defined(J9VM_OPT_JITSERVER)
// Do not allow local compilations in JITServer server mode
if (compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::SERVER)
return 0;
#endif
bool queued = false;
TR_MethodEvent event;
event._eventType = TR_MethodEvent::NewInstanceImpl;
event._j9method = method;
event._oldStartPC = 0;
event._vmThread = vmThread;
bool newPlanCreated;
TR_OptimizationPlan *plan = TR::CompilationController::getCompilationStrategy()->processEvent(&event, &newPlanCreated);
UDATA result = 0;
if (plan)
{
// if the controller decides to compile this method, trigger the compilation and wait here
{ // scope for details
J9::NewInstanceThunkDetails details(method, classNeedingThunk);
result = (UDATA)compInfo->compileMethod(vmThread, details, 0, TR_maybe, compErrCode, &queued, plan);
}
if (!queued && newPlanCreated)
TR_OptimizationPlan::freeOptimizationPlan(plan);
}
return result;
}
extern "C" UDATA
j9jit_createNewInstanceThunk(struct J9JITConfig * jitConfig, J9VMThread * vmThread, J9Class * classNeedingThunk)
{
return j9jit_createNewInstanceThunk_err(jitConfig, vmThread, classNeedingThunk, NULL);
}
// -----------------------------------------------------------------------------
// JIT shutdown
// -----------------------------------------------------------------------------
extern "C" void
stopSamplingThread(J9JITConfig * jitConfig)
{
// Was the samplerThread even created?
if (jitConfig->samplerThread)
{
// TODO: add a trace point in this routine
//Trc_JIT_stopSamplingThread_Entry();
TR::CompilationInfo *compInfo = getCompilationInfo(jitConfig);
j9thread_monitor_enter(jitConfig->samplerMonitor);
// In most cases the state of the sampling thread should be SAMPLE_THR_INITIALIZED
// However, if an early error happens, stage 15 might not be run (this is where we initialize
// the thread) and so the thread could be in ATTACHED_STATE
TR_ASSERT(compInfo->getSamplingThreadLifetimeState() == TR::CompilationInfo::SAMPLE_THR_INITIALIZED ||
compInfo->getSamplingThreadLifetimeState() == TR::CompilationInfo::SAMPLE_THR_ATTACHED,
"sampling thread must be initialized or at least attached if we try to stop it");
// inform samplerThread to exit its loop
shutdownSamplerThread = true;
compInfo->setSamplingThreadLifetimeState(TR::CompilationInfo::SAMPLE_THR_STOPPING);
// interrupt possible nap time for the samplerThread
j9thread_interrupt(jitConfig->samplerThread);
// Wait for samplerThread to exit
while (compInfo->getSamplingThreadLifetimeState() != TR::CompilationInfo::SAMPLE_THR_DESTROYED)
j9thread_monitor_wait(jitConfig->samplerMonitor);
// At this point samplingThread has gone away
compInfo->setSamplerThread(NULL);
jitConfig->samplerThread = 0; // just in case we enter this routine again
j9thread_monitor_exit(jitConfig->samplerMonitor);
// sampleMonitor is no longer needed
j9thread_monitor_destroy(jitConfig->samplerMonitor);
jitConfig->samplerMonitor = 0;
//Trc_JIT_stopSamplingThread_Exit();
}
}
extern "C" void
freeJITConfig(J9JITConfig * jitConfig)
{
// This method must be called only when we are sure that there are no other java threads running
if (jitConfig)
{
// Do all JIT compiler present freeing.
//
J9JavaVM * javaVM = jitConfig->javaVM;
PORT_ACCESS_FROM_JAVAVM(javaVM);
// stopSamplingThread(jitConfig); // done during JitShutdown below
jitConfig->runtimeFlags &= ~J9JIT_JIT_ATTACHED;
// If the jitConfig will be freed,
// there is no chance to do all te JIT cleanup.
// Do the JIT cleanup now, even if we tried it before
//
JitShutdown(jitConfig);
// free the compilation info before freeing the frontend without thread info (jitConfig->compilationInfo)
// which is done on codert_OnUnload
TR::CompilationInfo::freeCompilationInfo(jitConfig);
// Tell the runtime to unload.
//
codert_OnUnload(javaVM);
}
}
// exclusive shutdown. This normally occurs when System.exit() is called by
// the usercode. The VM needs to notify the compilation thread before going
// exclusive - otherwise we end up with a deadlock scenario upon exit
//
extern "C" void
jitExclusiveVMShutdownPending(J9VMThread * vmThread)
{
#ifndef SMALL_APPTHREAD
J9JavaVM *javaVM = vmThread->javaVM;
#if defined(J9VM_OPT_JITSERVER)
TR::CompilationInfo * compInfo = getCompilationInfo(javaVM->jitConfig);
if (compInfo->getPersistentInfo()->getRemoteCompilationMode() == JITServer::SERVER)
{
TR_Listener *listener = ((TR_JitPrivateConfig*)(javaVM->jitConfig->privateConfig))->listener;
if (listener)
{
listener->stop();
}
}
#endif /* defined(J9VM_OPT_JITSERVER) */
getCompilationInfo(javaVM->jitConfig)->stopCompilationThreads();
#endif
}
// Code cache callbacks to be used by the VM
//
extern "C" U_8*
getCodeCacheWarmAlloc(void *codeCache)
{
TR::CodeCache * cc = static_cast<TR::CodeCache*>(codeCache);
return cc->getWarmCodeAlloc();
}
extern "C" U_8*
getCodeCacheColdAlloc(void *codeCache)
{
TR::CodeCache * cc = static_cast<TR::CodeCache*>(codeCache);
return cc->getColdCodeAlloc();
}
// -----------------------------------------------------------------------------
// JIT control
// -----------------------------------------------------------------------------
// Temporarily disable the jit so that no more methods are compiled
extern "C" void
disableJit(J9JITConfig * jitConfig)
{
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
J9JavaVM * vm = jitConfig->javaVM;
if (compInfo && compInfo->getNumCompThreadsActive() > 0)
{
// Suspend compilation.
// This will also flush any methods waiting for compilation
//
compInfo->suspendCompilationThread();
// Generate a trace point
Trc_JIT_DisableJIT(vm->internalVMFunctions->currentVMThread(vm));
// Turn off interpreter profiling
//
#if defined(J9VM_INTERP_PROFILING_BYTECODES)
stopInterpreterProfiling(jitConfig);
#endif
j9thread_monitor_enter(vm->vmThreadListMutex);
// Set the sampling frequency to a high value - this will send the sampling
// thread to sleep for a long time. If the jit is enabled later we will have
// to interrupt the sampling thread to get it going again.
//
TR::CompilationInfo::TR_SamplerStates samplerState = compInfo->getSamplerState();
if (samplerState != TR::CompilationInfo::SAMPLER_NOT_INITIALIZED && //jitConfig->samplerThread && // Must have a sampler thread
samplerState != TR::CompilationInfo::SAMPLER_SUSPENDED && // Nothing to do if sampler is already suspended
samplerState != TR::CompilationInfo::SAMPLER_STOPPED)
{
TR::PersistentInfo *persistentInfo = compInfo->getPersistentInfo();
compInfo->setSamplerState(TR::CompilationInfo::SAMPLER_SUSPENDED);
jitConfig->samplingFrequency = MAX_SAMPLING_FREQUENCY;
persistentInfo->setLastTimeSamplerThreadWasSuspended(persistentInfo->getElapsedTime());
if (TR::Options::getVerboseOption(TR_VerbosePerformance))
{
TR_VerboseLog::writeLineLocked(TR_Vlog_PERF,"t=%u\tSampling thread suspended and changed frequency to %d ms",
persistentInfo->getElapsedTime(), jitConfig->samplingFrequency);
}
}
// TODO: what are the implications of remaining in an appropriate JIT state?
// Should we change the state to STEADY just to be on the safe side?
// What if this happens during startup? We will kill the startup time?
//set the countDelta
J9VMThread * currentThread = vm->mainThread;
do
{
currentThread->jitCountDelta = 0;
} while ((currentThread = currentThread->linkNext) != vm->mainThread);
j9thread_monitor_exit(vm->vmThreadListMutex);
}
}
// Re-enable the jit after it has been disabled.
extern "C" void
enableJit(J9JITConfig * jitConfig)
{
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
J9JavaVM * vm = jitConfig->javaVM;
if (compInfo && compInfo->getNumCompThreadsActive() == 0)
{
// Re-enable interpreter profiling if needed
//
#if defined(J9VM_INTERP_PROFILING_BYTECODES)
restartInterpreterProfiling();
#endif
// Resume compilation.
//
compInfo->resumeCompilationThread();
// Generate a trace point
Trc_JIT_EnableJIT(vm->internalVMFunctions->currentVMThread(vm));
j9thread_monitor_enter(vm->vmThreadListMutex);
// Set the sampling frequency back to its proper value and kick the
// sampler thread back into life.
//
TR::CompilationInfo::TR_SamplerStates samplerState = compInfo->getSamplerState();
if (samplerState == TR::CompilationInfo::SAMPLER_SUSPENDED) // Nothing to do if sampler is not suspended
{
TR::PersistentInfo *persistentInfo = compInfo->getPersistentInfo();
compInfo->setSamplerState(TR::CompilationInfo::SAMPLER_DEFAULT);
jitConfig->samplingFrequency = TR::Options::getCmdLineOptions()->getSamplingFrequency();
// this wake up call counts as a tick, so mark that we have seen ticks active
// so that we need to wait another 5 seconds to enter idle
persistentInfo->setLastTimeThreadsWereActive(persistentInfo->getElapsedTime());
j9thread_interrupt(jitConfig->samplerThread); // interrupt the sampler to look at its new state
if (TR::Options::getVerboseOption(TR_VerbosePerformance))
{
TR_VerboseLog::writeLineLocked(TR_Vlog_SAMPLING,"t=%u\tSampling thread interrupted and changed frequency to %d ms",
persistentInfo->getElapsedTime(), jitConfig->samplingFrequency);
}
}
// Set the countDelta
J9VMThread * currentThread = vm->mainThread;
do
{
currentThread->jitCountDelta = 2;
} while ((currentThread = currentThread->linkNext) != vm->mainThread);
j9thread_monitor_exit(vm->vmThreadListMutex);
}
} // enableJit
// -----------------------------------------------------------------------------
// Programmatic compile interface
// -----------------------------------------------------------------------------
extern "C" I_32
command(J9VMThread * vmThread, const char * cmdString)
{
TR::CompilationInfo * compInfo = TR::CompilationInfo::get();
if (strncmp(cmdString, "beginningOfStartup", 18) == 0)
{
TR::Options::getCmdLineOptions()->setOption(TR_AssumeStartupPhaseUntilToldNotTo); // let the JIT know it should wait for endOfStartup hint
if (compInfo)
{
TR::PersistentInfo *persistentInfo = compInfo->getPersistentInfo();
// If we trust the application completely, then set CLP right away
// Otherwise, let JIT heuristics decide at next decision point
if (TR::Options::getCmdLineOptions()->getOption(TR_UseStrictStartupHints))
persistentInfo->setClassLoadingPhase(true); // Enter CLP right away
if (TR::Options::isAnyVerboseOptionSet(TR_VerboseCLP, TR_VerbosePerformance))
{
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO,"Compiler.command(beginningOfStartup)");
}
}
return 0;
}
if (strncmp(cmdString, "endOfStartup", 12) == 0)
{
if (TR::Options::getCmdLineOptions()->getOption(TR_AssumeStartupPhaseUntilToldNotTo)) // refuse to do anything if JIT option was not specified
{
if (compInfo)
{
TR::PersistentInfo *persistentInfo = compInfo->getPersistentInfo();
persistentInfo->setExternalStartupEndedSignal(true);
// If we trust the application completely, then set CLP right away
// Otherwise, let JIT heuristics decide at next decision point
if (TR::Options::getCmdLineOptions()->getOption(TR_UseStrictStartupHints))
persistentInfo->setClassLoadingPhase(false);
if (TR::Options::isAnyVerboseOptionSet(TR_VerboseCLP, TR_VerbosePerformance))
{
TR_VerboseLog::writeLineLocked(TR_Vlog_INFO,"Compiler.command(endOfStartup)");
}
}
}
return 0;
}
return 0;
}
static IDATA
internalCompileClass(J9VMThread * vmThread, J9Class * clazz)
{
J9JavaVM * javaVM = vmThread->javaVM;
J9JITConfig * jitConfg = javaVM->jitConfig;
TR::CompilationInfo * compInfo = getCompilationInfo(jitConfig);
TR_J9VMBase *fe = TR_J9VMBase::get(jitConfg, NULL);
// To prevent class unloading we need VM access
bool threadHadNoVMAccess = (!(vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS));
if (threadHadNoVMAccess)
acquireVMAccess(vmThread);
J9Method * newInstanceThunk = getNewInstancePrototype(vmThread);
J9ROMMethod * romMethod = (J9ROMMethod *) J9ROMCLASS_ROMMETHODS(clazz->romClass);
J9Method * ramMethods = (J9Method *) (clazz->ramMethods);
for (uint32_t m = 0; m < clazz->romClass->romMethodCount; m++)
{
J9Method * method = &ramMethods[m];
if (!(romMethod->modifiers & (J9AccNative | J9AccAbstract))
&& method != newInstanceThunk
&& !TR::CompilationInfo::isCompiled(method)
&& !fe->isThunkArchetype(method))
{
bool queued = false;
TR_MethodEvent event;
event._eventType = TR_MethodEvent::InterpreterCounterTripped;
event._j9method = method;
event._oldStartPC = 0;
event._vmThread = vmThread;
event._classNeedingThunk = 0;
bool newPlanCreated;
TR_OptimizationPlan *plan = TR::CompilationController::getCompilationStrategy()->processEvent(&event, &newPlanCreated);
if (plan)
{
plan->setIsExplicitCompilation(true);
// If the controller decides to compile this method, trigger the compilation and wait here
{ // scope for details
TR::IlGeneratorMethodDetails details(method);
compInfo->compileMethod(vmThread, details, 0, TR_no, NULL, &queued, plan);
}
if (!queued && newPlanCreated)
TR_OptimizationPlan::freeOptimizationPlan(plan);
}
else // No memory to create an optimizationPlan
{
// There is no point to try to compile other methods if we cannot
// create an optimization plan. Bail out
break;
}
}
romMethod = nextROMMethod(romMethod);
}
if (threadHadNoVMAccess)
releaseVMAccess(vmThread);
return 1;
}
extern "C" IDATA
compileClass(J9VMThread * vmThread, jclass clazzParm)
{
J9JavaVM * javaVM = vmThread->javaVM;
J9Class * clazz;
IDATA rc;
acquireVMAccess(vmThread);
clazz = J9VM_J9CLASS_FROM_JCLASS(vmThread, clazzParm);
rc = internalCompileClass(vmThread, clazz);
releaseVMAccess(vmThread);
return rc;
}
extern "C" IDATA
compileClasses(J9VMThread * vmThread, const char * pattern)
{
IDATA foundClassToCompile = 0;
J9JavaVM * javaVM = vmThread->javaVM;
J9JITConfig * jitConfig = javaVM->jitConfig;
TR_FrontEnd * vm = TR_J9VMBase::get(jitConfig, vmThread);
TR::CompilationInfo * compInfo = TR::CompilationInfo::get(jitConfig);
if (!compInfo)
return foundClassToCompile;
PORT_ACCESS_FROM_JAVAVM(javaVM);
J9ClassWalkState classWalkState;
#define PATTERN_BUFFER_LENGTH 256
char patternBuffer[PATTERN_BUFFER_LENGTH];
char * patternString = patternBuffer;
int32_t patternLength = strlen(pattern);
bool freePatternString = false;
if (patternLength >= PATTERN_BUFFER_LENGTH)
{
patternString = (char*)j9mem_allocate_memory(patternLength + 1, J9MEM_CATEGORY_JIT);
if (!patternString)
return false;
freePatternString = true;
}
#undef PATTERN_BUFFER_LENGTH
memcpy(patternString, (char *)pattern, patternLength + 1);
/* Slashify the className */
for (int32_t i = 0; i < patternLength; ++i)
if (patternString[i] == '.')
patternString[i] = '/';
bool threadHadNoVMAccess = (!(vmThread->publicFlags & J9_PUBLIC_FLAGS_VM_ACCESS));
if (threadHadNoVMAccess)
acquireVMAccess(vmThread);
J9Class * clazz = javaVM->internalVMFunctions->allLiveClassesStartDo(&classWalkState, javaVM, NULL);
TR_LinkHead0<TR_ClassHolder> *classList = compInfo->getListOfClassesToCompile();
bool classListWasEmpty = classList ? false : true;
bool abortFlag = false;
while (clazz && !abortFlag)
{
if (!J9ROMCLASS_IS_PRIMITIVE_OR_ARRAY(clazz->romClass))
{
J9UTF8 * clazzUTRF8 = J9ROMCLASS_CLASSNAME(clazz->romClass);
// j9tty_printf(PORTLIB, "Class %.*s\n", J9UTF8_LENGTH(clazz), J9UTF8_DATA(clazz));
#define CLASSNAME_BUFFER_LENGTH 1000
char classNameBuffer[CLASSNAME_BUFFER_LENGTH];
char * classNameString = classNameBuffer;
bool freeClassNameString = false;
if (J9UTF8_LENGTH(clazzUTRF8) >= CLASSNAME_BUFFER_LENGTH)
{
classNameString = (char*)j9mem_allocate_memory((J9UTF8_LENGTH(clazzUTRF8)+1)*sizeof(char), J9MEM_CATEGORY_JIT);
if (!classNameString)
{
clazz = javaVM->internalVMFunctions->allLiveClassesNextDo(&classWalkState);
continue;
}
freeClassNameString = true;
}
#undef CLASSNAME_BUFFER_LENGTH
strncpy(classNameString, (char*)J9UTF8_DATA(clazzUTRF8), J9UTF8_LENGTH(clazzUTRF8));
if (strstr(classNameString, patternString))
{
TR_ClassHolder * c = NULL;
if (!classListWasEmpty) // If the list was not empty we must search for duplicates
{
for (c = classList->getFirst(); c; c = c->getNext())