stats: always free log
[platform/upstream/gstreamer.git] / tools / gst-stats.c
index b2a6f32..f5efdda 100644 (file)
@@ -41,7 +41,8 @@ static guint64 num_buffers = 0, num_events = 0, num_messages = 0, num_queries =
     0;
 static guint num_elements = 0, num_bins = 0, num_pads = 0, num_ghostpads = 0;
 static GstClockTime last_ts = G_GUINT64_CONSTANT (0);
-static GstClockTime tproc = G_GUINT64_CONSTANT (0);
+static guint total_cpuload = 0;
+static gboolean have_cpuload = FALSE;
 
 typedef struct
 {
@@ -76,8 +77,6 @@ typedef struct
   guint num_events, num_messages, num_queries;
   /* first activity on the element */
   GstClockTime first_ts, last_ts;
-  /* time spend in this element */
-  GstClockTime tthread;
   /* hierarchy */
   guint parent_ix;
 } GstElementStats;
@@ -86,6 +85,7 @@ typedef struct
 {
   /* time spend in this thread */
   GstClockTime tthread;
+  guint cpuload;
 } GstThreadStats;
 
 /* stats helper */
@@ -121,6 +121,7 @@ get_thread_stats (guint id)
 
   if (G_UNLIKELY (!stats)) {
     stats = g_slice_new0 (GstThreadStats);
+    stats->tthread = GST_CLOCK_TIME_NONE;
     g_hash_table_insert (threads, GUINT_TO_POINTER (id), stats);
   }
   return stats;
@@ -277,7 +278,7 @@ do_element_stats (GstElementStats * stats, GstElementStats * peer_stats,
 static void
 do_buffer_stats (GstStructure * s)
 {
-  guint64 ts, buffer_ts, buffer_dur;
+  guint64 ts, buffer_pts, buffer_dur;
   guint pad_ix, elem_ix, peer_elem_ix;
   guint size;
   GstBufferFlags buffer_flags;
@@ -290,7 +291,7 @@ do_buffer_stats (GstStructure * s)
       "elem-ix", G_TYPE_UINT, &elem_ix,
       "peer-elem-ix", G_TYPE_UINT, &peer_elem_ix,
       "buffer-size", G_TYPE_UINT, &size,
-      "buffer-ts", G_TYPE_UINT64, &buffer_ts,
+      "buffer-pts", G_TYPE_UINT64, &buffer_pts,
       "buffer-duration", G_TYPE_UINT64, &buffer_dur,
       "buffer-flags", GST_TYPE_BUFFER_FLAGS, &buffer_flags, NULL);
   last_ts = MAX (last_ts, ts);
@@ -306,7 +307,7 @@ do_buffer_stats (GstStructure * s)
     GST_WARNING ("no element stats found for ix=%u", peer_elem_ix);
     return;
   }
-  do_pad_stats (pad_stats, elem_ix, size, ts, buffer_ts, buffer_dur,
+  do_pad_stats (pad_stats, elem_ix, size, ts, buffer_pts, buffer_dur,
       buffer_flags);
   if (pad_stats->dir == GST_PAD_SRC) {
     /* push */
@@ -378,21 +379,33 @@ do_query_stats (GstStructure * s)
 }
 
 static void
-do_rusage_stats (GstStructure * s)
+do_thread_rusage_stats (GstStructure * s)
 {
   guint64 ts, tthread;
-  guint thread_id;
+  guint thread_id, cpuload;
   GstThreadStats *thread_stats;
 
   gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
       "thread-id", G_TYPE_UINT, &thread_id,
-      "thread-time", G_TYPE_UINT64, &tthread,
-      "proc-time", G_TYPE_UINT64, &tproc, NULL);
+      "average-cpuload", G_TYPE_UINT, &cpuload, "time", G_TYPE_UINT64, &tthread,
+      NULL);
   thread_stats = get_thread_stats (thread_id);
+  thread_stats->cpuload = cpuload;
   thread_stats->tthread = tthread;
   last_ts = MAX (last_ts, ts);
 }
 
