-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
132 additions
and
122 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 was deleted.
Oops, something went wrong.
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,48 @@ | ||
#ifndef _PAREAS_PROFILER_PROFILER_HPP | ||
#define _PAREAS_PROFILER_PROFILER_HPP | ||
|
||
#include <iosfwd> | ||
#include <chrono> | ||
#include <vector> | ||
#include <functional> | ||
|
||
namespace pareas { | ||
struct Profiler { | ||
using SyncCallback = std::function<void()>; | ||
|
||
using Clock = std::chrono::high_resolution_clock; | ||
|
||
struct HistoryEntry { | ||
unsigned level; | ||
const char* name; | ||
Clock::duration elapsed; | ||
}; | ||
|
||
unsigned max_level; | ||
unsigned level; | ||
|
||
SyncCallback sync_callback; | ||
std::vector<Clock::time_point> starts; | ||
std::vector<HistoryEntry> history; | ||
|
||
Profiler(unsigned max_level); | ||
|
||
void set_sync_callback(SyncCallback sync_callback = null_callback); | ||
|
||
void begin(); | ||
void end(const char* name); | ||
|
||
void dump(std::ostream& os); | ||
|
||
template <typename F> | ||
void measure(const char* name, F f) { | ||
this->begin(); | ||
f(); | ||
this->end(name); | ||
} | ||
|
||
static void null_callback() {} | ||
}; | ||
} | ||
|
||
#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
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 was deleted.
Oops, something went wrong.
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,71 @@ | ||
#include "pareas/profiler/profiler.hpp" | ||
|
||
#include <fmt/ostream.h> | ||
#include <fmt/chrono.h> | ||
|
||
#include <cstddef> | ||
#include <cassert> | ||
|
||
namespace pareas { | ||
Profiler::Profiler(unsigned max_level): | ||
max_level(max_level), | ||
level(0), | ||
sync_callback(null_callback) { | ||
} | ||
|
||
void Profiler::set_sync_callback(SyncCallback sync_callback) { | ||
this->sync_callback = sync_callback; | ||
} | ||
|
||
void Profiler::begin() { | ||
++this->level; | ||
if (this->level > this->max_level) | ||
return; | ||
|
||
this->sync_callback(); | ||
|
||
auto start = Clock::now(); | ||
this->starts.push_back(start); | ||
} | ||
|
||
void Profiler::end(const char* name) { | ||
--this->level; | ||
if (this->level >= this->max_level) | ||
return; | ||
|
||
this->sync_callback(); | ||
|
||
auto end = Clock::now(); | ||
auto start = this->starts.back(); | ||
this->starts.pop_back(); | ||
auto diff = end - start; | ||
this->history.push_back(HistoryEntry{this->level, name, diff}); | ||
} | ||
|
||
void Profiler::dump(std::ostream& os) { | ||
assert(this->level == 0); | ||
|
||
auto ordered_history = std::vector<HistoryEntry>(this->history.size()); | ||
auto level_index_stack = std::vector<size_t>(); | ||
|
||
size_t j = 0; | ||
for (const auto& entry : this->history) { | ||
for (unsigned i = level_index_stack.size(); i <= entry.level; ++i) { | ||
level_index_stack.push_back(j++); | ||
} | ||
|
||
size_t index = level_index_stack.back(); | ||
ordered_history[index] = entry; | ||
level_index_stack.pop_back(); | ||
} | ||
|
||
auto name_stack = std::vector<const char*>(); | ||
for (auto [level, name, elapsed] : ordered_history) { | ||
name_stack.resize(level); | ||
name_stack.push_back(name); | ||
|
||
auto us = std::chrono::duration_cast<std::chrono::microseconds>(elapsed); | ||
fmt::print("{}: {}\n", fmt::join(name_stack, "."), us); | ||
} | ||
} | ||
} |