Skip to content

Commit

Permalink
logger and profiler revised
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Oct 1, 2024
1 parent 05c45e9 commit 49e0578
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .vscode_MacOS/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"vendor/tracy/include"
],
"defines": [
"MACOSX"
"MACOSX", "PROFILING"
],
"cStandard": "c17",
"cppStandard": "c++20"
Expand Down
4 changes: 2 additions & 2 deletions application/lucre/gameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace LucreApp
{

GameState::GameState()
: m_State{State::SPLASH}, m_NextState{State::SPLASH}, m_LastState{State::SPLASH}, m_InputIdle{false},
m_UserInputEnabled{false}, m_DeleteScene{State::NULL_STATE}, m_LoadingState{State::NULL_STATE}
: m_State{State::SPLASH}, m_NextState{State::SPLASH}, m_LastState{State::SPLASH}, m_UserInputEnabled{false},
m_DeleteScene{State::NULL_STATE}, m_LoadingState{State::NULL_STATE}
{
memset(m_StateLoaded, false, static_cast<int>(State::MAX_STATES) * sizeof(bool));
}
Expand Down
1 change: 0 additions & 1 deletion application/lucre/gameState.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ namespace LucreApp
State m_State, m_NextState, m_LastState, m_DeleteScene, m_LoadingState;
std::shared_ptr<Scene> m_Scenes[static_cast<int>(State::MAX_STATES)];
bool m_UserInputEnabled;
bool m_InputIdle;
bool m_StateLoaded[static_cast<int>(State::MAX_STATES)];
int m_DeleteSceneCounter;
};
Expand Down
51 changes: 19 additions & 32 deletions engine/auxiliary/instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ namespace GfxRenderEngine
{
namespace Instrumentation
{
Timer::Timer(const char* name) : m_Name(name) { m_Start = std::chrono::high_resolution_clock::now(); }
Timer::Timer(Profiler& profiler, const char* name) : m_Profiler{profiler}, m_Name(name)
{
m_Start = std::chrono::high_resolution_clock::now();
}

Timer::~Timer()
{
Expand All @@ -42,20 +45,10 @@ namespace GfxRenderEngine
auto start = std::chrono::duration<double, std::micro>{m_Start.time_since_epoch()};
auto elapsedMicroSeconds = std::chrono::time_point_cast<std::chrono::microseconds>(m_End).time_since_epoch() -
std::chrono::time_point_cast<std::chrono::microseconds>(m_Start).time_since_epoch();
SessionManager::Get().CreateEntry({m_Name, start, elapsedMicroSeconds, std::this_thread::get_id()});
m_Profiler.CreateEntry({m_Name, start, elapsedMicroSeconds, std::this_thread::get_id()});
}

SessionManager::SessionManager() : m_CurrentSession(nullptr) {}

SessionManager::~SessionManager() { End(); }

void SessionManager::Begin(const std::string& name, const std::string& filename)
Profiler::Profiler(const std::string& name, const std::string& filename)
{
std::lock_guard lock(m_Mutex);
if (m_CurrentSession)
{
EndInternal();
}
m_StartTime = std::chrono::steady_clock::now();

// this function must be called
Expand Down Expand Up @@ -85,18 +78,23 @@ namespace GfxRenderEngine
}
else
{
// plain cout because logger might not yet be available
std::cout << "SessionManager could not open output file '" << filepath << "'" << std::endl;
LOG_CORE_CRITICAL("Profiler could not open output file '{0}'", filepath);
}
}

void SessionManager::End()
Profiler::~Profiler()
{
std::lock_guard lock(m_Mutex);
EndInternal();
if (m_CurrentSession)
{
EndJsonFile();
m_OutputStream.close();
delete m_CurrentSession;
m_CurrentSession = nullptr;
}
}

void SessionManager::CreateEntry(const Result& result)
void Profiler::CreateEntry(const Result& result)
{
if ((std::chrono::steady_clock::now() - m_StartTime) > 5min)
{
Expand All @@ -111,10 +109,9 @@ namespace GfxRenderEngine
outputFile << "\"name\":\"" << result.m_Name << "\",";
outputFile << "\"ph\":\"X\",";
outputFile << "\"pid\":0,";
outputFile << "\"tid\":" << result.m_ThreadID << ",";
outputFile << "\"tid\":" << std::hash<std::thread::id>()(result.m_ThreadID) << ",";
outputFile << "\"ts\":" << result.m_Start.count();
outputFile << "}";

std::lock_guard lock(m_Mutex);
if (m_CurrentSession)
{
Expand All @@ -123,28 +120,18 @@ namespace GfxRenderEngine
}
}

void SessionManager::StartJsonFile()
void Profiler::StartJsonFile()
{
m_OutputStream << "{\"otherData\": {},\"traceEvents\":[{}";
m_OutputStream.flush();
}

void SessionManager::EndJsonFile()
void Profiler::EndJsonFile()
{
m_OutputStream << "]}";
m_OutputStream.flush();
}

void SessionManager::EndInternal()
{
if (m_CurrentSession)
{
EndJsonFile();
m_OutputStream.close();
delete m_CurrentSession;
m_CurrentSession = nullptr;
}
}
} // namespace Instrumentation
} // namespace GfxRenderEngine
#endif
29 changes: 8 additions & 21 deletions engine/auxiliary/instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ namespace GfxRenderEngine
#define FUNC_SIGNATURE __PRETTY_FUNCTION__
#endif

