Skip to content

Commit

Permalink
Migrate MonoJitMemoryManager::seq_points to simdhash
Browse files Browse the repository at this point in the history
  • Loading branch information
kg committed Apr 4, 2024
1 parent 8ca9f7a commit c57c075
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 36 deletions.
35 changes: 22 additions & 13 deletions src/mono/mono/component/debugger-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,29 +364,38 @@ typedef struct {
GPtrArray *methods;
GPtrArray *method_domains;
GPtrArray *method_seq_points;

MonoDomain *domain;
} CollectDomainData;

static void
collect_domain_bp_inner (gpointer key, gpointer value, gpointer user_data)
{
MonoMethod *m = (MonoMethod *)key;
MonoSeqPointInfo *seq_points = (MonoSeqPointInfo *)value;
CollectDomainData *ud = (CollectDomainData*)user_data;

if (!bp_matches_method (ud->bp, m))
return;

/* Save the info locally to simplify the code inside the domain lock */
g_ptr_array_add (ud->methods, m);
g_ptr_array_add (ud->method_domains, ud->domain);
g_ptr_array_add (ud->method_seq_points, seq_points);
}

static void
collect_domain_bp (gpointer key, gpointer value, gpointer user_data)
{
GHashTableIter iter;
MonoSeqPointInfo *seq_points;
MonoDomain *domain = (MonoDomain*)key;
CollectDomainData *ud = (CollectDomainData*)user_data;
MonoMethod *m;
ud->domain = (MonoDomain*)key;

// FIXME:
MonoJitMemoryManager *jit_mm = get_default_jit_mm ();
jit_mm_lock (jit_mm);
g_hash_table_iter_init (&iter, jit_mm->seq_points);
while (g_hash_table_iter_next (&iter, (void**)&m, (void**)&seq_points)) {
if (bp_matches_method (ud->bp, m)) {
/* Save the info locally to simplify the code inside the domain lock */
g_ptr_array_add (ud->methods, m);
g_ptr_array_add (ud->method_domains, domain);
g_ptr_array_add (ud->method_seq_points, seq_points);
}
}
// FIXME: This previously used an iterator instead of foreach, so introducing
// an iterator API to simdhash might make it faster.
dn_simdhash_ght_foreach (jit_mm->seq_points, collect_domain_bp_inner, ud);
jit_mm_unlock (jit_mm);
}

Expand Down
6 changes: 3 additions & 3 deletions src/mono/mono/mini/aot-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2480,7 +2480,7 @@ load_container_amodule (MonoAssemblyLoadContext *alc)

