Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 60 additions & 14 deletions core/libs/prx/libSceAgcDriver/Execution/src/Driver/Lifecycle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
#include <cstdint>
#include <cstdio>
#include <cerrno>
#include <condition_variable>
#include <exception>
#include <mutex>
#include <stdexcept>
#include <system_error>
#include <thread>
Expand Down Expand Up @@ -171,6 +173,62 @@ void Driver::Shutdown() {
rethrowFailure();
}

#ifdef APS5_PROFILE_GENERATE
extern "C" void __gcov_dump(void);
extern "C" void __gcov_reset(void);

namespace {

struct ProfileDumps {
std::mutex mutex;
std::condition_variable wake;
std::thread thread;
bool stopping = false;
};

ProfileDumps& profileDumps() {
static auto* const dumps = new ProfileDumps();
return *dumps;
}

void dumpProfile() {
__gcov_dump();
__gcov_reset();
}

void startProfileDumps() {
auto& dumps = profileDumps();
std::lock_guard lock(dumps.mutex);
if (dumps.stopping || dumps.thread.joinable()) return;
dumps.thread = std::thread([&dumps] {
std::unique_lock wait(dumps.mutex);
while (!dumps.wake.wait_for(wait, std::chrono::seconds(20), [&] { return dumps.stopping; })) dumpProfile();
});
}

void stopProfileDumps() {
auto& dumps = profileDumps();
{
std::lock_guard lock(dumps.mutex);
if (dumps.stopping) return;
dumps.stopping = true;
}
dumps.wake.notify_one();
if (dumps.thread.joinable()) dumps.thread.join();
dumpProfile();
}

}
#else
namespace {

void startProfileDumps() {}

void stopProfileDumps() {}

}
#endif

void Driver::stop() {
require(!onWorkerThread(), "worker cannot stop itself");
std::lock_guard shutdownLock(shutdownMutex);
Expand Down Expand Up @@ -214,24 +272,12 @@ void Driver::stop() {
FlushAsyncLogAtEnd_nid_no_patch();
StopProfileOutput_nid_no_patch();
CaptureTrace::Stop();
stopProfileDumps();
stopped = true;
}

#ifdef APS5_PROFILE_GENERATE
extern "C" void __gcov_dump(void);
extern "C" void __gcov_reset(void);
#endif

Driver::Driver() {
#ifdef APS5_PROFILE_GENERATE
std::thread([] {
for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(20));
__gcov_dump();
__gcov_reset();
}
}).detach();
#endif
startProfileDumps();
InstallAsyncLog_nid_no_patch();
static const bool driverMemory = (MapDriverMemory(), true);
static_cast<void>(driverMemory);
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ With `ANYPS5_USE_SYSTEM_DEPS=ON` those libraries come from the system (on Debian
The AGC driver's hot paths (the queue worker, the draw finisher and the draw committer) gain from a profile-guided build. Use a separate build directory:

1. Configure with `-DAPS5_AGC_PROFILE=generate` and build `libs`. Only `libSceAgcDriver` and `shader_recompiler_agc_driver` are instrumented: the runtime libraries export all their symbols, so instrumenting every library fails at link time.
2. Play the scenes to optimize for. The instrumented driver writes its `.gcda` files next to its objects every 20 seconds and resets its counters, so a session ended by closing the emulator keeps everything up to the last dump.
2. Play the scenes to optimize for. The instrumented driver writes its `.gcda` files next to its objects every 20 seconds and resets its counters, so a session ended by closing the emulator keeps everything up to the last dump. The driver's shutdown stops that thread and dumps once more. Check that the session reached the scenes before you build with the profile: a session that stayed in a menu still leaves `.gcda` files and the `use` build reports nothing (`-Wno-missing-profile`), but the driver is then laid out for the menu and can run the scenes slower than a plain build. The draw commits on the `[commits]` lines (`APS5_PIPELINE_STATS=1`) show whether the session drew the scenes. Delete the old `.gcda` files before recording again: a later session adds its counts to them.
3. Configure the same directory with `-DAPS5_AGC_PROFILE=use` and build `libs` again. Functions changed after the recording lose their profile (`-Wno-coverage-mismatch`), so record again after large changes.

`-DCMAKE_CXX_FLAGS="-march=x86-64-v3"` adds a further small gain on CPUs that have AVX2; such a build does not run on older CPUs. libc keeps `-ffp-contract=off` either way.
Expand Down