2.0 beta init
[framework/multimedia/gstreamer0.10.git] / gst / gsttrace.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gsttrace.c: Tracing functions (deprecated)
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gsttrace
25  * @short_description: Tracing functionality
26  *
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
29  * it when it's freed.
30  *
31  * <example>
32  * <title>Tracing object instances</title>
33  *   <programlisting>
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).");
38  *     }
39  *     gst_alloc_trace_print_live ();
40  *     // do something here
41  *     gst_alloc_trace_print_live ();
42  *   </programlisting>
43  * </example>
44  *
45  * Last reviewed on 2005-11-21 (0.9.5)
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51 #include <stdio.h>
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55 #include <sys/stat.h>
56 #include <fcntl.h>
57 #include <string.h>
58 #include <errno.h>
59
60 #if defined (_MSC_VER) && _MSC_VER >= 1400
61 # include <io.h>
62 #endif
63
64 #include "gst_private.h"
65 #include "gstinfo.h"
66
67 #include "gsttrace.h"
68
69 GStaticMutex _gst_trace_mutex = G_STATIC_MUTEX_INIT;
70
71 static
72 #ifdef __inline__
73   __inline__
74 #endif
75     void
76 read_tsc (gint64 * dst)
77 {
78 #if defined(HAVE_RDTSC) && defined(__GNUC__)
79   guint64 tsc;
80   __asm__ __volatile__ ("rdtsc":"=A" (tsc));
81
82   *dst = tsc;
83 #else
84   *dst = 0;
85 #endif
86 }
87
88 /**
89  * gst_trace_read_tsc:
90  * @dst: (out) pointer to hold the result.
91  *
92  * Read a platform independent timer value that can be used in
93  * benchmarks.
94  */
95 void
96 gst_trace_read_tsc (gint64 * dst)
97 {
98   read_tsc (dst);
99 }
100
101 static GstTrace *_gst_trace_default = NULL;
102 gint _gst_trace_on = 1;
103
104 /**
105  * gst_trace_new:
106  * @filename: a filename
107  * @size: the max size of the file
108  *
109  * Create a ringbuffer of @size in the file with @filename to
110  * store trace results in.
111  *
112  * Free-function: gst_trace_destroy
113  *
114  * Returns: (transfer full): a new #GstTrace.
115  */
116 GstTrace *
117 gst_trace_new (const gchar * filename, gint size)
118 {
119   GstTrace *trace = g_slice_new (GstTrace);
120
121   g_return_val_if_fail (trace != NULL, NULL);
122   trace->filename = g_strdup (filename);
123   GST_DEBUG ("opening '%s'", trace->filename);
124 #ifndef S_IWUSR
125 #define S_IWUSR S_IWRITE
126 #endif
127 #ifndef S_IRUSR
128 #define S_IRUSR S_IREAD
129 #endif
130   trace->fd =
131       open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
132   perror ("opening trace file");
133   g_return_val_if_fail (trace->fd > 0, NULL);
134   trace->buf = g_malloc (size * sizeof (GstTraceEntry));
135   g_return_val_if_fail (trace->buf != NULL, NULL);
136   trace->bufsize = size;
137   trace->bufoffset = 0;
138
139   return trace;
140 }
141
142 /**
143  * gst_trace_destroy:
144  * @trace: (in) (transfer full): the #GstTrace to destroy
145  *
146  * Flush an close the previously allocated @trace.
147  */
148 void
149 gst_trace_destroy (GstTrace * trace)
150 {
151   g_return_if_fail (trace != NULL);
152   g_return_if_fail (trace->buf != NULL);
153
154   if (gst_trace_get_remaining (trace) > 0)
155     gst_trace_flush (trace);
156   close (trace->fd);
157   g_free (trace->buf);
158   g_slice_free (GstTrace, trace);
159 }
160
161 /**
162  * gst_trace_flush:
163  * @trace: the #GstTrace to flush.
164  *
165  * Flush any pending trace entries in @trace to the trace file.
166  * @trace can be NULL in which case the default #GstTrace will be
167  * flushed.
168  */
169 void
170 gst_trace_flush (GstTrace * trace)
171 {
172   int res, buf_len;
173
174   if (!trace) {
175     trace = _gst_trace_default;
176     if (!trace)
177       return;
178   }
179
180   buf_len = trace->bufoffset * sizeof (GstTraceEntry);
181   res = write (trace->fd, trace->buf, buf_len);
182   if (res < 0) {
183     g_warning ("Failed to write trace: %s", g_strerror (errno));
184     return;
185   } else if (res < buf_len) {
186     g_warning ("Failed to write trace: only wrote %d/%d bytes", res, buf_len);
187     return;
188   }
189   trace->bufoffset = 0;
190 }
191
192 /**
193  * gst_trace_text_flush:
194  * @trace: the #GstTrace to flush.
195  *
196  * Flush any pending trace entries in @trace to the trace file,
197  * formatted as a text line with timestamp and sequence numbers.
198  * @trace can be NULL in which case the default #GstTrace will be
199  * flushed.
200  */
201 void
202 gst_trace_text_flush (GstTrace * trace)
203 {
204   int i;
205
206 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
207   char str[STRSIZE];
208
209   if (!trace) {
210     trace = _gst_trace_default;
211     if (!trace)
212       return;
213   }
214
215   for (i = 0; i < trace->bufoffset; i++) {
216     g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
217         trace->buf[i].timestamp,
218         trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
219     if (write (trace->fd, str, strlen (str)) < 0) {
220       g_warning ("Failed to write trace %d: %s", i, g_strerror (errno));
221       return;
222     }
223   }
224   trace->bufoffset = 0;
225 #undef STRSIZE
226 }
227
228 /**
229  * gst_trace_set_default:
230  * @trace: the #GstTrace to set as the default.
231  *
232  * Set the default #GstTrace to @trace.
233  */
234 void
235 gst_trace_set_default (GstTrace * trace)
236 {
237   g_return_if_fail (trace != NULL);
238   _gst_trace_default = trace;
239 }
240
241 void
242 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
243 {
244   GstTraceEntry *entry;
245
246   if (!trace) {
247     trace = _gst_trace_default;
248     if (!trace)
249       return;
250   }
251
252   entry = trace->buf + trace->bufoffset;
253   read_tsc (&(entry->timestamp));
254   entry->sequence = seq;
255   entry->data = data;
256   strncpy (entry->message, msg, 112);
257   entry->message[111] = '\0';
258   trace->bufoffset++;
259
260   gst_trace_flush (trace);
261 }
262
263
264 /* global flags */
265 static GstAllocTraceFlags _gst_trace_flags = GST_ALLOC_TRACE_NONE;
266
267 /* list of registered tracers */
268 static GList *_gst_alloc_tracers = NULL;
269
270 /**
271  * gst_alloc_trace_available:
272  *
273  * Check if alloc tracing was compiled into the core
274  *
275  * Returns: TRUE if the core was compiled with alloc
276  * tracing enabled.
277  */
278 gboolean
279 gst_alloc_trace_available (void)
280 {
281 #ifdef GST_DISABLE_ALLOC_TRACE
282   return FALSE;
283 #else
284   return TRUE;
285 #endif
286 }
287
288 /**
289  * _gst_alloc_trace_register:
290  * @name: the name of the new alloc trace object.
291  *
292  * Register an get a handle to a GstAllocTrace object that
293  * can be used to trace memory allocations.
294  *
295  * Returns: A handle to a GstAllocTrace.
296  */
297 GstAllocTrace *
298 _gst_alloc_trace_register (const gchar * name)
299 {
300   GstAllocTrace *trace;
301
302   g_return_val_if_fail (name, NULL);
303
304   trace = g_slice_new (GstAllocTrace);
305   trace->name = g_strdup (name);
306   trace->live = 0;
307   trace->mem_live = NULL;
308   trace->flags = _gst_trace_flags;
309
310   _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
311
312   return trace;
313 }
314
315 /**
316  * gst_alloc_trace_list:
317  *
318  * Get a list of all registered alloc trace objects.
319  *
320  * Returns: a GList of GstAllocTrace objects.
321  */
322 const GList *
323 gst_alloc_trace_list (void)
324 {
325   return _gst_alloc_tracers;
326 }
327
328 /**
329  * gst_alloc_trace_live_all:
330  *
331  * Get the total number of live registered alloc trace objects.
332  *
333  * Returns: the total number of live registered alloc trace objects.
334  */
335 int
336 gst_alloc_trace_live_all (void)
337 {
338   GList *walk = _gst_alloc_tracers;
339   int num = 0;
340
341   while (walk) {
342     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
343
344     num += trace->live;
345
346     walk = g_list_next (walk);
347   }
348
349   return num;
350 }
351
352 static gint
353 compare_func (GstAllocTrace * a, GstAllocTrace * b)
354 {
355   return strcmp (a->name, b->name);
356 }
357
358 static GList *
359 gst_alloc_trace_list_sorted (void)
360 {
361   GList *ret;
362
363   ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
364       (GCompareFunc) compare_func);
365
366   return ret;
367 }
368
369 /**
370  * gst_alloc_trace_print_all:
371  *
372  * Print the status of all registered alloc trace objects.
373  */
374 void
375 gst_alloc_trace_print_all (void)
376 {
377   GList *orig, *walk;
378
379   orig = walk = gst_alloc_trace_list_sorted ();
380
381   while (walk) {
382     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
383
384     gst_alloc_trace_print (trace);
385
386     walk = g_list_next (walk);
387   }
388
389   g_list_free (orig);
390 }
391
392 /**
393  * gst_alloc_trace_print_live:
394  *
395  * Print the status of all registered alloc trace objects, ignoring those
396  * without live objects.
397  */
398 void
399 gst_alloc_trace_print_live (void)
400 {
401   GList *orig, *walk;
402
403   orig = walk = gst_alloc_trace_list_sorted ();
404
405   while (walk) {
406     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
407
408     if (trace->live)
409       gst_alloc_trace_print (trace);
410
411     walk = g_list_next (walk);
412   }
413
414   g_list_free (orig);
415 }
416
417 /**
418  * gst_alloc_trace_set_flags_all:
419  * @flags: the options to enable
420  *
421  * Enable the specified options on all registered alloc trace
422  * objects.
423  */
424 void
425 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
426 {
427   GList *walk = _gst_alloc_tracers;
428
429   while (walk) {
430     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
431
432     GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
433     gst_alloc_trace_set_flags (trace, flags);
434
435     walk = g_list_next (walk);
436   }
437   _gst_trace_flags = flags;
438 }
439
440 /**
441  * gst_alloc_trace_get:
442  * @name: the name of the alloc trace object
443  *
444  * Get the named alloc trace object.
445  *
446  * Returns: a GstAllocTrace with the given name or NULL when
447  * no alloc tracer was registered with that name.
448  */
449 GstAllocTrace *
450 gst_alloc_trace_get (const gchar * name)
451 {
452   GList *walk = _gst_alloc_tracers;
453
454   g_return_val_if_fail (name, NULL);
455
456   while (walk) {
457     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
458
459     if (!strcmp (trace->name, name))
460       return trace;
461
462     walk = g_list_next (walk);
463   }
464   return NULL;
465 }
466
467 /**
468  * gst_alloc_trace_print:
469  * @trace: the GstAllocTrace to print
470  *
471  * Print the status of the given GstAllocTrace.
472  */
473 void
474 gst_alloc_trace_print (const GstAllocTrace * trace)
475 {
476   GSList *mem_live;
477
478   g_return_if_fail (trace != NULL);
479
480   if (trace->flags & GST_ALLOC_TRACE_LIVE) {
481     g_print ("%-22.22s : %d\n", trace->name, trace->live);
482   } else {
483     g_print ("%-22.22s : (no live count)\n", trace->name);
484   }
485
486   if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
487     mem_live = trace->mem_live;
488
489     while (mem_live) {
490       gpointer data = mem_live->data;
491
492       if (G_IS_OBJECT (data)) {
493         g_print ("%-22.22s : %p\n", g_type_name (G_OBJECT_TYPE (data)), data);
494       } else {
495         g_print ("%-22.22s : %p\n", "", data);
496       }
497       mem_live = mem_live->next;
498     }
499   }
500 }
501
502 /**
503  * gst_alloc_trace_set_flags:
504  * @trace: the GstAllocTrace
505  * @flags: flags to set
506  *
507  * Enable the given features on the given GstAllocTrace object.
508  */
509 void
510 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
511 {
512   g_return_if_fail (trace != NULL);
513
514   trace->flags = flags;
515 }