~siborgium/prelockdpp

eef9bd7e7f5c3044b9fc5e304bf679dfe6088415 — Sergey Smirnykh 1 year, 10 months ago 5d92301 trunk
mlock comms, link against fmt-header-only, fix fd leak
4 files changed, 18 insertions(+), 4 deletions(-)

M CMakeLists.txt
M config.cpp
M config.hpp
M main.cpp
M CMakeLists.txt => CMakeLists.txt +1 -1
@@ 29,7 29,7 @@ endif()

add_executable(prelockdpp main.cpp config.cpp)
target_compile_features(prelockdpp PRIVATE cxx_std_20)
target_link_libraries(prelockdpp PRIVATE Boost::boost nlohmann_json::nlohmann_json fmt::fmt tomlplusplus::tomlplusplus)
target_link_libraries(prelockdpp PRIVATE Boost::boost nlohmann_json::nlohmann_json fmt::fmt-header-only tomlplusplus::tomlplusplus)
target_include_directories(prelockdpp PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${plf_colony_SOURCE_DIR}")

if (WITH_LIBCAP)

M config.cpp => config.cpp +1 -0
@@ 59,6 59,7 @@ config parse_config_file(std::string_view path) {
        .comms = {
            .begins = { begins, pointers_count },
            .ptr = std::move(ptr),
            .len = map_size
        }
    };
}

M config.hpp => config.hpp +5 -0
@@ 13,12 13,17 @@ struct critical_comms {
    std::span<const char*> begins;

    std::unique_ptr<char[]> ptr;
    size_t                  len;

    auto size() const {
        assert(begins.size() > 0);
        return begins.size() - 1;
    }

    auto total_size() {
        return len;
    }

    std::string_view operator [](size_t n) {
        return { begins[n], begins[n + 1] };
    }

M main.cpp => main.cpp +11 -3
@@ 354,8 354,11 @@ struct context {
    // uniq_id -> mappings list

    void mlockall() {
        if (::mlock(cfg.comms.ptr.get(), cfg.comms.total_size())) {
            throw errno_exception{ "mlock(cfg.comms) failed", errno };
        }
        if (::mlockall(MCL_FUTURE)) {
            throw std::runtime_error{ concat("mlockall failed ", errno) };
            throw errno_exception{ "ctx.mlockall failed", errno };
        }
    }
    auto get_clock_time(clockid_t id) {


@@ 522,6 525,7 @@ struct context {
            if (!file) {
                throw errno_exception("fopen(/proc/PID/maps) failed", errno);
            }
            DEFER{ fclose(file); };
            ssize_t n = 0;
            while ((n = line_buffer.getline(file)) != -1) {
                auto end = line_buffer.ptr + n;


@@ 677,7 681,11 @@ struct lock_context {

    void on_timer() {
        uint64_t time;
        if (read(timer.fd, &time, sizeof(time)) != sizeof(time)) {
        ssize_t bytes_read = read(timer.fd, &time, sizeof(time));
        if (bytes_read < 0) {
            throw errno_exception{ "Failed to read from timer fd", errno };
        }
        if (bytes_read != sizeof(time)) {
            throw std::logic_error{ "Reading from timer fd returned fewer bytes than expected" };
        }
        auto m0 = ctx.get_clock_time(CLOCK_MONOTONIC);


@@ 692,7 700,7 @@ struct lock_context {

        fmt::print("Uptime:         {}.{}\n", uptime.tv_sec, uptime.tv_nsec);
        fmt::print("Monotonic time: {}.{}\n", m.tv_sec, m.tv_nsec);
        fmt::print("Proc CPU time:  {}.{}\n", p.tv_sec, p.tv_nsec);
        fmt::print("CPU time:       {}.{}\n", p.tv_sec, p.tv_nsec);
    }
    void on_signal() {
        signalfd_siginfo info;