Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the human functions in their own files / headers #147

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/wtf/bochscpu_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "bochscpu.hpp"
#include "debugger.h"
#include "globals.h"
#include "human.h"
#include "kdmp-parser.h"
#include "platform.h"
#include "tsl/robin_map.h"
Expand All @@ -23,14 +24,12 @@ struct BochscpuRunStats_t {
fmt::print("--------------------------------------------------\n");
fmt::print("Run stats:\n");
fmt::print("Instructions executed: {} ({} unique)\n",
NumberInstructionsExecuted, AggregatedCodeCoverage);
NumberToHuman(NumberInstructionsExecuted),
NumberToHuman(AggregatedCodeCoverage));
const uint64_t DirtyMemoryBytes = DirtyGpas * Page::Size;
const uint64_t DirtyMemoryMb = DirtyGpas / Page::Size;
fmt::print(" Dirty pages: {} bytes ({} MB)\n", DirtyMemoryBytes,
DirtyMemoryMb);
const uint64_t MemoryAccessMb = NumberMemoryAccesses / _1MB;
fmt::print(" Memory accesses: {} bytes ({} MB)\n",
NumberMemoryAccesses, MemoryAccessMb);
fmt::print(" Dirty pages: {}\n", BytesToHuman(DirtyMemoryBytes));
fmt::print(" Memory accesses: {}\n",
BytesToHuman(NumberMemoryAccesses));
}

