Skip to content

Commit

Permalink
Profiler: Setup for usage on Windows
Browse files Browse the repository at this point in the history
This will get gpuviz working under Wine.
  • Loading branch information
Sonicadvance1 committed Jan 9, 2025
1 parent 2293d30 commit d7fe334
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 24 deletions.
54 changes: 39 additions & 15 deletions FEXCore/Source/Utils/Profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <linux/magic.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <time.h>
#endif

#include <FEXCore/Utils/LogManager.h>
Expand All @@ -18,6 +19,34 @@
#define BACKEND_GPUVIS 1

#ifdef ENABLE_FEXCORE_PROFILER
#ifndef _WIN32
static inline uint64_t GetTime() {
// We want the time in the least amount of overhead possible
// clock_gettime will do a VDSO call with the least amount of overhead
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1'000'000'000ULL + ts.tv_nsec;
}
#else

static inline uint64_t GetTime() {
// GetTime needs to return nanoseconds, query the interface.
static uint64_t FrequencyScale = {};
if (!FrequencyScale) [[unlikely]] {
LARGE_INTEGER Frequency{};
while (!QueryPerformanceFrequency(&Frequency));
constexpr uint64_t NanosecondsInSecond = 1'000'000'000ULL;

// On WINE this will always result in a scale of 100.
FrequencyScale = NanosecondsInSecond / Frequency.QuadPart;
}
LARGE_INTEGER ticks;
while (!QueryPerformanceCounter(&ticks));
return ticks.QuadPart * FrequencyScale;
}

#endif

#if FEXCORE_PROFILER_BACKEND == BACKEND_GPUVIS
namespace FEXCore::Profiler {
ProfilerBlock::ProfilerBlock(std::string_view const Format)
Expand All @@ -41,23 +70,18 @@ static std::array<const char*, 2> TraceFSDirectories {
"/sys/kernel/debug/tracing",
};

static bool IsTraceFS(const char* Path) {
struct statfs stat;
if (statfs(Path, &stat)) {
return false;
}
return stat.f_type == TRACEFS_MAGIC;
}

void Init() {
for (auto Path : TraceFSDirectories) {
if (IsTraceFS(Path)) {
fextl::string FilePath = fextl::fmt::format("{}/trace_marker", Path);
TraceFD = open(FilePath.c_str(), O_WRONLY | O_CLOEXEC);
if (TraceFD != -1) {
// Opened TraceFD, early exit
break;
}
#ifdef _WIN32
constexpr auto flags = O_WRONLY;
#else
constexpr auto flags = O_WRONLY | O_CLOEXEC;
#endif
fextl::string FilePath = fextl::fmt::format("{}/trace_marker", Path);
TraceFD = open(FilePath.c_str(), flags);
if (TraceFD != -1) {
// Opened TraceFD, early exit
break;
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions FEXCore/include/FEXCore/Utils/Profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#pragma once
#include <cstdint>
#include <string_view>
#include <time.h>

#include <FEXCore/Utils/CompilerDefs.h>

Expand All @@ -14,14 +13,6 @@ FEX_DEFAULT_VISIBILITY void Shutdown();
FEX_DEFAULT_VISIBILITY void TraceObject(std::string_view const Format);
FEX_DEFAULT_VISIBILITY void TraceObject(std::string_view const Format, uint64_t Duration);

static inline uint64_t GetTime() {
// We want the time in the least amount of overhead possible
// clock_gettime will do a VDSO call with the least amount of overhead
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1'000'000'000ULL + ts.tv_nsec;
}

// A class that follows scoping rules to generate a profile duration block
class ProfilerBlock final {
public:
Expand Down

0 comments on commit d7fe334

Please sign in to comment.