-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15506 from ethereum/profiler-probe
Generalize optimizer step profiler
- Loading branch information
Showing
5 changed files
with
188 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
#include <libsolutil/Profiler.h> | ||
|
||
#include <fmt/format.h> | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std::chrono; | ||
using namespace solidity; | ||
|
||
#ifdef PROFILE_OPTIMIZER_STEPS | ||
|
||
util::Profiler::Probe::Probe(std::string _scopeName): | ||
m_scopeName(std::move(_scopeName)), | ||
m_startTime(steady_clock::now()) | ||
{ | ||
} | ||
|
||
util::Profiler::Probe::~Probe() | ||
{ | ||
steady_clock::time_point endTime = steady_clock::now(); | ||
|
||
auto [metricsIt, inserted] = Profiler::singleton().m_metrics.try_emplace(m_scopeName, Metrics{0us, 0}); | ||
metricsIt->second.durationInMicroseconds += duration_cast<microseconds>(endTime - m_startTime); | ||
++metricsIt->second.callCount; | ||
} | ||
|
||
util::Profiler::~Profiler() | ||
{ | ||
outputPerformanceMetrics(); | ||
} | ||
|
||
util::Profiler& util::Profiler::singleton() | ||
{ | ||
static Profiler profiler; | ||
return profiler; | ||
} | ||
|
||
void util::Profiler::outputPerformanceMetrics() | ||
{ | ||
std::vector<std::pair<std::string, Metrics>> sortedMetrics(m_metrics.begin(), m_metrics.end()); | ||
std::sort( | ||
sortedMetrics.begin(), | ||
sortedMetrics.end(), | ||
[](std::pair<std::string, Metrics> const& _lhs, std::pair<std::string, Metrics> const& _rhs) -> bool | ||
{ | ||
return _lhs.second.durationInMicroseconds < _rhs.second.durationInMicroseconds; | ||
} | ||
); | ||
|
||
std::chrono::microseconds totalDurationInMicroseconds = 0us; | ||
size_t totalCallCount = 0; | ||
for (auto&& [scopeName, scopeMetrics]: sortedMetrics) | ||
{ | ||
totalDurationInMicroseconds += scopeMetrics.durationInMicroseconds; | ||
totalCallCount += scopeMetrics.callCount; | ||
} | ||
|
||
std::cerr << "PERFORMANCE METRICS FOR PROFILED SCOPES\n\n"; | ||
std::cerr << "| Time % | Time | Calls | Scope |\n"; | ||
std::cerr << "|-------:|-----------:|--------:|--------------------------------|\n"; | ||
|
||
double totalDurationInSeconds = duration_cast<duration<double>>(totalDurationInMicroseconds).count(); | ||
for (auto&& [scopeName, scopeMetrics]: sortedMetrics) | ||
{ | ||
double durationInSeconds = duration_cast<duration<double>>(scopeMetrics.durationInMicroseconds).count(); | ||
double percentage = 100.0 * durationInSeconds / totalDurationInSeconds; | ||
std::cerr << fmt::format( | ||
"| {:5.1f}% | {:8.3f} s | {:7} | {:30} |\n", | ||
percentage, | ||
durationInSeconds, | ||
scopeMetrics.callCount, | ||
scopeName | ||
); | ||
} | ||
std::cerr << fmt::format("| {:5.1f}% | {:8.3f} s | {:7} | {:30} |\n", 100.0, totalDurationInSeconds, totalCallCount, "**TOTAL**"); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
#pragma once | ||
|
||
#include <chrono> | ||
#include <map> | ||
#include <optional> | ||
#include <string> | ||
|
||
#ifdef PROFILE_OPTIMIZER_STEPS | ||
#define PROFILER_PROBE(_scopeName, _variable) solidity::util::Profiler::Probe _variable(_scopeName); | ||
#else | ||
#define PROFILER_PROBE(_scopeName, _variable) void(0); | ||
#endif | ||
|
||
namespace solidity::util | ||
{ | ||
|
||
#ifdef PROFILE_OPTIMIZER_STEPS | ||
|
||
/// Simpler profiler class that gathers metrics during program execution and prints them out on exit. | ||
/// | ||
/// To gather metrics, create a Probe instance and let it live until the end of the scope. | ||
/// The probe will register its creation and destruction time and store the results in the profiler | ||
/// singleton. | ||
/// | ||
/// Use the PROFILER_PROBE macro to create probes conditionally, in a way that will not affect performance | ||
/// unless profiling is enabled at compilation time via PROFILE_OPTIMIZER_STEPS CMake option. | ||
/// | ||
/// Scopes are identified by the name supplied to the probe. Using the same name multiple times | ||
/// will result in metrics for those scopes being aggregated together as if they were the same scope. | ||
class Profiler | ||
{ | ||
public: | ||
class Probe | ||
{ | ||
public: | ||
Probe(std::string _scopeName); | ||
~Probe(); | ||
|
||
private: | ||
std::string m_scopeName; | ||
std::chrono::steady_clock::time_point m_startTime; | ||
}; | ||
|
||
static Profiler& singleton(); | ||
|
||
private: | ||
~Profiler(); | ||
|
||
struct Metrics | ||
{ | ||
std::chrono::microseconds durationInMicroseconds; | ||
size_t callCount; | ||
}; | ||
|
||
/// Summarizes gathered metric and prints a report to standard error output. | ||
void outputPerformanceMetrics(); | ||
|
||
std::map<std::string, Metrics> m_metrics; | ||
}; | ||
|
||
#endif | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters