- basic arch check for HAVE_RDTSC
[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 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <string.h>
30
31 #include "gst_private.h"
32
33 #include "gsttrace.h"
34
35
36 __inline__ void read_tsc(guint64 *dst) {
37 #ifdef HAVE_RDTSC
38   guint64 tsc;
39   __asm__ __volatile__ ("rdtsc" : "=A" (tsc));
40   *dst = tsc;
41 #else
42   *dst = 0;
43 #endif
44 }
45
46 void gst_trace_read_tsc(guint64 *dst) {
47   read_tsc(dst);
48 }
49
50 GstTrace *_gst_trace_default = NULL;
51 gint _gst_trace_on = 1;
52
53 GstTrace *gst_trace_new(guchar *filename,gint size) {
54   GstTrace *trace = g_malloc(sizeof(GstTrace));
55
56   g_return_val_if_fail(trace != NULL,NULL);
57   trace->filename = g_strdup(filename);
58   g_print("opening '%s'\n",trace->filename);
59   trace->fd = open(trace->filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
60   perror("opening trace file");
61   g_return_val_if_fail(trace->fd > 0,NULL);
62   trace->buf = g_malloc(size * sizeof(GstTraceEntry));
63   g_return_val_if_fail(trace->buf != NULL,NULL);
64   trace->bufsize = size;
65   trace->bufoffset = 0;
66
67   return trace;
68 }
69
70 void gst_trace_destroy(GstTrace *trace) {
71   g_return_if_fail(trace != NULL);
72   g_return_if_fail(trace->buf != NULL);
73
74   if (gst_trace_get_remaining(trace) > 0)
75     gst_trace_flush(trace);
76   close(trace->fd);
77   g_free(trace->buf);
78   g_free(trace);
79 }
80
81 void gst_trace_flush(GstTrace *trace) {
82   if (!trace) {
83     trace = _gst_trace_default;
84     if (!trace ) return;
85   }
86
87   write(trace->fd,trace->buf,trace->bufoffset * sizeof(GstTraceEntry));
88   trace->bufoffset = 0;
89 }
90
91 void gst_trace_text_flush(GstTrace *trace) {
92   int i;
93   const int strsize = 20+1 + 10+1 + 10+1 + 112+1 + 1;
94   char str[strsize];
95
96   if (!trace) {
97     trace = _gst_trace_default;
98     if (!trace ) return;
99   }
100
101   for (i=0; i<trace->bufoffset; i++) {
102     snprintf(str, strsize, "%20lld %10d %10d %s\n",
103         trace->buf[i].timestamp,
104         trace->buf[i].sequence,
105         trace->buf[i].data,
106         trace->buf[i].message);
107     write(trace->fd,str,strlen(str));
108   }
109   trace->bufoffset = 0;
110 }
111
112 void gst_trace_set_default(GstTrace *trace) {
113   g_return_if_fail(trace != NULL);
114   _gst_trace_default = trace;
115 }
116
117 void _gst_trace_add_entry(GstTrace *trace,guint32 seq,guint32 data,gchar *msg) {
118   GstTraceEntry *entry;
119   if (!trace) {
120     trace = _gst_trace_default;
121     if (!trace ) return;
122   }
123
124   entry = trace->buf + trace->bufoffset;
125   read_tsc(&(entry->timestamp));
126   entry->sequence = seq;
127   entry->data = data;
128   strncpy(entry->message,msg,112);
129   trace->bufoffset++;
130
131   gst_trace_flush(trace);
132 }