-
Notifications
You must be signed in to change notification settings - Fork 0
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
13 changed files
with
278 additions
and
48 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,50 @@ | ||
#include <Common/Profiler.hpp> | ||
|
||
namespace shkyera { | ||
|
||
ProfileBlock& ProfileBlock::operator+=(const ProfileBlock& other) | ||
{ | ||
totalLengthInNanoSeconds += other.totalLengthInNanoSeconds; | ||
numberOfCalls += other.numberOfCalls; | ||
return *this; | ||
} | ||
|
||
ProfileGuard::ProfileGuard(std::string&& name) : mProfileName(std::move(name)), mStartTime(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch())) {} | ||
|
||
ProfileGuard::~ProfileGuard() | ||
{ | ||
const auto endTime = std::chrono::high_resolution_clock::now(); | ||
const auto guardDuration = endTime - mStartTime; | ||
const auto guardDurationInNanoSeconds = std::chrono::duration_cast<std::chrono::nanoseconds>(guardDuration); | ||
Profiler::getInstance().addBlock(std::move(mProfileName), guardDurationInNanoSeconds); | ||
} | ||
|
||
Profiler& Profiler::getInstance() | ||
{ | ||
static Profiler p; | ||
return p; | ||
} | ||
|
||
void Profiler::clear() | ||
{ | ||
std::unique_lock lock(mMutex); | ||
mProfileBlocks.clear(); | ||
} | ||
|
||
void Profiler::addBlock(std::string&& name, std::chrono::nanoseconds time) | ||
{ | ||
const auto threadId = std::this_thread::get_id(); | ||
ProfileBlock newBlock { .totalLengthInNanoSeconds = static_cast<double>(time.count()), .numberOfCalls = 1 }; | ||
|
||
std::unique_lock lock(mMutex); | ||
mProfileBlocks[threadId][name] += newBlock; | ||
} | ||
|
||
Profiler::BlocksPerThread Profiler::getProfiles() | ||
{ | ||
std::unique_lock lock(mMutex); | ||
return mProfileBlocks; | ||
} | ||
|
||
|
||
} |
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,52 @@ | ||
#pragma once | ||
|
||
#include <chrono> | ||
#include <thread> | ||
#include <mutex> | ||
#include <map> | ||
#include <string> | ||
|
||
namespace shkyera { | ||
|
||
struct ProfileBlock { | ||
double totalLengthInNanoSeconds = 0; | ||
size_t numberOfCalls = 0; | ||
|
||
ProfileBlock& operator+=(const ProfileBlock& other); | ||
}; | ||
|
||
class ProfileGuard { | ||
public: | ||
ProfileGuard(std::string&& name); | ||
~ProfileGuard(); | ||
|
||
private: | ||
std::string mProfileName; | ||
std::chrono::high_resolution_clock::time_point mStartTime; | ||
}; | ||
|
||
class Profiler { | ||
public: | ||
using BlocksPerThread = std::map<std::thread::id, std::map<std::string, ProfileBlock>>; | ||
static Profiler& getInstance(); | ||
|
||
void clear(); | ||
void addBlock(std::string&& name, std::chrono::nanoseconds time); | ||
BlocksPerThread getProfiles(); | ||
|
||
private: | ||
Profiler() = default; | ||
|
||
std::mutex mMutex; | ||
BlocksPerThread mProfileBlocks; | ||
}; | ||
|
||
#define SHKYERA_PROFILE(name) ProfileGuard __SHKYERA_UNIQUE_NAME(__LINE__) (name) | ||
#define SHKYERA_READ_PROFILE Profiler::getInstance().getProfiles() | ||
#define SHKYERA_CLEAR_PROFILE Profiler::getInstance().clear() | ||
|
||
#define __SHKYERA_UNIQUE_NAME(LINE) __SHKYERA_CONCAT(profileGuard_, LINE) | ||
#define __SHKYERA_CONCAT(X, Y) __SHKYERA_CONCAT_IMPL(X, Y) | ||
#define __SHKYERA_CONCAT_IMPL(X, Y) X##Y | ||
|
||
} |
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 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.