Make gcc inline assembly conditional on gcc
[platform/upstream/gstreamer.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
59 #if defined (_MSC_VER) && _MSC_VER >= 1400
60 # include <io.h>
61 #endif
62
63 #include "gst_private.h"
64 #include "gstinfo.h"
65
66 #include "gsttrace.h"
67
68 GStaticMutex _gst_trace_mutex = G_STATIC_MUTEX_INIT;
69
70 static
71 #ifdef __inline__
72   __inline__
73 #endif
74     void
75 read_tsc (gint64 * dst)
76 {
77 #if defined(HAVE_RDTSC) && defined(__GNUC__)
78   guint64 tsc;
79   __asm__ __volatile__ ("rdtsc":"=A" (tsc));
80
81   *dst = tsc;
82 #else
83   *dst = 0;
84 #endif
85 }
86
87 /**
88  * gst_trace_read_tsc:
89  * @dst: pointer to hold the result.
90  *
91  * Read a platform independent timer value that can be used in
92  * benchmarks.
93  */
94 void
95 gst_trace_read_tsc (gint64 * dst)
96 {
97   read_tsc (dst);
98 }
99
100 static GstTrace *_gst_trace_default = NULL;
101 gint _gst_trace_on = 1;
102
103 /**
104  * gst_trace_new:
105  * @filename: a filename
106  * @size: the max size of the file
107  *
108  * Create a ringbuffer of @size in the file with @filename to 
109  * store trace results in.
110  *
111  * Returns: a new #GstTrace.
112  */
113 GstTrace *
114 gst_trace_new (gchar * filename, gint size)
115 {
116   GstTrace *trace = g_malloc (sizeof (GstTrace));
117
118   g_return_val_if_fail (trace != NULL, NULL);
119   trace->filename = g_strdup (filename);
120   GST_DEBUG ("opening '%s'", trace->filename);
121 #ifndef S_IWUSR
122 #define S_IWUSR S_IWRITE
123 #endif
124 #ifndef S_IRUSR
125 #define S_IRUSR S_IREAD
126 #endif
127   trace->fd =
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;
135
136   return trace;
137 }
138
139 /**
140  * gst_trace_destroy:
141  * @trace: the #GstTrace to destroy
142  *
143  * Flush an close the previously allocated @trace.
144  */
145 void
146 gst_trace_destroy (GstTrace * trace)
147 {
148   g_return_if_fail (trace != NULL);
149   g_return_if_fail (trace->buf != NULL);
150
151   if (gst_trace_get_remaining (trace) > 0)
152     gst_trace_flush (trace);
153   close (trace->fd);
154   g_free (trace->buf);
155   g_free (trace);
156 }
157
158 /**
159  * gst_trace_flush:
160  * @trace: the #GstTrace to flush.
161  *
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
164  * flushed.
165  */
166 void
167 gst_trace_flush (GstTrace * trace)
168 {
169   if (!trace) {
170     trace = _gst_trace_default;
171     if (!trace)
172       return;
173   }
174
175   g_return_if_fail (write (trace->fd, trace->buf,
176           trace->bufoffset * sizeof (GstTraceEntry)) != -1);
177   trace->bufoffset = 0;
178 }
179
180 /**
181  * gst_trace_text_flush:
182  * @trace: the #GstTrace to flush.
183  *
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
187  * flushed.
188  */
189 void
190 gst_trace_text_flush (GstTrace * trace)
191 {
192   int i;
193
194 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
195   char str[STRSIZE];
196
197   if (!trace) {
198     trace = _gst_trace_default;
199     if (!trace)
200       return;
201   }
202
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);
208   }
209   trace->bufoffset = 0;
210 #undef STRSIZE
211 }
212
213 /**
214  * gst_trace_set_default:
215  * @trace: the #GstTrace to set as the default.
216  *
217  * Set the default #GstTrace to @trace.
218  */
219 void
220 gst_trace_set_default (GstTrace * trace)
221 {
222   g_return_if_fail (trace != NULL);
223   _gst_trace_default = trace;
224 }
225
226 void
227 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
228 {
229   GstTraceEntry *entry;
230
231   if (!trace) {
232     trace = _gst_trace_default;
233     if (!trace)
234       return;
235   }
236
237   entry = trace->buf + trace->bufoffset;
238   read_tsc (&(entry->timestamp));
239   entry->sequence = seq;
240   entry->data = data;
241   strncpy (entry->message, msg, 112);
242   trace->bufoffset++;
243
244   gst_trace_flush (trace);
245 }
246
247
248 /* global flags */
249 static GstAllocTraceFlags _gst_trace_flags = 0;
250
251 /* list of registered tracers */
252 static GList *_gst_alloc_tracers = NULL;
253
254 /**
255  * gst_alloc_trace_available:
256  *
257  * Check if alloc tracing was compiled into the core
258  *
259  * Returns: TRUE if the core was compiled with alloc
260  * tracing enabled.
261  */
262 gboolean
263 gst_alloc_trace_available (void)
264 {
265 #ifdef GST_DISABLE_ALLOC_TRACE
266   return FALSE;
267 #else
268   return TRUE;
269 #endif
270 }
271
272 /**
273  * _gst_alloc_trace_register:
274  * @name: the name of the new alloc trace object.
275  *
276  * Register an get a handle to a GstAllocTrace object that
277  * can be used to trace memory allocations.
278  *
279  * Returns: A handle to a GstAllocTrace.
280  */
281 GstAllocTrace *
282 _gst_alloc_trace_register (const gchar * name)
283 {
284   GstAllocTrace *trace;
285
286   g_return_val_if_fail (name, NULL);
287
288   trace = g_new0 (GstAllocTrace, 1);
289   trace->name = g_strdup (name);
290   trace->live = 0;
291   trace->mem_live = NULL;
292   trace->flags = _gst_trace_flags;
293
294   _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
295
296   return trace;
297 }
298
299 /**
300  * gst_alloc_trace_list:
301  *
302  * Get a list of all registered alloc trace objects.
303  *
304  * Returns: a GList of GstAllocTrace objects.
305  */
306 const GList *
307 gst_alloc_trace_list (void)
308 {
309   return _gst_alloc_tracers;
310 }
311
312 /**
313  * gst_alloc_trace_live_all:
314  *
315  * Get the total number of live registered alloc trace objects.
316  *
317  * Returns: the total number of live registered alloc trace objects.
318  */
319 int
320 gst_alloc_trace_live_all (void)
321 {
322   GList *walk = _gst_alloc_tracers;
323   int num = 0;
324
325   while (walk) {
326     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
327
328     num += trace->live;
329
330     walk = g_list_next (walk);
331   }
332
333   return num;
334 }
335
336 static gint
337 compare_func (GstAllocTrace * a, GstAllocTrace * b)
338 {
339   return strcmp (a->name, b->name);
340 }
341
342 static GList *
343 gst_alloc_trace_list_sorted (void)
344 {
345   GList *ret;
346
347   ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
348       (GCompareFunc) compare_func);
349
350   return ret;
351 }
352
353 /**
354  * gst_alloc_trace_print_all:
355  *
356  * Print the status of all registered alloc trace objects.
357  */
358 void
359 gst_alloc_trace_print_all (void)
360 {
361   GList *orig, *walk;
362
363   orig = walk = gst_alloc_trace_list_sorted ();
364
365   while (walk) {
366     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
367
368     gst_alloc_trace_print (trace);
369
370     walk = g_list_next (walk);
371   }
372
373   g_list_free (orig);
374 }
375
376 /**
377  * gst_alloc_trace_print_live:
378  *
379  * Print the status of all registered alloc trace objects, ignoring those
380  * without live objects.
381  */
382 void
383 gst_alloc_trace_print_live (void)
384 {
385   GList *orig, *walk;
386
387   orig = walk = gst_alloc_trace_list_sorted ();
388
389   while (walk) {
390     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
391
392     if (trace->live)
393       gst_alloc_trace_print (trace);
394
395     walk = g_list_next (walk);
396   }
397
398   g_list_free (orig);
399 }
400
401 /**
402  * gst_alloc_trace_set_flags_all:
403  * @flags: the options to enable
404  *
405  * Enable the specified options on all registered alloc trace
406  * objects.
407  */
408 void
409 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
410 {
411   GList *walk = _gst_alloc_tracers;
412
413   while (walk) {
414     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
415
416     GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
417     gst_alloc_trace_set_flags (trace, flags);
418
419     walk = g_list_next (walk);
420   }
421   _gst_trace_flags = flags;
422 }
423
424 /**
425  * gst_alloc_trace_get:
426  * @name: the name of the alloc trace object
427  *
428  * Get the named alloc trace object.
429  *
430  * Returns: a GstAllocTrace with the given name or NULL when
431  * no alloc tracer was registered with that name.
432  */
433 GstAllocTrace *
434 gst_alloc_trace_get (const gchar * name)
435 {
436   GList *walk = _gst_alloc_tracers;
437
438   g_return_val_if_fail (name, NULL);
439
440   while (walk) {
441     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
442
443     if (!strcmp (trace->name, name))
444       return trace;
445
446     walk = g_list_next (walk);
447   }
448   return NULL;
449 }
450
451 /**
452  * gst_alloc_trace_print:
453  * @trace: the GstAllocTrace to print
454  *
455  * Print the status of the given GstAllocTrace.
456  */
457 void
458 gst_alloc_trace_print (const GstAllocTrace * trace)
459 {
460   GSList *mem_live;
461
462   g_return_if_fail (trace != NULL);
463
464   if (trace->flags & GST_ALLOC_TRACE_LIVE) {
465     g_print ("%-22.22s : %d\n", trace->name, trace->live);
466   } else {
467     g_print ("%-22.22s : (no live count)\n", trace->name);
468   }
469
470   if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
471     mem_live = trace->mem_live;
472
473     while (mem_live) {
474       g_print ("%-22.22s : %p\n", "", mem_live->data);
475       mem_live = mem_live->next;
476     }
477   }
478 }
479
480 /**
481  * gst_alloc_trace_set_flags:
482  * @trace: the GstAllocTrace
483  * @flags: flags to set
484  *
485  * Enable the given features on the given GstAllocTrace object.
486  */
487 void
488 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
489 {
490   g_return_if_fail (trace != NULL);
491
492   trace->flags = flags;
493 }