aboutsummaryrefslogtreecommitdiffstats
path: root/code/custom/4coder_profile_inspect.cpp
blob: 586258d287cbedb42a13e0c1e669e67515fc67bc (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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/*
 * 4coder_profile_inspect.cpp - Built in self profiling UI.
 */

// TOP

function Profile_Slot*
profile_parse_get_slot(Arena *arena, Profile_Inspection *insp,
                       String_Const_u8 loc, String_Const_u8 name){
    Profile_Slot *result = 0;
    for (Profile_Slot *node = insp->first_slot;
         node != 0;
         node = node->next){
        if (string_match(node->location, loc) &&
            string_match(node->name, name)){
            result = node;
            break;
        }
    }
    if (result == 0){
        result = push_array_zero(arena, Profile_Slot, 1);
        sll_queue_push(insp->first_slot, insp->last_slot, result);
        insp->slot_count += 1;
        result->location = loc;
        result->name = name;
    }
    return(result);
}

function void
profile_parse_error(Arena *arena, Profile_Inspection *insp, String_Const_u8 message,
                    String_Const_u8 location){
    Profile_Error *error = push_array(arena, Profile_Error, 1);
    sll_queue_push(insp->first_error, insp->last_error, error);
    insp->error_count += 1;
    error->message = message;
    error->location = location;
}

function Profile_Record*
profile_parse_record(Arena *arena, Profile_Inspection *insp,
                     Profile_Node *parent, Profile_Record *record,
                     Range_u64 *total_time_range){
    for (;record != 0;){
        if (record->id <= parent->id){
            break;
        }

        Profile_ID id = record->id;
        Profile_Node *node = push_array(arena, Profile_Node, 1);
        sll_queue_push(parent->first_child, parent->last_child, node);
        parent->child_count += 1;
        node->parent = parent;
        node->thread = parent->thread;

        String_Const_u8 location = record->location;
        String_Const_u8 name = record->name;

        node->time.min = record->time;
        node->time.max = max_u64;
        node->id = id;
        node->first_child = 0;
        node->last_child = 0;
        node->child_count = 0;
        node->closed = false;

        record = profile_parse_record(arena, insp, node, record->next,
                                      total_time_range);

        b32 quit_loop = false;
        Profile_Slot *slot = 0;
        if (record == 0 || record->id < id){
            if (record == 0){
#define M "List ended before all nodes closed"
                profile_parse_error(arena, insp, string_u8_litexpr(M), location);
#undef M
            }
            else{
#define M "Node '%.*s' closed by parent ending (or higher priority sibling starting)"
                String_Const_u8 str = push_u8_stringf(arena, M, string_expand(name));
                profile_parse_error(arena, insp, str, location);
#undef M
                if (parent->id != 0){
                    quit_loop = true;
                }
            }
            slot = profile_parse_get_slot(arena, insp, location, name);
        }
        else if (record->id == id){
            slot = profile_parse_get_slot(arena, insp, location, name);
            node->time.max = record->time;
            node->closed = true;
            total_time_range->min = Min(total_time_range->min, node->time.min);
            total_time_range->max = Max(total_time_range->max, node->time.max);
            record = record->next;
        }
        else{
            // NOTE(allen): This would mean that record exists and it's id
            // is greater than id, but then the sub-call should not have returned!
            InvalidPath;
        }

        node->slot = slot;
        if (!slot->corrupted_time){
            if (node->closed){
                slot->total_time += range_size(node->time);
            }
            else{
                slot->corrupted_time = true;
            }
        }
        {
            Profile_Node_Ptr *node_ptr = push_array(arena, Profile_Node_Ptr, 1);
            sll_queue_push(slot->first_hit, slot->last_hit, node_ptr);
            slot->hit_count += 1;
            node_ptr->ptr = node;
            node->unique_counter = (u64)slot->hit_count;
        }

        if (quit_loop){
            break;
        }
    }
    return(record);
}

function Profile_Inspection
profile_parse(Arena *arena, Profile_Global_List *src){
    Mutex_Lock lock(src->mutex);

    Profile_Inspection result = {};

    result.thread_count = src->thread_count;
    result.threads = push_array_zero(arena, Profile_Inspection_Thread,
                                     result.thread_count);

    i32 counter = 0;
    Profile_Inspection_Thread *insp_thread = result.threads;
    for (Profile_Thread *node = src->first_thread;
         node != 0;
         node = node->next, counter += 1, insp_thread += 1){
        insp_thread->thread_id = node->thread_id;
        insp_thread->name = node->name;

        // NOTE(allen): This is the "negative infinity" range.
        // We will be "maxing" it against all the ranges durring the parse,
        // to get the root range.
        Range_u64 time_range = {max_u64, 0};
        insp_thread->root.thread = insp_thread;
        profile_parse_record(arena, &result, &insp_thread->root, node->first_record, &time_range);
        insp_thread->root.time = time_range;
        insp_thread->root.closed = true;

        for (Profile_Node *prof_node = insp_thread->root.first_child;
             prof_node != 0;
             prof_node = prof_node->next){
            insp_thread->active_time += range_size(prof_node->time);
        }
    }

    return(result);
}

////////////////////////////////

struct Tab_State{
    Vec2_f32 p;
    Range_f32 tabs_y;
    Face_ID face_id;
    f32 x_half_padding;
    Vec2_f32 m_p;
};

function void
profile_draw_tab(Application_Links *app, Tab_State *state, Profile_Inspection *insp,
                 String_Const_u8 string, Profile_Inspection_Tab tab_id){
    Scratch_Block scratch(app);

    state->p.x += state->x_half_padding;

    Fancy_String *fstring = push_fancy_string(scratch, 0, string);

    b32 hover = false;
    f32 width = get_fancy_string_width(app, state->face_id, fstring);
    Rect_f32 box = Rf32(If32_size(state->p.x, width), state->tabs_y);
    if (rect_contains_point(box, state->m_p)){
        hover = true;
        insp->tab_id_hovered = tab_id;
    }

    FColor text = fcolor_id(defcolor_text_default);
    if (insp->tab_id == tab_id){
        text = fcolor_id(defcolor_pop2);
    }
    else if (hover){
        text = fcolor_id(defcolor_pop1);
    }

    Vec2_f32 np = draw_fancy_string(app, state->face_id, text, fstring, state->p);
    state->p = np;
    state->p.x += state->x_half_padding;
}

function void
profile_select_thread(Profile_Inspection *inspect, Profile_Inspection_Thread *thread){
    inspect->tab_id = ProfileInspectTab_Selection;
    inspect->selected_thread = thread;
    inspect->selected_slot = 0;
    inspect->selected_node = 0;
}

function void
profile_select_slot(Profile_Inspection *inspect, Profile_Slot *slot){
    inspect->tab_id = ProfileInspectTab_Selection;
    inspect->selected_thread = 0;
    inspect->selected_slot = slot;
    inspect->selected_node = 0;
}

function void
profile_select_node(Profile_Inspection *inspect, Profile_Node *node){
    inspect->tab_id = ProfileInspectTab_Selection;
    inspect->selected_thread = 0;
    inspect->selected_slot = 0;
    inspect->selected_node = node;
}

function String_Const_u8
profile_node_thread_name(Profile_Node *node){
    String_Const_u8 result = {};
    if (node->thread != 0){
        result = node->thread->name;
    }
    return(result);
}

function String_Const_u8
profile_node_name(Profile_Node *node){
    String_Const_u8 result = string_u8_litexpr("*root*");
    if (node->slot != 0){
        result = node->slot->name;
    }
    return(result);
}

function String_Const_u8
profile_node_location(Profile_Node *node){
    String_Const_u8 result = {};
    if (node->slot != 0){
        result = node->slot->location;
    }
    return(result);
}

function void
profile_qsort_nodes(Profile_Node **nodes, i32 first, i32 one_past_last){
    if (first + 1 < one_past_last){
        i32 pivot_index = one_past_last - 1;
        Profile_Node *pivot = nodes[pivot_index];
        u64 pivot_time = range_size(pivot->time);
        i32 j = first;
        for (i32 i = first; i < one_past_last; i += 1){
            Profile_Node *node = nodes[i];
            u64 node_time = range_size(node->time);
            if (node_time > pivot_time){
                Swap(Profile_Node*, nodes[i], nodes[j]);
                j += 1;
            }
        }
        Swap(Profile_Node*, nodes[pivot_index], nodes[j]);
        profile_qsort_nodes(nodes, first, j);
        profile_qsort_nodes(nodes, j + 1, one_past_last);
    }
}

function void
profile_draw_node(Application_Links *app, View_ID view, Face_ID face_id,
                  Profile_Node *node, Rect_f32 rect,
                  Profile_Inspection *insp, Vec2_f32 m_p){
    Range_f32 x = rect_range_x(rect);
    Range_f32 y = rect_range_y(rect);

    // TODO(allen): share this shit
    Face_Metrics metrics = get_face_metrics(app, face_id);
    f32 line_height = metrics.line_height;
    f32 normal_advance = metrics.normal_advance;
    f32 block_height = line_height*2.f;
    f32 x_padding = normal_advance*1.5f;
    f32 x_half_padding = x_padding*0.5f;

    Color_Array colors = finalize_color_array(defcolor_back_cycle);

    Scratch_Block scratch(app);

    f32 x_pos = x.min + x_half_padding;
    f32 nav_bar_w = 0.f;
    Range_f32 nav_bar_y = {};
    nav_bar_y.min = y.min;

    String_Const_u8 thread_name = profile_node_thread_name(node);
    if (thread_name.size > 0){
        Fancy_String *fstr =
            push_fancy_string(scratch, 0, fcolor_id(defcolor_pop1), thread_name);
        Vec2_f32 p = V2f32(x_pos, y.min + 1.f);
        draw_fancy_string(app, face_id, fcolor_zero(), fstr, p);
        f32 w = get_fancy_string_width(app, face_id, fstr);
        nav_bar_w = Max(nav_bar_w, w);
    }
    y.min += line_height + 2.f;

    String_Const_u8 name = profile_node_name(node);
    if (name.size > 0){
        Fancy_String *fstr =
            push_fancy_string(scratch, 0, fcolor_id(defcolor_text_default), name);
        Vec2_f32 p = V2f32(x_pos, y.min + 1.f);
        draw_fancy_string(app, face_id, fcolor_zero(), fstr, p);
        f32 w = get_fancy_string_width(app, face_id, fstr);
        nav_bar_w = Max(nav_bar_w, w);
    }
    y.min += line_height + 2.f;

    nav_bar_y.max = y.min;

    x_pos += nav_bar_w + x_half_padding;
    if (node->parent != 0){
        Fancy_String *fstr = push_fancy_string(scratch, 0, fcolor_zero(),
                                               string_u8_litexpr("to parent"));
        f32 w = get_fancy_string_width(app, face_id, fstr) + x_padding;
        Range_f32 btn_x = If32_size(x_pos, w);
        Rect_f32 box = Rf32(btn_x, nav_bar_y);

        FColor color = fcolor_id(defcolor_text_default);
        if (rect_contains_point(box, m_p)){
            draw_rectangle_fcolor(app, box, 0.f, fcolor_id(defcolor_margin));
            color = fcolor_id(defcolor_pop1);
            insp->hover_node = node->parent;
        }

        Vec2_f32 p = V2f32(box.x0 + x_half_padding,
                           (box.y0 + box.y1 - line_height)*0.5f);
        draw_fancy_string(app, face_id, color, fstr, p);

        x_pos = btn_x.max;
    }

    Range_u64 top_time = node->time;

    Rect_f32_Pair side_by_side = rect_split_left_right_lerp(Rf32(x, y), 0.5f);

    Rect_f32 time_slice_box = side_by_side.min;
    time_slice_box = rect_inner(time_slice_box, 3.f);
    draw_rectangle_outline_fcolor(app, time_slice_box, 0.f, 3.f, f_white);
    time_slice_box = rect_inner(time_slice_box, 3.f);

    if (node->closed){
        draw_set_clip(app, time_slice_box);

        x = rect_range_x(time_slice_box);
        y = rect_range_y(time_slice_box);

        i32 cycle_counter = 0;
        for (Profile_Node *child = node->first_child;
             child != 0;
             child = child->next){
            if (!child->closed){
                continue;
            }

            Range_u64 child_time = child->time;
            Range_f32 child_y = {};
            child_y.min = unlerp(top_time.min, child_time.min, top_time.max);
            child_y.max = unlerp(top_time.min, child_time.max, top_time.max);
            child_y.min = ed_lerp(y.min, child_y.min, y.max);
            child_y.max = ed_lerp(y.min, child_y.max, y.max);

            Rect_f32 box = Rf32(x, child_y);
            ARGB_Color argb = finalize_color(colors, cycle_counter);
            draw_rectangle(app, box, 0.f, argb);
            cycle_counter += 1;

            if (rect_contains_point(box, m_p)){
                insp->full_name_hovered = profile_node_name(child);
                insp->unique_counter_hovered = child->unique_counter;
                insp->location_jump_hovered = profile_node_location(child);
                insp->hover_node = child;
            }

            if (range_size(child_y) >= line_height){
                String_Const_u8 child_name = profile_node_name(child);
                Fancy_Line line = {};
                push_fancy_string(scratch, &line, fcolor_id(defcolor_pop1),
                                  child_name);
                push_fancy_stringf(scratch, &line, fcolor_id(defcolor_text_default),
                                   0.5f, 0.f, "#%4llu", child->unique_counter);

                Vec2_f32 p = V2f32(x.min + x_half_padding,
                                   child_y.min);
                draw_fancy_line(app, face_id, fcolor_zero(),
                                &line, p);
            }
        }
    }

    Rect_f32 info_box = side_by_side.max;

    {
        draw_set_clip(app, info_box);

        x = rect_range_x(info_box);

        x_pos = x.min + x_half_padding;
        f32 y_pos = info_box.y0;

        // NOTE(allen): duration
        {
            f32 duration = ((f32)range_size(node->time))/1000000.f;
            Fancy_Line list = {};
            push_fancy_stringf(scratch, &list, fcolor_id(defcolor_text_default),
                               "time: %11.9f", duration);
            draw_fancy_line(app, face_id, fcolor_zero(),
                            &list, V2f32(x_pos, y_pos + 1.f));
            y_pos += line_height + 2.f;
        }

        i32 child_count = node->child_count;
        Profile_Node **children_array = push_array(scratch, Profile_Node*, child_count);
        i32 counter = 0;
        for (Profile_Node *child = node->first_child;
             child != 0;
             child = child->next){
            children_array[counter] = child;
            counter += 1;
        }

        profile_qsort_nodes(children_array, 0, child_count);

        Profile_Node **child_ptr = children_array;
        for (i32 i = 0; i < child_count; i += 1, child_ptr += 1){
            Profile_Node *child = *child_ptr;
            y = If32_size(y_pos, block_height);

            f32 child_duration = ((f32)range_size(child->time))/1000000.f;

            String_Const_u8 child_name = profile_node_name(child);
            Fancy_Line line = {};
            push_fancy_string_trunc(scratch, &line, child_name, 20);
            push_fancy_stringf(scratch, &line, fcolor_id(defcolor_text_default), 0.5f, 0.f,
                               "#%4llu", child->unique_counter);
            push_fancy_stringf(scratch, &line, fcolor_id(defcolor_pop2),
                               0.5f, 0.f, "%6.4f", child_duration);

            Vec2_f32 p = V2f32(x.min + x_half_padding,
                               (y.min + y.max - line_height)*0.5f);
            draw_fancy_line(app, face_id, fcolor_id(defcolor_pop1), &line, p);

            Rect_f32 box = Rf32(x, y);
            FColor margin = fcolor_id(defcolor_margin);
            if (rect_contains_point(box, m_p)){
                insp->full_name_hovered = child_name;
                insp->unique_counter_hovered = child->unique_counter;
                insp->location_jump_hovered = profile_node_location(child);
                insp->hover_node = child;
                margin = fcolor_id(defcolor_margin_hover);
            }
            draw_rectangle_outline_fcolor(app, box, 6.f, 3.f, margin);

            y_pos = y.max;
            if (y_pos >= info_box.y1){
                break;
            }
        }
    }
}

function void
profile_memory_sort_by_count(Memory_Bucket **buckets, i32 first, i32 one_past_last){
    if (first + 1 < one_past_last){
        i32 pivot = one_past_last - 1;
        i32 pivot_key = buckets[pivot]->annotation.count;
        i32 j = first;
        for (i32 i = first; i < pivot; i += 1){
            i32 key = buckets[i]->annotation.count;
            if (key <= pivot_key){
                Swap(Memory_Bucket*, buckets[j], buckets[i]);
                j += 1;
            }
        }
        Swap(Memory_Bucket*, buckets[j], buckets[pivot]);
        profile_memory_sort_by_count(buckets, first, j);
        profile_memory_sort_by_count(buckets, j + 1, one_past_last);
    }
}

function void
profile_render(Application_Links *app, Frame_Info frame_info, View_ID view){
    Scratch_Block scratch(app);

    Rect_f32 region = draw_background_and_margin(app, view);
    Rect_f32 prev_clip = draw_set_clip(app, region);

    Face_ID face_id = get_face_id(app, 0);
    // TODO(allen): share this shit
    Face_Metrics metrics = get_face_metrics(app, face_id);
    f32 line_height = metrics.line_height;
    f32 normal_advance = metrics.normal_advance;
    f32 block_height = line_height*2.f;
    f32 x_padding = normal_advance*1.5f;
    f32 x_half_padding = x_padding*0.5f;

    Mouse_State mouse = get_mouse_state(app);
    Vec2_f32 m_p = V2f32(mouse.p);

    Profile_Inspection *inspect = &global_profile_inspection;

    if (inspect->thread_count == 0){
        Fancy_String *fstr = push_fancy_string(scratch, 0, fcolor_id(defcolor_pop2),
                                               string_u8_litexpr("no profile data"));
        f32 width = get_fancy_string_width(app, face_id, fstr);
        Vec2_f32 view_center = (region.p0 + region.p1)*0.5f;
        Vec2_f32 half_dim = V2f32(width, line_height)*0.5f;
        Vec2_f32 p = view_center - half_dim;
        draw_fancy_string(app, face_id, fcolor_zero(), fstr, p);
    }
    else{
        Rect_f32_Pair tabs_body = rect_split_top_bottom(region, line_height + 2.f);
        Range_f32 tabs_y = rect_range_y(tabs_body.min);

        inspect->tab_id_hovered = ProfileInspectTab_None;
        block_zero_struct(&inspect->full_name_hovered);
        inspect->unique_counter_hovered = 0;
        block_zero_struct(&inspect->location_jump_hovered);
        inspect->hover_thread = 0;
        inspect->hover_slot = 0;
        inspect->hover_node = 0;

        // NOTE(allen): tabs
        {
            f32 y = (tabs_y.min + tabs_y.max - line_height)*0.5f;
            f32 x = region.x0;

            Tab_State tab_state = {};
            tab_state.p = V2f32(x, y);
            tab_state.tabs_y = tabs_y;
            tab_state.face_id = face_id;
            tab_state.x_half_padding = x_half_padding;
            tab_state.m_p = m_p;

            draw_rectangle_fcolor(app, tabs_body.min, 0.f, fcolor_id(defcolor_margin_hover));

            if (inspect->tab_id == ProfileInspectTab_None){
                inspect->tab_id = ProfileInspectTab_Threads;
            }

            profile_draw_tab(app, &tab_state, inspect,
                             string_u8_litexpr("threads"),
                             ProfileInspectTab_Threads);

            if (inspect->slot_count > 0){
                profile_draw_tab(app, &tab_state, inspect,
                                 string_u8_litexpr("blocks"),
                                 ProfileInspectTab_Blocks);
            }

            if (inspect->error_count > 0){
                profile_draw_tab(app, &tab_state, inspect,
                                 string_u8_litexpr("errors"),
                                 ProfileInspectTab_Errors);
            }

            profile_draw_tab(app, &tab_state, inspect,
                             string_u8_litexpr("memory"),
                             ProfileInspectTab_Memory);

            if (inspect->tab_id == ProfileInspectTab_Selection){
                String_Const_u8 string = {};
                if (inspect->selected_thread != 0){
                    String_Const_u8 name = inspect->selected_thread->name;
                    string = push_u8_stringf(scratch, "%.*s (%d)",
                                             string_expand(name),
                                             inspect->selected_thread->thread_id);
                }
                else if (inspect->selected_slot != 0){
                    String_Const_u8 name = inspect->selected_slot->name;
                    string = push_u8_stringf(scratch, "block %.*s",
                                             string_expand(name));
                }
                else if (inspect->selected_node != 0){
                    String_Const_u8 name = profile_node_name(inspect->selected_node);
                    string = push_u8_stringf(scratch, "node %.*s",
                                             string_expand(name));
                }
                else{
                    inspect->tab_id = ProfileInspectTab_Threads;
                }
                if (string.str != 0){
                    profile_draw_tab(app, &tab_state, inspect,
                                     string, ProfileInspectTab_Selection);
                }
            }
        }

        draw_set_clip(app, tabs_body.max);
        switch (inspect->tab_id){
            case ProfileInspectTab_Threads:
            {
                Range_f32 x = rect_range_x(tabs_body.max);
                f32 y_pos = tabs_body.max.y0;
                i32 count = inspect->thread_count;
                Profile_Inspection_Thread *thread = inspect->threads;
                for (i32 i = 0; i < count; i += 1, thread += 1){
                    Range_f32 y = If32_size(y_pos, block_height);

                    Fancy_Line list = {};
                    push_fancy_stringf(scratch, &list, fcolor_id(defcolor_pop1),
                                       "%-20.*s (%6d) ",
                                       string_expand(thread->name),
                                       thread->thread_id);

                    f32 active_time = ((f32)thread->active_time)/1000000.f;
                    push_fancy_stringf(scratch, &list, fcolor_id(defcolor_pop2),
                                       "active time %11.9f",
                                       active_time);

                    Vec2_f32 p = V2f32(x.min + x_half_padding,
                                       (y.min + y.max - line_height)*0.5f);
                    draw_fancy_line(app, face_id, fcolor_zero(), &list, p);

                    Rect_f32 box = Rf32(x, y);
                    FColor margin = fcolor_id(defcolor_margin);
                    if (rect_contains_point(box, m_p)){
                        inspect->hover_thread = thread;
                        margin = fcolor_id(defcolor_margin_hover);
                    }
                    draw_rectangle_outline_fcolor(app, box, 6.f, 3.f, margin);

                    y_pos = y.max;
                    if (y_pos >= tabs_body.max.y1){
                        break;
                    }
                }
            }break;

            case ProfileInspectTab_Blocks:
            {
                Range_f32 x = rect_range_x(tabs_body.max);
                f32 y_pos = tabs_body.max.y0;
                for (Profile_Slot *node = inspect->first_slot;
                     node != 0;
                     node = node->next){
                    Range_f32 y = If32_size(y_pos, block_height);

                    u32 name_width = 45;
                    b32 name_too_long = (node->name.size > name_width);
                    Fancy_Line list = {};
                    push_fancy_string_fixed(scratch, &list, fcolor_id(defcolor_pop1),
                                            node->name, name_width);

                    if (node->corrupted_time){
                        push_fancy_string(scratch, &list, fcolor_id(defcolor_pop2),
                                          string_u8_litexpr("timing error "));
                    }
                    else{
                        push_fancy_stringf(scratch, &list, fcolor_id(defcolor_pop2),
                                           "%11.9fs ",
                                           ((f32)node->total_time)/1000000.f);
                    }

                    push_fancy_stringf(scratch, &list, fcolor_id(defcolor_keyword),
                                       "hit # %5d", node->hit_count);

                    Vec2_f32 p = V2f32(x.min + x_half_padding,
                                       (y.min + y.max - line_height)*0.5f);
                    draw_fancy_line(app, face_id, fcolor_zero(), &list, p);

                    Rect_f32 box = Rf32(x, y);
                    FColor margin = fcolor_id(defcolor_margin);
                    if (rect_contains_point(box, m_p)){
                        if (name_too_long){
                            inspect->full_name_hovered = node->name;
                        }
                        inspect->location_jump_hovered = node->location;
                        inspect->hover_slot = node;
                        margin = fcolor_id(defcolor_margin_hover);
                    }
                    draw_rectangle_outline_fcolor(app, box, 6.f, 3.f, margin);

                    y_pos = y.max;
                    if (y_pos >= tabs_body.max.y1){
                        break;
                    }
                }
            }break;

            case ProfileInspectTab_Errors:
            {
                draw_set_clip(app, tabs_body.max);
                Range_f32 x = rect_range_x(tabs_body.max);
                f32 y_pos = tabs_body.max.y0;
                for (Profile_Error *node = inspect->first_error;
                     node != 0;
                     node = node->next){
                    Range_f32 y = If32_size(y_pos, block_height);

                    Fancy_Line list = {};
                    push_fancy_string(scratch, &list, fcolor_id(defcolor_pop2),
                                      node->message);

                    Vec2_f32 p = V2f32(x.min + x_half_padding,
                                       (y.min + y.max - line_height)*0.5f);
                    draw_fancy_line(app, face_id, fcolor_zero(), &list, p);

                    Rect_f32 box = Rf32(x, y);
                    FColor margin = fcolor_id(defcolor_margin);
                    if (rect_contains_point(box, m_p)){
                        inspect->location_jump_hovered = node->location;
                        margin = fcolor_id(defcolor_margin_hover);
                    }
                    draw_rectangle_outline_fcolor(app, box, 6.f, 3.f, margin);

                    y_pos = y.max;
                    if (y_pos >= tabs_body.max.y1){
                        break;
                    }
                }
            }break;

            case ProfileInspectTab_Memory:
            {
                draw_set_clip(app, tabs_body.max);
                Range_f32 x = rect_range_x(tabs_body.max);
                f32 y_pos = tabs_body.max.y0;
                Memory_Annotation annotation = system_memory_annotation(scratch);

                Base_Allocator *allocator = get_base_allocator_system();

                Memory_Bucket *first_bucket = 0;
                Memory_Bucket *last_bucket = 0;
                i32 bucket_count = 0;
                Table_Data_u64 table = make_table_Data_u64(allocator, 100);

                for (Memory_Annotation_Node *node = annotation.first, *next = 0;
                     node != 0;
                     node = next){
                    next = node->next;
                    String_Const_u8 key = make_data(node->location.str, node->location.size);
                    Table_Lookup lookup = table_lookup(&table, key);
                    Memory_Bucket *bucket = 0;
                    if (lookup.found_match){
                        u64 val = 0;
                        table_read(&table, lookup, &val);
                        bucket = (Memory_Bucket*)IntAsPtr(val);
                    }
                    else{
                        bucket = push_array_zero(scratch, Memory_Bucket, 1);
                        sll_queue_push(first_bucket, last_bucket, bucket);
                        bucket_count += 1;
                        bucket->location = node->location;
                        table_insert(&table, key, PtrAsInt(bucket));
                    }
                    sll_queue_push(bucket->annotation.first, bucket->annotation.last, node);
                    bucket->annotation.count += 1;
                    bucket->total_memory += node->size;
                }

                Memory_Bucket **buckets = push_array(scratch, Memory_Bucket*, bucket_count);
                i32 counter = 0;
                for (Memory_Bucket *node = first_bucket;
                     node != 0;
                     node = node->next){
                    buckets[counter] = node;
                    counter += 1;
                }

                profile_memory_sort_by_count(buckets, 0, bucket_count);

                for (i32 i = bucket_count - 1; i >= 0; i -= 1){
                    Memory_Bucket *node = buckets[i];
                    Range_f32 y = If32_size(y_pos, block_height);

                    Fancy_Line list = {};
                    push_fancy_stringf(scratch, &list, fcolor_id(defcolor_pop2), "[%12llu] / %6d ",
                                       node->total_memory, node->annotation.count);
                    push_fancy_stringf(scratch, &list, fcolor_id(defcolor_pop1), "%.*s",
                                       string_expand(node->location));

                    Vec2_f32 p = V2f32(x.min + x_half_padding,
                                       (y.min + y.max - line_height)*0.5f);
                    draw_fancy_line(app, face_id, fcolor_zero(), &list, p);

                    Rect_f32 box = Rf32(x, y);
                    FColor margin = fcolor_id(defcolor_margin);
                    if (rect_contains_point(box, m_p)){
                        inspect->location_jump_hovered = node->location;
                        margin = fcolor_id(defcolor_margin_hover);
                    }

                    y_pos = y.max;
                    if (y_pos >= tabs_body.max.y1){
                        break;
                    }
                }

                table_free(&table);
            }break;

            case ProfileInspectTab_Selection:
            {
                if (inspect->selected_thread != 0){
                    profile_draw_node(app, view, face_id,
                                      &inspect->selected_thread->root, tabs_body.max,
                                      inspect, m_p);
                }
                else if (inspect->selected_slot != 0){

                }
                else if (inspect->selected_node != 0){
                    profile_draw_node(app, view, face_id,
                                      inspect->selected_node, tabs_body.max,
                                      inspect, m_p);
                }
            }break;
        }

        if (!rect_contains_point(region, m_p)){
            // NOTE(allen): don't draw tool tip when the mouse doesn't hover in our view
        }
        else if (inspect->tab_id_hovered != ProfileInspectTab_None){
            // NOTE(allen): no tool tip for tabs
        }
        else{
            Fancy_Block block = {};
            FColor text_color = fcolor_change_alpha(f_white, 0.5f);
            FColor back_color = fcolor_change_alpha(f_black, 0.5f);

            if (inspect->full_name_hovered.size > 0){
                Fancy_Line *line = push_fancy_line(scratch, &block, text_color);
                push_fancy_stringf(scratch, line, "%.*s",
                                   string_expand(inspect->full_name_hovered));
                if (inspect->unique_counter_hovered > 0){
                    push_fancy_stringf(scratch, line, text_color, 0.5f, 0.f,
                                       "#%4llu", inspect->unique_counter_hovered);
                }
            }
            if (inspect->location_jump_hovered.size > 0){
                Fancy_Line *line = push_fancy_line(scratch, &block, text_color);
                push_fancy_stringf(scratch, line, "[shift] '%.*s'",
                                   string_expand(inspect->location_jump_hovered));
            }

            draw_tool_tip(app, face_id, &block, m_p, region,
                          x_padding, x_half_padding, back_color);
        }
    }

    draw_set_clip(app, prev_clip);
}

function void
profile_inspect__left_click(Application_Links *app, View_ID view,
                            Profile_Inspection *insp, Input_Event *event){
    if (has_modifier(event, KeyCode_Shift)){
        if (insp->location_jump_hovered.size != 0){
            View_ID target_view = view;
            target_view = get_next_view_looped_primary_panels(app, target_view,
                                                              Access_Always);
            String_Const_u8 location = insp->location_jump_hovered;
            jump_to_location(app, target_view, location);
        }
    }
    else{
        if (insp->tab_id_hovered != ProfileInspectTab_None){
            insp->tab_id = insp->tab_id_hovered;
        }
        else if (insp->hover_thread != 0){
            profile_select_thread(insp, insp->hover_thread);
        }
        else if (insp->hover_slot != 0){
            profile_select_slot(insp, insp->hover_slot);
        }
        else if (insp->hover_node != 0){
            profile_select_node(insp, insp->hover_node);
        }
    }
}

CUSTOM_UI_COMMAND_SIG(profile_inspect)
CUSTOM_DOC("Inspect all currently collected profiling information in 4coder's self profiler.")
{
    Profile_Global_List *list = get_core_profile_list(app);
    if (HasFlag(list->disable_bits, ProfileEnable_InspectBit)){
        return;
    }

    profile_set_enabled(list, false, ProfileEnable_InspectBit);

    Scratch_Block scratch(app);
    global_profile_inspection = profile_parse(scratch, list);
    Profile_Inspection *insp = &global_profile_inspection;

    View_ID view = get_active_view(app, Access_Always);
    View_Context ctx = view_current_context(app, view);
    ctx.render_caller = profile_render;
    ctx.hides_buffer = true;
    View_Context_Block ctx_block(app, view, &ctx);

    for (;;){
        User_Input in = get_next_input(app, EventPropertyGroup_Any, EventProperty_Escape);
        if (in.abort){
            break;
        }

        b32 handled = true;
        switch (in.event.kind){
            case InputEventKind_MouseButton:
            {
                switch (in.event.mouse.code){
                    case MouseCode_Left:
                    {
                        profile_inspect__left_click(app, view, insp, &in.event);
                    }break;
                }
            }break;

            default:
            {
                handled = false;
            }break;
        }

        if (!handled){
            if (ui_fallback_command_dispatch(app, view, &in)){
                break;
            }
        }
    }

    profile_set_enabled(list, true, ProfileEnable_InspectBit);
}

// BOTTOM