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)
59 #include "gst_private.h"
69 read_tsc (gint64 * dst)
73 __asm__ __volatile__ ("rdtsc":"=A" (tsc));
83 * @dst: pointer to hold the result.
85 * Read a platform independent timer value that can be used in
89 gst_trace_read_tsc (gint64 * dst)
94 GstTrace *_gst_trace_default = NULL;
95 gint _gst_trace_on = 1;
99 * @filename: a filename
100 * @size: the max size of the file
102 * Create a ringbuffer of @size in the file with @filename to
103 * store trace results in.
105 * Returns: a new #GstTrace.
108 gst_trace_new (gchar * filename, gint size)
110 GstTrace *trace = g_malloc (sizeof (GstTrace));
112 g_return_val_if_fail (trace != NULL, NULL);
113 trace->filename = g_strdup (filename);
114 GST_DEBUG ("opening '%s'\n", trace->filename);
116 #define S_IWUSR S_IWRITE
119 #define S_IRUSR S_IREAD
122 open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
123 perror ("opening trace file");
124 g_return_val_if_fail (trace->fd > 0, NULL);
125 trace->buf = g_malloc (size * sizeof (GstTraceEntry));
126 g_return_val_if_fail (trace->buf != NULL, NULL);
127 trace->bufsize = size;
128 trace->bufoffset = 0;
135 * @trace: the #GstTrace to destroy
137 * Flush an close the previously allocated @trace.
140 gst_trace_destroy (GstTrace * trace)
142 g_return_if_fail (trace != NULL);
143 g_return_if_fail (trace->buf != NULL);
145 if (gst_trace_get_remaining (trace) > 0)
146 gst_trace_flush (trace);
154 * @trace: the #GstTrace to flush.
156 * Flush any pending trace entries in @trace to the trace file.
157 * @trace can be NULL in which case the default #GstTrace will be
161 gst_trace_flush (GstTrace * trace)
164 trace = _gst_trace_default;
169 g_return_if_fail (write (trace->fd, trace->buf,
170 trace->bufoffset * sizeof (GstTraceEntry)) != -1);
171 trace->bufoffset = 0;
175 * gst_trace_text_flush:
176 * @trace: the #GstTrace to flush.
178 * Flush any pending trace entries in @trace to the trace file,
179 * formatted as a text line with timestamp and sequence numbers.
180 * @trace can be NULL in which case the default #GstTrace will be
184 gst_trace_text_flush (GstTrace * trace)
188 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
192 trace = _gst_trace_default;
197 for (i = 0; i < trace->bufoffset; i++) {
198 g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
199 trace->buf[i].timestamp,
200 trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
201 g_return_if_fail (write (trace->fd, str, strlen (str)) != -1);
203 trace->bufoffset = 0;
208 * gst_trace_set_default:
209 * @trace: the #GstTrace to set as the default.
211 * Set the default #GstTrace to @trace.
214 gst_trace_set_default (GstTrace * trace)
216 g_return_if_fail (trace != NULL);
217 _gst_trace_default = trace;
221 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
223 GstTraceEntry *entry;
226 trace = _gst_trace_default;
231 entry = trace->buf + trace->bufoffset;
232 read_tsc (&(entry->timestamp));
233 entry->sequence = seq;
235 strncpy (entry->message, msg, 112);
238 gst_trace_flush (trace);
243 static GstAllocTraceFlags _gst_trace_flags = 0;
245 /* list of registered tracers */
246 static GList *_gst_alloc_tracers = NULL;
249 * gst_alloc_trace_available:
251 * Check if alloc tracing was commiled into the core
253 * Returns: TRUE if the core was compiled with alloc
257 gst_alloc_trace_available (void)
259 #ifdef GST_DISABLE_ALLOC_TRACE
267 * _gst_alloc_trace_register:
268 * @name: the name of the new alloc trace object.
270 * Register an get a handle to a GstAllocTrace object that
271 * can be used to trace memory allocations.
273 * Returns: A handle to a GstAllocTrace.
276 _gst_alloc_trace_register (const gchar * name)
278 GstAllocTrace *trace;
280 g_return_val_if_fail (name, NULL);
282 trace = g_new0 (GstAllocTrace, 1);
283 trace->name = g_strdup (name);
285 trace->mem_live = NULL;
286 trace->flags = _gst_trace_flags;
288 _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
294 * gst_alloc_trace_list:
296 * Get a list of all registered alloc trace objects.
298 * Returns: a GList of GstAllocTrace objects.
301 gst_alloc_trace_list (void)
303 return _gst_alloc_tracers;
307 * gst_alloc_trace_live_all:
309 * Get the total number of live registered alloc trace objects.
311 * Returns: the total number of live registered alloc trace objects.
314 gst_alloc_trace_live_all (void)
316 GList *walk = _gst_alloc_tracers;
320 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
324 walk = g_list_next (walk);
331 compare_func (GstAllocTrace * a, GstAllocTrace * b)
333 return strcmp (a->name, b->name);
337 gst_alloc_trace_list_sorted (void)
341 ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
342 (GCompareFunc) compare_func);
348 * gst_alloc_trace_print_all:
350 * Print the status of all registered alloc trace objects.
353 gst_alloc_trace_print_all (void)
357 orig = walk = gst_alloc_trace_list_sorted ();
360 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
362 gst_alloc_trace_print (trace);
364 walk = g_list_next (walk);
371 * gst_alloc_trace_print_live:
373 * Print the status of all registered alloc trace objects, ignoring those
374 * without live objects.
377 gst_alloc_trace_print_live (void)
381 orig = walk = gst_alloc_trace_list_sorted ();
384 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
387 gst_alloc_trace_print (trace);
389 walk = g_list_next (walk);
396 * gst_alloc_trace_set_flags_all:
397 * @flags: the options to enable
399 * Enable the specified options on all registered alloc trace
403 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
405 GList *walk = _gst_alloc_tracers;
408 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
410 GST_DEBUG ("set flags on %p\n", trace);
411 gst_alloc_trace_set_flags (trace, flags);
413 walk = g_list_next (walk);
415 _gst_trace_flags = flags;
419 * gst_alloc_trace_get:
420 * @name: the name of the alloc trace object
422 * Get the named alloc trace object.
424 * Returns: a GstAllocTrace with the given name or NULL when
425 * no alloc tracer was registered with that name.
428 gst_alloc_trace_get (const gchar * name)
430 GList *walk = _gst_alloc_tracers;
432 g_return_val_if_fail (name, NULL);
435 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
437 if (!strcmp (trace->name, name))
440 walk = g_list_next (walk);
446 * gst_alloc_trace_print:
447 * @trace: the GstAllocTrace to print
449 * Print the status of the given GstAllocTrace.
452 gst_alloc_trace_print (const GstAllocTrace * trace)
456 g_return_if_fail (trace != NULL);
458 if (trace->flags & GST_ALLOC_TRACE_LIVE) {
459 g_print ("%-22.22s : %d\n", trace->name, trace->live);
461 g_print ("%-22.22s : (no live count)\n", trace->name);
464 if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
465 mem_live = trace->mem_live;
468 g_print ("%-22.22s : %p\n", "", mem_live->data);
469 mem_live = mem_live->next;
475 * gst_alloc_trace_set_flags:
476 * @trace: the GstAllocTrace
477 * @flags: flags to set
479 * Enable the given features on the given GstAllocTrace object.
482 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
484 g_return_if_fail (trace != NULL);
486 trace->flags = flags;