From 2c86789262f2f5cba6cecbe390dfc760406b4c32 Mon Sep 17 00:00:00 2001
From: "Huang, Li H"
Date: Thu, 5 Dec 2024 00:24:17 +0000
Subject: [PATCH] [Decode]Log enhancement for DPB log
---
_studio/shared/include/mfx_utils.h | 1 +
_studio/shared/mfx_logging/CMakeLists.txt | 2 +
.../include/mfx_decode_dpb_logging.h | 41 +++++
.../src/mfx_decode_dpb_logging.cpp | 152 ++++++++++++++++++
_studio/shared/mfx_trace/src/mfx_trace.cpp | 12 ++
5 files changed, 208 insertions(+)
create mode 100644 _studio/shared/mfx_logging/include/mfx_decode_dpb_logging.h
create mode 100644 _studio/shared/mfx_logging/src/mfx_decode_dpb_logging.cpp
diff --git a/_studio/shared/include/mfx_utils.h b/_studio/shared/include/mfx_utils.h
index cab944559a..5cb2466a1d 100644
--- a/_studio/shared/include/mfx_utils.h
+++ b/_studio/shared/include/mfx_utils.h
@@ -32,6 +32,7 @@
#include "mfx_trace.h"
#include "mfx_utils_logging.h"
#include "mfx_utils_perf.h"
+#include "mfx_decode_dpb_logging.h"
#include "mfx_timing.h"
#include "mfxsurfacepool.h"
#include "mfx_error.h"
diff --git a/_studio/shared/mfx_logging/CMakeLists.txt b/_studio/shared/mfx_logging/CMakeLists.txt
index 9353fcc440..7442a7737e 100644
--- a/_studio/shared/mfx_logging/CMakeLists.txt
+++ b/_studio/shared/mfx_logging/CMakeLists.txt
@@ -9,6 +9,7 @@ target_sources(mfx_logging
include/mfx_unified_h264d_logging.h
include/mfx_unified_av1d_logging.h
include/mfx_unified_vp9d_logging.h
+ include/mfx_decode_dpb_logging.h
src/mfx_utils_logging.cpp
src/mfx_utils_perf.cpp
src/mfx_unified_decode_logging.cpp
@@ -16,6 +17,7 @@ target_sources(mfx_logging
src/mfx_unified_h264d_logging.cpp
src/mfx_unified_av1d_logging.cpp
src/mfx_unified_vp9d_logging.cpp
+ src/mfx_decode_dpb_logging.cpp
)
target_include_directories(mfx_logging
diff --git a/_studio/shared/mfx_logging/include/mfx_decode_dpb_logging.h b/_studio/shared/mfx_logging/include/mfx_decode_dpb_logging.h
new file mode 100644
index 0000000000..9c880c7cbd
--- /dev/null
+++ b/_studio/shared/mfx_logging/include/mfx_decode_dpb_logging.h
@@ -0,0 +1,41 @@
+#pragma once
+
+#include
+#include
+#include
+#include
+#include
+
+#define MFX_MAX_DPBLOG_FILENAME_LEN 260
+#define MFX_MAX_PATH_LENGTH 256
+
+
+#define DPBLOG_PRINT(funtion,line, change, frame, count) \
+ if(dpb_logger) \
+ { \
+ uintptr_t address = reinterpret_cast(frame); \
+ dpb_logger->Addlog(funtion, line, change, address, count); \
+ }
+
+class DPBLog
+{
+public:
+
+ DPBLog();
+ ~DPBLog();
+ void Addlog(const std::string& funtion, int line, std::string change, uintptr_t frameaddress, uint32_t refoucnter);
+ int32_t getPid();
+ int32_t getTid();
+ static DPBLog* getInstance();
+
+ //flush
+ void Flushlog();
+ static std::string dpbFilePath;
+
+private:
+ std::ostringstream Logbuffer;
+ static std::shared_ptr instance;
+};
+
+extern DPBLog* dpb_logger;
+
diff --git a/_studio/shared/mfx_logging/src/mfx_decode_dpb_logging.cpp b/_studio/shared/mfx_logging/src/mfx_decode_dpb_logging.cpp
new file mode 100644
index 0000000000..9be68f2c24
--- /dev/null
+++ b/_studio/shared/mfx_logging/src/mfx_decode_dpb_logging.cpp
@@ -0,0 +1,152 @@
+#include "mfx_config.h"
+#include
+#include
+#include
+#include
+#include "unistd.h"
+#include
+
+#include "mfx_decode_dpb_logging.h"
+
+
+DPBLog* dpb_logger = nullptr;
+std::string DPBLog::dpbFilePath = "Initialize";
+std::shared_ptr DPBLog::instance = nullptr;
+
+
+int32_t DpbSecureStringPrint(char* buffer, size_t bufSize, size_t length, const char* const format, ...)
+{
+ int32_t iRet = 0;
+ va_list var_args;
+
+ va_start(var_args, format);
+ iRet = vsnprintf(buffer, bufSize, format, var_args);
+ va_end(var_args);
+
+ return iRet;
+}
+
+#define Dpb_SecureStringPrint(buffer, bufSize, length, format, ...) \
+ DpbSecureStringPrint(buffer, bufSize, length, format, ##__VA_ARGS__)
+
+DPBLog::DPBLog()
+{
+ instance = nullptr;
+}
+
+
+DPBLog::~DPBLog()
+{
+ if (instance)
+ {
+ try
+ {
+ Flushlog();
+ }
+ catch (...)
+ {
+
+ }
+ }
+}
+
+
+std::string GetFunctionName(const std::string& input)
+{
+ // find last ':'
+ size_t pos = input.rfind(':');
+ if (pos == std::string::npos)
+ {
+ // if not find ':', return empty
+ return "";
+ }
+ // extract ':'
+ return input.substr(pos + 1);
+}
+
+void DPBLog::Addlog(const std::string& funtion, int line, std::string change, uintptr_t frameaddress, uint32_t refoucnter)
+{
+ //get function name
+ std::string function_name = GetFunctionName(funtion);
+ if (!function_name.empty())
+ {
+ Logbuffer << std::left
+ << std::setw(25) << function_name << ":"
+ << std::setw(10) << std::dec << line
+ << std::setw(16) << std::hex<< std::uppercase << frameaddress << "refcounter "
+ << std::setw(5) << std::dec << change
+ << std::setw(5) << std::dec << refoucnter << std::endl;
+ }
+ else
+ {
+ Logbuffer << "Not find function name" << std::endl;
+ }
+}
+
+void DPBLog::Flushlog()
+{
+
+ int32_t pid = getPid();
+ int32_t tid = getTid();
+
+ const char* const dpb_log_path_fmt = "%s/dpb_pid%d_tid%d.txt";
+
+
+ char logfilename[MFX_MAX_DPBLOG_FILENAME_LEN + 1] = { '\0' };
+ Dpb_SecureStringPrint(logfilename, MFX_MAX_PATH_LENGTH + 1, MFX_MAX_PATH_LENGTH + 1,
+ dpb_log_path_fmt, dpbFilePath.c_str(), pid, tid);
+
+
+ if (access(dpbFilePath.c_str(), 0) == -1)
+ {
+ int folder_exist_status = mkdir(dpbFilePath.c_str(), S_IRWXU);
+ if (folder_exist_status == -1)
+ {
+ return;
+ }
+ }
+
+ std::ofstream file(logfilename);
+ if (file.is_open()) {
+ file << Logbuffer.str(); // write into file
+ Logbuffer.str(""); // reset buffer
+ Logbuffer.clear(); // clear buffer
+ file.close();
+ }
+ else {
+ std::cerr << "Failed to open log file." << std::endl;
+ }
+}
+
+
+DPBLog* DPBLog::getInstance()
+{
+ if (instance == nullptr)
+ {
+ instance = std::make_shared();
+ if (!instance)
+ {
+ return nullptr;
+ }
+ }
+
+ return instance.get();
+}
+
+int32_t DPBLog::getPid()
+{
+ int32_t pid = getpid();
+ return pid;
+}
+
+int32_t DPBLog::getTid()
+{
+ int32_t tid = pthread_self();
+ return tid;
+}
+
+
+
+
+
+
diff --git a/_studio/shared/mfx_trace/src/mfx_trace.cpp b/_studio/shared/mfx_trace/src/mfx_trace.cpp
index 50b4392d33..da64a11e96 100644
--- a/_studio/shared/mfx_trace/src/mfx_trace.cpp
+++ b/_studio/shared/mfx_trace/src/mfx_trace.cpp
@@ -21,6 +21,7 @@
#include "mfxdefs.h"
#include "mfx_trace.h"
#include "mfx_utils_perf.h"
+#include "mfx_decode_dpb_logging.h"
static mfx_reflect::AccessibleTypesCollection g_Reflection;
@@ -271,6 +272,17 @@ mfxTraceU32 MFXTrace_GetRegistryParams(void)
PerfUtility::perfFilePath = iter->second;
}
}
+ else if (iter->first == "VPL DPB LOG" && stoi(iter->second))
+ {
+ dpb_logger = DPBLog::getInstance();
+ }
+ else if (iter->first == "VPL DPB PATH")
+ {
+ if (!iter->second.empty())
+ {
+ DPBLog::dpbFilePath = iter->second;
+ }
+ }
}
}
}