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
40 #include "gst_private.h"
50 read_tsc (gint64 * dst)
54 __asm__ __volatile__ ("rdtsc":"=A" (tsc));
63 gst_trace_read_tsc (gint64 * dst)
68 GstTrace *_gst_trace_default = NULL;
69 gint _gst_trace_on = 1;
72 gst_trace_new (gchar * filename, gint size)
74 GstTrace *trace = g_malloc (sizeof (GstTrace));
76 g_return_val_if_fail (trace != NULL, NULL);
77 trace->filename = g_strdup (filename);
78 GST_DEBUG ("opening '%s'\n", trace->filename);
80 #define S_IWUSR S_IWRITE
83 #define S_IRUSR S_IREAD
86 open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
87 perror ("opening trace file");
88 g_return_val_if_fail (trace->fd > 0, NULL);
89 trace->buf = g_malloc (size * sizeof (GstTraceEntry));
90 g_return_val_if_fail (trace->buf != NULL, NULL);
91 trace->bufsize = size;
98 gst_trace_destroy (GstTrace * trace)
100 g_return_if_fail (trace != NULL);
101 g_return_if_fail (trace->buf != NULL);
103 if (gst_trace_get_remaining (trace) > 0)
104 gst_trace_flush (trace);
111 gst_trace_flush (GstTrace * trace)
114 trace = _gst_trace_default;
119 g_return_if_fail (write (trace->fd, trace->buf,
120 trace->bufoffset * sizeof (GstTraceEntry)) != -1);
121 trace->bufoffset = 0;
125 gst_trace_text_flush (GstTrace * trace)
129 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
133 trace = _gst_trace_default;
138 for (i = 0; i < trace->bufoffset; i++) {
139 g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
140 trace->buf[i].timestamp,
141 trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
142 g_return_if_fail (write (trace->fd, str, strlen (str)) != -1);
144 trace->bufoffset = 0;
149 gst_trace_set_default (GstTrace * trace)
151 g_return_if_fail (trace != NULL);
152 _gst_trace_default = trace;
156 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
158 GstTraceEntry *entry;
161 trace = _gst_trace_default;
166 entry = trace->buf + trace->bufoffset;
167 read_tsc (&(entry->timestamp));
168 entry->sequence = seq;
170 strncpy (entry->message, msg, 112);
173 gst_trace_flush (trace);
178 static GstAllocTraceFlags _gst_trace_flags = 0;
180 /* list of registered tracers */
181 static GList *_gst_alloc_tracers = NULL;
184 * gst_alloc_trace_available:
186 * Check if alloc tracing was commiled into the core
188 * Returns: TRUE if the core was compiled with alloc
192 gst_alloc_trace_available (void)
194 #ifdef GST_DISABLE_ALLOC_TRACE
202 * _gst_alloc_trace_register:
203 * @name: the name of the new alloc trace object.
205 * Register an get a handle to a GstAllocTrace object that
206 * can be used to trace memory allocations.
208 * Returns: A handle to a GstAllocTrace.
211 _gst_alloc_trace_register (const gchar * name)
213 GstAllocTrace *trace;
215 g_return_val_if_fail (name, NULL);
217 trace = g_new0 (GstAllocTrace, 1);
218 trace->name = g_strdup (name);
220 trace->mem_live = NULL;
221 trace->flags = _gst_trace_flags;
223 _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
229 * gst_alloc_trace_list:
231 * Get a list of all registered alloc trace objects.
233 * Returns: a GList of GstAllocTrace objects.
236 gst_alloc_trace_list (void)
238 return _gst_alloc_tracers;
242 * gst_alloc_trace_live_all:
244 * Returns the total number of live registered alloc trace objects.
247 gst_alloc_trace_live_all (void)
249 GList *walk = _gst_alloc_tracers;
253 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
257 walk = g_list_next (walk);
264 compare_func (GstAllocTrace * a, GstAllocTrace * b)
266 return strcmp (a->name, b->name);
270 gst_alloc_trace_list_sorted (void)
274 ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
275 (GCompareFunc) compare_func);
281 * gst_alloc_trace_print_all:
283 * Print the status of all registered alloc trace objects.
286 gst_alloc_trace_print_all (void)
290 orig = walk = gst_alloc_trace_list_sorted ();
293 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
295 gst_alloc_trace_print (trace);
297 walk = g_list_next (walk);
304 * gst_alloc_trace_print_live:
306 * Print the status of all registered alloc trace objects, ignoring those
307 * without live objects.
310 gst_alloc_trace_print_live (void)
314 orig = walk = gst_alloc_trace_list_sorted ();
317 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
320 gst_alloc_trace_print (trace);
322 walk = g_list_next (walk);
329 * gst_alloc_trace_set_flags_all:
330 * @flags: the options to enable
332 * Enable the specified options on all registered alloc trace
336 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
338 GList *walk = _gst_alloc_tracers;
341 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
343 GST_DEBUG ("set flags on %p\n", trace);
344 gst_alloc_trace_set_flags (trace, flags);
346 walk = g_list_next (walk);
348 _gst_trace_flags = flags;
352 * gst_alloc_trace_get:
353 * @name: the name of the alloc trace object
355 * Get the named alloc trace object.
357 * Returns: a GstAllocTrace with the given name or NULL when
358 * no alloc tracer was registered with that name.
361 gst_alloc_trace_get (const gchar * name)
363 GList *walk = _gst_alloc_tracers;
365 g_return_val_if_fail (name, NULL);
368 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
370 if (!strcmp (trace->name, name))
373 walk = g_list_next (walk);
379 * gst_alloc_trace_print:
380 * @trace: the GstAllocTrace to print
382 * Print the status of the given GstAllocTrace.
385 gst_alloc_trace_print (const GstAllocTrace * trace)
389 g_return_if_fail (trace != NULL);
391 if (trace->flags & GST_ALLOC_TRACE_LIVE) {
392 g_print ("%-22.22s : %d\n", trace->name, trace->live);
394 g_print ("%-22.22s : (no live count)\n", trace->name);
397 if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
398 mem_live = trace->mem_live;
401 g_print ("%-22.22s : %p\n", "", mem_live->data);
402 mem_live = mem_live->next;
408 * gst_alloc_trace_set_flags:
409 * @trace: the GstAllocTrace
410 * @flags: flags to set
412 * Enable the given features on the given GstAllocTrace object.
415 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
417 g_return_if_fail (trace != NULL);
419 trace->flags = flags;