summaryrefslogtreecommitdiff
path: root/gst/gsttrace.c
blob: b2762205d60f1d8844a3180ca79f9368d6ee24dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gsttrace.c: Tracing functions (deprecated)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/**
 * SECTION:gsttrace
 * @short_description: Tracing functionality
 *
 * Traces allows to track object allocation. They provide a instance counter per
 * #GType. The counter is incremented for each object allocated and decremented
 * it when it's freed.
 *
 * <example>
 * <title>Tracing object instances</title>
 *   <programlisting>
 *     // trace un-freed object instances
 *     gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
 *     if (!gst_alloc_trace_available ()) {
 *       g_warning ("Trace not available (recompile with trace enabled).");
 *     }
 *     gst_alloc_trace_print_live ();
 *     // do something here
 *     gst_alloc_trace_print_live ();
 *   </programlisting>
 * </example>
 *
 * Last reviewed on 2005-11-21 (0.9.5)
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#if defined (_MSC_VER) && _MSC_VER >= 1400
# include <io.h>
#endif

#include "gst_private.h"
#include "gstinfo.h"

#include "gsttrace.h"

GStaticMutex _gst_trace_mutex = G_STATIC_MUTEX_INIT;

static
#ifdef __inline__
  __inline__
#endif
    void
read_tsc (gint64 * dst)
{
#ifdef HAVE_RDTSC
  guint64 tsc;
  __asm__ __volatile__ ("rdtsc":"=A" (tsc));

  *dst = tsc;
#else
  *dst = 0;
#endif
}

/**
 * gst_trace_read_tsc:
 * @dst: pointer to hold the result.
 *
 * Read a platform independent timer value that can be used in
 * benchmarks.
 */
void
gst_trace_read_tsc (gint64 * dst)
{
  read_tsc (dst);
}

static GstTrace *_gst_trace_default = NULL;
gint _gst_trace_on = 1;

/**
 * gst_trace_new:
 * @filename: a filename
 * @size: the max size of the file
 *
 * Create a ringbuffer of @size in the file with @filename to 
 * store trace results in.
 *
 * Returns: a new #GstTrace.
 */
