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
|
/* GStreamer preload library
* Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
*
* 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.
*/
/* TODO:
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/resource.h>
#include <glib.h>
#include <gst/gst.h>
/* log variables */
static gchar *gstpl_log_name;
/* global statistics */
#if 0
GHashTable *buffers_per_pad = NULL;
#endif
GHashTable *threads = NULL;
guint num_threads = 0;
guint max_cpuload = 0;
guint max_qos = 0;
gboolean log_cpu = FALSE;
/* utillity stuff */
GstClockTime _priv_start_time=G_GUINT64_CONSTANT(0);
static GstClockTime
_get_timestamp (void)
{
struct timespec now;
clock_gettime (CLOCK_MONOTONIC, &now);
return GST_TIMESPEC_TO_TIME (now);
/*
GTimeVal now;
g_get_current_time (&now);
return GST_TIMEVAL_TO_TIME (now);
*/
}
FILE *lf = NULL;
static void
_log_entry (const gchar *fmt, ...)
{
va_list ap;
if (G_UNLIKELY(!lf)) {
if(!(lf = fopen (gstpl_log_name, "w"))) {
fprintf (stderr, "Can't open log file %s : %s", gstpl_log_name, strerror (errno));
return;
}
}
va_start(ap, fmt);
vfprintf (lf, fmt, ap);
va_end(ap);
}
static void
_log_common (GstClockTimeDiff treal)
{
guint cpuload;
struct rusage ru;
GstClockTimeDiff tuser, tsys;
// self is PID, so this is for all threads
getrusage (RUSAGE_SELF,&ru);
tuser = GST_TIMEVAL_TO_TIME (ru.ru_utime);
tsys = GST_TIMEVAL_TO_TIME (ru.ru_stime);
if (log_cpu) {
// we only want cpu-load in playing
cpuload = (guint) gst_util_uint64_scale (tuser+tsys, G_GINT64_CONSTANT(100), treal);
if (cpuload <= 100) {
if (cpuload > max_cpuload) max_cpuload = cpuload;
}
else {
// this happens during prerolling
printf("%" GST_TIME_FORMAT " %u\n", GST_TIME_ARGS (treal), cpuload);
}
_log_entry(
"rusage %" GST_TIME_FORMAT " %" G_GINT64_FORMAT " %u\n",
GST_TIME_ARGS (treal), treal, cpuload);
}
gpointer thread_id = g_thread_self();
if (!g_hash_table_lookup (threads, thread_id)) {
/* FIXME: store some stats for each thread later */
g_hash_table_insert (threads, thread_id, (gpointer)1);
num_threads++;
}
}
static const gchar*
_get_pad_name (GstPad *pad)
{
static GHashTable *pad_names = NULL;
gchar *pad_name;
if (G_UNLIKELY(!pad_names)) {
pad_names = g_hash_table_new (NULL,NULL);
}
if( !(pad_name = g_hash_table_lookup (pad_names, pad))) {
// yes we leak thoses ...
pad_name = g_strdup_printf ("%s_%s", GST_DEBUG_PAD_NAME (pad));
g_hash_table_insert (pad_names, pad, (gpointer)pad_name);
//printf ("new pad_name : %p -> %s\n",pad,pad_name);
}
return pad_name;
}
/* wrapped api */
static GstFlowReturn (*orig_pad_push) (GstPad *,GstBuffer *) = NULL;
GstFlowReturn
gst_pad_push (GstPad *pad, GstBuffer *buffer)
{
GstClockTimeDiff elapsed = GST_CLOCK_DIFF (_priv_start_time, _get_timestamp ());
const gchar *pad_name = _get_pad_name (pad);
_log_entry(
"pad_%s %" GST_TIME_FORMAT " %" G_GINT64_FORMAT " %u\n",
pad_name, GST_TIME_ARGS (elapsed), elapsed, GST_BUFFER_SIZE(buffer));
_log_common (elapsed);
return orig_pad_push (pad, buffer);
}
static gboolean (*orig_pad_get_range) (GstPad *, guint64, guint, GstBuffer **) = NULL;
GstFlowReturn
gst_pad_get_range (GstPad * pad, guint64 offset, guint size, GstBuffer ** buffer)
{
GstClockTimeDiff elapsed = GST_CLOCK_DIFF (_priv_start_time, _get_timestamp ());
_log_entry(
"pad_%s %" GST_TIME_FORMAT " %" G_GINT64_FORMAT " %u %"G_GUINT64_FORMAT"\n",
_get_pad_name (pad), GST_TIME_ARGS (elapsed), elapsed, size, offset);
_log_common (elapsed);
return orig_pad_get_range (pad, offset, size, buffer);
}
static gboolean (*orig_pad_send_event) (GstPad *, GstEvent *);
gboolean
gst_pad_send_event (GstPad *pad, GstEvent *event)
{
GstClockTimeDiff elapsed = GST_CLOCK_DIFF (_priv_start_time, _get_timestamp ());
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_QOS: {
gdouble proportion;
guint qos;
gst_event_parse_qos (event, &proportion, NULL, NULL);
qos = (guint)(proportion * 100);
if (qos > max_qos) max_qos = qos;
_log_entry(
"qos %" GST_TIME_FORMAT " %" G_GINT64_FORMAT " %u\n",
GST_TIME_ARGS (elapsed), elapsed, qos);
} break;
}
log_cpu = (GST_STATE (GST_PAD_PARENT (pad)) == GST_STATE_PLAYING);
_log_common(elapsed);
return orig_pad_send_event (pad, event);
}
/* init/free */
static void
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
__attribute__ ((constructor))
#endif /* !__GNUC__ */
gst_preload_init (void)
{
void *lib_gstreamer;
gchar *errstr;
if (!g_thread_supported ())
g_thread_init (NULL);
g_type_init ();
gst_init(NULL,NULL);
/* get configuration */
if(!(gstpl_log_name = getenv ("GSTPL_LOG_NAME"))) {
gstpl_log_name = "/tmp/gstpl.log";
}
/* init structures to gather stats */
threads = g_hash_table_new (NULL,NULL);
/* load library (abort if not found) */
lib_gstreamer = dlopen ("libgstreamer-0.10.so", RTLD_LAZY);
if (!lib_gstreamer) {
errstr = dlerror ();
g_critical ("Failed to load gstreamer library: %s, aborting..",
errstr ? errstr : "<No error details>");
exit (1);
}
/* get pointer to original functions to chain up */
if (!(orig_pad_push = dlsym (lib_gstreamer, "gst_pad_push"))) {
errstr = dlerror ();
g_critical ("Failed to find gst_pad_push() in gstreamer library: %s, aborting..",
errstr ? errstr : "<Symbol is NULL>");
exit (1);
}
if (!(orig_pad_get_range = dlsym (lib_gstreamer, "gst_pad_get_range"))) {
errstr = dlerror ();
g_critical ("Failed to find gst_pad_get_range() in gstreamer library: %s, aborting..",
errstr ? errstr : "<Symbol is NULL>");
exit (1);
}
if (!(orig_pad_send_event = dlsym (lib_gstreamer, "gst_pad_send_event"))) {
errstr = dlerror ();
g_critical ("Failed to find gst_pad_send_event() in gstreamer library: %s, aborting..",
errstr ? errstr : "<Symbol is NULL>");
exit (1);
}
_priv_start_time = _get_timestamp ();
}
static void
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
__attribute__ ((destructor))
#endif /* !__GNUC__ */
gst_preload_done (void)
{
/* print overall stats */
puts("Statistics:");
printf("Number of threads : %u\n", num_threads);
printf("Max CPU load : %u %%\n", max_cpuload);
printf("Max QOS load : %u %%\n", max_qos);
/* free ressources */
if(lf) fclose(lf);
g_hash_table_destroy (threads);
}
|