2026-04-21 23:30:35 +02:00
|
|
|
|
/*
|
|
|
|
|
|
LocalBufferStore(Test) - verify thread-local buffer memory allocator proxy
|
|
|
|
|
|
|
|
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2026, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
|
|
**Lumiera** 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 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
|
|
|
|
|
|
|
|
|
|
|
* *****************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/** @file local-buffer-store-test.cpp
|
|
|
|
|
|
** unit test \ref LocalBufferStore_test
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "test/run.hpp"
|
|
|
|
|
|
#include "test/test-helper.hpp"
|
|
|
|
|
|
//#include "vault/mem/engine-buffer-metadata.hpp"
|
|
|
|
|
|
#include "vault/mem/local-buffer-store.hpp"
|
2026-05-16 23:06:30 +02:00
|
|
|
|
#include "vault/mem/engine-buffer-manager.hpp"
|
|
|
|
|
|
#include "vault/mem/engine-buffer-allocator.hpp"
|
|
|
|
|
|
#include "lib/depend-inject.hpp"
|
|
|
|
|
|
#include "lib/thread.hpp"
|
2026-04-21 23:30:35 +02:00
|
|
|
|
//#include "lib/error.hpp"
|
2026-05-16 23:06:30 +02:00
|
|
|
|
#include "lib/util.hpp"
|
2026-05-18 23:12:43 +02:00
|
|
|
|
#include "test/diagnostic-output.hpp"
|
2026-04-21 23:30:35 +02:00
|
|
|
|
|
2026-05-16 23:06:30 +02:00
|
|
|
|
#include <algorithm>
|
2026-04-21 23:30:35 +02:00
|
|
|
|
|
2026-05-16 23:06:30 +02:00
|
|
|
|
using util::isSameAdr;
|
|
|
|
|
|
using std::this_thread::yield;
|
|
|
|
|
|
using lib::Thread;
|
2026-04-21 23:30:35 +02:00
|
|
|
|
|
|
|
|
|
|
namespace vault {
|
|
|
|
|
|
namespace mem {
|
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
|
|
// using LERR_(LOGIC);
|
|
|
|
|
|
// using LERR_(LIFECYCLE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // Test helper
|
|
|
|
|
|
|
|
|
|
|
|
// template<typename X>
|
|
|
|
|
|
// Buff*
|
|
|
|
|
|
// mark_as_Buffer(X& something)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// return reinterpret_cast<Buff*> (std::addressof(something));
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**************************************************************************//**
|
|
|
|
|
|
* @test verify worker memory handling that relies on a thread-local allocator.
|
|
|
|
|
|
*/
|
|
|
|
|
|
class LocalBufferStore_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2026-05-16 23:06:30 +02:00
|
|
|
|
// seedRand();
|
2026-04-21 23:30:35 +02:00
|
|
|
|
|
2026-05-16 23:06:30 +02:00
|
|
|
|
verify_API();
|
|
|
|
|
|
verify_announce();
|
|
|
|
|
|
verify_allocate();
|
|
|
|
|
|
verify_release();
|
|
|
|
|
|
verify_heuristic();
|
2026-04-21 23:30:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
|
|
void
|
2026-05-16 23:06:30 +02:00
|
|
|
|
verify_API()
|
|
|
|
|
|
{
|
|
|
|
|
|
// provide a »central BufferPool« (as a transient instance for this test)
|
|
|
|
|
|
lib::DependInject<EngineBufferManager>::Local<EngineBufferManager> globalPool;
|
|
|
|
|
|
|
|
|
|
|
|
// provide the *test subject*,
|
|
|
|
|
|
// which implements the BufferStage API
|
|
|
|
|
|
// and uses a thread-local metadata table internally
|
|
|
|
|
|
LocalBufferStore localStore;
|
|
|
|
|
|
BufferProviderSetup::Store& storeAPI = localStore;
|
|
|
|
|
|
|
|
|
|
|
|
const size_t BUFFSIZ = 555;
|
|
|
|
|
|
const HashVal TYPEID = 12345;
|
|
|
|
|
|
const size_t CNT = 5;
|
|
|
|
|
|
const int64_t ARG = 7;
|
|
|
|
|
|
const LocalTag MARK{42};
|
|
|
|
|
|
|
|
|
|
|
|
const size_t ALLOC = HeapBufferAllocator::numTiles(BUFFSIZ) * HeapBufferAllocator::TILE_SIZ;
|
|
|
|
|
|
|
2026-05-18 23:12:43 +02:00
|
|
|
|
Thread testWorker{[&] /* === Play through one typical usage cycle === */
|
2026-05-16 23:06:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
uint avail = storeAPI.prepareBuffers(TYPEID, CNT, BUFFSIZ);
|
|
|
|
|
|
CHECK (0 == avail);
|
|
|
|
|
|
|
|
|
|
|
|
BuffAlloc storageSlot = storeAPI.provideBuffer (TYPEID, BUFFSIZ, MARK, ARG);
|
|
|
|
|
|
auto& [storage,buffSiz,specifics] = storageSlot;
|
|
|
|
|
|
CHECK (storage);
|
|
|
|
|
|
CHECK (BUFFSIZ <= buffSiz);
|
|
|
|
|
|
CHECK (ALLOC == buffSiz);
|
|
|
|
|
|
CHECK (MARK == specifics);
|
|
|
|
|
|
|
|
|
|
|
|
// can use that memory
|
|
|
|
|
|
char* buf = reinterpret_cast<char*> (storage);
|
|
|
|
|
|
std::string ranS{lib::randStr(555-1)};
|
|
|
|
|
|
auto* p = std::copy (ranS.begin(), ranS.end(), buf);
|
|
|
|
|
|
CHECK (ranS.length() == size_t(p - buf));
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
CHECK (ranS == buf);
|
|
|
|
|
|
|
|
|
|
|
|
storeAPI.mark_emitted (TYPEID, storageSlot);
|
|
|
|
|
|
storeAPI.detachBuffer (TYPEID, storageSlot);
|
|
|
|
|
|
|
|
|
|
|
|
avail = storeAPI.prepareBuffers(TYPEID, 2, BUFFSIZ);
|
|
|
|
|
|
CHECK (1 == avail);
|
|
|
|
|
|
|
|
|
|
|
|
BuffAlloc slot1 = storeAPI.provideBuffer (TYPEID, BUFFSIZ, MARK, ARG);
|
|
|
|
|
|
auto& [sto1,siz1,tag1] = slot1;
|
|
|
|
|
|
BuffAlloc slot2 = storeAPI.provideBuffer (TYPEID, BUFFSIZ, MARK, ARG);
|
|
|
|
|
|
auto& [sto2,siz2,tag2] = slot2;
|
|
|
|
|
|
CHECK (sto1);
|
|
|
|
|
|
CHECK (sto2);
|
|
|
|
|
|
CHECK (isSameAdr (storage, sto1));
|
2026-05-18 23:12:43 +02:00
|
|
|
|
CHECK (not isSameAdr (storage, sto2));
|
2026-05-16 23:06:30 +02:00
|
|
|
|
CHECK (BUFFSIZ <= siz1);
|
|
|
|
|
|
CHECK (BUFFSIZ <= siz2);
|
|
|
|
|
|
|
|
|
|
|
|
storeAPI.detachBuffer (TYPEID, slot2);
|
|
|
|
|
|
storeAPI.detachBuffer (TYPEID, slot1);
|
|
|
|
|
|
}}; // LocalMemPool winds down at end of thread
|
|
|
|
|
|
while (testWorker)
|
|
|
|
|
|
yield(); // wait for worker to finish
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (globalPool); // the Mock was accessed (and thus instantiated)
|
|
|
|
|
|
|
|
|
|
|
|
// before shutdown, LocalMemPool has sent back all allocations;
|
|
|
|
|
|
// these should sit now in the EngineBufferManager's input queue
|
2026-05-18 23:12:43 +02:00
|
|
|
|
SHOW_EXPR(watch(*globalPool).numAllocs());
|
2026-05-16 23:06:30 +02:00
|
|
|
|
CHECK ( CNT+1 == watch(*globalPool).numAllocs());
|
|
|
|
|
|
CHECK ((CNT+1)*ALLOC == watch(*globalPool).bytesLeased());
|
|
|
|
|
|
|
|
|
|
|
|
// process in-queue and retrieve all allocations..
|
|
|
|
|
|
globalPool->processPendingRequests();
|
|
|
|
|
|
CHECK (0 == watch(*globalPool).bytesLeased());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verify_announce()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verify_allocate()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
verify_release()
|
2026-04-21 23:30:35 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
2026-05-16 23:06:30 +02:00
|
|
|
|
verify_heuristic()
|
2026-04-21 23:30:35 +02:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (LocalBufferStore_test, "unit engine");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}}} // namespace vault::mem::test
|