Skip to content

Commit ae17084

Browse files
committed
better artifact cache cancellation thread work organization - only do more frequent cancellation scans when in a tick, otherwise sleep
1 parent 721649b commit ae17084

File tree

4 files changed

+63
-41
lines changed

4 files changed

+63
-41
lines changed

src/artifact_cache/artifact_cache.c

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ac_init(void)
1919
ac_shared->req_batches[idx].arena = arena_alloc();
2020
}
2121
ac_shared->cancel_thread = thread_launch(ac_cancel_thread_entry_point, 0);
22+
ac_shared->cancel_thread_mutex = mutex_alloc();
23+
mutex_take(ac_shared->cancel_thread_mutex);
2224
}
2325

2426
////////////////////////////////
@@ -211,6 +213,14 @@ ac_async_tick(void)
211213
{
212214
Temp scratch = scratch_begin(0, 0);
213215

216+
//////////////////////////////
217+
//- rjf: enable cancellation scanning
218+
//
219+
if(lane_idx() == 0)
220+
{
221+
mutex_drop(ac_shared->cancel_thread_mutex);
222+
}
223+
214224
//////////////////////////////
215225
//- rjf: do eviction pass across all caches
216226
//
@@ -575,6 +585,13 @@ ac_async_tick(void)
575585
}
576586
lane_sync();
577587

588+
//////////////////////////////
589+
//- rjf: disable cancellation scanning
590+
//
591+
if(lane_idx() == 0)
592+
{
593+
mutex_take(ac_shared->cancel_thread_mutex);
594+
}
578595
scratch_end(scratch);
579596
}
580597

