Skip to content

Commit

Permalink
Cache symbol lookups and add some inline documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jan 14, 2024
1 parent 9f8b11d commit c5a399c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
21 changes: 13 additions & 8 deletions engine/src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ using gaddr_t = Script::gaddr_t;
static std::vector<uint8_t> load_file(const std::string& filename);
// Some dynamic calls are currently enabled late in initialization
static constexpr bool WARN_ON_UNIMPLEMENTED_DYNCALL = false;
// the shared area is read-write for the guest
static constexpr size_t STACK_SIZE = 2ULL << 20;
/// @brief The shared memory area is 8KB and read+write
static constexpr gaddr_t SHM_BASE = 0x2000;
static constexpr gaddr_t SHM_SIZE = 2 * riscv::Page::size();
static const int HEAP_SYSCALLS_BASE = 570;
Expand Down Expand Up @@ -109,7 +108,7 @@ bool Script::initialize()
// run through the initialization
try
{
machine().simulate<false>(MAX_INSTR);
machine().simulate<false>(MAX_BOOT_INSTR);

if (UNLIKELY(machine().instruction_limit_reached()))
{
Expand Down Expand Up @@ -147,10 +146,6 @@ void Script::machine_setup()
strf::to(stdout)(std::string_view {p, len});
});
machine().set_debug_printer(machine().get_printer());
machine().set_stdin(
(machine_t::stdin_func)[](const machine_t&, char*, size_t)->long {
return 0;
});
machine().on_unhandled_csr = [](machine_t& machine, int csr, int, int)
{
auto& script = *machine.template get_userdata<Script>();
Expand Down Expand Up @@ -292,7 +287,17 @@ void Script::print(std::string_view text)

gaddr_t Script::address_of(const std::string& name) const
{
return machine().address_of(name.c_str());
// We need to cache lookups because they are fairly expensive
// Dynamic executables usually have a hash lookup table for symbols,
// but no such thing for static executables. So, we compensate by
// storing symbols in a local cache in order to reduce latencies.
auto it = m_lookup_cache.find(name);
if (it != m_lookup_cache.end())
return it->second;

const auto addr = machine().address_of(name.c_str());
m_lookup_cache.try_emplace(name, addr);
return addr;
}

std::string Script::symbol_name(gaddr_t address) const
Expand Down
19 changes: 15 additions & 4 deletions engine/src/script/script.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ struct Script
using machine_t = riscv::Machine<MARCH>;
using ghandler_t = std::function<void(Script&)>;
using exit_func_t = std::function<void(Script&)>;
/// @brief The total physical memory of the program
static constexpr gaddr_t MAX_MEMORY = 1024 * 1024 * 16ull;
/// @brief A virtual memory area set aside for the initial stack
static constexpr gaddr_t STACK_SIZE = 1024 * 1024 * 2ull;
/// @brief A virtual memory area set aside for the heap
static constexpr gaddr_t MAX_HEAP = 1024 * 1024 * 256ull;
static constexpr uint64_t MAX_INSTR = 32'000'000ull;
/// @brief The max number of instructions allowed during startup
static constexpr uint64_t MAX_BOOT_INSTR = 32'000'000ull;
/// @brief The max number of instructions allowed during calls
static constexpr uint64_t MAX_CALL_INSTR = 32'000'000ull;

// Call any script function, with any parameters
template <typename... Args>
Expand Down Expand Up @@ -207,8 +214,12 @@ struct Script
Script* m_remote_script = nullptr;
/// @brief Functions accessible when remote access is *strict*
std::unordered_set<gaddr_t> m_remote_access;
// dynamic call arguments
/// @brief List of arguments added by dynamic arguments feature
std::vector<std::any> m_arguments;
/// @brief Cached addresses for symbol lookups
/// This could probably be improved by doing it per-binary instead
/// of a separate cache per instance. But at least it's thread-safe.
mutable std::unordered_map<std::string, gaddr_t> m_lookup_cache;
// dynamic call array, lazily resolved at run-time
struct DyncallDesc {
uint32_t hash;
Expand Down Expand Up @@ -239,7 +250,7 @@ inline long Script::call(gaddr_t address, Args&&... args)
{
try
{
return machine().vmcall<MAX_INSTR>(
return machine().vmcall<MAX_CALL_INSTR>(
address, std::forward<Args>(args)...);
}
catch (const std::exception& e)
Expand Down Expand Up @@ -267,7 +278,7 @@ inline long Script::preempt(gaddr_t address, Args&&... args)
try
{
return machine().preempt(
MAX_INSTR, address, std::forward<Args>(args)...);
MAX_CALL_INSTR, address, std::forward<Args>(args)...);
}
catch (const std::exception& e)
{
Expand Down

0 comments on commit c5a399c

Please sign in to comment.