gst/elements/gstfilesrc.*: send NEW_MEDIA events correctly
[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 (depracated)
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 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34
35 #include "gst_private.h"
36
37 #include "gsttrace.h"
38
39 static
40 #ifdef __inline__
41   __inline__
42 #endif
43     void
44 read_tsc (gint64 * dst)
45 {
46 #ifdef HAVE_RDTSC
47   guint64 tsc;
48   __asm__ __volatile__ ("rdtsc":"=A" (tsc));
49
50   *dst = tsc;
51 #else
52   *dst = 0;
53 #endif
54 }
55
56 void
57 gst_trace_read_tsc (gint64 * dst)
58 {
59   read_tsc (dst);
60 }
61
62 GstTrace *_gst_trace_default = NULL;
63 gint _gst_trace_on = 1;
64
65 GstTrace *
66 gst_trace_new (gchar * filename, gint size)
67 {
68   GstTrace *trace = g_malloc (sizeof (GstTrace));
69
70   g_return_val_if_fail (trace != NULL, NULL);
71   trace->filename = g_strdup (filename);
72   GST_DEBUG ("opening '%s'\n", trace->filename);
73 #ifndef S_IWUSR
74 #define S_IWUSR S_IWRITE
75 #endif
76 #ifndef S_IRUSR
77 #define S_IDUSR S_IREAD
78 #endif
79   trace->fd =
80       open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
81   perror ("opening trace file");
82   g_return_val_if_fail (trace->fd > 0, NULL);
83   trace->buf = g_malloc (size * sizeof (GstTraceEntry));
84   g_return_val_if_fail (trace->buf != NULL, NULL);
85   trace->bufsize = size;
86   trace->bufoffset = 0;
87
88   return trace;
89 }
90
91 void
92 gst_trace_destroy (GstTrace * trace)
93 {
94   g_return_if_fail (trace != NULL);
95   g_return_if_fail (trace->buf != NULL);
96
97   if (gst_trace_get_remaining (trace) > 0)
98     gst_trace_flush (trace);
99   close (trace->fd);
100   g_free (trace->buf);
101   g_free (trace);
102 }
103
104 void
105 gst_trace_flush (GstTrace * trace)
106 {
107   if (!trace) {
108     trace = _gst_trace_default;
109     if (!trace)
110       return;
111   }
112
113   write (trace->fd, trace->buf, trace->bufoffset * sizeof (GstTraceEntry));
114   trace->bufoffset = 0;
115 }
116
117 void
118 gst_trace_text_flush (GstTrace * trace)
119 {
120   int i;
121
122 #define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
123   char str[STRSIZE];
124
125   if (!trace) {
126     trace = _gst_trace_default;
127     if (!trace)
128       return;
129   }
130
131   for (i = 0; i < trace->bufoffset; i++) {
132     g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
133         trace->buf[i].timestamp,
134         trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
135     write (trace->fd, str, strlen (str));
136   }
137   trace->bufoffset = 0;
138 #undef STRSIZE
139 }
140
141 void
142 gst_trace_set_default (GstTrace * trace)
143 {
144   g_return_if_fail (trace != NULL);
145   _gst_trace_default = trace;
146 }
147
148 void
149 _gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
150 {
151   GstTraceEntry *entry;
152
153   if (!trace) {
154     trace = _gst_trace_default;
155     if (!trace)
156       return;
157   }
158
159   entry = trace->buf + trace->bufoffset;
160   read_tsc (&(entry->timestamp));
161   entry->sequence = seq;
162   entry->data = data;
163   strncpy (entry->message, msg, 112);
164   trace->bufoffset++;
165
166   gst_trace_flush (trace);
167 }
168
169
170 /* global flags */
171 static GstAllocTraceFlags _gst_trace_flags = 0;
172
173 /* list of registered tracers */
174 static GList *_gst_alloc_tracers = NULL;
175
176 /**
177  * gst_alloc_trace_available:
178  *
179  * Check if alloc tracing was commiled into the core
180  *
181  * Returns: TRUE if the core was compiled with alloc
182  * tracing enabled.
183  */
184 gboolean
185 gst_alloc_trace_available (void)
186 {
187 #ifdef GST_DISABLE_ALLOC_TRACE
188   return FALSE;
189 #else
190   return TRUE;
191 #endif
192 }
193
194 /**
195  * _gst_alloc_trace_register:
196  * @name: the name of the new alloc trace object.
197  *
198  * Register an get a handle to a GstAllocTrace object that
199  * can be used to trace memory allocations.
200  *
201  * Returns: A handle to a GstAllocTrace.
202  */
203 GstAllocTrace *
204 _gst_alloc_trace_register (const gchar * name)
205 {
206   GstAllocTrace *trace;
207
208   g_return_val_if_fail (name, NULL);
209
210   trace = g_new0 (GstAllocTrace, 1);
211   trace->name = g_strdup (name);
212   trace->live = 0;
213   trace->mem_live = NULL;
214   trace->flags = _gst_trace_flags;
215
216   _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);
217
218   return trace;
219 }
220
221 /**
222  * gst_alloc_trace_list:
223  *
224  * Get a list of all registered alloc trace objects.
225  *
226  * Returns: a GList of GstAllocTrace objects.
227  */
228 const GList *
229 gst_alloc_trace_list (void)
230 {
231   return _gst_alloc_tracers;
232 }
233
234 /**
235  * gst_alloc_trace_live_all:
236  *
237  * Returns the total number of live registered alloc trace objects.
238  */
239 int
240 gst_alloc_trace_live_all (void)
241 {
242   GList *walk = _gst_alloc_tracers;
243   int num = 0;
244
245   while (walk) {
246     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
247
248     num += trace->live;
249
250     walk = g_list_next (walk);
251   }
252
253   return num;
254 }
255
256 /**
257  * gst_alloc_trace_print_all:
258  *
259  * Print the status of all registered alloc trace objectes.
260  */
261 void
262 gst_alloc_trace_print_all (void)
263 {
264   GList *walk = _gst_alloc_tracers;
265
266   while (walk) {
267     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
268
269     gst_alloc_trace_print (trace);
270
271     walk = g_list_next (walk);
272   }
273 }
274
275 /**
276  * gst_alloc_trace_set_flags_all:
277  * @flags: the options to enable
278  *
279  * Enable the specified options on all registered alloc trace
280  * objects.
281  */
282 void
283 gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
284 {
285   GList *walk = _gst_alloc_tracers;
286
287   while (walk) {
288     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
289
290     GST_DEBUG ("set flags on %p\n", trace);
291     gst_alloc_trace_set_flags (trace, flags);
292
293     walk = g_list_next (walk);
294   }
295   _gst_trace_flags = flags;
296 }
297
298 /**
299  * gst_alloc_trace_get:
300  * @name: the name of the alloc trace object
301  *
302  * Get the named alloc trace object.
303  *
304  * Returns: a GstAllocTrace with the given name or NULL when
305  * no alloc tracer was registered with that name.
306  */
307 GstAllocTrace *
308 gst_alloc_trace_get (const gchar * name)
309 {
310   GList *walk = _gst_alloc_tracers;
311
312   g_return_val_if_fail (name, NULL);
313
314   while (walk) {
315     GstAllocTrace *trace = (GstAllocTrace *) walk->data;
316
317     if (!strcmp (trace->name, name))
318       return trace;
319
320     walk = g_list_next (walk);
321   }
322   return NULL;
323 }
324
325 /**
326  * gst_alloc_trace_print:
327  * @trace: the GstAllocTrace to print
328  *
329  * Print the status of the given GstAllocTrace.
330  */
331 void
332 gst_alloc_trace_print (const GstAllocTrace * trace)
333 {
334   GSList *mem_live;
335
336   g_return_if_fail (trace != NULL);
337
338   g_print ("%s (%p): flags %d", trace->name, trace, trace->flags);
339
340   if (trace->flags & GST_ALLOC_TRACE_LIVE) {
341     g_print (", live %d", trace->live);
342   }
343   if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
344     mem_live = trace->mem_live;
345
346     if (!mem_live) {
347       g_print (", no live memory");
348     } else {
349       g_print (", dumping live memory: ");
350
351       while (mem_live) {
352         g_print ("%p ", mem_live->data);
353         mem_live = g_slist_next (mem_live);
354       }
355       g_print ("\ntotal %d", g_slist_length (trace->mem_live));
356     }
357   }
358   g_print ("\n");
359 }
360
361 /**
362  * gst_alloc_trace_set_flags:
363  * @trace: the GstAllocTrace 
364  * @flags: flags to set 
365  *
366  * Enable the given features on the given GstAllocTrace object.
367  */
368 void
369 gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
370 {
371   g_return_if_fail (trace != NULL);
372
373   trace->flags = flags;
374 }