memory.cpp 7.92 KB
Newer Older
Steffen Vogel's avatar
Steffen Vogel committed
1
2
3
/** Memory managment.
 *
 * @author Daniel Krebs <github@daniel-krebs.net>
Steffen Vogel's avatar
Steffen Vogel committed
4
 * @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
Steffen Vogel's avatar
Steffen Vogel committed
5
6
 * @license GNU General Public License (version 3)
 *
7
 * VILLAScommon
Steffen Vogel's avatar
Steffen Vogel committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 *
 * This program 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
 * any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <sys/mman.h>
#include <unistd.h>

#include <sstream>
#include <fstream>

#include <villas/memory.hpp>

namespace villas {

std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn>
HostRam::HostRamAllocator::allocateBlock(size_t size)
{
	/* Align to next bigger page size chunk */
	if (size &  size_t(0xFFF)) {
		size += size_t(0x1000);
		size &= size_t(~0xFFF);
	}

42
#if defined(__linux__) && !defined(__arm__)
Steffen Vogel's avatar
Steffen Vogel committed
43
44
45
	const int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT;
#else
	const int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS;
46
#endif
Steffen Vogel's avatar
Steffen Vogel committed
47
	const int mmap_protection = PROT_READ | PROT_WRITE;
48

Steffen Vogel's avatar
Steffen Vogel committed
49
50
51
52
53
54
55
	const void* addr = mmap(nullptr, size, mmap_protection, mmap_flags, 0, 0);
	if(addr == nullptr) {
		throw std::bad_alloc();
	}

	auto& mm = MemoryManager::get();

56
	/* Assemble name for this block */
Steffen Vogel's avatar
Steffen Vogel committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	std::stringstream name;
	name << std::showbase << std::hex << reinterpret_cast<uintptr_t>(addr);

	auto blockAddrSpaceId = mm.getProcessAddressSpaceMemoryBlock(name.str());

	const auto localAddr = reinterpret_cast<uintptr_t>(addr);
	std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn>
	        mem(new MemoryBlock(localAddr, size, blockAddrSpaceId), this->free);

	insertMemoryBlock(*mem);

	return mem;
}


LinearAllocator::LinearAllocator(MemoryManager::AddressSpaceId memoryAddrSpaceId,
                                 size_t memorySize,
                                 size_t internalOffset) :
    BaseAllocator(memoryAddrSpaceId),
    nextFreeAddress(0),
    memorySize(memorySize),
    internalOffset(internalOffset),
    allocationCount(0)
{
81
	/* Make sure to start at aligned offset, reduce size in case we need padding */
Steffen Vogel's avatar
Steffen Vogel committed
82
83
84
85
86
87
88
	if(const size_t paddingBytes = getAlignmentPadding(internalOffset)) {
		assert(paddingBytes < memorySize);

		internalOffset += paddingBytes;
		memorySize -= paddingBytes;
	}

89
	/* Deallocation callback */
Steffen Vogel's avatar
Steffen Vogel committed
90
91
92
93
94
95
96
97
98
99
	free = [&](MemoryBlock* mem) {
		logger->debug("Deallocating memory block at local addr {:#x} (addr space {})",
		              mem->getOffset(), mem->getAddrSpaceId());

		removeMemoryBlock(*mem);

		allocationCount--;
		if(allocationCount == 0) {
			logger->debug("All allocations are deallocated now, freeing memory");

100
			/* All allocations have been deallocated, free all memory */
Steffen Vogel's avatar
Steffen Vogel committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
			nextFreeAddress = 0;
		}

		logger->debug("Available memory: {:#x} bytes", getAvailableMemory());
	};
}


std::string
LinearAllocator::getName() const
{
	std::stringstream name;
	name << "LinearAlloc" << getAddrSpaceId();
	if(internalOffset != 0) {
		name << "@0x" << std::hex << internalOffset;
	}

	return name.str();
}


std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn>
LinearAllocator::allocateBlock(size_t size)
{
	if(size > getAvailableMemory()) {
		throw std::bad_alloc();
	}

129
	/* Assign address */
Steffen Vogel's avatar
Steffen Vogel committed
130
131
	const uintptr_t localAddr = nextFreeAddress + internalOffset;

132
	/* Reserve memory */
Steffen Vogel's avatar
Steffen Vogel committed
133
134
	nextFreeAddress += size;

135
	/* Make sure it is aligned */
Steffen Vogel's avatar
Steffen Vogel committed
136
137
138
	if(const size_t paddingBytes = getAlignmentPadding(nextFreeAddress)) {
		nextFreeAddress += paddingBytes;

139
		/* If next free address is outside this block due to padding, cap it */
Steffen Vogel's avatar
Steffen Vogel committed
140
141
142
143
144
145
		nextFreeAddress = std::min(nextFreeAddress, memorySize);
	}


	auto& mm = MemoryManager::get();

146
	/* Assemble name for this block */
Steffen Vogel's avatar
Steffen Vogel committed
147
148
149
	std::stringstream blockName;
	blockName << std::showbase << std::hex << localAddr;

150
	/* Create address space */
Steffen Vogel's avatar
Steffen Vogel committed
151
152
153
154
155
156
157
158
159
	auto addrSpaceName = mm.getSlaveAddrSpaceName(getName(), blockName.str());
	auto addrSpaceId = mm.getOrCreateAddressSpace(addrSpaceName);

	logger->debug("Allocated {:#x} bytes for {}, {:#x} bytes remaining",
	              size, addrSpaceId, getAvailableMemory());

	std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn>
	        mem(new MemoryBlock(localAddr, size, addrSpaceId), this->free);

160
	/* Mount block into the memory graph */
Steffen Vogel's avatar
Steffen Vogel committed
161
162
	insertMemoryBlock(*mem);

163
	/* Increase the allocation count */
Steffen Vogel's avatar
Steffen Vogel committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
	allocationCount++;

	return mem;
}