#define PROFILE_BEGIN_SESSION(name, filepath) ::GfxRenderEngine::Instrumentation::SessionManager::Get().Begin(name, filepath)
#define PROFILE_END_SESSION() ::GfxRenderEngine::Instrumentation::SessionManager::Get().End()

#define PROFILE_SCOPE_LINE2(name, line) ::GfxRenderEngine::Instrumentation::Timer timer##line(name)
#define PROFILE_SCOPE_LINE2(name, line) Instrumentation::Timer timer##line(*g_Profiler, name)
#define PROFILE_SCOPE_LINE(name, line) PROFILE_SCOPE_LINE2(name, line)
#define PROFILE_SCOPE(name) PROFILE_SCOPE_LINE(name, __LINE__)
#define PROFILE_FUNCTION() PROFILE_SCOPE(FUNC_SIGNATURE)
Expand All @@ -68,30 +65,19 @@ namespace GfxRenderEngine
std::string m_Name;
};

class SessionManager
class Profiler
{
public:
SessionManager(const SessionManager&) = delete;
SessionManager(SessionManager&&) = delete;

void Begin(const std::string& name, const std::string& filename = "results.json");
void End();
Profiler(const std::string& name, const std::string& filename = "results.json");
~Profiler();
Profiler(const Profiler&) = delete;
Profiler(Profiler&&) = delete;

void CreateEntry(const Result& result);

static SessionManager& Get()
{
static SessionManager instance;
return instance;
}

private:
SessionManager();
~SessionManager();

void StartJsonFile();
void EndJsonFile();
void EndInternal();

private:
std::mutex m_Mutex;
Expand All @@ -104,10 +90,11 @@ namespace GfxRenderEngine
{

public:
Timer(const char* name);
Timer(Profiler& Profiler, const char* name);
~Timer();

private:
Profiler& m_Profiler;
const char* m_Name;
std::chrono::time_point<std::chrono::high_resolution_clock> m_Start, m_End;
};
Expand Down
1 change: 1 addition & 0 deletions engine/auxiliary/threadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#pragma once
#include <iostream>
#include <mutex>
#include "BS_thread_pool/BS_thread_pool.hpp"

Expand Down
5 changes: 0 additions & 5 deletions engine/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ namespace GfxRenderEngine

bool Engine::Start()
{
// init logger
if (!Log::Init())
{
std::cout << "Could not initialize logger" << std::endl;
}
InitSettings();

// signal handling
Expand Down
16 changes: 11 additions & 5 deletions engine/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@

#include "core.h"
#include "engine.h"
#include "auxiliary/instrumentation.h"
#include "application.h"

using Profiler = GfxRenderEngine::Instrumentation::Profiler;
// global logger for the engine and application
std::unique_ptr<GfxRenderEngine::Log> g_Logger;
// non-owning pointer to the global profiler
std::unique_ptr<Profiler> g_Profiler;

int engine(int argc, char* argv[])
{
PROFILE_BEGIN_SESSION("RunTime", "profiling (open with chrome tracing).json");
g_Logger = std::make_unique<GfxRenderEngine::Log>();
g_Profiler = std::make_unique<Profiler>("RunTime", "profiling (open with chrome tracing).json");

std::unique_ptr<GfxRenderEngine::Engine> engine;
std::unique_ptr<GfxRenderEngine::Application> application;
Expand All @@ -53,7 +59,6 @@ int engine(int argc, char* argv[])
if (!engine->IsRunning())
{
engine->Quit();
PROFILE_END_SESSION();
exit(0);
}
std::this_thread::sleep_for(16ms);
Expand Down Expand Up @@ -103,10 +108,11 @@ int engine(int argc, char* argv[])
engine->Shutdown();
application.reset();
engine->Quit();
g_Profiler.reset();
g_Logger.reset();

PROFILE_END_SESSION();
#ifdef DEBUG
LOG_CORE_INFO("leaving main");
std::cout << "leaving main" << std::endl;
#endif
return 0;
}
36 changes: 21 additions & 15 deletions engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
#include <chrono>

#include "log/log.h"
#include "auxiliary/instrumentation.h"
#ifdef TRACY_ENABLE
#include "tracy/Tracy.hpp"
#endif

#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include "glm.hpp"
Expand All @@ -36,10 +40,6 @@
#define GLM_ENABLE_EXPERIMENTAL
#include "gtx/hash.hpp"

#ifdef TRACY_ENABLE
#include "tracy/Tracy.hpp"
#endif

typedef uint8_t uchar;
typedef uint16_t uint16;
typedef uint32_t uint;
Expand All @@ -66,11 +66,28 @@ int engine(int argc, char* argv[]);

#define BIT(x) (1 << (x))

// profiling
extern std::unique_ptr<GfxRenderEngine::Instrumentation::Profiler> g_Profiler;

// logging
#ifndef DISTRIBUTION_BUILD
#define LOGGING_AND_ASSERTS
#endif
#ifdef LOGGING_AND_ASSERTS

extern std::unique_ptr<GfxRenderEngine::Log> g_Logger;
#define LOG_CORE_TRACE(...) g_Logger->GetLogger().trace(__VA_ARGS__)
#define LOG_CORE_INFO(...) g_Logger->GetLogger().info(__VA_ARGS__)
#define LOG_CORE_WARN(...) g_Logger->GetLogger().warn(__VA_ARGS__)
#define LOG_CORE_ERROR(...) g_Logger->GetLogger().error(__VA_ARGS__)
#define LOG_CORE_CRITICAL(...) g_Logger->GetLogger().critical(__VA_ARGS__)

#define LOG_APP_TRACE(...) g_Logger->GetAppLogger().trace(__VA_ARGS__)
#define LOG_APP_INFO(...) g_Logger->GetAppLogger().info(__VA_ARGS__)
#define LOG_APP_WARN(...) g_Logger->GetAppLogger().warn(__VA_ARGS__)
#define LOG_APP_ERROR(...) g_Logger->GetAppLogger().error(__VA_ARGS__)
#define LOG_APP_CRITICAL(...) g_Logger->GetAppLogger().critical(__VA_ARGS__)

#define CORE_ASSERT(x, str) \
if (!(x)) \
LOG_CORE_CRITICAL("ASSERT on line number {0} in file {1}: {2} (error)", __LINE__, __FILE__, str)
Expand All @@ -84,17 +101,6 @@ int engine(int argc, char* argv[]);
if (!(x)) \
LOG_APP_CRITICAL("ASSERT on line number {0} in file {1}: {2} (error)", __LINE__, __FILE__, str)

#define LOG_CORE_TRACE(...) GfxRenderEngine::Log::GetLogger()->trace(__VA_ARGS__)
#define LOG_CORE_INFO(...) GfxRenderEngine::Log::GetLogger()->info(__VA_ARGS__)
#define LOG_CORE_WARN(...) GfxRenderEngine::Log::GetLogger()->warn(__VA_ARGS__)
#define LOG_CORE_ERROR(...) GfxRenderEngine::Log::GetLogger()->error(__VA_ARGS__)
#define LOG_CORE_CRITICAL(...) GfxRenderEngine::Log::GetLogger()->critical(__VA_ARGS__)

#define LOG_APP_TRACE(...) GfxRenderEngine::Log::GetAppLogger()->trace(__VA_ARGS__)
#define LOG_APP_INFO(...) GfxRenderEngine::Log::GetAppLogger()->info(__VA_ARGS__)
#define LOG_APP_WARN(...) GfxRenderEngine::Log::GetAppLogger()->warn(__VA_ARGS__)
#define LOG_APP_ERROR(...) GfxRenderEngine::Log::GetAppLogger()->error(__VA_ARGS__)
#define LOG_APP_CRITICAL(...) GfxRenderEngine::Log::GetAppLogger()->critical(__VA_ARGS__)
#else
#define CORE_ASSERT(x, str) \
{ \
Expand Down
7 changes: 1 addition & 6 deletions engine/gui/Common/UI/viewGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,7 @@ namespace GfxRenderEngine
return;
}

float sum = 0.0f;
float maxOther = 0.0f;
float totalWeight = 0.0f;
float weightSum = 0.0f;
float weightZeroSum = 0.0f;

Expand All @@ -520,7 +518,6 @@ namespace GfxRenderEngine

if (linLayoutParams)
{
totalWeight += linLayoutParams->weight;
if (linLayoutParams->HasMargins())
{
margins = linLayoutParams->margins;
Expand Down Expand Up @@ -566,7 +563,6 @@ namespace GfxRenderEngine
maxOther = std::max(maxOther, view->GetMeasuredWidth() + margins.horiz());
}

sum += amount;
if (linLayoutParams)
{
if (linLayoutParams->weight == 0.0f)
Expand Down Expand Up @@ -940,7 +936,6 @@ namespace GfxRenderEngine
}

const float friction = 0.92f;
const float stop_threshold = 0.1f;

void ScrollView::Draw(SCREEN_UIContext& dc)
{
Expand Down Expand Up @@ -1345,7 +1340,7 @@ namespace GfxRenderEngine
}

TabHolder::TabHolder(Orientation orientation, float stripSize, LayoutParams* layoutParams, float leftMargin)
: LinearLayout(Opposite(orientation), layoutParams), stripSize_(stripSize)
: LinearLayout(Opposite(orientation), layoutParams)
{
if (orientation == ORIENT_HORIZONTAL)
{
Expand Down
3 changes: 0 additions & 3 deletions engine/gui/Common/UI/viewGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ namespace GfxRenderEngine

Orientation orientation_;
float scrollPos_ = 0.0f;
float scrollStart_ = 0.0f;
float scrollTarget_ = 0.0f;
int scrollTouchId_ = -1;
bool scrollToTarget_ = false;
float inertia_ = 0.0f;
float pull_ = 0.0f;
Expand Down Expand Up @@ -384,7 +382,6 @@ namespace GfxRenderEngine
ScrollView* tabScroll_ = nullptr;
AnchorLayout* contents_ = nullptr;

float stripSize_;
int currentTab_ = 0;
std::vector<View*> tabs_;
std::vector<AnchorTranslateTween*> tabTweens_;
Expand Down
Loading

0 comments on commit 49e0578

Please sign in to comment.