summaryrefslogtreecommitdiff
path: root/src/vm/jithost.cpp
blob: a3190bb6b5f6eb4803fadb2d596009a13068e608 (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
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
// 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.

#include "common.h"

#include "utilcode.h"
#include "corjit.h"
#include "jithost.h"

void* JitHost::allocateMemory(size_t size)
{
    WRAPPER_NO_CONTRACT;

    return ClrAllocInProcessHeap(0, S_SIZE_T(size));
}

void JitHost::freeMemory(void* block)
{
    WRAPPER_NO_CONTRACT;

    ClrFreeInProcessHeap(0, block);
}

int JitHost::getIntConfigValue(const wchar_t* name, int defaultValue)
{
    WRAPPER_NO_CONTRACT;

    // Translate JIT call into runtime configuration query
    CLRConfig::ConfigDWORDInfo info{ name, (DWORD)defaultValue, CLRConfig::EEConfig_default };

    // Perform a CLRConfig look up on behalf of the JIT.
    return CLRConfig::GetConfigValue(info);
}

const wchar_t* JitHost::getStringConfigValue(const wchar_t* name)
{
    WRAPPER_NO_CONTRACT;

    // Translate JIT call into runtime configuration query
    CLRConfig::ConfigStringInfo info{ name, CLRConfig::EEConfig_default };

    // Perform a CLRConfig look up on behalf of the JIT.
    return CLRConfig::GetConfigValue(info);
}

void JitHost::freeStringConfigValue(const wchar_t* value)
{
    WRAPPER_NO_CONTRACT;

    CLRConfig::FreeConfigString(const_cast<wchar_t*>(value));
}

//
// Pool memory blocks for JIT to avoid frequent commit/decommit. The frequent commit/decommit has been 
// shown to slow down the JIT significantly (10% or more). The memory blocks used by the JIT tend to be too big
// to be covered by pooling done by the default malloc.
//
// - Keep up to some limit worth of memory, with loose affinization of memory blocks to threads.
// - On finalizer thread, release the extra memory that was not used recently.
//

void* JitHost::allocateSlab(size_t size, size_t* pActualSize)
{
    size = max(size, sizeof(Slab));

    Thread* pCurrentThread = GetThread();
    if (m_pCurrentCachedList != NULL || m_pPreviousCachedList != NULL)
    {
        CrstHolder lock(&m_jitSlabAllocatorCrst);
        Slab** ppCandidate = NULL;

        for (Slab ** ppList = &m_pCurrentCachedList; *ppList != NULL; ppList = &(*ppList)->pNext)
        {
            Slab* p = *ppList;
            if (p->size >= size && p->size <= 4 * size) // Avoid wasting more than 4x memory
            {
                ppCandidate = ppList;
                if (p->affinity == pCurrentThread)
                    break;
            }
        }

        if (ppCandidate == NULL)
        {
            for (Slab ** ppList = &m_pPreviousCachedList; *ppList != NULL; ppList = &(*ppList)->pNext)
            {
                Slab* p = *ppList;
                if (p->size == size) // Allocation from previous list requires exact match
                {
                    ppCandidate = ppList;
                    if (p->affinity == pCurrentThread)
                        break;
                }
            }
        }

        if (ppCandidate != NULL)
        {
            Slab* p = *ppCandidate;
            *ppCandidate = p->pNext;

            m_totalCached -= p->size;
            *pActualSize = p->size;

            return p;
        }
    }

    *pActualSize = size;
    return ClrAllocInProcessHeap(0, S_SIZE_T(size));
}

void JitHost::freeSlab(void* slab, size_t actualSize)
{
    _ASSERTE(actualSize >= sizeof(Slab));

    if (actualSize < 0x100000) // Do not cache blocks that are more than 1MB
    {
        CrstHolder lock(&m_jitSlabAllocatorCrst);

        if (m_totalCached < g_pConfig->JitHostMaxSlabCache()) // Do not cache more than maximum allowed value
        {
            m_totalCached += actualSize;

            Slab* pSlab = (Slab*)slab;
            pSlab->size = actualSize;
            pSlab->affinity = GetThread();
            pSlab->pNext = m_pCurrentCachedList;
            m_pCurrentCachedList = pSlab;
            return;
        }
    }

    ClrFreeInProcessHeap(0, slab);
}

void JitHost::init()
{
    m_jitSlabAllocatorCrst.Init(CrstLeafLock);
}

void JitHost::reclaim()
{
    if (m_pCurrentCachedList != NULL || m_pPreviousCachedList != NULL)
    {
        DWORD ticks = ::GetTickCount();

        if (m_lastFlush == 0) // Just update m_lastFlush first time around
        {
            m_lastFlush = ticks;
            return;
        }

        if ((DWORD)(ticks - m_lastFlush) < 2000) // Flush the free lists every 2 seconds
            return;
        m_lastFlush = ticks;

        // Flush all slabs in m_pPreviousCachedList
        for (;;)
        {
            Slab* slabToDelete = NULL;

            {
                CrstHolder lock(&m_jitSlabAllocatorCrst);
                slabToDelete = m_pPreviousCachedList;
                if (slabToDelete == NULL)
                {
                    m_pPreviousCachedList = m_pCurrentCachedList;
                    m_pCurrentCachedList = NULL;
                    break;
                }
                m_totalCached -= slabToDelete->size;
                m_pPreviousCachedList = slabToDelete->pNext;
            }

            ClrFreeInProcessHeap(0, slabToDelete);
        }
    }
}

JitHost JitHost::s_theJitHost;