Files
raddebugger/src/ptr_graph_cache/ptr_graph_cache.c
T

203 lines
6.0 KiB
C

// Copyright (c) Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ rjf: Main Layer Initialization
internal void
ptg_init(void)
{
Arena *arena = arena_alloc();
ptg_shared = push_array(arena, PTG_Shared, 1);
ptg_shared->arena = arena;
ptg_shared->slots_count = 1024;
ptg_shared->stripes_count = Min(ptg_shared->slots_count, os_get_system_info()->logical_processor_count);
ptg_shared->slots = push_array(arena, PTG_GraphSlot, ptg_shared->slots_count);
ptg_shared->stripes = push_array(arena, PTG_GraphStripe, ptg_shared->stripes_count);
for(U64 idx = 0; idx < ptg_shared->stripes_count; idx += 1)
{
ptg_shared->stripes[idx].arena = arena_alloc();
ptg_shared->stripes[idx].rw_mutex = rw_mutex_alloc();
ptg_shared->stripes[idx].cv = cond_var_alloc();
}
ptg_shared->u2b_ring_size = KB(64);
ptg_shared->u2b_ring_base = push_array_no_zero(arena, U8, ptg_shared->u2b_ring_size);
ptg_shared->u2b_ring_cv = cond_var_alloc();
ptg_shared->u2b_ring_mutex = mutex_alloc();
ptg_shared->builder_thread_count = Clamp(1, os_get_system_info()->logical_processor_count-1, 4);
ptg_shared->builder_threads = push_array(arena, Thread, ptg_shared->builder_thread_count);
for(U64 idx = 0; idx < ptg_shared->builder_thread_count; idx += 1)
{
ptg_shared->builder_threads[idx] = thread_launch(ptg_builder_thread__entry_point, (void *)idx);
}
ptg_shared->evictor_thread = thread_launch(ptg_evictor_thread__entry_point, 0);
}
////////////////////////////////
//~ rjf: Cache Lookups
internal PTG_Graph *
ptg_graph_from_key(Access *access, PTG_Key *key)
{
PTG_Graph *g = 0;
return g;
}
////////////////////////////////
//~ rjf: Transfer Threads
internal B32
ptg_u2b_enqueue_req(PTG_Key *key, U64 endt_us)
{
B32 good = 0;
MutexScope(ptg_shared->u2b_ring_mutex) for(;;)
{
U64 unconsumed_size = ptg_shared->u2b_ring_write_pos-ptg_shared->u2b_ring_read_pos;
U64 available_size = ptg_shared->u2b_ring_size-unconsumed_size;
if(available_size >= sizeof(key))
{
good = 1;
ptg_shared->u2b_ring_write_pos += ring_write_struct(ptg_shared->u2b_ring_base, ptg_shared->u2b_ring_size, ptg_shared->u2b_ring_write_pos, &key);
break;
}
if(os_now_microseconds() >= endt_us)
{
break;
}
cond_var_wait(ptg_shared->u2b_ring_cv, ptg_shared->u2b_ring_mutex, endt_us);
}
if(good)
{
cond_var_broadcast(ptg_shared->u2b_ring_cv);
}
return good;
}
internal void
ptg_u2b_dequeue_req(PTG_Key *key_out)
{
MutexScope(ptg_shared->u2b_ring_mutex) for(;;)
{
U64 unconsumed_size = ptg_shared->u2b_ring_write_pos-ptg_shared->u2b_ring_read_pos;
if(unconsumed_size >= sizeof(*key_out))
{
ptg_shared->u2b_ring_read_pos += ring_read_struct(ptg_shared->u2b_ring_base, ptg_shared->u2b_ring_size, ptg_shared->u2b_ring_read_pos, key_out);
break;
}
cond_var_wait(ptg_shared->u2b_ring_cv, ptg_shared->u2b_ring_mutex, max_U64);
}
cond_var_broadcast(ptg_shared->u2b_ring_cv);
}
internal void
ptg_builder_thread__entry_point(void *p)
{
for(;;)
{
Access *access = access_open();
//- rjf: get next key
PTG_Key key = {0};
ptg_u2b_dequeue_req(&key);
//- rjf: unpack hash
U64 slot_idx = key.root_hash.u64[1]%ptg_shared->slots_count;
U64 stripe_idx = slot_idx%ptg_shared->stripes_count;
PTG_GraphSlot *slot = &ptg_shared->slots[slot_idx];
PTG_GraphStripe *stripe = &ptg_shared->stripes[stripe_idx];
//- rjf: take task
B32 got_task = 0;
MutexScopeR(stripe->rw_mutex)
{
for(PTG_GraphNode *n = slot->first; n != 0; n = n->next)
{
if(MemoryMatchStruct(&n->key, &key))
{
got_task = !ins_atomic_u32_eval_cond_assign(&n->is_working, 1, 0);
break;
}
}
}
//- rjf: do task
if(got_task)
{
}
//- rjf: commit results to cache
if(got_task) MutexScopeW(stripe->rw_mutex)
{
for(PTG_GraphNode *n = slot->first; n != 0; n = n->next)
{
if(MemoryMatchStruct(&n->key, &key))
{
ins_atomic_u32_eval_assign(&n->is_working, 0);
ins_atomic_u64_inc_eval(&n->load_count);
break;
}
}
}
access_close(access);
}
}
////////////////////////////////
//~ rjf: Evictor Threads
internal void
ptg_evictor_thread__entry_point(void *p)
{
for(;;)
{
U64 check_time_us = os_now_microseconds();
U64 check_time_user_clocks = ptg_user_clock_idx();
U64 evict_threshold_us = 10*1000000;
U64 evict_threshold_user_clocks = 10;
for(U64 slot_idx = 0; slot_idx < ptg_shared->slots_count; slot_idx += 1)
{
U64 stripe_idx = slot_idx%ptg_shared->stripes_count;
PTG_GraphSlot *slot = &ptg_shared->slots[slot_idx];
PTG_GraphStripe *stripe = &ptg_shared->stripes[stripe_idx];
B32 slot_has_work = 0;
MutexScopeR(stripe->rw_mutex)
{
for(PTG_GraphNode *n = slot->first; n != 0; n = n->next)
{
if(n->scope_ref_count == 0 &&
n->last_time_touched_us+evict_threshold_us <= check_time_us &&
n->last_user_clock_idx_touched+evict_threshold_user_clocks <= check_time_user_clocks &&
n->load_count != 0 &&
n->is_working == 0)
{
slot_has_work = 1;
break;
}
}
}
if(slot_has_work) MutexScopeW(stripe->rw_mutex)
{
for(PTG_GraphNode *n = slot->first, *next = 0; n != 0; n = next)
{
next = n->next;
if(n->scope_ref_count == 0 &&
n->last_time_touched_us+evict_threshold_us <= check_time_us &&
n->last_user_clock_idx_touched+evict_threshold_user_clocks <= check_time_user_clocks &&
n->load_count != 0 &&
n->is_working == 0)
{
DLLRemove(slot->first, slot->last, n);
arena_clear(n->arena);
SLLStackPush(stripe->free_node, n);
}
}
}
os_sleep_milliseconds(5);
}
os_sleep_milliseconds(1000);
}
}