~siborgium/prelockdpp

d953dcf033a8e2176f876502e8e122fd6e8cfb51 — Sergey Smirnykh 2 years ago 0b03575
get_current_set v3 -- uniq_id set
1 files changed, 39 insertions(+), 6 deletions(-)

M main.cpp
M main.cpp => main.cpp +39 -6
@@ 147,6 147,32 @@ struct timer_fd: public base_fd {
    }
};

struct uniq_id {
    timespec start_time;
    int      pid;

    constexpr bool operator == (const uniq_id& id) const {
        return start_time.tv_sec == id.start_time.tv_sec
            && start_time.tv_nsec == id.start_time.tv_nsec
            && pid == id.pid;
    }
};

namespace std {
    template<>
    struct hash<uniq_id> {
        size_t operator()(const uniq_id &id) const {
            constexpr size_t prime = 257;

            size_t h1{ hash<decltype(id.start_time.tv_sec)>{}(id.start_time.tv_sec) };
            size_t h2{ hash<decltype(id.start_time.tv_nsec)>{}(id.start_time.tv_nsec) };
            size_t h3{ hash<decltype(id.pid)>{}(id.pid) };

            return (h1 * prime + h2) * prime + h3;
        }
    };
}

struct context {
    timespec start_time;
    int      self_pid;


@@ 253,7 279,10 @@ struct context {
    }
    // return whether cgroup matches on any of specified regexes
    auto match_cgroup(std::string_view cgroup) {
        // FIXME: unimplemented
#if 0
        std::cout << "match_cgroup(" << cgroup << "): unimplemented\n";
#endif
        return true;
    }
    auto with_uniq_id(const char* stat_path, auto && callback) {


@@ 310,7 339,7 @@ struct context {
                return stat_view.substr(begin, end - begin);
            }();

            callback(start_time, stat_view.substr(0, rparen_idx));
            callback(start_time);
        } catch (const std::system_error& e) {
            auto code = e.code();
            if (code.value() != ENOENT) {


@@ 321,6 350,8 @@ struct context {
    auto get_current_set() {
        using namespace std::string_view_literals;

        std::unordered_set<uniq_id> uniq_set;

        auto m0 = get_clock_time(CLOCK_MONOTONIC);
        auto p0 = get_clock_time(CLOCK_PROCESS_CPUTIME_ID);
        auto uptime = get_uptime();


@@ 348,8 379,7 @@ struct context {
            pid_end = std::copy(entry->d_name, name_end, pid_begin);
            fmt_proc_pid_file(stat_path);

            std::cout << pid << '\n';
            with_uniq_id(path_buf, [&](auto start_time, auto prefix){
            with_uniq_id(path_buf, [&](auto start_time) {
                auto start_time_ = [=,this] {
                    unsigned long long ticks;
                    auto [p, ec] = std::from_chars(


@@ 363,9 393,8 @@ struct context {
                    return tspec_from_ticks(ticks);
                }();

                std::cout << "prefix:            " << prefix << '\n';
                std::cout << "start_time:        " << start_time << '\n';
                std::cout << "start_time (tspc): " << start_time_.tv_sec << '.' << start_time_.tv_nsec << '\n';
                auto [iter, inserted] = uniq_set.emplace(uniq_id{ start_time_, pid });
                assert(inserted);
            });
        }
        if (errno) {


@@ 378,6 407,10 @@ struct context {
        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';

        for (auto && id: uniq_set) {
            std::cout << id.start_time.tv_sec << '.' << id.start_time.tv_nsec << ' ' << id.pid << '\n';
        }
    }

    context(config& cfg):