GstTrace *
gst_trace_new (gchar * filename, gint size)
{
  GstTrace *trace = g_malloc (sizeof (GstTrace));

  g_return_val_if_fail (trace != NULL, NULL);
  trace->filename = g_strdup (filename);
  GST_DEBUG ("opening '%s'", trace->filename);
#ifndef S_IWUSR
#define S_IWUSR S_IWRITE
#endif
#ifndef S_IRUSR
#define S_IRUSR S_IREAD
#endif
  trace->fd =
      open (trace->filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  perror ("opening trace file");
  g_return_val_if_fail (trace->fd > 0, NULL);
  trace->buf = g_malloc (size * sizeof (GstTraceEntry));
  g_return_val_if_fail (trace->buf != NULL, NULL);
  trace->bufsize = size;
  trace->bufoffset = 0;

  return trace;
}

/**
 * gst_trace_destroy:
 * @trace: the #GstTrace to destroy
 *
 * Flush an close the previously allocated @trace.
 */
void
gst_trace_destroy (GstTrace * trace)
{
  g_return_if_fail (trace != NULL);
  g_return_if_fail (trace->buf != NULL);

  if (gst_trace_get_remaining (trace) > 0)
    gst_trace_flush (trace);
  close (trace->fd);
  g_free (trace->buf);
  g_free (trace);
}

/**
 * gst_trace_flush:
 * @trace: the #GstTrace to flush.
 *
 * Flush any pending trace entries in @trace to the trace file.
 * @trace can be NULL in which case the default #GstTrace will be
 * flushed.
 */
void
gst_trace_flush (GstTrace * trace)
{
  if (!trace) {
    trace = _gst_trace_default;
    if (!trace)
      return;
  }

  g_return_if_fail (write (trace->fd, trace->buf,
          trace->bufoffset * sizeof (GstTraceEntry)) != -1);
  trace->bufoffset = 0;
}

/**
 * gst_trace_text_flush:
 * @trace: the #GstTrace to flush.
 *
 * Flush any pending trace entries in @trace to the trace file, 
 * formatted as a text line with timestamp and sequence numbers.
 * @trace can be NULL in which case the default #GstTrace will be
 * flushed.
 */
void
gst_trace_text_flush (GstTrace * trace)
{
  int i;

#define STRSIZE (20 + 1 + 10 + 1 + 10 + 1 + 112 + 1 + 1)
  char str[STRSIZE];

  if (!trace) {
    trace = _gst_trace_default;
    if (!trace)
      return;
  }

  for (i = 0; i < trace->bufoffset; i++) {
    g_snprintf (str, STRSIZE, "%20" G_GINT64_FORMAT " %10d %10d %s\n",
        trace->buf[i].timestamp,
        trace->buf[i].sequence, trace->buf[i].data, trace->buf[i].message);
    g_return_if_fail (write (trace->fd, str, strlen (str)) != -1);
  }
  trace->bufoffset = 0;
#undef STRSIZE
}

/**
 * gst_trace_set_default:
 * @trace: the #GstTrace to set as the default.
 *
 * Set the default #GstTrace to @trace.
 */
void
gst_trace_set_default (GstTrace * trace)
{
  g_return_if_fail (trace != NULL);
  _gst_trace_default = trace;
}

void
_gst_trace_add_entry (GstTrace * trace, guint32 seq, guint32 data, gchar * msg)
{
  GstTraceEntry *entry;

  if (!trace) {
    trace = _gst_trace_default;
    if (!trace)
      return;
  }

  entry = trace->buf + trace->bufoffset;
  read_tsc (&(entry->timestamp));
  entry->sequence = seq;
  entry->data = data;
  strncpy (entry->message, msg, 112);
  trace->bufoffset++;

  gst_trace_flush (trace);
}


/* global flags */
static GstAllocTraceFlags _gst_trace_flags = 0;

/* list of registered tracers */
static GList *_gst_alloc_tracers = NULL;

/**
 * gst_alloc_trace_available:
 *
 * Check if alloc tracing was compiled into the core
 *
 * Returns: TRUE if the core was compiled with alloc
 * tracing enabled.
 */
gboolean
gst_alloc_trace_available (void)
{
#ifdef GST_DISABLE_ALLOC_TRACE
  return FALSE;
#else
  return TRUE;
#endif
}

/**
 * _gst_alloc_trace_register:
 * @name: the name of the new alloc trace object.
 *
 * Register an get a handle to a GstAllocTrace object that
 * can be used to trace memory allocations.
 *
 * Returns: A handle to a GstAllocTrace.
 */
GstAllocTrace *
_gst_alloc_trace_register (const gchar * name)
{
  GstAllocTrace *trace;

  g_return_val_if_fail (name, NULL);

  trace = g_new0 (GstAllocTrace, 1);
  trace->name = g_strdup (name);
  trace->live = 0;
  trace->mem_live = NULL;
  trace->flags = _gst_trace_flags;

  _gst_alloc_tracers = g_list_prepend (_gst_alloc_tracers, trace);

  return trace;
}

/**
 * gst_alloc_trace_list:
 *
 * Get a list of all registered alloc trace objects.
 *
 * Returns: a GList of GstAllocTrace objects.
 */
const GList *
gst_alloc_trace_list (void)
{
  return _gst_alloc_tracers;
}

/**
 * gst_alloc_trace_live_all:
 *
 * Get the total number of live registered alloc trace objects.
 *
 * Returns: the total number of live registered alloc trace objects.
 */
int
gst_alloc_trace_live_all (void)
{
  GList *walk = _gst_alloc_tracers;
  int num = 0;

  while (walk) {
    GstAllocTrace *trace = (GstAllocTrace *) walk->data;

    num += trace->live;

    walk = g_list_next (walk);
  }

  return num;
}

static gint
compare_func (GstAllocTrace * a, GstAllocTrace * b)
{
  return strcmp (a->name, b->name);
}

static GList *
gst_alloc_trace_list_sorted (void)
{
  GList *ret;

  ret = g_list_sort (g_list_copy (_gst_alloc_tracers),
      (GCompareFunc) compare_func);

  return ret;
}

/**
 * gst_alloc_trace_print_all:
 *
 * Print the status of all registered alloc trace objects.
 */
void
gst_alloc_trace_print_all (void)
{
  GList *orig, *walk;

  orig = walk = gst_alloc_trace_list_sorted ();

  while (walk) {
    GstAllocTrace *trace = (GstAllocTrace *) walk->data;

    gst_alloc_trace_print (trace);

    walk = g_list_next (walk);
  }

  g_list_free (orig);
}

/**
 * gst_alloc_trace_print_live:
 *
 * Print the status of all registered alloc trace objects, ignoring those
 * without live objects.
 */
void
gst_alloc_trace_print_live (void)
{
  GList *orig, *walk;

  orig = walk = gst_alloc_trace_list_sorted ();

  while (walk) {
    GstAllocTrace *trace = (GstAllocTrace *) walk->data;

    if (trace->live)
      gst_alloc_trace_print (trace);

    walk = g_list_next (walk);
  }

  g_list_free (orig);
}

/**
 * gst_alloc_trace_set_flags_all:
 * @flags: the options to enable
 *
 * Enable the specified options on all registered alloc trace
 * objects.
 */
void
gst_alloc_trace_set_flags_all (GstAllocTraceFlags flags)
{
  GList *walk = _gst_alloc_tracers;

  while (walk) {
    GstAllocTrace *trace = (GstAllocTrace *) walk->data;

    GST_DEBUG ("setting flags %d on %p", (gint) flags, trace);
    gst_alloc_trace_set_flags (trace, flags);

    walk = g_list_next (walk);
  }
  _gst_trace_flags = flags;
}

/**
 * gst_alloc_trace_get:
 * @name: the name of the alloc trace object
 *
 * Get the named alloc trace object.
 *
 * Returns: a GstAllocTrace with the given name or NULL when
 * no alloc tracer was registered with that name.
 */
GstAllocTrace *
gst_alloc_trace_get (const gchar * name)
{
  GList *walk = _gst_alloc_tracers;

  g_return_val_if_fail (name, NULL);

  while (walk) {
    GstAllocTrace *trace = (GstAllocTrace *) walk->data;

    if (!strcmp (trace->name, name))
      return trace;

    walk = g_list_next (walk);
  }
  return NULL;
}

/**
 * gst_alloc_trace_print:
 * @trace: the GstAllocTrace to print
 *
 * Print the status of the given GstAllocTrace.
 */
void
gst_alloc_trace_print (const GstAllocTrace * trace)
{
  GSList *mem_live;

  g_return_if_fail (trace != NULL);

  if (trace->flags & GST_ALLOC_TRACE_LIVE) {
    g_print ("%-22.22s : %d\n", trace->name, trace->live);
  } else {
    g_print ("%-22.22s : (no live count)\n", trace->name);
  }

  if (trace->flags & GST_ALLOC_TRACE_MEM_LIVE) {
    mem_live = trace->mem_live;

    while (mem_live) {
      g_print ("%-22.22s : %p\n", "", mem_live->data);
      mem_live = mem_live->next;
    }
  }
}

/**
 * gst_alloc_trace_set_flags:
 * @trace: the GstAllocTrace
 * @flags: flags to set
 *
 * Enable the given features on the given GstAllocTrace object.
 */
void
gst_alloc_trace_set_flags (GstAllocTrace * trace, GstAllocTraceFlags flags)
{
  g_return_if_fail (trace != NULL);

  trace->flags = flags;
}