1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// ===========================================================================
5// File: TieredCompilation.h
6//
7// ===========================================================================
8
9
10#ifndef TIERED_COMPILATION_H
11#define TIERED_COMPILATION_H
12
13// TieredCompilationManager determines which methods should be recompiled and
14// how they should be recompiled to best optimize the running code. It then
15// handles logistics of getting new code created and installed.
16class TieredCompilationManager
17{
18#ifdef FEATURE_TIERED_COMPILATION
19
20public:
21#if defined(DACCESS_COMPILE) || defined(CROSSGEN_COMPILE)
22 TieredCompilationManager() {}
23#else
24 TieredCompilationManager();
25#endif
26
27 void Init(ADID appDomainId);
28
29#endif // FEATURE_TIERED_COMPILATION
30
31public:
32 static NativeCodeVersion::OptimizationTier GetInitialOptimizationTier(PTR_MethodDesc pMethodDesc);
33
34#ifdef FEATURE_TIERED_COMPILATION
35
36public:
37 static bool RequiresCallCounting(MethodDesc* pMethodDesc);
38 void OnMethodCalled(MethodDesc* pMethodDesc, DWORD currentCallCount, BOOL* shouldStopCountingCallsRef, BOOL* wasPromotedToTier1Ref);
39 void OnMethodCallCountingStoppedWithoutTier1Promotion(MethodDesc* pMethodDesc);
40 void AsyncPromoteMethodToTier1(MethodDesc* pMethodDesc);
41 void Shutdown();
42 static CORJIT_FLAGS GetJitFlags(NativeCodeVersion nativeCodeVersion);
43
44private:
45 bool IsTieringDelayActive();
46 bool TryInitiateTieringDelay();
47 static void WINAPI TieringDelayTimerCallback(PVOID parameter, BOOLEAN timerFired);
48 static void TieringDelayTimerCallbackInAppDomain(LPVOID parameter);
49 void TieringDelayTimerCallbackWorker();
50 static void ResumeCountingCalls(MethodDesc* pMethodDesc);
51
52 bool TryAsyncOptimizeMethods();
53 static DWORD StaticOptimizeMethodsCallback(void* args);
54 void OptimizeMethodsCallback();
55 void OptimizeMethods();
56 void OptimizeMethod(NativeCodeVersion nativeCodeVersion);
57 NativeCodeVersion GetNextMethodToOptimize();
58 BOOL CompileCodeVersion(NativeCodeVersion nativeCodeVersion);
59 void ActivateCodeVersion(NativeCodeVersion nativeCodeVersion);
60
61 bool IncrementWorkerThreadCountIfNeeded();
62 void DecrementWorkerThreadCount();
63#ifdef _DEBUG
64 DWORD DebugGetWorkerThreadCount();
65#endif
66
67 Crst m_lock;
68 SList<SListElem<NativeCodeVersion>> m_methodsToOptimize;
69 ADID m_domainId;
70 BOOL m_isAppDomainShuttingDown;
71 DWORD m_countOptimizationThreadsRunning;
72 DWORD m_callCountOptimizationThreshhold;
73 DWORD m_optimizationQuantumMs;
74 SArray<MethodDesc*>* m_methodsPendingCountingForTier1;
75 HANDLE m_tieringDelayTimerHandle;
76 bool m_tier1CallCountingCandidateMethodRecentlyRecorded;
77
78 CLREvent m_asyncWorkDoneEvent;
79
80#endif // FEATURE_TIERED_COMPILATION
81};
82
83#endif // TIERED_COMPILATION_H
84