Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instrumentation (mostly) for DATAS #107853

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 164 additions & 3 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ class t_join
return join_struct.n_threads;
}

// This is for instrumentation only.
int get_join_lock()
{
Maoni0 marked this conversation as resolved.
Show resolved Hide resolved
return VolatileLoadWithoutBarrier (&join_struct.join_lock);
}

void destroy ()
{
dprintf (JOIN_LOG, ("Destroying join structure"));
Expand Down Expand Up @@ -2420,13 +2426,28 @@ last_recorded_gc_info gc_heap::last_full_blocking_gc_info;

uint64_t gc_heap::last_alloc_reset_suspended_end_time = 0;
size_t gc_heap::max_peak_heap_size = 0;
VOLATILE(size_t) gc_heap::llc_size = 0;

#ifdef BACKGROUND_GC
last_recorded_gc_info gc_heap::last_bgc_info[2];
VOLATILE(bool) gc_heap::is_last_recorded_bgc = false;
VOLATILE(int) gc_heap::last_bgc_info_index = 0;
#endif //BACKGROUND_GC

#ifdef DYNAMIC_HEAP_COUNT
size_t gc_heap::hc_change_cancelled_count_prep = 0;
#ifdef BACKGROUND_GC
int gc_heap::bgc_th_creation_hist_index = 0;
Maoni0 marked this conversation as resolved.
Show resolved Hide resolved
gc_heap::bgc_thread_creation_history gc_heap::bgc_th_creation_hist[max_bgc_thread_creation_count];
size_t gc_heap::bgc_th_count_created = 0;
size_t gc_heap::bgc_th_count_created_th_existed = 0;
size_t gc_heap::bgc_th_count_creation_failed = 0;
size_t gc_heap::bgc_init_gc_index = 0;
VOLATILE(short) gc_heap::bgc_init_n_heaps = 0;
size_t gc_heap::hc_change_cancelled_count_bgc = 0;
#endif //BACKGROUND_GC
#endif //DYNAMIC_HEAP_COUNT

#if defined(HOST_64BIT)
#define MAX_ALLOWED_MEM_LOAD 85

Expand Down Expand Up @@ -2898,6 +2919,8 @@ gc_heap::dynamic_heap_count_data_t SVR::gc_heap::dynamic_heap_count_data;
size_t gc_heap::current_total_soh_stable_size = 0;
uint64_t gc_heap::last_suspended_end_time = 0;
uint64_t gc_heap::change_heap_count_time = 0;
uint64_t gc_heap::total_change_heap_count = 0;
uint64_t gc_heap::total_change_heap_count_time = 0;
size_t gc_heap::gc_index_full_gc_end = 0;
uint64_t gc_heap::before_distribute_free_regions_time = 0;
bool gc_heap::trigger_initial_gen2_p = false;
Expand Down Expand Up @@ -7099,6 +7122,7 @@ void gc_heap::gc_thread_function ()
#endif //BACKGROUND_GC
{
dprintf (6666, ("changing heap count due to timeout"));
add_to_hc_history (hc_record_before_check_timeout);
check_heap_count();
}
}
Expand Down Expand Up @@ -7127,6 +7151,7 @@ void gc_heap::gc_thread_function ()
{
// this was a request to do a GC so make sure we follow through with one.
dprintf (6666, ("changing heap count at a GC start"));
add_to_hc_history (hc_record_before_check_gc_start);
check_heap_count ();
}
}
Expand All @@ -7146,6 +7171,8 @@ void gc_heap::gc_thread_function ()
VolatileLoadWithoutBarrier (&dynamic_heap_count_data.idle_thread_count)));
}

add_to_hc_history (hc_record_set_last_heaps);

