Commit 06b496e2 authored by Julius Athenstaedt's avatar Julius Athenstaedt
Browse files

Debug Timer, it should be moved to the logger

parent df83a7d8
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@
#define EUNKNOWN (-1)

#define CTX gkfs::preload::PreloadContext::getInstance()
#define DT gkfs::preload::DebugTimer::getInstance()
namespace gkfs::preload {
void
init_environment();
+45 −0
Original line number Diff line number Diff line
@@ -50,6 +50,14 @@

#include <bitset>

// Debug timer
#include <chrono>
#include <unordered_map>
#include <string>
#include <mutex>
extern char *program_invocation_name;
// ---------

/* Forward declarations */
namespace gkfs {
namespace filemap {
@@ -75,6 +83,43 @@ class WriteSizeCache;
} // namespace cache

namespace preload {

// Debug timer
class DebugTimer {
public:
    static DebugTimer&
    getInstance() {
        static DebugTimer instance;
        return instance;
    }

    void
    record(const std::string& name);

    void
    write_stats(const std::string& file);

private:
    struct Stat {
        std::chrono::high_resolution_clock::time_point lastTime{};
        int callCount = 0;
        double maxDuration = 0.0;
        double totalDuration = 0.0;
    };

    std::chrono::high_resolution_clock::time_point start_time{};
    std::unordered_map<std::string, Stat> stats_;
    mutable std::mutex mutex_;

    // Private constructor to enforce singleton
    DebugTimer();
    ~DebugTimer() = default;
    DebugTimer(const DebugTimer&) = delete;
    DebugTimer&
    operator=(const DebugTimer&) = delete;
};
// ---------

/*
 * Client file system config
 */
+56 −0
Original line number Diff line number Diff line
@@ -71,6 +71,62 @@ namespace gkfs {

namespace preload {

DebugTimer::DebugTimer() {
    this->start_time = std::chrono::high_resolution_clock::now();
    LOG(DEBUG, "Create DebugTimer");
}

// Debug timer
void
DebugTimer::record(const std::string& name) {
    std::lock_guard<std::mutex> lock(mutex_);

    auto now = std::chrono::high_resolution_clock::now();
    auto& stat = stats_[name];

    if(stat.lastTime.time_since_epoch().count() != 0) {
        auto duration =
                std::chrono::duration<double, std::milli>(now - stat.lastTime)
                        .count();
        stat.callCount++;
        if(duration > stat.maxDuration)
            stat.maxDuration = duration;
        stat.totalDuration += duration;
    }

    stat.lastTime = now;
}

void
DebugTimer::write_stats(const std::string& file) {
    LOG(DEBUG, "Write DebugTimer stats");
    std::lock_guard<std::mutex> lock(mutex_);

    std::ofstream myfile(file, std::ios_base::app);

    auto now = std::chrono::high_resolution_clock::now();
    auto duration =
            std::chrono::duration<double, std::milli>(now - this->start_time)
                    .count();

    if(myfile.is_open()) {
        myfile << "-----" << program_invocation_name << "-----" << std::endl;
        myfile << "timestamp: " << now.time_since_epoch().count() << std::endl;
        myfile << "duration: " << duration << "ms" << std::endl << std::endl;
        for(const auto& [name, stat] : stats_) {
            myfile << "Name: " << name << "\n  Calls: " << stat.callCount
                   << "\n  Max duration: " << stat.maxDuration << " ms"
                   << "\n  Avg duration: "
                   << (stat.callCount ? stat.totalDuration / stat.callCount : 0)
                   << " ms\n";
        }
        myfile.close();
    } else {
        LOG(DEBUG, "could not write stats o.0");
    }
}
// ---------

decltype(PreloadContext::MIN_INTERNAL_FD) constexpr PreloadContext::
        MIN_INTERNAL_FD;
decltype(PreloadContext::MAX_USER_FDS) constexpr PreloadContext::MAX_USER_FDS;