From 9dc484858a81f1eaee65264c441b5b3a692d4012 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Tue, 26 Mar 2024 11:59:54 -0700 Subject: [PATCH] eliminate geo cache fallback key/hash cache; we can just use the hash store to rewind --- src/df/gfx/df_view_rule_hooks.c | 36 ++--------- src/geo_cache/geo_cache.c | 102 +++++++------------------------- src/geo_cache/geo_cache.h | 28 ++------- 3 files changed, 30 insertions(+), 136 deletions(-) diff --git a/src/df/gfx/df_view_rule_hooks.c b/src/df/gfx/df_view_rule_hooks.c index eff10ef3..dbdd83b2 100644 --- a/src/df/gfx/df_view_rule_hooks.c +++ b/src/df/gfx/df_view_rule_hooks.c @@ -1112,39 +1112,13 @@ DF_GFX_VIEW_RULE_BLOCK_UI_FUNCTION_DEF(geo) DF_Entity *thread = df_entity_from_handle(ctrl_ctx->thread); DF_Entity *process = df_entity_ancestor_from_kind(thread, DF_EntityKind_Process); - //- rjf: produce unique keys for index buffer - U128 index_buffer_key = {0}; - { - U64 data[] = - { - (U64)process->ctrl_machine_id, - (U64)process->ctrl_handle.u64[0], - index_buffer_vaddr_range.min, - index_buffer_vaddr_range.max, - }; - index_buffer_key = hs_hash_from_data(str8((U8 *)data, sizeof(data))); - } - - //- rjf: produce unique keys for vertex buffer - U128 vertex_buffer_key = {0}; - { - U64 data[] = - { - (U64)process->ctrl_machine_id, - (U64)process->ctrl_handle.u64[0], - vertex_buffer_vaddr_range.min, - vertex_buffer_vaddr_range.max, - }; - vertex_buffer_key = hs_hash_from_data(str8((U8 *)data, sizeof(data))); - } - - //- rjf: address range -> hash - U128 index_buffer_hash = ctrl_stored_hash_from_process_vaddr_range(process->ctrl_machine_id, process->ctrl_handle, index_buffer_vaddr_range, 0, 0, 0); - U128 vertex_buffer_hash = ctrl_stored_hash_from_process_vaddr_range(process->ctrl_machine_id, process->ctrl_handle, vertex_buffer_vaddr_range, 0, 0, 0); + //- rjf: obtain keys for index buffer & vertex buffer memory + U128 index_buffer_key = ctrl_hash_store_key_from_process_vaddr_range(process->ctrl_machine_id, process->ctrl_handle, index_buffer_vaddr_range, 0); + U128 vertex_buffer_key = ctrl_hash_store_key_from_process_vaddr_range(process->ctrl_machine_id, process->ctrl_handle, vertex_buffer_vaddr_range, 0); //- rjf: get gpu buffers - R_Handle index_buffer = geo_buffer_from_key_hash(geo_scope, index_buffer_key, index_buffer_hash); - R_Handle vertex_buffer = geo_buffer_from_key_hash(geo_scope, vertex_buffer_key, vertex_buffer_hash); + R_Handle index_buffer = geo_buffer_from_key(geo_scope, index_buffer_key); + R_Handle vertex_buffer = geo_buffer_from_key(geo_scope, vertex_buffer_key); //- rjf: build preview F32 rate = 1 - pow_f32(2, (-15.f * df_dt())); diff --git a/src/geo_cache/geo_cache.c b/src/geo_cache/geo_cache.c index 58377510..cec24960 100644 --- a/src/geo_cache/geo_cache.c +++ b/src/geo_cache/geo_cache.c @@ -21,16 +21,6 @@ geo_init(void) geo_shared->stripes[idx].rw_mutex = os_rw_mutex_alloc(); geo_shared->stripes[idx].cv = os_condition_variable_alloc(); } - geo_shared->fallback_slots_count = 1024; - geo_shared->fallback_stripes_count = Min(geo_shared->fallback_slots_count, os_logical_core_count()); - geo_shared->fallback_slots = push_array(arena, GEO_KeyFallbackSlot, geo_shared->fallback_slots_count); - geo_shared->fallback_stripes = push_array(arena, GEO_Stripe, geo_shared->fallback_stripes_count); - for(U64 idx = 0; idx < geo_shared->fallback_stripes_count; idx += 1) - { - geo_shared->fallback_stripes[idx].arena = arena_alloc(); - geo_shared->fallback_stripes[idx].rw_mutex = os_rw_mutex_alloc(); - geo_shared->fallback_stripes[idx].cv = os_condition_variable_alloc(); - } geo_shared->u2x_ring_size = KB(64); geo_shared->u2x_ring_base = push_array_no_zero(arena, U8, geo_shared->u2x_ring_size); geo_shared->u2x_ring_cv = os_condition_variable_alloc(); @@ -144,7 +134,7 @@ geo_scope_touch_node__stripe_r_guarded(GEO_Scope *scope, GEO_Node *node) //~ rjf: Cache Lookups internal R_Handle -geo_buffer_from_key_hash(GEO_Scope *scope, U128 key, U128 hash) +geo_buffer_from_hash(GEO_Scope *scope, U128 hash) { R_Handle handle = {0}; if(!u128_match(hash, u128_zero())) @@ -201,42 +191,23 @@ geo_buffer_from_key_hash(GEO_Scope *scope, U128 key, U128 hash) } if(node_is_new) { - geo_u2x_enqueue_req(key, hash, max_U64); + geo_u2x_enqueue_req(hash, max_U64); } - if(r_handle_match(handle, r_handle_zero())) + } + return handle; +} + +internal R_Handle +geo_buffer_from_key(GEO_Scope *scope, U128 key) +{ + R_Handle handle = {0}; + for(U64 rewind_idx = 0; rewind_idx < 2; rewind_idx += 1) + { + U128 hash = hs_hash_from_key(key, rewind_idx); + handle = geo_buffer_from_hash(scope, hash); + if(!r_handle_match(handle, r_handle_zero())) { - U128 fallback_hash = {0}; - U64 fallback_slot_idx = key.u64[1]%geo_shared->fallback_slots_count; - U64 fallback_stripe_idx = fallback_slot_idx%geo_shared->fallback_stripes_count; - GEO_KeyFallbackSlot *fallback_slot = &geo_shared->fallback_slots[fallback_slot_idx]; - GEO_Stripe *fallback_stripe = &geo_shared->fallback_stripes[fallback_stripe_idx]; - OS_MutexScopeR(fallback_stripe->rw_mutex) for(GEO_KeyFallbackNode *n = fallback_slot->first; n != 0; n = n->next) - { - if(u128_match(key, n->key)) - { - fallback_hash = n->hash; - break; - } - } - if(!u128_match(fallback_hash, u128_zero())) - { - U64 retry_slot_idx = fallback_hash.u64[1]%geo_shared->slots_count; - U64 retry_stripe_idx = retry_slot_idx%geo_shared->stripes_count; - GEO_Slot *retry_slot = &geo_shared->slots[retry_slot_idx]; - GEO_Stripe *retry_stripe = &geo_shared->stripes[retry_stripe_idx]; - OS_MutexScopeR(retry_stripe->rw_mutex) - { - for(GEO_Node *n = retry_slot->first; n != 0; n = n->next) - { - if(u128_match(fallback_hash, n->hash)) - { - handle = n->buffer; - geo_scope_touch_node__stripe_r_guarded(scope, n); - break; - } - } - } - } + break; } } return handle; @@ -246,17 +217,16 @@ geo_buffer_from_key_hash(GEO_Scope *scope, U128 key, U128 hash) //~ rjf: Transfer Threads internal B32 -geo_u2x_enqueue_req(U128 key, U128 hash, U64 endt_us) +geo_u2x_enqueue_req(U128 hash, U64 endt_us) { B32 good = 0; OS_MutexScope(geo_shared->u2x_ring_mutex) for(;;) { U64 unconsumed_size = geo_shared->u2x_ring_write_pos-geo_shared->u2x_ring_read_pos; U64 available_size = geo_shared->u2x_ring_size-unconsumed_size; - if(available_size >= sizeof(key)+sizeof(hash)) + if(available_size >= sizeof(hash)) { good = 1; - geo_shared->u2x_ring_write_pos += ring_write_struct(geo_shared->u2x_ring_base, geo_shared->u2x_ring_size, geo_shared->u2x_ring_write_pos, &key); geo_shared->u2x_ring_write_pos += ring_write_struct(geo_shared->u2x_ring_base, geo_shared->u2x_ring_size, geo_shared->u2x_ring_write_pos, &hash); break; } @@ -274,14 +244,13 @@ geo_u2x_enqueue_req(U128 key, U128 hash, U64 endt_us) } internal void -geo_u2x_dequeue_req(U128 *key_out, U128 *hash_out) +geo_u2x_dequeue_req(U128 *hash_out) { OS_MutexScope(geo_shared->u2x_ring_mutex) for(;;) { U64 unconsumed_size = geo_shared->u2x_ring_write_pos-geo_shared->u2x_ring_read_pos; - if(unconsumed_size >= sizeof(*key_out)+sizeof(*hash_out)) + if(unconsumed_size >= sizeof(*hash_out)) { - geo_shared->u2x_ring_read_pos += ring_read_struct(geo_shared->u2x_ring_base, geo_shared->u2x_ring_size, geo_shared->u2x_ring_read_pos, key_out); geo_shared->u2x_ring_read_pos += ring_read_struct(geo_shared->u2x_ring_base, geo_shared->u2x_ring_size, geo_shared->u2x_ring_read_pos, hash_out); break; } @@ -298,9 +267,8 @@ geo_xfer_thread__entry_point(void *p) HS_Scope *scope = hs_scope_open(); //- rjf: decode - U128 key = {0}; U128 hash = {0}; - geo_u2x_dequeue_req(&key, &hash); + geo_u2x_dequeue_req(&hash); //- rjf: unpack hash U64 slot_idx = hash.u64[1]%geo_shared->slots_count; @@ -351,34 +319,6 @@ geo_xfer_thread__entry_point(void *p) } } - //- rjf: commit this key/hash pair to fallback cache - if(got_task && !u128_match(key, u128_zero()) && !u128_match(hash, u128_zero())) - { - U64 fallback_slot_idx = key.u64[1]%geo_shared->fallback_slots_count; - U64 fallback_stripe_idx = fallback_slot_idx%geo_shared->fallback_stripes_count; - GEO_KeyFallbackSlot *fallback_slot = &geo_shared->fallback_slots[fallback_slot_idx]; - GEO_Stripe *fallback_stripe = &geo_shared->fallback_stripes[fallback_stripe_idx]; - OS_MutexScopeW(fallback_stripe->rw_mutex) - { - GEO_KeyFallbackNode *node = 0; - for(GEO_KeyFallbackNode *n = fallback_slot->first; n != 0; n = n->next) - { - if(u128_match(n->key, key)) - { - node = n; - break; - } - } - if(node == 0) - { - node = push_array(fallback_stripe->arena, GEO_KeyFallbackNode, 1); - SLLQueuePush(fallback_slot->first, fallback_slot->last, node); - } - node->key = key; - node->hash = hash; - } - } - hs_scope_close(scope); } } diff --git a/src/geo_cache/geo_cache.h b/src/geo_cache/geo_cache.h index 256f29ac..60d882b3 100644 --- a/src/geo_cache/geo_cache.h +++ b/src/geo_cache/geo_cache.h @@ -7,21 +7,6 @@ //////////////////////////////// //~ rjf: Cache Types -typedef struct GEO_KeyFallbackNode GEO_KeyFallbackNode; -struct GEO_KeyFallbackNode -{ - GEO_KeyFallbackNode *next; - U128 key; - U128 hash; -}; - -typedef struct GEO_KeyFallbackSlot GEO_KeyFallbackSlot; -struct GEO_KeyFallbackSlot -{ - GEO_KeyFallbackNode *first; - GEO_KeyFallbackNode *last; -}; - typedef struct GEO_Node GEO_Node; struct GEO_Node { @@ -97,12 +82,6 @@ struct GEO_Shared GEO_Stripe *stripes; GEO_Node **stripes_free_nodes; - // rjf: fallback cache - U64 fallback_slots_count; - U64 fallback_stripes_count; - GEO_KeyFallbackSlot *fallback_slots; - GEO_Stripe *fallback_stripes; - // rjf: user -> xfer thread U64 u2x_ring_size; U8 *u2x_ring_base; @@ -151,13 +130,14 @@ internal void geo_scope_touch_node__stripe_r_guarded(GEO_Scope *scope, GEO_Node //////////////////////////////// //~ rjf: Cache Lookups -internal R_Handle geo_buffer_from_key_hash(GEO_Scope *scope, U128 key, U128 hash); +internal R_Handle geo_buffer_from_hash(GEO_Scope *scope, U128 hash); +internal R_Handle geo_buffer_from_key(GEO_Scope *scope, U128 key); //////////////////////////////// //~ rjf: Transfer Threads -internal B32 geo_u2x_enqueue_req(U128 key, U128 hash, U64 endt_us); -internal void geo_u2x_dequeue_req(U128 *key_out, U128 *hash_out); +internal B32 geo_u2x_enqueue_req(U128 hash, U64 endt_us); +internal void geo_u2x_dequeue_req(U128 *hash_out); internal void geo_xfer_thread__entry_point(void *p); ////////////////////////////////