dynamic_heap_count_data.last_n_heaps = n_heaps;
}
#endif //DYNAMIC_HEAP_COUNT
Expand Down Expand Up @@ -7209,11 +7236,14 @@ void gc_heap::gc_thread_function ()
// only that many threads will participate in the following GCs.
if (heap_number < new_n_heaps)
{
add_to_hc_history (hc_record_still_active);
dprintf (9999, ("h%d < %d participating (dec)", heap_number, new_n_heaps));
}
else
{
Interlocked::Increment (&dynamic_heap_count_data.idle_thread_count);
add_to_hc_history (hc_record_became_inactive);

dprintf (9999, ("GC thread %d wait_on_idle(%d < %d)(gc%Id), total idle %d", heap_number, old_n_heaps, new_n_heaps,
VolatileLoadWithoutBarrier (&settings.gc_index), VolatileLoadWithoutBarrier (&dynamic_heap_count_data.idle_thread_count)));
gc_idle_thread_event.Wait (INFINITE, FALSE);
Expand All @@ -7222,12 +7252,14 @@ void gc_heap::gc_thread_function ()
}
else
{
add_to_hc_history ((heap_number < old_n_heaps) ? hc_record_still_active : hc_record_became_active);
dprintf (9999, ("h%d < %d participating (inc)", heap_number, new_n_heaps));
}
}
else
{
Interlocked::Increment (&dynamic_heap_count_data.idle_thread_count);
add_to_hc_history (hc_record_inactive_waiting);
dprintf (9999, ("GC thread %d wait_on_idle(< max %d)(gc%Id), total idle %d", heap_number, num_threads_to_wake,
VolatileLoadWithoutBarrier (&settings.gc_index), VolatileLoadWithoutBarrier (&dynamic_heap_count_data.idle_thread_count)));
gc_idle_thread_event.Wait (INFINITE, FALSE);
Expand Down Expand Up @@ -15046,6 +15078,14 @@ gc_heap::init_gc_heap (int h_number)
gc_done_event_set = false;

#ifdef DYNAMIC_HEAP_COUNT
hchist_index_per_heap = 0;
memset (hchist_per_heap, 0, sizeof (hchist_per_heap));

#ifdef BACKGROUND_GC
bgc_hchist_index_per_heap = 0;
memset (bgc_hchist_per_heap, 0, sizeof (bgc_hchist_per_heap));
#endif //BACKGROUND_GC

if (h_number != 0)
{
if (!gc_idle_thread_event.CreateAutoEventNoThrow (FALSE))
Expand Down Expand Up @@ -24371,10 +24411,31 @@ void gc_heap::garbage_collect (int n)
#ifdef MULTIPLE_HEAPS
if (heap_number == 0)
{
#ifdef DYNAMIC_HEAP_COUNT
size_t current_gc_index = VolatileLoadWithoutBarrier (&settings.gc_index);
if (!bgc_init_gc_index)
{
assert (!bgc_init_n_heaps);
bgc_init_gc_index = current_gc_index;
bgc_init_n_heaps = (short)n_heaps;
}
size_t saved_bgc_th_count_created = bgc_th_count_created;
size_t saved_bgc_th_count_created_th_existed = bgc_th_count_created_th_existed;
size_t saved_bgc_th_count_creation_failed = bgc_th_count_creation_failed;
#endif //DYNAMIC_HEAP_COUNT

for (int i = 0; i < n_heaps; i++)
{
prepare_bgc_thread (g_heaps[i]);
}

#ifdef DYNAMIC_HEAP_COUNT
add_to_bgc_th_creation_history (current_gc_index,
(bgc_th_count_created - saved_bgc_th_count_created),
(bgc_th_count_created_th_existed - saved_bgc_th_count_created_th_existed),
(bgc_th_count_creation_failed - saved_bgc_th_count_creation_failed));
#endif //DYNAMIC_HEAP_COUNT

dprintf (2, ("setting bgc_threads_sync_event"));
bgc_threads_sync_event.Set();
}
Expand Down Expand Up @@ -25891,6 +25952,8 @@ void gc_heap::check_heap_count ()
if (gc_heap::background_running_p())
{
// background GC is running - reset the new heap count
add_to_hc_history (hc_record_check_cancelled_bgc);
hc_change_cancelled_count_bgc++;
dynamic_heap_count_data.new_n_heaps = n_heaps;
dprintf (6666, ("can't change heap count! BGC in progress"));
}
Expand All @@ -25903,6 +25966,8 @@ void gc_heap::check_heap_count ()
if (!prepare_to_change_heap_count (dynamic_heap_count_data.new_n_heaps))
{
// we don't have sufficient resources - reset the new heap count
add_to_hc_history (hc_record_check_cancelled_prep);
hc_change_cancelled_count_prep++;
dynamic_heap_count_data.new_n_heaps = n_heaps;
}
}
Expand Down Expand Up @@ -26478,7 +26543,10 @@ bool gc_heap::change_heap_count (int new_n_heaps)

if (heap_number == 0)
{
add_to_hc_history (hc_record_change_done);
change_heap_count_time = GetHighPrecisionTimeStamp() - start_time;
total_change_heap_count_time += change_heap_count_time;
total_change_heap_count++;
dprintf (6666, ("changing HC took %I64dus", change_heap_count_time));
}

Expand Down Expand Up @@ -26595,6 +26663,52 @@ void gc_heap::process_datas_sample()
last_suspended_end_time = before_distribute_free_regions_time;
}

