2019-10-18 20:54:45 +00:00
|
|
|
/*
|
|
|
|
* 4coder_profile.cpp - Built in self profiling report.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_init(Profile_Global_List *list){
|
|
|
|
list->mutex = system_mutex_make();
|
|
|
|
list->node_arena = make_arena_system(KB(4));
|
2019-12-10 18:46:12 +00:00
|
|
|
list->disable_bits = ProfileEnable_UserBit;
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Profile_Thread*
|
2019-10-26 20:48:50 +00:00
|
|
|
prof__get_thread(Profile_Global_List *list, i32 thread_id){
|
2019-10-20 01:17:44 +00:00
|
|
|
Profile_Thread *result = 0;
|
2019-10-26 20:48:50 +00:00
|
|
|
for (Profile_Thread *node = list->first_thread;
|
2019-10-20 01:17:44 +00:00
|
|
|
node != 0;
|
|
|
|
node = node->next){
|
|
|
|
if (thread_id == node->thread_id){
|
|
|
|
result = node;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
2019-10-20 01:17:44 +00:00
|
|
|
if (result == 0){
|
2019-10-26 20:48:50 +00:00
|
|
|
result = push_array_zero(&list->node_arena, Profile_Thread, 1);
|
|
|
|
sll_queue_push(list->first_thread, list->last_thread, result);
|
|
|
|
list->thread_count += 1;
|
2019-10-20 01:17:44 +00:00
|
|
|
result->thread_id = thread_id;
|
|
|
|
}
|
|
|
|
return(result);
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_clear(Profile_Global_List *list){
|
|
|
|
Mutex_Lock lock(list->mutex);
|
|
|
|
for (Arena_Node *node = list->first_arena;
|
2019-10-20 01:17:44 +00:00
|
|
|
node != 0;
|
|
|
|
node = node->next){
|
|
|
|
linalloc_clear(&node->arena);
|
|
|
|
}
|
2019-10-26 20:48:50 +00:00
|
|
|
list->first_arena = 0;
|
|
|
|
list->last_arena = 0;
|
2019-10-20 01:17:44 +00:00
|
|
|
|
2019-10-26 20:48:50 +00:00
|
|
|
linalloc_clear(&list->node_arena);
|
|
|
|
list->first_thread = 0;
|
|
|
|
list->last_thread = 0;
|
|
|
|
list->thread_count = 0;
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_thread_flush(Thread_Context *tctx, Profile_Global_List *list){
|
2019-10-20 01:17:44 +00:00
|
|
|
if (tctx->prof_record_count > 0){
|
2019-10-26 20:48:50 +00:00
|
|
|
Mutex_Lock lock(list->mutex);
|
|
|
|
if (list->disable_bits == 0){
|
|
|
|
Profile_Thread* thread = prof__get_thread(list, system_thread_get_id());
|
2019-10-20 01:17:44 +00:00
|
|
|
|
2019-10-26 20:48:50 +00:00
|
|
|
Arena_Node* node = push_array(&list->node_arena, Arena_Node, 1);
|
|
|
|
sll_queue_push(list->first_arena, list->last_arena, node);
|
2019-10-20 01:17:44 +00:00
|
|
|
node->arena = tctx->prof_arena;
|
2019-11-22 05:37:11 +00:00
|
|
|
tctx->prof_arena = make_arena_system(KB(16));
|
2019-10-20 01:17:44 +00:00
|
|
|
|
2019-11-22 05:37:11 +00:00
|
|
|
if (tctx->prof_first != 0){
|
2019-10-20 01:17:44 +00:00
|
|
|
if (thread->first_record == 0){
|
|
|
|
thread->first_record = tctx->prof_first;
|
|
|
|
thread->last_record = tctx->prof_last;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
thread->last_record->next = tctx->prof_first;
|
|
|
|
thread->last_record = tctx->prof_last;
|
|
|
|
}
|
|
|
|
thread->record_count += tctx->prof_record_count;
|
2019-11-22 05:37:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
linalloc_clear(&tctx->prof_arena);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
2019-11-22 05:37:11 +00:00
|
|
|
tctx->prof_record_count = 0;
|
|
|
|
tctx->prof_first = 0;
|
|
|
|
tctx->prof_last = 0;
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:07:05 +00:00
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_thread_set_name(Thread_Context *tctx, Profile_Global_List *list, String_Const_u8 name){
|
|
|
|
Mutex_Lock lock(list->mutex);
|
|
|
|
Profile_Thread* thread = prof__get_thread(list, system_thread_get_id());
|
2019-10-22 22:07:05 +00:00
|
|
|
thread->name = name;
|
|
|
|
}
|
|
|
|
|
2019-10-26 20:48:50 +00:00
|
|
|
#define ProfileThreadName(tctx,list,name) profile_thread_set_name((tctx), (list), (name))
|
2019-10-22 22:07:05 +00:00
|
|
|
|
2019-10-18 20:54:45 +00:00
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_set_enabled(Profile_Global_List *list, b32 value, Profile_Enable_Flag flag){
|
|
|
|
Mutex_Lock lock(list->mutex);
|
2019-10-20 01:17:44 +00:00
|
|
|
if (value){
|
2019-10-26 20:48:50 +00:00
|
|
|
RemFlag(list->disable_bits, flag);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
else{
|
2019-10-26 20:48:50 +00:00
|
|
|
AddFlag(list->disable_bits, flag);
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function void
|
2019-10-20 01:17:44 +00:00
|
|
|
thread_profile_record__inner(Thread_Context *tctx, Profile_ID id, u64 time,
|
|
|
|
String_Const_u8 name, String_Const_u8 location){
|
|
|
|
Profile_Record *record = push_array_zero(&tctx->prof_arena, Profile_Record, 1);
|
|
|
|
sll_queue_push(tctx->prof_first, tctx->prof_last, record);
|
|
|
|
tctx->prof_record_count += 1;
|
|
|
|
record->id = id;
|
|
|
|
record->time = time;
|
|
|
|
record->location = location;
|
|
|
|
record->name = name;
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:17:44 +00:00
|
|
|
function Profile_ID
|
|
|
|
thread_profile_record_push(Thread_Context *tctx, u64 time,
|
|
|
|
String_Const_u8 name, String_Const_u8 location){
|
|
|
|
Profile_ID id = tctx->prof_id_counter;
|
|
|
|
tctx->prof_id_counter += 1;
|
|
|
|
thread_profile_record__inner(tctx, id, time, name, location);
|
|
|
|
return(id);
|
|
|
|
}
|
2019-10-18 20:54:45 +00:00
|
|
|
function void
|
2019-10-20 01:17:44 +00:00
|
|
|
thread_profile_record_pop(Thread_Context *tctx, u64 time, Profile_ID id){
|
|
|
|
Assert(tctx->prof_id_counter > 1);
|
|
|
|
tctx->prof_id_counter = id;
|
|
|
|
thread_profile_record__inner(tctx, id, time, SCu8(""), SCu8(""));
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:17:44 +00:00
|
|
|
function Profile_ID
|
|
|
|
thread_profile_record_push(Application_Links *app, u64 time,
|
|
|
|
String_Const_u8 name, String_Const_u8 location){
|
|
|
|
Thread_Context *tctx = get_thread_context(app);
|
|
|
|
return(thread_profile_record_push(tctx, time, name, location));
|
|
|
|
}
|
2019-10-18 20:54:45 +00:00
|
|
|
function void
|
2019-10-20 01:17:44 +00:00
|
|
|
thread_profile_record_pop(Application_Links *app, u64 time, Profile_ID id){
|
|
|
|
Thread_Context *tctx = get_thread_context(app);
|
|
|
|
thread_profile_record_pop(tctx, time, id);
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:17:44 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2019-10-18 20:54:45 +00:00
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_block__init(Thread_Context *tctx, Profile_Global_List *list,
|
|
|
|
String_Const_u8 name, String_Const_u8 location, Profile_Block *block){
|
2019-10-20 01:17:44 +00:00
|
|
|
block->tctx = tctx;
|
2019-10-26 20:48:50 +00:00
|
|
|
block->list = list;
|
2019-10-20 01:17:44 +00:00
|
|
|
block->is_closed = false;
|
|
|
|
block->id = thread_profile_record_push(tctx, system_now_time(), name, location);
|
|
|
|
}
|
|
|
|
function void
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_block__init(Thread_Context *tctx, Profile_Global_List *list,
|
|
|
|
String_Const_u8 name, String_Const_u8 location,
|
|
|
|
Profile_Scope_Block *block){
|
2019-10-20 01:17:44 +00:00
|
|
|
block->tctx = tctx;
|
2019-10-26 20:48:50 +00:00
|
|
|
block->list = list;
|
2019-10-20 01:17:44 +00:00
|
|
|
block->is_closed = false;
|
|
|
|
block->id = thread_profile_record_push(tctx, system_now_time(), name, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////
|
|
|
|
|
2019-10-26 20:48:50 +00:00
|
|
|
Profile_Block::Profile_Block(Thread_Context *tctx, Profile_Global_List *list,
|
|
|
|
String_Const_u8 name, String_Const_u8 location){
|
|
|
|
profile_block__init(tctx, list, name, location, this);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
Profile_Block::Profile_Block(Application_Links *app, String_Const_u8 name,
|
|
|
|
String_Const_u8 location){
|
2019-10-26 20:48:50 +00:00
|
|
|
Thread_Context *v_tctx = get_thread_context(app);
|
|
|
|
Profile_Global_List *v_list = get_core_profile_list(app);
|
|
|
|
profile_block__init(v_tctx, v_list, name, location, this);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
Profile_Block::~Profile_Block(){
|
|
|
|
this->close_now();
|
|
|
|
}
|
|
|
|
void
|
|
|
|
Profile_Block::close_now(){
|
|
|
|
if (!this->is_closed){
|
|
|
|
thread_profile_record_pop(this->tctx, system_now_time(), this->id);
|
|
|
|
this->is_closed = true;
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 01:17:44 +00:00
|
|
|
////////
|
|
|
|
|
2019-10-26 20:48:50 +00:00
|
|
|
Profile_Scope_Block::Profile_Scope_Block(Thread_Context *tctx, Profile_Global_List *list,
|
|
|
|
String_Const_u8 name, String_Const_u8 location){
|
|
|
|
profile_block__init(tctx, list, name, location, this);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
Profile_Scope_Block::Profile_Scope_Block(Application_Links *app, String_Const_u8 name,
|
|
|
|
String_Const_u8 location){
|
2019-10-26 20:48:50 +00:00
|
|
|
Thread_Context *v_tctx = get_thread_context(app);
|
|
|
|
Profile_Global_List *v_list = get_core_profile_list(app);
|
|
|
|
profile_block__init(v_tctx, v_list, name, location, this);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
Profile_Scope_Block::~Profile_Scope_Block(){
|
|
|
|
this->close_now();
|
2019-10-26 20:48:50 +00:00
|
|
|
profile_thread_flush(this->tctx, this->list);
|
2019-10-20 01:17:44 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
Profile_Scope_Block::close_now(){
|
|
|
|
if (!this->is_closed){
|
|
|
|
thread_profile_record_pop(this->tctx, system_now_time(), this->id);
|
|
|
|
this->is_closed = true;
|
|
|
|
}
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(profile_enable)
|
|
|
|
CUSTOM_DOC("Allow 4coder's self profiler to gather new profiling information.")
|
|
|
|
{
|
2019-10-26 20:48:50 +00:00
|
|
|
Profile_Global_List *list = get_core_profile_list(app);
|
|
|
|
profile_set_enabled(list, true, ProfileEnable_UserBit);
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(profile_disable)
|
|
|
|
CUSTOM_DOC("Prevent 4coder's self profiler from gathering new profiling information.")
|
|
|
|
{
|
2019-10-26 20:48:50 +00:00
|
|
|
Profile_Global_List *list = get_core_profile_list(app);
|
|
|
|
profile_set_enabled(list, false, ProfileEnable_UserBit);
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(profile_clear)
|
|
|
|
CUSTOM_DOC("Clear all profiling information from 4coder's self profiler.")
|
|
|
|
{
|
2019-10-26 20:48:50 +00:00
|
|
|
Profile_Global_List *list = get_core_profile_list(app);
|
|
|
|
profile_clear(list);
|
2019-10-18 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|