2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gsttrace.c: Tracing functions (deprecated)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
25 * @short_description: Tracing functionality
27 * Traces allows to track object allocation. They provide a instance counter per
28 * #GType. The counter is incremented for each object allocated and decremented
32 * <title>Tracing object instances</title>
34 * // trace un-freed object instances
35 * gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
36 * if (!gst_alloc_trace_available ()) {
37 * g_warning ("Trace not available (recompile with trace enabled).");
39 * gst_alloc_trace_print_live ();
40 * // do something here
41 * gst_alloc_trace_print_live ();
45 * Last reviewed on 2005-11-21 (0.9.5)
60 #if defined (_MSC_VER) && _MSC_VER >= 1400
64 #include "gst_private.h"
69 GStaticMutex _gst_trace_mutex = G_STATIC_MUTEX_INIT;
76 read_tsc (gint64 * dst)
78 #if defined(HAVE_RDTSC) && defined(__GNUC__)
80 __asm__ __volatile__ ("rdtsc":"=A" (tsc));
90 * @dst: (out) pointer to hold the result.
92 * Read a platform independent timer value that can be used in
96 gst_trace_read_tsc (gint64 * dst)
101 static GstTrace *_gst_trace_default = NULL;
102 gint _gst_trace_on = 1;
106 * @filename: a filename
107 * @size: the max size of the file
109 * Create a ringbuffer of @size in the file with @filename to
110 * store trace results in.
112 * Free-function: gst_trace_destroy
114 * Returns: (transfer full): a new #GstTrace.
117 gst_trace_new (const gchar * filename, gint size)
119 GstTrace *trace = g_slice_new (GstTrace);
121 g_return_val_if_fail (trace != NULL, NULL);
122 trace->filename = g_strdup (filename);
123 GST_DEBUG ("opening '%s'", trace->filename);
125 #define S_IWUSR S_IWRITE
128 #define S_IRUSR S_IREAD
131 open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
132 perror ("opening trace file");
133 g_return_val_if_fail (trace->fd > 0, NULL);
134 trace->buf = g_malloc (size * sizeof (GstTraceEntry));
135 g_return_val_if_fail (trace->buf != NULL, NULL);
136 trace->bufsize = size;
137 trace->bufoffset = 0;
144 * @trace: (in) (transfer full): the #GstTrace to destroy
146 * Flush an close the previously allocated @trace.
149 gst_trace_destroy (GstTrace * trace)
151 g_return_if_fail (trace != NULL);
152 g_return_if_fail (trace->buf != NULL);
154 if (gst_trace_get_remaining (trace) > 0)
155 gst_trace_flush (trace);
158 g_slice_free (GstTrace, trace);
163 * @trace: the #GstTrace to flush.
165 * Flush any pending trace entries in @trace to the trace file.
166 * @trace can be NULL in which case the default #GstTrace will be
170 gst_trace_flush (GstTrace * trace)
175 trace = _gst_trace_default;
180 buf_len = trace->bufoffset * sizeof (GstTraceEntry);
181 res = write (trace->fd, trace->buf, buf_len);
183 g_warning ("Failed to write trace: %s", g_strerror (errno));
185 } else if (res < buf_len) {
186 g_warning ("Failed to write trace: only wrote %d/%d bytes", res, buf_len);
189 trace->bufoffset = 0;
193 * gst_trace_text_flush:
194 * @trace: the #GstTrace to flush.
196 * Flush any pending trace entries in @trace to the trace file,
197 * formatted as a text line with timestamp and sequence numbers.
198 * @trace can be NULL in which case the default #GstTrace will be
202 gst_trace_text_flush (GstTrace * trace)
206 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
210 trace = _gst_trace_default;
215 for (i = 0; i < trace->bufoffset; i++) {
216 g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
217 trace->buf[i].timestamp,
218 trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
219 if (write (trace->fd, str, strlen (str)) < 0) {
220 g_warning ("Failed to write trace %d: %s", i, g_strerror (errno));
224 trace->bufoffset = 0;
229 * gst_trace_set_default:
230 * @trace: the #GstTrace to set as the default.
232 * Set the default #GstTrace to @trace.
235 gst_trace_set_default (GstTrace * trace)
237 g_return_if_fail (trace != NULL);
238 _gst_trace_default = trace;
242 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
244 GstTraceEntry *entry;
247 trace = _gst_trace_default;
252 entry = trace->buf + trace->bufoffset;
253 read_tsc (&(entry->timestamp));
254 entry->sequence = seq;
256 strncpy (entry->message, msg, 112);
257 entry->message[111] = '\0';
260 gst_trace_flush (trace);
265 static GstAllocTraceFlags _gst_trace_flags = 0;
267 /* list of registered tracers */
268 static GList *_gst_alloc_tracers = NULL;
271 * gst_alloc_trace_available:
273 * Check if alloc tracing was compiled into the core
275 * Returns: TRUE if the core was compiled with alloc
279 gst_alloc_trace_available (void)
281 #ifdef GST_DISABLE_ALLOC_TRACE
289 * _gst_alloc_trace_register:
290 * @name: the name of the new alloc trace object.
292 * Register an get a handle to a GstAllocTrace object that
293 * can be used to trace memory allocations.
295 * Returns: A handle to a GstAllocTrace.
298 _gst_alloc_trace_register (const gchar * name)
300 GstAllocTrace *trace;
302 g_return_val_if_fail (name, NULL);
304 trace = g_slice_new (GstAllocTrace);
305 trace->name = g_strdup (name);
307 trace->mem_live = NULL;
308 trace->flags = _gst_trace_flags;
310 _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
316 * gst_alloc_trace_list:
318 * Get a list of all registered alloc trace objects.
320 * Returns: a GList of GstAllocTrace objects.
323 gst_alloc_trace_list (void)
325 return _gst_alloc_tracers;
329 * gst_alloc_trace_live_all:
331 * Get the total number of live registered alloc trace objects.
333 * Returns: the total number of live registered alloc trace objects.
336 gst_alloc_trace_live_all (void)
338 GList *walk = _gst_alloc_tracers;
342 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
346 walk = g_list_next (walk);
353 compare_func (GstAllocTrace * a, GstAllocTrace * b)
355 return strcmp (a->name, b->name);
359 gst_alloc_trace_list_sorted (void)
363 ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
364 (GCompareFunc) compare_func);
370 * gst_alloc_trace_print_all:
372 * Print the status of all registered alloc trace objects.
375 gst_alloc_trace_print_all (void)
379 orig = walk = gst_alloc_trace_list_sorted ();
382 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
384 gst_alloc_trace_print (trace);
386 walk = g_list_next (walk);
393 * gst_alloc_trace_print_live:
395 * Print the status of all registered alloc trace objects, ignoring those
396 * without live objects.
399 gst_alloc_trace_print_live (void)
403 orig = walk = gst_alloc_trace_list_sorted ();
406 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
409 gst_alloc_trace_print (trace);
411 walk = g_list_next (walk);
418 * gst_alloc_trace_set_flags_all:
419 * @flags: the options to enable
421 * Enable the specified options on all registered alloc trace
425 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
427 GList *walk = _gst_alloc_tracers;
430 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
432 GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
433 gst_alloc_trace_set_flags (trace, flags);
435 walk = g_list_next (walk);
437 _gst_trace_flags = flags;
441 * gst_alloc_trace_get:
442 * @name: the name of the alloc trace object
444 * Get the named alloc trace object.
446 * Returns: a GstAllocTrace with the given name or NULL when
447 * no alloc tracer was registered with that name.
450 gst_alloc_trace_get (const gchar * name)
452 GList *walk = _gst_alloc_tracers;
454 g_return_val_if_fail (name, NULL);
457 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
459 if (!strcmp (trace->name, name))
462 walk = g_list_next (walk);
468 * gst_alloc_trace_print:
469 * @trace: the GstAllocTrace to print
471 * Print the status of the given GstAllocTrace.
474 gst_alloc_trace_print (const GstAllocTrace * trace)
478 g_return_if_fail (trace != NULL);
480 if (trace->flags & GST_ALLOC_TRACE_LIVE) {
481 g_print ("%-22.22s : %d\n", trace->name, trace->live);
483 g_print ("%-22.22s : (no live count)\n", trace->name);
486 if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
487 mem_live = trace->mem_live;
490 gpointer data = mem_live->data;
492 if (G_IS_OBJECT (data)) {
493 g_print ("%-22.22s : %p\n", g_type_name (G_OBJECT_TYPE (data)), data);
495 g_print ("%-22.22s : %p\n", "", data);
497 mem_live = mem_live->next;
503 * gst_alloc_trace_set_flags:
504 * @trace: the GstAllocTrace
505 * @flags: flags to set
507 * Enable the given features on the given GstAllocTrace object.
510 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
512 g_return_if_fail (trace != NULL);
514 trace->flags = flags;