~siborgium/prelockdpp

24973ddc52eae9b1eb418d414ba4476098337436 — Sergey Smirnykh 2 years ago f72557e
Replace iostreams with fmtlib
2 files changed, 17 insertions(+), 10 deletions(-)

M CMakeLists.txt
M main.cpp
M CMakeLists.txt => CMakeLists.txt +2 -1
@@ 12,10 12,11 @@ FetchContent_MakeAvailable(plf_colony)

find_package(Boost 1.70 REQUIRED)
find_package(nlohmann_json 3.10.5 REQUIRED)
find_package(fmt REQUIRED)

add_executable(prelockdpp main.cpp)
target_compile_features(prelockdpp PRIVATE cxx_std_20)
target_link_libraries(prelockdpp PRIVATE Boost::boost nlohmann_json::nlohmann_json)
target_link_libraries(prelockdpp PRIVATE Boost::boost nlohmann_json::nlohmann_json fmt::fmt)
target_include_directories(prelockdpp PRIVATE "${plf_colony_SOURCE_DIR}")

install(TARGETS prelockdpp RUNTIME DESTINATION bin)

M main.cpp => main.cpp +15 -9
@@ 1,5 1,4 @@

#include <iostream>
#include <limits>
#include <charconv>
#include <string>


@@ 13,6 12,7 @@
#include <boost/intrusive/set.hpp>
#include <boost/intrusive/set_hook.hpp>
#include <plf_colony.h>
#include <fmt/core.h>

#include <dirent.h>
#include <fcntl.h>


@@ 230,7 230,11 @@ struct linebuf {
    size_t capacity{ 0 };

    ssize_t getline(FILE* file) {
        return ::getline(&ptr, &capacity, file);
        auto n = ::getline(&ptr, &capacity, file);
        if (n == -1 && errno != 0) {
            throw errno_exception("linebuf::getline failed", errno);
        }
        return n;
    }
    ~linebuf() {
        if (ptr) {


@@ 368,6 372,9 @@ struct context {
            }
            DEFER{ fclose(stat); };
            auto stat_len = stat_buffer.getline(stat);
            if (stat_len == -1) {
                throw std::logic_error{ "/proc/PID/stat is empty" };
            }

            std::string_view stat_view( stat_buffer.ptr, stat_len );
            if (stat_view.ends_with(" 0 0 0")) {


@@ 555,7 562,7 @@ struct context {
        }

        for (auto && id: current_uniqs_set) {
            std::cout << id.start_time.tv_sec << '.' << id.start_time.tv_nsec << ' ' << id.pid << '\n';
            fmt::print("{}.{} {}\n", id.start_time.tv_sec, id.start_time.tv_nsec, id.pid);
        }

#if 0


@@ 568,9 575,9 @@ struct context {
        auto m = tspec_diff(m0, get_clock_time(CLOCK_MONOTONIC));
        auto p = tspec_diff(p0, get_clock_time(CLOCK_PROCESS_CPUTIME_ID));

        std::cout << "Uptime:         " << uptime.tv_sec << '.' << uptime.tv_nsec << '\n';
        std::cout << "Monotonic time: " << m.tv_sec << '.' << m.tv_nsec << '\n';
        std::cout << "Proc CPU  time: " << p.tv_sec << '.' << p.tv_nsec << '\n';
        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);
    }

    context(config& cfg):


@@ 634,7 641,6 @@ struct lock_context {
        if (read(timer.fd, &time, sizeof(time)) != sizeof(time)) {
            throw std::logic_error{ "Reading from timer fd returned fewer bytes than expected" };
        }
        std::cerr << "Timer ticked!\n";
        ctx.get_current_set();
    }
    void on_signal() {


@@ 646,7 652,6 @@ struct lock_context {
        if (r == 0 || r != sizeof(info)) {
            throw std::logic_error{ "signalfd was reported as active, but read(2) returned eof" };
        }
        std::cerr << "Got signal!\n";
    }
    void run() {
        timer.start(ctx.cfg.interval_sec);


@@ 683,12 688,13 @@ int main(int argc, char **argv) {
        config  cfg;
        context ctx{ cfg };

        // FIXME: we should ensure CAP_IPC_LOCK capability
        ctx.mlockall();

        lock_context runner{ ctx };
        runner.run();
    } catch (const std::exception& e) {
        std::cerr << "[ERROR]: " << e.what() << '\n';
        fmt::print(stderr, "[ERROR]: {}\n", e.what());
        return EXIT_FAILURE;
    }
}