HostRam::HostRamAllocator
HostRam::allocator;

HostRam::HostRamAllocator::HostRamAllocator() :
    BaseAllocator(MemoryManager::get().getProcessAddressSpace())
{
	free = [&](MemoryBlock* mem) {
		if(::munmap(reinterpret_cast<void*>(mem->getOffset()), mem->getSize()) != 0) {
			logger->warn("munmap() failed for {:#x} of size {:#x}",
			             mem->getOffset(), mem->getSize());
		}

		removeMemoryBlock(*mem);
	};
}


std::map<int, std::unique_ptr<HostDmaRam::HostDmaRamAllocator>>
HostDmaRam::allocators;

HostDmaRam::HostDmaRamAllocator::HostDmaRamAllocator(int num) :
    LinearAllocator(MemoryManager::get().getOrCreateAddressSpace(getUdmaBufName(num)), getUdmaBufBufSize(num)),
    num(num)
{
	auto& mm = MemoryManager::get();
195
	logger = logging.get(getName());
Steffen Vogel's avatar
Steffen Vogel committed
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
223
224
225
226
227
228
229
230
231
232
233
234
235

	if(getSize() == 0) {
		logger->error("Zero-sized DMA buffer not supported, is the kernel module loaded?");
		throw std::bad_alloc();
	}

	const uintptr_t base = getUdmaBufPhysAddr(num);

	mm.createMapping(base, 0, getSize(), getName() + "-PCI",
	                 mm.getPciAddressSpace(), getAddrSpaceId());

	const auto bufPath = std::string("/dev/") + getUdmaBufName(num);
	const int bufFd = open(bufPath.c_str(), O_RDWR | O_SYNC);
	if(bufFd != -1) {
		void* buf = mmap(nullptr, getSize(), PROT_READ|PROT_WRITE, MAP_SHARED, bufFd, 0);
		close(bufFd);

		if(buf != MAP_FAILED) {
			mm.createMapping(reinterpret_cast<uintptr_t>(buf), 0, getSize(),
			                 getName() + "-VA",
			                 mm.getProcessAddressSpace(), getAddrSpaceId());
		} else {
			logger->warn("Cannot map {}", bufPath);
		}
	} else {
		logger->warn("Cannot open {}", bufPath);
	}

	logger->info("Mapped {} of size {} bytes", bufPath, getSize());
}

HostDmaRam::HostDmaRamAllocator::~HostDmaRamAllocator()
{
	auto& mm = MemoryManager::get();

	void* baseVirt;
	try {
		auto translation = mm.getTranslationFromProcess(getAddrSpaceId());
		baseVirt = reinterpret_cast<void*>(translation.getLocalAddr(0));
	} catch(const std::out_of_range&) {
236
		/* Not mapped, nothing to do */
Steffen Vogel's avatar
Steffen Vogel committed
237
238
239
240
241
		return;
	}

	logger->debug("Unmapping {}", getName());

242
	/* Try to unmap it */
Steffen Vogel's avatar
Steffen Vogel committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	if(::munmap(baseVirt, getSize()) != 0) {
		logger->warn("munmap() failed for {:p} of size {:#x}",
		             baseVirt, getSize());
	}
}

std::string
HostDmaRam::getUdmaBufName(int num)
{
	std::stringstream name;
	name << "udmabuf" << num;

	return name.str();
}

std::string
HostDmaRam::getUdmaBufBasePath(int num)
{
	std::stringstream path;
	path << "/sys/class/udmabuf/udmabuf" << num << "/";
	return path.str();
}

size_t
HostDmaRam::getUdmaBufBufSize(int num)
{
	std::fstream s(getUdmaBufBasePath(num) + "size", s.in);
	if(s.is_open()) {
		std::string line;
		if(std::getline(s, line)) {
			return std::strtoul(line.c_str(), nullptr, 10);
		}
	}

	return 0;
}

uintptr_t
HostDmaRam::getUdmaBufPhysAddr(int num)
{
	std::fstream s(getUdmaBufBasePath(num) + "phys_addr", s.in);
	if(s.is_open()) {
		std::string line;
		if(std::getline(s, line)) {
			return std::strtoul(line.c_str(), nullptr, 16);
		}
	}

	return UINTPTR_MAX;
}

HostDmaRam::HostDmaRamAllocator&HostDmaRam::getAllocator(int num)
{
	auto& allocator = allocators[num];
	if(not allocator) {
		allocator = std::make_unique<HostDmaRamAllocator>(num);
	}

	return *allocator;
}

304
} /* namespace villas */