tracers: rusage: use thread-local storage for per-thread stats

.. instead of looking things up by thread id from a GHashTable,
which also happens to have no locking around insertion/lookup.
This commit is contained in:
Tim-Philipp Müller 2020-01-26 00:56:44 +00:00
parent 04aad3f4cf
commit b8f5cb5a3a
2 changed files with 13 additions and 11 deletions

View file

@ -60,11 +60,15 @@ static GstTracerRecord *tr_proc, *tr_thread;
typedef struct typedef struct
{ {
/* time spend in this thread */ /* time spent in this thread */
GstClockTime tthread; GstClockTime tthread;
GstTraceValues *tvs_thread; GstTraceValues *tvs_thread;
} GstThreadStats; } GstThreadStats;
static void free_thread_stats (gpointer data);
static GPrivate thread_stats_key = G_PRIVATE_INIT (free_thread_stats);
/* data helper */ /* data helper */
static void static void
@ -140,12 +144,13 @@ update_trace_value (GstTraceValues * self, GstClockTime nts,
return ret; return ret;
} }
static void static void
free_thread_stats (gpointer data) free_thread_stats (gpointer data)
{ {
free_trace_values (((GstThreadStats *) data)->tvs_thread); GstThreadStats *stats = data;
g_slice_free (GstThreadStats, data);
free_trace_values (stats->tvs_thread);
g_free (stats);
} }
static void static void
@ -199,10 +204,10 @@ do_stats (GstTracer * obj, guint64 ts)
#endif #endif
#endif #endif
/* get stats record for current thread */ /* get stats record for current thread */
if (!(stats = g_hash_table_lookup (self->threads, thread_id))) { if (!(stats = g_private_get (&thread_stats_key))) {
stats = g_slice_new0 (GstThreadStats); stats = g_new0 (GstThreadStats, 1);
stats->tvs_thread = make_trace_values (GST_SECOND); stats->tvs_thread = make_trace_values (GST_SECOND);
g_hash_table_insert (self->threads, thread_id, stats); g_private_set (&thread_stats_key, stats);
} }
stats->tthread = tthread; stats->tthread = tthread;
@ -288,7 +293,6 @@ gst_rusage_tracer_finalize (GObject * obj)
{ {
GstRUsageTracer *self = GST_RUSAGE_TRACER (obj); GstRUsageTracer *self = GST_RUSAGE_TRACER (obj);
g_hash_table_destroy (self->threads);
free_trace_values (self->tvs_proc); free_trace_values (self->tvs_proc);
G_OBJECT_CLASS (parent_class)->finalize (obj); G_OBJECT_CLASS (parent_class)->finalize (obj);
@ -399,7 +403,6 @@ gst_rusage_tracer_init (GstRUsageTracer * self)
gst_tracing_register_hook (tracer, hooks[i], G_CALLBACK (do_stats)); gst_tracing_register_hook (tracer, hooks[i], G_CALLBACK (do_stats));
} }
self->threads = g_hash_table_new_full (NULL, NULL, NULL, free_thread_stats);
self->tvs_proc = make_trace_values (GST_SECOND); self->tvs_proc = make_trace_values (GST_SECOND);
self->main_thread_id = g_thread_self (); self->main_thread_id = g_thread_self ();

View file

@ -62,8 +62,7 @@ typedef struct
struct _GstRUsageTracer { struct _GstRUsageTracer {
GstTracer parent; GstTracer parent;
/*< private >*/ /*< private >*/
GHashTable *threads;
GstTraceValues *tvs_proc; GstTraceValues *tvs_proc;
/* for ts calibration */ /* for ts calibration */