From d789a5d6d5ca65933e051bc5807c1b50803243eb Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 8 May 2022 15:51:17 -0400 Subject: [PATCH] Added in instrumentation classes for generating json files. Will allow for more complete profiling (at least as a start). --- .gitignore | 1 + NavProject/src/MassMap.cpp | 3 + NavProject/src/SPSAnalysisStage.cpp | 2 + NavProject/src/SPSInputLayer.cpp | 2 + NavProject/src/main.cpp | 6 + Navigator/src/Navigator.h | 1 + Navigator/src/Navigator/Core/Application.cpp | 7 + Navigator/src/Navigator/Core/Cut.cpp | 11 +- Navigator/src/Navigator/Core/Histogram.cpp | 13 + .../src/Navigator/Core/SpectrumManager.cpp | 21 ++ .../src/Navigator/Editor/EditorLayer.cpp | 4 + Navigator/src/Navigator/Editor/FileDialog.cpp | 5 + .../src/Navigator/Editor/SourceDialog.cpp | 1 + .../src/Navigator/Editor/SpectrumDialog.cpp | 1 + .../src/Navigator/Editor/SpectrumPanel.cpp | 2 + Navigator/src/Navigator/ImGui/ImGuiLayer.cpp | 4 + .../Navigator/Physics/Caen/CompassFile.cpp | 8 +- .../Physics/Caen/CompassOnlineSource.cpp | 4 + .../src/Navigator/Physics/Caen/CompassRun.cpp | 4 +- .../Navigator/Physics/PhysicsEventBuilder.cpp | 1 + .../src/Navigator/Physics/PhysicsLayer.cpp | 8 + Navigator/src/Navigator/Utils/Instrumentor.h | 251 ++++++++++++++++++ Navigator/src/Navigator/Utils/Timer.h | 2 +- Navigator/src/navpch.h | 2 +- 24 files changed, 356 insertions(+), 8 deletions(-) create mode 100644 Navigator/src/Navigator/Utils/Instrumentor.h diff --git a/.gitignore b/.gitignore index 3fb180b..b65e3bc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ Makefile *.sln *.vcxproj *.vcxproj.filters +*.json Navigator/Makefile NavProject/Makefile diff --git a/NavProject/src/MassMap.cpp b/NavProject/src/MassMap.cpp index 91cfa03..4cf4987 100644 --- a/NavProject/src/MassMap.cpp +++ b/NavProject/src/MassMap.cpp @@ -14,6 +14,7 @@ */ MassMap::MassMap() { + NAV_PROFILE_FUNCTION(); std::ifstream massfile("Assets/amdc2016_mass.txt"); if (massfile.is_open()) { @@ -42,6 +43,7 @@ MassMap::~MassMap() {} //Returns nuclear mass in MeV double MassMap::FindMass(int Z, int A) { + NAV_PROFILE_FUNCTION(); std::string key = "(" + std::to_string(Z) + "," + std::to_string(A) + ")"; auto data = massTable.find(key); if (data == massTable.end()) @@ -55,6 +57,7 @@ double MassMap::FindMass(int Z, int A) //returns element symbol std::string MassMap::FindSymbol(int Z, int A) { + NAV_PROFILE_FUNCTION(); auto data = elementTable.find(Z); if (data == elementTable.end()) { diff --git a/NavProject/src/SPSAnalysisStage.cpp b/NavProject/src/SPSAnalysisStage.cpp index 1884011..f0c8028 100644 --- a/NavProject/src/SPSAnalysisStage.cpp +++ b/NavProject/src/SPSAnalysisStage.cpp @@ -13,6 +13,7 @@ namespace Navigator { 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") { + NAV_PROFILE_FUNCTION(); SpectrumManager& manager = SpectrumManager::GetInstance(); manager.BindParameter(delayFLTime); manager.BindParameter(delayFRTime); @@ -49,6 +50,7 @@ namespace Navigator { void SPSAnalysisStage::AnalyzePhysicsEvent(const NavEvent& event) { + NAV_PROFILE_FUNCTION(); //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 //create mapping classes to use text-file-based id association (commonly called channel maps). diff --git a/NavProject/src/SPSInputLayer.cpp b/NavProject/src/SPSInputLayer.cpp index 0ef5e0e..f1b341e 100644 --- a/NavProject/src/SPSInputLayer.cpp +++ b/NavProject/src/SPSInputLayer.cpp @@ -45,6 +45,7 @@ namespace Navigator { void SPSInputLayer::OnImGuiRender() { + NAV_PROFILE_FUNCTION(); if (ImGui::Begin("SPS Input")) { //Create widgets for all of our inputs @@ -73,6 +74,7 @@ namespace Navigator { void SPSInputLayer::UpdateWeights() { + NAV_PROFILE_FUNCTION(); m_rxnEqn = ""; //reset //Calculate residual nucleus from reaction diff --git a/NavProject/src/main.cpp b/NavProject/src/main.cpp index 580654a..6650123 100644 --- a/NavProject/src/main.cpp +++ b/NavProject/src/main.cpp @@ -30,9 +30,15 @@ int main(int argc, const char** argv) Navigator::Logger::Init(); NAV_TRACE("Logger Initialized!"); + NAV_PROFILE_BEGIN_SESSION("Startup", "navprofile_startup.json"); auto app = Navigator::CreateApplication(); + NAV_PROFILE_END_SESSION(); + NAV_PROFILE_BEGIN_SESSION("Runtime", "navprofile_runtime.json"); app->Run(); + NAV_PROFILE_END_SESSION(); + NAV_PROFILE_BEGIN_SESSION("Shutdown", "navprofile_shutdown.json"); delete app; + NAV_PROFILE_END_SESSION(); } \ No newline at end of file diff --git a/Navigator/src/Navigator.h b/Navigator/src/Navigator.h index 9f7f949..37c1151 100644 --- a/Navigator/src/Navigator.h +++ b/Navigator/src/Navigator.h @@ -33,5 +33,6 @@ #include "Navigator/Core/Layer.h" #include "Navigator/Events/Event.h" #include "Navigator/Utils/TestServerLayer.h" +#include "Navigator/Utils/Instrumentor.h" #endif diff --git a/Navigator/src/Navigator/Core/Application.cpp b/Navigator/src/Navigator/Core/Application.cpp index 31daa8f..5868a95 100644 --- a/Navigator/src/Navigator/Core/Application.cpp +++ b/Navigator/src/Navigator/Core/Application.cpp @@ -14,11 +14,14 @@ namespace Navigator { + Application* Application::s_instance = nullptr; Application::Application() : m_runFlag(true) { + NAV_PROFILE_FUNCTION(); + s_instance = this; m_window = std::unique_ptr(Window::Create()); @@ -39,6 +42,7 @@ namespace Navigator { void Application::OnEvent(Event& event) { + NAV_PROFILE_FUNCTION(); EventDispatcher dispatch(event); dispatch.Dispatch(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent)); for(auto iter = m_stack.end(); iter != m_stack.begin(); ) @@ -58,18 +62,21 @@ namespace Navigator { void Application::PushLayer(Layer* layer) { + NAV_PROFILE_FUNCTION(); m_stack.PushLayer(layer); layer->OnAttach(); } void Application::PushOverlay(Layer* layer) { + NAV_PROFILE_FUNCTION(); m_stack.PushOverlay(layer); layer->OnAttach(); } void Application::Run() { + NAV_PROFILE_FUNCTION(); while(m_runFlag) { RenderCommand::SetClearColor(m_bckgnd_color); diff --git a/Navigator/src/Navigator/Core/Cut.cpp b/Navigator/src/Navigator/Core/Cut.cpp index 0d3dbae..b37fa0e 100644 --- a/Navigator/src/Navigator/Core/Cut.cpp +++ b/Navigator/src/Navigator/Core/Cut.cpp @@ -21,6 +21,7 @@ namespace Navigator { std::string ConvertCutTypeToString(CutType type) { + NAV_PROFILE_FUNCTION(); switch(type) { case CutType::Cut1D: return "Cut1D"; @@ -43,12 +44,14 @@ namespace Navigator { void Cut1D::IsInside(double x, double y) { + NAV_PROFILE_FUNCTION(); m_isValid = x >= m_minVal && x <= m_maxVal; } //Only within an ImPlot/ImGui context!!! void Cut1D::Draw() const { + NAV_PROFILE_FUNCTION(); double points[2] = { m_minVal, m_maxVal }; ImPlot::PlotVLines(m_params.name.c_str(), points, 2); } @@ -71,7 +74,8 @@ namespace Navigator { */ void Cut2D::IsInside(double x, double y) { - m_isValid = false; + NAV_PROFILE_FUNCTION(); + m_isValid = false; double slope; for(size_t i=0; i<(m_xpoints.size()-1); i++) { @@ -99,7 +103,8 @@ namespace Navigator { //Only in ImPlot/ImGui context!!!! 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*/ @@ -114,12 +119,14 @@ namespace Navigator { void CutSummary::IsInside(double x, double y) { + NAV_PROFILE_FUNCTION(); m_isValid = x >= m_minVal && x <= m_maxVal; } //Only within an ImPlot/ImGui context!!! void CutSummary::Draw() const { + NAV_PROFILE_FUNCTION(); double points[2] = { m_minVal, m_maxVal }; ImPlot::PlotVLines(m_params.name.c_str(), points, 2); } diff --git a/Navigator/src/Navigator/Core/Histogram.cpp b/Navigator/src/Navigator/Core/Histogram.cpp index e7254a2..e90b7b0 100644 --- a/Navigator/src/Navigator/Core/Histogram.cpp +++ b/Navigator/src/Navigator/Core/Histogram.cpp @@ -33,6 +33,7 @@ namespace Navigator { std::string ConvertSpectrumTypeToString(SpectrumType type) { + NAV_PROFILE_FUNCTION(); switch (type) { case SpectrumType::Histo1D: return "Histogram1D"; @@ -56,6 +57,7 @@ namespace Navigator { void Histogram1D::InitBins() { + NAV_PROFILE_FUNCTION(); m_params.type = SpectrumType::Histo1D; 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 void Histogram1D::FillData(double x, double y) { + NAV_PROFILE_FUNCTION(); if (x < m_params.min_x || x >= m_params.max_x) return; 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!! void Histogram1D::Draw() { + NAV_PROFILE_FUNCTION(); 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); } @@ -103,6 +107,7 @@ namespace Navigator { //Again here yvalues can be ignored, only for compliance StatResults Histogram1D::AnalyzeRegion(double x_min, double x_max, double y_min, double y_max) { + NAV_PROFILE_FUNCTION(); int bin_min, bin_max; StatResults results; @@ -148,6 +153,7 @@ namespace Navigator { void Histogram2D::InitBins() { + NAV_PROFILE_FUNCTION(); 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) { @@ -172,6 +178,7 @@ namespace Navigator { 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) return; int bin_x = int((x - m_params.min_x)/m_binWidthX); @@ -192,6 +199,7 @@ namespace Navigator { */ void Histogram2D::Draw() { + NAV_PROFILE_FUNCTION(); ImPlot::SetupAxes(m_params.x_par.c_str(), m_params.y_par.c_str()); 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, @@ -208,6 +216,7 @@ namespace Navigator { 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 curbin; @@ -294,6 +303,7 @@ namespace Navigator { void HistogramSummary::InitBins() { + NAV_PROFILE_FUNCTION(); m_params.type = SpectrumType::Summary; 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) { + NAV_PROFILE_FUNCTION(); if (x < m_params.min_x || x >= m_params.max_x || y <= m_params.min_y || y > m_params.max_y) return; int bin_x = int((x - m_params.min_x) / m_binWidthX); @@ -333,6 +344,7 @@ namespace Navigator { 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::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, @@ -348,6 +360,7 @@ namespace Navigator { 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 curbin; diff --git a/Navigator/src/Navigator/Core/SpectrumManager.cpp b/Navigator/src/Navigator/Core/SpectrumManager.cpp index 65538c8..431759d 100644 --- a/Navigator/src/Navigator/Core/SpectrumManager.cpp +++ b/Navigator/src/Navigator/Core/SpectrumManager.cpp @@ -28,6 +28,7 @@ namespace Navigator { void SpectrumManager::AddHistogram(const HistogramArgs& params) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); if (params.y_par == "None") //Check dimensionality m_histoMap[params.name].reset(new Histogram1D(params)); @@ -37,18 +38,21 @@ namespace Navigator { void SpectrumManager::AddHistogramSummary(const HistogramArgs& params, const std::vector& subhistos) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); m_histoMap[params.name].reset(new HistogramSummary(params, subhistos)); } void SpectrumManager::RemoveHistogram(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); m_histoMap.erase(name); } void SpectrumManager::AddCutToHistogramDraw(const std::string& cutname, const std::string& histoname) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(histoname); if (iter != m_histoMap.end()) @@ -57,6 +61,7 @@ namespace Navigator { void SpectrumManager::AddCutToHistogramApplied(const std::string& cutname, const std::string& histoname) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(histoname); 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? void SpectrumManager::UpdateHistograms() { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); //Set state of all cuts for the event @@ -128,6 +134,7 @@ namespace Navigator { void SpectrumManager::ClearHistograms() { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); for (auto& pair : m_histoMap) pair.second->ClearData(); @@ -135,6 +142,7 @@ namespace Navigator { void SpectrumManager::ClearHistogram(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(name); if (iter != m_histoMap.end()) @@ -143,6 +151,7 @@ namespace Navigator { void SpectrumManager::DrawHistogram(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(name); if (iter != m_histoMap.end()) @@ -155,6 +164,7 @@ namespace Navigator { const HistogramArgs& SpectrumManager::GetHistogramParams(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(name); if (iter != m_histoMap.end()) @@ -166,6 +176,7 @@ namespace Navigator { //For 2D spectra, we want to allow zooming along the z-axis (color) float* SpectrumManager::GetColorScaleRange(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(name); if (iter != m_histoMap.end()) @@ -190,6 +201,7 @@ namespace Navigator { std::vector SpectrumManager::GetSubHistograms(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_histoMap.find(name); 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 void SpectrumManager::BindParameter(NavParameter& param) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_paramMap.find(param.GetName()); 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) void SpectrumManager::BindParameter(NavParameter& param, int nbins, double minVal, double maxVal) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_paramMap.find(param.GetName()); if (iter == m_paramMap.end()) @@ -267,6 +281,7 @@ namespace Navigator { //Once an analysis pass is done and histograms filled, reset all parameters void SpectrumManager::InvalidateParameters() { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); for (auto& param : m_paramMap) { @@ -295,6 +310,7 @@ namespace Navigator { void SpectrumManager::BindVariable(NavVariable& var) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); auto iter = m_varMap.find(var.GetName()); if (iter == m_varMap.end()) @@ -320,6 +336,7 @@ namespace Navigator { void SpectrumManager::RemoveCut(const std::string& name) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_managerMutex); m_cutMap.erase(name); 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. void SpectrumManager::RemoveCutFromHistograms(const std::string& cutname) { + NAV_PROFILE_FUNCTION(); for (auto& gram : m_histoMap) { auto& params = gram.second->GetParameters(); @@ -402,6 +420,7 @@ namespace Navigator { //Obv. only need to draw a cut if its parent histogram is drawn. void SpectrumManager::DrawCut(const std::string& name) { + NAV_PROFILE_FUNCTION(); auto iter = m_cutMap.find(name); if (iter != m_cutMap.end()) iter->second->Draw(); @@ -410,6 +429,7 @@ namespace Navigator { //Set the state of the cuts for the current event. Called by the void SpectrumManager::CheckCuts() { + NAV_PROFILE_FUNCTION(); for (auto& iter : m_cutMap) { const std::string& xpar = iter.second->GetXParameter(); @@ -472,6 +492,7 @@ namespace Navigator { bool SpectrumManager::IsCutValid(const std::string& name) { + NAV_PROFILE_FUNCTION(); auto iter = m_cutMap.find(name); if (iter != m_cutMap.end()) return iter->second->IsValid(); diff --git a/Navigator/src/Navigator/Editor/EditorLayer.cpp b/Navigator/src/Navigator/Editor/EditorLayer.cpp index b57b0ce..7d9e7e5 100644 --- a/Navigator/src/Navigator/Editor/EditorLayer.cpp +++ b/Navigator/src/Navigator/Editor/EditorLayer.cpp @@ -68,6 +68,7 @@ namespace Navigator { //The main function void EditorLayer::OnImGuiRender() { + NAV_PROFILE_FUNCTION(); static bool startFlag = true; //first render retrieve base if(startFlag) { @@ -276,6 +277,7 @@ namespace Navigator { void EditorLayer::RemoveHistogramDialog() { + NAV_PROFILE_FUNCTION(); static std::string selectedGram = ""; if (m_removeHistogram) { @@ -311,6 +313,7 @@ namespace Navigator { void EditorLayer::RemoveCutDialog() { + NAV_PROFILE_FUNCTION(); static std::string selectedCut = ""; if (m_removeCut) { @@ -347,6 +350,7 @@ namespace Navigator { void EditorLayer::ExportHistogramDialog() { + NAV_PROFILE_FUNCTION(); static std::string filename = ""; static HistogramArgs selectedGram = HistogramArgs(); if(m_exportHistogram) diff --git a/Navigator/src/Navigator/Editor/FileDialog.cpp b/Navigator/src/Navigator/Editor/FileDialog.cpp index 47f99e5..662cc6c 100644 --- a/Navigator/src/Navigator/Editor/FileDialog.cpp +++ b/Navigator/src/Navigator/Editor/FileDialog.cpp @@ -20,6 +20,7 @@ namespace Navigator { //Helper function to handle file size printing std::string ConvertFileSystemSizeToString(std::uintmax_t value) { + NAV_PROFILE_FUNCTION(); int i = 0; double mantissa = (double)value; for (; mantissa >= 1024.0; ++i) @@ -41,6 +42,7 @@ namespace Navigator { std::pair FileDialog::RenderFileDialog(const std::string& ext) { + NAV_PROFILE_FUNCTION(); if (m_openDialogFlag) { m_selectedItem = ""; @@ -79,6 +81,7 @@ namespace Navigator { std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext) { + NAV_PROFILE_FUNCTION(); std::string result = ""; std::string text = ""; @@ -143,6 +146,7 @@ namespace Navigator { std::string FileDialog::ImGuiRenderSaveFile(const std::string& ext) { + NAV_PROFILE_FUNCTION(); std::string result = ""; std::string text = ""; @@ -207,6 +211,7 @@ namespace Navigator { std::string FileDialog::ImGuiRenderOpenDir() { + NAV_PROFILE_FUNCTION(); std::string result = ""; std::string text = ""; diff --git a/Navigator/src/Navigator/Editor/SourceDialog.cpp b/Navigator/src/Navigator/Editor/SourceDialog.cpp index 2b1d5bd..b3c7666 100644 --- a/Navigator/src/Navigator/Editor/SourceDialog.cpp +++ b/Navigator/src/Navigator/Editor/SourceDialog.cpp @@ -27,6 +27,7 @@ namespace Navigator { void SourceDialog::ImGuiRenderSourceDialog() { + NAV_PROFILE_FUNCTION(); static bool onlineFlag = false; static bool offlineFlag = false; static std::vector availTypes = { DataSource::SourceType::CompassOnline, DataSource::SourceType::CompassOffline }; diff --git a/Navigator/src/Navigator/Editor/SpectrumDialog.cpp b/Navigator/src/Navigator/Editor/SpectrumDialog.cpp index be9e2f6..45baf28 100644 --- a/Navigator/src/Navigator/Editor/SpectrumDialog.cpp +++ b/Navigator/src/Navigator/Editor/SpectrumDialog.cpp @@ -26,6 +26,7 @@ namespace Navigator { bool SpectrumDialog::ImGuiRenderSpectrumDialog(const std::vector& histoList, const std::vector& cutList, const std::vector& paramList) { + NAV_PROFILE_FUNCTION(); static std::string selectedCut = ""; bool result = false; if (m_openFlag) diff --git a/Navigator/src/Navigator/Editor/SpectrumPanel.cpp b/Navigator/src/Navigator/Editor/SpectrumPanel.cpp index 0ba6eaf..bda5759 100644 --- a/Navigator/src/Navigator/Editor/SpectrumPanel.cpp +++ b/Navigator/src/Navigator/Editor/SpectrumPanel.cpp @@ -15,6 +15,7 @@ namespace Navigator { //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) { + NAV_PROFILE_FUNCTION(); std::stringstream stream; stream << "Region: " << name << "\n" << "Integral: " << results.integral << "\n"; 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 bool SpectrumPanel::OnImGuiRender(const std::vector& histoList, const std::vector& cutList, const std::vector& paramList) { + NAV_PROFILE_FUNCTION(); static bool acceptCutFlag = false; m_result = false; if (ImGui::Begin("Active View")) diff --git a/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp b/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp index 022bf52..ee89d24 100644 --- a/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp +++ b/Navigator/src/Navigator/ImGui/ImGuiLayer.cpp @@ -32,6 +32,7 @@ namespace Navigator { void ImGuiLayer::OnAttach() { + NAV_PROFILE_FUNCTION(); IMGUI_CHECKVERSION(); NAV_INFO("Creating ImGui Context..."); @@ -82,6 +83,7 @@ namespace Navigator { void ImGuiLayer::OnDetach() { + NAV_PROFILE_FUNCTION(); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImPlot::DestroyContext(); @@ -90,6 +92,7 @@ namespace Navigator { void ImGuiLayer::Begin() { + NAV_PROFILE_FUNCTION(); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); @@ -98,6 +101,7 @@ namespace Navigator { void ImGuiLayer::End() { + NAV_PROFILE_FUNCTION(); Application& app = Application::Get(); ImGuiIO& io = ImGui::GetIO(); diff --git a/Navigator/src/Navigator/Physics/Caen/CompassFile.cpp b/Navigator/src/Navigator/Physics/Caen/CompassFile.cpp index e00e34d..a597136 100644 --- a/Navigator/src/Navigator/Physics/Caen/CompassFile.cpp +++ b/Navigator/src/Navigator/Physics/Caen/CompassFile.cpp @@ -47,6 +47,7 @@ namespace Navigator { void CompassFile::Open(const std::string& filename) { + NAV_PROFILE_FUNCTION(); eofFlag = false; hitUsedFlag = true; m_filename = filename; @@ -106,6 +107,7 @@ namespace Navigator { */ bool CompassFile::GetNextHit() { + NAV_PROFILE_FUNCTION(); if(!IsOpen()) return true; if((bufferIter == nullptr || bufferIter == bufferEnd) && !IsEOF()) @@ -131,8 +133,8 @@ namespace Navigator { */ void CompassFile::GetNextBuffer() { - - if(m_file->eof()) + NAV_PROFILE_FUNCTION(); + if(m_file->eof()) { eofFlag = true; return; @@ -147,7 +149,7 @@ namespace Navigator { void CompassFile::ParseNextHit() { - + NAV_PROFILE_FUNCTION(); m_currentHit.board = *((uint16_t*)bufferIter); bufferIter += 2; m_currentHit.channel = *((uint16_t*)bufferIter); diff --git a/Navigator/src/Navigator/Physics/Caen/CompassOnlineSource.cpp b/Navigator/src/Navigator/Physics/Caen/CompassOnlineSource.cpp index c0b59ba..fc4ae2a 100644 --- a/Navigator/src/Navigator/Physics/Caen/CompassOnlineSource.cpp +++ b/Navigator/src/Navigator/Physics/Caen/CompassOnlineSource.cpp @@ -35,6 +35,7 @@ namespace Navigator { void CompassOnlineSource::InitConnection(const std::string& hostname, const std::string& port) { + NAV_PROFILE_FUNCTION(); m_validFlag = false; m_connection.Connect(hostname, port); if (m_connection.IsOpen()) @@ -45,6 +46,7 @@ namespace Navigator { const NavData& CompassOnlineSource::GetData() { + NAV_PROFILE_FUNCTION(); size_t range = m_bufferEnd - m_bufferIter; //how much buffer we have left if (!IsValid()) { @@ -75,6 +77,7 @@ namespace Navigator { void CompassOnlineSource::FillBuffer() { + NAV_PROFILE_FUNCTION(); if (!m_connection.IsOpen()) //Make sure connection is still cool { m_validFlag = false; @@ -100,6 +103,7 @@ namespace Navigator { void CompassOnlineSource::GetHit() { + NAV_PROFILE_FUNCTION(); m_currentHit.board = *((uint16_t*)m_bufferIter); m_bufferIter += 2; m_currentHit.channel = *((uint16_t*)m_bufferIter); diff --git a/Navigator/src/Navigator/Physics/Caen/CompassRun.cpp b/Navigator/src/Navigator/Physics/Caen/CompassRun.cpp index 88f3a76..4f3853b 100644 --- a/Navigator/src/Navigator/Physics/Caen/CompassRun.cpp +++ b/Navigator/src/Navigator/Physics/Caen/CompassRun.cpp @@ -33,6 +33,7 @@ namespace Navigator { void CompassRun::CollectFiles() { + NAV_PROFILE_FUNCTION(); int nfiles=0; for(auto& item : std::filesystem::directory_iterator(m_directory)) { @@ -90,7 +91,7 @@ namespace Navigator { */ bool CompassRun::GetHitsFromFiles() { - + NAV_PROFILE_FUNCTION(); std::pair earliestHit = std::make_pair(CompassHit(), nullptr); for(unsigned int i=m_startIndex; i(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStartEvent)); dispatch.Dispatch(BIND_EVENT_FUNCTION(PhysicsLayer::OnPhysicsStopEvent)); @@ -50,6 +52,7 @@ namespace Navigator { bool PhysicsLayer::OnPhysicsStartEvent(PhysicsStartEvent& event) { + NAV_PROFILE_FUNCTION(); //If the Physics thread is active, kill it if(m_activeFlag) { @@ -70,6 +73,7 @@ namespace Navigator { bool PhysicsLayer::OnPhysicsStopEvent(PhysicsStopEvent& event) { + NAV_PROFILE_FUNCTION(); if (m_activeFlag) { DetachDataSource(); @@ -89,6 +93,7 @@ namespace Navigator { void PhysicsLayer::DestroyPhysThread() { + NAV_PROFILE_FUNCTION(); NAV_INFO("Destroying the analysis thread..."); //Join the thread back to the parent (finish up the thread) if(m_physThread != nullptr && m_physThread->joinable()) @@ -107,6 +112,7 @@ namespace Navigator { void PhysicsLayer::AttachDataSource(PhysicsStartEvent& event) { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_sourceMutex); //Shouldn't matter for this, but safety first m_source.reset(CreateDataSource(event.GetSourceLocation(), event.GetSourcePort(), event.GetSourceType())); m_eventBuilder.SetCoincidenceWindow(event.GetCoincidenceWindow()); @@ -125,6 +131,7 @@ namespace Navigator { void PhysicsLayer::DetachDataSource() { + NAV_PROFILE_FUNCTION(); std::scoped_lock guard(m_sourceMutex); NAV_INFO("Detaching physics data source..."); m_activeFlag = false; @@ -134,6 +141,7 @@ namespace Navigator { void PhysicsLayer::RunSource() { + NAV_PROFILE_FUNCTION(); SpectrumManager& manager = SpectrumManager::GetInstance(); NavEvent event; diff --git a/Navigator/src/Navigator/Utils/Instrumentor.h b/Navigator/src/Navigator/Utils/Instrumentor.h new file mode 100644 index 0000000..18b528b --- /dev/null +++ b/Navigator/src/Navigator/Utils/Instrumentor.h @@ -0,0 +1,251 @@ +#ifndef INSTRUMENTOR_H +#define INSTRUMENTOR_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Navigator/Core/Logger.h" +#include "Timer.h" + +namespace Navigator { + + using FloatingPointMicroseconds = std::chrono::duration; + + 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 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 guard(m_instMutex); + InternalEndSession(); + } + + void WriteProfile(const ProfileResult& result) + { + std::scoped_lock 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(stopTime).time_since_epoch() - std::chrono::time_point_cast(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 + struct ChangeResult + { + char data[N]; + }; + + template + constexpr auto CleanupOutputString(const char(&expr)[N], const char(&remove)[M]) + { + ChangeResult 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 \ No newline at end of file diff --git a/Navigator/src/Navigator/Utils/Timer.h b/Navigator/src/Navigator/Utils/Timer.h index 1e6f459..2b08bbf 100644 --- a/Navigator/src/Navigator/Utils/Timer.h +++ b/Navigator/src/Navigator/Utils/Timer.h @@ -26,7 +26,7 @@ namespace Navigator { auto stopTime = Clock::now(); int64_t start = std::chrono::time_point_cast(m_startTime).time_since_epoch().count(); int64_t stop = std::chrono::time_point_cast(stopTime).time_since_epoch().count(); - float duration = (stop - start)*0.001; + float duration = (stop - start)*0.001f; m_stopped = true; NAV_INFO("{1} -- Duration: {0} ms", m_name, duration); diff --git a/Navigator/src/navpch.h b/Navigator/src/navpch.h index 1a7df96..b4f95d6 100644 --- a/Navigator/src/navpch.h +++ b/Navigator/src/navpch.h @@ -16,7 +16,7 @@ #include - #include "Navigator/Core/Logger.h" +#include "Navigator/Utils/Instrumentor.h" #endif \ No newline at end of file