Fabcoin Core  0.16.2
P2P Digital Currency
SmartVM.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include "SmartVM.h"
19 #include <unordered_map>
20 #include <thread>
22 #include <libdevcore/Log.h>
23 #include <libdevcore/Guards.h>
24 #include "VMFactory.h"
25 #include "JitVM.h"
26 
27 namespace dev
28 {
29 namespace eth
30 {
31 namespace
32 {
33  struct JitInfo: LogChannel { static const char* name() { return "JIT"; }; static const int verbosity = 11; };
34 
35  using HitMap = std::unordered_map<h256, uint64_t>;
36 
37  HitMap& getHitMap()
38  {
39  static HitMap s_hitMap;
40  return s_hitMap;
41  }
42 
43  struct JitTask
44  {
48 
49  static JitTask createStopSentinel() { return JitTask(); }
50 
51  bool isStopSentinel()
52  {
53  assert((!code.empty() || !codeHash) && "'empty code => empty hash' invariant failed");
54  return code.empty();
55  }
56  };
57 
58  class JitWorker
59  {
60  concurrent_queue<JitTask> m_queue;
61  std::thread m_worker; // Worker must be last to initialize
62 
63  void work()
64  {
65  clog(JitInfo) << "JIT worker started.";
66  JitTask task;
67  while (!(task = m_queue.pop()).isStopSentinel())
68  {
69  clog(JitInfo) << "Compilation... " << task.codeHash;
70  JitVM::compile(task.mode, {task.code.data(), task.code.size()}, task.codeHash);
71  clog(JitInfo) << " ...finished " << task.codeHash;
72  }
73  clog(JitInfo) << "JIT worker finished.";
74  }
75 
76  public:
77  JitWorker() noexcept: m_worker([this]{ work(); })
78  {}
79 
80  ~JitWorker()
81  {
82  push(JitTask::createStopSentinel());
83  m_worker.join();
84  }
85 
86  void push(JitTask&& _task) { m_queue.push(std::move(_task)); }
87  };
88 }
89 
90 owning_bytes_ref SmartVM::exec(u256& io_gas, ExtVMFace& _ext, OnOpFunc const& _onOp)
91 {
92  auto vmKind = VMKind::Interpreter; // default VM
93  auto mode = JitVM::scheduleToMode(_ext.evmSchedule());
94  // Jitted EVM code already in memory?
95  if (JitVM::isCodeReady(mode, _ext.codeHash))
96  {
97  clog(JitInfo) << "JIT: " << _ext.codeHash;
98  vmKind = VMKind::JIT;
99  }
100  else if (!_ext.code.empty()) // This check is needed for VM tests
101  {
102  static JitWorker s_worker;
103 
104  // Check EVM code hit count
105  static const uint64_t c_hitTreshold = 2;
106  auto& hits = getHitMap()[_ext.codeHash];
107  ++hits;
108  if (hits == c_hitTreshold)
109  {
110  clog(JitInfo) << "Schedule: " << _ext.codeHash;
111  s_worker.push({_ext.code, _ext.codeHash, mode});
112  }
113  clog(JitInfo) << "Interpreter: " << _ext.codeHash;
114  }
115 
116  return VMFactory::create(vmKind)->exec(io_gas, _ext, _onOp);
117 }
118 
119 }
120 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
evm_mode mode
Definition: SmartVM.cpp:47
assert(len-trim+(2 *lenIndices)<=WIDTH)
static void compile(evm_mode _mode, bytesConstRef _code, h256 _codeHash)
Definition: JitVM.cpp:353
static std::unique_ptr< VMFace > create()
Creates a VM instance of global kind (controlled by setKind() function).
Definition: VMFactory.cpp:41
h256 codeHash
SHA3 hash of the executing code.
Definition: ExtVMFace.h:330
bytes code
Definition: SmartVM.cpp:45
concurrent_queue< JitTask > m_queue
Definition: SmartVM.cpp:60
owning_bytes_ref exec(u256 &io_gas, ExtVMFace &_ext, OnOpFunc const &_onOp) overridefinal
VM implementation.
Definition: SmartVM.cpp:90
const char * name
Definition: rest.cpp:36
std::vector< byte > bytes
Definition: Common.h:75
std::function< void(uint64_t, uint64_t, Instruction, bigint, bigint, bigint, VM *, ExtVMFace const *)> OnOpFunc
Definition: ExtVMFace.h:193
FixedHash< 32 > h256
Definition: FixedHash.h:340
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
evm_mode
EVM compatibility mode aka chain mode.
Definition: evm.h:359
Interface and null implementation of the class for specifying VM externalities.
Definition: ExtVMFace.h:265
h256 codeHash
Definition: SmartVM.cpp:46
bytes code
Current code that is executing.
Definition: ExtVMFace.h:329
static bool isCodeReady(evm_mode _mode, h256 _codeHash)
Definition: JitVM.cpp:348
Reference to a slice of buffer that also owns the buffer.
Definition: ExtVMFace.h:56
virtual EVMSchedule const & evmSchedule() const
Return the EVM gas-price schedule for this execution context.
Definition: ExtVMFace.h:316
std::thread m_worker
Definition: SmartVM.cpp:61
#define clog(X)
Definition: Log.h:295
static evm_mode scheduleToMode(EVMSchedule const &_schedule)
Definition: JitVM.cpp:339