From 6c97ebddc2ba128a636a79317ce6ca75d966a418 Mon Sep 17 00:00:00 2001 From: Mateusz Przybylski Date: Tue, 27 May 2025 14:05:37 -0700 Subject: [PATCH 1/2] Add ability to append pid to the filename of the log dumping in ApiDump layer. --- layersvt/api_dump.h | 55 +++++++++++++++++++++----- layersvt/json/VkLayer_api_dump.json.in | 17 ++++++++ 2 files changed, 63 insertions(+), 9 deletions(-) diff --git a/layersvt/api_dump.h b/layersvt/api_dump.h index b25578c25e..62a558b0d0 100644 --- a/layersvt/api_dump.h +++ b/layersvt/api_dump.h @@ -81,6 +81,7 @@ // Defines for utilized environment variables. #define kSettingsKeyFile "file" #define kSettingsKeyLogFilename "log_filename" +#define kSettingsKeyAppendPid "append_pid" #define kSettingsKeyOutputFormat "output_format" #define kSettingsKeyDetailedOutput "detailed" #define kSettingsKeyNoAddr "no_addr" @@ -549,18 +550,36 @@ class ApiDumpSettings { size_t html_pos = filename_string.find(".html", filename_string.size() - 5); size_t json_pos = filename_string.find(".json", filename_string.size() - 5); + bool append_pid = false; + + // Check if pid should be appended to the filename. + if (vkuHasLayerSetting(layerSettingSet, kSettingsKeyAppendPid)) { + vkuGetLayerSettingValue(layerSettingSet, kSettingsKeyAppendPid, append_pid); + } + + std::string pid_string; + + if (append_pid) { + pid_string = getCurrentPidString(); + } + + // Filter the filename itself. + if (json_pos != std::string::npos) filename_string.erase(json_pos); + if (txt_pos != std::string::npos) filename_string.erase(txt_pos); + if (html_pos != std::string::npos) filename_string.erase(html_pos); + + if (append_pid) { + // Append with pid if needed. + filename_string.append("_").append(pid_string); + } + + // Restore the file extension. if (output_format == ApiDumpFormat::Html) { - if (json_pos != std::string::npos) filename_string.erase(json_pos); - if (txt_pos != std::string::npos) filename_string.erase(txt_pos); - if (html_pos == std::string::npos) filename_string.append(".html"); + filename_string.append(".html"); } else if (output_format == ApiDumpFormat::Json) { - if (html_pos != std::string::npos) filename_string.erase(html_pos); - if (txt_pos != std::string::npos) filename_string.erase(txt_pos); - if (json_pos == std::string::npos) filename_string.append(".json"); + filename_string.append(".json"); } else { - if (html_pos != std::string::npos) filename_string.erase(html_pos); - if (json_pos != std::string::npos) filename_string.erase(json_pos); - if (txt_pos == std::string::npos) filename_string.append(".txt"); + filename_string.append(".txt"); } } @@ -776,6 +795,24 @@ class ApiDumpSettings { return lower_value; } + std::string getCurrentPidString() { + std::stringstream ss; +#ifdef _WIN32 +#include + DWORD pid = GetCurrentProcessId(); + ss << pid; +#elif defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNX__) || defined(__FreeBSD__) || \ + defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__) || defined(__ANDROID__) +#include + pid_t pid = getpid(); + ss << pid; +#else + // Should never reach here due to the #error above + throw std::runtime_error("Unsupported operating system"); +#endif + return ss.str(); + } + // The mutable is necessary because everyone who 'writes' to the stream necessarily must be able to modify it. // Since basically every function in this struct is const, we have to work around that. mutable std::ostream output_stream; diff --git a/layersvt/json/VkLayer_api_dump.json.in b/layersvt/json/VkLayer_api_dump.json.in index 4aeab0e0a1..3d123387b9 100644 --- a/layersvt/json/VkLayer_api_dump.json.in +++ b/layersvt/json/VkLayer_api_dump.json.in @@ -191,6 +191,23 @@ } ] } + }, + { + "key": "append_pid", + "env": "VK_APIDUMP_APPEND_LOG_FILENAME_WITH_PID", + "label": "Append log filename with pid.", + "description": "If set, appends the log filename with pid.", + "type": "BOOL", + "default": "false", + "dependence": { + "mode": "ALL", + "settings": [ + { + "key": "file", + "value": true + } + ] + } } ] }, From dcfa2cf45dcb0aa8d9e9480e1cb6f66d1cca4fa2 Mon Sep 17 00:00:00 2001 From: Mateusz Przybylski Date: Tue, 27 May 2025 23:16:59 -0700 Subject: [PATCH 2/2] Try to fix Linux compilation --- layersvt/api_dump.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/layersvt/api_dump.h b/layersvt/api_dump.h index 62a558b0d0..3ca1e6b94d 100644 --- a/layersvt/api_dump.h +++ b/layersvt/api_dump.h @@ -76,6 +76,13 @@ #pragma warning(disable : 4554) #endif +#ifdef _WIN32 +#include +#elif defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNX__) || defined(__FreeBSD__) || \ + defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__) || defined(__ANDROID__) +#include +#endif + #define MAX_STRING_LENGTH 1024 // Defines for utilized environment variables. @@ -798,12 +805,10 @@ class ApiDumpSettings { std::string getCurrentPidString() { std::stringstream ss; #ifdef _WIN32 -#include DWORD pid = GetCurrentProcessId(); ss << pid; #elif defined(__linux__) || defined(__APPLE__) || defined(__Fuchsia__) || defined(__QNX__) || defined(__FreeBSD__) || \ defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__) || defined(__ANDROID__) -#include pid_t pid = getpid(); ss << pid; #else