eliminate ptr graph cache experiment

This commit is contained in:
Ryan Fleury
2025-09-25 11:52:33 -07:00
parent f61cd2b673
commit 6d39a66672
2 changed files with 0 additions and 390 deletions
-204
View File
@@ -1,204 +0,0 @@
// 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)
{
#if 0
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);
}
#endif
}
-186
View File
@@ -1,186 +0,0 @@
// Copyright (c) Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
#ifndef PTR_GRAPH_CACHE_H
#define PTR_GRAPH_CACHE_H
////////////////////////////////
//~ rjf: Graph Search Key
typedef struct PTG_Key PTG_Key;
struct PTG_Key
{
U128 root_hash;
U64 link_offsets[8];
U64 link_offsets_count;
};
////////////////////////////////
//~ rjf: Cache Types
typedef struct PTG_Node PTG_Node;
struct PTG_Node
{
U64 value;
};
typedef struct PTG_Link PTG_Link;
struct PTG_Link
{
U32 from;
U32 to;
};
typedef struct PTG_NodeChunkNode PTG_NodeChunkNode;
struct PTG_NodeChunkNode
{
PTG_NodeChunkNode *next;
PTG_Node *v;
U64 count;
U64 cap;
};
typedef struct PTG_NodeChunkList PTG_NodeChunkList;
struct PTG_NodeChunkList
{
PTG_NodeChunkNode *first;
PTG_NodeChunkNode *last;
U64 chunk_count;
U64 total_count;
};
typedef struct PTG_NodeArray PTG_NodeArray;
struct PTG_NodeArray
{
PTG_Node *v;
U64 count;
};
typedef struct PTG_LinkChunkNode PTG_LinkChunkNode;
struct PTG_LinkChunkNode
{
PTG_LinkChunkNode *next;
PTG_Link *v;
U64 count;
U64 cap;
};
typedef struct PTG_LinkChunkList PTG_LinkChunkList;
struct PTG_LinkChunkList
{
PTG_LinkChunkNode *first;
PTG_LinkChunkNode *last;
U64 chunk_count;
U64 total_count;
};
typedef struct PTG_LinkArray PTG_LinkArray;
struct PTG_LinkArray
{
PTG_Link *v;
U64 count;
};
typedef struct PTG_Graph PTG_Graph;
struct PTG_Graph
{
PTG_NodeArray nodes;
PTG_LinkArray links;
};
typedef struct PTG_GraphNode PTG_GraphNode;
struct PTG_GraphNode
{
// rjf: links
PTG_GraphNode *next;
PTG_GraphNode *prev;
// rjf: key
PTG_Key key;
// rjf: metadata
U64 scope_ref_count;
U64 last_time_touched_us;
U64 last_user_clock_idx_touched;
U64 load_count;
B32 is_working;
// rjf: content
Arena *arena;
PTG_Graph graph;
};
typedef struct PTG_GraphSlot PTG_GraphSlot;
struct PTG_GraphSlot
{
PTG_GraphNode *first;
PTG_GraphNode *last;
};
typedef struct PTG_GraphStripe PTG_GraphStripe;
struct PTG_GraphStripe
{
Arena *arena;
RWMutex rw_mutex;
CondVar cv;
PTG_GraphNode *free_node;
};
////////////////////////////////
//~ rjf: Shared State
typedef struct PTG_Shared PTG_Shared;
struct PTG_Shared
{
Arena *arena;
// rjf: cache
U64 slots_count;
U64 stripes_count;
PTG_GraphSlot *slots;
PTG_GraphStripe *stripes;
// rjf: user -> xfer thread
U64 u2b_ring_size;
U8 *u2b_ring_base;
U64 u2b_ring_write_pos;
U64 u2b_ring_read_pos;
CondVar u2b_ring_cv;
Mutex u2b_ring_mutex;
// rjf: builder threads
U64 builder_thread_count;
Thread *builder_threads;
// rjf: evictor thread
Thread evictor_thread;
};
////////////////////////////////
//~ rjf: Globals
global PTG_Shared *ptg_shared = 0;
////////////////////////////////
//~ rjf: Main Layer Initialization
internal void ptg_init(void);
////////////////////////////////
//~ rjf: Cache Lookups
internal PTG_Graph *ptg_graph_from_key(Access *access, PTG_Key *key);
////////////////////////////////
//~ rjf: Transfer Threads
internal B32 ptg_u2b_enqueue_req(PTG_Key *key, U64 endt_us);
internal void ptg_u2b_dequeue_req(PTG_Key *key_out);
internal void ptg_builder_thread__entry_point(void *p);
////////////////////////////////
//~ rjf: Evictor Threads
internal void ptg_evictor_thread__entry_point(void *p);
#endif // PTR_GRAPH_CACHE_H