86 lines
2.9 KiB
C
86 lines
2.9 KiB
C
#include "rvprof_internal.h"
|
|
|
|
// get function id for given name / caller combination or create new one
|
|
int rvprof_stats_find_or_create_function( const char* name, const char* caller){
|
|
if(g_rvprof.config.merge_regions){
|
|
// ignore caller when merging stacks
|
|
for (int i=0; i<g_rvprof.functions.size; i++){
|
|
if (strcmp(g_rvprof.functions.data[i].name, name) == 0){
|
|
return i;
|
|
}
|
|
}
|
|
} else {
|
|
for (int i = 0; i<g_rvprof.functions.size; i++) {
|
|
if (strcmp(g_rvprof.functions.data[i].name, name) == 0 && strcmp(g_rvprof.functions.data[i].caller, caller) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ensure capacity
|
|
int err = function_stats_array_ensure_capacity(&g_rvprof.functions, g_rvprof.functions.size);
|
|
if (err < 0) return -1;
|
|
|
|
// add entry
|
|
int idx = g_rvprof.functions.size++;
|
|
function_stats_t* stats = &g_rvprof.functions.data[idx];
|
|
memset(stats, 0, sizeof(function_stats_t));
|
|
strncpy(stats->name, name, MAX_NAME_LEN-1);
|
|
stats->name[MAX_NAME_LEN-1] = '\0';
|
|
if(!g_rvprof.config.merge_regions){
|
|
strncpy(stats->caller, caller, MAX_NAME_LEN-1);
|
|
stats->caller[MAX_NAME_LEN-1] = '\0';
|
|
}
|
|
|
|
return idx;
|
|
}
|
|
|
|
// get call stack or create it
|
|
int rvprof_stats_get_or_create_stack_id(void) {
|
|
static char call_stack[4096];
|
|
call_stack [0] = '\0';
|
|
|
|
for (int i=0; i<=g_rvprof.stack_ptr; i++){
|
|
if (i>0){
|
|
strcat(call_stack, "<");
|
|
}
|
|
strcat(call_stack, g_rvprof.regions.data[i].name);
|
|
}
|
|
|
|
// check if this stack exists already
|
|
for (int i=0; i<g_rvprof.stacks.size; i++){
|
|
if (g_rvprof.stacks.data[i].stack_path && strcmp(g_rvprof.stacks.data[i].stack_path, call_stack) == 0){
|
|
return g_rvprof.stacks.data[i].stid;
|
|
}
|
|
}
|
|
|
|
// create new stack
|
|
int err = stack_info_array_ensure_capacity(&g_rvprof.stacks, g_rvprof.stacks.size);
|
|
if (err < 0) return -1;
|
|
|
|
int stack_id = g_rvprof.stacks.size + 1; // start at 1
|
|
|
|
// add stack
|
|
g_rvprof.stacks.data[g_rvprof.stacks.size].stack_path = rvprof_malloc(strlen(call_stack)+1);
|
|
if (!g_rvprof.stacks.data[g_rvprof.stacks.size].stack_path){
|
|
return -1;
|
|
}
|
|
strcpy(g_rvprof.stacks.data[g_rvprof.stacks.size].stack_path, call_stack);
|
|
g_rvprof.stacks.data[g_rvprof.stacks.size].stid = stack_id;
|
|
g_rvprof.stacks.size++;
|
|
|
|
return stack_id;
|
|
}
|
|
|
|
// function metadata
|
|
void rvprof_stats_add_stack_id_to_function(int func_id, int stack_id) {
|
|
if (rvprof_memory_add_stack_id_to_function(func_id, stack_id) != RVPROF_SUCCESS) {
|
|
fprintf(stderr, "rvprof: Failed to add stack ID to function\n");
|
|
}
|
|
}
|
|
|
|
void rvprof_stats_add_caller_to_function(int func_id, const char* caller) {
|
|
if (rvprof_memory_add_caller_to_function(func_id, caller) != RVPROF_SUCCESS) {
|
|
fprintf(stderr, "rvprof: Failed to add caller to function\n");
|
|
}
|
|
} |