void Reset() {
Expand Down
16 changes: 8 additions & 8 deletions src/wtf/client.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Axel '0vercl0k' Souchet - November 19 2020
#include "client.h"
#include "human.h"

namespace chrono = std::chrono;

Expand Down Expand Up @@ -34,26 +35,25 @@ class ClientStats_t {
// Compute the amount of time since the last time we got new coverage.
//

const auto &[LastCov, LastCovUnit] = SecondsToHuman(SecondsSince(LastCov_));
const auto &LastCov = SecondsToHuman(SecondsSince(LastCov_));

//
// Compute the amount of time since the server started.
//

const auto &[Uptime, UptimeUnit] = SecondsToHuman(SecondsSince(Start_));
const auto &Uptime = SecondsToHuman(SecondsSince(Start_));

//
// Compute the amount of testcases executed per second.
//

const auto &[ExecsPerSecond, ExecsPerSecondUnit] = NumberToHuman(
const auto &ExecsPerSecond = NumberToHuman(
double(TestcasesNumber_) / double(SecondsSince(Start_).count()));

fmt::print("#{} cov: {} exec/s: {:.1f}{} lastcov: {:.1f}{} crash: {} "
"timeout: {} cr3: {} uptime: {:.1f}{}\n",
TestcasesNumber_, Coverage_, ExecsPerSecond, ExecsPerSecondUnit,
LastCov, LastCovUnit, Crashes_, Timeouts_, Cr3s_, Uptime,
UptimeUnit);
fmt::print("#{} cov: {} exec/s: {} lastcov: {} crash: {} "
"timeout: {} cr3: {} uptime: {}\n",
TestcasesNumber_, Coverage_, ExecsPerSecond, LastCov, Crashes_,
Timeouts_, Cr3s_, Uptime);

LastPrint_ = chrono::system_clock::now();
}
Expand Down
72 changes: 72 additions & 0 deletions src/wtf/human.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Axel '0vercl0k' Souchet - November 27 2022
#include "human.h"

[[nodiscard]] chrono::seconds
SecondsSince(const chrono::system_clock::time_point &Since) {
const auto &Now = chrono::system_clock::now();
return chrono::duration_cast<chrono::seconds>(Now - Since);
}

//
// Utility that is used to print microseconds for human.
//

[[nodiscard]] SecondsHuman_t SecondsToHuman(const chrono::seconds &Seconds) {
const char *Unit = "s";
double SecondNumber = double(Seconds.count());
const double M = 60;
const double H = M * 60;
const double D = H * 24;
if (SecondNumber >= D) {
Unit = "d";
SecondNumber /= D;
} else if (SecondNumber >= H) {
Unit = "hr";
SecondNumber /= H;
} else if (SecondNumber >= M) {
Unit = "min";
SecondNumber /= M;
}

return {SecondNumber, Unit};
}

//
// Utility that is used to print bytes for human.
//

[[nodiscard]] BytesHuman_t BytesToHuman(const uint64_t Bytes_) {
const char *Unit = "b";
double Bytes = double(Bytes_);
const uint64_t K = 1'024;
const uint64_t M = K * K;
const uint64_t G = M * K;
if (Bytes >= G) {
Unit = "gb";
Bytes /= G;
} else if (Bytes >= M) {
Unit = "mb";
Bytes /= M;
} else if (Bytes >= K) {
Unit = "kb";
Bytes /= K;
}

return {Bytes, Unit};
}

[[nodiscard]] NumberHuman_t NumberToHuman(const uint64_t N_) {
const char *Unit = "";
double N = double(N_);
const uint64_t K = 1'000;
const uint64_t M = K * K;
if (N > M) {
Unit = "m";
N /= M;
} else if (N > K) {
Unit = "k";
N /= K;
}

return {N, Unit};
}
83 changes: 83 additions & 0 deletions src/wtf/human.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Axel '0vercl0k' Souchet - November 27 2022
#pragma once
#include "pch.h"

namespace chrono = std::chrono;

struct PercentageHuman_t {
uint32_t Value;
};

struct BytesHuman_t {
double Value;
const char *Unit;
};

struct NumberHuman_t {
double Value;
const char *Unit;
};

struct SecondsHuman_t {
double Value;
const char *Unit;
};

template <>
struct fmt::formatter<PercentageHuman_t> : fmt::formatter<std::string> {
template <typename FormatContext>
auto format(const PercentageHuman_t &Percentage, FormatContext &Ctx) const
-> decltype(Ctx.out()) {
return fmt::format_to(Ctx.out(), "{}%", Percentage.Value);
}
};

template <> struct fmt::formatter<BytesHuman_t> : fmt::formatter<std::string> {
template <typename FormatContext>
auto format(const BytesHuman_t &Bytes, FormatContext &Ctx) const
-> decltype(Ctx.out()) {
return fmt::format_to(Ctx.out(), "{:.1f}{}", Bytes.Value, Bytes.Unit);
}
};

template <> struct fmt::formatter<NumberHuman_t> : fmt::formatter<std::string> {
template <typename FormatContext>
auto format(const NumberHuman_t &Number, FormatContext &Ctx) const
-> decltype(Ctx.out()) {
return fmt::format_to(Ctx.out(), "{:.1f}{}", Number.Value, Number.Unit);
}
};

template <>
struct fmt::formatter<SecondsHuman_t> : fmt::formatter<std::string> {
template <typename FormatContext>
auto format(const SecondsHuman_t &Micro, FormatContext &Ctx) const
-> decltype(Ctx.out()) {
return fmt::format_to(Ctx.out(), "{:.1f}{}", Micro.Value, Micro.Unit);
}
};

//
// Utility that calculates the number of seconds sine a time point.
//

[[nodiscard]] chrono::seconds
SecondsSince(const chrono::system_clock::time_point &Since);

//
// Utility that is used to print seconds for human.
//

[[nodiscard]] SecondsHuman_t SecondsToHuman(const chrono::seconds &Seconds);

//
// Utility that is used to print bytes for human.
//

[[nodiscard]] BytesHuman_t BytesToHuman(const uint64_t Bytes_);

//
// Utility that is used to print numbers for human.
//

[[nodiscard]] NumberHuman_t NumberToHuman(const uint64_t N_);
65 changes: 0 additions & 65 deletions src/wtf/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,71 +374,6 @@ std::optional<bool> SaveFile(const fs::path &Path, const uint8_t *Buffer,
return true;
}

//
// Utility that calculates the number of seconds sine a time point.
//

[[nodiscard]] chrono::seconds
SecondsSince(const chrono::system_clock::time_point &Since) {
const auto &Now = chrono::system_clock::now();
return chrono::duration_cast<chrono::seconds>(Now - Since);
}

//
// Utility that is used to print seconds for human.
//

[[nodiscard]] std::pair<double, const char *>
SecondsToHuman(const chrono::seconds &Seconds) {
const char *Unit = "s";
double SecondsNumber = Seconds.count();
if (SecondsNumber > 86'400) {
Unit = "d";
SecondsNumber /= 86'400;
} else if (SecondsNumber > 3'600) {
Unit = "hr";
SecondsNumber /= 3'600;
} else if (SecondsNumber > 60) {
Unit = "min";
SecondsNumber /= 60;
}

return std::make_pair(SecondsNumber, Unit);
}

//
// Utility that is used to print bytes for human.
//

[[nodiscard]] std::pair<double, const char *>
BytesToHuman(const uint64_t Bytes_) {
const char *Unit = "b";
double Bytes = double(Bytes_);
if (Bytes > 1'048'576) {
Unit = "mb";
Bytes /= 1'048'576;
} else if (Bytes > 1024) {
Unit = "kb";
Bytes /= 1024;
}

return std::make_pair(Bytes, Unit);
}

[[nodiscard]] std::pair<double, const char *> NumberToHuman(const double N_) {
const char *Unit = "";
double N = N_;
if (N > 1'000'000) {
Unit = "m";
N /= 1'000'000;
} else if (N > 1000) {
Unit = "k";
N /= 1000;
}

return std::make_pair(N, Unit);
}

[[nodiscard]] std::string_view
ExceptionCodeToStr(const uint32_t ExceptionCode) {
switch (ExceptionCode) {
Expand Down
27 changes: 0 additions & 27 deletions src/wtf/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,33 +255,6 @@ ParseCovFiles(const Backend_t &Backend, const fs::path &CovBreakpointFile);
[[nodiscard]] std::optional<bool>
SaveFile(const fs::path &Path, const uint8_t *Buffer, const size_t BufferSize);

//
// Utility that calculates the number of seconds sine a time point.
//

[[nodiscard]] chrono::seconds
SecondsSince(const chrono::system_clock::time_point &Since);

//
// Utility that is used to print seconds for human.
//

[[nodiscard]] std::pair<double, const char *>
SecondsToHuman(const chrono::seconds &Seconds);

//
// Utility that is used to print bytes for human.
//

[[nodiscard]] std::pair<double, const char *>
BytesToHuman(const uint64_t Bytes_);

//
// Utility that is used to print numbers for human.
//

[[nodiscard]] std::pair<double, const char *> NumberToHuman(const double N);

//
// Utility to convert an exception code to a string.
//
Expand Down