From 4830268a1118d9da2327ee329180fdc2721e62be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= Date: Fri, 3 Apr 2020 17:28:45 -0300 Subject: [PATCH 1/4] logging: fix indentation --- src/logging.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/logging.h b/src/logging.h index da04f464..677093cf 100644 --- a/src/logging.h +++ b/src/logging.h @@ -41,16 +41,21 @@ void *logging(void *params_void){ overlay_params *params = reinterpret_cast(params_void); time_t now_log = time(0); tm *log_time = localtime(&now_log); - string date = to_string(log_time->tm_year + 1900) + "-" + to_string(1 + log_time->tm_mon) + "-" + to_string(log_time->tm_mday) + "_" + to_string(1 + log_time->tm_hour) + "-" + to_string(1 + log_time->tm_min) + "-" + to_string(1 + log_time->tm_sec); + string date = to_string(log_time->tm_year + 1900) + "-" + + to_string(1 + log_time->tm_mon) + "-" + + to_string(log_time->tm_mday) + "_" + + to_string(1 + log_time->tm_hour) + "-" + + to_string(1 + log_time->tm_min) + "-" + + to_string(1 + log_time->tm_sec); log_start = os_time_get(); out.open(params->output_file + date, ios::out | ios::app); out << "os," << "cpu," << "gpu," << "ram," << "kernel," << "driver" << endl; out << os << "," << cpu << "," << gpu << "," << ram << "," << kernel << "," << driver << endl; - while (loggingOn){ + while (loggingOn){ uint64_t now = os_time_get(); elapsedLog = (double)(now - log_start); out << fps << "," << cpuLoadLog << "," << gpuLoadLog << "," << now - log_start << endl; - // logArray.push_back({fps, cpuLoadLog, gpuLoadLog, 0.0f}); + // logArray.push_back({fps, cpuLoadLog, gpuLoadLog, 0.0f}); if ((elapsedLog) >= params->log_duration * 1000000 && params->log_duration) loggingOn = false; @@ -60,4 +65,4 @@ void *logging(void *params_void){ // writeFile(date); out.close(); return NULL; -} \ No newline at end of file +} From 0303f8de28c1e3f5ab59bdb45005884d53e9d93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= Date: Wed, 8 Apr 2020 11:53:34 -0300 Subject: [PATCH 2/4] logging: write log to a memory buffer Instead of writing every line of the log to the file (and, consequently, to the disk), write to a memory buffer first and after the log finishes flush from memory to disk. This improve the performance of the tool, since it avoids making the program blocked by disk IO. --- src/logging.h | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/src/logging.h b/src/logging.h index 677093cf..126da885 100644 --- a/src/logging.h +++ b/src/logging.h @@ -10,16 +10,16 @@ using namespace std; string os, cpu, gpu, ram, kernel, driver; bool sysInfoFetched = false; -int gpuLoadLog = 0, cpuLoadLog = 0, log_period = 0; +int gpuLoadLog = 0, cpuLoadLog = 0, log_period = 0, elapsedLog; struct logData{ double fps; - double cpu; - double gpu; - double previous; + int cpu; + int gpu; + int previous; }; -double fps, elapsedLog; +double fps; std::vector logArray; ofstream out; const char* log_period_env = std::getenv("LOG_PERIOD"); @@ -27,15 +27,15 @@ int num; bool loggingOn; uint64_t log_start; -// void writeFile(string date){ -// out.open(mangohud_output_env + date, ios::out | ios::app); +void writeFile(string filename){ + out.open(filename, ios::out | ios::app); -// for (size_t i = 0; i < logArray.size(); i++) { -// out << logArray[i].fps << "," << logArray[i].cpu << "," << logArray[i].gpu << endl; -// } -// out.close(); -// logArray.clear(); -// } + for (size_t i = 0; i < logArray.size(); i++) + out << logArray[i].fps << "," << logArray[i].cpu << "," << logArray[i].gpu << "," << logArray[i].previous << endl; + + out.close(); + logArray.clear(); +} void *logging(void *params_void){ overlay_params *params = reinterpret_cast(params_void); @@ -51,18 +51,19 @@ void *logging(void *params_void){ out.open(params->output_file + date, ios::out | ios::app); out << "os," << "cpu," << "gpu," << "ram," << "kernel," << "driver" << endl; out << os << "," << cpu << "," << gpu << "," << ram << "," << kernel << "," << driver << endl; + out.close(); + while (loggingOn){ uint64_t now = os_time_get(); - elapsedLog = (double)(now - log_start); - out << fps << "," << cpuLoadLog << "," << gpuLoadLog << "," << now - log_start << endl; - // logArray.push_back({fps, cpuLoadLog, gpuLoadLog, 0.0f}); + elapsedLog = now - log_start; + logArray.push_back({fps, cpuLoadLog, gpuLoadLog, elapsedLog}); if ((elapsedLog) >= params->log_duration * 1000000 && params->log_duration) loggingOn = false; - + this_thread::sleep_for(chrono::milliseconds(log_period)); } - // writeFile(date); - out.close(); + + writeFile(params->output_file + date); return NULL; } From 0a942512e9c6cc63ecd6d6f63a529ac4c95468b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= Date: Wed, 8 Apr 2020 11:55:28 -0300 Subject: [PATCH 3/4] logging: don't sleep if time is over --- src/logging.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logging.h b/src/logging.h index 126da885..77dc58cc 100644 --- a/src/logging.h +++ b/src/logging.h @@ -60,8 +60,8 @@ void *logging(void *params_void){ if ((elapsedLog) >= params->log_duration * 1000000 && params->log_duration) loggingOn = false; - - this_thread::sleep_for(chrono::milliseconds(log_period)); + else + this_thread::sleep_for(chrono::milliseconds(log_period)); } writeFile(params->output_file + date); From f07aea54b6d705f8e0fecb188dfb45fd30108c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Almeida?= Date: Thu, 9 Apr 2020 15:58:05 -0300 Subject: [PATCH 4/4] logging: check if duration is enabled before checking if it has expired If we check if log_duration is enabled before checking if it has expired we can save some math and comparison instructions on every log write. --- src/logging.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logging.h b/src/logging.h index 77dc58cc..12329b2c 100644 --- a/src/logging.h +++ b/src/logging.h @@ -58,7 +58,7 @@ void *logging(void *params_void){ elapsedLog = now - log_start; logArray.push_back({fps, cpuLoadLog, gpuLoadLog, elapsedLog}); - if ((elapsedLog) >= params->log_duration * 1000000 && params->log_duration) + if (params->log_duration && (elapsedLog) >= params->log_duration * 1000000) loggingOn = false; else this_thread::sleep_for(chrono::milliseconds(log_period));