Browse Source

SmartVM: initial implementation

cl-refactor
Paweł Bylica 10 years ago
parent
commit
346770def9
  1. 79
      libevm/SmartVM.cpp
  2. 43
      libevm/SmartVM.h
  3. 12
      libevm/VMFactory.cpp
  4. 3
      libevm/VMFactory.h
  5. 7
      test/TestHelper.cpp
  6. 1
      test/TestHelper.h

79
libevm/SmartVM.cpp

@ -0,0 +1,79 @@
/*
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/>.
*/
// SmartVM is only available if EVM JIT is enabled
#if ETH_EVMJIT
#include "SmartVM.h"
#include <unordered_map>
#include <libdevcore/Log.h>
#include <libdevcrypto/SHA3.h>
#include <evmjit/JIT.h>
#include <evmjit/libevmjit-cpp/Utils.h>
#include "VMFactory.h"
namespace dev
{
namespace eth
{
namespace
{
using HitMap = std::unordered_map<h256, uint64_t>;
HitMap& getHitMap()
{
static HitMap s_hitMap;
return s_hitMap;
}
}
bytesConstRef SmartVM::go(ExtVMFace& _ext, OnOpFunc const& _onOp, uint64_t _steps)
{
auto codeHash = sha3(_ext.code);
auto vmKind = VMKind::Interpreter; // default VM
// Jitted EVM code already in memory?
if (evmjit::JIT::isCodeReady(eth2llvm(codeHash)))
{
cnote << "Jitted";
vmKind = VMKind::JIT;
}
else
{
// Check EVM code hit count
static const uint64_t c_hitTreshold = 1;
auto& hits = getHitMap()[codeHash];
++hits;
if (hits > c_hitTreshold)
{
cnote << "JIT selected";
vmKind = VMKind::JIT;
}
}
// TODO: Selected VM must be kept only because it returns reference to its internal memory.
// VM implementations should be stateless, without gas counter and escaping memory reference.
m_selectedVM = VMFactory::create(vmKind, gas());
auto out = m_selectedVM->go(_ext, _onOp, _steps);
m_gas = m_selectedVM->gas();
return out;
}
}
}
#endif

43
libevm/SmartVM.h

@ -0,0 +1,43 @@
/*
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/>.
*/
#pragma once
#include "VMFace.h"
namespace dev
{
namespace eth
{
/// Smart VM proxy.
///
/// This class is a strategy pattern implementation for VM. For every EVM code
/// execution request it tries to select the best VM implementation (Interpreter or JIT)
/// by analyzing available information like: code size, hit count, JIT status, etc.
class SmartVM: public VMFace
{
public:
SmartVM(u256 _gas): VMFace(_gas) {}
virtual bytesConstRef go(ExtVMFace& _ext, OnOpFunc const& _onOp = {}, uint64_t _steps = (uint64_t)-1) override final;
private:
std::unique_ptr<VMFace> m_selectedVM;
};
}
}

12
libevm/VMFactory.cpp

@ -18,6 +18,7 @@
#include "VMFactory.h"
#include <libdevcore/Assertions.h>
#include "VM.h"
#include "SmartVM.h"
#if ETH_EVMJIT
#include <evmjit/libevmjit-cpp/JitVM.h>
@ -45,7 +46,16 @@ std::unique_ptr<VMFace> VMFactory::create(u256 _gas)
std::unique_ptr<VMFace> VMFactory::create(VMKind _kind, u256 _gas)
{
#if ETH_EVMJIT
return std::unique_ptr<VMFace>(_kind == VMKind::JIT ? static_cast<VMFace*>(new JitVM(_gas)) : static_cast<VMFace*>(new VM(_gas)));
switch (_kind)
{
default:
case VMKind::Interpreter:
return std::unique_ptr<VMFace>(new VM(_gas));
case VMKind::JIT:
return std::unique_ptr<VMFace>(new JitVM(_gas));
case VMKind::Smart:
return std::unique_ptr<VMFace>(new SmartVM(_gas));
}
#else
asserts(_kind == VMKind::Interpreter && "JIT disabled in build configuration");
return std::unique_ptr<VMFace>(new VM(_gas));

3
libevm/VMFactory.h

@ -23,10 +23,11 @@ namespace dev
namespace eth
{
enum class VMKind: bool
enum class VMKind
{
Interpreter,
JIT,
Smart,
};
class VMFactory

7
test/TestHelper.cpp

@ -288,7 +288,7 @@ void ImportTest::checkExpectedState(State const& _stateExpect, State const& _sta
{
addressOptions = _expectedStateOptions.at(a.first);
}
catch(std::out_of_range)
catch(std::out_of_range const&)
{
BOOST_ERROR("expectedStateOptions map does not match expectedState in checkExpectedState!");
break;
@ -707,10 +707,9 @@ Options::Options()
{
auto arg = std::string{argv[i]};
if (arg == "--jit")
{
jit = true;
eth::VMFactory::setKind(eth::VMKind::JIT);
}
else if (arg == "--vm=smart")
eth::VMFactory::setKind(eth::VMKind::Smart);
else if (arg == "--vmtrace")
vmtrace = true;
else if (arg == "--filltests")

1
test/TestHelper.h

@ -179,7 +179,6 @@ void checkAddresses(mapType& _expectedAddrs, mapType& _resultAddrs)
class Options
{
public:
bool jit = false; ///< Use JIT
bool vmtrace = false; ///< Create EVM execution tracer // TODO: Link with log verbosity?
bool fillTests = false; ///< Create JSON test files from execution results
bool stats = false; ///< Execution time stats

Loading…
Cancel
Save