mono_loader_lock ();
// There might be several threads that passed the first check
// Adding another check to ensure single load of a container assembly due to race condition
// Adding another check to ensure single load of a container assembly due to race condition
if (!container_amodule) {
ERROR_DECL (error);

Expand Down Expand Up @@ -3396,8 +3396,8 @@ decode_exception_debug_info (MonoAotModule *amodule,
jit_mm_lock (jit_mm);
/* This could be set already since this function can be called more than once for the same method */
MonoSeqPointInfo *existing_seq_points = NULL;
if (!g_hash_table_lookup_extended (jit_mm->seq_points, method, NULL, (gpointer *)&existing_seq_points)) {
g_hash_table_insert (jit_mm->seq_points, method, seq_points);
if (!dn_simdhash_ght_try_get_value (jit_mm->seq_points, method, (void **)&existing_seq_points)) {
dn_simdhash_ght_try_add (jit_mm->seq_points, method, seq_points);
} else {
mono_seq_point_info_free (seq_points);
seq_points = existing_seq_points;
Expand Down
11 changes: 8 additions & 3 deletions src/mono/mono/mini/interp/transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -9385,9 +9385,14 @@ mono_interp_transform_method (InterpMethod *imethod, ThreadContext *context, Mon

// FIXME Publishing of seq points seems to be racy with tiereing. We can have both tiered and untiered method
// running at the same time. We could therefore get the optimized imethod seq points for the unoptimized method.
gpointer seq_points = g_hash_table_lookup (jit_mm->seq_points, imethod->method);
if (!seq_points || seq_points != imethod->jinfo->seq_points)
g_hash_table_replace (jit_mm->seq_points, imethod->method, imethod->jinfo->seq_points);
gpointer seq_points = NULL;
uint8_t match = dn_simdhash_ght_try_get_value (jit_mm->seq_points, imethod->method, (void **)&seq_points);
if (!seq_points || seq_points != imethod->jinfo->seq_points) {
if (match)
dn_simdhash_ght_try_replace (jit_mm->seq_points, imethod->method, imethod->jinfo->seq_points);
else
dn_simdhash_ght_try_add (jit_mm->seq_points, imethod->method, imethod->jinfo->seq_points);
}
}
jit_mm_unlock (jit_mm);

Expand Down
8 changes: 4 additions & 4 deletions src/mono/mono/mini/mini-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3027,7 +3027,7 @@ mono_jit_free_method (MonoMethod *method)
//seq_points are always on get_default_jit_mm
jit_mm = get_default_jit_mm ();
jit_mm_lock (jit_mm);
g_hash_table_remove (jit_mm->seq_points, method);
dn_simdhash_ght_try_remove (jit_mm->seq_points, method);
jit_mm_unlock (jit_mm);

jit_mm = jit_mm_for_method (method);
Expand All @@ -3043,7 +3043,7 @@ mono_jit_free_method (MonoMethod *method)
mono_conc_hashtable_remove (jit_mm->runtime_invoke_hash, method);
g_hash_table_remove (jit_mm->dynamic_code_hash, method);
g_hash_table_remove (jit_mm->jump_trampoline_hash, method);
g_hash_table_remove (jit_mm->seq_points, method);
dn_simdhash_ght_try_remove (jit_mm->seq_points, method);

g_hash_table_iter_init (&iter, jit_mm->jump_target_hash);
while (g_hash_table_iter_next (&iter, NULL, (void**)&jlist)) {
Expand Down Expand Up @@ -4380,7 +4380,7 @@ init_jit_mem_manager (MonoMemoryManager *mem_manager)
info->jump_target_hash = g_hash_table_new (NULL, NULL);
info->jit_trampoline_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
info->delegate_info_hash = g_hash_table_new (delegate_class_method_pair_hash, delegate_class_method_pair_equal);
info->seq_points = g_hash_table_new_full (mono_aligned_addr_hash, NULL, NULL, mono_seq_point_info_free);
info->seq_points = dn_simdhash_ght_new_full (mono_aligned_addr_hash, NULL, NULL, mono_seq_point_info_free, 0, NULL);
info->runtime_invoke_hash = mono_conc_hashtable_new_full (mono_aligned_addr_hash, NULL, NULL, runtime_invoke_info_free);
info->arch_seq_points = g_hash_table_new (mono_aligned_addr_hash, NULL);
mono_jit_code_hash_init (&info->jit_code_hash);
Expand Down Expand Up @@ -4454,7 +4454,7 @@ free_jit_mem_manager (MonoMemoryManager *mem_manager)
g_hash_table_destroy (info->mrgctx_hash);
g_hash_table_destroy (info->interp_method_pointer_hash);
mono_conc_hashtable_destroy (info->runtime_invoke_hash);
g_hash_table_destroy (info->seq_points);
dn_simdhash_free (info->seq_points);
g_hash_table_destroy (info->arch_seq_points);
if (info->agent_info)
mono_component_debugger ()->free_mem_manager (info);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/mini/mini-runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ typedef struct {
MonoConcurrentHashTable *runtime_invoke_hash;
/* Maps MonoMethod to a GPtrArray containing sequence point locations */
/* Protected by the domain lock */
GHashTable *seq_points;
dn_simdhash_ght_t *seq_points;
/* Debugger agent data */
gpointer agent_info;
/* Maps MonoMethod to an arch-specific structure */
Expand Down
12 changes: 6 additions & 6 deletions src/mono/mono/mini/seq-points.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ mono_save_seq_point_info (MonoCompile *cfg, MonoJitInfo *jinfo)
jit_mm_lock (jit_mm);
// FIXME: The lookup can fail if the method is JITted recursively though a type cctor
MonoSeqPointInfo *existing_seq_points = NULL;
if (!g_hash_table_lookup_extended (jit_mm->seq_points, cfg->method_to_register, NULL, (gpointer *)&existing_seq_points)) {
g_hash_table_insert (jit_mm->seq_points, cfg->method_to_register, cfg->seq_point_info);
if (!dn_simdhash_ght_try_get_value (jit_mm->seq_points, cfg->method_to_register, (void **)&existing_seq_points)) {
dn_simdhash_ght_try_add (jit_mm->seq_points, cfg->method_to_register, cfg->seq_point_info);
} else {
mono_seq_point_info_free (cfg->seq_point_info);
cfg->seq_point_info = existing_seq_points;
Expand All @@ -259,7 +259,7 @@ MonoSeqPointInfo*
mono_get_seq_points (MonoMethod *method)
{
ERROR_DECL (error);
MonoSeqPointInfo *seq_points;
MonoSeqPointInfo *seq_points = NULL;
MonoMethod *declaring_generic_method = NULL, *shared_method = NULL;
MonoJitMemoryManager *jit_mm;

Expand All @@ -272,12 +272,12 @@ mono_get_seq_points (MonoMethod *method)
// FIXME:
jit_mm = get_default_jit_mm ();
jit_mm_lock (jit_mm);
seq_points = (MonoSeqPointInfo *)g_hash_table_lookup (jit_mm->seq_points, method);
dn_simdhash_ght_try_get_value (jit_mm->seq_points, method, (void **)&seq_points);
if (!seq_points && method->is_inflated) {
/* generic sharing + aot */
seq_points = (MonoSeqPointInfo *)g_hash_table_lookup (jit_mm->seq_points, declaring_generic_method);
dn_simdhash_ght_try_get_value (jit_mm->seq_points, declaring_generic_method, (void **)&seq_points);
if (!seq_points)
seq_points = (MonoSeqPointInfo *)g_hash_table_lookup (jit_mm->seq_points, shared_method);
dn_simdhash_ght_try_get_value (jit_mm->seq_points, shared_method, (void **)&seq_points);
}
jit_mm_unlock (jit_mm);

Expand Down
10 changes: 5 additions & 5 deletions src/native/containers/dn-simdhash-ght-compatible.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct dn_simdhash_ght_data {
} dn_simdhash_ght_data;

static inline uint32_t
dn_simdhash_ght_hash (dn_simdhash_ght_data data, gconstpointer key)
dn_simdhash_ght_hash (dn_simdhash_ght_data data, gpointer key)
{
GHashFunc hash_func = data.hash_func;
if (hash_func)
Expand All @@ -34,7 +34,7 @@ dn_simdhash_ght_hash (dn_simdhash_ght_data data, gconstpointer key)
}

static inline gboolean
dn_simdhash_ght_equals (dn_simdhash_ght_data data, gconstpointer lhs, gconstpointer rhs)
dn_simdhash_ght_equals (dn_simdhash_ght_data data, gpointer lhs, gpointer rhs)
{
GEqualFunc equal_func = data.key_equal_func;
if (equal_func)
Expand All @@ -44,7 +44,7 @@ dn_simdhash_ght_equals (dn_simdhash_ght_data data, gconstpointer lhs, gconstpoin
}

static inline void
dn_simdhash_ght_removed (dn_simdhash_ght_data data, gconstpointer key, gpointer value)
dn_simdhash_ght_removed (dn_simdhash_ght_data data, gpointer key, gpointer value)
{
GDestroyNotify key_destroy_func = data.key_destroy_func,
value_destroy_func = data.value_destroy_func;
Expand All @@ -55,7 +55,7 @@ dn_simdhash_ght_removed (dn_simdhash_ght_data data, gconstpointer key, gpointer
}

static inline void
dn_simdhash_ght_replaced (dn_simdhash_ght_data data, gconstpointer key, gpointer old_value, gpointer new_value)
dn_simdhash_ght_replaced (dn_simdhash_ght_data data, gpointer key, gpointer old_value, gpointer new_value)
{
if (old_value == new_value)
return;
Expand All @@ -66,7 +66,7 @@ dn_simdhash_ght_replaced (dn_simdhash_ght_data data, gconstpointer key, gpointer
}

#define DN_SIMDHASH_T dn_simdhash_ght
#define DN_SIMDHASH_KEY_T gconstpointer
#define DN_SIMDHASH_KEY_T gpointer
#define DN_SIMDHASH_VALUE_T gpointer
#define DN_SIMDHASH_INSTANCE_DATA_T dn_simdhash_ght_data
#define DN_SIMDHASH_KEY_HASHER dn_simdhash_ght_hash
Expand Down
2 changes: 1 addition & 1 deletion src/native/containers/dn-simdhash-specializations.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef struct dn_simdhash_str_key dn_simdhash_str_key;
#undef DN_SIMDHASH_VALUE_T

#define DN_SIMDHASH_T dn_simdhash_ght
#define DN_SIMDHASH_KEY_T gconstpointer
#define DN_SIMDHASH_KEY_T gpointer
#define DN_SIMDHASH_VALUE_T gpointer
#define DN_SIMDHASH_NO_DEFAULT_NEW 1

Expand Down

0 comments on commit c57c075

Please sign in to comment.