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