summaryrefslogtreecommitdiff
path: root/src/vm/tieredcompilation.h
blob: 2665ad4abfa285283b43c5e2fc39f145b5aaf3f9 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ===========================================================================
// File: TieredCompilation.h
//
// ===========================================================================


#ifndef TIERED_COMPILATION_H
#define TIERED_COMPILATION_H

#ifdef FEATURE_TIERED_COMPILATION

// TieredCompilationManager determines which methods should be recompiled and
// how they should be recompiled to best optimize the running code. It then
// handles logistics of getting new code created and installed.
class TieredCompilationManager
{
public:
#if defined(DACCESS_COMPILE) || defined(CROSSGEN_COMPILE)
    TieredCompilationManager() {}
#else
    TieredCompilationManager();
#endif

    void Init(ADID appDomainId);

    void InitiateTier1CountingDelay();
    void OnTier0JitInvoked();

    void OnMethodCalled(MethodDesc* pMethodDesc, DWORD currentCallCount, BOOL* shouldStopCountingCallsRef, BOOL* wasPromotedToTier1Ref);
    void OnMethodCallCountingStoppedWithoutTier1Promotion(MethodDesc* pMethodDesc);
    void AsyncPromoteMethodToTier1(MethodDesc* pMethodDesc);
    void Shutdown();
    static CORJIT_FLAGS GetJitFlags(NativeCodeVersion nativeCodeVersion);

private:

    static VOID WINAPI Tier1DelayTimerCallback(PVOID parameter, BOOLEAN timerFired);
    static void Tier1DelayTimerCallbackInAppDomain(LPVOID parameter);
    void Tier1DelayTimerCallbackWorker();
    static void ResumeCountingCalls(MethodDesc* pMethodDesc);

    static DWORD StaticOptimizeMethodsCallback(void* args);
    void OptimizeMethodsCallback();
    void OptimizeMethod(NativeCodeVersion nativeCodeVersion);
    NativeCodeVersion GetNextMethodToOptimize();
    BOOL CompileCodeVersion(NativeCodeVersion nativeCodeVersion);
    void ActivateCodeVersion(NativeCodeVersion nativeCodeVersion);

    void IncrementWorkerThreadCount();
    void DecrementWorkerThreadCount();

    SpinLock m_lock;
    SList<SListElem<NativeCodeVersion>> m_methodsToOptimize;
    ADID m_domainId;
    BOOL m_isAppDomainShuttingDown;
    DWORD m_countOptimizationThreadsRunning;
    DWORD m_callCountOptimizationThreshhold;
    DWORD m_optimizationQuantumMs;

    SpinLock m_tier1CountingDelayLock;
    SArray<MethodDesc*>* m_methodsPendingCountingForTier1;
    HANDLE m_tier1CountingDelayTimerHandle;
    bool m_wasTier0JitInvokedSinceCountingDelayReset;

    CLREvent m_asyncWorkDoneEvent;
};

#endif // FEATURE_TIERED_COMPILATION

#endif // TIERED_COMPILATION_H