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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
//- 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);
#if PLATFORM_LINUX
thread.tid = (s32)gettid();
thread.main = (thread.tid == (s32)getpid());
clockid_t clock = 0;
pthread_getcpuclockid(pthread_self(), &clock);
thread.clock = (u64)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;
}
#elif PLATFORM_APPLE
u64 tid = 0;
pthread_threadid_np(NULL, &tid);
thread.tid = (s32)tid;
thread.main = pthread_main_np() != 0;
thread.clock = (u64)pthread_mach_thread_np(pthread_self());
// NOTE: stackaddr is the top, the stack grows down from it
u8 *top = (u8 *)pthread_get_stackaddr_np(pthread_self());
thread.stack_size = (u64)pthread_get_stacksize_np(pthread_self());
thread.stack_low = top - thread.stack_size;
#elif PLATFORM_WINDOWS
// NOTE: the main thread registers first (ENTRYPOINT), there is no tid == pid on windows
thread.tid = (s32)GetCurrentThreadId();
thread.main = (profiler->thread_count == 0);
HANDLE handle = NULL;
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(), &handle, THREAD_QUERY_LIMITED_INFORMATION, FALSE, 0);
thread.clock = (u64)(umm)handle;
ULONG_PTR low = 0, high = 0;
GetCurrentThreadStackLimits(&low, &high);
thread.stack_low = (u8 *)low;
thread.stack_size = (u64)(high - low);
#endif
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;
}
#if PLATFORM_LINUX || PLATFORM_APPLE
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;
}
#endif
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 PLATFORM_LINUX
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((clockid_t)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;
}
}
}
#elif PLATFORM_APPLE
if(!profiler->page_size) profiler->page_size = (u64)getpagesize();
//- process memory
mach_task_basic_info_data_t task = {};
mach_msg_type_number_t task_count = MACH_TASK_BASIC_INFO_COUNT;
if(task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&task, &task_count) == KERN_SUCCESS) {
profiler->rss = (u64)task.resident_size;
profiler->rss_peak = (u64)task.resident_size_max;
profiler->vm_size = (u64)task.virtual_size;
}
//- the malloc heap (default zone)
malloc_statistics_t heap = {};
malloc_zone_statistics(NULL, &heap);
profiler->heap_arena = (u64)heap.size_allocated;
profiler->heap_in_use = (u64)heap.size_in_use;
profiler->heap_free = (u64)(heap.size_allocated - heap.size_in_use);
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];
thread_basic_info_data_t info = {};
mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;
if(thread_info((thread_act_t)thread->clock, THREAD_BASIC_INFO, (thread_info_t)&info, &count) == KERN_SUCCESS) {
u64 cpu_ns = ((u64)(info.user_time.seconds + info.system_time.seconds) * 1000000ull +
(u64)(info.user_time.microseconds + info.system_time.microseconds)) * 1000ull;
if(dt > 0 && thread->cpu_ns) thread->cpu = (f32)((f64)(cpu_ns - thread->cpu_ns) / 1e9) / dt;
thread->cpu_ns = cpu_ns;
}
}
#elif PLATFORM_WINDOWS
if(!profiler->page_size) {
SYSTEM_INFO info;
GetSystemInfo(&info);
profiler->page_size = (u64)info.dwPageSize;
}
//- process memory
PROCESS_MEMORY_COUNTERS_EX memory = {};
memory.cb = sizeof(memory);
if(GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS *)&memory, sizeof(memory))) {
profiler->rss = (u64)memory.WorkingSetSize;
profiler->rss_peak = (u64)memory.PeakWorkingSetSize;
profiler->rss_anon = (u64)memory.PrivateUsage;
profiler->vm_size = (u64)memory.PagefileUsage;
profiler->vm_data = (u64)memory.PrivateUsage;
}
//- the malloc heap: the crt allocates from the process heap, HeapSummary sizes it
// (kernel32, windows 10+; looked up so older systems just show zeros). the per call malloc
// counters come from the glibc interposer and stay linux only.
typedef struct Win32HeapSummary { DWORD cb; SIZE_T allocated, committed, reserved, max_reserve; } Win32HeapSummary;
typedef BOOL WINAPI Win32HeapSummaryProc(HANDLE heap, DWORD flags, Win32HeapSummary *summary);
local_persist Win32HeapSummaryProc *heap_summary = (Win32HeapSummaryProc *)(void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "HeapSummary");
if(heap_summary) {
Win32HeapSummary summary = {sizeof(summary)};
if(heap_summary(GetProcessHeap(), 0, &summary)) {
profiler->heap_arena = (u64)summary.committed;
profiler->heap_in_use = (u64)summary.allocated;
profiler->heap_free = (u64)(summary.committed > summary.allocated ? summary.committed - summary.allocated : 0);
profiler->heap_mmapped = (u64)summary.reserved;
}
}
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 (FILETIME is in 100ns units)
FILETIME created, exited, kernel, user;
if(GetProcessTimes(GetCurrentProcess(), &created, &exited, &kernel, &user)) {
u64 cpu = ((((u64)kernel.dwHighDateTime << 32) | kernel.dwLowDateTime) + (((u64)user.dwHighDateTime << 32) | user.dwLowDateTime)) * 100ull;
if(dt > 0 && profiler->cpu_process_ns) profiler->cpu_process = (f32)((f64)(cpu - profiler->cpu_process_ns) / 1e9) / dt;
profiler->cpu_process_ns = cpu;
}
// NOTE: windows counts soft + hard faults together
if(dt > 0 && profiler->minflt) profiler->minflt_per_s = (f32)((u64)memory.PageFaultCount - profiler->minflt) / dt;
profiler->minflt = (u64)memory.PageFaultCount;
//- threads
for(u32 index = 0; index < profiler->thread_count; ++index) {
ProfileThread *thread = &profiler->threads[index];
HANDLE handle = (HANDLE)(umm)thread->clock;
if(handle && GetThreadTimes(handle, &created, &exited, &kernel, &user)) {
u64 cpu_ns = ((((u64)kernel.dwHighDateTime << 32) | kernel.dwLowDateTime) + (((u64)user.dwHighDateTime << 32) | user.dwLowDateTime)) * 100ull;
if(dt > 0 && thread->cpu_ns) thread->cpu = (f32)((f64)(cpu_ns - thread->cpu_ns) / 1e9) / dt;
thread->cpu_ns = cpu_ns;
}
// NOTE: stack high water mark: the stack grows down by committing pages behind the guard
// page, so the committed region at the top of the reservation is what was ever touched
if(thread->stack_low && thread->stack_size) {
u8 *top = thread->stack_low + thread->stack_size;
MEMORY_BASIC_INFORMATION region;
if(VirtualQuery(top - 1, ®ion, sizeof(region)) && region.State == MEM_COMMIT) {
thread->stack_touched = (u64)(top - (u8 *)region.BaseAddress);
}
}
}
#endif
//- 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);
}
}
|