// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project // SPDX-License-Identifier: GPL-3.0-or-later #pragma once #include #include #include #include #include #include "common/common_types.h" #include "common/logging.h" #include "common/cpu_features.h" // EDEN_NX2_GPU_PROFILE: time where the GPU thread spends its time. Each zone counts its own // (exclusive) time, nested zones excluded; every 10 s the thread logs each zone's share. namespace VideoCommon::Nx2Profile { enum class Zone : u32 { Other, ///< GPU thread time outside every zone below (register writes, command parsing) Idle, PendingSleep, ResumePending, Push, Tick, Run, FlushInvalidate, Macro, Draw, Clear, Compute, FlushWork, PipelineLookup, Configure, SyncDescriptors, ReadHandles, FillImageViews, TextureBuffers, UpdateBuffers, DescriptorAcquire, BindStageBuffers, PushDescriptors, RenderTargets, ConfigureDraw, DynamicState, DrawRecord, TextureUpload, Fence, ///< Fences made (SignalFence) FenceSkipped, ///< Empty fences not made Method, ///< Engine method calls (what a split GPU thread's back half would run) SplitDrain, ///< Split GPU thread: the front waits for the back (gpu_split.h) SplitWait, ///< Split GPU thread: the back waits for work Count, }; inline constexpr std::array(Zone::Count)> zone_names{ "other", "idle", "pending sleep", "resume pending", "push", "tick", "run", "flush/inval", "macro", "draw", "clear", "compute", "flush work", "pipeline lookup", "configure", "sync descriptors", "read handles", "fill image views", "texture buffers", "update buffers", "descriptor acquire", "bind stage buffers", "push descriptors", "render targets", "configure draw", "dynamic state", "draw record", "texture upload", "fence", "fence skipped", "method", "split drain", "split wait", }; // Read once at startup: Enabled() runs several times per method write (no guard to check). inline const bool g_enabled = std::getenv("EDEN_NX2_GPU_PROFILE") != nullptr; inline bool Enabled() { return g_enabled; } // Nanoseconds; from the host's own counter where the wall clock reads it directly (the zones // run ~150,000 times a second, and QueryPerformanceCounter cost ~5% of the GPU thread). inline u64 Now() { return static_cast(Common::g_wall_clock.GetTimeNS().count()); } struct ThreadData { std::array(Zone::Count)> exclusive_ns{}; std::array(Zone::Count)> count{}; std::array counters{}; ///< See Counter u64 child_ns{}; ///< Time of the zones nested in the current one u64 window_start{}; }; inline thread_local ThreadData t_data; class Scope { public: explicit Scope(Zone zone_) : zone{zone_}, enabled{Enabled()} { if (!enabled) { return; } parent_child_ns = t_data.child_ns; t_data.child_ns = 0; start = Now(); } ~Scope() { if (!enabled) { return; } const u64 elapsed = Now() - start; auto& data = t_data; const auto index = static_cast(zone); data.exclusive_ns[index] += elapsed - data.child_ns; ++data.count[index]; data.child_ns = parent_child_ns + elapsed; } Scope(const Scope&) = delete; Scope& operator=(const Scope&) = delete; private: Zone zone; bool enabled; u64 start{}; u64 parent_child_ns{}; }; /// Amounts counted on the GPU thread, logged per second with the zones. enum class Counter : u32 { DownloadBytes, ///< Buffer data copied back from the host GPU at fences DownloadCopies, ///< Buffer ranges copied back DmaReadBytes, ///< Guest memory the native MME read with DMA DownloadBytesOnDmaPages, ///< Of DownloadBytes: on pages the MME has DMA-read MethodWrites, ///< 3D register writes (Maxwell3D::CallMethod below the macro range) SameValueWrites, ///< Of MethodWrites: the register already held the value AcquireChecks, ///< Semaphore acquires evaluated AcquireWaits, ///< Of AcquireChecks: not met yet (the channel waits) }; /// Device pages (4 KiB) the native MME has DMA-read since start (GPU thread only; filled only /// while profiling): which buffer downloads anything on the GPU thread reads back. inline std::unordered_set& DmaReadPages() { static std::unordered_set pages; return pages; } inline void Add(Counter counter, u64 amount) { if (Enabled()) { t_data.counters[static_cast(counter)] += amount; } } /// Called by the GPU thread (and the split's back thread) between commands: logs and resets /// the totals every 10 s. inline void Report(const char* thread_name = "GPU thread") { if (!Enabled()) { return; } auto& data = t_data; const u64 now = Now(); if (data.window_start == 0) { data.window_start = now; data.child_ns = 0; return; } const u64 window = now - data.window_start; if (window < 10'000'000'000ULL) { return; } // Time in no zone at all is the wall time minus the zones' totals. u64 zoned{}; for (size_t i = 1; i < data.exclusive_ns.size(); ++i) { zoned += data.exclusive_ns[i]; } data.exclusive_ns[0] = window > zoned ? window - zoned : 0; std::string line; for (size_t i = 0; i < data.exclusive_ns.size(); ++i) { if (data.exclusive_ns[i] == 0 && data.count[i] == 0) { continue; } // Per second of wall time: milliseconds spent and calls made. const double ms = static_cast(data.exclusive_ns[i]) * 1000.0 / window; const double calls = static_cast(data.count[i]) * 1e9 / window; line += fmt::format(" | {} {:.1f} ms ({:.0f}/s)", zone_names[i], ms, calls); } line += fmt::format(" | buffer downloads {:.0f} KiB in {:.0f} copies ({:.0f} KiB on pages " "MME DMA reads) | MME DMA reads {:.0f} KiB", static_cast(data.counters[0]) * 1e9 / 1024.0 / window, static_cast(data.counters[1]) * 1e9 / window, static_cast(data.counters[3]) * 1e9 / 1024.0 / window, static_cast(data.counters[2]) * 1e9 / 1024.0 / window); line += fmt::format(" | 3D register writes {:.0f} ({:.0f} same value)", static_cast(data.counters[4]) * 1e9 / window, static_cast(data.counters[5]) * 1e9 / window); line += fmt::format(" | acquires {:.0f} ({:.0f} waited)", static_cast(data.counters[6]) * 1e9 / window, static_cast(data.counters[7]) * 1e9 / window); LOG_INFO(HW_GPU, "{} per second:{}", thread_name, line); data = {}; data.window_start = now; } } // namespace VideoCommon::Nx2Profile