aboutsummaryrefslogtreecommitdiffstats
path: root/source/core/core_profile.h
blob: eed941885a9ca4276ddafb72c1a6a2cc57a60593 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef CORE_PROFILE_H
#define CORE_PROFILE_H

// NOTE: measuring without paying for it.
//
// always on, a handful of nanoseconds each:
//   - arena allocation counts / bytes / peak (fields in Arena, plain increments, one thread each)
//   - process wide malloc / free counts and live heap bytes: the host interposes malloc and bumps
//     relaxed atomics that each sit on their own cache line, so threads never fight over one
//   - per frame and per audio callback timers (vdso clock_gettime, no syscall)
//
// sampled only while a Profiler view is on screen, twice a second, on the main thread:
//   - /proc/self/status (rss, peak rss, anon / file / stack), mallinfo2, getrusage
//   - per registered thread: cpu time (thread clock), page faults and context switches
//     (/proc/self/task/<tid>), touched stack (mincore over the thread's stack)

#define PROFILE_MAX_THREADS 24
#define PROFILE_MAX_ARENAS  32
#define PROFILE_FRAMES      256

enum ProfileUIPhase {
	PROFILE_UI_KEYBOARD,
	PROFILE_UI_PANELS,
	PROFILE_UI_STATUS,
	PROFILE_UI_OVERLAY,
	PROFILE_UI_PHASES,
};
#define PROFILE_SITES       4096 // allocation site table, pow2

typedef struct ProfileThread ProfileThread;
struct ProfileThread {
	char      name[16];
	s32       tid;
	b32       main;
	clockid_t clock;
	u8       *stack_low;   // lowest address of the stack mapping
	u64       stack_size;
	// sampled
	u64 stack_touched;     // resident stack pages: the high water mark
	u64 cpu_ns;
	f32 cpu;               // fraction of one core
	u64 minflt, majflt;
	u64 vcsw, ivcsw;
	f32 minflt_per_s;
	f32 csw_per_s;
};

typedef struct ProfileArena ProfileArena;
struct ProfileArena {
	char   name[24];
	Arena *arena;
	u64    last_count;
	f32    allocs_per_s;
};

typedef struct Profiler Profiler;
struct Profiler {
	//- heap, written from any thread by the malloc interposer
	CacheAligned u64 malloc_count;
	CacheAligned u64 free_count;
	CacheAligned s64 heap_live;   // bytes, from malloc_usable_size
	CacheAligned u64 heap_peak;
	CacheAligned u64 heap_total;  // bytes ever handed out

	//- allocation sites (MP3_PLAYER_ALLOC_SITES=1): caller address -> count, lock free
	b32 track_sites;
	u64 site_address[PROFILE_SITES];
	u64 site_count[PROFILE_SITES];
	u64 site_bytes[PROFILE_SITES];

	//- registry
	CacheAligned SpinLock lock;
	ProfileThread threads[PROFILE_MAX_THREADS];
	u32           thread_count;
	ProfileArena  arenas[PROFILE_MAX_ARENAS];
	u32           arena_count;

	//- frames, main thread
	// NOTE: rings over the last 256 rendered frames, every statistic comes from the same window
	f32 frame_ms[PROFILE_FRAMES];  // cpu ms per rendered frame
	f32 ui_ring[PROFILE_FRAMES];   // building the ui
	f32 render_ring[PROFILE_FRAMES]; // vk_render (wait + record + submit) and friends
	f32 idle_ring[PROFILE_FRAMES]; // blocked in poll before the frame
	u32 frame_head;
	u32 frames_total;
	f32 ui_ms, ui_p99;             // over the ring, at the last sample
	f32 render_ms, render_p99;
	f32 idle_ms;
	f32 ui_phase_us[4];            // ProfileUIPhase, smoothed, written by the ui each frame

	//- samples, main thread, 2 hz while visible
	u64 sample_ns;
	b32 sampling;    // a Profiler view is on screen
	u64 page_size;
	u64 rss, rss_peak, rss_anon, rss_file, rss_shmem, vm_size, vm_data, vm_stack, vm_lib;
	u64 heap_arena, heap_in_use, heap_free, heap_mmapped;
	u64 minflt, majflt, vcsw, ivcsw;
	f32 minflt_per_s, majflt_per_s, csw_per_s;
	f32 mallocs_per_s, frees_per_s;
	u64 last_malloc_count, last_free_count;
	f32 cpu_process;
	u64 cpu_process_ns;
	f32 p50, p99, worst, average;  // frame ms over the ring
};

global_variable Profiler  profiler_instance;
global_variable Profiler *global_profiler = &profiler_instance;

internal void profile_thread_register(const char *name);           // call from the thread itself
internal void profile_arena_register(const char *name, Arena *arena);
internal void profile_frame(Profiler *profiler, f32 frame_ms, f32 ui_ms, f32 render_ms, f32 idle_ms);
internal void profile_sample(Profiler *profiler);                   // main thread

#endif // CORE_PROFILE_H