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 GMutex _gst_trace_mutex;
72 read_tsc (gint64 * dst)
74 #if defined(HAVE_RDTSC) && defined(__GNUC__)
76 __asm__ __volatile__ ("rdtsc":"=A" (tsc));
86 * @dst: (out) pointer to hold the result.
88 * Read a platform independent timer value that can be used in
92 gst_trace_read_tsc (gint64 * dst)
97 static GstTrace *_gst_trace_default = NULL;
98 gint _gst_trace_on = 1;
102 * @filename: a filename
103 * @size: the max size of the file
105 * Create a ringbuffer of @size in the file with @filename to
106 * store trace results in.
108 * Free-function: gst_trace_destroy
110 * Returns: (transfer full): a new #GstTrace.
113 gst_trace_new (const gchar * filename, gint size)
115 GstTrace *trace = g_slice_new (GstTrace);
117 g_return_val_if_fail (trace != NULL, NULL);
118 trace->filename = g_strdup (filename);
119 GST_DEBUG ("opening '%s'", trace->filename);
121 #define S_IWUSR S_IWRITE
124 #define S_IRUSR S_IREAD
127 open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
128 perror ("opening trace file");
129 g_return_val_if_fail (trace->fd > 0, NULL);
130 trace->buf = g_malloc (size * sizeof (GstTraceEntry));
131 g_return_val_if_fail (trace->buf != NULL, NULL);
132 trace->bufsize = size;
133 trace->bufoffset = 0;
140 * @trace: (in) (transfer full): the #GstTrace to destroy
142 * Flush an close the previously allocated @trace.
145 gst_trace_destroy (GstTrace * trace)
147 g_return_if_fail (trace != NULL);
148 g_return_if_fail (trace->buf != NULL);
150 if (gst_trace_get_remaining (trace) > 0)
151 gst_trace_flush (trace);
154 g_slice_free (GstTrace, trace);
159 * @trace: the #GstTrace to flush.
161 * Flush any pending trace entries in @trace to the trace file.
162 * @trace can be NULL in which case the default #GstTrace will be
166 gst_trace_flush (GstTrace * trace)
171 trace = _gst_trace_default;
176 buf_len = trace->bufoffset * sizeof (GstTraceEntry);
177 res = write (trace->fd, trace->buf, buf_len);
179 g_warning ("Failed to write trace: %s", g_strerror (errno));
181 } else if (res < buf_len) {
182 g_warning ("Failed to write trace: only wrote %d/%d bytes", res, buf_len);
185 trace->bufoffset = 0;
189 * gst_trace_text_flush:
190 * @trace: the #GstTrace to flush.
192 * Flush any pending trace entries in @trace to the trace file,
193 * formatted as a text line with timestamp and sequence numbers.
194 * @trace can be NULL in which case the default #GstTrace will be
198 gst_trace_text_flush (GstTrace * trace)
202 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
206 trace = _gst_trace_default;
211 for (i = 0; i < trace->bufoffset; i++) {
212 g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
213 trace->buf[i].timestamp,
214 trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
215 if (write (trace->fd, str, strlen (str)) < 0) {
216 g_warning ("Failed to write trace %d: %s", i, g_strerror (errno));
220 trace->bufoffset = 0;
225 * gst_trace_set_default:
226 * @trace: the #GstTrace to set as the default.
228 * Set the default #GstTrace to @trace.
231 gst_trace_set_default (GstTrace * trace)
233 g_return_if_fail (trace != NULL);
234 _gst_trace_default = trace;
238 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
240 GstTraceEntry *entry;
243 trace = _gst_trace_default;
248 entry = trace->buf + trace->bufoffset;
249 read_tsc (&(entry->timestamp));
250 entry->sequence = seq;
252 strncpy (entry->message, msg, 112);
253 entry->message[111] = '\0';
256 gst_trace_flush (trace);
261 static GstAllocTraceFlags _gst_trace_flags = GST_ALLOC_TRACE_NONE;
263 /* list of registered tracers */
264 static GList *_gst_alloc_tracers = NULL;
267 * gst_alloc_trace_available:
269 * Check if alloc tracing was compiled into the core
271 * Returns: TRUE if the core was compiled with alloc
275 gst_alloc_trace_available (void)
277 #ifdef GST_DISABLE_ALLOC_TRACE
285 * _gst_alloc_trace_register:
286 * @name: the name of the new alloc trace object.
288 * Register an get a handle to a GstAllocTrace object that
289 * can be used to trace memory allocations.
291 * Returns: A handle to a GstAllocTrace.
294 _gst_alloc_trace_register (const gchar * name)
296 GstAllocTrace *trace;
298 g_return_val_if_fail (name, NULL);
300 trace = g_slice_new (GstAllocTrace);
301 trace->name = g_strdup (name);
303 trace->mem_live = NULL;
304 trace->flags = _gst_trace_flags;
306 _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
312 * gst_alloc_trace_list:
314 * Get a list of all registered alloc trace objects.
316 * Returns: a GList of GstAllocTrace objects.
319 gst_alloc_trace_list (void)
321 return _gst_alloc_tracers;
325 * gst_alloc_trace_live_all:
327 * Get the total number of live registered alloc trace objects.
329 * Returns: the total number of live registered alloc trace objects.
332 gst_alloc_trace_live_all (void)
334 GList *walk = _gst_alloc_tracers;
338 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
342 walk = g_list_next (walk);
349 compare_func (GstAllocTrace * a, GstAllocTrace * b)
351 return strcmp (a->name, b->name);
355 gst_alloc_trace_list_sorted (void)
359 ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
360 (GCompareFunc) compare_func);
366 * gst_alloc_trace_print_all:
368 * Print the status of all registered alloc trace objects.
371 gst_alloc_trace_print_all (void)
375 orig = walk = gst_alloc_trace_list_sorted ();
378 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
380 gst_alloc_trace_print (trace);
382 walk = g_list_next (walk);
389 * gst_alloc_trace_print_live:
391 * Print the status of all registered alloc trace objects, ignoring those
392 * without live objects.
395 gst_alloc_trace_print_live (void)
399 orig = walk = gst_alloc_trace_list_sorted ();
402 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
405 gst_alloc_trace_print (trace);
407 walk = g_list_next (walk);
414 * gst_alloc_trace_set_flags_all:
415 * @flags: the options to enable
417 * Enable the specified options on all registered alloc trace
421 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
423 GList *walk = _gst_alloc_tracers;
426 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
428 GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
429 gst_alloc_trace_set_flags (trace, flags);
431 walk = g_list_next (walk);
433 _gst_trace_flags = flags;
437 * gst_alloc_trace_get:
438 * @name: the name of the alloc trace object
440 * Get the named alloc trace object.
442 * Returns: a GstAllocTrace with the given name or NULL when
443 * no alloc tracer was registered with that name.
446 gst_alloc_trace_get (const gchar * name)
448 GList *walk = _gst_alloc_tracers;
450 g_return_val_if_fail (name, NULL);
453 GstAllocTrace *trace = (GstAllocTrace *) walk->data;
455 if (!strcmp (trace->name, name))
458 walk = g_list_next (walk);
464 * gst_alloc_trace_print:
465 * @trace: the GstAllocTrace to print
467 * Print the status of the given GstAllocTrace.
470 gst_alloc_trace_print (const GstAllocTrace * trace)
474 g_return_if_fail (trace != NULL);
476 if (trace->flags & GST_ALLOC_TRACE_LIVE) {
477 g_print ("%-22.22s : %d\n", trace->name, trace->live);
479 g_print ("%-22.22s : (no live count)\n", trace->name);
482 if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
483 mem_live = trace->mem_live;
486 gpointer data = mem_live->data;
488 if (G_IS_OBJECT (data)) {
489 g_print ("%-22.22s : %p\n", g_type_name (G_OBJECT_TYPE (data)), data);
491 g_print ("%-22.22s : %p\n", "", data);
493 mem_live = mem_live->next;
499 * gst_alloc_trace_set_flags:
500 * @trace: the GstAllocTrace
501 * @flags: flags to set
503 * Enable the given features on the given GstAllocTrace object.
506 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
508 g_return_if_fail (trace != NULL);
510 trace->flags = flags;