memory_manager.cpp 6.57 KB
Newer Older
1
#include <memory>
2
3
#include <limits>
#include <cstdint>
4

5
#include <villas/utils.hpp>
6
#include <villas/memory_manager.hpp>
7

8
9
using namespace villas::utils;

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace villas {

MemoryManager*
MemoryManager::instance = nullptr;

MemoryManager&
MemoryManager::get()
{
	if(instance == nullptr) {
		instance = new MemoryManager;
	}

	return *instance;
}

25
MemoryManager::AddressSpaceId
26
MemoryManager::getOrCreateAddressSpace(std::string name)
27
{
28
29
30
31
32
33
34
	try {
		// try fast lookup
		return addrSpaceLookup.at(name);
	} catch (const std::out_of_range&) {
		// does not yet exist, create
		std::shared_ptr<AddressSpace> addrSpace(new AddressSpace);
		addrSpace->name = name;
35

36
37
38
39
40
		// cache it for the next access
		addrSpaceLookup[name] = memoryGraph.addVertex(addrSpace);

		return addrSpaceLookup[name];
	}
41
42
43
44
}

MemoryManager::MappingId
MemoryManager::createMapping(uintptr_t src, uintptr_t dest, size_t size,
45
                             const std::string& name,
46
47
48
49
                             MemoryManager::AddressSpaceId fromAddrSpace,
                             MemoryManager::AddressSpaceId toAddrSpace)
{
	std::shared_ptr<Mapping> mapping(new Mapping);
50
51

	mapping->name = name;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
	mapping->src = src;
	mapping->dest = dest;
	mapping->size = size;

	return addMapping(mapping, fromAddrSpace, toAddrSpace);
}

MemoryManager::MappingId
MemoryManager::addMapping(std::shared_ptr<Mapping> mapping,
                          MemoryManager::AddressSpaceId fromAddrSpace,
                          MemoryManager::AddressSpaceId toAddrSpace)
{
	return memoryGraph.addEdge(mapping, fromAddrSpace, toAddrSpace);
}

67
MemoryManager::AddressSpaceId
68
MemoryManager::findAddressSpace(const std::string& name)
69
70
71
72
73
74
75
{
	return memoryGraph.findVertex(
	            [&](const std::shared_ptr<AddressSpace>& v) {
		return v->name == name;
	});
}

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
std::list<MemoryManager::AddressSpaceId>
MemoryManager::findPath(MemoryManager::AddressSpaceId fromAddrSpaceId,
                        MemoryManager::AddressSpaceId toAddrSpaceId)
{
	std::list<AddressSpaceId> path;

	auto fromAddrSpace = memoryGraph.getVertex(fromAddrSpaceId);
	auto toAddrSpace = memoryGraph.getVertex(toAddrSpaceId);

	// find a path through the memory graph
	MemoryGraph::Path pathGraph;
	if(not memoryGraph.getPath(fromAddrSpaceId, toAddrSpaceId, pathGraph, pathCheckFunc)) {

		logger->debug("No translation found from ({}) to ({})",
		              *fromAddrSpace, *toAddrSpace);

		throw std::out_of_range("no translation found");
	}

	for(auto& mappingId : pathGraph) {
		auto mapping = memoryGraph.getEdge(mappingId);
		path.push_back(mapping->getVertexTo());
	}

	return path;
}

103
104
105
106
107
108
MemoryTranslation
MemoryManager::getTranslation(MemoryManager::AddressSpaceId fromAddrSpaceId,
                              MemoryManager::AddressSpaceId toAddrSpaceId)
{
	// find a path through the memory graph
	MemoryGraph::Path path;
109
110
	if(not memoryGraph.getPath(fromAddrSpaceId, toAddrSpaceId, path, pathCheckFunc)) {

111
112
		auto fromAddrSpace = memoryGraph.getVertex(fromAddrSpaceId);
		auto toAddrSpace = memoryGraph.getVertex(toAddrSpaceId);
113
		logger->debug("No translation found from ({}) to ({})",
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		              *fromAddrSpace, *toAddrSpace);

		throw std::out_of_range("no translation found");
	}

	// start with an identity mapping
	MemoryTranslation translation(0, 0, SIZE_MAX);

	// iterate through path and merge all mappings into a single translation
	for(auto& mappingId : path) {
		auto mapping = memoryGraph.getEdge(mappingId);
		translation += getTranslationFromMapping(*mapping);
	}

	return translation;
}
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
bool
MemoryManager::pathCheck(const MemoryGraph::Path& path)
{
	// start with an identity mapping
	MemoryTranslation translation(0, 0, SIZE_MAX);

	// Try to add all mappings together to a common translation. If this fails
	// there is a non-overlapping window
	for(auto& mappingId : path) {
		auto mapping = memoryGraph.getEdge(mappingId);
		try {
			translation += getTranslationFromMapping(*mapping);
		} catch(const InvalidTranslation&) {
			return false;
		}
	}

	return true;
}

151

152
153
154
155
156
157
uintptr_t
MemoryTranslation::getForeignAddr(uintptr_t addrInLocalAddrSpace) const
{
	assert(addrInLocalAddrSpace >= src);
	assert(addrInLocalAddrSpace < (src + size));
	return dst + addrInLocalAddrSpace - src;
158
159
}

160
161
162
163
164
165
166
167
168
169
MemoryTranslation&
MemoryTranslation::operator+=(const MemoryTranslation& other)
{
	auto logger = loggerGetOrCreate("MemoryTranslation");
	// set level to debug to enable debug output
	logger->set_level(spdlog::level::info);

	const uintptr_t this_dst_high = this->dst + this->size;
	const uintptr_t other_src_high = other.src + other.size;

170
171
172
173
174
175
176
177
178
	logger->debug("this->src:      {:#x}", this->src);
	logger->debug("this->dst:      {:#x}", this->dst);
	logger->debug("this->size:     {:#x}", this->size);
	logger->debug("other.src:      {:#x}", other.src);
	logger->debug("other.dst:      {:#x}", other.dst);
	logger->debug("other.size:     {:#x}", other.size);
	logger->debug("this_dst_high:  {:#x}", this_dst_high);
	logger->debug("other_src_high: {:#x}", other_src_high);

179
	// make sure there is a common memory area
180
181
	assertExcept(other.src < this_dst_high, MemoryManager::InvalidTranslation());
	assertExcept(this->dst < other_src_high, MemoryManager::InvalidTranslation());
182
183
184
185
186
187
188
189

	const uintptr_t hi = std::max(this_dst_high, other_src_high);
	const uintptr_t lo = std::min(this->dst, other.src);

	const uintptr_t diff_hi = (this_dst_high > other_src_high)
	                          ? (this_dst_high - other_src_high)
	                          : (other_src_high - this_dst_high);

190
191
	const bool otherSrcIsSmaller = this->dst > other.src;
	const uintptr_t diff_lo = (otherSrcIsSmaller)
192
193
194
	                          ? (this->dst - other.src)
	                          : (other.src - this->dst);

195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	logger->debug("hi:             {:#x}", hi);
	logger->debug("lo:             {:#x}", lo);
	logger->debug("diff_hi:        {:#x}", diff_hi);
	logger->debug("diff_lo:        {:#x}", diff_lo);

	// new size of aperture, can only stay or shrink
	this->size = (hi - lo) - diff_hi - diff_lo;

	// new translation will come out other's destination (by default)
	this->dst = other.dst;

	// the source stays the same and can only increase with merged translations
	this->src = this->src;

	if(otherSrcIsSmaller) {
		// other mapping starts at lower addresses, so we actually arrive at
		// higher addresses
		this->dst += diff_lo;
	} else {
		// other mapping starts at higher addresses than this, so we have to
		// increase the start
		// NOTE: for addresses equality, this just adds 0
		this->src += diff_lo;
	}

	logger->debug("result src:     {:#x}", this->src);
	logger->debug("result dst:     {:#x}", this->dst);
	logger->debug("result size:    {:#x}", this->size);
223
224
225

	return *this;
}
226
227

} // namespace villas