2 * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
4 * gst-stats.c: statistics tracing front end
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
33 static GRegex *raw_log = NULL;
34 static GRegex *ansi_log = NULL;
36 /* global statistics */
37 static GHashTable *threads = NULL;
38 static GPtrArray *elements = NULL;
39 static GPtrArray *pads = NULL;
40 static guint64 num_buffers = 0, num_events = 0, num_messages = 0, num_queries =
42 static guint num_elements = 0, num_bins = 0, num_pads = 0, num_ghostpads = 0;
46 /* human readable pad name and details */
47 gchar *name, *type_name;
49 gboolean is_ghost_pad;
51 /* buffer statistics */
53 guint num_live, num_decode_only, num_discont, num_resync, num_corrupted,
54 num_marker, num_header, num_gap, num_droppable, num_delta;
55 guint min_size, max_size, avg_size;
56 /* first and last activity on the pad, expected next_ts */
57 GstClockTime first_ts, last_ts, next_ts;
58 /* in which thread does it operate */
66 /* human readable element name */
67 gchar *name, *type_name;
70 /* buffer statistics */
71 guint recv_buffers, sent_buffers;
72 guint64 recv_bytes, sent_bytes;
73 /* event, message statistics */
74 guint num_events, num_messages, num_queries;
75 /* first activity on the element */
76 GstClockTime first_ts, last_ts;
77 /* time spend in this element */
85 /* time spend in this thread */
93 free_element_stats (gpointer data)
95 g_slice_free (GstElementStats, data);
98 static inline GstElementStats *
99 get_element_stats (guint ix)
101 return (ix != G_MAXUINT) ? g_ptr_array_index (elements, ix) : NULL;
104 static inline GstPadStats *
105 get_pad_stats (guint ix)
107 return (ix != G_MAXUINT) ? g_ptr_array_index (pads, ix) : NULL;
111 free_pad_stats (gpointer data)
113 g_slice_free (GstPadStats, data);
117 new_pad_stats (GstStructure * s)
122 gboolean is_ghost_pad;
126 gst_structure_get (s,
127 "ix", G_TYPE_UINT, &ix,
128 "name", G_TYPE_STRING, &name,
129 "type", G_TYPE_STRING, &type,
130 "is-ghostpad", G_TYPE_BOOLEAN, &is_ghost_pad,
131 "pad-direction", GST_TYPE_PAD_DIRECTION, &dir,
132 "thread-id", G_TYPE_UINT, &thread_id, NULL);
134 stats = g_slice_new0 (GstPadStats);
139 stats->type_name = type;
141 stats->is_ghost_pad = is_ghost_pad;
143 stats->min_size = G_MAXUINT;
144 stats->first_ts = stats->last_ts = stats->next_ts = GST_CLOCK_TIME_NONE;
145 stats->thread_id = thread_id;
148 g_ptr_array_set_size (pads, ix + 1);
149 g_ptr_array_index (pads, ix) = stats;
152 GstThreadStats *thread_stats;
153 if (!(thread_stats = g_hash_table_lookup (threads,
154 GUINT_TO_POINTER (thread_id)))) {
155 thread_stats = g_slice_new0 (GstThreadStats);
156 g_hash_table_insert (threads, GUINT_TO_POINTER (thread_id), thread_stats);
162 new_element_stats (GstStructure * s)
164 GstElementStats *stats;
169 gst_structure_get (s,
170 "ix", G_TYPE_UINT, &ix,
171 "name", G_TYPE_STRING, &name,
172 "type", G_TYPE_STRING, &type, "is-bin", G_TYPE_BOOLEAN, &is_bin, NULL);
174 stats = g_slice_new0 (GstElementStats);
180 stats->type_name = type;
181 stats->is_bin = is_bin;
182 stats->first_ts = GST_CLOCK_TIME_NONE;
183 stats->parent_ix = G_MAXUINT;
185 if (elements->len <= ix)
186 g_ptr_array_set_size (elements, ix + 1);
187 g_ptr_array_index (elements, ix) = stats;
191 free_thread_stats (gpointer data)
193 g_slice_free (GstThreadStats, data);
197 do_pad_stats (GstPadStats * stats, guint elem_ix, guint size, guint64 ts,
198 guint64 buffer_ts, guint64 buffer_dur, GstBufferFlags buffer_flags)
203 if (stats->parent_ix == G_MAXUINT) {
204 stats->parent_ix = elem_ix;
208 avg_size = (((gulong) stats->avg_size * (gulong) stats->num_buffers) + size);
209 stats->num_buffers++;
210 stats->avg_size = (guint) (avg_size / stats->num_buffers);
211 if (size < stats->min_size)
212 stats->min_size = size;
213 else if (size > stats->max_size)
214 stats->max_size = size;
216 if (!GST_CLOCK_TIME_IS_VALID (stats->last_ts))
217 stats->first_ts = ts;
220 if (buffer_flags & GST_BUFFER_FLAG_LIVE)
222 if (buffer_flags & GST_BUFFER_FLAG_DECODE_ONLY)
223 stats->num_decode_only++;
224 if (buffer_flags & GST_BUFFER_FLAG_DISCONT)
225 stats->num_discont++;
226 if (buffer_flags & GST_BUFFER_FLAG_RESYNC)
228 if (buffer_flags & GST_BUFFER_FLAG_CORRUPTED)
229 stats->num_corrupted++;
230 if (buffer_flags & GST_BUFFER_FLAG_MARKER)
232 if (buffer_flags & GST_BUFFER_FLAG_HEADER)
234 if (buffer_flags & GST_BUFFER_FLAG_GAP)
236 if (buffer_flags & GST_BUFFER_FLAG_DROPPABLE)
237 stats->num_droppable++;
238 if (buffer_flags & GST_BUFFER_FLAG_DELTA_UNIT)
240 /* update timestamps */
241 if (GST_CLOCK_TIME_IS_VALID (buffer_ts) &&
242 GST_CLOCK_TIME_IS_VALID (buffer_dur)) {
243 stats->next_ts = buffer_ts + buffer_dur;
245 stats->next_ts = GST_CLOCK_TIME_NONE;
250 do_element_stats (GstElementStats * stats, GstElementStats * peer_stats,
251 guint size, guint64 ts)
253 stats->sent_buffers++;
254 peer_stats->recv_buffers++;
255 stats->sent_bytes += size;
256 peer_stats->recv_bytes += size;
258 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (stats->first_ts))) {
259 stats->first_ts = ts;
261 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (peer_stats->first_ts))) {
262 peer_stats->first_ts = ts + 1;
267 do_buffer_stats (GstStructure * s)
269 guint64 ts, buffer_ts, buffer_dur;
270 guint pad_ix, elem_ix, peer_elem_ix;
272 GstBufferFlags buffer_flags;
273 GstPadStats *pad_stats;
274 GstElementStats *elem_stats, *peer_elem_stats;
277 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
278 "pad-ix", G_TYPE_UINT, &pad_ix,
279 "elem-ix", G_TYPE_UINT, &elem_ix,
280 "peer-elem-ix", G_TYPE_UINT, &peer_elem_ix,
281 "buffer-size", G_TYPE_UINT, &size,
282 "buffer-ts", G_TYPE_UINT64, &buffer_ts,
283 "buffer-duration", G_TYPE_UINT64, &buffer_dur,
284 "buffer-flags", GST_TYPE_BUFFER_FLAGS, &buffer_flags, NULL);
285 if (!(pad_stats = get_pad_stats (pad_ix))) {
286 GST_WARNING ("no pad stats found for ix=%u", pad_ix);
289 if (!(elem_stats = get_element_stats (elem_ix))) {
290 GST_WARNING ("no element stats found for ix=%u", elem_ix);
293 if (!(peer_elem_stats = get_element_stats (peer_elem_ix))) {
294 GST_WARNING ("no element stats found for ix=%u", peer_elem_ix);
297 do_pad_stats (pad_stats, elem_ix, size, ts, buffer_ts, buffer_dur,
299 if (pad_stats->dir == GST_PAD_SRC) {
301 do_element_stats (elem_stats, peer_elem_stats, size, ts);
304 do_element_stats (peer_elem_stats, elem_stats, size, ts);
309 do_event_stats (GstStructure * s)
312 guint pad_ix, elem_ix;
313 GstPadStats *pad_stats;
314 GstElementStats *elem_stats;
317 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
318 "pad-ix", G_TYPE_UINT, &pad_ix, "elem-ix", G_TYPE_UINT, &elem_ix, NULL);
319 if (!(pad_stats = get_pad_stats (pad_ix))) {
320 GST_WARNING ("no pad stats found for ix=%u", pad_ix);
323 if (!(elem_stats = get_element_stats (elem_ix))) {
324 GST_WARNING ("no element stats found for ix=%u", elem_ix);
327 elem_stats->num_events++;
331 do_message_stats (GstStructure * s)
335 GstElementStats *elem_stats;
338 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
339 "elem-ix", G_TYPE_UINT, &elem_ix, NULL);
340 if (!(elem_stats = get_element_stats (elem_ix))) {
341 GST_WARNING ("no element stats found for ix=%u", elem_ix);
344 elem_stats->num_messages++;
348 do_query_stats (GstStructure * s)
352 GstElementStats *elem_stats;
355 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
356 "elem-ix", G_TYPE_UINT, &elem_ix, NULL);
357 if (!(elem_stats = get_element_stats (elem_ix))) {
358 GST_WARNING ("no element stats found for ix=%u", elem_ix);
361 elem_stats->num_queries++;
367 print_pad_stats (gpointer value, gpointer user_data)
369 GstPadStats *stats = (GstPadStats *) value;
371 if (stats->thread_id == GPOINTER_TO_UINT (user_data)) {
372 /* there seem to be some temporary pads */
373 if (stats->num_buffers) {
374 GstClockTimeDiff running =
375 GST_CLOCK_DIFF (stats->first_ts, stats->last_ts);
376 GstElementStats *elem_stats = get_element_stats (stats->parent_ix);
377 gchar fullname[30 + 1];
379 g_snprintf (fullname, 30, "%s.%s", elem_stats->name, stats->name);
382 (" %c %-30.30s: buffers %7u (live %5u,dec %5u,dis %5u,res %5u,"
383 "cor %5u,mar %5u,hdr %5u,gap %5u,drop %5u,dlt %5u),",
384 (stats->dir == GST_PAD_SRC) ? '>' : '<', fullname, stats->num_buffers,
385 stats->num_live, stats->num_decode_only, stats->num_discont,
386 stats->num_resync, stats->num_corrupted, stats->num_marker,
387 stats->num_header, stats->num_gap, stats->num_droppable,
389 if (stats->min_size == stats->max_size) {
390 printf (" size (min/avg/max) ......./%7u/.......,", stats->avg_size);
392 printf (" size (min/avg/max) %7u/%7u/%7u,",
393 stats->min_size, stats->avg_size, stats->max_size);
395 printf (" time %" GST_TIME_FORMAT ","
397 GST_TIME_ARGS (running),
398 ((gdouble) (stats->num_buffers * stats->avg_size) * GST_SECOND) /
399 ((gdouble) running));
405 print_thread_stats (gpointer key, gpointer value, gpointer user_data)
407 GSList *list = user_data;
409 GstThreadStats *stats = (GstThreadStats *) value;
414 (guint) gst_util_uint64_scale (stats->treal, G_GINT64_CONSTANT (100),
419 /* cpuload for the process, scaled by the timeslice of this thread */
420 //cpuload = (guint) gst_util_uint64_scale (tusersys, G_GINT64_CONSTANT(100) * stats->treal, last_ts * last_ts);
422 total = gst_util_uint64_scale (tusersys, G_GINT64_CONSTANT (100), last_ts);
423 cpuload = gst_util_uint64_scale (total, stats->treal, last_ts);
426 printf ("Thread %p Statistics:\n", key);
427 printf (" Time: %" GST_TIME_FORMAT ", %u %%\n", GST_TIME_ARGS (stats->treal),
428 (guint) time_percent);
429 printf (" Avg/Max CPU load: %u %%, %u %%\n", cpuload, stats->max_cpuload);
431 /* FIXME: would be nice to skip, if there are no pads for that thread
432 * (e.g. a pipeline), we would need to pass as struct to
433 * g_hash_table_foreach::user_data
435 puts (" Pad Statistics:");
436 g_slist_foreach (list, print_pad_stats, key);
440 print_element_stats (gpointer value, gpointer user_data)
442 GstElementStats *stats = (GstElementStats *) value;
444 /* skip temporary elements */
445 if (stats->first_ts != GST_CLOCK_TIME_NONE) {
446 //GstClockTimeDiff running = GST_CLOCK_DIFF (stats->first_ts, stats->last_ts);
447 gchar fullname[45 + 1];
449 g_snprintf (fullname, 45, "%s:%s", stats->type_name, stats->name);
451 printf (" %-45s:", fullname);
452 if (stats->recv_buffers)
453 printf (" buffers in/out %7u", stats->recv_buffers);
455 printf (" buffers in/out %7s", "-");
456 if (stats->sent_buffers)
457 printf ("/%7u", stats->sent_buffers);
459 printf ("/%7s", "-");
460 if (stats->recv_bytes)
461 printf (" bytes in/out %12" G_GUINT64_FORMAT, stats->recv_bytes);
463 printf (" bytes in/out %12s", "-");
464 if (stats->sent_bytes)
465 printf ("/%12" G_GUINT64_FORMAT, stats->sent_bytes);
467 printf ("/%12s", "-");
468 printf (" first activity %" GST_TIME_FORMAT ", " " ev/msg/qry sent %5u/%5u/%5u" /*", "
469 " cpu usage %u %%" */
470 "\n", GST_TIME_ARGS (stats->first_ts), stats->num_events, stats->num_messages, stats->num_queries /*,
471 (guint)(100*stats->treal/last_ts) */ );
476 accum_element_stats (gpointer value, gpointer user_data)
478 GstElementStats *stats = (GstElementStats *) value;
480 if (stats->parent_ix != G_MAXUINT) {
481 GstElementStats *parent_stats = get_element_stats (stats->parent_ix);
483 parent_stats->num_events += stats->num_events;
484 parent_stats->num_messages += stats->num_messages;
485 parent_stats->num_queries += stats->num_queries;
486 if (!GST_CLOCK_TIME_IS_VALID (parent_stats->first_ts)) {
487 parent_stats->first_ts = stats->first_ts;
488 } else if (GST_CLOCK_TIME_IS_VALID (stats->first_ts)) {
489 parent_stats->first_ts = MIN (parent_stats->first_ts, stats->first_ts);
491 if (!GST_CLOCK_TIME_IS_VALID (parent_stats->last_ts)) {
492 parent_stats->last_ts = stats->last_ts;
493 } else if (GST_CLOCK_TIME_IS_VALID (stats->last_ts)) {
494 parent_stats->last_ts = MAX (parent_stats->last_ts, stats->last_ts);
502 sort_pad_stats_by_first_activity (gconstpointer ps1, gconstpointer ps2)
504 GstPadStats *s1 = (GstPadStats *) ps1;
505 GstPadStats *s2 = (GstPadStats *) ps2;
507 gint order = GST_CLOCK_DIFF (s2->first_ts, s1->first_ts);
510 order = s1->dir - s2->dir;
516 sort_pad_stats (gpointer value, gpointer user_data)
518 GSList **list = user_data;
521 g_slist_insert_sorted (*list, value, sort_pad_stats_by_first_activity);
525 sort_element_stats_by_first_activity (gconstpointer es1, gconstpointer es2)
527 return (GST_CLOCK_DIFF (((GstElementStats *) es2)->first_ts,
528 ((GstElementStats *) es1)->first_ts));
532 sort_bin_stats (gpointer value, gpointer user_data)
534 if (((GstElementStats *) value)->is_bin) {
535 GSList **list = user_data;
538 g_slist_insert_sorted (*list, value,
539 sort_element_stats_by_first_activity);
544 sort_element_stats (gpointer value, gpointer user_data)
546 if (!(((GstElementStats *) value)->is_bin)) {
547 GSList **list = user_data;
550 g_slist_insert_sorted (*list, value,
551 sort_element_stats_by_first_activity);
556 check_bin_parent (gpointer key, gpointer value, gpointer user_data)
558 GstElementStats *stats = (GstElementStats *) value;
560 return (stats->parent_ix == GPOINTER_TO_UINT (user_data));
564 process_leaf_bins (gpointer key, gpointer value, gpointer user_data)
566 GHashTable *accum_bins = user_data;
568 /* if we find no bin that has this bin as a parent ... */
569 if (!g_hash_table_find (accum_bins, check_bin_parent, key)) {
570 /* accumulate stats to the parent and remove */
571 accum_element_stats (value, NULL);
582 /* compile the parser regexps */
583 /* 0:00:00.004925027 31586 0x1c5c600 DEBUG GST_REGISTRY gstregistry.c:463:gst_registry_add_plugin:<registry0> adding plugin 0x1c79160 for filename "/usr/lib/gstreamer-1.0/libgstxxx.so" */
584 raw_log = g_regex_new (
595 /* 6: file:line:func: */
596 "([^:]+:[0-9]+:[^:]+:)"
597 /* 7: (obj)? log-text */
598 "(.*)$", 0, 0, NULL);
600 GST_WARNING ("failed to compile the 'raw' parser");
604 ansi_log = g_regex_new (
608 "\\\e\\\[[0-9;]+m *([0-9]+)\\\e\\\[00m +"
612 "(?:\\\e\\\[[0-9;]+m)?([A-Z]+) +\\\e\\\[00m +"
614 "\\\e\\\[[0-9;]+m +([a-zA-Z_-]+) +"
615 /* 6: file:line:func: */
616 "([^:]+:[0-9]+:[^:]+:)(?:\\\e\\\[00m)?"
617 /* 7: (obj)? log-text */
618 "(.*)$", 0, 0, NULL);
620 GST_WARNING ("failed to compile the 'ansi' parser");
624 elements = g_ptr_array_new_with_free_func (free_element_stats);
625 pads = g_ptr_array_new_with_free_func (free_pad_stats);
626 threads = g_hash_table_new_full (NULL, NULL, NULL, free_thread_stats);
635 g_ptr_array_free (pads, TRUE);
637 g_ptr_array_free (elements, TRUE);
639 g_hash_table_destroy (threads);
642 g_regex_unref (raw_log);
644 g_regex_unref (ansi_log);
650 guint num_threads = g_hash_table_size (threads);
652 /* print overall stats */
653 puts ("\nOverall Statistics:");
654 printf ("Number of Threads: %u\n", num_threads);
655 printf ("Number of Elements: %u\n", num_elements - num_bins);
656 printf ("Number of Bins: %u\n", num_bins);
657 printf ("Number of Pads: %u\n", num_pads - num_ghostpads);
658 printf ("Number of GhostPads: %u\n", num_ghostpads);
659 printf ("Number of Buffers passed: %" G_GUINT64_FORMAT "\n", num_buffers);
660 printf ("Number of Events sent: %" G_GUINT64_FORMAT "\n", num_events);
661 printf ("Number of Message sent: %" G_GUINT64_FORMAT "\n", num_messages);
662 printf ("Number of Queries sent: %" G_GUINT64_FORMAT "\n", num_queries);
669 g_ptr_array_foreach (pads, sort_pad_stats, &list);
670 g_hash_table_foreach (threads, print_thread_stats, list);
679 puts ("Element Statistics:");
680 /* sort by first_activity */
681 g_ptr_array_foreach (elements, sort_element_stats, &list);
682 /* attribute element stats to bins */
683 g_slist_foreach (list, accum_element_stats, NULL);
684 g_slist_foreach (list, print_element_stats, NULL);
693 GHashTable *accum_bins = g_hash_table_new_full (NULL, NULL, NULL, NULL);
695 puts ("Bin Statistics:");
696 /* attribute bin stats to parent-bins */
697 for (i = 0; i < num_elements; i++) {
698 GstElementStats *stats = g_ptr_array_index (elements, i);
700 g_hash_table_insert (accum_bins, GUINT_TO_POINTER (i), stats);
703 while (g_hash_table_size (accum_bins)) {
704 g_hash_table_foreach_remove (accum_bins, process_leaf_bins, accum_bins);
706 g_hash_table_destroy (accum_bins);
707 /* sort by first_activity */
708 g_ptr_array_foreach (elements, sort_bin_stats, &list);
709 g_slist_foreach (list, print_element_stats, NULL);
716 collect_stats (const gchar * filename)
720 if ((log = fopen (filename, "rt"))) {
724 if (fgets (line, 5000, log)) {
725 GMatchInfo *match_info;
731 if (strchr (line, 27)) {
733 GST_INFO ("format is 'ansi'");
736 GST_INFO ("format is 'raw'");
741 while (!feof (log)) {
742 if (fgets (line, 5000, log)) {
743 if (g_regex_match (parser, line, 0, &match_info)) {
744 /* filter by level */
745 level = g_match_info_fetch (match_info, 4);
746 if (!strcmp (level, "TRACE")) {
747 data = g_match_info_fetch (match_info, 7);
748 if ((s = gst_structure_from_string (data, NULL))) {
749 const gchar *name = gst_structure_get_name (s);
751 // TODO(ensonic): add a function for each name-id quark
752 // these function will do the actual stats tracking
753 if (!strcmp (name, "new-pad")) {
755 } else if (!strcmp (name, "new-element")) {
756 new_element_stats (s);
757 } else if (!strcmp (name, "buffer")) {
759 } else if (!strcmp (name, "event")) {
761 } else if (!strcmp (name, "message")) {
762 do_message_stats (s);
763 } else if (!strcmp (name, "query")) {
766 GST_WARNING ("unknown log entry: '%s'", data);
768 gst_structure_free (s);
770 GST_WARNING ("unknown log entry: '%s'", data);
775 GST_WARNING ("foreign log entry: %s:%d:'%s'", filename, lnr,
779 g_match_info_free (match_info);
784 // TODO(ensonic): run wc -L on the log file
785 fprintf (stderr, "line too long");
790 // TODO(ensonic): print stats
792 GST_WARNING ("empty log");
798 main (gint argc, gchar * argv[])
800 gchar **filenames = NULL;
804 GOptionEntry options[] = {
805 GST_TOOLS_GOPTION_VERSION,
806 // TODO(ensonic): add a summary flag, if set read the whole thing, print
807 // stats once, and exit
808 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL}
814 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
815 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
816 textdomain (GETTEXT_PACKAGE);
819 g_set_prgname ("gst-stats-" GST_API_VERSION);
821 ctx = g_option_context_new ("FILE");
822 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
823 g_option_context_add_group (ctx, gst_init_get_option_group ());
824 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
825 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
828 g_option_context_free (ctx);
830 gst_tools_print_version ();
832 if (filenames == NULL || *filenames == NULL) {
833 g_print ("Please give one filename to %s\n\n", g_get_prgname ());
836 num = g_strv_length (filenames);
837 if (num == 0 || num > 1) {
838 g_print ("Please give exactly one filename to %s (%d given).\n\n",
839 g_get_prgname (), num);
844 collect_stats (filenames[0]);
849 g_strfreev (filenames);