Make everything in core connector memory resource aware
This commit is contained in:
parent
161ba94f45
commit
5ab6a47efb
@ -62,8 +62,7 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char ** argv) {
|
|||||||
|
|
||||||
auto finalizeTransaction = [&](const AuthenticationStatus & status) mutable -> tl::expected< AuthenticationStatus, Error > {
|
auto finalizeTransaction = [&](const AuthenticationStatus & status) mutable -> tl::expected< AuthenticationStatus, Error > {
|
||||||
if(status.userAuthorized()) {
|
if(status.userAuthorized()) {
|
||||||
auto tok = std::string{status.accessToken().data()};
|
Finish finish{session, status.accessToken()};
|
||||||
Finish finish{session, std::move(tok)};
|
|
||||||
finish.handle(CH);
|
finish.handle(CH);
|
||||||
}
|
}
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
@ -171,8 +171,8 @@ class CheckApplication {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
tl::expected< int, Error > call(const CoreHandler_t & coreHandler, std::string_view systemToken) const {
|
tl::expected< int, Error > call(const CoreHandler_t & coreHandler, std::string_view systemToken) const {
|
||||||
memory::Monotonic_1k_Resource mr;
|
memory::MonotonicStack_1k_Resource mr;
|
||||||
RapidJSONPMRStackAlloc< 2048 > alloc{};
|
RapidJSONPMRAlloc alloc{&mr};
|
||||||
constexpr std::string_view api = "/api/app/init";
|
constexpr std::string_view api = "/api/app/init";
|
||||||
Status status;
|
Status status;
|
||||||
|
|
||||||
|
|||||||
@ -50,24 +50,22 @@ class Configuration {
|
|||||||
class ConfigurationReader {
|
class ConfigurationReader {
|
||||||
public:
|
public:
|
||||||
ConfigurationReader(std::pmr::memory_resource * memResource, std::string_view filepath) : memoryResource(memResource) {
|
ConfigurationReader(std::pmr::memory_resource * memResource, std::string_view filepath) : memoryResource(memResource) {
|
||||||
|
keyValues.reserve(20);
|
||||||
loadFromFile(filepath);
|
loadFromFile(filepath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load config from file path
|
// Load config from file path
|
||||||
void loadFromFile(std::string_view filepath) {
|
void loadFromFile(std::string_view filepath) {
|
||||||
using namespace memory::literals;
|
using namespace memory::literals;
|
||||||
memory::MonotonicStackResource< 4_kB > stackResource;
|
memory::MonotonicStack_1k_Resource memoryResource;
|
||||||
std::ifstream file(filepath.data());
|
std::ifstream file(filepath.data());
|
||||||
if(not file.good())
|
if(not file.good())
|
||||||
return ;
|
return ;
|
||||||
|
|
||||||
std::pmr::string line{&stackResource};
|
std::pmr::string line{&memoryResource};
|
||||||
line.reserve(4000); // allocate full stack to line
|
line.reserve(300);
|
||||||
|
|
||||||
while(std::getline(file, line)) {
|
while(std::getline(file, line)) {
|
||||||
std::pmr::string key{memoryResource};
|
|
||||||
std::pmr::string value{memoryResource};
|
|
||||||
|
|
||||||
details::trimInPlace(line);
|
details::trimInPlace(line);
|
||||||
if(!line.length())
|
if(!line.length())
|
||||||
continue;
|
continue;
|
||||||
@ -76,8 +74,8 @@ class ConfigurationReader {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
auto posEqual = line.find('=');
|
auto posEqual = line.find('=');
|
||||||
key = line.substr(0, posEqual);
|
std::pmr::string key {line.substr(0, posEqual),&memoryResource};
|
||||||
value = line.substr(posEqual + 1);
|
std::pmr::string value{line.substr(posEqual + 1), &memoryResource};
|
||||||
|
|
||||||
keyValues[std::move(key)] = std::move(value);
|
keyValues[std::move(key)] = std::move(value);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -157,24 +157,23 @@ class CoreHandler : public CoreHandlerInterface< CoreHandler< HttpHandler > > {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tl::expected< Document, Error > request(RapidJSONPMRAlloc & mr, std::string_view path, const Document & body) const {
|
tl::expected< Document, Error > request(RapidJSONPMRAlloc & mr, std::string_view path, const Document & body) const {
|
||||||
memory::Monotonic_8k_Resource memoryResource;
|
memory::Monotonic_4k_Resource memoryResource;
|
||||||
|
|
||||||
const auto validateSignature = [&](const auto & arg) { return this->validateSignature(arg); };
|
const auto validateSignature = [&](const auto & arg) { return this->validateSignature(arg); };
|
||||||
const auto validateResponse = [&](const auto & arg) { return this->validateResponse(mr, arg); };
|
const auto validateResponse = [&](const auto & arg) { return this->validateResponse(mr, arg); };
|
||||||
const auto handleError = [&](const auto & error) { return this->handleError(error); };
|
const auto handleError = [&](const auto & error) { return this->handleError(error); };
|
||||||
const auto pmrs = [&](const auto & txt) { return std::pmr::string{txt, &memoryResource}; };
|
|
||||||
|
|
||||||
Request request{&memoryResource};
|
Request request{&memoryResource};
|
||||||
Response response{&memoryResource};
|
Response response{&memoryResource};
|
||||||
|
|
||||||
request.headers["Content-Type"] = pmrs("application/json");
|
request.headers["Content-Type"] = "application/json";
|
||||||
request.headers["Accept"] = pmrs("application/json");
|
request.headers["Accept"] = "application/json";
|
||||||
|
|
||||||
stringifyTo(body, request.body);
|
stringifyTo(body, request.body);
|
||||||
|
|
||||||
signRequest(request);
|
signRequest(request);
|
||||||
std::pmr::string uri{&memoryResource};
|
std::pmr::string uri{&memoryResource};
|
||||||
uri.reserve(config().apiServer.size() + path.size() + 1);
|
uri.reserve(conservative_estimate(config().apiServer, path.size()));
|
||||||
uri += config().apiServer;
|
uri += config().apiServer;
|
||||||
uri += path.data();
|
uri += path.data();
|
||||||
|
|
||||||
|
|||||||
@ -122,8 +122,8 @@ struct FileWriter {
|
|||||||
|
|
||||||
template < typename T >
|
template < typename T >
|
||||||
static void stringifyTo(const Document & body, T & to) {
|
static void stringifyTo(const Document & body, T & to) {
|
||||||
memory::Monotonic_1k_Resource tmpResource;
|
memory::MonotonicStack_1k_Resource memoryResource;
|
||||||
RapidJSONPMRAlloc alloc{&tmpResource};
|
RapidJSONPMRAlloc alloc{&memoryResource};
|
||||||
StringWriter< T > s{to};
|
StringWriter< T > s{to};
|
||||||
rapidjson::Writer< StringWriter< T >, rapidjson::UTF8<>, rapidjson::UTF8<>, RapidJSONPMRAlloc > writer{s, &alloc};
|
rapidjson::Writer< StringWriter< T >, rapidjson::UTF8<>, rapidjson::UTF8<>, RapidJSONPMRAlloc > writer{s, &alloc};
|
||||||
body.Accept(writer);
|
body.Accept(writer);
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory_resource>
|
||||||
#include <rublon/stdlib.hpp>
|
#include <rublon/stdlib.hpp>
|
||||||
|
|
||||||
namespace rublon {
|
namespace rublon {
|
||||||
@ -8,7 +9,7 @@ namespace memory {
|
|||||||
constexpr std::uint64_t operator"" _kB(unsigned long long kilobytes) {
|
constexpr std::uint64_t operator"" _kB(unsigned long long kilobytes) {
|
||||||
return kilobytes * 1024ULL;
|
return kilobytes * 1024ULL;
|
||||||
}
|
}
|
||||||
}
|
} // namespace literals
|
||||||
|
|
||||||
struct default_memory_resource {
|
struct default_memory_resource {
|
||||||
static inline std::pmr::memory_resource * _mr = std::pmr::get_default_resource();
|
static inline std::pmr::memory_resource * _mr = std::pmr::get_default_resource();
|
||||||
@ -22,14 +23,6 @@ namespace memory {
|
|||||||
return default_memory_resource{}._mr;
|
return default_memory_resource{}._mr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template < std::size_t N >
|
|
||||||
class MonotonicStackResource : public std::pmr::monotonic_buffer_resource {
|
|
||||||
char _buffer[N];
|
|
||||||
|
|
||||||
public:
|
|
||||||
MonotonicStackResource() : std::pmr::monotonic_buffer_resource{_buffer, N, std::pmr::null_memory_resource()} {}
|
|
||||||
};
|
|
||||||
|
|
||||||
class MonotonicResourceBase {
|
class MonotonicResourceBase {
|
||||||
public:
|
public:
|
||||||
std::pmr::memory_resource * _upstream{};
|
std::pmr::memory_resource * _upstream{};
|
||||||
@ -47,15 +40,31 @@ namespace memory {
|
|||||||
template < std::size_t N >
|
template < std::size_t N >
|
||||||
class MonotonicResource : MonotonicResourceBase, public std::pmr::monotonic_buffer_resource {
|
class MonotonicResource : MonotonicResourceBase, public std::pmr::monotonic_buffer_resource {
|
||||||
public:
|
public:
|
||||||
MonotonicResource()
|
MonotonicResource(std::pmr::memory_resource * mr = default_resource())
|
||||||
: MonotonicResourceBase{N}, std::pmr::monotonic_buffer_resource{this->_buffer, this->_size, default_resource()} {}
|
: MonotonicResourceBase{N}, std::pmr::monotonic_buffer_resource{this->_buffer, this->_size, mr} {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template < std::size_t N >
|
||||||
|
class MonotonicStackResourceBase {
|
||||||
|
public:
|
||||||
|
char _buffer[N];
|
||||||
|
};
|
||||||
|
|
||||||
|
template < std::size_t N >
|
||||||
|
class MonotonicStackResource : MonotonicStackResourceBase< N >, public std::pmr::monotonic_buffer_resource {
|
||||||
|
public:
|
||||||
|
MonotonicStackResource()
|
||||||
|
: MonotonicStackResourceBase< N >{}, std::pmr::monotonic_buffer_resource{this->_buffer, N, default_resource()} {}
|
||||||
|
};
|
||||||
|
|
||||||
|
using MonotonicStack_1k_Resource = MonotonicStackResource< 1 * 1024 >;
|
||||||
|
using MonotonicStack_2k_Resource = MonotonicStackResource< 2 * 1024 >;
|
||||||
|
|
||||||
|
|
||||||
using Monotonic_1k_Resource = MonotonicResource< 1 * 1024 >;
|
using Monotonic_1k_Resource = MonotonicResource< 1 * 1024 >;
|
||||||
using Monotonic_2k_Resource = MonotonicResource< 2 * 1024 >;
|
using Monotonic_2k_Resource = MonotonicResource< 2 * 1024 >;
|
||||||
using Monotonic_4k_Resource = MonotonicResource< 4 * 1024 >;
|
using Monotonic_4k_Resource = MonotonicResource< 4 * 1024 >;
|
||||||
using Monotonic_8k_Resource = MonotonicResource< 8 * 1024 >;
|
using Monotonic_8k_Resource = MonotonicResource< 8 * 1024 >;
|
||||||
using Monotonic_16k_Resource = MonotonicResource< 16 * 1024 >;
|
|
||||||
} // namespace memory
|
} // namespace memory
|
||||||
|
|
||||||
// class RublonMemory {
|
// class RublonMemory {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "rublon/memory.hpp"
|
||||||
#include <tl/expected.hpp>
|
#include <tl/expected.hpp>
|
||||||
|
|
||||||
#include <rublon/authentication_step_interface.hpp>
|
#include <rublon/authentication_step_interface.hpp>
|
||||||
@ -59,8 +60,9 @@ class PasscodeBasedAuth : public AuthenticationStep {
|
|||||||
|
|
||||||
tl::expected< std::reference_wrapper< Document >, Error > readPasscode(Document & body, const Pam_t & pam) const {
|
tl::expected< std::reference_wrapper< Document >, Error > readPasscode(Document & body, const Pam_t & pam) const {
|
||||||
/// TODO assert in interactive mode
|
/// TODO assert in interactive mode
|
||||||
|
memory::MonotonicStackResource< 100 > memoryResource;
|
||||||
auto & alloc = body.GetAllocator();
|
auto & alloc = body.GetAllocator();
|
||||||
auto vericode = pam.scan([](const char * userInput) { return std::string{userInput}; }, _userMessage);
|
auto vericode = pam.scan([&](const char * userInput) { return std::pmr::string{userInput, &memoryResource}; }, _userMessage);
|
||||||
|
|
||||||
if(hasValidLength(vericode) and hasValidCharacters(vericode)) {
|
if(hasValidLength(vericode) and hasValidCharacters(vericode)) {
|
||||||
Value confirmFieldValue(_confirmField, alloc);
|
Value confirmFieldValue(_confirmField, alloc);
|
||||||
|
|||||||
@ -14,8 +14,8 @@ class RublonFactory {
|
|||||||
public:
|
public:
|
||||||
tl::expected< void, Error > initializeSession(Session & session) {
|
tl::expected< void, Error > initializeSession(Session & session) {
|
||||||
log(LogLevel::Debug, "Configuration read start");
|
log(LogLevel::Debug, "Configuration read start");
|
||||||
memory::Monotonic_16k_Resource heap;
|
memory::MonotonicStack_2k_Resource memory_resource;
|
||||||
ConfigurationReader reader{&heap, "/etc/rublon.config"};
|
ConfigurationReader reader{&memory_resource, "/etc/rublon.config"};
|
||||||
|
|
||||||
if(auto ok = reader.applyTo(session.config()); not ok.has_value()) {
|
if(auto ok = reader.applyTo(session.config()); not ok.has_value()) {
|
||||||
log(LogLevel::Warning, "Configuration contains errors");
|
log(LogLevel::Warning, "Configuration contains errors");
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "rublon/memory.hpp"
|
||||||
#include <memory_resource>
|
#include <memory_resource>
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
#include <rublon/bits.hpp>
|
#include <rublon/bits.hpp>
|
||||||
@ -9,9 +10,91 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
|
||||||
namespace rublon {
|
namespace rublon {
|
||||||
|
|
||||||
class Session {
|
|
||||||
|
// class LoggingMemoryResource : public std::pmr::memory_resource {
|
||||||
|
// public:
|
||||||
|
// LoggingMemoryResource(std::pmr::memory_resource* upstream = std::pmr::get_default_resource())
|
||||||
|
// : upstream_(upstream), allocated_bytes_(0)
|
||||||
|
// {
|
||||||
|
// pid_t pid = getpid();
|
||||||
|
// std::ostringstream filename;
|
||||||
|
// filename << "/tmp/memory" << pid << ".log";
|
||||||
|
// log_file_.open(filename.str(), std::ios::out | std::ios::trunc);
|
||||||
|
|
||||||
|
// if (!log_file_) {
|
||||||
|
// throw std::runtime_error("Failed to open log file");
|
||||||
|
// }
|
||||||
|
// log("Memory logging started.");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ~LoggingMemoryResource() override {
|
||||||
|
// log("Memory logging ended.");
|
||||||
|
// log_file_.close();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// protected:
|
||||||
|
// void* do_allocate(std::size_t bytes, std::size_t alignment) override {
|
||||||
|
// std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
// void* ptr = upstream_->allocate(bytes, alignment);
|
||||||
|
// allocated_bytes_ += bytes;
|
||||||
|
// active_allocations_[ptr] = bytes;
|
||||||
|
|
||||||
|
// log("ALLOC", ptr, bytes, alignment);
|
||||||
|
// return ptr;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// void do_deallocate(void* ptr, std::size_t bytes, std::size_t alignment) override {
|
||||||
|
// std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
// auto it = active_allocations_.find(ptr);
|
||||||
|
// if (it != active_allocations_.end()) {
|
||||||
|
// allocated_bytes_ -= it->second;
|
||||||
|
// active_allocations_.erase(it);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// log("FREE", ptr, bytes, alignment);
|
||||||
|
// upstream_->deallocate(ptr, bytes, alignment);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
|
||||||
|
// return this == &other;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private:
|
||||||
|
// std::pmr::memory_resource* upstream_;
|
||||||
|
// std::ofstream log_file_;
|
||||||
|
// std::mutex mutex_;
|
||||||
|
// std::size_t allocated_bytes_;
|
||||||
|
// std::map<void*, std::size_t> active_allocations_;
|
||||||
|
|
||||||
|
// void log(const std::string& action, void* ptr = nullptr, std::size_t bytes = 0, std::size_t alignment = 0) {
|
||||||
|
// auto now = std::chrono::system_clock::now();
|
||||||
|
// auto now_time = std::chrono::system_clock::to_time_t(now);
|
||||||
|
// log_file_ << std::put_time(std::localtime(&now_time), "%F %T")
|
||||||
|
// << " | " << std::setw(6) << action;
|
||||||
|
|
||||||
|
// if (ptr) {
|
||||||
|
// log_file_ << " | ptr=" << ptr << " bytes=" << bytes << " align=" << alignment;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// log_file_ << " | total=" << allocated_bytes_ << " bytes\n";
|
||||||
|
// log_file_.flush();
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// std::pmr::unsynchronized_pool_resource resource{};
|
||||||
|
// LoggingMemoryResource mr{&resource};
|
||||||
|
|
||||||
|
class DefaultResource {
|
||||||
|
public:
|
||||||
|
DefaultResource() {
|
||||||
|
// memory::set_default_resource(&mr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Session :public DefaultResource {
|
||||||
std::pmr::memory_resource * mr;
|
std::pmr::memory_resource * mr;
|
||||||
const Pam_t & _pam;
|
const Pam_t & _pam;
|
||||||
Configuration _config;
|
Configuration _config;
|
||||||
@ -22,8 +105,7 @@ class Session {
|
|||||||
CoreHandler_t _coreHandler;
|
CoreHandler_t _coreHandler;
|
||||||
/// TODO log
|
/// TODO log
|
||||||
public:
|
public:
|
||||||
Session(const Pam_t & pam)
|
Session(const Pam_t & pam) : DefaultResource{}, mr{memory::default_resource()}, _pam{pam}, _config{mr}, _coreHandler{_config} {
|
||||||
: mr{memory::default_resource()}, _pam{pam}, _config{mr}, _coreHandler{_config} {
|
|
||||||
details::initLog();
|
details::initLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -234,7 +234,7 @@ namespace details {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::pmr::string osName(std::pmr::memory_resource * mr) {
|
std::pmr::string osName(std::pmr::memory_resource * mr) {
|
||||||
memory::Monotonic_8k_Resource memoryResource;
|
memory::Monotonic_1k_Resource memoryResource;
|
||||||
|
|
||||||
std::ifstream file(std::filesystem::path{"/etc/os-release"});
|
std::ifstream file(std::filesystem::path{"/etc/os-release"});
|
||||||
if(not file.good())
|
if(not file.good())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user