Add backend diagnostics for capture stalls
- add rotating XDMA backend and Qt preview logs - expose IRQ, DMA, ring, and delivery debug state - record IRQ wait heartbeats and register state - make XDMA tracing configurable through CMake - prevent preview status queries from blocking rendering - recover from D3D swapchain and Present failures - add debug API struct size validation - avoid stale MainWindow object-size stack corruption
This commit is contained in:
@@ -1389,7 +1389,15 @@ bool D3DPreviewPipeline::ensure_preview_swapchain(int w, int h)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (FAILED(hr) || !preview_swapchain_)
|
if (FAILED(hr) || !preview_swapchain_)
|
||||||
|
{
|
||||||
|
char err[320] = {};
|
||||||
|
std::snprintf(err, sizeof(err),
|
||||||
|
"[SharedScene] preview swapchain create failed hr=0x%08X format=%s",
|
||||||
|
static_cast<unsigned>(hr),
|
||||||
|
ss_dxgi_format_name(sd.Format));
|
||||||
|
ssp_log_text(err);
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
preview_swapchain_format_ = sd.Format;
|
preview_swapchain_format_ = sd.Format;
|
||||||
preview_swapchain_10bit_ = (sd.Format == DXGI_FORMAT_R10G10B10A2_UNORM);
|
preview_swapchain_10bit_ = (sd.Format == DXGI_FORMAT_R10G10B10A2_UNORM);
|
||||||
@@ -1417,7 +1425,17 @@ bool D3DPreviewPipeline::ensure_preview_swapchain(int w, int h)
|
|||||||
0);
|
0);
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
|
{
|
||||||
|
char err[256] = {};
|
||||||
|
std::snprintf(err, sizeof(err),
|
||||||
|
"[SharedScene] preview swapchain resize failed hr=0x%08X size=%dx%d",
|
||||||
|
static_cast<unsigned>(hr),
|
||||||
|
clientW,
|
||||||
|
clientH);
|
||||||
|
ssp_log_text(err);
|
||||||
|
release_preview_swapchain();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
char msg[256] = {};
|
char msg[256] = {};
|
||||||
@@ -1430,15 +1448,33 @@ bool D3DPreviewPipeline::ensure_preview_swapchain(int w, int h)
|
|||||||
|
|
||||||
if (!preview_backbuf_)
|
if (!preview_backbuf_)
|
||||||
{
|
{
|
||||||
if (FAILED(preview_swapchain_->GetBuffer(0, IID_PPV_ARGS(&preview_backbuf_))) || !preview_backbuf_)
|
HRESULT hr = preview_swapchain_->GetBuffer(0, IID_PPV_ARGS(&preview_backbuf_));
|
||||||
|
if (FAILED(hr) || !preview_backbuf_)
|
||||||
|
{
|
||||||
|
char err[256] = {};
|
||||||
|
std::snprintf(err, sizeof(err),
|
||||||
|
"[SharedScene] preview backbuffer get failed hr=0x%08X",
|
||||||
|
static_cast<unsigned>(hr));
|
||||||
|
ssp_log_text(err);
|
||||||
|
release_preview_swapchain();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!preview_rtv_)
|
if (!preview_rtv_)
|
||||||
{
|
{
|
||||||
if (FAILED(d3d_->CreateRenderTargetView(preview_backbuf_.Get(), nullptr, &preview_rtv_)) || !preview_rtv_)
|
HRESULT hr = d3d_->CreateRenderTargetView(preview_backbuf_.Get(), nullptr, &preview_rtv_);
|
||||||
|
if (FAILED(hr) || !preview_rtv_)
|
||||||
|
{
|
||||||
|
char err[256] = {};
|
||||||
|
std::snprintf(err, sizeof(err),
|
||||||
|
"[SharedScene] preview RTV create failed hr=0x%08X",
|
||||||
|
static_cast<unsigned>(hr));
|
||||||
|
ssp_log_text(err);
|
||||||
|
release_preview_swapchain();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
preview_w_ = clientW;
|
preview_w_ = clientW;
|
||||||
preview_h_ = clientH;
|
preview_h_ = clientH;
|
||||||
@@ -1554,8 +1590,32 @@ bool D3DPreviewPipeline::present_preview(int src_w, int src_h)
|
|||||||
ID3D11ShaderResourceView *nullSrv[1] = {nullptr};
|
ID3D11ShaderResourceView *nullSrv[1] = {nullptr};
|
||||||
ctx_->PSSetShaderResources(0, 1, nullSrv);
|
ctx_->PSSetShaderResources(0, 1, nullSrv);
|
||||||
|
|
||||||
HRESULT hr = preview_swapchain_->Present(1, 0);
|
HRESULT hr = preview_swapchain_->Present(0, DXGI_PRESENT_DO_NOT_WAIT);
|
||||||
return SUCCEEDED(hr);
|
if (hr == DXGI_ERROR_WAS_STILL_DRAWING)
|
||||||
|
{
|
||||||
|
static uint64_t s_presentBusyCount = 0;
|
||||||
|
++s_presentBusyCount;
|
||||||
|
if (s_presentBusyCount <= 5 || (s_presentBusyCount % 60) == 0)
|
||||||
|
{
|
||||||
|
char warn[256] = {};
|
||||||
|
std::snprintf(warn, sizeof(warn),
|
||||||
|
"[SharedScene] preview present skipped: swapchain busy count=%llu",
|
||||||
|
static_cast<unsigned long long>(s_presentBusyCount));
|
||||||
|
ssp_log_text(warn);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (FAILED(hr))
|
||||||
|
{
|
||||||
|
char err[256] = {};
|
||||||
|
std::snprintf(err, sizeof(err),
|
||||||
|
"[SharedScene] preview present failed hr=0x%08X",
|
||||||
|
static_cast<unsigned>(hr));
|
||||||
|
ssp_log_text(err);
|
||||||
|
release_preview_swapchain();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
DXGI_FORMAT D3DPreviewPipeline::preview_backbuffer_format() const
|
DXGI_FORMAT D3DPreviewPipeline::preview_backbuffer_format() const
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "d3d_preview_pipeline.h"
|
#include "d3d_preview_pipeline.h"
|
||||||
|
|
||||||
#include <d3d11_4.h>
|
#include <d3d11_4.h>
|
||||||
|
#include <atomic>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
@@ -37,10 +38,16 @@ public:
|
|||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
if (!configured_ || !hwnd_ || !frame.data || frame.width <= 0 || frame.height <= 0)
|
if (!configured_ || !hwnd_ || !frame.data || frame.width <= 0 || frame.height <= 0)
|
||||||
|
{
|
||||||
|
clearActiveInfo();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!ensureDevice() || !ensurePipeline(frame.width, frame.height, frame.bit_depth > 0 ? frame.bit_depth : 8))
|
if (!ensureDevice() || !ensurePipeline(frame.width, frame.height, frame.bit_depth > 0 ? frame.bit_depth : 8))
|
||||||
|
{
|
||||||
|
clearActiveInfo();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
gvfg::internal::gvfg_render_pixfmt_t renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
|
gvfg::internal::gvfg_render_pixfmt_t renderFmt = gvfg::internal::GVFG_RENDER_FMT_YUY2;
|
||||||
const uint8_t *base = static_cast<const uint8_t *>(frame.data);
|
const uint8_t *base = static_cast<const uint8_t *>(frame.data);
|
||||||
@@ -87,19 +94,31 @@ public:
|
|||||||
if (!uploaded ||
|
if (!uploaded ||
|
||||||
!pipeline_->render_uploaded_yuv_to_fp16(renderFmt, frame.width, frame.height) ||
|
!pipeline_->render_uploaded_yuv_to_fp16(renderFmt, frame.width, frame.height) ||
|
||||||
!pipeline_->copy_fp16_to_scene())
|
!pipeline_->copy_fp16_to_scene())
|
||||||
|
{
|
||||||
|
clearActiveInfo();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
if (!pipeline_->preview_swapchain_10bit())
|
if (!pipeline_->preview_swapchain_10bit())
|
||||||
ok = pipeline_->blit_fp16_to_rgba8(frame.width, frame.height);
|
ok = pipeline_->blit_fp16_to_rgba8(frame.width, frame.height);
|
||||||
if (!ok)
|
if (!ok)
|
||||||
|
{
|
||||||
|
clearActiveInfo();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
pipeline_->present_preview(frame.width, frame.height);
|
if (!pipeline_->present_preview(frame.width, frame.height))
|
||||||
width_ = pipeline_->preview_w_;
|
{
|
||||||
height_ = pipeline_->preview_h_;
|
clearActiveInfo();
|
||||||
bitDepth_ = pipeline_->preview_swapchain_10bit() ? 10 : 8;
|
return false;
|
||||||
swapchain10Bit_ = pipeline_->preview_swapchain_10bit();
|
}
|
||||||
|
|
||||||
|
width_.store(pipeline_->preview_w_, std::memory_order_relaxed);
|
||||||
|
height_.store(pipeline_->preview_h_, std::memory_order_relaxed);
|
||||||
|
bitDepth_.store(pipeline_->preview_swapchain_10bit() ? 10 : 8, std::memory_order_relaxed);
|
||||||
|
swapchain10Bit_.store(pipeline_->preview_swapchain_10bit(), std::memory_order_relaxed);
|
||||||
|
active_.store(true, std::memory_order_relaxed);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,40 +131,32 @@ public:
|
|||||||
d3d_.reset();
|
d3d_.reset();
|
||||||
configured_ = false;
|
configured_ = false;
|
||||||
hwnd_ = nullptr;
|
hwnd_ = nullptr;
|
||||||
width_ = 0;
|
clearActiveInfo();
|
||||||
height_ = 0;
|
|
||||||
bitDepth_ = 0;
|
|
||||||
swapchain10Bit_ = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool active() const
|
bool active() const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
return active_.load(std::memory_order_relaxed);
|
||||||
return configured_ && pipeline_ && width_ > 0 && height_ > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int width() const
|
int width() const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
return width_.load(std::memory_order_relaxed);
|
||||||
return width_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int height() const
|
int height() const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
return height_.load(std::memory_order_relaxed);
|
||||||
return height_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int bitDepth() const
|
int bitDepth() const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
return bitDepth_.load(std::memory_order_relaxed);
|
||||||
return bitDepth_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *pixelFormat() const
|
const char *pixelFormat() const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
return swapchain10Bit_.load(std::memory_order_relaxed) ? "RGB10A2" : "BGRA8";
|
||||||
return swapchain10Bit_ ? "RGB10A2" : "BGRA8";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -155,6 +166,15 @@ private:
|
|||||||
ComPtr<ID3D11DeviceContext> context;
|
ComPtr<ID3D11DeviceContext> context;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void clearActiveInfo()
|
||||||
|
{
|
||||||
|
width_.store(0, std::memory_order_relaxed);
|
||||||
|
height_.store(0, std::memory_order_relaxed);
|
||||||
|
bitDepth_.store(0, std::memory_order_relaxed);
|
||||||
|
swapchain10Bit_.store(false, std::memory_order_relaxed);
|
||||||
|
active_.store(false, std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
bool ensureDevice()
|
bool ensureDevice()
|
||||||
{
|
{
|
||||||
if (d3d_ && d3d_->device && d3d_->context)
|
if (d3d_ && d3d_->device && d3d_->context)
|
||||||
@@ -226,10 +246,11 @@ private:
|
|||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
void *hwnd_ = nullptr;
|
void *hwnd_ = nullptr;
|
||||||
bool configured_ = false;
|
bool configured_ = false;
|
||||||
int width_ = 0;
|
std::atomic<int> width_{0};
|
||||||
int height_ = 0;
|
std::atomic<int> height_{0};
|
||||||
int bitDepth_ = 0;
|
std::atomic<int> bitDepth_{0};
|
||||||
bool swapchain10Bit_ = false;
|
std::atomic<bool> swapchain10Bit_{false};
|
||||||
|
std::atomic<bool> active_{false};
|
||||||
std::unique_ptr<D3DState> d3d_;
|
std::unique_ptr<D3DState> d3d_;
|
||||||
std::unique_ptr<gvfg::internal::D3DPreviewPipeline> pipeline_;
|
std::unique_ptr<gvfg::internal::D3DPreviewPipeline> pipeline_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
MainWindow window;
|
QWidget *window = createMainWindow();
|
||||||
window.show();
|
window->show();
|
||||||
|
|
||||||
return app.exec();
|
const int result = app.exec();
|
||||||
|
delete window;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,10 @@
|
|||||||
#include <gvfg_debug.h>
|
#include <gvfg_debug.h>
|
||||||
|
|
||||||
#include <QCloseEvent>
|
#include <QCloseEvent>
|
||||||
|
#include <QCoreApplication>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QIODevice>
|
||||||
#include <QMetaObject>
|
#include <QMetaObject>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
@@ -44,6 +47,7 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
ui_->statusLabel->setWordWrap(true);
|
ui_->statusLabel->setWordWrap(true);
|
||||||
signalStatusTimer_ = new QTimer(this);
|
signalStatusTimer_ = new QTimer(this);
|
||||||
signalStatusTimer_->setInterval(1000);
|
signalStatusTimer_->setInterval(1000);
|
||||||
|
openLogFile();
|
||||||
|
|
||||||
connect(ui_->refreshButton, &QPushButton::clicked, this, [this]() { refreshDevices(); });
|
connect(ui_->refreshButton, &QPushButton::clicked, this, [this]() { refreshDevices(); });
|
||||||
connect(ui_->openButton, &QPushButton::clicked, this, [this]()
|
connect(ui_->openButton, &QPushButton::clicked, this, [this]()
|
||||||
@@ -60,6 +64,9 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
connect(signalStatusTimer_, &QTimer::timeout, this, [this]() { updateSignalStatus(true); });
|
connect(signalStatusTimer_, &QTimer::timeout, this, [this]() { updateSignalStatus(true); });
|
||||||
|
|
||||||
updateUiState();
|
updateUiState();
|
||||||
|
appendLog(logFile_.isOpen()
|
||||||
|
? QStringLiteral("Log file | %1").arg(logFilePath_)
|
||||||
|
: QStringLiteral("Log file unavailable | %1").arg(logFilePath_));
|
||||||
refreshDevices();
|
refreshDevices();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,6 +77,11 @@ MainWindow::~MainWindow()
|
|||||||
delete ui_;
|
delete ui_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QWidget *createMainWindow()
|
||||||
|
{
|
||||||
|
return new MainWindow();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::closeEvent(QCloseEvent *event)
|
void MainWindow::closeEvent(QCloseEvent *event)
|
||||||
{
|
{
|
||||||
closeDevice();
|
closeDevice();
|
||||||
@@ -213,6 +225,7 @@ void MainWindow::startCapture()
|
|||||||
}
|
}
|
||||||
|
|
||||||
frameCount_ = 0;
|
frameCount_ = 0;
|
||||||
|
previewFailureCount_ = 0;
|
||||||
captureStop_.store(false, std::memory_order_release);
|
captureStop_.store(false, std::memory_order_release);
|
||||||
captureRunning_ = true;
|
captureRunning_ = true;
|
||||||
captureThread_ = std::thread([this]()
|
captureThread_ = std::thread([this]()
|
||||||
@@ -299,6 +312,9 @@ void MainWindow::updateSignalStatus(bool writeLog)
|
|||||||
const auto &readFrame = info.last_frame;
|
const auto &readFrame = info.last_frame;
|
||||||
gvfg_debug_fpga_signal_raw_t fpgaRaw{};
|
gvfg_debug_fpga_signal_raw_t fpgaRaw{};
|
||||||
const bool haveRaw = gvfg_debug_get_fpga_signal_raw(handle_, &fpgaRaw) == GVFG_OK;
|
const bool haveRaw = gvfg_debug_get_fpga_signal_raw(handle_, &fpgaRaw) == GVFG_OK;
|
||||||
|
gvfg_debug_backend_stats_t backendStats{};
|
||||||
|
backendStats.struct_size = sizeof(backendStats);
|
||||||
|
const bool haveBackendStats = gvfg_debug_get_backend_stats(handle_, &backendStats) == GVFG_OK;
|
||||||
if (previewWindow_->isVisible())
|
if (previewWindow_->isVisible())
|
||||||
updatePreviewSourceSize(info);
|
updatePreviewSourceSize(info);
|
||||||
|
|
||||||
@@ -348,8 +364,23 @@ void MainWindow::updateSignalStatus(bool writeLog)
|
|||||||
.arg(info.capture_fps > 0.0 ? QString::number(info.capture_fps, 'f', 2)
|
.arg(info.capture_fps > 0.0 ? QString::number(info.capture_fps, 'f', 2)
|
||||||
: QStringLiteral("--"))
|
: QStringLiteral("--"))
|
||||||
.arg(static_cast<qulonglong>(info.delivered_frames));
|
.arg(static_cast<qulonglong>(info.delivered_frames));
|
||||||
|
const QString line6 = haveBackendStats
|
||||||
|
? QStringLiteral("Backend debug | run=%1 active=%2 worker_stop=%3 pending=%4 latest=%5 delivered=%6 captured=%7 dropped=%8 held_slot=%9 ring=%10")
|
||||||
|
.arg(backendStats.backend_running)
|
||||||
|
.arg(backendStats.backend_capture_active)
|
||||||
|
.arg(backendStats.backend_data_worker_stop)
|
||||||
|
.arg(backendStats.backend_pending_events)
|
||||||
|
.arg(static_cast<qulonglong>(backendStats.backend_latest_sequence))
|
||||||
|
.arg(static_cast<qulonglong>(backendStats.backend_delivered_sequence))
|
||||||
|
.arg(static_cast<qulonglong>(backendStats.backend_frames_captured))
|
||||||
|
.arg(static_cast<qulonglong>(backendStats.backend_frames_dropped))
|
||||||
|
.arg(backendStats.backend_active_delivery_slot == UINT64_MAX
|
||||||
|
? QStringLiteral("--")
|
||||||
|
: QString::number(static_cast<qulonglong>(backendStats.backend_active_delivery_slot)))
|
||||||
|
.arg(static_cast<qulonglong>(backendStats.backend_ring_size))
|
||||||
|
: QStringLiteral("Backend debug | unavailable");
|
||||||
|
|
||||||
const QString statusText = line0 + QLatin1Char('\n') + line2 + QLatin1Char('\n') + lineRaw + QLatin1Char('\n') + line3 + QLatin1Char('\n') + line4 + QLatin1Char('\n') + line5;
|
const QString statusText = line0 + QLatin1Char('\n') + line2 + QLatin1Char('\n') + lineRaw + QLatin1Char('\n') + line3 + QLatin1Char('\n') + line4 + QLatin1Char('\n') + line5 + QLatin1Char('\n') + line6;
|
||||||
const bool changed = lastSignalStatusText_ != statusText;
|
const bool changed = lastSignalStatusText_ != statusText;
|
||||||
if (changed)
|
if (changed)
|
||||||
{
|
{
|
||||||
@@ -378,12 +409,97 @@ void MainWindow::showError(const QString &apiName, gvfg_status_t status)
|
|||||||
appendLog(QStringLiteral("%1 failed: %2").arg(apiName, QString::fromUtf8(gvfg_strerror(status))));
|
appendLog(QStringLiteral("%1 failed: %2").arg(apiName, QString::fromUtf8(gvfg_strerror(status))));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::openLogFile()
|
||||||
|
{
|
||||||
|
logSessionStamp_ = QDateTime::currentDateTime().toString(QStringLiteral("yyyyMMdd_HHmmss"));
|
||||||
|
logPartIndex_ = 1;
|
||||||
|
logDirPath_ = QCoreApplication::applicationDirPath() + QStringLiteral("/logs");
|
||||||
|
if (!QDir().mkpath(logDirPath_))
|
||||||
|
{
|
||||||
|
logFilePath_ = logDirPath_;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
openLogFilePart();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MainWindow::openLogFilePart()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(logFileMutex_);
|
||||||
|
if (logFile_.isOpen())
|
||||||
|
logFile_.close();
|
||||||
|
|
||||||
|
logFilePath_ = QStringLiteral("%1/gvfg_qt_preview_%2_part%3.log")
|
||||||
|
.arg(logDirPath_, logSessionStamp_)
|
||||||
|
.arg(logPartIndex_, 2, 10, QLatin1Char('0'));
|
||||||
|
logFile_.setFileName(logFilePath_);
|
||||||
|
if (!logFile_.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const QString header = QStringLiteral("\n==== gvfg_qt_preview session %1 part %2 ====\n")
|
||||||
|
.arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz")))
|
||||||
|
.arg(logPartIndex_);
|
||||||
|
logFile_.write(header.toUtf8());
|
||||||
|
logFile_.flush();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::rotateLogFileIfNeeded()
|
||||||
|
{
|
||||||
|
if (!logFile_.isOpen() || logFile_.size() < kMaxLogFileBytes)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const QString footer = QStringLiteral("==== log rotated at %1 ====\n")
|
||||||
|
.arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz")));
|
||||||
|
logFile_.write(footer.toUtf8());
|
||||||
|
logFile_.flush();
|
||||||
|
++logPartIndex_;
|
||||||
|
openLogFilePart();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::writeLogFileLine(const QString &line)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(logFileMutex_);
|
||||||
|
if (!logFile_.isOpen())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (logFile_.size() >= kMaxLogFileBytes)
|
||||||
|
{
|
||||||
|
const QString footer = QStringLiteral("==== log rotated at %1 ====\n")
|
||||||
|
.arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz")));
|
||||||
|
logFile_.write(footer.toUtf8());
|
||||||
|
logFile_.flush();
|
||||||
|
++logPartIndex_;
|
||||||
|
if (logFile_.isOpen())
|
||||||
|
logFile_.close();
|
||||||
|
logFilePath_ = QStringLiteral("%1/gvfg_qt_preview_%2_part%3.log")
|
||||||
|
.arg(logDirPath_, logSessionStamp_)
|
||||||
|
.arg(logPartIndex_, 2, 10, QLatin1Char('0'));
|
||||||
|
logFile_.setFileName(logFilePath_);
|
||||||
|
if (logFile_.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
|
||||||
|
{
|
||||||
|
const QString header = QStringLiteral("\n==== gvfg_qt_preview session %1 part %2 ====\n")
|
||||||
|
.arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyy-MM-dd HH:mm:ss.zzz")))
|
||||||
|
.arg(logPartIndex_);
|
||||||
|
logFile_.write(header.toUtf8());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!logFile_.isOpen())
|
||||||
|
return;
|
||||||
|
|
||||||
|
logFile_.write(line.toUtf8());
|
||||||
|
logFile_.write("\n");
|
||||||
|
logFile_.flush();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::appendLog(const QString &message)
|
void MainWindow::appendLog(const QString &message)
|
||||||
{
|
{
|
||||||
const QString line = QStringLiteral("[%1] %2")
|
const QString line = QStringLiteral("[%1] %2")
|
||||||
.arg(QDateTime::currentDateTime().toString(QStringLiteral("HH:mm:ss.zzz")))
|
.arg(QDateTime::currentDateTime().toString(QStringLiteral("HH:mm:ss.zzz")))
|
||||||
.arg(message);
|
.arg(message);
|
||||||
ui_->logEdit->appendPlainText(line);
|
ui_->logEdit->appendPlainText(line);
|
||||||
|
writeLogFileLine(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::captureReadLoop()
|
void MainWindow::captureReadLoop()
|
||||||
@@ -414,7 +530,36 @@ void MainWindow::captureReadLoop()
|
|||||||
const int height = frame.height;
|
const int height = frame.height;
|
||||||
const uint64_t frameId = frame.frame_id;
|
const uint64_t frameId = frame.frame_id;
|
||||||
if (previewHandle_)
|
if (previewHandle_)
|
||||||
gvfg_preview_render_frame(previewHandle_, &frame);
|
{
|
||||||
|
const gvfg_preview_status_t previewStatus = gvfg_preview_render_frame(previewHandle_, &frame);
|
||||||
|
if (previewStatus != GVFG_PREVIEW_OK)
|
||||||
|
{
|
||||||
|
const uint64_t failures = ++previewFailureCount_;
|
||||||
|
if (failures <= 5 || (failures % 60) == 0)
|
||||||
|
{
|
||||||
|
QMetaObject::invokeMethod(this,
|
||||||
|
[this, failures, previewStatus]()
|
||||||
|
{
|
||||||
|
appendLog(QStringLiteral("preview render failed #%1: %2")
|
||||||
|
.arg(static_cast<qulonglong>(failures))
|
||||||
|
.arg(QString::fromUtf8(gvfg_preview_strerror(previewStatus))));
|
||||||
|
},
|
||||||
|
Qt::QueuedConnection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (previewFailureCount_ != 0)
|
||||||
|
{
|
||||||
|
const uint64_t failures = previewFailureCount_;
|
||||||
|
previewFailureCount_ = 0;
|
||||||
|
QMetaObject::invokeMethod(this,
|
||||||
|
[this, failures]()
|
||||||
|
{
|
||||||
|
appendLog(QStringLiteral("preview render recovered after %1 failure(s)")
|
||||||
|
.arg(static_cast<qulonglong>(failures)));
|
||||||
|
},
|
||||||
|
Qt::QueuedConnection);
|
||||||
|
}
|
||||||
|
}
|
||||||
gvfg_release_frame(handle_, &frame);
|
gvfg_release_frame(handle_, &frame);
|
||||||
|
|
||||||
if (count <= 5 || (count % 60) == 0)
|
if (count <= 5 || (count % 60) == 0)
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
#include <gvfg_capture.h>
|
#include <gvfg_capture.h>
|
||||||
#include <gvfg_preview.h>
|
#include <gvfg_preview.h>
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
class QCloseEvent;
|
class QCloseEvent;
|
||||||
@@ -47,20 +49,35 @@ private:
|
|||||||
void updateSignalStatus(bool writeLog);
|
void updateSignalStatus(bool writeLog);
|
||||||
void updateUiState();
|
void updateUiState();
|
||||||
void showError(const QString &apiName, gvfg_status_t status);
|
void showError(const QString &apiName, gvfg_status_t status);
|
||||||
|
void openLogFile();
|
||||||
|
bool openLogFilePart();
|
||||||
|
void rotateLogFileIfNeeded();
|
||||||
|
void writeLogFileLine(const QString &line);
|
||||||
void appendLog(const QString &message);
|
void appendLog(const QString &message);
|
||||||
void captureReadLoop();
|
void captureReadLoop();
|
||||||
void joinCaptureThread();
|
void joinCaptureThread();
|
||||||
|
|
||||||
|
static constexpr qint64 kMaxLogFileBytes = 20ll * 1024ll * 1024ll;
|
||||||
|
|
||||||
Ui::MainWindow *ui_ = nullptr;
|
Ui::MainWindow *ui_ = nullptr;
|
||||||
PreviewWindow *previewWindow_ = nullptr;
|
PreviewWindow *previewWindow_ = nullptr;
|
||||||
std::array<gvfg_device_info_t, GVFG_MAX_DEVICES> devices_{};
|
std::array<gvfg_device_info_t, GVFG_MAX_DEVICES> devices_{};
|
||||||
int deviceCount_ = 0;
|
int deviceCount_ = 0;
|
||||||
gvfg_handle handle_ = nullptr;
|
gvfg_handle handle_ = nullptr;
|
||||||
gvfg_preview_handle previewHandle_ = nullptr;
|
gvfg_preview_handle previewHandle_ = nullptr;
|
||||||
bool captureRunning_ = false;
|
std::atomic<bool> captureRunning_{false};
|
||||||
std::atomic<bool> captureStop_{false};
|
std::atomic<bool> captureStop_{false};
|
||||||
std::thread captureThread_;
|
std::thread captureThread_;
|
||||||
std::atomic<uint64_t> frameCount_{0};
|
std::atomic<uint64_t> frameCount_{0};
|
||||||
|
uint64_t previewFailureCount_ = 0;
|
||||||
QTimer *signalStatusTimer_ = nullptr;
|
QTimer *signalStatusTimer_ = nullptr;
|
||||||
QString lastSignalStatusText_;
|
QString lastSignalStatusText_;
|
||||||
|
QFile logFile_;
|
||||||
|
std::mutex logFileMutex_;
|
||||||
|
QString logDirPath_;
|
||||||
|
QString logFilePath_;
|
||||||
|
QString logSessionStamp_;
|
||||||
|
int logPartIndex_ = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QWidget *createMainWindow();
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ if (NOT DEFINED GVFG_SDK_VERSION)
|
|||||||
set(GVFG_SDK_VERSION "0.1.0" CACHE STRING "GVFG SDK version")
|
set(GVFG_SDK_VERSION "0.1.0" CACHE STRING "GVFG SDK version")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
option(GVFG_XDMA_DEBUG_LOG "Enable verbose XDMA flow debug logging in GVFG internal XDMA backend" OFF)
|
option(GVFG_XDMA_DEBUG_LOG "Write verbose XDMA backend trace logs" OFF)
|
||||||
|
|
||||||
project(gvfg VERSION ${GVFG_SDK_VERSION} LANGUAGES CXX)
|
project(gvfg VERSION ${GVFG_SDK_VERSION} LANGUAGES CXX)
|
||||||
|
|
||||||
@@ -36,6 +36,8 @@ target_compile_definitions(gvfg PRIVATE
|
|||||||
|
|
||||||
if (GVFG_XDMA_DEBUG_LOG)
|
if (GVFG_XDMA_DEBUG_LOG)
|
||||||
target_compile_definitions(gvfg PRIVATE GVFG_XDMA_DEBUG_LOG=1)
|
target_compile_definitions(gvfg PRIVATE GVFG_XDMA_DEBUG_LOG=1)
|
||||||
|
else()
|
||||||
|
target_compile_definitions(gvfg PRIVATE GVFG_XDMA_DEBUG_LOG=0)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(gvfg PRIVATE
|
target_link_libraries(gvfg PRIVATE
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
|
uint32_t struct_size; /* Set to sizeof(gvfg_debug_backend_stats_t) before calling. */
|
||||||
int sdk_running;
|
int sdk_running;
|
||||||
int frame_held;
|
int frame_held;
|
||||||
uint32_t event_queue_depth;
|
uint32_t event_queue_depth;
|
||||||
@@ -35,6 +36,16 @@ typedef struct
|
|||||||
uint64_t backend_dma_errors;
|
uint64_t backend_dma_errors;
|
||||||
uint64_t backend_interrupt_count;
|
uint64_t backend_interrupt_count;
|
||||||
uint64_t backend_wait_timeouts;
|
uint64_t backend_wait_timeouts;
|
||||||
|
|
||||||
|
int backend_running;
|
||||||
|
int backend_capture_active;
|
||||||
|
int backend_data_worker_stop;
|
||||||
|
uint32_t backend_pending_events;
|
||||||
|
uint64_t backend_latest_sequence;
|
||||||
|
uint64_t backend_delivered_sequence;
|
||||||
|
uint64_t backend_active_delivery_slot; /* UINT64_MAX when no frame is held by the caller. */
|
||||||
|
uint64_t backend_next_write_slot;
|
||||||
|
uint64_t backend_ring_size;
|
||||||
} gvfg_debug_backend_stats_t;
|
} gvfg_debug_backend_stats_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
|||||||
@@ -93,6 +93,19 @@ typedef struct xdma_stream_stats_t
|
|||||||
uint64_t interrupt_count;
|
uint64_t interrupt_count;
|
||||||
} xdma_stream_stats_t;
|
} xdma_stream_stats_t;
|
||||||
|
|
||||||
|
typedef struct xdma_debug_state_t
|
||||||
|
{
|
||||||
|
int running;
|
||||||
|
int capture_active;
|
||||||
|
int data_worker_stop;
|
||||||
|
uint32_t pending_events;
|
||||||
|
uint64_t latest_sequence;
|
||||||
|
uint64_t delivered_sequence;
|
||||||
|
uint64_t active_delivery_slot;
|
||||||
|
uint64_t next_write_slot;
|
||||||
|
uint64_t ring_size;
|
||||||
|
} xdma_debug_state_t;
|
||||||
|
|
||||||
typedef enum xdma_event_type_t
|
typedef enum xdma_event_type_t
|
||||||
{
|
{
|
||||||
XDMA_EVENT_VIDEO_IRQ = 1,
|
XDMA_EVENT_VIDEO_IRQ = 1,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -157,25 +158,182 @@ namespace
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool env_value_enabled(const char *value, bool defaultValue)
|
||||||
|
{
|
||||||
|
if (!value || !*value)
|
||||||
|
return defaultValue;
|
||||||
|
|
||||||
|
std::string s(value);
|
||||||
|
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
|
||||||
|
{ return static_cast<char>(std::tolower(c)); });
|
||||||
|
return !(s == "0" || s == "false" || s == "off" || s == "no");
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool xdma_trace_default_enabled()
|
||||||
|
{
|
||||||
#if defined(GVFG_XDMA_DEBUG_LOG)
|
#if defined(GVFG_XDMA_DEBUG_LOG)
|
||||||
static void xdma_debug_log(const char *fmt, ...)
|
return GVFG_XDMA_DEBUG_LOG != 0;
|
||||||
{
|
#elif defined(_DEBUG)
|
||||||
char msg[1024] = {};
|
return true;
|
||||||
va_list args;
|
|
||||||
va_start(args, fmt);
|
|
||||||
vsnprintf(msg, sizeof(msg), fmt, args);
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
char line[1152] = {};
|
|
||||||
snprintf(line, sizeof(line), "[GVFG][XDMA] %s\n", msg);
|
|
||||||
OutputDebugStringA(line);
|
|
||||||
}
|
|
||||||
#define XDMA_LOG(...) xdma_debug_log(__VA_ARGS__)
|
|
||||||
#else
|
#else
|
||||||
#define XDMA_LOG(...) ((void)0)
|
return false;
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static void xdma_hotplug_log(const char *fmt, ...)
|
class XdmaTraceSink
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
XdmaTraceSink()
|
||||||
|
: enabled_(env_value_enabled(std::getenv("GVFG_XDMA_TRACE"), xdma_trace_default_enabled()))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
~XdmaTraceSink()
|
||||||
|
{
|
||||||
|
if (file_)
|
||||||
|
std::fclose(file_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void write(bool forceDebugOutput, const char *tag, const char *message)
|
||||||
|
{
|
||||||
|
SYSTEMTIME st{};
|
||||||
|
GetLocalTime(&st);
|
||||||
|
|
||||||
|
char line[1400] = {};
|
||||||
|
std::snprintf(line,
|
||||||
|
sizeof(line),
|
||||||
|
"%04u-%02u-%02u %02u:%02u:%02u.%03u [tid=%lu] [GVFG][XDMA]%s %s\n",
|
||||||
|
st.wYear,
|
||||||
|
st.wMonth,
|
||||||
|
st.wDay,
|
||||||
|
st.wHour,
|
||||||
|
st.wMinute,
|
||||||
|
st.wSecond,
|
||||||
|
st.wMilliseconds,
|
||||||
|
GetCurrentThreadId(),
|
||||||
|
tag ? tag : "",
|
||||||
|
message ? message : "");
|
||||||
|
|
||||||
|
if (enabled_ || forceDebugOutput)
|
||||||
|
OutputDebugStringA(line);
|
||||||
|
|
||||||
|
if (!enabled_)
|
||||||
|
return;
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
if (!open_locked())
|
||||||
|
return;
|
||||||
|
|
||||||
|
const size_t len = std::strlen(line);
|
||||||
|
if (bytes_written_ + len > kMaxTracePartBytes)
|
||||||
|
{
|
||||||
|
rotate_locked();
|
||||||
|
if (!open_locked())
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::fwrite(line, 1, len, file_);
|
||||||
|
std::fflush(file_);
|
||||||
|
bytes_written_ += len;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr uint64_t kMaxTracePartBytes = 20ull * 1024ull * 1024ull;
|
||||||
|
|
||||||
|
static std::string make_base_path()
|
||||||
|
{
|
||||||
|
std::string dir;
|
||||||
|
if (const char *envDir = std::getenv("GVFG_XDMA_TRACE_DIR"); envDir && *envDir)
|
||||||
|
{
|
||||||
|
dir = envDir;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char exePath[MAX_PATH] = {};
|
||||||
|
const DWORD len = GetModuleFileNameA(nullptr, exePath, static_cast<DWORD>(sizeof(exePath)));
|
||||||
|
dir = ".";
|
||||||
|
if (len > 0 && len < sizeof(exePath))
|
||||||
|
{
|
||||||
|
std::string path(exePath, exePath + len);
|
||||||
|
const size_t slash = path.find_last_of("\\/");
|
||||||
|
if (slash != std::string::npos)
|
||||||
|
dir = path.substr(0, slash);
|
||||||
|
}
|
||||||
|
dir += "\\logs";
|
||||||
|
}
|
||||||
|
|
||||||
|
CreateDirectoryA(dir.c_str(), nullptr);
|
||||||
|
|
||||||
|
SYSTEMTIME st{};
|
||||||
|
GetLocalTime(&st);
|
||||||
|
char stamp[64] = {};
|
||||||
|
std::snprintf(stamp,
|
||||||
|
sizeof(stamp),
|
||||||
|
"%04u%02u%02u_%02u%02u%02u_pid%lu",
|
||||||
|
st.wYear,
|
||||||
|
st.wMonth,
|
||||||
|
st.wDay,
|
||||||
|
st.wHour,
|
||||||
|
st.wMinute,
|
||||||
|
st.wSecond,
|
||||||
|
GetCurrentProcessId());
|
||||||
|
|
||||||
|
return dir + "\\gvfg_xdma_backend_" + stamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string part_path() const
|
||||||
|
{
|
||||||
|
char part[16] = {};
|
||||||
|
std::snprintf(part, sizeof(part), "_part%02u.log", part_index_);
|
||||||
|
return base_path_ + part;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool open_locked()
|
||||||
|
{
|
||||||
|
if (file_)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (base_path_.empty())
|
||||||
|
base_path_ = make_base_path();
|
||||||
|
|
||||||
|
const std::string path = part_path();
|
||||||
|
file_ = std::fopen(path.c_str(), "ab");
|
||||||
|
bytes_written_ = 0;
|
||||||
|
if (!file_)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
std::fseek(file_, 0, SEEK_END);
|
||||||
|
const long pos = std::ftell(file_);
|
||||||
|
bytes_written_ = pos > 0 ? static_cast<uint64_t>(pos) : 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rotate_locked()
|
||||||
|
{
|
||||||
|
if (file_)
|
||||||
|
{
|
||||||
|
std::fclose(file_);
|
||||||
|
file_ = nullptr;
|
||||||
|
}
|
||||||
|
bytes_written_ = 0;
|
||||||
|
++part_index_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool enabled_ = false;
|
||||||
|
std::mutex mutex_;
|
||||||
|
std::FILE *file_ = nullptr;
|
||||||
|
uint64_t bytes_written_ = 0;
|
||||||
|
unsigned part_index_ = 1;
|
||||||
|
std::string base_path_;
|
||||||
|
};
|
||||||
|
|
||||||
|
static XdmaTraceSink &xdma_trace_sink()
|
||||||
|
{
|
||||||
|
static XdmaTraceSink sink;
|
||||||
|
return sink;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xdma_trace_log(bool forceDebugOutput, const char *tag, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
char msg[1024] = {};
|
char msg[1024] = {};
|
||||||
va_list args;
|
va_list args;
|
||||||
@@ -183,11 +341,11 @@ namespace
|
|||||||
vsnprintf(msg, sizeof(msg), fmt, args);
|
vsnprintf(msg, sizeof(msg), fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
char line[1152] = {};
|
xdma_trace_sink().write(forceDebugOutput, tag, msg);
|
||||||
snprintf(line, sizeof(line), "[GVFG][XDMA][hotplug] %s\n", msg);
|
|
||||||
OutputDebugStringA(line);
|
|
||||||
}
|
}
|
||||||
#define XDMA_HOTPLUG_LOG(...) xdma_hotplug_log(__VA_ARGS__)
|
|
||||||
|
#define XDMA_LOG(...) xdma_trace_log(false, "", __VA_ARGS__)
|
||||||
|
#define XDMA_HOTPLUG_LOG(...) xdma_trace_log(true, "[hotplug]", __VA_ARGS__)
|
||||||
|
|
||||||
static std::string wide_to_utf8(const std::wstring &s)
|
static std::string wide_to_utf8(const std::wstring &s)
|
||||||
{
|
{
|
||||||
@@ -668,6 +826,11 @@ namespace gvfg::internal
|
|||||||
}
|
}
|
||||||
save_frames_after_plug_in_.store(0);
|
save_frames_after_plug_in_.store(0);
|
||||||
fix_pulsed_after_plug_in_.store(false);
|
fix_pulsed_after_plug_in_.store(false);
|
||||||
|
last_video_irq_ns_.store(0, std::memory_order_relaxed);
|
||||||
|
last_irq_clear_ns_.store(0, std::memory_order_relaxed);
|
||||||
|
last_irq_enable_ns_.store(0, std::memory_order_relaxed);
|
||||||
|
last_irq_clear_ok_.store(0, std::memory_order_relaxed);
|
||||||
|
last_irq_enable_ok_.store(0, std::memory_order_relaxed);
|
||||||
|
|
||||||
// Match CaptureDemo's hot-plug-safe startup order: make the event/data
|
// Match CaptureDemo's hot-plug-safe startup order: make the event/data
|
||||||
// workers ready first, then clear stale IRQs, toggle capture, and only
|
// workers ready first, then clear stale IRQs, toggle capture, and only
|
||||||
@@ -996,6 +1159,7 @@ namespace gvfg::internal
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
last_video_irq_ns_.store(steady_now_ns(), std::memory_order_relaxed);
|
||||||
uint32_t pending = 0;
|
uint32_t pending = 0;
|
||||||
if (capture_active_)
|
if (capture_active_)
|
||||||
{
|
{
|
||||||
@@ -1035,8 +1199,46 @@ namespace gvfg::internal
|
|||||||
uint32_t pendingAfterPop = 0;
|
uint32_t pendingAfterPop = 0;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
data_cv_.wait(lock, [this]()
|
while (running_ && !data_worker_stop_ && (!capture_active_ || pending_events_ == 0))
|
||||||
{ return (capture_active_ && pending_events_ > 0) || !running_ || data_worker_stop_; });
|
{
|
||||||
|
if (data_cv_.wait_for(lock, std::chrono::seconds(1)) == std::cv_status::timeout)
|
||||||
|
{
|
||||||
|
const int active = capture_active_.load(std::memory_order_relaxed) ? 1 : 0;
|
||||||
|
const uint32_t pending = pending_events_;
|
||||||
|
const uint64_t latest = latest_sequence_;
|
||||||
|
const uint64_t delivered = delivered_sequence_;
|
||||||
|
const uint64_t captured = stats_.frames_captured;
|
||||||
|
const uint64_t dropped = stats_.frames_dropped;
|
||||||
|
const uint64_t nowNs = steady_now_ns();
|
||||||
|
const uint64_t lastIrqNs = last_video_irq_ns_.load(std::memory_order_relaxed);
|
||||||
|
const uint64_t lastClearNs = last_irq_clear_ns_.load(std::memory_order_relaxed);
|
||||||
|
const uint64_t lastEnableNs = last_irq_enable_ns_.load(std::memory_order_relaxed);
|
||||||
|
const int clearOk = last_irq_clear_ok_.load(std::memory_order_relaxed);
|
||||||
|
const int enableOk = last_irq_enable_ok_.load(std::memory_order_relaxed);
|
||||||
|
lock.unlock();
|
||||||
|
uint32_t fpgaStatus = 0;
|
||||||
|
uint32_t captureEnable = 0;
|
||||||
|
const bool statusOk = read_user_reg(kFpgaStatusReg, fpgaStatus);
|
||||||
|
const bool captureEnableOk = read_user_reg(capture_enable_reg(), captureEnable);
|
||||||
|
XDMA_LOG("data_thread: heartbeat waiting_irq active=%d pending=%u latest=%llu delivered=%llu captured=%llu dropped=%llu irq_age_ms=%llu clear=%d/%llums enable=%d/%llums fpga_status=%d/0x%08x capture_enable=%d/0x%08x",
|
||||||
|
active,
|
||||||
|
pending,
|
||||||
|
static_cast<unsigned long long>(latest),
|
||||||
|
static_cast<unsigned long long>(delivered),
|
||||||
|
static_cast<unsigned long long>(captured),
|
||||||
|
static_cast<unsigned long long>(dropped),
|
||||||
|
static_cast<unsigned long long>(lastIrqNs == 0 ? UINT64_MAX : (nowNs - lastIrqNs) / 1000000ull),
|
||||||
|
clearOk,
|
||||||
|
static_cast<unsigned long long>(lastClearNs == 0 ? UINT64_MAX : (nowNs - lastClearNs) / 1000000ull),
|
||||||
|
enableOk,
|
||||||
|
static_cast<unsigned long long>(lastEnableNs == 0 ? UINT64_MAX : (nowNs - lastEnableNs) / 1000000ull),
|
||||||
|
statusOk ? 1 : 0,
|
||||||
|
fpgaStatus,
|
||||||
|
captureEnableOk ? 1 : 0,
|
||||||
|
captureEnable);
|
||||||
|
lock.lock();
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!running_ || data_worker_stop_)
|
if (!running_ || data_worker_stop_)
|
||||||
break;
|
break;
|
||||||
if (!capture_active_)
|
if (!capture_active_)
|
||||||
@@ -1055,9 +1257,13 @@ namespace gvfg::internal
|
|||||||
// A VIDEO_INT means one frame is ready in C2H. Acknowledge the FPGA
|
// A VIDEO_INT means one frame is ready in C2H. Acknowledge the FPGA
|
||||||
// interrupt, re-enable XDMA user interrupt delivery, then read the
|
// interrupt, re-enable XDMA user interrupt delivery, then read the
|
||||||
// full video frame from c2h_N.
|
// full video frame from c2h_N.
|
||||||
write_user_reg(kInterruptClearReg, video_event_mask());
|
const bool clearHighOk = write_user_reg(kInterruptClearReg, video_event_mask());
|
||||||
write_user_reg(kInterruptClearReg, 0);
|
const bool clearLowOk = write_user_reg(kInterruptClearReg, 0);
|
||||||
enable_user_event(video_event_mask());
|
last_irq_clear_ok_.store(clearHighOk && clearLowOk ? 1 : 0, std::memory_order_relaxed);
|
||||||
|
last_irq_clear_ns_.store(steady_now_ns(), std::memory_order_relaxed);
|
||||||
|
const bool enableOk = enable_user_event(video_event_mask());
|
||||||
|
last_irq_enable_ok_.store(enableOk ? 1 : 0, std::memory_order_relaxed);
|
||||||
|
last_irq_enable_ns_.store(steady_now_ns(), std::memory_order_relaxed);
|
||||||
|
|
||||||
if (!capture_active_)
|
if (!capture_active_)
|
||||||
continue;
|
continue;
|
||||||
@@ -1067,17 +1273,38 @@ namespace gvfg::internal
|
|||||||
uint8_t *slotData = nullptr;
|
uint8_t *slotData = nullptr;
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mutex_);
|
std::unique_lock<std::mutex> lock(mutex_);
|
||||||
data_cv_.wait(lock, [this]()
|
auto hasFreeSlot = [this]()
|
||||||
{
|
{
|
||||||
if (!running_ || data_worker_stop_)
|
|
||||||
return true;
|
|
||||||
for (const FrameSlot &slot : frame_ring_)
|
for (const FrameSlot &slot : frame_ring_)
|
||||||
{
|
{
|
||||||
if (!slot.in_use)
|
if (!slot.in_use)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
};
|
||||||
|
while (running_ && !data_worker_stop_ && !hasFreeSlot())
|
||||||
|
{
|
||||||
|
if (data_cv_.wait_for(lock, std::chrono::seconds(1)) == std::cv_status::timeout)
|
||||||
|
{
|
||||||
|
const uint64_t activeSlot = active_delivery_slot_ == static_cast<size_t>(-1)
|
||||||
|
? UINT64_MAX
|
||||||
|
: static_cast<uint64_t>(active_delivery_slot_);
|
||||||
|
const uint64_t latest = latest_sequence_;
|
||||||
|
const uint64_t delivered = delivered_sequence_;
|
||||||
|
const uint64_t captured = stats_.frames_captured;
|
||||||
|
const uint64_t dropped = stats_.frames_dropped;
|
||||||
|
const uint64_t ringSize = static_cast<uint64_t>(frame_ring_.size());
|
||||||
|
lock.unlock();
|
||||||
|
XDMA_LOG("data_thread: heartbeat waiting_free_slot active_slot=%llu ring=%llu latest=%llu delivered=%llu captured=%llu dropped=%llu",
|
||||||
|
static_cast<unsigned long long>(activeSlot),
|
||||||
|
static_cast<unsigned long long>(ringSize),
|
||||||
|
static_cast<unsigned long long>(latest),
|
||||||
|
static_cast<unsigned long long>(delivered),
|
||||||
|
static_cast<unsigned long long>(captured),
|
||||||
|
static_cast<unsigned long long>(dropped));
|
||||||
|
lock.lock();
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!running_ || data_worker_stop_)
|
if (!running_ || data_worker_stop_)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -1153,6 +1380,7 @@ namespace gvfg::internal
|
|||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
|
XDMA_LOG("data_thread: unhandled exception");
|
||||||
running_ = false;
|
running_ = false;
|
||||||
capture_active_ = false;
|
capture_active_ = false;
|
||||||
frame_cv_.notify_all();
|
frame_cv_.notify_all();
|
||||||
@@ -1689,11 +1917,25 @@ namespace gvfg::internal
|
|||||||
return last_error_.c_str();
|
return last_error_.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void XdmaCaptureSession::get_debug_stats(xdma_stream_stats_t &outStats, uint64_t &outWaitTimeouts) const
|
void XdmaCaptureSession::get_debug_stats(xdma_stream_stats_t &outStats,
|
||||||
|
uint64_t &outWaitTimeouts,
|
||||||
|
xdma_debug_state_t &outDebugState) const
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
outStats = stats_;
|
outStats = stats_;
|
||||||
outWaitTimeouts = wait_timeout_count_;
|
outWaitTimeouts = wait_timeout_count_;
|
||||||
|
std::memset(&outDebugState, 0, sizeof(outDebugState));
|
||||||
|
outDebugState.running = running_.load(std::memory_order_relaxed) ? 1 : 0;
|
||||||
|
outDebugState.capture_active = capture_active_.load(std::memory_order_relaxed) ? 1 : 0;
|
||||||
|
outDebugState.data_worker_stop = data_worker_stop_.load(std::memory_order_relaxed) ? 1 : 0;
|
||||||
|
outDebugState.pending_events = pending_events_;
|
||||||
|
outDebugState.latest_sequence = latest_sequence_;
|
||||||
|
outDebugState.delivered_sequence = delivered_sequence_;
|
||||||
|
outDebugState.active_delivery_slot = active_delivery_slot_ == static_cast<size_t>(-1)
|
||||||
|
? UINT64_MAX
|
||||||
|
: static_cast<uint64_t>(active_delivery_slot_);
|
||||||
|
outDebugState.next_write_slot = static_cast<uint64_t>(next_write_slot_);
|
||||||
|
outDebugState.ring_size = static_cast<uint64_t>(frame_ring_.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t XdmaCaptureSession::active_input_path() const
|
uint32_t XdmaCaptureSession::active_input_path() const
|
||||||
|
|||||||
@@ -45,7 +45,9 @@ namespace gvfg::internal
|
|||||||
xdma_status_t release_frame(const xdma_frame_t &frame);
|
xdma_status_t release_frame(const xdma_frame_t &frame);
|
||||||
|
|
||||||
const char *last_error() const;
|
const char *last_error() const;
|
||||||
void get_debug_stats(xdma_stream_stats_t &outStats, uint64_t &outWaitTimeouts) const;
|
void get_debug_stats(xdma_stream_stats_t &outStats,
|
||||||
|
uint64_t &outWaitTimeouts,
|
||||||
|
xdma_debug_state_t &outDebugState) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
xdma_status_t open_device(const XdmaDevice &device);
|
xdma_status_t open_device(const XdmaDevice &device);
|
||||||
@@ -110,6 +112,11 @@ namespace gvfg::internal
|
|||||||
std::atomic<bool> data_worker_stop_{false};
|
std::atomic<bool> data_worker_stop_{false};
|
||||||
std::atomic<int> save_frames_after_plug_in_{0};
|
std::atomic<int> save_frames_after_plug_in_{0};
|
||||||
std::atomic<bool> fix_pulsed_after_plug_in_{false};
|
std::atomic<bool> fix_pulsed_after_plug_in_{false};
|
||||||
|
std::atomic<uint64_t> last_video_irq_ns_{0};
|
||||||
|
std::atomic<uint64_t> last_irq_clear_ns_{0};
|
||||||
|
std::atomic<uint64_t> last_irq_enable_ns_{0};
|
||||||
|
std::atomic<int> last_irq_clear_ok_{0};
|
||||||
|
std::atomic<int> last_irq_enable_ok_{0};
|
||||||
std::thread event_thread_[4];
|
std::thread event_thread_[4];
|
||||||
std::thread data_thread_;
|
std::thread data_thread_;
|
||||||
|
|
||||||
|
|||||||
@@ -797,8 +797,9 @@ struct gvfg_handle_t
|
|||||||
if (backend)
|
if (backend)
|
||||||
{
|
{
|
||||||
xdma_stream_stats_t stats{};
|
xdma_stream_stats_t stats{};
|
||||||
|
xdma_debug_state_t debugState{};
|
||||||
uint64_t waitTimeouts = 0;
|
uint64_t waitTimeouts = 0;
|
||||||
backend->get_debug_stats(stats, waitTimeouts);
|
backend->get_debug_stats(stats, waitTimeouts, debugState);
|
||||||
out.backend_state = static_cast<int>(stats.state);
|
out.backend_state = static_cast<int>(stats.state);
|
||||||
out.backend_frames_captured = stats.frames_captured;
|
out.backend_frames_captured = stats.frames_captured;
|
||||||
out.backend_frames_delivered = stats.frames_delivered;
|
out.backend_frames_delivered = stats.frames_delivered;
|
||||||
@@ -806,6 +807,15 @@ struct gvfg_handle_t
|
|||||||
out.backend_dma_errors = stats.dma_errors;
|
out.backend_dma_errors = stats.dma_errors;
|
||||||
out.backend_interrupt_count = stats.interrupt_count;
|
out.backend_interrupt_count = stats.interrupt_count;
|
||||||
out.backend_wait_timeouts = waitTimeouts;
|
out.backend_wait_timeouts = waitTimeouts;
|
||||||
|
out.backend_running = debugState.running;
|
||||||
|
out.backend_capture_active = debugState.capture_active;
|
||||||
|
out.backend_data_worker_stop = debugState.data_worker_stop;
|
||||||
|
out.backend_pending_events = debugState.pending_events;
|
||||||
|
out.backend_latest_sequence = debugState.latest_sequence;
|
||||||
|
out.backend_delivered_sequence = debugState.delivered_sequence;
|
||||||
|
out.backend_active_delivery_slot = debugState.active_delivery_slot;
|
||||||
|
out.backend_next_write_slot = debugState.next_write_slot;
|
||||||
|
out.backend_ring_size = debugState.ring_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
return GVFG_OK;
|
return GVFG_OK;
|
||||||
@@ -1063,7 +1073,20 @@ extern "C"
|
|||||||
{
|
{
|
||||||
if (!handle || !out_stats)
|
if (!handle || !out_stats)
|
||||||
return GVFG_EINVAL;
|
return GVFG_EINVAL;
|
||||||
return handle->getDebugBackendStats(*out_stats);
|
|
||||||
|
const uint32_t callerSize = out_stats->struct_size;
|
||||||
|
if (callerSize < sizeof(uint32_t) || callerSize > sizeof(gvfg_debug_backend_stats_t))
|
||||||
|
return GVFG_EINVAL;
|
||||||
|
|
||||||
|
gvfg_debug_backend_stats_t stats{};
|
||||||
|
const gvfg_status_t status = handle->getDebugBackendStats(stats);
|
||||||
|
if (status != GVFG_OK)
|
||||||
|
return status;
|
||||||
|
|
||||||
|
stats.struct_size = sizeof(stats);
|
||||||
|
std::memcpy(out_stats, &stats, callerSize);
|
||||||
|
out_stats->struct_size = callerSize;
|
||||||
|
return GVFG_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
gvfg_status_t gvfg_debug_get_fpga_signal_raw(gvfg_handle handle,
|
gvfg_status_t gvfg_debug_get_fpga_signal_raw(gvfg_handle handle,
|
||||||
|
|||||||
Reference in New Issue
Block a user