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 #if defined (_MSC_VER) && _MSC_VER >= 1400
63 #include "gst_private.h"
68 GStaticMutex _gst_trace_mutex = G_STATIC_MUTEX_INIT;
75 read_tsc (gint64 * dst)
79 __asm__ __volatile__ ("rdtsc":"=A" (tsc));
89 * @dst: pointer to hold the result.
91 * Read a platform independent timer value that can be used in
95 gst_trace_read_tsc (gint64 * dst)
100 static GstTrace *_gst_trace_default = NULL;
101 gint _gst_trace_on = 1;
105 * @filename: a filename
106 * @size: the max size of the file
108 * Create a ringbuffer of @size in the file with @filename to
109 * store trace results in.
111 * Returns: a new #GstTrace.
114 gst_trace_new (gchar * filename, gint size)
116 GstTrace *trace = g_malloc (sizeof (GstTrace));
118 g_return_val_if_fail (trace != NULL, NULL);
119 trace->filename = g_strdup (filename);
120 GST_DEBUG ("opening '%s'", trace->filename);
122 #define S_IWUSR S_IWRITE
125 #define S_IRUSR S_IREAD
128 open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
129 perror ("opening trace file");
130 g_return_val_if_fail (trace->fd > 0, NULL);
131 trace->buf = g_malloc (size * sizeof (GstTraceEntry));
132 g_return_val_if_fail (trace->buf != NULL, NULL);
133 trace->bufsize = size;
134 trace->bufoffset = 0;
141 * @trace: the #GstTrace to destroy
143 * Flush an close the previously allocated @trace.
146 gst_trace_destroy (GstTrace * trace)
148 g_return_if_fail (trace != NULL);
149 g_return_if_fail (trace->buf != NULL);
151 if (gst_trace_get_remaining (trace) > 0)
152 gst_trace_flush (trace);
160 * @trace: the #GstTrace to flush.
162 * Flush any pending trace entries in @trace to the trace file.
163 * @trace can be NULL in which case the default #GstTrace will be
167 gst_trace_flush (GstTrace * trace)
170 trace = _gst_trace_default;
175 g_return_if_fail (write (trace->fd, trace->buf,
176 trace->bufoffset * sizeof (GstTraceEntry)) != -1);
177 trace->bufoffset = 0;
181 * gst_trace_text_flush:
182 * @trace: the #GstTrace to flush.
184 * Flush any pending trace entries in @trace to the trace file,
185 * formatted as a text line with timestamp and sequence numbers.
186 * @trace can be NULL in which case the default #GstTrace will be
190 gst_trace_text_flush (GstTrace * trace)
194 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
198 trace = _gst_trace_default;
203 for (i = 0; i < trace->bufoffset; i++) {
204 g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
205 trace->buf[i].timestamp,
206 trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
207 g_return_if_fail (write (trace->fd, str, strlen (str)) != -1);
209 trace->bufoffset = 0;
214 * gst_trace_set_default:
215 * @trace: the #GstTrace to set as the default.
217 * Set the default #GstTrace to @trace.
220 gst_trace_set_default (GstTrace * trace)
222 g_return_if_fail (trace != NULL);
223 _gst_trace_default = trace;
227 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
229 GstTraceEntry *entry;
232 trace = _gst_trace_default;
237 entry = trace->buf + trace->bufoffset;
238 read_tsc (&(entry->timestamp));
239 entry->sequence = seq;
241 strncpy (entry->message, msg, 112);
244 gst_trace_flush (trace);
249 static GstAllocTraceFlags _gst_trace_flags = 0;
251 /* list of registered tracers */
252 static GList *_gst_alloc_tracers = NULL;
255 * gst_alloc_trace_available:
257 * Check if alloc tracing was compiled into the core
259 * Returns: TRUE if the core was compiled with alloc
263 gst_alloc_trace_available (void)
265 #ifdef GST_DISABLE_ALLOC_TRACE
273 * _gst_alloc_trace_register:
274 * @name: the name of the new alloc trace object.
276 * Register an get a handle to a GstAllocTrace object that
277 * can be used to trace memory allocations.
279 * Returns: A handle to a GstAllocTrace.
282 _gst_alloc_trace_register (const gchar * name)
284 GstAllocTrace *trace;
286 g_return_val_if_fail (name, NULL);
288 trace = g_new0 (GstAllocTrace, 1);
289 trace->name = g_strdup (name);
291 trace->mem_live = NULL;
292 trace->flags = _gst_trace_flags;
294 _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
300 * gst_alloc_trace_list:
302 * Get a list of all registered alloc trace objects.
304 * Returns: a GList of GstAllocTrace objects.
307 gst_alloc_trace_list (void)
309 return _gst_alloc_tracers;
313 * gst_alloc_trace_live_all:
315 * Get the total number of live registered alloc trace objects.
317 * Returns: the total number of live registered alloc trace objects.
320 gst_alloc_trace_live_all (void)
322 GList *walk = _gst_alloc_tracers;
326 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
330 walk = g_list_next (walk);
337 compare_func (GstAllocTrace * a, GstAllocTrace * b)
339 return strcmp (a->name, b->name);
343 gst_alloc_trace_list_sorted (void)
347 ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
348 (GCompareFunc) compare_func);
354 * gst_alloc_trace_print_all:
356 * Print the status of all registered alloc trace objects.
359 gst_alloc_trace_print_all (void)
363 orig = walk = gst_alloc_trace_list_sorted ();
366 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
368 gst_alloc_trace_print (trace);
370 walk = g_list_next (walk);
377 * gst_alloc_trace_print_live:
379 * Print the status of all registered alloc trace objects, ignoring those
380 * without live objects.
383 gst_alloc_trace_print_live (void)
387 orig = walk = gst_alloc_trace_list_sorted ();
390 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
393 gst_alloc_trace_print (trace);
395 walk = g_list_next (walk);
402 * gst_alloc_trace_set_flags_all:
403 * @flags: the options to enable
405 * Enable the specified options on all registered alloc trace
409 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
411 GList *walk = _gst_alloc_tracers;
414 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
416 GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
417 gst_alloc_trace_set_flags (trace, flags);
419 walk = g_list_next (walk);
421 _gst_trace_flags = flags;
425 * gst_alloc_trace_get:
426 * @name: the name of the alloc trace object
428 * Get the named alloc trace object.
430 * Returns: a GstAllocTrace with the given name or NULL when
431 * no alloc tracer was registered with that name.
434 gst_alloc_trace_get (const gchar * name)
436 GList *walk = _gst_alloc_tracers;
438 g_return_val_if_fail (name, NULL);
441 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
443 if (!strcmp (trace->name, name))
446 walk = g_list_next (walk);
452 * gst_alloc_trace_print:
453 * @trace: the GstAllocTrace to print
455 * Print the status of the given GstAllocTrace.
458 gst_alloc_trace_print (const GstAllocTrace * trace)
462 g_return_if_fail (trace != NULL);
464 if (trace->flags & GST_ALLOC_TRACE_LIVE) {
465 g_print ("%-22.22s : %d\n", trace->name, trace->live);
467 g_print ("%-22.22s : (no live count)\n", trace->name);
470 if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
471 mem_live = trace->mem_live;
474 g_print ("%-22.22s : %p\n", "", mem_live->data);
475 mem_live = mem_live->next;
481 * gst_alloc_trace_set_flags:
482 * @trace: the GstAllocTrace
483 * @flags: flags to set
485 * Enable the given features on the given GstAllocTrace object.
488 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
490 g_return_if_fail (trace != NULL);
492 trace->flags = flags;