From b1ee9612b1ac7dcc59aba6ecf85484f58b849a51 Mon Sep 17 00:00:00 2001 From: Luca G Date: Fri, 26 Jan 2024 12:48:08 +0100 Subject: [PATCH] moved ram and cpu usage to separate window --- src/systeminfo/SystemInfo.cpp | 8 ++-- src/systeminfo/SystemInfo.h | 14 +++--- src/ui/GUI.cpp | 88 ++++++++++++++++++++++------------- src/ui/GUI.h | 9 ++-- 4 files changed, 71 insertions(+), 48 deletions(-) diff --git a/src/systeminfo/SystemInfo.cpp b/src/systeminfo/SystemInfo.cpp index 8f03f9e5..1504ef17 100644 --- a/src/systeminfo/SystemInfo.cpp +++ b/src/systeminfo/SystemInfo.cpp @@ -56,13 +56,13 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double long long l_totalPhysMem = l_memInfo.totalram; // Multiply in next statement to avoid int overflow on right hand side... l_totalPhysMem *= l_memInfo.mem_unit; - l_totalPhysMem *= m_bytesToGB; + l_totalPhysMem *= m_bytesToGiB; o_totalRAM = l_totalPhysMem; long long l_physMemUsed = l_memInfo.totalram - l_memInfo.freeram; // Multiply in next statement to avoid int overflow on right hand side... l_physMemUsed *= l_memInfo.mem_unit; - l_physMemUsed *= m_bytesToGB; + l_physMemUsed *= m_bytesToGiB; o_usedRAM = l_physMemUsed; #elif __APPLE__ || __MACH__ @@ -73,7 +73,7 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double unsigned long l_length = sizeof(int64_t); sysctl(l_mib, 2, &l_physicalMemoryLong, &l_length, NULL, 0); double l_physicalMemoryDouble = l_physicalMemoryLong; - l_physicalMemoryDouble *= m_bytesToGB; + l_physicalMemoryDouble *= m_bytesToGiB; o_totalRAM = l_physicalMemoryDouble; std::array l_buffer; @@ -135,7 +135,7 @@ void tsunami_lab::systeminfo::SystemInfo::getRAMUsage(double &o_totalRAM, double l_pages -= std::stol(l_valueString); } } - o_usedRAM = (l_pages * (double)l_pagesize) * m_bytesToGB; + o_usedRAM = (l_pages * (double)l_pagesize) * m_bytesToGiB; #endif } diff --git a/src/systeminfo/SystemInfo.h b/src/systeminfo/SystemInfo.h index df1b2c2a..3df64644 100644 --- a/src/systeminfo/SystemInfo.h +++ b/src/systeminfo/SystemInfo.h @@ -22,11 +22,11 @@ class tsunami_lab::systeminfo::SystemInfo { private: - //! Conversion constant from bytes to gigabytes (1/1000^3) - double m_bytesToGB = 0.000000001; + //! Conversion constant from bytes to gibibytes (1/1000^3) + double m_bytesToGiB = 0.00000000093132257; //! To check whether its the first time of reading cpu usage data; Linux only - bool m_firstCPURead = true; + [[maybe_unused]] bool m_firstCPURead = true; struct CPUData { @@ -43,7 +43,7 @@ class tsunami_lab::systeminfo::SystemInfo }; //! Vector for last read cpu usage data; Linux only - std::vector m_lastData; + [[maybe_unused]] std::vector m_lastData; /** * Reads CPU usage data from /proc/stat @@ -59,7 +59,7 @@ class tsunami_lab::systeminfo::SystemInfo SystemInfo(); /** - * Gets RAM usage data. + * Gets RAM usage data in GiB. * * @param o_totalRAM output pointer for total RAM value. * @param o_usedRAM output pointer for used RAM value. @@ -67,14 +67,14 @@ class tsunami_lab::systeminfo::SystemInfo void getRAMUsage(double &o_totalRAM, double &o_usedRAM); /** - * @brief Gets CPU usage data. + * @brief Gets CPU usage data in percent. * * This function collects CPU usage data. On MacOS it will return one * value as the overall CPU usage over all cores. This value is read from * the "top" command. On Linux, /proc/stat is read and the * values are calculated for each core. * - * @return Vector with CPU usage in percent for each core. + * @return vector with CPU usage in percent for each core. */ std::vector getCPUUsage(); }; diff --git a/src/ui/GUI.cpp b/src/ui/GUI.cpp index 04be80c8..4f36e16e 100644 --- a/src/ui/GUI.cpp +++ b/src/ui/GUI.cpp @@ -55,12 +55,12 @@ int tsunami_lab::ui::GUI::exec(std::string i_cmd, std::string i_outputFile) return system(commandChars); } -void tsunami_lab::ui::GUI::updateData() +void tsunami_lab::ui::GUI::updateSystemInfo() { if (m_connected) { - m_communicator.sendToServer(xlpmg::messageToJsonString(xlpmg::GET_SYSTEM_INFORMATION), false); - std::string l_responseString = m_communicator.receiveFromServer(false); + m_communicator.sendToServer(xlpmg::messageToJsonString(xlpmg::GET_SYSTEM_INFORMATION), m_logSystemInfoDataTransmission); + std::string l_responseString = m_communicator.receiveFromServer(m_logSystemInfoDataTransmission); if (json::accept(l_responseString)) { xlpmg::Message l_responseMessage = xlpmg::jsonToMessage(json::parse(l_responseString)); @@ -186,6 +186,7 @@ int tsunami_lab::ui::GUI::launch() bool showCompilerOptionsWindow = false; bool showClientLog = false; bool showSimulationParameterWindow = false; + bool showSystemInfoWindow = false; ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); @@ -215,34 +216,12 @@ int tsunami_lab::ui::GUI::launch() ImGui::NewFrame(); - if (m_lastDataUpdate <= std::chrono::system_clock::now()) - { - updateData(); - m_lastDataUpdate = std::chrono::system_clock::now() + std::chrono::seconds(m_dataUpdateFrequency); - } - if (show_demo_window) ImGui::ShowDemoWindow(&show_demo_window); // Main window { ImGui::Begin("Welcome to the Tsunami Simulator GUI!"); - - if (m_cpuData.size() > 0) - { - ImGui::ProgressBar(m_cpuData[0] / 100, ImVec2(0.0f, 0.0f)); - ImGui::SameLine(); - ImGui::Text("Overall CPU usage"); - for (unsigned long i = 1; i < m_cpuData.size(); ++i) - { - ImGui::ProgressBar(m_cpuData[i] / 100, ImVec2(0.0f, 0.0f)); - ImGui::SameLine(); - ImGui::Text("%s %lu", "CPU: ", i-1); - } - } - - ImGui::Text("%f / %f GB RAM usage", m_usedRAM, m_totalRAM); - //-------------------------------------------// //-----------------MAIN TABS-----------------// //-------------------------------------------// @@ -302,7 +281,7 @@ int tsunami_lab::ui::GUI::launch() ImGui::SameLine(); if (ImGui::Button("Check connection")) { - m_connected = m_communicator.isConnected; + m_connected = m_communicator.isConnected; } ImGui::BeginDisabled(!m_connected); ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(1.0f, 0.6f, 0.6f)); @@ -483,6 +462,7 @@ int tsunami_lab::ui::GUI::launch() ImGui::Checkbox("Edit compiler/runtime options", &showCompilerOptionsWindow); ImGui::Checkbox("Edit simulation parameters", &showSimulationParameterWindow); ImGui::Checkbox("Show client log", &showClientLog); + ImGui::Checkbox("Show system info", &showSystemInfoWindow); ImGui::EndTabItem(); } //------------------------------------------// @@ -542,14 +522,13 @@ int tsunami_lab::ui::GUI::launch() if (json::accept(response)) { xlpmg::Message responseMessage = xlpmg::jsonToMessage(json::parse(response)); - m_currentTimeStep = responseMessage.args.value("currentTimeStep", (int) 0); - m_maxTimeSteps = responseMessage.args.value("maxTimeStep", (int) 0); - m_timePerTimeStep = responseMessage.args.value("timePerTimeStep", (int) 0); - m_estimatedLeftTime = ((m_maxTimeSteps-m_currentTimeStep) * m_timePerTimeStep) /1000; - + m_currentTimeStep = responseMessage.args.value("currentTimeStep", (int)0); + m_maxTimeSteps = responseMessage.args.value("maxTimeStep", (int)0); + m_timePerTimeStep = responseMessage.args.value("timePerTimeStep", (int)0); + m_estimatedLeftTime = ((m_maxTimeSteps - m_currentTimeStep) * m_timePerTimeStep) / 1000; + std::cout << responseMessage.args << std::endl; } - } ImGui::Text("current Time Step: %i", m_currentTimeStep); ImGui::Text("Max Time Steps: %i", m_maxTimeSteps); @@ -1117,6 +1096,51 @@ int tsunami_lab::ui::GUI::launch() ImGui::TextWrapped("Our advice: only set config data before running a simulation and if you are not writing into an existing solution file."); ImGui::End(); } + //--------------------------------------------// + //-------------SYSTEM INFORMATION-------------// + //--------------------------------------------// + if (showSystemInfoWindow) + { + if (m_lastDataUpdate <= std::chrono::system_clock::now()) + { + updateSystemInfo(); + m_lastDataUpdate = std::chrono::system_clock::now() + std::chrono::seconds(m_systemInfoUpdateFrequency); + } + + ImGui::Begin("System information"); + + ImGui::Text("%f / %f GiB RAM usage", m_usedRAM, m_totalRAM); + + if (m_cpuData.size() > 0) + { + ImGui::ProgressBar(m_cpuData[0] / 100, ImVec2(0.0f, 0.0f)); + ImGui::SameLine(); + ImGui::Text("%% overall CPU usage"); + + if (ImGui::TreeNode("Core info")) + { + for (unsigned long i = 1; i < m_cpuData.size(); ++i) + { + ImGui::ProgressBar(m_cpuData[i] / 100, ImVec2(0.0f, 0.0f)); + ImGui::SameLine(); + ImGui::Text("%s %lu", "CPU: ", i - 1); + } + ImGui::TreePop(); + } + } + if (ImGui::TreeNode("Update settings")) + { + ImGui::InputInt("Update frequency", &m_systemInfoUpdateFrequency, 0); + ImGui::SetItemTooltip("in seconds"); + ImGui::SameLine(); + HelpMarker("The server will update data with a constant frequency. With this option, you can only specify the frequency with which the client/gui is getting that data from the server."); + m_systemInfoUpdateFrequency = abs(m_systemInfoUpdateFrequency); + + ImGui::Checkbox("Log data transmission", &m_logSystemInfoDataTransmission); + ImGui::TreePop(); + } + ImGui::End(); + } //-------------------------------------------// //-------------CONNECTION STATUS-------------// diff --git a/src/ui/GUI.h b/src/ui/GUI.h index a7080e1f..0728b65f 100644 --- a/src/ui/GUI.h +++ b/src/ui/GUI.h @@ -37,7 +37,8 @@ class tsunami_lab::ui::GUI bool m_connected = false; std::chrono::time_point m_lastDataUpdate; - int m_dataUpdateFrequency = 5; + int m_systemInfoUpdateFrequency = 2; + bool m_logSystemInfoDataTransmission = false; int m_clientReadBufferSize = m_communicator.BUFF_SIZE_READ_DEFAULT; int m_clientSendBufferSize = m_communicator.BUFF_SIZE_READ_DEFAULT; int m_serverReadBufferSize = m_communicator.BUFF_SIZE_READ_DEFAULT; @@ -145,11 +146,9 @@ class tsunami_lab::ui::GUI json createConfigJson(); /** - * Updates local config parameters with current server data. - * - * TODO: implementation + * Gets info on CPU and RAM usage from the server. */ - void updateData(); + void updateSystemInfo(); public: /**