Browse Source

Additional cache options: readonly, writeonly and clear.

cl-refactor
Paweł Bylica 10 years ago
parent
commit
4b37ed9e16
  1. 23
      evmjit/libevmjit/Cache.cpp
  2. 14
      evmjit/libevmjit/Cache.h
  3. 14
      evmjit/libevmjit/ExecutionEngine.cpp

23
evmjit/libevmjit/Cache.cpp

@ -22,6 +22,7 @@ namespace jit
namespace
{
CacheMode g_mode;
llvm::MemoryBuffer* g_lastObject;
ExecutionEngineListener* g_listener;
static const size_t c_versionStampLength = 32;
@ -38,15 +39,31 @@ namespace
}
}
ObjectCache* Cache::getObjectCache(ExecutionEngineListener* _listener)
ObjectCache* Cache::getObjectCache(CacheMode _mode, ExecutionEngineListener* _listener)
{
static ObjectCache objectCache;
g_mode = _mode;
g_listener = _listener;
return &objectCache;
}
void Cache::clear()
{
using namespace llvm::sys;
llvm::SmallString<256> cachePath;
path::system_temp_directory(false, cachePath);
path::append(cachePath, "evm_objs");
std::error_code err;
for (auto it = fs::directory_iterator{cachePath.str(), err}; it != fs::directory_iterator{}; it.increment(err))
fs::remove(it->path());
}
std::unique_ptr<llvm::Module> Cache::getObject(std::string const& id)
{
if (g_mode != CacheMode::on && g_mode != CacheMode::read)
return nullptr;
if (g_listener)
g_listener->stateChanged(ExecState::CacheLoad);
@ -88,6 +105,10 @@ std::unique_ptr<llvm::Module> Cache::getObject(std::string const& id)
void ObjectCache::notifyObjectCompiled(llvm::Module const* _module, llvm::MemoryBuffer const* _object)
{
// Only in "on" and "write" mode
if (g_mode != CacheMode::on && g_mode != CacheMode::write)
return;
if (g_listener)
g_listener->stateChanged(ExecState::CacheWrite);

14
evmjit/libevmjit/Cache.h

@ -12,6 +12,15 @@ namespace jit
{
class ExecutionEngineListener;
enum class CacheMode
{
on,
off,
read,
write,
clear
};
class ObjectCache : public llvm::ObjectCache
{
public:
@ -29,8 +38,11 @@ public:
class Cache
{
public:
static ObjectCache* getObjectCache(ExecutionEngineListener* _listener);
static ObjectCache* getObjectCache(CacheMode _mode, ExecutionEngineListener* _listener);
static std::unique_ptr<llvm::Module> getObject(std::string const& id);
/// Clears cache storage
static void clear();
};
}

14
evmjit/libevmjit/ExecutionEngine.cpp

@ -67,7 +67,14 @@ void printVersion()
namespace cl = llvm::cl;
cl::opt<bool> g_optimize{"O", cl::desc{"Optimize"}};
cl::opt<bool> g_cache{"cache", cl::desc{"Cache compiled EVM code on disk"}, cl::init(true)};
cl::opt<CacheMode> g_cache{"cache", cl::desc{"Cache compiled EVM code on disk"},
cl::values(
clEnumValN(CacheMode::on, "1", "Enabled"),
clEnumValN(CacheMode::off, "0", "Disabled"),
clEnumValN(CacheMode::read, "r", "Read only. No new objects are added to cache."),
clEnumValN(CacheMode::write, "w", "Write only. No objects are loaded from cache."),
clEnumValN(CacheMode::clear, "c", "Clear the cache storage. Cache is disabled."),
clEnumValEnd)};
cl::opt<bool> g_stats{"st", cl::desc{"Statistics"}};
cl::opt<bool> g_dump{"dump", cl::desc{"Dump LLVM IR module"}};
@ -89,11 +96,14 @@ ReturnCode ExecutionEngine::run(RuntimeData* _data, Env* _env)
std::unique_ptr<ExecStats> listener{new ExecStats};
listener->stateChanged(ExecState::Started);
auto objectCache = g_cache ? Cache::getObjectCache(listener.get()) : nullptr;
auto objectCache = (g_cache != CacheMode::off && g_cache != CacheMode::clear) ? Cache::getObjectCache(g_cache, listener.get()) : nullptr;
static std::unique_ptr<llvm::ExecutionEngine> ee;
if (!ee)
{
if (g_cache == CacheMode::clear)
Cache::clear();
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();

Loading…
Cancel
Save