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
7 changes: 7 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ platform setup (Vulkan SDK, GLFW, etc.).
module, any thread): `Fling::Screenshot::Request(path, callback)` in
`Screenshot.h`; the editor's GPU Options window has a Take screenshot button. Detail:
[`Skills/testing.md`](Skills/testing.md#seeing-the-game-without-a-window).
- See where the time goes: add `--profile` to any run (it works with `--null-render`,
`--offscreen` and a window) and a table of timed scopes prints after the exit line.
A scope is `FLING_PROFILE_SCOPE("Name")` from `Profiler.h` (Core): off it costs one
atomic load. Shipping builds compile it out; `-DFLING_PROFILER=ON|OFF` overrides
that in any build ([`Skills/building.md`](Skills/building.md)). Measure in the build mode you care about:
Debug is much slower than RelWithDebInfo for per-entity work. In a window, use `--play`
(a window without it starts the editor, which runs no ticks).
- Drive a headless run with `--replay-input <file>`. Agents can write their own
`.input.jsonl` files. Format and flags: [`Skills/input-playback.md`](Skills/input-playback.md).
- Load a level other than `[Game] StartLevel` with `--level <file>` (relative
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ OPTION( WITH_CRASH_HANDLER_FLAG "Builds CrashHandler: --unattended crash handlin
set( FLING_PHYSICS_PROVIDER "Jolt" CACHE STRING "Physics provider to build: Jolt, Bullet, None, or a provider of your own" )
list( APPEND FLING_PHYSICS_PROVIDER_PATHS "${CMAKE_CURRENT_SOURCE_DIR}/FlingEngine" )

# The instrumented profiler (Profiler.h, --profile). AUTO: in every build except shipping. ON or OFF
# forces it, for example ON to profile a shipping build, or OFF to compile it out of a dev build.
set( FLING_PROFILER "AUTO" CACHE STRING "Profiler: AUTO (on unless DEFINE_SHIPPING), ON or OFF" )
set_property( CACHE FLING_PROFILER PROPERTY STRINGS AUTO ON OFF )
if( FLING_PROFILER STREQUAL "AUTO" )
if( DEFINE_SHIPPING )
set( FLING_PROFILER_ENABLED OFF )
else()
set( FLING_PROFILER_ENABLED ON )
endif()
elseif( FLING_PROFILER )
set( FLING_PROFILER_ENABLED ON )
else()
set( FLING_PROFILER_ENABLED OFF )
endif()

OPTION( ENABLE_MULTICORE "ENABLE_MULTICORE will allow MSVC to use all cores by adding the /MP option" ON)

# We can't have the editor without ImGUI!
Expand All @@ -69,6 +85,7 @@ message( STATUS "WITH_EDITOR_FLAG=${WITH_EDITOR_FLAG}" )
message( STATUS "WITH_IMGUI_FLAG=${WITH_IMGUI_FLAG}" )
message( STATUS "WITH_CRASH_HANDLER_FLAG=${WITH_CRASH_HANDLER_FLAG}" )
message( STATUS "DEFINE_SHIPPING=${DEFINE_SHIPPING}" )
message( STATUS "FLING_PROFILER=${FLING_PROFILER} (enabled: ${FLING_PROFILER_ENABLED})" )
message( STATUS "ENABLE_MULTICORE=${ENABLE_MULTICORE}" )
message( STATUS "FLING_BUILD_TESTS=${FLING_BUILD_TESTS}" )
message( STATUS "FLING_PHYSICS_PROVIDER=${FLING_PHYSICS_PROVIDER}" )
Expand Down
27 changes: 24 additions & 3 deletions FlingEngine/Application/src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "Misc/CrashHandler.h"
#include "Physics.h"
#include "PresentMode.h"
#include "Profiler.h"
#include "Random.h"
#include "RegisterGraphics.h"
#include "Screenshot.h"
Expand Down Expand Up @@ -582,6 +583,9 @@ namespace Fling
// sends the messages of a tick over its N frames
const float NullRenderDeltaTime = 1.0f / static_cast<float>(m_Settings.SimHz * m_Options.FramesPerTick);

#if WITH_PROFILER
Profiler::SetEnabled(m_Options.Profile);
#endif
const std::chrono::steady_clock::time_point LoopStart = std::chrono::steady_clock::now();
while (ShouldTick())
{
Expand All @@ -591,13 +595,20 @@ namespace Fling
// for its ticks, so a run is as fast as the CPU allows
const float DeltaTime = m_Options.IsHeadless() ? NullRenderDeltaTime : Timing.GetDeltaTime();
Stats::Frames::FrameStats(DeltaTime);
FLING_PROFILE_SCOPE("Frame");

PumpInput(DeltaTime);
m_Controls->OnFrame(PlatformTime::NowNanoseconds());
{
FLING_PROFILE_SCOPE("Frame.Input");
PumpInput(DeltaTime);
m_Controls->OnFrame(PlatformTime::NowNanoseconds());
}

// The play requests, this frame's ticks (the game thread runs them itself in
// threaded play), the ticks' outbox, then the frame systems on what the world shows
m_World->Update(DeltaTime);
{
FLING_PROFILE_SCOPE("Frame.WorldUpdate");
m_World->Update(DeltaTime);
}
DumpNewTicks();
WriteStateDumps();

Expand Down Expand Up @@ -706,6 +717,7 @@ namespace Fling
VulkanApp::Get().SetVSync(m_Controls->VSync());
VulkanApp::Get().SetFrustumCulling(m_Controls->FrustumCulling());
VulkanApp::Get().SetOcclusionCulling(m_Controls->OcclusionCulling());
FLING_PROFILE_SCOPE("Frame.Present");
VulkanApp::Get().Update(t_DeltaTime, m_World->GetPresentedStorage());

// Offscreen there is no window: no focus, no monitor, and no frame cap (headless)
Expand Down Expand Up @@ -987,6 +999,15 @@ namespace Fling
}

PrintExitSummary(t_Code);
#if WITH_PROFILER
if (m_Options.Profile)
{
const double WallSeconds =
std::chrono::duration<double>(std::chrono::steady_clock::now() - m_StartTime).count();
std::printf("%s", Profiler::Report(WallSeconds).c_str());
std::fflush(stdout);
}
#endif

// Last, so that every system above can still log while it shuts down
Logger::Get().Shutdown();
Expand Down
2 changes: 2 additions & 0 deletions FlingEngine/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ target_compile_definitions(Core PUBLIC
# Input recording and playback (--record-input, --replay-input, the editor UI).
# Shipping builds do not have it, so a shipped game cannot be driven by an input file.
WITH_INPUT_RECORDING=$<NOT:$<BOOL:${DEFINE_SHIPPING}>>
# The instrumented profiler (FLING_PROFILER in the root CMakeLists.txt): off in shipping builds unless asked for
WITH_PROFILER=$<BOOL:${FLING_PROFILER_ENABLED}>
$<$<BOOL:${DEFINE_SHIPPING}>:FLING_SHIPPING>
)

Expand Down
4 changes: 4 additions & 0 deletions FlingEngine/Core/inc/LaunchOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ namespace Fling
* --occlusion-culling on|off
* Skip the meshes that other meshes hide (on) or draw every mesh
* (off). Overrides [Engine] OcclusionCulling
* --profile Time the FLING_PROFILE_SCOPE scopes and print a table of them at exit
* --thread-jitter S:U --exec threaded only: sleep a random 0..U microseconds (seed S) before each wake
* and after each publish on the game thread, and before each frame
*/
Expand Down Expand Up @@ -112,6 +113,9 @@ namespace Fling

bool Unattended = false;

/** Time the profiled scopes and print the table at exit (--profile). See Profiler.h. */
bool Profile = false;

/** Number of frames to run before the engine exits (--frames). 0 means no limit. */
uint32 Frames = 0;

Expand Down
95 changes: 95 additions & 0 deletions FlingEngine/Core/inc/Profiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#pragma once

#include "CoreAPI.h"
#include "FlingTypes.h"
#include "Strings/String.h"

#include <chrono>

#if WITH_PROFILER

namespace Fling
{
/**
* Instrumented timing for the engine. A FLING_PROFILE_SCOPE("Name") at the top of a
* block times that block, on any thread. Scopes with the same name add up, so a scope in
* a loop shows its total, its call count and its slowest call.
*
* It is off until --profile (or Profiler::SetEnabled) turns it on. An off scope costs one
* relaxed atomic load. An on scope costs two clock reads and three stores into the
* calling thread's own table: no lock, no allocation after the first call on a thread.
* Shipping builds compile every scope out.
*
* Profiler::Report gives the table: Engine prints it at exit with --profile.
*/
class Profiler
{
public:
/** The most scopes (different names) that one run can have. */
static constexpr uint32 MaxScopes = 256;

static CORE_API bool IsEnabled();
static CORE_API void SetEnabled(bool t_On);

/** The id of a scope name. The name must stay valid for the whole run (a string literal). */
static CORE_API uint32 Register(const char* t_Name);

/** Add one timed call to the calling thread's table. */
static CORE_API void Record(uint32 t_Id, int64 t_Nanoseconds);

/** Forget every recorded call (the names stay). */
static CORE_API void Reset();

/**
* A table of every scope, slowest total first: calls, total, average and max in
* milliseconds, and each total as a share of t_WallSeconds when that is above 0.
* Times add up across threads, so a total can pass the wall time.
*/
static CORE_API String Report(double t_WallSeconds);
};

/** The timer behind FLING_PROFILE_SCOPE. */
class ProfileScope
{
public:
explicit ProfileScope(const uint32 t_Id) : m_Id(t_Id), m_On(Profiler::IsEnabled())
{
if (m_On)
{
m_Start = std::chrono::steady_clock::now();
}
}

~ProfileScope()
{
if (m_On)
{
const auto Elapsed = std::chrono::steady_clock::now() - m_Start;
Profiler::Record(m_Id, std::chrono::duration_cast<std::chrono::nanoseconds>(Elapsed).count());
}
}

ProfileScope(const ProfileScope&) = delete;
ProfileScope& operator=(const ProfileScope&) = delete;

private:
uint32 m_Id;
bool m_On;
std::chrono::steady_clock::time_point m_Start;
};
} // namespace Fling

#define FLING_PROFILE_CONCAT_INNER(A, B) A##B
#define FLING_PROFILE_CONCAT(A, B) FLING_PROFILE_CONCAT_INNER(A, B)
/** Time the rest of the enclosing block. Name is a string literal. */
#define FLING_PROFILE_SCOPE(Name) \
static const uint32 FLING_PROFILE_CONCAT(FlingProfileId, __LINE__) = ::Fling::Profiler::Register(Name); \
::Fling::ProfileScope FLING_PROFILE_CONCAT(FlingProfileScope, \
__LINE__)(FLING_PROFILE_CONCAT(FlingProfileId, __LINE__))

#else

// Compiled out (WITH_PROFILER is 0): a scope is nothing, and there is no Profiler class
#define FLING_PROFILE_SCOPE(Name) ((void)0)

#endif
11 changes: 10 additions & 1 deletion FlingEngine/Core/src/LaunchOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Fling
constexpr StringView DumpStateFlag = "dump-state";
constexpr StringView DumpStateTicksFlag = "dump-state-ticks";
constexpr StringView UnattendedFlag = "unattended";
constexpr StringView ProfileFlag = "profile";
constexpr StringView FramesFlag = "frames";
constexpr StringView FramesPerTickFlag = "frames-per-tick";
constexpr StringView SeedFlag = "seed";
Expand Down Expand Up @@ -219,10 +220,18 @@ namespace Fling
return false;
}

if (!ReadBool(t_CmdLine, UnattendedFlag, t_Out.Unattended, t_OutError))
if (!ReadBool(t_CmdLine, UnattendedFlag, t_Out.Unattended, t_OutError) ||
!ReadBool(t_CmdLine, ProfileFlag, t_Out.Profile, t_OutError))
{
return false;
}
#if !WITH_PROFILER
if (t_Out.Profile)
{
t_OutError = "--profile is not available in this build: configure with -DFLING_PROFILER=ON";
return false;
}
#endif

if (!ReadBool(t_CmdLine, OffscreenFlag, t_Out.Offscreen, t_OutError))
{
Expand Down
Loading