forked from bcosorg/bcos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartVM.cpp
123 lines (103 loc) · 3.26 KB
/
SmartVM.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
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SmartVM.h"
#include <unordered_map>
#include <thread>
#include <libdevcore/concurrent_queue.h>
#include <libdevcore/Guards.h>
#include "VMFactory.h"
#include "JitVM.h"
namespace dev
{
namespace eth
{
namespace
{
struct JitInfo: LogChannel { static const char* name() { return "JIT"; }; static const int verbosity = 11; };
using HitMap = std::unordered_map<h256, uint64_t>;
HitMap& getHitMap()
{
static HitMap s_hitMap;
return s_hitMap;
}
struct JitTask
{
bytes code;
h256 codeHash;
evm_mode mode;
static JitTask createStopSentinel() { return JitTask(); }
bool isStopSentinel()
{
assert((!code.empty() || !codeHash) && "'empty code => empty hash' invariant failed");
return code.empty();
}
};
class JitWorker
{
concurrent_queue<JitTask> m_queue;
std::thread m_worker; // Worker must be last to initialize
void work()
{
clog(JitInfo) << "JIT worker started.";
JitTask task;
while (!(task = m_queue.pop()).isStopSentinel())
{
clog(JitInfo) << "Compilation... " << task.codeHash;
JitVM::compile(task.mode, {task.code.data(), task.code.size()}, task.codeHash);
clog(JitInfo) << " ...finished " << task.codeHash;
}
clog(JitInfo) << "JIT worker finished.";
}
public:
JitWorker() noexcept: m_worker([this]{ work(); })
{}
~JitWorker()
{
push(JitTask::createStopSentinel());
m_worker.join();
}
void push(JitTask&& _task) { m_queue.push(std::move(_task)); }
};
}
bytesConstRef SmartVM::execImpl(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp)
{
auto vmKind = VMKind::Interpreter; // default VM
auto mode = JitVM::scheduleToMode(_ext.evmSchedule());
// Jitted EVM code already in memory?
if (JitVM::isCodeReady(mode, _ext.codeHash))
{
clog(JitInfo) << "JIT: " << _ext.codeHash;
vmKind = VMKind::JIT;
}
else if (!_ext.code.empty()) // This check is needed for VM tests
{
static JitWorker s_worker;
// Check EVM code hit count
static const uint64_t c_hitTreshold = 2;
auto& hits = getHitMap()[_ext.codeHash];
++hits;
if (hits == c_hitTreshold)
{
clog(JitInfo) << "Schedule: " << _ext.codeHash;
s_worker.push({_ext.code, _ext.codeHash, mode});
}
clog(JitInfo) << "Interpreter: " << _ext.codeHash;
}
// TODO: Selected VM must be kept only because it returns reference to its internal memory.
// VM implementations should be stateless, without escaping memory reference.
m_selectedVM = VMFactory::create(vmKind);
return m_selectedVM->execImpl(io_gas, _ext, _onOp);
}
}
}