+static void
+do_proc_rusage_stats (GstStructure * s)
+{
+  guint64 ts;
+
+  gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
+      "average-cpuload", G_TYPE_UINT, &total_cpuload, NULL);
+  last_ts = MAX (last_ts, ts);
+  have_cpuload = TRUE;
+}
+
 /* reporting */
 
 static gint
@@ -451,19 +464,16 @@ print_thread_stats (gpointer key, gpointer value, gpointer user_data)
   GSList *list = user_data;
   GSList *node = g_slist_find_custom (list, key, find_pad_stats_for_thread);
   GstThreadStats *stats = (GstThreadStats *) value;
-  guint cpuload;
 
   /* skip stats if there are no pads for that thread (e.g. a pipeline) */
   if (!node)
     return;
 
-  cpuload =
-      (guint) gst_util_uint64_scale (stats->tthread, G_GINT64_CONSTANT (100),
-      last_ts);
-
   printf ("Thread %p Statistics:\n", key);
-  printf ("  Time: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (stats->tthread));
-  printf ("  Avg CPU load: %u %%\n", cpuload);
+  if (GST_CLOCK_TIME_IS_VALID (stats->tthread)) {
+    printf ("  Time: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (stats->tthread));
+    printf ("  Avg CPU load: %4.1f %%\n", (gfloat) stats->cpuload / 10.0);
+  }
 
   puts ("  Pad Statistics:");
   g_slist_foreach (node, print_pad_stats, key);
@@ -679,7 +689,6 @@ static void
 print_stats (void)
 {
   guint num_threads = g_hash_table_size (threads);
-  guint64 total_cpuload = 0;
 
   /* print overall stats */
   puts ("\nOverall Statistics:");
@@ -693,10 +702,8 @@ print_stats (void)
   printf ("Number of Message sent: %" G_GUINT64_FORMAT "\n", num_messages);
   printf ("Number of Queries sent: %" G_GUINT64_FORMAT "\n", num_queries);
   printf ("Time: %" GST_TIME_FORMAT "\n", GST_TIME_ARGS (last_ts));
-  if (tproc) {
-    total_cpuload = gst_util_uint64_scale (tproc,
-        G_GINT64_CONSTANT (100), last_ts);
-    printf ("Avg CPU load: %u %%\n", (guint) total_cpuload);
+  if (have_cpuload) {
+    printf ("Avg CPU load: %4.1f %%\n", (gfloat) total_cpuload / 10.0);
   }
   puts ("");
 
@@ -786,8 +793,6 @@ collect_stats (const gchar * filename)
               if ((s = gst_structure_from_string (data, NULL))) {
                 const gchar *name = gst_structure_get_name (s);
 
-                // TODO(ensonic): add a function for each name-id quark
-                // these function will do the actual stats tracking
                 if (!strcmp (name, "new-pad")) {
                   new_pad_stats (s);
                 } else if (!strcmp (name, "new-element")) {
@@ -800,10 +805,15 @@ collect_stats (const gchar * filename)
                   do_message_stats (s);
                 } else if (!strcmp (name, "query")) {
                   do_query_stats (s);
-                } else if (!strcmp (name, "rusage")) {
-                  do_rusage_stats (s);
+                } else if (!strcmp (name, "thread-rusage")) {
+                  do_thread_rusage_stats (s);
+                } else if (!strcmp (name, "proc-rusage")) {
+                  do_proc_rusage_stats (s);
                 } else {
-                  GST_WARNING ("unknown log entry: '%s'", data);
+                  // TODO(ensonic): parse the xxx.class log lines
+                  if (!g_str_has_suffix (data, ".class")) {
+                    GST_WARNING ("unknown log entry: '%s'", data);
+                  }
                 }
                 gst_structure_free (s);
               } else {
@@ -826,10 +836,10 @@ collect_stats (const gchar * filename)
           }
         }
       }
-      fclose (log);
     } else {
       GST_WARNING ("empty log");
     }
+    fclose (log);
   }
 }