void gc_heap::add_to_hc_history_worker (hc_history* hist, int* current_index, hc_record_stage stage, const char* msg)
{
dprintf (6666, ("h%d ADDING %s HC hist to entry #%d, stage %d, gc index %Id, last %d, n %d, new %d",
heap_number, msg, *current_index, (int)stage, VolatileLoadWithoutBarrier (&settings.gc_index),
dynamic_heap_count_data.last_n_heaps, n_heaps, dynamic_heap_count_data.new_n_heaps));
hc_history* current_hist = &hist[*current_index];
current_hist->gc_index = VolatileLoadWithoutBarrier (&settings.gc_index);
current_hist->stage = (short)stage;
current_hist->last_n_heaps = (short)dynamic_heap_count_data.last_n_heaps;
current_hist->n_heaps = (short)n_heaps;
current_hist->new_n_heaps = (short)dynamic_heap_count_data.new_n_heaps;
current_hist->idle_thread_count = (short)dynamic_heap_count_data.idle_thread_count;
current_hist->gc_t_join_n_threads = (short)gc_t_join.get_num_threads();
current_hist->gc_t_join_join_lock = (short)gc_t_join.get_join_lock();
current_hist->gc_t_join_joined_p = (bool)gc_t_join.joined();
#ifdef BACKGROUND_GC
current_hist->bgc_t_join_n_threads = (short)bgc_t_join.get_num_threads();
current_hist->bgc_t_join_join_lock = (short)bgc_t_join.get_join_lock();
current_hist->bgc_t_join_joined_p = (bool)bgc_t_join.joined();
current_hist->concurrent_p = (bool)settings.concurrent;
current_hist->bgc_thread_running = (bool)bgc_thread_running;

#if defined(TARGET_AMD64) && defined(TARGET_WINDOWS) && !defined(_DEBUG)
int bgc_thread_os_id = 0;

if (bgc_thread)
{
bgc_thread_os_id = (int)(*(size_t*)((uint8_t*)bgc_thread + 0x130));
}

current_hist->bgc_thread_os_id = bgc_thread_os_id;
#endif //TARGET_AMD64 && TARGET_WINDOWS && !_DEBUG
#endif //BACKGROUND_GC

Maoni0 marked this conversation as resolved.
Show resolved Hide resolved
*current_index = (*current_index + 1) % max_hc_history_count;
}

void gc_heap::add_to_hc_history (hc_record_stage stage)
{
add_to_hc_history_worker (hchist_per_heap, &hchist_index_per_heap, stage, "GC");
}

