1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-23 02:38:52 -05:00

Added in instrumentation classes for generating json files. Will allow for more complete profiling (at least as a start).

This commit is contained in:
Gordon McCann 2022-05-08 15:51:17 -04:00
parent 6cb8536c3c
commit d789a5d6d5
24 changed files with 356 additions and 8 deletions

1
.gitignore vendored
View File

@ -9,6 +9,7 @@ Makefile
*.sln *.sln
*.vcxproj *.vcxproj
*.vcxproj.filters *.vcxproj.filters
*.json
Navigator/Makefile Navigator/Makefile
NavProject/Makefile NavProject/Makefile

View File

@ -14,6 +14,7 @@
*/ */
MassMap::MassMap() MassMap::MassMap()
{ {
NAV_PROFILE_FUNCTION();
std::ifstream massfile("Assets/amdc2016_mass.txt"); std::ifstream massfile("Assets/amdc2016_mass.txt");
if (massfile.is_open()) if (massfile.is_open())
{ {
@ -42,6 +43,7 @@ MassMap::~MassMap() {}
//Returns nuclear mass in MeV //Returns nuclear mass in MeV
double MassMap::FindMass(int Z, int A) double MassMap::FindMass(int Z, int A)
{ {
NAV_PROFILE_FUNCTION();
std::string key = "(" + std::to_string(Z) + "," + std::to_string(A) + ")"; std::string key = "(" + std::to_string(Z) + "," + std::to_string(A) + ")";
auto data = massTable.find(key); auto data = massTable.find(key);
if (data == massTable.end()) if (data == massTable.end())
@ -55,6 +57,7 @@ double MassMap::FindMass(int Z, int A)
//returns element symbol //returns element symbol
std::string MassMap::FindSymbol(int Z, int A) std::string MassMap::FindSymbol(int Z, int A)
{ {
NAV_PROFILE_FUNCTION();
auto data = elementTable.find(Z); auto data = elementTable.find(Z);
if (data == elementTable.end()) if (data == elementTable.end())
{ {

View File

@ -13,6 +13,7 @@ namespace Navigator {
AnalysisStage("SPSAnalysis"), delayFLTime("delayFLTime"), delayFRTime("delayFRTime"), delayBLTime("delayBLTime"), delayBRTime("delayBRTime"), x1("x1"), x2("x2"), xavg("xavg"), AnalysisStage("SPSAnalysis"), delayFLTime("delayFLTime"), delayFRTime("delayFRTime"), delayBLTime("delayBLTime"), delayBRTime("delayBRTime"), x1("x1"), x2("x2"), xavg("xavg"),
scintLeft("scintLeft"), anodeBack("anodeBack"), cathode("cathode"), xavg_sabreCoinc("xavg_sabreCoinc"), x1_weight("x1_weight"), x2_weight("x2_weight") scintLeft("scintLeft"), anodeBack("anodeBack"), cathode("cathode"), xavg_sabreCoinc("xavg_sabreCoinc"), x1_weight("x1_weight"), x2_weight("x2_weight")
{ {
NAV_PROFILE_FUNCTION();
SpectrumManager& manager = SpectrumManager::GetInstance(); SpectrumManager& manager = SpectrumManager::GetInstance();
manager.BindParameter(delayFLTime); manager.BindParameter(delayFLTime);
manager.BindParameter(delayFRTime); manager.BindParameter(delayFRTime);
@ -49,6 +50,7 @@ namespace Navigator {
void SPSAnalysisStage::AnalyzePhysicsEvent(const NavEvent& event) void SPSAnalysisStage::AnalyzePhysicsEvent(const NavEvent& event)
{ {
NAV_PROFILE_FUNCTION();
//Most analysis stages will start kinda like this. Take the raw event data and //Most analysis stages will start kinda like this. Take the raw event data and
//put it into NavParameters using the hit id. Switches are perfect for this. Can also //put it into NavParameters using the hit id. Switches are perfect for this. Can also
//create mapping classes to use text-file-based id association (commonly called channel maps). //create mapping classes to use text-file-based id association (commonly called channel maps).

View File

@ -45,6 +45,7 @@ namespace Navigator {
void SPSInputLayer::OnImGuiRender() void SPSInputLayer::OnImGuiRender()
{ {
NAV_PROFILE_FUNCTION();
if (ImGui::Begin("SPS Input")) if (ImGui::Begin("SPS Input"))
{ {
//Create widgets for all of our inputs //Create widgets for all of our inputs
@ -73,6 +74,7 @@ namespace Navigator {
void SPSInputLayer::UpdateWeights() void SPSInputLayer::UpdateWeights()
{ {
NAV_PROFILE_FUNCTION();
m_rxnEqn = ""; //reset m_rxnEqn = ""; //reset
//Calculate residual nucleus from reaction //Calculate residual nucleus from reaction

View File

@ -30,9 +30,15 @@ int main(int argc, const char** argv)
Navigator::Logger::Init(); Navigator::Logger::Init();
NAV_TRACE("Logger Initialized!"); NAV_TRACE("Logger Initialized!");
NAV_PROFILE_BEGIN_SESSION("Startup", "navprofile_startup.json");
auto app = Navigator::CreateApplication(); auto app = Navigator::CreateApplication();
NAV_PROFILE_END_SESSION();
NAV_PROFILE_BEGIN_SESSION("Runtime", "navprofile_runtime.json");
app->Run(); app->Run();
NAV_PROFILE_END_SESSION();
NAV_PROFILE_BEGIN_SESSION("Shutdown", "navprofile_shutdown.json");
delete app; delete app;
NAV_PROFILE_END_SESSION();
} }

View File

@ -33,5 +33,6 @@
#include "Navigator/Core/Layer.h" #include "Navigator/Core/Layer.h"
#include "Navigator/Events/Event.h" #include "Navigator/Events/Event.h"
#include "Navigator/Utils/TestServerLayer.h" #include "Navigator/Utils/TestServerLayer.h"
#include "Navigator/Utils/Instrumentor.h"
#endif #endif

View File

@ -14,11 +14,14 @@
namespace Navigator { namespace Navigator {
Application* Application::s_instance = nullptr; Application* Application::s_instance = nullptr;
Application::Application() : Application::Application() :
m_runFlag(true) m_runFlag(true)
{ {
NAV_PROFILE_FUNCTION();
s_instance = this; s_instance = this;
m_window = std::unique_ptr<Window>(Window::Create()); m_window = std::unique_ptr<Window>(Window::Create());
@ -39,6 +42,7 @@ namespace Navigator {
void Application::OnEvent(Event& event) void Application::OnEvent(Event& event)
{ {
NAV_PROFILE_FUNCTION();
EventDispatcher dispatch(event); EventDispatcher dispatch(event);
dispatch.Dispatch<WindowCloseEvent>(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent)); dispatch.Dispatch<WindowCloseEvent>(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent));
for(auto iter = m_stack.end(); iter != m_stack.begin(); ) for(auto iter = m_stack.end(); iter != m_stack.begin(); )
@ -58,18 +62,21 @@ namespace Navigator {
void Application::PushLayer(Layer* layer) void Application::PushLayer(Layer* layer)
{ {
NAV_PROFILE_FUNCTION();
m_stack.PushLayer(layer); m_stack.PushLayer(layer);
layer->OnAttach(); layer->OnAttach();
} }
void Application::PushOverlay(Layer* layer) void Application::PushOverlay(Layer* layer)
{ {
NAV_PROFILE_FUNCTION();
m_stack.PushOverlay(layer); m_stack.PushOverlay(layer);
layer->OnAttach(); layer->OnAttach();
} }
void Application::Run() void Application::Run()
{ {
NAV_PROFILE_FUNCTION();
while(m_runFlag) while(m_runFlag)
{ {
RenderCommand::SetClearColor(m_bckgnd_color); RenderCommand::SetClearColor(m_bckgnd_color);

View File

@ -21,6 +21,7 @@ namespace Navigator {
std::string ConvertCutTypeToString(CutType type) std::string ConvertCutTypeToString(CutType type)
{ {
NAV_PROFILE_FUNCTION();
switch(type) switch(type)
{ {
case CutType::Cut1D: return "Cut1D"; case CutType::Cut1D: return "Cut1D";
@ -43,12 +44,14 @@ namespace Navigator {
void Cut1D::IsInside(double x, double y) void Cut1D::IsInside(double x, double y)
{ {
NAV_PROFILE_FUNCTION();
m_isValid = x >= m_minVal && x <= m_maxVal; m_isValid = x >= m_minVal && x <= m_maxVal;
} }
//Only within an ImPlot/ImGui context!!! //Only within an ImPlot/ImGui context!!!
void Cut1D::Draw() const void Cut1D::Draw() const
{ {
NAV_PROFILE_FUNCTION();
double points[2] = { m_minVal, m_maxVal }; double points[2] = { m_minVal, m_maxVal };
ImPlot::PlotVLines(m_params.name.c_str(), points, 2); ImPlot::PlotVLines(m_params.name.c_str(), points, 2);
} }
@ -71,7 +74,8 @@ namespace Navigator {
*/ */
void Cut2D::IsInside(double x, double y) void Cut2D::IsInside(double x, double y)
{ {
m_isValid = false; NAV_PROFILE_FUNCTION();
m_isValid = false;
double slope; double slope;
for(size_t i=0; i<(m_xpoints.size()-1); i++) for(size_t i=0; i<(m_xpoints.size()-1); i++)
{ {
@ -99,7 +103,8 @@ namespace Navigator {
//Only in ImPlot/ImGui context!!!! //Only in ImPlot/ImGui context!!!!
void Cut2D::Draw() const void Cut2D::Draw() const
{ {
ImPlot::PlotLine(m_params.name.c_str(), m_xpoints.data(), m_ypoints.data(), (int)m_xpoints.size()); NAV_PROFILE_FUNCTION();
ImPlot::PlotLine(m_params.name.c_str(), m_xpoints.data(), m_ypoints.data(), (int)m_xpoints.size());
} }
/*CutSummaryAll -- Can only be made on a HistogramSummary but can be applied to any*/ /*CutSummaryAll -- Can only be made on a HistogramSummary but can be applied to any*/
@ -114,12 +119,14 @@ namespace Navigator {
void CutSummary::IsInside(double x, double y) void CutSummary::IsInside(double x, double y)
{ {
NAV_PROFILE_FUNCTION();
m_isValid = x >= m_minVal && x <= m_maxVal; m_isValid = x >= m_minVal && x <= m_maxVal;
} }
//Only within an ImPlot/ImGui context!!! //Only within an ImPlot/ImGui context!!!
void CutSummary::Draw() const void CutSummary::Draw() const
{ {
NAV_PROFILE_FUNCTION();
double points[2] = { m_minVal, m_maxVal }; double points[2] = { m_minVal, m_maxVal };
ImPlot::PlotVLines(m_params.name.c_str(), points, 2); ImPlot::PlotVLines(m_params.name.c_str(), points, 2);
} }

View File

@ -33,6 +33,7 @@ namespace Navigator {
std::string ConvertSpectrumTypeToString(SpectrumType type) std::string ConvertSpectrumTypeToString(SpectrumType type)
{ {
NAV_PROFILE_FUNCTION();
switch (type) switch (type)
{ {
case SpectrumType::Histo1D: return "Histogram1D"; case SpectrumType::Histo1D: return "Histogram1D";
@ -56,6 +57,7 @@ namespace Navigator {
void Histogram1D::InitBins() void Histogram1D::InitBins()
{ {
NAV_PROFILE_FUNCTION();
m_params.type = SpectrumType::Histo1D; m_params.type = SpectrumType::Histo1D;
if(m_params.nbins_x == 0 || (m_params.min_x >= m_params.max_x)) if(m_params.nbins_x == 0 || (m_params.min_x >= m_params.max_x))
{ {
@ -81,6 +83,7 @@ namespace Navigator {
//Note: only x is used here, y is simply present to maintain compliance with 2D case and can be ignored //Note: only x is used here, y is simply present to maintain compliance with 2D case and can be ignored
void Histogram1D::FillData(double x, double y) void Histogram1D::FillData(double x, double y)
{ {
NAV_PROFILE_FUNCTION();
if (x < m_params.min_x || x >= m_params.max_x) if (x < m_params.min_x || x >= m_params.max_x)
return; return;
int bin = int((x - m_params.min_x)/(m_binWidth)); int bin = int((x - m_params.min_x)/(m_binWidth));
@ -90,6 +93,7 @@ namespace Navigator {
//Can only be used within an ImGui / ImPlot context!! //Can only be used within an ImGui / ImPlot context!!
void Histogram1D::Draw() void Histogram1D::Draw()
{ {
NAV_PROFILE_FUNCTION();
ImPlot::SetupAxes(m_params.x_par.c_str(), "Counts",0, ImPlotAxisFlags_LockMin | ImPlotAxisFlags_AutoFit); ImPlot::SetupAxes(m_params.x_par.c_str(), "Counts",0, ImPlotAxisFlags_LockMin | ImPlotAxisFlags_AutoFit);
ImPlot::PlotBars(m_params.name.c_str(), &m_binCenters.data()[0], &m_binCounts.data()[0], m_params.nbins_x, m_binWidth); ImPlot::PlotBars(m_params.name.c_str(), &m_binCenters.data()[0], &m_binCounts.data()[0], m_params.nbins_x, m_binWidth);
} }
@ -103,6 +107,7 @@ namespace Navigator {
//Again here yvalues can be ignored, only for compliance //Again here yvalues can be ignored, only for compliance
StatResults Histogram1D::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max) StatResults Histogram1D::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max)
{ {
NAV_PROFILE_FUNCTION();
int bin_min, bin_max; int bin_min, bin_max;
StatResults results; StatResults results;
@ -148,6 +153,7 @@ namespace Navigator {
void Histogram2D::InitBins() void Histogram2D::InitBins()
{ {
NAV_PROFILE_FUNCTION();
m_params.type = SpectrumType::Histo2D; m_params.type = SpectrumType::Histo2D;
if(m_params.nbins_x <= 0 || m_params.nbins_y <= 0 || m_params.min_x >= m_params.max_x || m_params.min_y >= m_params.max_y) if(m_params.nbins_x <= 0 || m_params.nbins_y <= 0 || m_params.min_x >= m_params.max_x || m_params.min_y >= m_params.max_y)
{ {
@ -172,6 +178,7 @@ namespace Navigator {
void Histogram2D::FillData(double x, double y) void Histogram2D::FillData(double x, double y)
{ {
NAV_PROFILE_FUNCTION();
if (x < m_params.min_x || x >= m_params.max_x || y <= m_params.min_y || y > m_params.max_y) if (x < m_params.min_x || x >= m_params.max_x || y <= m_params.min_y || y > m_params.max_y)
return; return;
int bin_x = int((x - m_params.min_x)/m_binWidthX); int bin_x = int((x - m_params.min_x)/m_binWidthX);
@ -192,6 +199,7 @@ namespace Navigator {
*/ */
void Histogram2D::Draw() void Histogram2D::Draw()
{ {
NAV_PROFILE_FUNCTION();
ImPlot::SetupAxes(m_params.x_par.c_str(), m_params.y_par.c_str()); ImPlot::SetupAxes(m_params.x_par.c_str(), m_params.y_par.c_str());
ImPlot::PushColormap(ImPlotColormap_Viridis); ImPlot::PushColormap(ImPlotColormap_Viridis);
ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, m_colorScaleRange[0], m_colorScaleRange[1], NULL, ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, m_colorScaleRange[0], m_colorScaleRange[1], NULL,
@ -208,6 +216,7 @@ namespace Navigator {
StatResults Histogram2D::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max) StatResults Histogram2D::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max)
{ {
NAV_PROFILE_FUNCTION();
int xbin_min, xbin_max, ybin_min, ybin_max; int xbin_min, xbin_max, ybin_min, ybin_max;
int curbin; int curbin;
@ -294,6 +303,7 @@ namespace Navigator {
void HistogramSummary::InitBins() void HistogramSummary::InitBins()
{ {
NAV_PROFILE_FUNCTION();
m_params.type = SpectrumType::Summary; m_params.type = SpectrumType::Summary;
if (m_params.nbins_x <= 0 || m_params.min_x >= m_params.max_x) if (m_params.nbins_x <= 0 || m_params.min_x >= m_params.max_x)
{ {
@ -322,6 +332,7 @@ namespace Navigator {
void HistogramSummary::FillData(double x, double y) void HistogramSummary::FillData(double x, double y)
{ {
NAV_PROFILE_FUNCTION();
if (x < m_params.min_x || x >= m_params.max_x || y <= m_params.min_y || y > m_params.max_y) if (x < m_params.min_x || x >= m_params.max_x || y <= m_params.min_y || y > m_params.max_y)
return; return;
int bin_x = int((x - m_params.min_x) / m_binWidthX); int bin_x = int((x - m_params.min_x) / m_binWidthX);
@ -333,6 +344,7 @@ namespace Navigator {
void HistogramSummary::Draw() void HistogramSummary::Draw()
{ {
NAV_PROFILE_FUNCTION();
ImPlot::SetupAxisTicks(ImAxis_Y1, m_params.min_y, m_params.max_y, m_params.nbins_y, m_labels, false); ImPlot::SetupAxisTicks(ImAxis_Y1, m_params.min_y, m_params.max_y, m_params.nbins_y, m_labels, false);
ImPlot::PushColormap(ImPlotColormap_Viridis); ImPlot::PushColormap(ImPlotColormap_Viridis);
ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, m_colorScaleRange[0], m_colorScaleRange[1], NULL, ImPlot::PlotHeatmap(m_params.name.c_str(), &m_binCounts.data()[0], m_params.nbins_y, m_params.nbins_x, m_colorScaleRange[0], m_colorScaleRange[1], NULL,
@ -348,6 +360,7 @@ namespace Navigator {
StatResults HistogramSummary::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max) StatResults HistogramSummary::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max)
{ {
NAV_PROFILE_FUNCTION();
int xbin_min, xbin_max, ybin_min, ybin_max; int xbin_min, xbin_max, ybin_min, ybin_max;
int curbin; int curbin;

View File

@ -28,6 +28,7 @@ namespace Navigator {
void SpectrumManager::AddHistogram(const HistogramArgs& params) void SpectrumManager::AddHistogram(const HistogramArgs& params)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
if (params.y_par == "None") //Check dimensionality if (params.y_par == "None") //Check dimensionality
m_histoMap[params.name].reset(new Histogram1D(params)); m_histoMap[params.name].reset(new Histogram1D(params));
@ -37,18 +38,21 @@ namespace Navigator {
void SpectrumManager::AddHistogramSummary(const HistogramArgs& params, const std::vector<std::string>& subhistos) void SpectrumManager::AddHistogramSummary(const HistogramArgs& params, const std::vector<std::string>& subhistos)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_histoMap[params.name].reset(new HistogramSummary(params, subhistos)); m_histoMap[params.name].reset(new HistogramSummary(params, subhistos));
} }
void SpectrumManager::RemoveHistogram(const std::string& name) void SpectrumManager::RemoveHistogram(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_histoMap.erase(name); m_histoMap.erase(name);
} }
void SpectrumManager::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname) void SpectrumManager::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(histoname); auto iter = m_histoMap.find(histoname);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
@ -57,6 +61,7 @@ namespace Navigator {
void SpectrumManager::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname) void SpectrumManager::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(histoname); auto iter = m_histoMap.find(histoname);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
@ -66,6 +71,7 @@ namespace Navigator {
//Use this to fill histograms. Currently can only be filled in bulk; maybe a use case for individual fills? //Use this to fill histograms. Currently can only be filled in bulk; maybe a use case for individual fills?
void SpectrumManager::UpdateHistograms() void SpectrumManager::UpdateHistograms()
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
//Set state of all cuts for the event //Set state of all cuts for the event
@ -128,6 +134,7 @@ namespace Navigator {
void SpectrumManager::ClearHistograms() void SpectrumManager::ClearHistograms()
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
for (auto& pair : m_histoMap) for (auto& pair : m_histoMap)
pair.second->ClearData(); pair.second->ClearData();
@ -135,6 +142,7 @@ namespace Navigator {
void SpectrumManager::ClearHistogram(const std::string& name) void SpectrumManager::ClearHistogram(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
@ -143,6 +151,7 @@ namespace Navigator {
void SpectrumManager::DrawHistogram(const std::string& name) void SpectrumManager::DrawHistogram(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
@ -155,6 +164,7 @@ namespace Navigator {
const HistogramArgs& SpectrumManager::GetHistogramParams(const std::string& name) const HistogramArgs& SpectrumManager::GetHistogramParams(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
@ -166,6 +176,7 @@ namespace Navigator {
//For 2D spectra, we want to allow zooming along the z-axis (color) //For 2D spectra, we want to allow zooming along the z-axis (color)
float* SpectrumManager::GetColorScaleRange(const std::string& name) float* SpectrumManager::GetColorScaleRange(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end()) if (iter != m_histoMap.end())
@ -190,6 +201,7 @@ namespace Navigator {
std::vector<std::string> SpectrumManager::GetSubHistograms(const std::string& name) std::vector<std::string> SpectrumManager::GetSubHistograms(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_histoMap.find(name); auto iter = m_histoMap.find(name);
if (iter != m_histoMap.end() && iter->second->GetType() == SpectrumType::Summary) if (iter != m_histoMap.end() && iter->second->GetType() == SpectrumType::Summary)
@ -233,6 +245,7 @@ namespace Navigator {
//Bind a NavParameter instance to the manager. If the Parameter doesn't exist, make a new one, otherwise attach to extant memory //Bind a NavParameter instance to the manager. If the Parameter doesn't exist, make a new one, otherwise attach to extant memory
void SpectrumManager::BindParameter(NavParameter& param) void SpectrumManager::BindParameter(NavParameter& param)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_paramMap.find(param.GetName()); auto iter = m_paramMap.find(param.GetName());
if (iter == m_paramMap.end()) if (iter == m_paramMap.end())
@ -247,6 +260,7 @@ namespace Navigator {
//Additionally, make a default 1D histogram for the parameter (histogram has same name as parameter) //Additionally, make a default 1D histogram for the parameter (histogram has same name as parameter)
void SpectrumManager::BindParameter(NavParameter& param, int nbins, double minVal, double maxVal) void SpectrumManager::BindParameter(NavParameter& param, int nbins, double minVal, double maxVal)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_paramMap.find(param.GetName()); auto iter = m_paramMap.find(param.GetName());
if (iter == m_paramMap.end()) if (iter == m_paramMap.end())
@ -267,6 +281,7 @@ namespace Navigator {
//Once an analysis pass is done and histograms filled, reset all parameters //Once an analysis pass is done and histograms filled, reset all parameters
void SpectrumManager::InvalidateParameters() void SpectrumManager::InvalidateParameters()
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
for (auto& param : m_paramMap) for (auto& param : m_paramMap)
{ {
@ -295,6 +310,7 @@ namespace Navigator {
void SpectrumManager::BindVariable(NavVariable& var) void SpectrumManager::BindVariable(NavVariable& var)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_varMap.find(var.GetName()); auto iter = m_varMap.find(var.GetName());
if (iter == m_varMap.end()) if (iter == m_varMap.end())
@ -320,6 +336,7 @@ namespace Navigator {
void SpectrumManager::RemoveCut(const std::string& name) void SpectrumManager::RemoveCut(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);
m_cutMap.erase(name); m_cutMap.erase(name);
RemoveCutFromHistograms(name); //Once a cut is gone, remove all references to it. RemoveCutFromHistograms(name); //Once a cut is gone, remove all references to it.
@ -379,6 +396,7 @@ namespace Navigator {
//Can only be called by RemoveCut currently. May be a use case where this should be promoted to public to on the fly mod a gram. //Can only be called by RemoveCut currently. May be a use case where this should be promoted to public to on the fly mod a gram.
void SpectrumManager::RemoveCutFromHistograms(const std::string& cutname) void SpectrumManager::RemoveCutFromHistograms(const std::string& cutname)
{ {
NAV_PROFILE_FUNCTION();
for (auto& gram : m_histoMap) for (auto& gram : m_histoMap)
{ {
auto& params = gram.second->GetParameters(); auto& params = gram.second->GetParameters();
@ -402,6 +420,7 @@ namespace Navigator {
//Obv. only need to draw a cut if its parent histogram is drawn. //Obv. only need to draw a cut if its parent histogram is drawn.
void SpectrumManager::DrawCut(const std::string& name) void SpectrumManager::DrawCut(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
auto iter = m_cutMap.find(name); auto iter = m_cutMap.find(name);
if (iter != m_cutMap.end()) if (iter != m_cutMap.end())
iter->second->Draw(); iter->second->Draw();
@ -410,6 +429,7 @@ namespace Navigator {
//Set the state of the cuts for the current event. Called by the //Set the state of the cuts for the current event. Called by the
void SpectrumManager::CheckCuts() void SpectrumManager::CheckCuts()
{ {
NAV_PROFILE_FUNCTION();
for (auto& iter : m_cutMap) for (auto& iter : m_cutMap)
{ {
const std::string& xpar = iter.second->GetXParameter(); const std::string& xpar = iter.second->GetXParameter();
@ -472,6 +492,7 @@ namespace Navigator {
bool SpectrumManager::IsCutValid(const std::string& name) bool SpectrumManager::IsCutValid(const std::string& name)
{ {
NAV_PROFILE_FUNCTION();
auto iter = m_cutMap.find(name); auto iter = m_cutMap.find(name);
if (iter != m_cutMap.end()) if (iter != m_cutMap.end())
return iter->second->IsValid(); return iter->second->IsValid();

View File

@ -68,6 +68,7 @@ namespace Navigator {
//The main function //The main function
void EditorLayer::OnImGuiRender() void EditorLayer::OnImGuiRender()
{ {
NAV_PROFILE_FUNCTION();
static bool startFlag = true; //first render retrieve base static bool startFlag = true; //first render retrieve base
if(startFlag) if(startFlag)
{ {
@ -276,6 +277,7 @@ namespace Navigator {
void EditorLayer::RemoveHistogramDialog() void EditorLayer::RemoveHistogramDialog()
{ {
NAV_PROFILE_FUNCTION();
static std::string selectedGram = ""; static std::string selectedGram = "";
if (m_removeHistogram) if (m_removeHistogram)
{ {
@ -311,6 +313,7 @@ namespace Navigator {
void EditorLayer::RemoveCutDialog() void EditorLayer::RemoveCutDialog()
{ {
NAV_PROFILE_FUNCTION();
static std::string selectedCut = ""; static std::string selectedCut = "";
if (m_removeCut) if (m_removeCut)
{ {
@ -347,6 +350,7 @@ namespace Navigator {
void EditorLayer::ExportHistogramDialog() void EditorLayer::ExportHistogramDialog()
{ {
NAV_PROFILE_FUNCTION();
static std::string filename = ""; static std::string filename = "";
static HistogramArgs selectedGram = HistogramArgs(); static HistogramArgs selectedGram = HistogramArgs();
if(m_exportHistogram) if(m_exportHistogram)

View File

@ -20,6 +20,7 @@ namespace Navigator {
//Helper function to handle file size printing //Helper function to handle file size printing
std::string ConvertFileSystemSizeToString(std::uintmax_t value) std::string ConvertFileSystemSizeToString(std::uintmax_t value)
{ {
NAV_PROFILE_FUNCTION();
int i = 0; int i = 0;
double mantissa = (double)value; double mantissa = (double)value;
for (; mantissa >= 1024.0; ++i) for (; mantissa >= 1024.0; ++i)
@ -41,6 +42,7 @@ namespace Navigator {
std::pair<std::string, FileDialog::Type> FileDialog::RenderFileDialog(const std::string& ext) std::pair<std::string, FileDialog::Type> FileDialog::RenderFileDialog(const std::string& ext)
{ {
NAV_PROFILE_FUNCTION();
if (m_openDialogFlag) if (m_openDialogFlag)
{ {
m_selectedItem = ""; m_selectedItem = "";
@ -79,6 +81,7 @@ namespace Navigator {
std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext) std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext)
{ {
NAV_PROFILE_FUNCTION();
std::string result = ""; std::string result = "";
std::string text = ""; std::string text = "";
@ -143,6 +146,7 @@ namespace Navigator {
std::string FileDialog::ImGuiRenderSaveFile(const std::string& ext) std::string FileDialog::ImGuiRenderSaveFile(const std::string& ext)
{ {
NAV_PROFILE_FUNCTION();
std::string result = ""; std::string result = "";
std::string text = ""; std::string text = "";
@ -207,6 +211,7 @@ namespace Navigator {
std::string FileDialog::ImGuiRenderOpenDir() std::string FileDialog::ImGuiRenderOpenDir()
{ {
NAV_PROFILE_FUNCTION();
std::string result = ""; std::string result = "";
std::string text = ""; std::string text = "";

View File

@ -27,6 +27,7 @@ namespace Navigator {
void SourceDialog::ImGuiRenderSourceDialog() void SourceDialog::ImGuiRenderSourceDialog()
{ {
NAV_PROFILE_FUNCTION();
static bool onlineFlag = false; static bool onlineFlag = false;
static bool offlineFlag = false; static bool offlineFlag = false;
static std::vector<DataSource::SourceType> availTypes = { DataSource::SourceType::CompassOnline, DataSource::SourceType::CompassOffline }; static std::vector<DataSource::SourceType> availTypes = { DataSource::SourceType::CompassOnline, DataSource::SourceType::CompassOffline };

View File

@ -26,6 +26,7 @@ namespace Navigator {
bool SpectrumDialog::ImGuiRenderSpectrumDialog(const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& cutList, const std::vector<std::string>& paramList) bool SpectrumDialog::ImGuiRenderSpectrumDialog(const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& cutList, const std::vector<std::string>& paramList)
{ {
NAV_PROFILE_FUNCTION();
static std::string selectedCut = ""; static std::string selectedCut = "";
bool result = false; bool result = false;
if (m_openFlag) if (m_openFlag)

View File

@ -15,6 +15,7 @@ namespace Navigator {
//Convert a StatResults struct from analysis to a std::string helper function //Convert a StatResults struct from analysis to a std::string helper function
std::string GenerateStatString(const std::string& name, const StatResults& results, bool is2D = true) std::string GenerateStatString(const std::string& name, const StatResults& results, bool is2D = true)
{ {
NAV_PROFILE_FUNCTION();
std::stringstream stream; std::stringstream stream;
stream << "Region: " << name << "\n" << "Integral: " << results.integral << "\n"; stream << "Region: " << name << "\n" << "Integral: " << results.integral << "\n";
if (results.integral == 0.0) if (results.integral == 0.0)
@ -36,6 +37,7 @@ namespace Navigator {
//Main render function. Handles generating subplot regions as well as the zoomed in region //Main render function. Handles generating subplot regions as well as the zoomed in region
bool SpectrumPanel::OnImGuiRender(const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& cutList, const std::vector<std::string>& paramList) bool SpectrumPanel::OnImGuiRender(const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& cutList, const std::vector<std::string>& paramList)
{ {
NAV_PROFILE_FUNCTION();
static bool acceptCutFlag = false; static bool acceptCutFlag = false;
m_result = false; m_result = false;
if (ImGui::Begin("Active View")) if (ImGui::Begin("Active View"))

View File

@ -32,6 +32,7 @@ namespace Navigator {
void ImGuiLayer::OnAttach() void ImGuiLayer::OnAttach()
{ {
NAV_PROFILE_FUNCTION();
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();
NAV_INFO("Creating ImGui Context..."); NAV_INFO("Creating ImGui Context...");
@ -82,6 +83,7 @@ namespace Navigator {
void ImGuiLayer::OnDetach() void ImGuiLayer::OnDetach()
{ {
NAV_PROFILE_FUNCTION();
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
ImPlot::DestroyContext(); ImPlot::DestroyContext();
@ -90,6 +92,7 @@ namespace Navigator {
void ImGuiLayer::Begin() void ImGuiLayer::Begin()
{ {
NAV_PROFILE_FUNCTION();
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
@ -98,6 +101,7 @@ namespace Navigator {
void ImGuiLayer::End() void ImGuiLayer::End()
{ {
NAV_PROFILE_FUNCTION();
Application& app = Application::Get(); Application& app = Application::Get();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();

View File

@ -47,6 +47,7 @@ namespace Navigator {
void CompassFile::Open(const std::string& filename) void CompassFile::Open(const std::string& filename)
{ {
NAV_PROFILE_FUNCTION();
eofFlag = false; eofFlag = false;
hitUsedFlag = true; hitUsedFlag = true;
m_filename = filename; m_filename = filename;
@ -106,6 +107,7 @@ namespace Navigator {
*/ */
bool CompassFile::GetNextHit() bool CompassFile::GetNextHit()
{ {
NAV_PROFILE_FUNCTION();
if(!IsOpen()) return true; if(!IsOpen()) return true;
if((bufferIter == nullptr || bufferIter == bufferEnd) && !IsEOF()) if((bufferIter == nullptr || bufferIter == bufferEnd) && !IsEOF())
@ -131,7 +133,7 @@ namespace Navigator {
*/ */
void CompassFile::GetNextBuffer() void CompassFile::GetNextBuffer()
{ {
NAV_PROFILE_FUNCTION();
if(m_file->eof()) if(m_file->eof())
{ {
eofFlag = true; eofFlag = true;
@ -147,7 +149,7 @@ namespace Navigator {
void CompassFile::ParseNextHit() void CompassFile::ParseNextHit()
{ {
NAV_PROFILE_FUNCTION();
m_currentHit.board = *((uint16_t*)bufferIter); m_currentHit.board = *((uint16_t*)bufferIter);
bufferIter += 2; bufferIter += 2;
m_currentHit.channel = *((uint16_t*)bufferIter); m_currentHit.channel = *((uint16_t*)bufferIter);

View File

@ -35,6 +35,7 @@ namespace Navigator {
void CompassOnlineSource::InitConnection(const std::string& hostname, const std::string& port) void CompassOnlineSource::InitConnection(const std::string& hostname, const std::string& port)
{ {
NAV_PROFILE_FUNCTION();
m_validFlag = false; m_validFlag = false;
m_connection.Connect(hostname, port); m_connection.Connect(hostname, port);
if (m_connection.IsOpen()) if (m_connection.IsOpen())
@ -45,6 +46,7 @@ namespace Navigator {
const NavData& CompassOnlineSource::GetData() const NavData& CompassOnlineSource::GetData()
{ {
NAV_PROFILE_FUNCTION();
size_t range = m_bufferEnd - m_bufferIter; //how much buffer we have left size_t range = m_bufferEnd - m_bufferIter; //how much buffer we have left
if (!IsValid()) if (!IsValid())
{ {
@ -75,6 +77,7 @@ namespace Navigator {
void CompassOnlineSource::FillBuffer() void CompassOnlineSource::FillBuffer()
{ {
NAV_PROFILE_FUNCTION();
if (!m_connection.IsOpen()) //Make sure connection is still cool if (!m_connection.IsOpen()) //Make sure connection is still cool
{ {
m_validFlag = false; m_validFlag = false;
@ -100,6 +103,7 @@ namespace Navigator {
void CompassOnlineSource::GetHit() void CompassOnlineSource::GetHit()
{ {
NAV_PROFILE_FUNCTION();
m_currentHit.board = *((uint16_t*)m_bufferIter); m_currentHit.board = *((uint16_t*)m_bufferIter);
m_bufferIter += 2; m_bufferIter += 2;
m_currentHit.channel = *((uint16_t*)m_bufferIter); m_currentHit.channel = *((uint16_t*)m_bufferIter);

View File

@ -33,6 +33,7 @@ namespace Navigator {
void CompassRun::CollectFiles() void CompassRun::CollectFiles()
{ {
NAV_PROFILE_FUNCTION();
int nfiles=0; int nfiles=0;
for(auto& item : std::filesystem::directory_iterator(m_directory)) for(auto& item : std::filesystem::directory_iterator(m_directory))
{ {
@ -90,7 +91,7 @@ namespace Navigator {
*/ */
bool CompassRun::GetHitsFromFiles() bool CompassRun::GetHitsFromFiles()
{ {
NAV_PROFILE_FUNCTION();
std::pair<CompassHit, bool*> earliestHit = std::make_pair(CompassHit(), nullptr); std::pair<CompassHit, bool*> earliestHit = std::make_pair(CompassHit(), nullptr);
for(unsigned int i=m_startIndex; i<m_datafiles.size(); i++) for(unsigned int i=m_startIndex; i<m_datafiles.size(); i++)
{ {
@ -122,6 +123,7 @@ namespace Navigator {
const NavData& CompassRun::GetData() const NavData& CompassRun::GetData()
{ {
NAV_PROFILE_FUNCTION();
if(!IsValid()) if(!IsValid())
{ {
NAV_ERROR("Trying to access CompassRun data when invalid, bug detected!"); NAV_ERROR("Trying to access CompassRun data when invalid, bug detected!");

View File

@ -23,6 +23,7 @@ namespace Navigator {
bool PhysicsEventBuilder::AddDatumToEvent(const NavData& datum) bool PhysicsEventBuilder::AddDatumToEvent(const NavData& datum)
{ {
NAV_PROFILE_FUNCTION();
if (datum.timestamp == 0) //Ignore empty data (need a valid timestamp) if (datum.timestamp == 0) //Ignore empty data (need a valid timestamp)
return false; return false;

View File

@ -20,6 +20,7 @@ namespace Navigator {
PhysicsLayer::~PhysicsLayer() PhysicsLayer::~PhysicsLayer()
{ {
NAV_PROFILE_FUNCTION();
if (m_activeFlag) if (m_activeFlag)
{ {
DetachDataSource(); DetachDataSource();
@ -43,6 +44,7 @@ namespace Navigator {
void PhysicsLayer::OnEvent(Event& event) void PhysicsLayer::OnEvent(Event& event)
{ {
NAV_PROFILE_FUNCTION();
EventDispatcher dispatch(event); EventDispatcher dispatch(event);
dispatch.Dispatch<PhysicsStartEvent>(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStartEvent)); dispatch.Dispatch<PhysicsStartEvent>(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStartEvent));
dispatch.Dispatch<PhysicsStopEvent>(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStopEvent)); dispatch.Dispatch<PhysicsStopEvent>(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStopEvent));
@ -50,6 +52,7 @@ namespace Navigator {
bool PhysicsLayer::OnPhysicsStartEvent(PhysicsStartEvent& event) bool PhysicsLayer::OnPhysicsStartEvent(PhysicsStartEvent& event)
{ {
NAV_PROFILE_FUNCTION();
//If the Physics thread is active, kill it //If the Physics thread is active, kill it
if(m_activeFlag) if(m_activeFlag)
{ {
@ -70,6 +73,7 @@ namespace Navigator {
bool PhysicsLayer::OnPhysicsStopEvent(PhysicsStopEvent& event) bool PhysicsLayer::OnPhysicsStopEvent(PhysicsStopEvent& event)
{ {
NAV_PROFILE_FUNCTION();
if (m_activeFlag) if (m_activeFlag)
{ {
DetachDataSource(); DetachDataSource();
@ -89,6 +93,7 @@ namespace Navigator {
void PhysicsLayer::DestroyPhysThread() void PhysicsLayer::DestroyPhysThread()
{ {
NAV_PROFILE_FUNCTION();
NAV_INFO("Destroying the analysis thread..."); NAV_INFO("Destroying the analysis thread...");
//Join the thread back to the parent (finish up the thread) //Join the thread back to the parent (finish up the thread)
if(m_physThread != nullptr && m_physThread->joinable()) if(m_physThread != nullptr && m_physThread->joinable())
@ -107,6 +112,7 @@ namespace Navigator {
void PhysicsLayer::AttachDataSource(PhysicsStartEvent& event) void PhysicsLayer::AttachDataSource(PhysicsStartEvent& event)
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_sourceMutex); //Shouldn't matter for this, but safety first std::scoped_lock<std::mutex> guard(m_sourceMutex); //Shouldn't matter for this, but safety first
m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourcePort(), event.GetSourceType())); m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourcePort(), event.GetSourceType()));
m_eventBuilder.SetCoincidenceWindow(event.GetCoincidenceWindow()); m_eventBuilder.SetCoincidenceWindow(event.GetCoincidenceWindow());
@ -125,6 +131,7 @@ namespace Navigator {
void PhysicsLayer::DetachDataSource() void PhysicsLayer::DetachDataSource()
{ {
NAV_PROFILE_FUNCTION();
std::scoped_lock<std::mutex> guard(m_sourceMutex); std::scoped_lock<std::mutex> guard(m_sourceMutex);
NAV_INFO("Detaching physics data source..."); NAV_INFO("Detaching physics data source...");
m_activeFlag = false; m_activeFlag = false;
@ -134,6 +141,7 @@ namespace Navigator {
void PhysicsLayer::RunSource() void PhysicsLayer::RunSource()
{ {
NAV_PROFILE_FUNCTION();
SpectrumManager& manager = SpectrumManager::GetInstance(); SpectrumManager& manager = SpectrumManager::GetInstance();
NavEvent event; NavEvent event;

View File

@ -0,0 +1,251 @@
#ifndef INSTRUMENTOR_H
#define INSTRUMENTOR_H
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <string>
#include <thread>
#include <mutex>
#include <sstream>
#include "Navigator/Core/Logger.h"
#include "Timer.h"
namespace Navigator {
using FloatingPointMicroseconds = std::chrono::duration<double, std::micro>;
struct ProfileResult
{
std::string name;
FloatingPointMicroseconds start;
std::chrono::microseconds elapsedTime;
std::thread::id threadID;
};
struct InstrumentationSession
{
std::string name;
};
class Instrumentor
{
public:
Instrumentor(const Instrumentor&) = delete;
Instrumentor(Instrumentor&&) = delete;
void BeginSession(const std::string& name, const std::string filename="inst_results.json")
{
std::scoped_lock<std::mutex> guard(m_instMutex);
if (m_session)
{
/*
* Can only ever have one session rolling. If for whatever reason attempt to open second, handle gracefully
*/
if (Logger::GetLogger()) //Ensure logger exists
{
NAV_ERROR("Attempting to create new instr. session ({0}) while session ({1}) is running! Setting to session {0}", name, m_session->name);
}
InternalEndSession();
}
m_outputFile.open(filename);
if (m_outputFile.is_open())
{
m_session = new InstrumentationSession({ name });
WriteHeader();
}
else if(Logger::GetLogger())
{
NAV_ERROR("Unable to open instr. output file named {0}!", filename);
}
}
void EndSession()
{
std::scoped_lock<std::mutex> guard(m_instMutex);
InternalEndSession();
}
void WriteProfile(const ProfileResult& result)
{
std::scoped_lock<std::mutex> guard(m_instMutex);
std::stringstream json_string;
json_string << std::setprecision(3) << std::fixed;
json_string << ",{";
json_string << "\"cat\":\"function\",";
json_string << "\"dur\":" << (result.elapsedTime.count()) << ',';
json_string << "\"name\":\"" << result.name << "\",";
json_string << "\"ph\":\"X\",";
json_string << "\"pid\":0,";
json_string << "\"tid\":" << result.threadID << ",";
json_string << "\"ts\":" << result.start.count();
json_string << "}";
if (m_session)
{
m_outputFile << json_string.str();
m_outputFile.flush();
}
}
static Instrumentor& Get()
{
static Instrumentor s_instance;
return s_instance;
}
private:
Instrumentor::Instrumentor() :
m_session(nullptr)
{
}
Instrumentor::~Instrumentor()
{
EndSession();
}
void InternalEndSession()
{
if (m_session)
{
WriteFooter();
m_outputFile.close();
delete m_session;
m_session = nullptr;
}
}
void WriteHeader()
{
m_outputFile << "{\"otherData\": {},\"traceEvents\":[{}";
m_outputFile.flush();
}
void WriteFooter()
{
m_outputFile << "]}";
m_outputFile.flush();
}
std::mutex m_instMutex;
InstrumentationSession* m_session;
std::ofstream m_outputFile;
};
class InstrumentationTimer
{
public:
InstrumentationTimer(const char* name) :
m_name(name), m_stopped(false)
{
m_startTime = Clock::now();
}
~InstrumentationTimer()
{
if (!m_stopped)
Stop();
}
void Stop()
{
auto stopTime = Clock::now();
FloatingPointMicroseconds start = FloatingPointMicroseconds((m_startTime).time_since_epoch());
auto elapsedTime = std::chrono::time_point_cast<std::chrono::microseconds>(stopTime).time_since_epoch() - std::chrono::time_point_cast<std::chrono::microseconds>(m_startTime).time_since_epoch();
m_stopped = true;
Instrumentor::Get().WriteProfile({ m_name, start, elapsedTime, std::this_thread::get_id() });
}
private:
using Time = std::chrono::steady_clock::time_point;
using Clock = std::chrono::steady_clock;
const char* m_name;
Time m_startTime;
bool m_stopped;
};
namespace InstrumentorUtils {
template<size_t N>
struct ChangeResult
{
char data[N];
};
template<size_t N, size_t M>
constexpr auto CleanupOutputString(const char(&expr)[N], const char(&remove)[M])
{
ChangeResult<N> result = {};
size_t srcIndex = 0;
size_t destIndex = 0;
while (srcIndex < N)
{
size_t matchIndex = 0;
while (matchIndex < M - 1 && srcIndex + matchIndex < N - 1 && expr[srcIndex + matchIndex] == remove[matchIndex])
matchIndex++;
if (matchIndex == M - 1)
srcIndex += matchIndex;
result.data[destIndex++] = expr[srcIndex] == '"' ? '\'' : expr[srcIndex];
srcIndex++;
}
return result;
}
}
}
//Macros for ease of use
#define NAV_PROFILE 0
#if NAV_PROFILE
//Attempt to resolve function signature with precompiler
#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)
#define NAV_FUNC_SIG __PRETTY_FUNCTION__
#elif defined(__DMC__) && (__DMC__ >= 0x810)
#define NAV_FUNC_SIG __PRETTY_FUNCTION__
#elif (defined(__FUNCSIG__) || (_MSC_VER))
#define NAV_FUNC_SIG __FUNCSIG__
#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || (defined(__IBMCPP__) && (__IBMCPP__ >= 500))
#define NAV_FUNC_SIG __FUNCTION__
#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
#define NAV_FUNC_SIG __FUNC__
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
#define NAV_FUNC_SIG __func__
#elif defined(__cplusplus) && (__cplusplus >= 201103)
#define NAV_FUNC_SIG __func__
#else
#define NAV_FUNC_SIG "NAV_FUNC_SIG unknown!"
#endif
#define NAV_PROFILE_BEGIN_SESSION(name, filename) ::Navigator::Instrumentor::Get().BeginSession(name, filename);
#define NAV_PROFILE_END_SESSION() ::Navigator::Instrumentor::Get().EndSession();
#define NAV_PROFILE_SCOPE_LINE2(name, line) constexpr auto fixedName##line = ::Navigator::InstrumentorUtils::CleanupOutputString(name, "__cdecl ");\
::Navigator::InstrumentationTimer timer##line(fixedName##line.data)
#define NAV_PROFILE_SCOPE_LINE(name, line) NAV_PROFILE_SCOPE_LINE2(name, line)
#define NAV_PROFILE_SCOPE(name) NAV_PROFILE_SCOPE_LINE(name, __LINE__)
#define NAV_PROFILE_FUNCTION() NAV_PROFILE_SCOPE(NAV_FUNC_SIG)
#else
#define NAV_PROFILE_BEGIN_SESSION(name, filename)
#define NAV_PROFILE_END_SESSION()
#define NAV_PROFILE_SCOPE_LINE2(name, line)
#define NAV_PROFILE_SCOPE_LINE(name, line)
#define NAV_PROFILE_SCOPE(name)
#define NAV_PROFILE_FUNCTION()
#endif
#endif

View File

@ -26,7 +26,7 @@ namespace Navigator {
auto stopTime = Clock::now(); auto stopTime = Clock::now();
int64_t start = std::chrono::time_point_cast<std::chrono::microseconds>(m_startTime).time_since_epoch().count(); int64_t start = std::chrono::time_point_cast<std::chrono::microseconds>(m_startTime).time_since_epoch().count();
int64_t stop = std::chrono::time_point_cast<std::chrono::microseconds>(stopTime).time_since_epoch().count(); int64_t stop = std::chrono::time_point_cast<std::chrono::microseconds>(stopTime).time_since_epoch().count();
float duration = (stop - start)*0.001; float duration = (stop - start)*0.001f;
m_stopped = true; m_stopped = true;
NAV_INFO("{1} -- Duration: {0} ms", m_name, duration); NAV_INFO("{1} -- Duration: {0} ms", m_name, duration);

View File

@ -16,7 +16,7 @@
#include <cstdint> #include <cstdint>
#include "Navigator/Core/Logger.h" #include "Navigator/Core/Logger.h"
#include "Navigator/Utils/Instrumentor.h"
#endif #endif