source: trunk/third/gstreamer/gst/gsttrace.c @ 21005

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