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_readonly, num_preroll, num_discont, num_gap, num_delta;
54 guint min_size, max_size, avg_size;
55 /* first and last activity on the pad, expected next_ts */
56 GstClockTime first_ts, last_ts, next_ts;
57 /* in which thread does it operate */
65 /* human readable element name */
66 gchar *name, *type_name;
69 /* buffer statistics */
70 guint recv_buffers, sent_buffers;
71 guint64 recv_bytes, sent_bytes;
72 /* event, message statistics */
73 guint num_events, num_messages, num_queries;
74 /* first activity on the element */
75 GstClockTime first_ts, last_ts;
76 /* time spend in this element */
84 /* time spend in this thread */
92 free_element_stats (gpointer data)
94 g_slice_free (GstElementStats, data);
97 static inline GstElementStats *
98 get_element_stats (guint ix)
100 return (ix != G_MAXUINT) ? g_ptr_array_index (elements, ix) : NULL;
103 static inline GstPadStats *
104 get_pad_stats (guint ix)
106 return (ix != G_MAXUINT) ? g_ptr_array_index (pads, ix) : NULL;
110 free_pad_stats (gpointer data)
112 g_slice_free (GstPadStats, data);
116 new_pad_stats (GstStructure * s)
121 gboolean is_ghost_pad;
125 gst_structure_get (s,
126 "ix", G_TYPE_UINT, &ix,
127 "name", G_TYPE_STRING, &name,
128 "type", G_TYPE_STRING, &type,
129 "is-ghostpad", G_TYPE_BOOLEAN, &is_ghost_pad,
130 "pad-direction", GST_TYPE_PAD_DIRECTION, &dir,
131 "thread-id", G_TYPE_UINT, &thread_id, NULL);
133 stats = g_slice_new0 (GstPadStats);
138 stats->type_name = type;
140 stats->is_ghost_pad = is_ghost_pad;
142 stats->min_size = G_MAXUINT;
143 stats->first_ts = stats->last_ts = stats->next_ts = GST_CLOCK_TIME_NONE;
144 stats->thread_id = thread_id;
147 g_ptr_array_set_size (pads, ix + 1);
148 g_ptr_array_index (pads, ix) = stats;
151 GstThreadStats *thread_stats;
152 if (!(thread_stats = g_hash_table_lookup (threads,
153 GUINT_TO_POINTER (thread_id)))) {
154 thread_stats = g_slice_new0 (GstThreadStats);
155 g_hash_table_insert (threads, GUINT_TO_POINTER (thread_id), thread_stats);
161 new_element_stats (GstStructure * s)
163 GstElementStats *stats;
168 gst_structure_get (s,
169 "ix", G_TYPE_UINT, &ix,
170 "name", G_TYPE_STRING, &name,
171 "type", G_TYPE_STRING, &type, "is-bin", G_TYPE_BOOLEAN, &is_bin, NULL);
173 stats = g_slice_new0 (GstElementStats);
179 stats->type_name = type;
180 stats->is_bin = is_bin;
181 stats->first_ts = GST_CLOCK_TIME_NONE;
182 stats->parent_ix = G_MAXUINT;
184 if (elements->len <= ix)
185 g_ptr_array_set_size (elements, ix + 1);
186 g_ptr_array_index (elements, ix) = stats;
190 free_thread_stats (gpointer data)
192 g_slice_free (GstThreadStats, data);
196 do_pad_stats (GstPadStats * stats, guint elem_ix, guint size, guint64 ts,
197 guint64 buffer_ts, guint64 buffer_dur, GstBufferFlags buffer_flags)
202 if (stats->parent_ix == G_MAXUINT) {
203 stats->parent_ix = elem_ix;
207 avg_size = (((gulong) stats->avg_size * (gulong) stats->num_buffers) + size);
208 stats->num_buffers++;
209 stats->avg_size = (guint) (avg_size / stats->num_buffers);
210 if (size < stats->min_size)
211 stats->min_size = size;
212 else if (size > stats->max_size)
213 stats->max_size = size;
215 if (!GST_CLOCK_TIME_IS_VALID (stats->last_ts))
216 stats->first_ts = ts;
219 if (buffer_flags & GST_BUFFER_FLAG_GAP)
221 if (buffer_flags & GST_BUFFER_FLAG_DELTA_UNIT)
223 if (buffer_flags & GST_BUFFER_FLAG_DISCONT)
224 stats->num_discont++;
225 /* TODO(ensonic): there is a bunch of new flags in 1.0 */
226 /* update timestamps */
227 if (GST_CLOCK_TIME_IS_VALID (buffer_ts) &&
228 GST_CLOCK_TIME_IS_VALID (buffer_dur)) {
229 stats->next_ts = buffer_ts + buffer_dur;
231 stats->next_ts = GST_CLOCK_TIME_NONE;
236 do_element_stats (GstElementStats * stats, GstElementStats * peer_stats,
237 guint size, guint64 ts)
239 stats->sent_buffers++;
240 peer_stats->recv_buffers++;
241 stats->sent_bytes += size;
242 peer_stats->recv_bytes += size;
244 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (stats->first_ts))) {
245 stats->first_ts = ts;
247 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (peer_stats->first_ts))) {
248 peer_stats->first_ts = ts + 1;
253 do_buffer_stats (GstStructure * s)
255 guint64 ts, buffer_ts, buffer_dur;
256 guint pad_ix, elem_ix, peer_elem_ix;
258 GstBufferFlags buffer_flags;
259 GstPadStats *pad_stats;
260 GstElementStats *elem_stats, *peer_elem_stats;
263 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
264 "pad-ix", G_TYPE_UINT, &pad_ix,
265 "elem-ix", G_TYPE_UINT, &elem_ix,
266 "peer-elem-ix", G_TYPE_UINT, &peer_elem_ix,
267 "buffer-size", G_TYPE_UINT, &size,
268 "buffer-ts", G_TYPE_UINT64, &buffer_ts,
269 "buffer-duration", G_TYPE_UINT64, &buffer_dur,
270 "buffer-flags", GST_TYPE_BUFFER_FLAGS, &buffer_flags, NULL);
271 if (!(pad_stats = get_pad_stats (pad_ix))) {
272 GST_WARNING ("no pad stats found for ix=%u", pad_ix);
275 if (!(elem_stats = get_element_stats (elem_ix))) {
276 GST_WARNING ("no element stats found for ix=%u", elem_ix);
279 if (!(peer_elem_stats = get_element_stats (peer_elem_ix))) {
280 GST_WARNING ("no element stats found for ix=%u", peer_elem_ix);
283 do_pad_stats (pad_stats, elem_ix, size, ts, buffer_ts, buffer_dur,
285 if (pad_stats->dir == GST_PAD_SRC) {
287 do_element_stats (elem_stats, peer_elem_stats, size, ts);
290 do_element_stats (peer_elem_stats, elem_stats, size, ts);
295 do_event_stats (GstStructure * s)
298 guint pad_ix, elem_ix;
299 GstPadStats *pad_stats;
300 GstElementStats *elem_stats;
303 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
304 "pad-ix", G_TYPE_UINT, &pad_ix, "elem-ix", G_TYPE_UINT, &elem_ix, NULL);
305 if (!(pad_stats = get_pad_stats (pad_ix))) {
306 GST_WARNING ("no pad stats found for ix=%u", pad_ix);
309 if (!(elem_stats = get_element_stats (elem_ix))) {
310 GST_WARNING ("no element stats found for ix=%u", elem_ix);
313 elem_stats->num_events++;
317 do_message_stats (GstStructure * s)
321 GstElementStats *elem_stats;
324 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
325 "elem-ix", G_TYPE_UINT, &elem_ix, NULL);
326 if (!(elem_stats = get_element_stats (elem_ix))) {
327 GST_WARNING ("no element stats found for ix=%u", elem_ix);
330 elem_stats->num_messages++;
334 do_query_stats (GstStructure * s)
338 GstElementStats *elem_stats;
341 gst_structure_get (s, "ts", G_TYPE_UINT64, &ts,
342 "elem-ix", G_TYPE_UINT, &elem_ix, NULL);
343 if (!(elem_stats = get_element_stats (elem_ix))) {
344 GST_WARNING ("no element stats found for ix=%u", elem_ix);
347 elem_stats->num_queries++;
353 print_pad_stats (gpointer value, gpointer user_data)
355 GstPadStats *stats = (GstPadStats *) value;
357 if (stats->thread_id == GPOINTER_TO_UINT (user_data)) {
358 /* there seem to be some temporary pads */
359 if (stats->num_buffers) {
360 GstClockTimeDiff running =
361 GST_CLOCK_DIFF (stats->first_ts, stats->last_ts);
362 GstElementStats *elem_stats = get_element_stats (stats->parent_ix);
363 gchar fullname[30 + 1];
365 g_snprintf (fullname, 30, "%s.%s", elem_stats->name, stats->name);
368 (" %c %-30.30s: buffers %7u (ro %5u,pre %3u,dis %5u,gap %5u,dlt %5u),",
369 (stats->dir == GST_PAD_SRC) ? '>' : '<', fullname,
370 stats->num_buffers, stats->num_readonly, stats->num_preroll,
371 stats->num_discont, stats->num_gap, stats->num_delta);
372 if (stats->min_size == stats->max_size) {
373 printf (" size (min/avg/max) ......./%7u/.......,", stats->avg_size);
375 printf (" size (min/avg/max) %7u/%7u/%7u,",
376 stats->min_size, stats->avg_size, stats->max_size);
378 printf (" time %" GST_TIME_FORMAT ","
380 GST_TIME_ARGS (running),
381 ((gdouble) (stats->num_buffers * stats->avg_size) * GST_SECOND) /
382 ((gdouble) running));
388 print_thread_stats (gpointer key, gpointer value, gpointer user_data)
390 GSList *list = user_data;
392 GstThreadStats *stats = (GstThreadStats *) value;
397 (guint) gst_util_uint64_scale (stats->treal, G_GINT64_CONSTANT (100),
402 /* cpuload for the process, scaled by the timeslice of this thread */
403 //cpuload = (guint) gst_util_uint64_scale (tusersys, G_GINT64_CONSTANT(100) * stats->treal, last_ts * last_ts);
405 total = gst_util_uint64_scale (tusersys, G_GINT64_CONSTANT (100), last_ts);
406 cpuload = gst_util_uint64_scale (total, stats->treal, last_ts);
409 printf ("Thread %p Statistics:\n", key);
410 printf (" Time: %" GST_TIME_FORMAT ", %u %%\n", GST_TIME_ARGS (stats->treal),
411 (guint) time_percent);
412 printf (" Avg/Max CPU load: %u %%, %u %%\n", cpuload, stats->max_cpuload);
414 /* FIXME: would be nice to skip, if there are no pads for that thread
415 * (e.g. a pipeline), we would need to pass as struct to
416 * g_hash_table_foreach::user_data
418 puts (" Pad Statistics:");
419 g_slist_foreach (list, print_pad_stats, key);
423 print_element_stats (gpointer value, gpointer user_data)
425 GstElementStats *stats = (GstElementStats *) value;
427 /* skip temporary elements */
428 if (stats->first_ts != GST_CLOCK_TIME_NONE) {
429 //GstClockTimeDiff running = GST_CLOCK_DIFF (stats->first_ts, stats->last_ts);
430 gchar fullname[45 + 1];
432 g_snprintf (fullname, 45, "%s:%s", stats->type_name, stats->name);
434 printf (" %-45s:", fullname);
435 if (stats->recv_buffers)
436 printf (" buffers in/out %7u", stats->recv_buffers);
438 printf (" buffers in/out %7s", "-");
439 if (stats->sent_buffers)
440 printf ("/%7u", stats->sent_buffers);
442 printf ("/%7s", "-");
443 if (stats->recv_bytes)
444 printf (" bytes in/out %12" G_GUINT64_FORMAT, stats->recv_bytes);
446 printf (" bytes in/out %12s", "-");
447 if (stats->sent_bytes)
448 printf ("/%12" G_GUINT64_FORMAT, stats->sent_bytes);
450 printf ("/%12s", "-");
451 printf (" first activity %" GST_TIME_FORMAT ", " " ev/msg/qry sent %5u/%5u/%5u" /*", "
452 " cpu usage %u %%" */
453 "\n", GST_TIME_ARGS (stats->first_ts), stats->num_events, stats->num_messages, stats->num_queries /*,
454 (guint)(100*stats->treal/last_ts) */ );
459 accum_element_stats (gpointer value, gpointer user_data)
461 GstElementStats *stats = (GstElementStats *) value;
463 if (stats->parent_ix != G_MAXUINT) {
464 GstElementStats *parent_stats = get_element_stats (stats->parent_ix);
466 parent_stats->num_events += stats->num_events;
467 parent_stats->num_messages += stats->num_messages;
468 parent_stats->num_queries += stats->num_queries;
469 if (!GST_CLOCK_TIME_IS_VALID (parent_stats->first_ts)) {
470 parent_stats->first_ts = stats->first_ts;
471 } else if (GST_CLOCK_TIME_IS_VALID (stats->first_ts)) {
472 parent_stats->first_ts = MIN (parent_stats->first_ts, stats->first_ts);
474 if (!GST_CLOCK_TIME_IS_VALID (parent_stats->last_ts)) {
475 parent_stats->last_ts = stats->last_ts;
476 } else if (GST_CLOCK_TIME_IS_VALID (stats->last_ts)) {
477 parent_stats->last_ts = MAX (parent_stats->last_ts, stats->last_ts);
485 sort_pad_stats_by_first_activity (gconstpointer ps1, gconstpointer ps2)
487 GstPadStats *s1 = (GstPadStats *) ps1;
488 GstPadStats *s2 = (GstPadStats *) ps2;
490 gint order = GST_CLOCK_DIFF (s2->first_ts, s1->first_ts);
493 order = s1->dir - s2->dir;
499 sort_pad_stats (gpointer value, gpointer user_data)
501 GSList **list = user_data;
504 g_slist_insert_sorted (*list, value, sort_pad_stats_by_first_activity);
508 sort_element_stats_by_first_activity (gconstpointer es1, gconstpointer es2)
510 return (GST_CLOCK_DIFF (((GstElementStats *) es2)->first_ts,
511 ((GstElementStats *) es1)->first_ts));
515 sort_bin_stats (gpointer value, gpointer user_data)
517 if (((GstElementStats *) value)->is_bin) {
518 GSList **list = user_data;
521 g_slist_insert_sorted (*list, value,
522 sort_element_stats_by_first_activity);
527 sort_element_stats (gpointer value, gpointer user_data)
529 if (!(((GstElementStats *) value)->is_bin)) {
530 GSList **list = user_data;
533 g_slist_insert_sorted (*list, value,
534 sort_element_stats_by_first_activity);
539 check_bin_parent (gpointer key, gpointer value, gpointer user_data)
541 GstElementStats *stats = (GstElementStats *) value;
543 return (stats->parent_ix == GPOINTER_TO_UINT (user_data));
547 process_leaf_bins (gpointer key, gpointer value, gpointer user_data)
549 GHashTable *accum_bins = user_data;
551 /* if we find no bin that has this bin as a parent ... */
552 if (!g_hash_table_find (accum_bins, check_bin_parent, key)) {
553 /* accumulate stats to the parent and remove */
554 accum_element_stats (value, NULL);
565 /* compile the parser regexps */
566 /* 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" */
567 raw_log = g_regex_new (
578 /* 6: file:line:func: */
579 "([^:]+:[0-9]+:[^:]+:)"
580 /* 7: (obj)? log-text */
581 "(.*)$", 0, 0, NULL);
583 GST_WARNING ("failed to compile the 'raw' parser");
587 ansi_log = g_regex_new (
591 "\\\e\\\[[0-9;]+m *([0-9]+)\\\e\\\[00m +"
595 "(?:\\\e\\\[[0-9;]+m)?([A-Z]+) +\\\e\\\[00m +"
597 "\\\e\\\[[0-9;]+m +([a-zA-Z_-]+) +"
598 /* 6: file:line:func: */
599 "([^:]+:[0-9]+:[^:]+:)(?:\\\e\\\[00m)?"
600 /* 7: (obj)? log-text */
601 "(.*)$", 0, 0, NULL);
603 GST_WARNING ("failed to compile the 'ansi' parser");
607 elements = g_ptr_array_new_with_free_func (free_element_stats);
608 pads = g_ptr_array_new_with_free_func (free_pad_stats);
609 threads = g_hash_table_new_full (NULL, NULL, NULL, free_thread_stats);
618 g_ptr_array_free (pads, TRUE);
620 g_ptr_array_free (elements, TRUE);
622 g_hash_table_destroy (threads);
625 g_regex_unref (raw_log);
627 g_regex_unref (ansi_log);
633 guint num_threads = g_hash_table_size (threads);
635 /* print overall stats */
636 puts ("\nOverall Statistics:");
637 printf ("Number of Threads: %u\n", num_threads);
638 printf ("Number of Elements: %u\n", num_elements - num_bins);
639 printf ("Number of Bins: %u\n", num_bins);
640 printf ("Number of Pads: %u\n", num_pads - num_ghostpads);
641 printf ("Number of GhostPads: %u\n", num_ghostpads);
642 printf ("Number of Buffers passed: %" G_GUINT64_FORMAT "\n", num_buffers);
643 printf ("Number of Events sent: %" G_GUINT64_FORMAT "\n", num_events);
644 printf ("Number of Message sent: %" G_GUINT64_FORMAT "\n", num_messages);
645 printf ("Number of Queries sent: %" G_GUINT64_FORMAT "\n", num_queries);
652 g_ptr_array_foreach (pads, sort_pad_stats, &list);
653 g_hash_table_foreach (threads, print_thread_stats, list);
662 puts ("Element Statistics:");
663 /* sort by first_activity */
664 g_ptr_array_foreach (elements, sort_element_stats, &list);
665 /* attribute element stats to bins */
666 g_slist_foreach (list, accum_element_stats, NULL);
667 g_slist_foreach (list, print_element_stats, NULL);
676 GHashTable *accum_bins = g_hash_table_new_full (NULL, NULL, NULL, NULL);
678 puts ("Bin Statistics:");
679 /* attribute bin stats to parent-bins */
680 for (i = 0; i < num_elements; i++) {
681 GstElementStats *stats = g_ptr_array_index (elements, i);
683 g_hash_table_insert (accum_bins, GUINT_TO_POINTER (i), stats);
686 while (g_hash_table_size (accum_bins)) {
687 g_hash_table_foreach_remove (accum_bins, process_leaf_bins, accum_bins);
689 g_hash_table_destroy (accum_bins);
690 /* sort by first_activity */
691 g_ptr_array_foreach (elements, sort_bin_stats, &list);
692 g_slist_foreach (list, print_element_stats, NULL);
699 collect_stats (const gchar * filename)
703 if ((log = fopen (filename, "rt"))) {
707 if (fgets (line, 5000, log)) {
708 GMatchInfo *match_info;
714 if (strchr (line, 27)) {
716 GST_INFO ("format is 'ansi'");
719 GST_INFO ("format is 'raw'");
724 while (!feof (log)) {
725 if (fgets (line, 5000, log)) {
726 if (g_regex_match (parser, line, 0, &match_info)) {
727 /* filter by level */
728 level = g_match_info_fetch (match_info, 4);
729 if (!strcmp (level, "TRACE")) {
730 data = g_match_info_fetch (match_info, 7);
731 if ((s = gst_structure_from_string (data, NULL))) {
732 const gchar *name = gst_structure_get_name (s);
734 // TODO(ensonic): add a function for each name-id quark
735 // these function will do the actual stats tracking
736 if (!strcmp (name, "new-pad")) {
738 } else if (!strcmp (name, "new-element")) {
739 new_element_stats (s);
740 } else if (!strcmp (name, "buffer")) {
742 } else if (!strcmp (name, "event")) {
744 } else if (!strcmp (name, "message")) {
745 do_message_stats (s);
746 } else if (!strcmp (name, "query")) {
749 GST_WARNING ("unknown log entry: '%s'", data);
751 gst_structure_free (s);
753 GST_WARNING ("unknown log entry: '%s'", data);
758 GST_WARNING ("foreign log entry: %s:%d:'%s'", filename, lnr,
762 g_match_info_free (match_info);
767 // TODO(ensonic): run wc -L on the log file
768 fprintf (stderr, "line too long");
773 // TODO(ensonic): print stats
775 GST_WARNING ("empty log");
781 main (gint argc, gchar * argv[])
783 gchar **filenames = NULL;
787 GOptionEntry options[] = {
788 GST_TOOLS_GOPTION_VERSION,
789 // TODO(ensonic): add a summary flag, if set read the whole thing, print
790 // stats once, and exit
791 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL}
797 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
798 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
799 textdomain (GETTEXT_PACKAGE);
802 g_set_prgname ("gst-stats-" GST_API_VERSION);
804 ctx = g_option_context_new ("FILE");
805 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
806 g_option_context_add_group (ctx, gst_init_get_option_group ());
807 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
808 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
811 g_option_context_free (ctx);
813 gst_tools_print_version ();
815 if (filenames == NULL || *filenames == NULL) {
816 g_print ("Please give one filename to %s\n\n", g_get_prgname ());
819 num = g_strv_length (filenames);
820 if (num == 0 || num > 1) {
821 g_print ("Please give exactly one filename to %s (%d given).\n\n",
822 g_get_prgname (), num);
827 collect_stats (filenames[0]);
832 g_strfreev (filenames);