void gc_heap::add_to_bgc_hc_history (hc_record_stage stage)
{
add_to_hc_history_worker (bgc_hchist_per_heap, &bgc_hchist_index_per_heap, stage, "BGC");
}
#endif //DYNAMIC_HEAP_COUNT
#endif //USE_REGIONS

Expand Down Expand Up @@ -39403,6 +39517,27 @@ void gc_heap::mark_absorb_new_alloc()
clear_gen0_bricks();
}

#ifdef DYNAMIC_HEAP_COUNT
void gc_heap::add_to_bgc_th_creation_history (size_t gc_index, size_t count_created,
size_t count_created_th_existed, size_t count_creation_failed)
{
if ((count_created != 0) || (count_created_th_existed != 0) || (count_creation_failed != 0))
{
dprintf (6666, ("ADDING to BGC th hist entry%d gc index %Id, created %d, %d th existed, %d failed",
bgc_th_creation_hist_index, gc_index, count_created, count_created_th_existed, count_creation_failed));

bgc_thread_creation_history* current_hist = &bgc_th_creation_hist[bgc_th_creation_hist_index];
current_hist->gc_index = gc_index;
current_hist->n_heaps = (short)n_heaps;
current_hist->count_created = (short)count_created;
current_hist->count_created_th_existed = (short)count_created_th_existed;
current_hist->count_creation_failed = (short)count_creation_failed;

bgc_th_creation_hist_index = (bgc_th_creation_hist_index + 1) % max_bgc_thread_creation_count;
}
}
#endif //DYNAMIC_HEAP_COUNT

BOOL gc_heap::prepare_bgc_thread(gc_heap* gh)
{
BOOL success = FALSE;
Expand All @@ -39412,10 +39547,28 @@ BOOL gc_heap::prepare_bgc_thread(gc_heap* gh)
if (!(gh->bgc_thread_running))
{
dprintf (2, ("GC thread not running"));
if ((gh->bgc_thread == 0) && create_bgc_thread(gh))
if (gh->bgc_thread == 0)
{
success = TRUE;
thread_created = TRUE;
if (create_bgc_thread(gh))
{
success = TRUE;
thread_created = TRUE;
#ifdef DYNAMIC_HEAP_COUNT
bgc_th_count_created++;
#endif //DYNAMIC_HEAP_COUNT
}
else
{
#ifdef DYNAMIC_HEAP_COUNT
bgc_th_count_creation_failed++;
#endif //DYNAMIC_HEAP_COUNT
}
}
else
{
#ifdef DYNAMIC_HEAP_COUNT
bgc_th_count_created_th_existed++;
#endif //DYNAMIC_HEAP_COUNT
}
}
else
Expand Down Expand Up @@ -39677,13 +39830,19 @@ void gc_heap::bgc_thread_function()
#ifdef DYNAMIC_HEAP_COUNT
if (n_heaps <= heap_number)
{
add_to_bgc_hc_history (hc_record_bgc_inactive);

// this is the case where we have more background GC threads than heaps
// - wait until we're told to continue...
dprintf (9999, ("BGC thread %d idle (%d heaps) (gc%Id)", heap_number, n_heaps, VolatileLoadWithoutBarrier (&settings.gc_index)));
bgc_idle_thread_event.Wait(INFINITE, FALSE);
dprintf (9999, ("BGC thread %d waking from idle (%d heaps) (gc%Id)", heap_number, n_heaps, VolatileLoadWithoutBarrier (&settings.gc_index)));
continue;
}
else
{
add_to_bgc_hc_history (hc_record_bgc_active);
}
#endif //DYNAMIC_HEAP_COUNT

gc1();
Expand Down Expand Up @@ -51559,6 +51718,8 @@ size_t gc_heap::get_gen0_min_size()
int n_heaps = 1;
#endif //SERVER_GC

llc_size = trueSize;

#ifdef DYNAMIC_HEAP_COUNT
if (dynamic_adaptation_mode == dynamic_adaptation_to_application_sizes)
{
Expand Down
Loading