Skip to content

Commit

Permalink
Merge branch 'origin/lvengesanam/update_command_queue_history' into '…
Browse files Browse the repository at this point in the history
…main'

Logging history of commands sent between client and server on crashes

See merge request lightspeedrtx/bridge-remix-nv!21
  • Loading branch information
lvengesanam committed Aug 23, 2023
2 parents 5c66703 + 82455fa commit 4a0203f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
11 changes: 9 additions & 2 deletions build_bridge.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ function Build {
[string]$BuildFlavour,

[Parameter(Mandatory)]
[string]$BuildSubDir
[string]$BuildSubDir,

[string]$BuildTarget
)

SetupVS -Platform $Platform
PerformBuild -Platform $Platform -BuildFlavour $BuildFlavour -BuildSubDir $BuildSubDir

if ($BuildTarget) {
PerformBuild -Backend ninja -Platform $Platform -BuildFlavour $BuildFlavour -BuildSubDir $BuildSubDir -BuildTarget $BuildTarget
} else {
PerformBuild -Backend vs -Platform $Platform -BuildFlavour $BuildFlavour -BuildSubDir $BuildSubDir
}
}
17 changes: 13 additions & 4 deletions build_common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,50 @@ function SetupVS {

function PerformBuild {
param(
[Parameter(Mandatory)]
[string]$Backend,

[Parameter(Mandatory)]
[string]$Platform,

[Parameter(Mandatory)]
[string]$BuildFlavour,

[Parameter(Mandatory)]
[string]$BuildSubDir
[string]$BuildSubDir,

[string]$BuildTarget
)

$CurrentDir = Get-Location
$OutputDir = [IO.Path]::Combine($CurrentDir, "_output")
$BuildDir = [IO.Path]::Combine($CurrentDir, $BuildSubDir)

Push-Location $CurrentDir
$mesonArgs = "setup --buildtype `"$BuildFlavour`" --backend vs `"$BuildSubDir`" --debug"
$mesonArgs = "setup --buildtype `"$BuildFlavour`" --backend `"$Backend`" `"$BuildSubDir`" --debug"
Start-Process "meson" -ArgumentList $mesonArgs -wait
Pop-Location

if ( $LASTEXITCODE -ne 0 ) {
Write-Output "Failed to run meson setup"
exit $LASTEXITCODE
}

# Note: Remove this if we modify Meson to include this copy step instead. For now only here so it only executes on build machines.
Copy-Item "Directory.Build.Props" -Destination $BuildDir

Push-Location $BuildDir
if ( $BuildTarget ) {
& meson compile unit_tests
}
else {
# The x86 solution platform is called Win32 so we have to fix up the parameter
If ($Platform -match "x86") {
& "msbuild" @('bridge.sln', '/t:Build', '/p:Platform="Win32"', '/p:BuildProjectReferences=true', '/m', '/p:BuildInParallel=true')
}
Else {
& "msbuild" @('bridge.sln', '/t:Build', '/p:Platform="x64"', '/p:BuildProjectReferences=true', '/m', '/p:BuildInParallel=true')
}
}
Pop-Location

if ( $LASTEXITCODE -ne 0 ) {
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,4 @@ bridge_version = vcs_tag(
output: 'version.h')

subdir('src')
subdir('test')
10 changes: 10 additions & 0 deletions src/client/d3d9_lss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,16 @@ void RemixDetach() {
delete gpServer;
}

// Log history of recent client side commands sent and received by the server
Logger::info("Most recent Device Queue commands sent from Client");
DeviceBridge::Command::print_writer_data_sent();
Logger::info("Most recent Device Queue commands received by Server");
DeviceBridge::Command::print_writer_data_received();
Logger::info("Most recent Module Queue commands sent from Client");
ModuleBridge::Command::print_writer_data_sent();
Logger::info("Most recent Module Queue commands received by Server");
ModuleBridge::Command::print_writer_data_received();

// Clean up resources
delete gpPresent;

Expand Down
10 changes: 10 additions & 0 deletions src/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,16 @@ void CALLBACK OnClientExited(void* context, BOOLEAN isTimeout) {
Logger::err("The client process has unexpectedly exited, shutting down server as well!");
gbBridgeRunning = false;

// Log history of recent client side commands sent and received by the server
Logger::info("Most recent Device Queue commands sent from Client");
DeviceBridge::Command::print_reader_data_sent();
Logger::info("Most recent Device Queue commands received by Server");
DeviceBridge::Command::print_reader_data_received();
Logger::info("Most recent Module Queue commands sent from Client");
ModuleBridge::Command::print_reader_data_sent();
Logger::info("Most recent Module Queue commands received by Server");
ModuleBridge::Command::print_reader_data_received();

// Give the server some time to shut down, but then force quit so it doesn't hang forever
uint32_t numRetries = 0;
uint32_t maxRetries = ServerOptions::getShutdownRetries();
Expand Down
37 changes: 34 additions & 3 deletions src/util/util_atomiccircularqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <cstdio>
#include <atomic>
#include <assert.h>
#include <vector>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

Expand Down Expand Up @@ -88,7 +89,7 @@ namespace bridge_util {
ULONGLONG start = 0, curTick;
do {
const auto currentRead = m_read->load(std::memory_order_relaxed);
const auto nextRead = inc(currentRead);
const auto nextRead = queueIdxInc(currentRead);
if (nextRead != m_write->load(std::memory_order_acquire)) {
m_data[currentRead] = obj;
// The store above is not atomic. Issue a membar after it to ensure
Expand Down Expand Up @@ -143,7 +144,7 @@ namespace bridge_util {
do {
const auto currentWrite = m_write->load(std::memory_order_relaxed);
if (currentWrite != m_read->load(std::memory_order_acquire)) {
m_write->store(inc(currentWrite), std::memory_order_release);
m_write->store(queueIdxInc(currentWrite), std::memory_order_release);
// Issue a membar before reading the data since it is not atomic
std::atomic_thread_fence(std::memory_order_seq_cst);
result = Result::Success;
Expand All @@ -167,9 +168,39 @@ namespace bridge_util {
return currentWrite == m_read->load(std::memory_order_acquire);
}

uint32_t inc(uint32_t idx) const {
std::vector<Commands::D3D9Command> buildQueueData(int maxQueueElements, int currentIndex) {
std::vector<Commands::D3D9Command> commandHistory;
int itemCount = 0;
while (itemCount < m_queueSize && itemCount < maxQueueElements) {
// To prevent adding default commands in the Queue to the command list
if (m_data[currentIndex].command == Commands::Bridge_Invalid)
break;
commandHistory.push_back(m_data[currentIndex].command);
currentIndex = queueIdxDec(currentIndex);
++itemCount;
}
return commandHistory;
}

std::vector<Commands::D3D9Command> getWriterQueueData(int maxQueueElements=10) {
const auto currentRead = m_read->load(std::memory_order_relaxed);
int currentIndex = queueIdxDec(currentRead);
return buildQueueData(maxQueueElements, currentIndex);
}

std::vector<Commands::D3D9Command> getReaderQueueData(int maxQueueElements = 10) {
const auto currentWrite = m_write->load(std::memory_order_relaxed);
int currentIndex = queueIdxDec(currentWrite);
return buildQueueData(maxQueueElements, currentIndex);
}

uint32_t queueIdxInc(uint32_t idx) const {
return idx + 1 < m_queueSize ? idx + 1 : 0;
}

uint32_t queueIdxDec(uint32_t idx) const {
return idx == 0 ? m_queueSize - 1 : idx - 1 ;
}
};

}
29 changes: 27 additions & 2 deletions src/util/util_bridgecommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "util_bridge_state.h"
#include "util_ipcchannel.h"
#include "util_singleton.h"

#include "../tracy/tracy.hpp"

extern bool gbBridgeRunning;
Expand Down Expand Up @@ -292,7 +291,33 @@ class Bridge {
static inline void reset_counter() {
s_cmdCounter = 0;
}


static inline void print_data(std::string prefix, std::vector<Commands::D3D9Command>& commandList) {
for (const auto& currentCommand : commandList) {
Logger::info(prefix + toString(currentCommand));
}
}

static inline void print_writer_data_sent() {
std::vector<Commands::D3D9Command> resultCommands = s_pWriterChannel->commands->getWriterQueueData();
print_data("Command sent: ", resultCommands);
}

static inline void print_writer_data_received() {
std::vector<Commands::D3D9Command> resultCommands = s_pWriterChannel->commands->getReaderQueueData();
print_data("Command received: ", resultCommands);
}

static inline void print_reader_data_sent() {
std::vector<Commands::D3D9Command> resultCommands = s_pReaderChannel->commands->getWriterQueueData();
print_data("Command sent: ", resultCommands);
}

static inline void print_reader_data_received() {
std::vector<Commands::D3D9Command> resultCommands = s_pReaderChannel->commands->getReaderQueueData();
print_data("Command received: ", resultCommands);
}

private:
const Commands::D3D9Command m_command;
const uint32_t m_handle;
Expand Down
1 change: 1 addition & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subdir('rtx/unit')

0 comments on commit 4a0203f

Please sign in to comment.