eliminate ptr graph scope; simplify dasm cache eviction loop

This commit is contained in:
Ryan Fleury
2025-09-18 15:59:21 -07:00
parent 4e99312b75
commit 30ae9620f9
3 changed files with 43 additions and 160 deletions
+41 -40
View File
@@ -405,63 +405,64 @@ dasm_tick(void)
U64 stripe_idx = slot_idx%dasm_shared->stripes_count;
DASM_Slot *slot = &dasm_shared->slots[slot_idx];
DASM_Stripe *stripe = &dasm_shared->stripes[stripe_idx];
B32 slot_has_work = 0;
MutexScopeR(stripe->rw_mutex)
for(B32 write_mode = 0; write_mode <= 1; write_mode += 1)
{
for(DASM_Node *n = slot->first; n != 0; n = n->next)
B32 slot_has_work = 0;
RWMutexScope(stripe->rw_mutex, write_mode) for(DASM_Node *n = slot->first; n != 0; n = n->next)
{
// rjf: node needs eviction
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 &&
ins_atomic_u64_eval(&n->working_count) == 0)
{
slot_has_work = 1;
break;
if(!write_mode)
{
break;
}
else
{
DLLRemove(slot->first, slot->last, n);
if(n->info_arena != 0)
{
arena_release(n->info_arena);
}
SLLStackPush(stripe->free_node, n);
}
}
// rjf: node needs recomputation
if(n->change_gen != 0 && n->change_gen != change_gen &&
n->last_time_requested_us+retry_threshold_us <= check_time_us &&
n->last_user_clock_idx_requested+retry_threshold_user_clocks <= check_time_user_clocks)
{
slot_has_work = 1;
break;
if(!write_mode)
{
break;
}
else
{
MutexScope(dasm_shared->req_mutex)
{
DASM_RequestNode *req_n = push_array(dasm_shared->req_arena, DASM_RequestNode, 1);
SLLQueuePush(dasm_shared->first_req, dasm_shared->last_req, req_n);
dasm_shared->req_count += 1;
req_n->v.root = n->root;
req_n->v.hash = n->hash;
req_n->v.params = n->params;
req_n->v.params.dbgi_key = di_key_copy(dasm_shared->req_arena, &req_n->v.params.dbgi_key);
}
n->last_time_requested_us = os_now_microseconds();
n->last_user_clock_idx_requested = check_time_user_clocks;
ins_atomic_u64_inc_eval(&n->working_count);
}
}
}
}
if(slot_has_work) MutexScopeW(stripe->rw_mutex)
{
for(DASM_Node *n = slot->first, *next = 0; n != 0; n = next)
if(!slot_has_work)
{
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 &&
ins_atomic_u64_eval(&n->working_count) == 0)
{
DLLRemove(slot->first, slot->last, n);
if(n->info_arena != 0)
{
arena_release(n->info_arena);
}
SLLStackPush(stripe->free_node, n);
}
if(n->change_gen != 0 && n->change_gen != change_gen &&
n->last_time_requested_us+retry_threshold_us <= check_time_us &&
n->last_user_clock_idx_requested+retry_threshold_user_clocks <= check_time_user_clocks)
{
MutexScope(dasm_shared->req_mutex)
{
DASM_RequestNode *req_n = push_array(dasm_shared->req_arena, DASM_RequestNode, 1);
SLLQueuePush(dasm_shared->first_req, dasm_shared->last_req, req_n);
dasm_shared->req_count += 1;
req_n->v.root = n->root;
req_n->v.hash = n->hash;
req_n->v.params = n->params;
req_n->v.params.dbgi_key = di_key_copy(dasm_shared->req_arena, &req_n->v.params.dbgi_key);
}
n->last_time_requested_us = os_now_microseconds();
n->last_user_clock_idx_requested = check_time_user_clocks;
ins_atomic_u64_inc_eval(&n->working_count);
}
break;
}
}
}
+1 -74
View File
@@ -33,83 +33,11 @@ ptg_init(void)
ptg_shared->evictor_thread = thread_launch(ptg_evictor_thread__entry_point, 0);
}
////////////////////////////////
//~ rjf: User Clock
internal void
ptg_user_clock_tick(void)
{
ins_atomic_u64_inc_eval(&ptg_shared->user_clock_idx);
}
internal U64
ptg_user_clock_idx(void)
{
return ins_atomic_u64_eval(&ptg_shared->user_clock_idx);
}
////////////////////////////////
//~ rjf: Scoped Access
internal PTG_Scope *
ptg_scope_open(void)
{
if(ptg_tctx == 0)
{
Arena *arena = arena_alloc();
ptg_tctx = push_array(arena, PTG_TCTX, 1);
ptg_tctx->arena = arena;
}
PTG_Scope *scope = ptg_tctx->free_scope;
if(scope)
{
SLLStackPop(ptg_tctx->free_scope);
}
else
{
scope = push_array_no_zero(ptg_tctx->arena, PTG_Scope, 1);
}
MemoryZeroStruct(scope);
return scope;
}
internal void
ptg_scope_close(PTG_Scope *scope)
{
for(PTG_Touch *touch = scope->top_touch, *next = 0; touch != 0; touch = next)
{
next = touch->next;
ins_atomic_u64_dec_eval(&touch->node->scope_ref_count);
SLLStackPush(ptg_tctx->free_touch, touch);
}
SLLStackPush(ptg_tctx->free_scope, scope);
}
internal void
ptg_scope_touch_node__stripe_r_guarded(PTG_Scope *scope, PTG_GraphNode *node)
{
PTG_Touch *touch = ptg_tctx->free_touch;
ins_atomic_u64_inc_eval(&node->scope_ref_count);
ins_atomic_u64_eval_assign(&node->last_time_touched_us, os_now_microseconds());
ins_atomic_u64_eval_assign(&node->last_user_clock_idx_touched, ptg_user_clock_idx());
if(touch != 0)
{
SLLStackPop(ptg_tctx->free_touch);
}
else
{
touch = push_array_no_zero(ptg_tctx->arena, PTG_Touch, 1);
}
MemoryZeroStruct(touch);
touch->node = node;
SLLStackPush(scope->top_touch, touch);
}
////////////////////////////////
//~ rjf: Cache Lookups
internal PTG_Graph *
ptg_graph_from_key(PTG_Scope *scope, PTG_Key *key)
ptg_graph_from_key(Access *access, PTG_Key *key)
{
PTG_Graph *g = 0;
return g;
@@ -198,7 +126,6 @@ ptg_builder_thread__entry_point(void *p)
}
//- rjf: commit results to cache
if(got_task) MutexScopeW(stripe->rw_mutex)
{
+1 -46
View File
@@ -126,34 +126,6 @@ struct PTG_GraphStripe
PTG_GraphNode *free_node;
};
////////////////////////////////
//~ rjf: Scoped Access Types
typedef struct PTG_Touch PTG_Touch;
struct PTG_Touch
{
PTG_Touch *next;
PTG_GraphNode *node;
};
typedef struct PTG_Scope PTG_Scope;
struct PTG_Scope
{
PTG_Scope *next;
PTG_Touch *top_touch;
};
////////////////////////////////
//~ rjf: Thread Context
typedef struct PTG_TCTX PTG_TCTX;
struct PTG_TCTX
{
Arena *arena;
PTG_Scope *free_scope;
PTG_Touch *free_touch;
};
////////////////////////////////
//~ rjf: Shared State
@@ -162,9 +134,6 @@ struct PTG_Shared
{
Arena *arena;
// rjf: user clock
U64 user_clock_idx;
// rjf: cache
U64 slots_count;
U64 stripes_count;
@@ -190,7 +159,6 @@ struct PTG_Shared
////////////////////////////////
//~ rjf: Globals
thread_static PTG_TCTX *ptg_tctx = 0;
global PTG_Shared *ptg_shared = 0;
////////////////////////////////
@@ -198,23 +166,10 @@ global PTG_Shared *ptg_shared = 0;
internal void ptg_init(void);
////////////////////////////////
//~ rjf: User Clock
internal void ptg_user_clock_tick(void);
internal U64 ptg_user_clock_idx(void);
////////////////////////////////
//~ rjf: Scoped Access
internal PTG_Scope *ptg_scope_open(void);
internal void ptg_scope_close(PTG_Scope *scope);
internal void ptg_scope_touch_node__stripe_r_guarded(PTG_Scope *scope, PTG_GraphNode *node);
////////////////////////////////
//~ rjf: Cache Lookups
internal PTG_Graph *ptg_graph_from_key(PTG_Scope *scope, PTG_Key *key);
internal PTG_Graph *ptg_graph_from_key(Access *access, PTG_Key *key);
////////////////////////////////
//~ rjf: Transfer Threads