Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
profiler.hpp
Go to the documentation of this file.
1/*
2 Copyright 2026 JigSawFr
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
19#include <aurore/core/event.hpp>
20#include <aurore/core/time.hpp>
21
22#include <cstdint>
23#include <functional>
24
25namespace aurore
26{
27
28// What one iteration of the main loop cost, measured while the Profiler is
29// enabled. Times are real microseconds, even under the test harness'
30// simulated clock.
32{
33 Time events = 0; // the platform's event pump (Platform::mainLoopIteration, which may wait)
34 Time input = 0; // touch, mouse, buttons and keys handled
35 Time update = 0; // highlight, tickings, sync tasks, run loop event, views deleted
36 Time layout = 0; // yoga layout, wherever it ran (not counted in the phase it ran in)
37 Time draw = 0; // the views, highlight and notifications drawn into nanovg
38 Time inspector = 0; // the inspector's panel drawn, while it is open
39 Time flush = 0; // nanovg's flush (nvgEndFrame)
40 Time swap = 0; // VideoContext::endFrame (buffer swap, present)
42 Time interval = 0; // since the iteration before started (0 for the first one measured)
43
44 uint32_t layouts = 0; // layout passes (a tree laid out from its root)
45 uint32_t viewsLaidOut = 0; // views given a new layout (yoga NodeLayout events)
46 uint32_t drawCalls = 0; // nanovg's draw calls for the views (the inspector's not counted)
47 uint32_t triangles = 0; // and their triangles
48 uint64_t allocations = 0; // through the allocation counter, 0 without one
49 bool drawn = false; // false: redraw on demand skipped the frame
50};
51
52// Per-frame cost of the main loop: time per phase, layouts, allocations.
53// Off by default; on with setEnabled(true) or AURORE_PROFILE=1 in the
54// environment (desktop), which also logs averages every 120 frames. The
55// inspector's panel shows it, and measures while it is open.
56// The bench (tests/bench.cpp) reads it frame by frame.
58{
59 public:
60 static void setEnabled(bool enabled);
61 static bool isEnabled() { return enabled; }
62
63 // Enabled, or the inspector open
64 static bool isMeasuring();
65
66 // The last main-loop iteration measured
67 static const FrameProfile& getLastFrame() { return last; }
68
69 // Fired after each iteration measured; getLastFrame() has it
70 static Event<>* getFrameEvent() { return &frameEvent; }
71
72 // A function returning the number of allocations made so far. Counting
73 // them is the program's business (a replaced global operator new, as
74 // the bench does); without one, allocations stay 0.
75 static void setAllocationCounter(std::function<uint64_t()> counter) { allocationCounter = std::move(counter); }
76
77 // The library's own calls
78 enum class Phase
79 {
80 EVENTS,
81 INPUT,
82 UPDATE,
83 DRAW,
85 FLUSH,
86 SWAP,
87 };
88 static void beginFrame();
89 static void endPhase(Phase phase);
90 static void endFrame(bool drawn);
91 static void addLayout(Time duration);
92 static void addViewLaidOut();
93 static void setDrawStats(uint32_t drawCalls, uint32_t triangles);
94
95 private:
96 inline static bool enabled = false;
97 inline static bool logging = false;
98 inline static FrameProfile current;
99 inline static FrameProfile last;
100 inline static Time phaseStart = 0;
101 inline static Time frameStart = 0;
102 inline static Time lastStart = 0; // frameStart of the iteration before
103 inline static Time layoutMark = 0; // current.layout when the phase started
104 inline static uint64_t allocationsStart = 0;
105 inline static Event<> frameEvent;
106 inline static std::function<uint64_t()> allocationCounter;
107};
108
109} // namespace aurore
Definition event.hpp:41
Definition profiler.hpp:58
static void setEnabled(bool enabled)
static void setDrawStats(uint32_t drawCalls, uint32_t triangles)
static void beginFrame()
static void endPhase(Phase phase)
static bool isEnabled()
Definition profiler.hpp:61
static const FrameProfile & getLastFrame()
Definition profiler.hpp:67
static bool isMeasuring()
static void addLayout(Time duration)
Phase
Definition profiler.hpp:79
static void endFrame(bool drawn)
static Event * getFrameEvent()
Definition profiler.hpp:70
static void setAllocationCounter(std::function< uint64_t()> counter)
Definition profiler.hpp:75
static void addViewLaidOut()
Definition actions.hpp:25
retro_time_t Time
Definition time.hpp:31
Definition profiler.hpp:32
Time flush
Definition profiler.hpp:39
Time draw
Definition profiler.hpp:37
uint32_t drawCalls
Definition profiler.hpp:46
bool drawn
Definition profiler.hpp:49
Time swap
Definition profiler.hpp:40
uint32_t triangles
Definition profiler.hpp:47
uint32_t layouts
Definition profiler.hpp:44
Time layout
Definition profiler.hpp:36
uint32_t viewsLaidOut
Definition profiler.hpp:45
Time total
Definition profiler.hpp:41
uint64_t allocations
Definition profiler.hpp:48
Time interval
Definition profiler.hpp:42
Time input
Definition profiler.hpp:34
Time events
Definition profiler.hpp:33
Time inspector
Definition profiler.hpp:38
Time update
Definition profiler.hpp:35