tracers: rusage: use thread-local storage for per-thread stats
authorTim-Philipp Müller <tim@centricular.com>
Sun, 26 Jan 2020 00:56:44 +0000 (00:56 +0000)
committerTim-Philipp Müller <tim@centricular.com>
Fri, 28 Feb 2020 15:54:38 +0000 (15:54 +0000)
.. instead of looking things up by thread id from a GHashTable,
which also happens to have no locking around insertion/lookup.

plugins/tracers/gstrusage.c
plugins/tracers/gstrusage.h

index b91cb16..9976ddf 100644 (file)
@@ -60,11 +60,15 @@ static GstTracerRecord *tr_proc, *tr_thread;
 
 typedef struct
 {
-  /* time spend in this thread */
+  /* time spent in this thread */
   GstClockTime tthread;
   GstTraceValues *tvs_thread;
 } GstThreadStats;
 
+static void free_thread_stats (gpointer data);
+
+static GPrivate thread_stats_key = G_PRIVATE_INIT (free_thread_stats);
+
 /* data helper */
 
 static void
@@ -140,12 +144,13 @@ update_trace_value (GstTraceValues * self, GstClockTime nts,
   return ret;
 }
 
-
 static void
 free_thread_stats (gpointer data)
 {
-  free_trace_values (((GstThreadStats *) data)->tvs_thread);
-  g_slice_free (GstThreadStats, data);
+  GstThreadStats *stats = data;
+
+  free_trace_values (stats->tvs_thread);
+  g_free (stats);
 }
 
 static void
@@ -199,10 +204,10 @@ do_stats (GstTracer * obj, guint64 ts)
 #endif
 #endif
   /* get stats record for current thread */
-  if (!(stats = g_hash_table_lookup (self->threads, thread_id))) {
-    stats = g_slice_new0 (GstThreadStats);
+  if (!(stats = g_private_get (&thread_stats_key))) {
+    stats = g_new0 (GstThreadStats, 1);
     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;
 
@@ -288,7 +293,6 @@ gst_rusage_tracer_finalize (GObject * obj)
 {
   GstRUsageTracer *self = GST_RUSAGE_TRACER (obj);
 
-  g_hash_table_destroy (self->threads);
   free_trace_values (self->tvs_proc);
 
   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));
   }
 
-  self->threads = g_hash_table_new_full (NULL, NULL, NULL, free_thread_stats);
   self->tvs_proc = make_trace_values (GST_SECOND);
   self->main_thread_id = g_thread_self ();
 
index b5abbca..fe84aa6 100644 (file)
@@ -62,8 +62,7 @@ typedef struct
 struct _GstRUsageTracer {
   GstTracer     parent;
 
-  /*< private >*/        
-  GHashTable *threads;
+  /*< private >*/
   GstTraceValues *tvs_proc;
 
   /* for ts calibration */