From f784f917988d8fa584a5e8e090d5a9343fdef394 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sat, 16 Mar 2024 16:42:23 +0100 Subject: [PATCH] add tracy and perform some markup --- external/CMakeLists.txt | 2 ++ external/solanaceae_message3 | 2 +- external/toxcore/CMakeLists.txt | 5 +-- external/tracy/CMakeLists.txt | 12 +++++++ flake.nix | 15 +++++++-- src/CMakeLists.txt | 2 ++ src/chat_gui4.cpp | 8 +++++ src/main.cpp | 10 ++++++ src/main_screen.cpp | 4 +++ src/tox_client.cpp | 55 +++++++++++++++++++++++++++++++++ 10 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 external/tracy/CMakeLists.txt diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index 59914ab..7a46778 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.9 FATAL_ERROR) +add_subdirectory(./tracy) + add_subdirectory(./entt) add_subdirectory(./solanaceae_util) diff --git a/external/solanaceae_message3 b/external/solanaceae_message3 index e6f4f18..7c28b23 160000 --- a/external/solanaceae_message3 +++ b/external/solanaceae_message3 @@ -1 +1 @@ -Subproject commit e6f4f18f0019b72533ad430ca397f3c78f85e444 +Subproject commit 7c28b232a46ebede9d6f09bc6eafb49bacfa99ea diff --git a/external/toxcore/CMakeLists.txt b/external/toxcore/CMakeLists.txt index 4887bb2..802686b 100644 --- a/external/toxcore/CMakeLists.txt +++ b/external/toxcore/CMakeLists.txt @@ -171,9 +171,10 @@ configure_file( target_include_directories(toxcore PRIVATE "${TOX_DIR}toxcore") target_include_directories(toxcore PUBLIC "${TOX_DIR}") -target_compile_definitions(toxcore PUBLIC USE_IPV6=1) +#target_compile_definitions(toxcore PUBLIC USE_IPV6=1) #target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_DEBUG) -target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_INFO) +#target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_INFO) +target_compile_definitions(toxcore PUBLIC MIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE) find_package(unofficial-sodium CONFIG QUIET) find_package(sodium QUIET) diff --git a/external/tracy/CMakeLists.txt b/external/tracy/CMakeLists.txt new file mode 100644 index 0000000..09eb45d --- /dev/null +++ b/external/tracy/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.24 FATAL_ERROR) + +include(FetchContent) + +if (NOT TARGET TracyClient) + FetchContent_Declare(tracy + GIT_REPOSITORY https://github.com/wolfpld/tracy.git + GIT_TAG v0.10 + ) + FetchContent_MakeAvailable(tracy) +endif() + diff --git a/flake.nix b/flake.nix index 5e4a9fc..7ccc3e0 100644 --- a/flake.nix +++ b/flake.nix @@ -12,6 +12,14 @@ flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; + + # get deps (move to lockfile or something) + # TODO: use system provided instead + tracy-src = pkgs.fetchFromGitHub { + owner = "wolfpld"; repo = "tracy"; + rev = "v0.10"; + hash = "sha256-DN1ExvQ5wcIUyhMAfiakFbZkDsx+5l8VMtYGvSdboPA="; + }; in { packages.default = pkgs.stdenv.mkDerivation { pname = "tomato"; @@ -56,8 +64,11 @@ ] ++ self.packages.${system}.default.dlopenBuildInputs; cmakeFlags = [ - "TOMATO_ASAN=1" - "CMAKE_BUILD_TYPE=RelWithDebInfo" + "-DTOMATO_ASAN=OFF" + "-DCMAKE_BUILD_TYPE=RelWithDebInfo" + + "-DTRACY_ENABLE=ON" + "-DFETCHCONTENT_SOURCE_DIR_TRACY=${tracy-src}" ]; # TODO: replace with install command diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6328a18..2487acd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -82,6 +82,8 @@ target_link_libraries(tomato PUBLIC solanaceae_tox_contacts solanaceae_tox_messages + TracyClient + SDL3::SDL3 imgui diff --git a/src/chat_gui4.cpp b/src/chat_gui4.cpp index a55c7d7..5d06368 100644 --- a/src/chat_gui4.cpp +++ b/src/chat_gui4.cpp @@ -2,6 +2,8 @@ #include "./file_selector.hpp" +#include + #include #include #include @@ -149,6 +151,7 @@ ChatGui4::~ChatGui4(void) { } float ChatGui4::render(float time_delta) { + ZoneScoped; _fss.render(); _sip.render(time_delta); @@ -338,6 +341,7 @@ float ChatGui4::render(float time_delta) { Components::ConvertedTimeCache prev_time {}; auto tmp_view = msg_reg.view(); for (auto view_it = tmp_view.rbegin(), view_last = tmp_view.rend(); view_it != view_last; view_it++) { + ZoneScoped; const Message3 e = *view_it; // manually filter ("reverse" iteration <.<) @@ -677,6 +681,7 @@ void ChatGui4::sendFilePath(const char* file_path) { // has MessageText void ChatGui4::renderMessageBodyText(Message3Registry& reg, const Message3 e) { + ZoneScoped; const auto& msgtext = reg.get(e).text; // TODO: set word wrap @@ -719,6 +724,7 @@ void ChatGui4::renderMessageBodyText(Message3Registry& reg, const Message3 e) { } void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) { + ZoneScoped; if ( !_show_chat_avatar_tf && ( @@ -978,6 +984,7 @@ void ChatGui4::renderMessageExtra(Message3Registry& reg, const Message3 e) { } void ChatGui4::renderContactList(void) { + ZoneScoped; if (ImGui::BeginChild("contacts", {TEXT_BASE_WIDTH*35, 0})) { //for (const auto& c : _cm.getBigContacts()) { for (const auto& c : _cr.view()) { @@ -1138,6 +1145,7 @@ bool ChatGui4::renderSubContactListContact(const Contact3 c, const bool selected } void ChatGui4::pasteFile(const char* mime_type) { + ZoneScoped; size_t data_size = 0; void* data = SDL_GetClipboardData(mime_type, &data_size); diff --git a/src/main.cpp b/src/main.cpp index fc5acc3..c1140ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include #include +#include + #include #include #include @@ -90,6 +92,7 @@ int main(int argc, char** argv) { bool quit = false; while (!quit) { + ZoneScopedN("tomato::mainloop"); auto new_time = std::chrono::steady_clock::now(); const float time_delta_tick = std::chrono::duration(new_time - last_time_tick).count(); @@ -99,7 +102,10 @@ int main(int argc, char** argv) { bool render = time_delta_render >= screen->nextRender(); if (tick) { + ZoneScopedN("tomato::tick"); + FrameMarkStart("tomato::tick"); Screen* ret_screen = screen->tick(time_delta_tick, quit); + FrameMarkEnd("tomato::tick"); if (ret_screen != nullptr) { screen.reset(ret_screen); } @@ -126,6 +132,8 @@ int main(int argc, char** argv) { // can do both in the same loop if (render) { + ZoneScopedN("tomato::render"); + FrameMarkStart("tomato::render"); ImGui_ImplSDLRenderer3_NewFrame(); ImGui_ImplSDL3_NewFrame(); ImGui::NewFrame(); @@ -141,9 +149,11 @@ int main(int argc, char** argv) { ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData()); SDL_RenderPresent(renderer.get()); + FrameMark; // clearing after present is (should) more performant, but first frame is a mess SDL_SetRenderDrawColor(renderer.get(), 0x10, 0x10, 0x10, SDL_ALPHA_OPAQUE); SDL_RenderClear(renderer.get()); + FrameMarkEnd("tomato::render"); last_time_render = new_time; } diff --git a/src/main_screen.cpp b/src/main_screen.cpp index e84a2c9..f53eac8 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -1,5 +1,7 @@ #include "./main_screen.hpp" +#include + #include #include @@ -155,6 +157,7 @@ bool MainScreen::handleEvent(SDL_Event& e) { } Screen* MainScreen::render(float time_delta, bool&) { + ZoneScoped; // HACK: render the tomato main window first, with proper flags set. // flags need to be set the first time begin() is called. // and plugins are run before the main cg is run. @@ -403,6 +406,7 @@ Screen* MainScreen::render(float time_delta, bool&) { } Screen* MainScreen::tick(float time_delta, bool& quit) { + ZoneScoped; quit = !tc.iterate(time_delta); // compute tcm.iterate(time_delta); // compute diff --git a/src/tox_client.cpp b/src/tox_client.cpp index dec854b..dbdf319 100644 --- a/src/tox_client.cpp +++ b/src/tox_client.cpp @@ -1,4 +1,12 @@ #include "./tox_client.hpp" +#include "toxcore/tox.h" + +// experimental +#include +#include +#include + +#include // meh, change this #include @@ -67,6 +75,52 @@ ToxClient::ToxClient(std::string_view save_path, std::string_view save_password) } } +#if 1 + // experimental tracy alloc tracking + static Tox_System g_mysystem = tox_default_system(); + static const Memory* g_systemmemory = os_memory(); + static Memory_Funcs g_mymemoryfuncs { + +[](void* obj, uint32_t size) { + auto ret = g_systemmemory->funcs->malloc(g_systemmemory->obj, size); + TracyAllocNS(ret, size, 32, "tox"); + return ret; + }, + +[](void* obj, uint32_t nmemb, uint32_t size) { + auto ret = g_systemmemory->funcs->calloc(g_systemmemory->obj, nmemb, size); + TracyAllocNS(ret, nmemb*size, 32, "tox"); + return ret; + }, + +[](void* obj, void* ptr, uint32_t size) { + TracyFreeNS(ptr, 32, "tox"); + auto ret = g_systemmemory->funcs->realloc(g_systemmemory->obj, ptr, size); + TracyAllocNS(ret, size, 32, "tox"); + return ret; + }, + +[](void* obj, void* ptr) { + TracyFreeNS(ptr, 32, "tox"); + g_systemmemory->funcs->free(g_systemmemory->obj, ptr); + } + }; + static Memory g_mymemory { + &g_mymemoryfuncs, + nullptr + }; + + g_mysystem.mem = &g_mymemory; + + tox_options_set_operating_system(options, &g_mysystem); + + + tox_options_set_log_callback(options, +[](Tox*, Tox_Log_Level level, const char*, uint32_t, const char*, const char* message, void*) { + std::string tmp_str; + tmp_str += "["; + tmp_str += tox_log_level_to_string(level); + tmp_str += "] "; + tmp_str += message; + TracyMessageS(tmp_str.c_str(), tmp_str.size(), 32); + }); +#endif + tox_options_set_experimental_groups_persistence(options, true); TOX_ERR_NEW err_new; @@ -127,6 +181,7 @@ ToxClient::~ToxClient(void) { } bool ToxClient::iterate(float time_delta) { + ZoneScoped; Tox_Err_Events_Iterate err_e_it = TOX_ERR_EVENTS_ITERATE_OK; auto* events = tox_events_iterate(_tox, false, &err_e_it); if (err_e_it == TOX_ERR_EVENTS_ITERATE_OK && events != nullptr) {