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 * Get the total number of live registered alloc trace objects.
246 * Returns: the total number of live registered alloc trace objects.
249 gst_alloc_trace_live_all (void)
251 GList *walk = _gst_alloc_tracers;
255 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
259 walk = g_list_next (walk);
266 compare_func (GstAllocTrace * a, GstAllocTrace * b)
268 return strcmp (a->name, b->name);
272 gst_alloc_trace_list_sorted (void)
276 ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
277 (GCompareFunc) compare_func);
283 * gst_alloc_trace_print_all:
285 * Print the status of all registered alloc trace objects.
288 gst_alloc_trace_print_all (void)
292 orig = walk = gst_alloc_trace_list_sorted ();
295 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
297 gst_alloc_trace_print (trace);
299 walk = g_list_next (walk);
306 * gst_alloc_trace_print_live:
308 * Print the status of all registered alloc trace objects, ignoring those
309 * without live objects.
312 gst_alloc_trace_print_live (void)
316 orig = walk = gst_alloc_trace_list_sorted ();
319 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
322 gst_alloc_trace_print (trace);
324 walk = g_list_next (walk);
331 * gst_alloc_trace_set_flags_all:
332 * @flags: the options to enable
334 * Enable the specified options on all registered alloc trace
338 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
340 GList *walk = _gst_alloc_tracers;
343 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
345 GST_DEBUG ("set flags on %p\n", trace);
346 gst_alloc_trace_set_flags (trace, flags);
348 walk = g_list_next (walk);
350 _gst_trace_flags = flags;
354 * gst_alloc_trace_get:
355 * @name: the name of the alloc trace object
357 * Get the named alloc trace object.
359 * Returns: a GstAllocTrace with the given name or NULL when
360 * no alloc tracer was registered with that name.
363 gst_alloc_trace_get (const gchar * name)
365 GList *walk = _gst_alloc_tracers;
367 g_return_val_if_fail (name, NULL);
370 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
372 if (!strcmp (trace->name, name))
375 walk = g_list_next (walk);
381 * gst_alloc_trace_print:
382 * @trace: the GstAllocTrace to print
384 * Print the status of the given GstAllocTrace.
387 gst_alloc_trace_print (const GstAllocTrace * trace)
391 g_return_if_fail (trace != NULL);
393 if (trace->flags & GST_ALLOC_TRACE_LIVE) {
394 g_print ("%-22.22s : %d\n", trace->name, trace->live);
396 g_print ("%-22.22s : (no live count)\n", trace->name);
399 if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
400 mem_live = trace->mem_live;
403 g_print ("%-22.22s : %p\n", "", mem_live->data);
404 mem_live = mem_live->next;
410 * gst_alloc_trace_set_flags:
411 * @trace: the GstAllocTrace
412 * @flags: flags to set
414 * Enable the given features on the given GstAllocTrace object.
417 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
419 g_return_if_fail (trace != NULL);
421 trace->flags = flags;