aboutsummaryrefslogtreecommitdiffstats
path: root/source/core/core_profile.cpp
blob: fb90f0a0433f5a2a3b57193226f59f3432c5177e (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//- registry

internal void profile_arena_register(const char *name, Arena *arena)
{
	Profiler *profiler = global_profiler;
	if(!arena) return;
	spin_lock(&profiler->lock);
	if(profiler->arena_count < PROFILE_MAX_ARENAS) {
		ProfileArena *entry = &profiler->arenas[profiler->arena_count++];
		snprintf(entry->name, sizeof(entry->name), "%s", name);
		entry->arena = arena;
	}
	spin_unlock(&profiler->lock);
}

internal void profile_thread_register(const char *name)
{
	Profiler     *profiler = global_profiler;
	ProfileThread thread   = {};
	snprintf(thread.name, sizeof(thread.name), "%s", name);
	thread.tid  = (s32)gettid();
	thread.main = (thread.tid == (s32)getpid());
	pthread_getcpuclockid(pthread_self(), &thread.clock);

	// NOTE: once per thread, never on a hot path (for the main thread glibc parses /proc here)
	pthread_attr_t attr;
	if(!thread.main && pthread_getattr_np(pthread_self(), &attr) == 0) {
		void  *low  = NULL;
		size_t size = 0;
		pthread_attr_getstack(&attr, &low, &size);
		pthread_attr_destroy(&attr);
		thread.stack_low  = (u8 *)low;
		thread.stack_size = size;
	}
	if(thread.main) {
		struct rlimit limit;
		getrlimit(RLIMIT_STACK, &limit);
		thread.stack_size = (limit.rlim_cur == RLIM_INFINITY) ? 0 : limit.rlim_cur;
	}

	spin_lock(&profiler->lock);
	if(profiler->thread_count < PROFILE_MAX_THREADS) {
		profiler->threads[profiler->thread_count++] = thread;
	}
	spin_unlock(&profiler->lock);

	if(tls_scratch_arena) {
		char arena_name[32];
		snprintf(arena_name, sizeof(arena_name), "scratch: %s", name);
		profile_arena_register(arena_name, tls_scratch_arena);
	}
}

//- frames

internal void profile_frame(Profiler *profiler, f32 frame_ms, f32 ui_ms, f32 render_ms, f32 idle_ms)
{
	u32 head = profiler->frame_head;
	profiler->frame_ms[head]    = frame_ms;
	profiler->ui_ring[head]     = ui_ms;
	profiler->render_ring[head] = render_ms;
	profiler->idle_ring[head]   = idle_ms;
	profiler->frame_head        = (head + 1) % PROFILE_FRAMES;
	++profiler->frames_total;
}

//- sampling

internal u64 profile_status_value(const char *text, const char *key)
{
	const char *at = strstr(text, key);
	return at ? (u64)strtoull(at + strlen(key), NULL, 10) * 1024ull : 0; // the kB fields
}

internal u64 profile_line_value(const char *text, const char *key)
{
	const char *at = strstr(text, key);
	return at ? (u64)strtoull(at + strlen(key), NULL, 10) : 0;
}

internal s64 profile_read(const char *path, char *buffer, u32 size)
{
	int fd = open(path, O_RDONLY | O_CLOEXEC);
	if(fd < 0) return -1;
	ssize_t got = read(fd, buffer, size - 1);
	close(fd);
	buffer[got > 0 ? got : 0] = 0;
	return got;
}

internal int profile_compare_f32(const void *a, const void *b)
{
	f32 x = *(const f32 *)a, y = *(const f32 *)b;
	return (x > y) - (x < y);
}

internal void profile_stats(f32 *ring, u32 count, f32 *sorted, f32 *mean)
{
	MemCpy(sorted, ring, sizeof(f32) * count);
	qsort(sorted, count, sizeof(f32), profile_compare_f32);
	f32 sum = 0;
	for(u32 index = 0; index < count; ++index) sum += sorted[index];
	*mean = sum / (f32)count;
}

internal void profile_sample(Profiler *profiler)
{
	u64 now = time_now_ns();
	f32 dt  = profiler->sample_ns ? (f32)((f64)(now - profiler->sample_ns) / 1e9) : 0.0f;
	profiler->sample_ns = now;
	if(!profiler->page_size) profiler->page_size = (u64)sysconf(_SC_PAGESIZE);

	char text[4096];

	//- process memory
	if(profile_read("/proc/self/status", text, sizeof(text)) > 0) {
		profiler->rss       = profile_status_value(text, "VmRSS:");
		profiler->rss_peak  = profile_status_value(text, "VmHWM:");
		profiler->rss_anon  = profile_status_value(text, "RssAnon:");
		profiler->rss_file  = profile_status_value(text, "RssFile:");
		profiler->rss_shmem = profile_status_value(text, "RssShmem:");
		profiler->vm_size   = profile_status_value(text, "VmSize:");
		profiler->vm_data   = profile_status_value(text, "VmData:");
		profiler->vm_stack  = profile_status_value(text, "VmStk:");
		profiler->vm_lib    = profile_status_value(text, "VmLib:");
	}

	//- the malloc heap
	struct mallinfo2 info = mallinfo2();
	profiler->heap_arena   = info.arena;
	profiler->heap_in_use  = info.uordblks;
	profiler->heap_free    = info.fordblks;
	profiler->heap_mmapped = info.hblkhd;

	u64 mallocs = AtomicLoadRelaxed(&profiler->malloc_count);
	u64 frees   = AtomicLoadRelaxed(&profiler->free_count);
	if(dt > 0) {
		profiler->mallocs_per_s = (f32)(mallocs - profiler->last_malloc_count) / dt;
		profiler->frees_per_s   = (f32)(frees - profiler->last_free_count) / dt;
	}
	profiler->last_malloc_count = mallocs;
	profiler->last_free_count   = frees;

	//- the whole process
	struct rusage usage;
	getrusage(RUSAGE_SELF, &usage);
	u64 cpu = (u64)(usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) * 1000000000ull + (u64)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) * 1000ull;
	if(dt > 0) {
		profiler->minflt_per_s = (f32)((u64)usage.ru_minflt - profiler->minflt) / dt;
		profiler->majflt_per_s = (f32)((u64)usage.ru_majflt - profiler->majflt) / dt;
		profiler->csw_per_s    = (f32)((u64)(usage.ru_nvcsw + usage.ru_nivcsw) - (profiler->vcsw + profiler->ivcsw)) / dt;
		profiler->cpu_process  = (f32)((f64)(cpu - profiler->cpu_process_ns) / 1e9) / dt;
	}
	profiler->cpu_process_ns = cpu;
	profiler->minflt = (u64)usage.ru_minflt;
	profiler->majflt = (u64)usage.ru_majflt;
	profiler->vcsw   = (u64)usage.ru_nvcsw;
	profiler->ivcsw  = (u64)usage.ru_nivcsw;

	//- threads
	for(u32 index = 0; index < profiler->thread_count; ++index) {
		ProfileThread  *thread = &profiler->threads[index];
		struct timespec spec;
		if(clock_gettime(thread->clock, &spec) == 0) {
			u64 cpu_ns = (u64)spec.tv_sec * 1000000000ull + (u64)spec.tv_nsec;
			if(dt > 0 && thread->cpu_ns) thread->cpu = (f32)((f64)(cpu_ns - thread->cpu_ns) / 1e9) / dt;
			thread->cpu_ns = cpu_ns;
		}

		char path[64];
		snprintf(path, sizeof(path), "/proc/self/task/%d/stat", thread->tid);
		if(profile_read(path, text, sizeof(text)) > 0) {
			// NOTE: fields after "(comm)", from 0: state ppid pgrp session tty tpgid flags minflt(7) cminflt majflt(9)
			const char *at = strrchr(text, ')');
			u64 fields[12] = {};
			for(u32 field = 0; at && field < 12; ++field) {
				at = strchr(at + 1, ' ');
				if(at) fields[field] = strtoull(at + 1, NULL, 10);
			}
			u64 minflt = fields[7];
			if(dt > 0 && thread->minflt) thread->minflt_per_s = (f32)(minflt - thread->minflt) / dt;
			thread->minflt = minflt;
			thread->majflt = fields[9];
		}
		snprintf(path, sizeof(path), "/proc/self/task/%d/status", thread->tid);
		if(profile_read(path, text, sizeof(text)) > 0) {
			u64 vcsw  = profile_line_value(text, "voluntary_ctxt_switches:\t");
			u64 ivcsw = profile_line_value(text, "nonvoluntary_ctxt_switches:\t");
			if(dt > 0 && (thread->vcsw || thread->ivcsw)) thread->csw_per_s = (f32)((vcsw + ivcsw) - (thread->vcsw + thread->ivcsw)) / dt;
			thread->vcsw  = vcsw;
			thread->ivcsw = ivcsw;
		}

		// NOTE: stack high water mark: pages of the stack mapping that were ever touched are
		// resident. mincore over the mapping, 512 bytes of vector for an 8MB stack on 16K pages.
		if(thread->main) {
			thread->stack_touched = profiler->vm_stack;
		} else if(thread->stack_low && thread->stack_size) {
			u64 pages = thread->stack_size / profiler->page_size;
			u8  vector[4096];
			if(pages <= sizeof(vector) && mincore(thread->stack_low, thread->stack_size, vector) == 0) {
				u64 resident = 0;
				for(u64 page = 0; page < pages; ++page) resident += vector[page] & 1;
				thread->stack_touched = resident * profiler->page_size;
			}
		}
	}

	//- arenas
	for(u32 index = 0; index < profiler->arena_count; ++index) {
		ProfileArena *entry = &profiler->arenas[index];
		u64 count = entry->arena->alloc_count;
		if(dt > 0) entry->allocs_per_s = (f32)(count - entry->last_count) / dt;
		entry->last_count = count;
	}

	//- frame statistics, all over the same window of the last 256 rendered frames
	u32 frames = Min(profiler->frames_total, (u32)PROFILE_FRAMES);
	if(frames) {
		f32 sorted[PROFILE_FRAMES];
		f32 mean = 0;
		profile_stats(profiler->frame_ms, frames, sorted, &mean);
		profiler->p50     = sorted[frames / 2];
		profiler->p99     = sorted[(frames * 99) / 100];
		profiler->worst   = sorted[frames - 1];
		profiler->average = mean;
		profile_stats(profiler->ui_ring, frames, sorted, &profiler->ui_ms);
		profiler->ui_p99 = sorted[(frames * 99) / 100];
		profile_stats(profiler->render_ring, frames, sorted, &profiler->render_ms);
		profiler->render_p99 = sorted[(frames * 99) / 100];
		profile_stats(profiler->idle_ring, frames, sorted, &profiler->idle_ms);
	}
}