From e555a942fb980b81c0a161ef87e327b2cfd6cf3e Mon Sep 17 00:00:00 2001 From: jackun Date: Fri, 18 Feb 2022 16:44:10 +0200 Subject: [PATCH] Use single IO stats instance --- src/app/main.cpp | 2 +- src/gl/imgui_hud.cpp | 2 +- src/hud_elements.cpp | 4 ++-- src/iostats.cpp | 47 ++++++++++++++++++++++---------------------- src/iostats.h | 3 ++- src/keybinds.cpp | 5 ++--- src/overlay.cpp | 16 +++++++-------- src/overlay.h | 7 +++---- src/vulkan.cpp | 4 ++-- 9 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 3e9aa655..daefa41c 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -272,7 +272,7 @@ int main(int, char**) new_frame = false; } - check_keybinds(sw_stats, *params, vendorID); + check_keybinds(*params, vendorID); // Start the Dear ImGui frame { if (render(window)) { diff --git a/src/gl/imgui_hud.cpp b/src/gl/imgui_hud.cpp index c59e01d4..6c811ada 100644 --- a/src/gl/imgui_hud.cpp +++ b/src/gl/imgui_hud.cpp @@ -188,7 +188,7 @@ void imgui_render(unsigned int width, unsigned int height) if (!state.imgui_ctx) return; - check_keybinds(sw_stats, params, vendorID); + check_keybinds(params, vendorID); update_hud_info(sw_stats, params, vendorID); ImGuiContext *saved_ctx = ImGui::GetCurrentContext(); diff --git a/src/hud_elements.cpp b/src/hud_elements.cpp index 7eb14da3..97129b57 100644 --- a/src/hud_elements.cpp +++ b/src/hud_elements.cpp @@ -316,7 +316,7 @@ void HudElements::io_stats(){ if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_io_read]){ ImGui::TableNextColumn(); - float val = HUDElements.sw_stats->io.per_second.read; + const float val = g_io_stats.per_second.read; right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, val < 100 ? "%.1f" : "%.f", val); ImGui::SameLine(0,1.0f); ImGui::PushFont(HUDElements.sw_stats->font1); @@ -325,7 +325,7 @@ void HudElements::io_stats(){ } if (HUDElements.params->enabled[OVERLAY_PARAM_ENABLED_io_write]){ ImGui::TableNextColumn(); - float val = HUDElements.sw_stats->io.per_second.write; + const float val = g_io_stats.per_second.write; right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, val < 100 ? "%.1f" : "%.f", val); ImGui::SameLine(0,1.0f); ImGui::PushFont(HUDElements.sw_stats->font1); diff --git a/src/iostats.cpp b/src/iostats.cpp index e4cdcb64..089afdb1 100644 --- a/src/iostats.cpp +++ b/src/iostats.cpp @@ -2,34 +2,33 @@ #include "string_utils.h" #include -void getIoStats(void *args) { - iostats *io = reinterpret_cast(args); - if (io) { - Clock::time_point now = Clock::now(); /* ns */ - std::chrono::duration time_diff = now - io->last_update; +struct iostats g_io_stats; - io->prev.read_bytes = io->curr.read_bytes; - io->prev.write_bytes = io->curr.write_bytes; +void getIoStats(iostats& io) { + Clock::time_point now = Clock::now(); /* ns */ + std::chrono::duration time_diff = now - io.last_update; - std::string line; - std::ifstream f("/proc/self/io"); - while (std::getline(f, line)) { - if (starts_with(line, "read_bytes:")) { - try_stoull(io->curr.read_bytes, line.substr(12)); - } - else if (starts_with(line, "write_bytes:")) { - try_stoull(io->curr.write_bytes, line.substr(13)); - } - } - - if (io->last_update.time_since_epoch().count()) { - io->diff.read = (io->curr.read_bytes - io->prev.read_bytes) / (1024.f * 1024.f); - io->diff.write = (io->curr.write_bytes - io->prev.write_bytes) / (1024.f * 1024.f); + io.prev.read_bytes = io.curr.read_bytes; + io.prev.write_bytes = io.curr.write_bytes; - io->per_second.read = io->diff.read / time_diff.count(); - io->per_second.write = io->diff.write / time_diff.count(); + std::string line; + std::ifstream f("/proc/self/io"); + while (std::getline(f, line)) { + if (starts_with(line, "read_bytes:")) { + try_stoull(io.curr.read_bytes, line.substr(12)); + } + else if (starts_with(line, "write_bytes:")) { + try_stoull(io.curr.write_bytes, line.substr(13)); } + } + + if (io.last_update.time_since_epoch().count()) { + io.diff.read = (io.curr.read_bytes - io.prev.read_bytes) / (1024.f * 1024.f); + io.diff.write = (io.curr.write_bytes - io.prev.write_bytes) / (1024.f * 1024.f); - io->last_update = now; + io.per_second.read = io.diff.read / time_diff.count(); + io.per_second.write = io.diff.write / time_diff.count(); } + + io.last_update = now; } diff --git a/src/iostats.h b/src/iostats.h index 52c8fdaf..e1a18b2a 100644 --- a/src/iostats.h +++ b/src/iostats.h @@ -25,6 +25,7 @@ struct iostats { Clock::time_point last_update; }; -void getIoStats(void *args); +extern iostats g_io_stats; +void getIoStats(iostats& io); #endif //MANGOHUD_IOSTATS_H diff --git a/src/keybinds.cpp b/src/keybinds.cpp index e3dbc3eb..d22da447 100644 --- a/src/keybinds.cpp +++ b/src/keybinds.cpp @@ -3,7 +3,7 @@ #include "logging.h" #include "keybinds.h" -void check_keybinds(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID){ +void check_keybinds(struct overlay_params& params, uint32_t vendorID){ using namespace std::chrono_literals; bool pressed = false; // FIXME just a placeholder until wayland support auto now = Clock::now(); /* us */ @@ -32,8 +32,7 @@ void check_keybinds(struct swapchain_stats& sw_stats, struct overlay_params& par logger->stop_logging(); } else { logger->start_logging(); - std::thread(update_hw_info, std::ref(sw_stats), std::ref(params), - vendorID) + std::thread(update_hw_info, std::ref(params), vendorID) .detach(); benchmark.fps_data.clear(); } diff --git a/src/overlay.cpp b/src/overlay.cpp index 0e3af8bb..18133025 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -49,7 +49,7 @@ bool gpu_metrics_exists = false; bool steam_focused = false; vector frametime_data = {}; -void update_hw_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID) +void update_hw_info(struct overlay_params& params, uint32_t vendorID) { if (params.enabled[OVERLAY_PARAM_ENABLED_cpu_stats] || logger->is_active()) { cpuStats.UpdateCPUData(); @@ -66,7 +66,7 @@ void update_hw_info(struct swapchain_stats& sw_stats, struct overlay_params& par if (params.enabled[OVERLAY_PARAM_ENABLED_gpu_stats] || logger->is_active()) { if (vendorID == 0x1002 && getAmdGpuInfo_actual) getAmdGpuInfo_actual(); - + if (gpu_metrics_exists) amdgpu_get_metrics(); @@ -82,7 +82,7 @@ void update_hw_info(struct swapchain_stats& sw_stats, struct overlay_params& par if (params.enabled[OVERLAY_PARAM_ENABLED_procmem]) update_procmem(); if (params.enabled[OVERLAY_PARAM_ENABLED_io_read] || params.enabled[OVERLAY_PARAM_ENABLED_io_write]) - getIoStats(&sw_stats.io); + getIoStats(g_io_stats); #endif currentLogData.gpu_load = gpu_info.load; @@ -109,7 +109,6 @@ struct hw_info_updater { bool quit = false; std::thread thread {}; - struct swapchain_stats* sw_stats = nullptr; struct overlay_params* params = nullptr; uint32_t vendorID; bool update_hw_info_thread = false; @@ -130,12 +129,11 @@ struct hw_info_updater thread.join(); } - void update(struct swapchain_stats* sw_stats_, struct overlay_params* params_, uint32_t vendorID_) + void update(struct overlay_params* params_, uint32_t vendorID_) { std::unique_lock lk_hw_updating(m_hw_updating, std::try_to_lock); if (lk_hw_updating.owns_lock()) { - sw_stats = sw_stats_; params = params_; vendorID = vendorID_; update_hw_info_thread = true; @@ -149,10 +147,10 @@ struct hw_info_updater cv_hwupdate.wait(lk_cv_hwupdate, [&]{ return update_hw_info_thread || quit; }); if (quit) break; - if (sw_stats && params) + if (params) { std::unique_lock lk_hw_updating(m_hw_updating); - update_hw_info(*sw_stats, *params, vendorID); + update_hw_info(*params, vendorID); } update_hw_info_thread = false; } @@ -190,7 +188,7 @@ void update_hud_info_with_frametime(struct swapchain_stats& sw_stats, struct ove if (elapsed >= params.fps_sampling_period) { if (!hw_update_thread) hw_update_thread = std::make_unique(); - hw_update_thread->update(&sw_stats, ¶ms, vendorID); + hw_update_thread->update(¶ms, vendorID); sw_stats.fps = fps; diff --git a/src/overlay.h b/src/overlay.h index 83cf8743..e3ef4c22 100644 --- a/src/overlay.h +++ b/src/overlay.h @@ -62,7 +62,6 @@ struct swapchain_stats { size_t font_params_hash = 0; std::string time; double fps; - struct iostats io; uint64_t last_present_time; unsigned n_frames_since_update; uint64_t last_fps_update; @@ -153,10 +152,10 @@ void position_layer(struct swapchain_stats& data, struct overlay_params& params, void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& window_size, bool is_vulkan); void update_hud_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID); void update_hud_info_with_frametime(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID, uint64_t frametime_ns); -void update_hw_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID); +void update_hw_info(struct overlay_params& params, uint32_t vendorID); void init_gpu_stats(uint32_t& vendorID, uint32_t reported_deviceID, overlay_params& params); void init_cpu_stats(overlay_params& params); -void check_keybinds(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID); +void check_keybinds(overlay_params& params, uint32_t vendorID); void init_system_info(void); void FpsLimiter(struct fps_limit& stats); void get_device_name(int32_t vendorID, int32_t deviceID, struct swapchain_stats& sw_stats); @@ -174,4 +173,4 @@ extern void process_control_socket(struct instance_data *instance_data); void render_mpris_metadata(overlay_params& params, mutexed_metadata& meta, uint64_t frame_timing); #endif -#endif //MANGOHUD_OVERLAY_H \ No newline at end of file +#endif //MANGOHUD_OVERLAY_H diff --git a/src/vulkan.cpp b/src/vulkan.cpp index a9835d8c..0709a630 100644 --- a/src/vulkan.cpp +++ b/src/vulkan.cpp @@ -425,7 +425,7 @@ static void snapshot_swapchain_frame(struct swapchain_data *data) struct device_data *device_data = data->device; struct instance_data *instance_data = device_data->instance; update_hud_info(data->sw_stats, instance_data->params, device_data->properties.vendorID); - check_keybinds(data->sw_stats, instance_data->params, device_data->properties.vendorID); + check_keybinds(instance_data->params, device_data->properties.vendorID); #ifdef __linux__ if (instance_data->params.control >= 0) { control_client_check(device_data); @@ -1521,7 +1521,7 @@ static VkResult overlay_CreateSwapchainKHR( #endif } swapchain_data->sw_stats.driverName = driverProps.driverInfo; - + return result; }