-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler.c
More file actions
965 lines (778 loc) · 27.6 KB
/
profiler.c
File metadata and controls
965 lines (778 loc) · 27.6 KB
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
/*
* profiler.c - Advanced Profiler Implementation
*
* Provides comprehensive profiling with function timing, call graphs,
* hot-spot analysis, and multiple export formats.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "profiler.h"
#include "host.h"
#include "misc.h"
/* Hash function for branch PC */
static unsigned int hash_pc(md_addr_t pc, int table_size)
{
return (unsigned int)((pc >> 2) % table_size);
}
/* Compare function for qsort - descending by count */
static int cmp_counter_desc(const void *a, const void *b)
{
counter_t ca = *(const counter_t *)a;
counter_t cb = *(const counter_t *)b;
if (cb > ca) return 1;
if (cb < ca) return -1;
return 0;
}
/* ========== Profiler Lifecycle ========== */
profiler_t *profiler_create(profiler_mode_t mode, md_addr_t pc_base, md_addr_t pc_size)
{
profiler_t *prof;
int hist_size;
prof = (profiler_t *)calloc(1, sizeof(profiler_t));
if (!prof) {
fprintf(stderr, "profiler_create: out of memory\n");
return NULL;
}
prof->mode = mode;
prof->sample_interval = 1; /* Default: profile every instruction */
prof->enabled = 1;
/* Initialize function list */
prof->num_functions = 0;
/* Initialize call graph */
prof->num_edges = 0;
/* Initialize call stack */
prof->stack_depth = 0;
/* Initialize PC histogram */
if (mode & PROF_MODE_HOTSPOT) {
prof->pc_base = pc_base;
prof->pc_size = pc_size;
prof->pc_shift = 2; /* 4-byte buckets (word aligned) */
hist_size = (pc_size >> prof->pc_shift);
if (hist_size > PROFILER_PC_HIST_SIZE)
hist_size = PROFILER_PC_HIST_SIZE;
if (hist_size < 1024)
hist_size = 1024;
prof->pc_histogram = (counter_t *)calloc(hist_size, sizeof(counter_t));
if (!prof->pc_histogram) {
fprintf(stderr, "profiler_create: failed to allocate PC histogram\n");
free(prof);
return NULL;
}
}
/* Initialize branch hash table */
if (mode & PROF_MODE_BRANCH) {
prof->branches_size = 4096;
prof->branches = (branch_profile_t *)calloc(prof->branches_size,
sizeof(branch_profile_t));
if (!prof->branches) {
fprintf(stderr, "profiler_create: failed to allocate branch table\n");
if (prof->pc_histogram) free(prof->pc_histogram);
free(prof);
return NULL;
}
prof->num_branches = 0;
}
/* Initialize memory samples */
prof->mem_sample_idx = 0;
prof->mem_sample_wrap = 0;
memset(&prof->mem_stats, 0, sizeof(prof->mem_stats));
/* Initialize counters */
prof->total_cycles = 0;
prof->total_insns = 0;
prof->total_calls = 0;
prof->total_returns = 0;
return prof;
}
void profiler_destroy(profiler_t *prof)
{
if (!prof)
return;
if (prof->pc_histogram)
free(prof->pc_histogram);
if (prof->branches)
free(prof->branches);
free(prof);
}
void profiler_reset(profiler_t *prof)
{
int hist_size;
if (!prof)
return;
/* Reset function profiles */
for (int i = 0; i < prof->num_functions; i++) {
prof->functions[i].call_count = 0;
prof->functions[i].total_cycles = 0;
prof->functions[i].self_cycles = 0;
prof->functions[i].total_insns = 0;
prof->functions[i].self_insns = 0;
}
/* Reset call edges */
for (int i = 0; i < prof->num_edges; i++) {
prof->call_edges[i].count = 0;
prof->call_edges[i].cycles = 0;
}
/* Reset call stack */
prof->stack_depth = 0;
/* Reset PC histogram */
if (prof->pc_histogram) {
hist_size = (prof->pc_size >> prof->pc_shift);
if (hist_size > PROFILER_PC_HIST_SIZE)
hist_size = PROFILER_PC_HIST_SIZE;
memset(prof->pc_histogram, 0, hist_size * sizeof(counter_t));
}
/* Reset branch profiles */
if (prof->branches) {
memset(prof->branches, 0, prof->branches_size * sizeof(branch_profile_t));
prof->num_branches = 0;
}
/* Reset memory samples */
prof->mem_sample_idx = 0;
prof->mem_sample_wrap = 0;
memset(&prof->mem_stats, 0, sizeof(prof->mem_stats));
/* Reset counters */
prof->total_cycles = 0;
prof->total_insns = 0;
prof->total_calls = 0;
prof->total_returns = 0;
}
void profiler_enable(profiler_t *prof, int enable)
{
if (prof)
prof->enabled = enable;
}
void profiler_set_sample_interval(profiler_t *prof, int interval)
{
if (prof && interval > 0)
prof->sample_interval = interval;
}
/* ========== Symbol Table ========== */
int profiler_add_function(profiler_t *prof, const char *name,
md_addr_t start, md_addr_t end)
{
int idx;
if (!prof || prof->num_functions >= PROFILER_MAX_FUNCTIONS)
return -1;
idx = prof->num_functions;
prof->functions[idx].start_addr = start;
prof->functions[idx].end_addr = end;
strncpy(prof->functions[idx].name, name ? name : "unknown", 63);
prof->functions[idx].name[63] = '\0';
prof->functions[idx].call_count = 0;
prof->functions[idx].total_cycles = 0;
prof->functions[idx].self_cycles = 0;
prof->functions[idx].total_insns = 0;
prof->functions[idx].self_insns = 0;
prof->num_functions++;
return idx;
}
int profiler_find_function(profiler_t *prof, md_addr_t addr)
{
int i;
if (!prof)
return -1;
/* Linear search - could be optimized with binary search if sorted */
for (i = 0; i < prof->num_functions; i++) {
if (addr >= prof->functions[i].start_addr &&
addr < prof->functions[i].end_addr) {
return i;
}
}
return -1;
}
void profiler_load_symbols(profiler_t *prof, const char *filename)
{
/* This would integrate with the symbol table loader
* For now, functions are added manually or from ELF parsing
*/
(void)prof;
(void)filename;
}
/* ========== Event Hooks ========== */
void profiler_instruction(profiler_t *prof, md_addr_t pc, counter_t cycle)
{
int bucket;
if (!prof || !prof->enabled)
return;
prof->total_insns++;
prof->total_cycles = cycle;
/* PC histogram */
if ((prof->mode & PROF_MODE_HOTSPOT) && prof->pc_histogram) {
if (pc >= prof->pc_base && pc < prof->pc_base + prof->pc_size) {
bucket = (pc - prof->pc_base) >> prof->pc_shift;
if (bucket < PROFILER_PC_HIST_SIZE) {
prof->pc_histogram[bucket]++;
}
}
}
/* Update current function's self time */
if ((prof->mode & PROF_MODE_TIMING) && prof->stack_depth > 0) {
int func_idx = prof->call_stack[prof->stack_depth - 1].func_idx;
if (func_idx >= 0 && func_idx < prof->num_functions) {
prof->functions[func_idx].self_insns++;
}
}
}
void profiler_call(profiler_t *prof, md_addr_t call_pc, md_addr_t target,
md_addr_t return_addr, counter_t cycle, counter_t insn_count)
{
int caller_idx, callee_idx;
int i, edge_idx;
if (!prof || !prof->enabled)
return;
prof->total_calls++;
if (!(prof->mode & (PROF_MODE_TIMING | PROF_MODE_CALLGRAPH)))
return;
/* Find caller and callee functions */
caller_idx = profiler_find_function(prof, call_pc);
callee_idx = profiler_find_function(prof, target);
/* If callee not found, try to add it */
if (callee_idx < 0 && prof->num_functions < PROFILER_MAX_FUNCTIONS) {
char name[32];
snprintf(name, sizeof(name), "func_%08x", target);
callee_idx = profiler_add_function(prof, name, target, target + 0x1000);
}
if (callee_idx >= 0) {
prof->functions[callee_idx].call_count++;
}
/* Push onto call stack */
if (prof->stack_depth < PROFILER_MAX_STACK_DEPTH) {
call_stack_entry_t *entry = &prof->call_stack[prof->stack_depth];
entry->func_idx = callee_idx;
entry->return_addr = return_addr;
entry->entry_cycle = cycle;
entry->entry_insn = insn_count;
entry->child_cycles = 0;
entry->child_insns = 0;
prof->stack_depth++;
}
/* Record call edge */
if (prof->mode & PROF_MODE_CALLGRAPH) {
edge_idx = -1;
/* Find existing edge */
for (i = 0; i < prof->num_edges; i++) {
if (prof->call_edges[i].caller_idx == caller_idx &&
prof->call_edges[i].callee_idx == callee_idx &&
prof->call_edges[i].call_site == call_pc) {
edge_idx = i;
break;
}
}
/* Create new edge if needed */
if (edge_idx < 0 && prof->num_edges < PROFILER_MAX_CALL_EDGES) {
edge_idx = prof->num_edges++;
prof->call_edges[edge_idx].caller_idx = caller_idx;
prof->call_edges[edge_idx].callee_idx = callee_idx;
prof->call_edges[edge_idx].call_site = call_pc;
prof->call_edges[edge_idx].count = 0;
prof->call_edges[edge_idx].cycles = 0;
}
if (edge_idx >= 0) {
prof->call_edges[edge_idx].count++;
}
}
}
void profiler_return(profiler_t *prof, md_addr_t return_pc, md_addr_t target,
counter_t cycle, counter_t insn_count)
{
call_stack_entry_t *entry;
counter_t elapsed_cycles, elapsed_insns;
counter_t self_cycles, self_insns;
int func_idx;
if (!prof || !prof->enabled)
return;
prof->total_returns++;
if (!(prof->mode & PROF_MODE_TIMING))
return;
if (prof->stack_depth <= 0)
return;
/* Pop from call stack */
entry = &prof->call_stack[--prof->stack_depth];
/* Calculate elapsed time */
elapsed_cycles = cycle - entry->entry_cycle;
elapsed_insns = insn_count - entry->entry_insn;
/* Self time = total - children */
self_cycles = elapsed_cycles - entry->child_cycles;
self_insns = elapsed_insns - entry->child_insns;
/* Update function statistics */
func_idx = entry->func_idx;
if (func_idx >= 0 && func_idx < prof->num_functions) {
prof->functions[func_idx].total_cycles += elapsed_cycles;
prof->functions[func_idx].self_cycles += self_cycles;
prof->functions[func_idx].total_insns += elapsed_insns;
/* Note: self_insns is tracked per-instruction in profiler_instruction */
}
/* Update parent's child time */
if (prof->stack_depth > 0) {
prof->call_stack[prof->stack_depth - 1].child_cycles += elapsed_cycles;
prof->call_stack[prof->stack_depth - 1].child_insns += elapsed_insns;
}
}
void profiler_branch(profiler_t *prof, md_addr_t pc, int taken, int predicted_taken)
{
unsigned int hash;
int i, slot;
branch_profile_t *bp;
if (!prof || !prof->enabled)
return;
if (!(prof->mode & PROF_MODE_BRANCH) || !prof->branches)
return;
/* Find or create branch entry */
hash = hash_pc(pc, prof->branches_size);
slot = -1;
for (i = 0; i < prof->branches_size; i++) {
int idx = (hash + i) % prof->branches_size;
bp = &prof->branches[idx];
if (bp->pc == pc) {
/* Found existing entry */
slot = idx;
break;
}
if (bp->pc == 0 && slot < 0) {
/* Empty slot - use it */
slot = idx;
}
}
if (slot < 0)
return; /* Hash table full */
bp = &prof->branches[slot];
if (bp->pc == 0) {
/* New entry */
bp->pc = pc;
prof->num_branches++;
}
/* Update counts */
if (taken)
bp->taken_count++;
else
bp->not_taken_count++;
if (taken == predicted_taken)
bp->predicted++;
else
bp->mispredicted++;
}
void profiler_memory(profiler_t *prof, md_addr_t pc, md_addr_t addr,
int is_load, int size, counter_t cycle)
{
mem_access_t *sample;
if (!prof || !prof->enabled)
return;
if (!(prof->mode & PROF_MODE_MEMORY))
return;
prof->mem_stats.total_accesses++;
/* Record sample */
sample = &prof->mem_samples[prof->mem_sample_idx];
sample->addr = addr;
sample->cycle = cycle;
sample->is_load = is_load;
sample->size = size;
prof->mem_sample_idx++;
if (prof->mem_sample_idx >= PROFILER_MEM_SAMPLES) {
prof->mem_sample_idx = 0;
prof->mem_sample_wrap = 1;
}
(void)pc; /* PC not used in basic implementation */
}
/* ========== Analysis ========== */
void profiler_analyze_memory(profiler_t *prof)
{
int num_samples;
int i;
int64_t stride;
int64_t stride_sum = 0;
int stride_count = 0;
if (!prof)
return;
num_samples = prof->mem_sample_wrap ? PROFILER_MEM_SAMPLES : prof->mem_sample_idx;
if (num_samples < 2)
return;
/* Analyze strides */
for (i = 1; i < num_samples; i++) {
stride = (int64_t)prof->mem_samples[i].addr -
(int64_t)prof->mem_samples[i-1].addr;
if (stride >= -8 && stride <= 8 && stride != 0) {
prof->mem_stats.sequential_accesses++;
} else if (stride_count > 0 && stride == prof->mem_stats.avg_stride) {
prof->mem_stats.strided_accesses++;
} else {
prof->mem_stats.random_accesses++;
}
stride_sum += stride;
stride_count++;
}
if (stride_count > 0)
prof->mem_stats.avg_stride = stride_sum / stride_count;
/* Calculate locality scores */
if (prof->mem_stats.total_accesses > 0) {
prof->mem_stats.spatial_locality =
(double)(prof->mem_stats.sequential_accesses + prof->mem_stats.strided_accesses) /
(double)prof->mem_stats.total_accesses;
}
/* Temporal locality would require tracking address reuse - simplified here */
prof->mem_stats.temporal_locality = 0.5; /* Placeholder */
}
int profiler_get_hotspots(profiler_t *prof, md_addr_t *addrs,
counter_t *counts, int max_results)
{
int i, n;
int hist_size;
if (!prof || !addrs || !counts || max_results <= 0)
return 0;
if (!prof->pc_histogram)
return 0;
hist_size = (prof->pc_size >> prof->pc_shift);
if (hist_size > PROFILER_PC_HIST_SIZE)
hist_size = PROFILER_PC_HIST_SIZE;
/* Simple insertion sort for top N */
n = 0;
for (i = 0; i < hist_size; i++) {
if (prof->pc_histogram[i] == 0)
continue;
md_addr_t addr = prof->pc_base + (i << prof->pc_shift);
counter_t count = prof->pc_histogram[i];
/* Find insertion point */
int j;
for (j = n; j > 0 && counts[j-1] < count; j--) {
if (j < max_results) {
addrs[j] = addrs[j-1];
counts[j] = counts[j-1];
}
}
if (j < max_results) {
addrs[j] = addr;
counts[j] = count;
if (n < max_results)
n++;
}
}
return n;
}
int profiler_get_top_functions(profiler_t *prof, int *indices, int max_results)
{
int i, n;
counter_t *temp_counts;
if (!prof || !indices || max_results <= 0)
return 0;
if (prof->num_functions == 0)
return 0;
temp_counts = (counter_t *)malloc(max_results * sizeof(counter_t));
if (!temp_counts)
return 0;
n = 0;
for (i = 0; i < prof->num_functions; i++) {
counter_t cycles = prof->functions[i].self_cycles;
if (cycles == 0)
continue;
/* Insertion sort */
int j;
for (j = n; j > 0 && temp_counts[j-1] < cycles; j--) {
if (j < max_results) {
indices[j] = indices[j-1];
temp_counts[j] = temp_counts[j-1];
}
}
if (j < max_results) {
indices[j] = i;
temp_counts[j] = cycles;
if (n < max_results)
n++;
}
}
free(temp_counts);
return n;
}
/* ========== Export ========== */
int profiler_export(profiler_t *prof, const char *filename, profiler_format_t format)
{
FILE *f;
int use_stdout = 0;
if (!prof || !filename)
return -1;
if (strcmp(filename, "-") == 0) {
f = stdout;
use_stdout = 1;
} else {
f = fopen(filename, "w");
if (!f) {
fprintf(stderr, "profiler_export: failed to open %s\n", filename);
return -1;
}
}
switch (format) {
case PROF_FORMAT_TEXT:
profiler_export_text(prof, f);
break;
case PROF_FORMAT_GPROF:
profiler_export_gprof(prof, f);
break;
case PROF_FORMAT_CALLGRIND:
profiler_export_callgrind(prof, f);
break;
case PROF_FORMAT_JSON:
profiler_export_json(prof, f);
break;
case PROF_FORMAT_CSV:
profiler_export_csv(prof, f);
break;
case PROF_FORMAT_FLAMEGRAPH:
profiler_export_flamegraph(prof, f);
break;
}
if (!use_stdout)
fclose(f);
return 0;
}
void profiler_export_text(profiler_t *prof, FILE *f)
{
profiler_print_summary(prof, f);
profiler_print_functions(prof, f, 20);
profiler_print_hotspots(prof, f, 20);
profiler_print_branches(prof, f, 10);
profiler_print_memory_patterns(prof, f);
}
void profiler_export_callgrind(profiler_t *prof, FILE *f)
{
int i;
if (!prof || !f)
return;
/* Callgrind header */
fprintf(f, "# callgrind format\n");
fprintf(f, "version: 1\n");
fprintf(f, "creator: sparc-v8-profiler\n");
fprintf(f, "positions: line\n");
fprintf(f, "events: Cycles Instructions\n");
fprintf(f, "\n");
/* Function costs */
for (i = 0; i < prof->num_functions; i++) {
func_profile_t *fn = &prof->functions[i];
if (fn->call_count == 0)
continue;
fprintf(f, "fn=%s\n", fn->name);
fprintf(f, "0 %lld %lld\n",
(long long)fn->self_cycles,
(long long)fn->self_insns);
fprintf(f, "\n");
}
/* Call edges */
for (i = 0; i < prof->num_edges; i++) {
call_edge_t *edge = &prof->call_edges[i];
if (edge->count == 0)
continue;
const char *caller_name = "unknown";
const char *callee_name = "unknown";
if (edge->caller_idx >= 0 && edge->caller_idx < prof->num_functions)
caller_name = prof->functions[edge->caller_idx].name;
if (edge->callee_idx >= 0 && edge->callee_idx < prof->num_functions)
callee_name = prof->functions[edge->callee_idx].name;
fprintf(f, "fn=%s\n", caller_name);
fprintf(f, "cfn=%s\n", callee_name);
fprintf(f, "calls=%lld 0\n", (long long)edge->count);
fprintf(f, "0 %lld 0\n", (long long)edge->cycles);
fprintf(f, "\n");
}
}
void profiler_export_json(profiler_t *prof, FILE *f)
{
int i;
if (!prof || !f)
return;
fprintf(f, "{\n");
fprintf(f, " \"summary\": {\n");
fprintf(f, " \"total_cycles\": %lld,\n", (long long)prof->total_cycles);
fprintf(f, " \"total_insns\": %lld,\n", (long long)prof->total_insns);
fprintf(f, " \"total_calls\": %lld,\n", (long long)prof->total_calls);
fprintf(f, " \"num_functions\": %d\n", prof->num_functions);
fprintf(f, " },\n");
fprintf(f, " \"functions\": [\n");
for (i = 0; i < prof->num_functions; i++) {
func_profile_t *fn = &prof->functions[i];
fprintf(f, " {\n");
fprintf(f, " \"name\": \"%s\",\n", fn->name);
fprintf(f, " \"start_addr\": \"0x%08x\",\n", fn->start_addr);
fprintf(f, " \"call_count\": %lld,\n", (long long)fn->call_count);
fprintf(f, " \"total_cycles\": %lld,\n", (long long)fn->total_cycles);
fprintf(f, " \"self_cycles\": %lld,\n", (long long)fn->self_cycles);
fprintf(f, " \"total_insns\": %lld,\n", (long long)fn->total_insns);
fprintf(f, " \"self_insns\": %lld\n", (long long)fn->self_insns);
fprintf(f, " }%s\n", (i < prof->num_functions - 1) ? "," : "");
}
fprintf(f, " ]\n");
fprintf(f, "}\n");
}
void profiler_export_csv(profiler_t *prof, FILE *f)
{
int i;
if (!prof || !f)
return;
/* Header */
fprintf(f, "Function,StartAddr,Calls,TotalCycles,SelfCycles,TotalInsns,SelfInsns,SelfPct\n");
/* Data */
for (i = 0; i < prof->num_functions; i++) {
func_profile_t *fn = &prof->functions[i];
double pct = 0;
if (prof->total_cycles > 0)
pct = 100.0 * (double)fn->self_cycles / (double)prof->total_cycles;
fprintf(f, "%s,0x%08x,%lld,%lld,%lld,%lld,%lld,%.2f\n",
fn->name, fn->start_addr,
(long long)fn->call_count,
(long long)fn->total_cycles,
(long long)fn->self_cycles,
(long long)fn->total_insns,
(long long)fn->self_insns,
pct);
}
}
void profiler_export_gprof(profiler_t *prof, FILE *f)
{
/* gprof flat profile format */
profiler_export_text(prof, f);
}
void profiler_export_flamegraph(profiler_t *prof, FILE *f)
{
int i;
if (!prof || !f)
return;
/* Flame graph format: stack;stack;stack count */
for (i = 0; i < prof->num_functions; i++) {
func_profile_t *fn = &prof->functions[i];
if (fn->self_cycles == 0)
continue;
fprintf(f, "%s %lld\n", fn->name, (long long)fn->self_cycles);
}
}
/* ========== Summary Output ========== */
void profiler_print_summary(profiler_t *prof, FILE *f)
{
double ipc, cpi;
if (!prof || !f)
return;
fprintf(f, "\n========== Profiler Summary ==========\n");
fprintf(f, "Total cycles: %lld\n", (long long)prof->total_cycles);
fprintf(f, "Total instructions: %lld\n", (long long)prof->total_insns);
if (prof->total_cycles > 0) {
ipc = (double)prof->total_insns / (double)prof->total_cycles;
cpi = (double)prof->total_cycles / (double)prof->total_insns;
fprintf(f, "IPC: %.4f\n", ipc);
fprintf(f, "CPI: %.4f\n", cpi);
}
fprintf(f, "Total calls: %lld\n", (long long)prof->total_calls);
fprintf(f, "Total returns: %lld\n", (long long)prof->total_returns);
fprintf(f, "Functions tracked: %d\n", prof->num_functions);
fprintf(f, "Call edges: %d\n", prof->num_edges);
fprintf(f, "Branches tracked: %d\n", prof->num_branches);
fprintf(f, "\n");
}
void profiler_print_functions(profiler_t *prof, FILE *f, int max_functions)
{
int indices[100];
int n, i;
if (!prof || !f)
return;
if (max_functions > 100)
max_functions = 100;
n = profiler_get_top_functions(prof, indices, max_functions);
fprintf(f, "\n========== Top Functions by Self Time ==========\n");
fprintf(f, "%-30s %10s %12s %8s %12s\n",
"Function", "Calls", "Self Cycles", "Self%", "Total Cycles");
fprintf(f, "----------------------------------------------------------------------\n");
for (i = 0; i < n; i++) {
func_profile_t *fn = &prof->functions[indices[i]];
double pct = 0;
if (prof->total_cycles > 0)
pct = 100.0 * (double)fn->self_cycles / (double)prof->total_cycles;
fprintf(f, "%-30s %10lld %12lld %7.2f%% %12lld\n",
fn->name,
(long long)fn->call_count,
(long long)fn->self_cycles,
pct,
(long long)fn->total_cycles);
}
fprintf(f, "\n");
}
void profiler_print_callgraph(profiler_t *prof, FILE *f)
{
int i;
if (!prof || !f)
return;
fprintf(f, "\n========== Call Graph ==========\n");
fprintf(f, "%-25s -> %-25s %10s\n", "Caller", "Callee", "Count");
fprintf(f, "--------------------------------------------------------------\n");
for (i = 0; i < prof->num_edges; i++) {
call_edge_t *edge = &prof->call_edges[i];
if (edge->count == 0)
continue;
const char *caller = "?";
const char *callee = "?";
if (edge->caller_idx >= 0 && edge->caller_idx < prof->num_functions)
caller = prof->functions[edge->caller_idx].name;
if (edge->callee_idx >= 0 && edge->callee_idx < prof->num_functions)
callee = prof->functions[edge->callee_idx].name;
fprintf(f, "%-25s -> %-25s %10lld\n",
caller, callee, (long long)edge->count);
}
fprintf(f, "\n");
}
void profiler_print_hotspots(profiler_t *prof, FILE *f, int max_spots)
{
md_addr_t addrs[100];
counter_t counts[100];
int n, i;
if (!prof || !f)
return;
if (max_spots > 100)
max_spots = 100;
n = profiler_get_hotspots(prof, addrs, counts, max_spots);
fprintf(f, "\n========== Hot Spots ==========\n");
fprintf(f, "%-12s %15s %8s\n", "Address", "Exec Count", "Pct");
fprintf(f, "--------------------------------------\n");
for (i = 0; i < n; i++) {
double pct = 0;
if (prof->total_insns > 0)
pct = 100.0 * (double)counts[i] / (double)prof->total_insns;
fprintf(f, "0x%08x %15lld %7.2f%%\n",
addrs[i], (long long)counts[i], pct);
}
fprintf(f, "\n");
}
void profiler_print_branches(profiler_t *prof, FILE *f, int max_branches)
{
int i, count;
if (!prof || !f || !prof->branches)
return;
fprintf(f, "\n========== Branch Statistics ==========\n");
fprintf(f, "%-12s %10s %10s %10s %8s\n",
"Address", "Taken", "NotTaken", "Mispred", "Accuracy");
fprintf(f, "--------------------------------------------------------\n");
count = 0;
for (i = 0; i < prof->branches_size && count < max_branches; i++) {
branch_profile_t *bp = &prof->branches[i];
if (bp->pc == 0)
continue;
counter_t total = bp->predicted + bp->mispredicted;
double accuracy = (total > 0) ? 100.0 * (double)bp->predicted / (double)total : 0;
fprintf(f, "0x%08x %10lld %10lld %10lld %7.2f%%\n",
bp->pc,
(long long)bp->taken_count,
(long long)bp->not_taken_count,
(long long)bp->mispredicted,
accuracy);
count++;
}
fprintf(f, "\n");
}
void profiler_print_memory_patterns(profiler_t *prof, FILE *f)
{
if (!prof || !f)
return;
profiler_analyze_memory(prof);
fprintf(f, "\n========== Memory Access Patterns ==========\n");
fprintf(f, "Total accesses: %lld\n", (long long)prof->mem_stats.total_accesses);
fprintf(f, "Sequential accesses: %lld\n", (long long)prof->mem_stats.sequential_accesses);
fprintf(f, "Strided accesses: %lld\n", (long long)prof->mem_stats.strided_accesses);
fprintf(f, "Random accesses: %lld\n", (long long)prof->mem_stats.random_accesses);
fprintf(f, "Average stride: %lld bytes\n", (long long)prof->mem_stats.avg_stride);
fprintf(f, "Spatial locality: %.2f%%\n", prof->mem_stats.spatial_locality * 100.0);
fprintf(f, "Temporal locality: %.2f%%\n", prof->mem_stats.temporal_locality * 100.0);
fprintf(f, "\n");
}