Skip to content

Commit

Permalink
Clean up serialization interface of Statistics class (#413)
Browse files Browse the repository at this point in the history
This is the last PR in series of PRs that refactored the Statistics
class serialization.

The other PRs are #384 and #382. Closes #361.
  • Loading branch information
phate authored Feb 29, 2024
1 parent ff3bc66 commit 8c4c301
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
22 changes: 10 additions & 12 deletions jlm/util/Statistics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,19 @@ Statistics::GetSourceFile() const
}

std::string
Statistics::ToString() const
{
return util::strfmt(GetName(), " ", GetSourceFile().to_str(), " ", Serialize());
}

std::string
Statistics::Serialize() const
Statistics::Serialize(char fieldSeparator, char nameValueSeparator) const
{
std::ostringstream ss;

ss << GetName() << fieldSeparator;
ss << GetSourceFile().to_str();

for (const auto & [mName, measurement] : Measurements_)
{
if (ss.tellp() != 0)
ss << " ";
ss << fieldSeparator;

ss << mName << ":";
ss << mName << nameValueSeparator;
std::visit(
[&](const auto & value)
{
Expand All @@ -89,9 +87,9 @@ Statistics::Serialize() const
for (const auto & [mName, timer] : Timers_)
{
if (ss.tellp() != 0)
ss << " ";
ss << fieldSeparator;

ss << mName << "[ns]:" << timer.ns();
ss << mName << "[ns]" << nameValueSeparator << timer.ns();
}

return ss.str();
Expand Down Expand Up @@ -177,7 +175,7 @@ StatisticsCollector::PrintStatistics() const

for (auto & statistics : CollectedStatistics())
{
fprintf(file.fd(), "%s\n", statistics.ToString().c_str());
fprintf(file.fd(), "%s\n", statistics.Serialize(' ', ':').c_str());
}

file.close();
Expand Down
20 changes: 9 additions & 11 deletions jlm/util/Statistics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,17 @@ class Statistics
GetSourceFile() const;

/**
* Converts the Statistics instance to a string containing all information it has
* @return a full serialized description of the Statistic instance
*/
[[nodiscard]] virtual std::string
ToString() const;

/**
* Creates a string containing all measurements and timers.
* Converts the Statistics instance to a string containing all information it has.
* Requires all timers to be stopped.
* @return the created string
*
* @param fieldSeparator Separation character used between different measurements and/or timers.
* @param nameValueSeparator Separation character used between the name and value of a measurement
* or timer.
*
* @return a full serialized description of the Statistic instance
*/
[[nodiscard]] virtual std::string
Serialize() const;
[[nodiscard]] std::string
Serialize(char fieldSeparator, char nameValueSeparator) const;

/**
* Checks if a measurement with the given \p name exists.
Expand Down

0 comments on commit 8c4c301

Please sign in to comment.