@@ -586,46 +603,47 @@ ac_cancel_thread_entry_point(void *p)
586603
{
587604
for(;;)
588605
{
589-
os_sleep_milliseconds(500);
590-
591-
//- rjf: scan in-flight nodes for expiration
592-
for EachIndex(cache_slot_idx, ac_shared->cache_slots_count)
606+
os_sleep_milliseconds(50);
607+
MutexScope(ac_shared->cancel_thread_mutex)
593608
{
594-
Stripe *cache_stripe = stripe_from_slot_idx(&ac_shared->cache_stripes, cache_slot_idx);
595-
RWMutexScope(cache_stripe->rw_mutex, 0)
609+
for EachIndex(cache_slot_idx, ac_shared->cache_slots_count)
596610
{
597-
for EachNode(cache, AC_Cache, ac_shared->cache_slots[cache_slot_idx])
611+
Stripe *cache_stripe = stripe_from_slot_idx(&ac_shared->cache_stripes, cache_slot_idx);
612+
RWMutexScope(cache_stripe->rw_mutex, 0)
598613
{
599-
Rng1U64 slot_range = lane_range(cache->slots_count);
600-
for EachInRange(slot_idx, slot_range)
614+
for EachNode(cache, AC_Cache, ac_shared->cache_slots[cache_slot_idx])
601615
{
602-
AC_Slot *slot = &cache->slots[slot_idx];
603-
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
604-
for(B32 write_mode = 0; write_mode <= 1; write_mode += 1)
616+
Rng1U64 slot_range = lane_range(cache->slots_count);
617+
for EachInRange(slot_idx, slot_range)
605618
{
606-
B32 slot_has_work = 0;
607-
RWMutexScope(stripe->rw_mutex, write_mode)
619+
AC_Slot *slot = &cache->slots[slot_idx];
620+
Stripe *stripe = stripe_from_slot_idx(&cache->stripes, slot_idx);
621+
for(B32 write_mode = 0; write_mode <= 1; write_mode += 1)
608622
{
609-
for(AC_Node *n = slot->first, *next = 0; n != 0; n = next)
623+
B32 slot_has_work = 0;
624+
RWMutexScope(stripe->rw_mutex, write_mode)
610625
{
611-
next = n->next;
612-
if(access_pt_is_expired(&n->access_pt, .time = n->evict_threshold_us) && ins_atomic_u64_eval(&n->working_count) > 0)
626+
for(AC_Node *n = slot->first, *next = 0; n != 0; n = next)
613627
{
614-
slot_has_work = 1;
615-
if(!write_mode)
628+
next = n->next;
629+
if(access_pt_is_expired(&n->access_pt, .time = n->evict_threshold_us) && ins_atomic_u64_eval(&n->working_count) > 0)
616630
{
617-
break;
618-
}
619-
else
620-
{
621-
n->cancelled = 1;
631+
slot_has_work = 1;
632+
if(!write_mode)
633+
{
634+
break;
635+
}
636+
else
637+
{
638+
n->cancelled = 1;
639+
}
622640
}
623641
}
624642
}
625-
}
626-
if(!slot_has_work)
627-
{
628-
break;
643+
if(!slot_has_work)
644+
{
645+
break;
646+
}
629647
}
630648
}
631649
}

src/artifact_cache/artifact_cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ struct AC_Shared
129129

130130
// rjf: cancel thread
131131
Thread cancel_thread;
132+
Mutex cancel_thread_mutex;
132133
};
133134

134135
////////////////////////////////

src/dbg_info/dbg_info.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,11 +1301,11 @@ di_search_artifact_create(String8 key, B32 *cancel_signal, B32 *retry_out, U64 *
13011301
{
13021302
if(lane_idx() == 0)
13031303
{
1304-
sort_records = push_array(scratch.arena, SortRecord, sort_records_count);
1304+
sort_records = push_array_no_zero(scratch.arena, SortRecord, sort_records_count);
13051305
}
13061306
if(lane_idx() == lane_from_task_idx(1))
13071307
{
1308-
sort_records__swap = push_array(scratch.arena, SortRecord, sort_records_count);
1308+
sort_records__swap = push_array_no_zero(scratch.arena, SortRecord, sort_records_count);
13091309
}
13101310
lane_sync_u64(&sort_records, 0);
13111311
lane_sync_u64(&sort_records__swap, lane_from_task_idx(1));
@@ -1429,7 +1429,7 @@ di_search_artifact_create(String8 key, B32 *cancel_signal, B32 *retry_out, U64 *
14291429
if(lane_idx() == 0)
14301430
{
14311431
items.count = all_items->total_count;
1432-
items.v = push_array(arena, DI_SearchItem, items.count);
1432+
items.v = push_array_no_zero(arena, DI_SearchItem, items.count);
14331433
}
14341434
lane_sync_u64(&items.count, 0);
14351435
lane_sync_u64(&items.v, 0);

src/raddbg/raddbg_core.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10308,16 +10308,6 @@ rd_frame(void)
1030810308
}
1030910309
#endif
1031010310

10311-
//////////////////////////////
10312-
//- rjf: [windows] clear pages from working set shortly after startup, many of which will not be needed
10313-
//
10314-
#if OS_WINDOWS
10315-
if(rd_state->frame_index == 1) ProfScope("SetProcessWorkingSetSize")
10316-
{
10317-
SetProcessWorkingSetSize(GetCurrentProcess(), max_U64, max_U64);
10318-
}
10319-
#endif
10320-
1032110311
//////////////////////////////
1032210312
//- rjf: do per-frame resets
1032310313
//
@@ -16563,6 +16553,19 @@ rd_frame(void)
1656316553
U64 frame_time_us = end_time_us-begin_time_us;
1656416554
rd_state->frame_time_us_history[rd_state->frame_index%ArrayCount(rd_state->frame_time_us_history)] = frame_time_us;
1656516555

16556+
//////////////////////////////
16557+
//- rjf: [windows] clear pages from working set shortly after startup, many of which will not be needed
16558+
//
16559+
#if OS_WINDOWS
16560+
if(di_load_count() < 50)
16561+
{
16562+
if(rd_state->frame_index == 15) ProfScope("SetProcessWorkingSetSize")
16563+
{
16564+
SetProcessWorkingSetSize(GetCurrentProcess(), max_U64, max_U64);
16565+
}
16566+
}
16567+
#endif
16568+
1656616569
//////////////////////////////
1656716570
//- rjf: bump frame time counters
1656816571
//

0 commit comments

Comments
 (0)