1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/common/include/villas/memory_manager.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

260 lines
7.5 KiB
C++
Raw Normal View History

2018-08-21 00:25:44 +02:00
/** Memory manager.
*
* @file
* @author Daniel Krebs <github@daniel-krebs.net>
2022-03-15 09:05:42 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-05-19 17:40:10 +02:00
* @license Apache License 2.0
2018-08-21 00:25:44 +02:00
*********************************************************************************/
#pragma once
#include <cstdint>
#include <string>
#include <map>
#include <stdexcept>
#include <unistd.h>
#include <villas/log.hpp>
#include <villas/graph/directed.hpp>
namespace villas {
/**
* @brief Translation between a local (master) to a foreign (slave) address space
*
* Memory translations can be chained together using the `+=` operator which is
* used internally by the MemoryManager to compute a translation through
* multiple hops (memory mappings).
*/
class MemoryTranslation {
public:
/**
* @brief MemoryTranslation
* @param src Base address of local address space
* @param dst Base address of foreign address space
* @param size Size of "memory window"
*/
MemoryTranslation(uintptr_t src, uintptr_t dst, size_t size) :
src(src),
dst(dst),
size(size)
{ }
2018-08-21 00:25:44 +02:00
uintptr_t getLocalAddr(uintptr_t addrInForeignAddrSpace) const;
2018-08-21 00:25:44 +02:00
uintptr_t getForeignAddr(uintptr_t addrInLocalAddrSpace) const;
2018-08-21 00:25:44 +02:00
size_t getSize() const
{
return size;
}
2018-08-21 00:25:44 +02:00
friend
std::ostream& operator<< (std::ostream &stream, const MemoryTranslation &translation)
2018-08-21 00:25:44 +02:00
{
return stream << std::hex
<< "(src=0x" << translation.src
<< ", dst=0x" << translation.dst
<< ", size=0x" << translation.size
<< ")";
}
// Merge two MemoryTranslations together
2020-06-14 21:53:18 +02:00
MemoryTranslation &operator+=(const MemoryTranslation &other);
2018-08-21 00:25:44 +02:00
private:
uintptr_t src; // Base address of local address space
uintptr_t dst; // Base address of foreign address space
size_t size; // Size of "memory window"
2018-08-21 00:25:44 +02:00
};
/**
* @brief Global memory manager to resolve addresses across address spaces
*
* Every entity in the system has to register its (master) address space and
* create mappings to other (slave) address spaces that it can access. A
* directed graph is then constructed which allows to traverse addresses spaces
* through multiple mappings and resolve addresses through this "tunnel" of
* memory mappings.
*/
class MemoryManager {
private:
// This is a singleton, so private constructor ...
MemoryManager() :
memoryGraph("memory:graph"),
logger(logging.get("memory:manager"))
2018-08-21 00:25:44 +02:00
{
2020-06-14 21:53:18 +02:00
pathCheckFunc = [&](const MemoryGraph::Path &path) {
2018-08-21 00:25:44 +02:00
return this->pathCheck(path);
};
}
// ... and no copying or assigning
MemoryManager(const MemoryManager&) = delete;
2020-06-14 21:53:18 +02:00
MemoryManager &operator=(const MemoryManager&) = delete;
2018-08-21 00:25:44 +02:00
/**
* @brief Custom edge in memory graph representing a memory mapping
*
* A memory mapping maps from one address space into another and can only be
* traversed in the forward direction which reflects the nature of real
* memory mappings.
*
* Implementation Notes:
* The member #src is the address in the "from" address space, where the
* destination address space is mapped. The member #dest is the address in
* the destination address space, where the mapping points to. Often, #dest
* will be zero for mappings to hardware, but consider the example when
* mapping FPGA to application memory:
* The application allocates a block 1kB at address 0x843001000 in its
* address space. The mapping would then have a #dest address of 0x843001000
* and a #size of 1024.
*/
class Mapping : public graph::Edge {
public:
std::string name; // Human-readable name
uintptr_t src; // Base address in "from" address space
uintptr_t dest; // Base address in "to" address space
size_t size; // Size of the mapping
2018-08-21 00:25:44 +02:00
friend std::ostream&
2020-06-14 21:53:18 +02:00
operator<< (std::ostream &stream, const Mapping &mapping)
2018-08-21 00:25:44 +02:00
{
return stream << static_cast<const Edge&>(mapping) << " = "
<< mapping.name
<< std::hex
<< " (src=0x" << mapping.src
<< ", dest=0x" << mapping.dest
<< ", size=0x" << mapping.size
<< ")";
}
};
/**
* @brief Custom vertex in memory graph representing an address space
*
* Since most information in the memory graph is stored in the edges (memory
* mappings), this is just a small extension to the default vertex. It only
* associates an additional string #name for human-readability.
*/
class AddressSpace : public graph::Vertex {
public:
std::string name; // Human-readable name
2018-08-21 00:25:44 +02:00
friend std::ostream&
2020-06-14 21:53:18 +02:00
operator<< (std::ostream &stream, const AddressSpace &addrSpace)
2018-08-21 00:25:44 +02:00
{
return stream << static_cast<const Vertex&>(addrSpace) << " = "
<< addrSpace.name;
}
};
// Memory graph with custom edges and vertices for address resolution
2018-08-21 00:25:44 +02:00
using MemoryGraph = graph::DirectedGraph<AddressSpace, Mapping>;
public:
using AddressSpaceId = MemoryGraph::VertexIdentifier;
using MappingId = MemoryGraph::EdgeIdentifier;
struct InvalidTranslation : public std::exception {};
// Get singleton instance
2018-08-21 00:25:44 +02:00
static MemoryManager&
get();
2020-06-11 23:32:46 +02:00
MemoryGraph & getGraph()
{
return memoryGraph;
}
2019-08-15 13:34:49 +02:00
AddressSpaceId getProcessAddressSpace()
{
return getOrCreateAddressSpace("process");
}
2018-08-21 00:25:44 +02:00
AddressSpaceId getPciAddressSpace()
{
return getOrCreateAddressSpace("pcie");
}
2018-08-21 00:25:44 +02:00
AddressSpaceId getProcessAddressSpaceMemoryBlock(const std::string &memoryBlock)
{
return getOrCreateAddressSpace(getSlaveAddrSpaceName("process", memoryBlock));
}
2018-08-21 00:25:44 +02:00
AddressSpaceId getOrCreateAddressSpace(std::string name);
2018-08-21 00:25:44 +02:00
void removeAddressSpace(const AddressSpaceId &addrSpaceId)
{
memoryGraph.removeVertex(addrSpaceId);
}
2018-08-21 00:25:44 +02:00
// Create a default mapping
MappingId createMapping(uintptr_t src, uintptr_t dest, size_t size,
2020-06-14 21:53:18 +02:00
const std::string &name,
2018-08-21 00:25:44 +02:00
AddressSpaceId fromAddrSpace,
AddressSpaceId toAddrSpace);
// Add a mapping
//
// Can be used to derive from Mapping in order to implement custom
// constructor/destructor.
MappingId addMapping(std::shared_ptr<Mapping> mapping,
2018-08-21 00:25:44 +02:00
AddressSpaceId fromAddrSpace,
AddressSpaceId toAddrSpace);
AddressSpaceId findAddressSpace(const std::string &name);
2018-08-21 00:25:44 +02:00
std::list<AddressSpaceId> findPath(const AddressSpaceId &fromAddrSpaceId, const AddressSpaceId &toAddrSpaceId);
2018-08-21 00:25:44 +02:00
MemoryTranslation getTranslation(const AddressSpaceId &fromAddrSpaceId, const AddressSpaceId &toAddrSpaceId);
2018-08-21 00:25:44 +02:00
2021-09-19 19:16:32 +02:00
// cppcheck-suppress passedByValue
MemoryTranslation getTranslationFromProcess(AddressSpaceId foreignAddrSpaceId)
{
return getTranslation(getProcessAddressSpace(), foreignAddrSpaceId);
}
2018-08-21 00:25:44 +02:00
static
std::string getSlaveAddrSpaceName(const std::string &ipInstance, const std::string &memoryBlock)
{
return ipInstance + "/" + memoryBlock;
}
2018-08-21 00:25:44 +02:00
static
std::string getMasterAddrSpaceName(const std::string &ipInstance, const std::string &busInterface)
{
return ipInstance + ":" + busInterface;
}
2018-08-21 00:25:44 +02:00
private:
// Convert a Mapping to MemoryTranslation for calculations
static
MemoryTranslation getTranslationFromMapping(const Mapping &mapping)
{
return MemoryTranslation(mapping.src, mapping.dest, mapping.size);
}
2018-08-21 00:25:44 +02:00
bool pathCheck(const MemoryGraph::Path &path);
2018-08-21 00:25:44 +02:00
// Directed graph that stores address spaces and memory mappings
2018-08-21 00:25:44 +02:00
MemoryGraph memoryGraph;
// Cache mapping of names to address space ids for fast lookup
2018-08-21 00:25:44 +02:00
std::map<std::string, AddressSpaceId> addrSpaceLookup;
// Logger for universal access in this class
2018-10-19 14:33:10 +02:00
Logger logger;
2018-08-21 00:25:44 +02:00
MemoryGraph::check_path_fn pathCheckFunc;
// Static pointer to global instance, because this is a singleton
2018-08-21 00:25:44 +02:00
static MemoryManager* instance;
};
} // namespace villas