tracerrecord: add a log record class
[platform/upstream/gstreamer.git] / plugins / tracers / gstrusage.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gstrusage.c: tracing module that logs resource usage stats
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 /**
22  * SECTION:gstrusage
23  * @short_description: log resource usage stats
24  *
25  * A tracing module that take rusage() snapshots and logs them.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #include <unistd.h>
33 #include "gstrusage.h"
34 #include <gst/gsttracerrecord.h>
35
36 #ifdef HAVE_SYS_RESOURCE_H
37 #ifndef __USE_GNU
38 # define __USE_GNU              /* RUSAGE_THREAD */
39 #endif
40 #include <sys/resource.h>
41 #endif
42
43 GST_DEBUG_CATEGORY_STATIC (gst_rusage_debug);
44 #define GST_CAT_DEFAULT gst_rusage_debug
45
46 G_LOCK_DEFINE (_proc);
47
48 #define _do_init \
49     GST_DEBUG_CATEGORY_INIT (gst_rusage_debug, "rusage", 0, "rusage tracer");
50 #define gst_rusage_tracer_parent_class parent_class
51 G_DEFINE_TYPE_WITH_CODE (GstRUsageTracer, gst_rusage_tracer, GST_TYPE_TRACER,
52     _do_init);
53
54 /* remember x measurements per self->window */
55 #define WINDOW_SUBDIV 100
56
57 /* number of cpus to scale cpu-usage in threads */
58 static glong num_cpus = 1;
59
60 static GstTracerRecord *tr_proc, *tr_thread;
61
62 typedef struct
63 {
64   /* time spend in this thread */
65   GstClockTime tthread;
66   GstTraceValues *tvs_thread;
67 } GstThreadStats;
68
69 /* data helper */
70
71 static void
72 free_trace_value (gpointer data)
73 {
74   g_slice_free (GstTraceValue, data);
75 }
76
77 static GstTraceValues *
78 make_trace_values (GstClockTime window)
79 {
80   GstTraceValues *self = g_slice_new0 (GstTraceValues);
81   self->window = window;
82   g_queue_init (&self->values);
83   return self;
84 }
85
86 static void
87 free_trace_values (GstTraceValues * self)
88 {
89   g_queue_free_full (&self->values, free_trace_value);
90   g_slice_free (GstTraceValues, self);
91 }
92
93 static gboolean
94 update_trace_value (GstTraceValues * self, GstClockTime nts,
95     GstClockTime nval, GstClockTime * dts, GstClockTime * dval)
96 {
97   GstTraceValue *lv;
98   GstClockTimeDiff dt;
99   GstClockTime window = self->window;
100   GQueue *q = &self->values;
101   GList *node = q->tail;
102   gboolean ret = FALSE;
103
104
105   /* search from the tail of the queue for a good GstTraceValue */
106   while (node) {
107     lv = node->data;
108     dt = GST_CLOCK_DIFF (lv->ts, nts);
109     if (dt < window) {
110       break;
111     } else {
112       node = g_list_previous (node);
113     }
114   }
115
116   if (node) {
117     /* calculate the windowed value */
118     *dts = dt;
119     *dval = GST_CLOCK_DIFF (lv->val, nval);
120
121     /* drop all older measurements */
122     while (q->tail != node) {
123       free_trace_value (g_queue_pop_tail (q));
124     }
125     ret = TRUE;
126   } else {
127     *dts = nts;
128     *dval = nval;
129   }
130
131   /* don't push too many data items */
132   lv = q->head ? q->head->data : NULL;
133   if (!lv || (GST_CLOCK_DIFF (lv->ts, nts) > (window / WINDOW_SUBDIV))) {
134     /* push the new measurement */
135     lv = g_slice_new0 (GstTraceValue);
136     lv->ts = nts;
137     lv->val = nval;
138     g_queue_push_head (q, lv);
139   }
140   return ret;
141 }
142
143
144 static void
145 free_thread_stats (gpointer data)
146 {
147   free_trace_values (((GstThreadStats *) data)->tvs_thread);
148   g_slice_free (GstThreadStats, data);
149 }
150
151 static void
152 do_stats (GstTracer * obj, guint64 ts)
153 {
154   GstRUsageTracer *self = GST_RUSAGE_TRACER_CAST (obj);
155   GstThreadStats *stats;
156   gpointer thread_id = g_thread_self ();
157   guint avg_cpuload, cur_cpuload;
158   struct rusage ru;
159   GstClockTime tproc = G_GUINT64_CONSTANT (0);
160   GstClockTime tthread = G_GUINT64_CONSTANT (0);
161   GstClockTime dts, dtproc;
162
163 #ifdef HAVE_CLOCK_GETTIME
164   {
165     struct timespec now;
166
167     if (!clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &now)) {
168       tproc = GST_TIMESPEC_TO_TIME (now);
169     } else {
170       GST_WARNING_OBJECT (obj,
171           "clock_gettime (CLOCK_PROCESS_CPUTIME_ID,...) failed: %s",
172           g_strerror (errno));
173       getrusage (RUSAGE_SELF, &ru);
174       tproc =
175           GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
176     }
177
178     /* cpu time per thread */
179     if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &now)) {
180       tthread = GST_TIMESPEC_TO_TIME (now);
181     } else {
182       GST_WARNING_OBJECT (obj,
183           "clock_gettime (CLOCK_THREAD_CPUTIME_ID,...) failed: %s",
184           g_strerror (errno));
185 #ifdef RUSAGE_THREAD
186       getrusage (RUSAGE_THREAD, &ru);
187       tthread =
188           GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
189 #endif
190     }
191   }
192 #else
193   getrusage (RUSAGE_SELF, &ru);
194   tproc = GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
195 #ifdef RUSAGE_THREAD
196   getrusage (RUSAGE_THREAD, &ru);
197   tthread =
198       GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
199 #endif
200 #endif
201   /* get stats record for current thread */
202   if (!(stats = g_hash_table_lookup (self->threads, thread_id))) {
203     stats = g_slice_new0 (GstThreadStats);
204     stats->tvs_thread = make_trace_values (GST_SECOND);
205     g_hash_table_insert (self->threads, thread_id, stats);
206   }
207   stats->tthread = tthread;
208
209   /* Calibrate ts for the process and main thread. For tthread[main] and tproc
210    * the time is larger than ts, as our base-ts is taken after the process has
211    * started.
212    */
213   if (G_UNLIKELY (thread_id == self->main_thread_id)) {
214     self->main_thread_id = NULL;
215     /* when the registry gets updated, the tproc is less than the debug time ? */
216     /* TODO(ensonic): we still see cases where tproc overtakes ts, especially
217      * when with sync=false, can this be due to multiple cores in use? */
218     if (tproc > ts) {
219       self->tproc_base = tproc - ts;
220       GST_DEBUG ("rusage: calibrating by %" G_GUINT64_FORMAT ", thread: %"
221           G_GUINT64_FORMAT ", proc: %" G_GUINT64_FORMAT,
222           self->tproc_base, stats->tthread, tproc);
223       stats->tthread -= self->tproc_base;
224     }
225   }
226   /* we always need to correct proc time */
227   tproc -= self->tproc_base;
228
229   /* FIXME: how can we take cpu-frequency scaling into account?
230    * - looking at /sys/devices/system/cpu/cpu0/cpufreq/
231    *   scale_factor=scaling_max_freq/scaling_cur_freq
232    * - as a workaround we can switch it via /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
233    *   cpufreq-selector -g performance
234    *   cpufreq-selector -g ondemand
235    */
236   /* *INDENT-OFF* */
237   avg_cpuload = (guint) gst_util_uint64_scale (stats->tthread,
238       G_GINT64_CONSTANT (1000), ts);
239   update_trace_value (stats->tvs_thread, ts, stats->tthread, &dts, &dtproc);
240   cur_cpuload = (guint) gst_util_uint64_scale (dtproc,
241       G_GINT64_CONSTANT (1000), dts);
242   gst_tracer_record_log (tr_thread, (guint64)thread_id, ts,
243       MIN (avg_cpuload, 1000), MIN (cur_cpuload, 1000), stats->tthread);
244
245   avg_cpuload = (guint) gst_util_uint64_scale (tproc / num_cpus,
246       G_GINT64_CONSTANT (1000), ts);
247   G_LOCK (_proc);
248   update_trace_value (self->tvs_proc, ts, tproc, &dts, &dtproc);
249   G_UNLOCK (_proc);
250   cur_cpuload = (guint) gst_util_uint64_scale (dtproc / num_cpus,
251       G_GINT64_CONSTANT (1000), dts);
252   gst_tracer_record_log (tr_proc, ts, MIN (avg_cpuload, 1000),
253       MIN (cur_cpuload, 1000), tproc);
254   /* *INDENT-ON* */
255 }
256
257 /* tracer class */
258
259 static void
260 gst_rusage_tracer_finalize (GObject * obj)
261 {
262   GstRUsageTracer *self = GST_RUSAGE_TRACER (obj);
263
264   g_hash_table_destroy (self->threads);
265   free_trace_values (self->tvs_proc);
266
267   G_OBJECT_CLASS (parent_class)->finalize (obj);
268 }
269
270 static void
271 gst_rusage_tracer_class_init (GstRUsageTracerClass * klass)
272 {
273   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
274
275   gobject_class->finalize = gst_rusage_tracer_finalize;
276
277   if ((num_cpus = sysconf (_SC_NPROCESSORS_ONLN)) == -1) {
278     GST_WARNING ("failed to get number of cpus online");
279     if ((num_cpus = sysconf (_SC_NPROCESSORS_CONF)) == -1) {
280       GST_WARNING ("failed to get number of cpus, assuming 1");
281       num_cpus = 1;
282     }
283   }
284   GST_DEBUG ("rusage: num_cpus=%ld", num_cpus);
285
286   /* announce trace formats */
287   /* *INDENT-OFF* */
288   tr_thread = gst_tracer_record_new (gst_structure_new ("thread-rusage.class",
289       "thread-id", GST_TYPE_STRUCTURE, gst_structure_new ("scope",
290           "type", G_TYPE_GTYPE, G_TYPE_UINT64,
291           "related-to", G_TYPE_STRING, "thread",  /* TODO: use genum */
292           NULL),
293       "ts", GST_TYPE_STRUCTURE, gst_structure_new ("value",
294           "type", G_TYPE_GTYPE, G_TYPE_UINT64,
295           "description", G_TYPE_STRING, "event ts",
296           NULL),
297       "average-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
298           "type", G_TYPE_GTYPE, G_TYPE_UINT,
299           "description", G_TYPE_STRING, "average cpu usage per thread in ‰",
300           "flags", G_TYPE_STRING, "aggregated",  /* TODO: use gflags */
301           "min", G_TYPE_UINT, 0,
302           "max", G_TYPE_UINT, 1000,
303           NULL),
304       "current-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
305           "type", G_TYPE_GTYPE, G_TYPE_UINT,
306           "description", G_TYPE_STRING, "current cpu usage per thread in ‰",
307           "flags", G_TYPE_STRING, "windowed",  /* TODO: use gflags */
308           "min", G_TYPE_UINT, 0,
309           "max", G_TYPE_UINT, 1000,
310           NULL),
311       "time", GST_TYPE_STRUCTURE, gst_structure_new ("value",
312           "type", G_TYPE_GTYPE, G_TYPE_UINT64,
313           "description", G_TYPE_STRING, "time spent in thread in ns",
314           "flags", G_TYPE_STRING, "aggregated",  /* TODO: use gflags */
315           "min", G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
316           "max", G_TYPE_UINT64, G_MAXUINT64,
317           NULL),
318       NULL));
319   tr_proc = gst_tracer_record_new (gst_structure_new ("proc-rusage.class",
320       "thread-id", GST_TYPE_STRUCTURE, gst_structure_new ("scope",
321           "type", G_TYPE_GTYPE, G_TYPE_UINT64,
322           "related-to", G_TYPE_STRING, "process",  /* TODO: use genum */
323           NULL),
324       "ts", GST_TYPE_STRUCTURE, gst_structure_new ("value",
325           "type", G_TYPE_GTYPE, G_TYPE_UINT64,
326           "description", G_TYPE_STRING, "event ts",
327           NULL),
328       "average-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
329           "type", G_TYPE_GTYPE, G_TYPE_UINT,
330           "description", G_TYPE_STRING, "average cpu usage per process in ‰",
331           "flags", G_TYPE_STRING, "aggregated",  /* TODO: use gflags */
332           "min", G_TYPE_UINT, 0,
333           "max", G_TYPE_UINT, 1000,
334           NULL),
335       "current-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
336           "type", G_TYPE_GTYPE, G_TYPE_UINT,
337           "description", G_TYPE_STRING, "current cpu usage per process in ‰",
338           "flags", G_TYPE_STRING, "windowed",  /* TODO: use gflags */
339           "min", G_TYPE_UINT, 0,
340           "max", G_TYPE_UINT, 1000,
341           NULL),
342       "time", GST_TYPE_STRUCTURE, gst_structure_new ("value",
343           "type", G_TYPE_GTYPE, G_TYPE_UINT64,
344           "description", G_TYPE_STRING, "time spent in process in ns",
345           "flags", G_TYPE_STRING, "aggregated",  /* TODO: use gflags */
346           "min", G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
347           "max", G_TYPE_UINT64, G_MAXUINT64,
348           NULL),
349       NULL));
350   /* *INDENT-ON* */
351 }
352
353 static void
354 gst_rusage_tracer_init (GstRUsageTracer * self)
355 {
356   GstTracer *tracer = GST_TRACER (self);
357
358   gst_tracing_register_hook (tracer, NULL, G_CALLBACK (do_stats));
359
360   self->threads = g_hash_table_new_full (NULL, NULL, NULL, free_thread_stats);
361   self->tvs_proc = make_trace_values (GST_SECOND);
362   self->main_thread_id = g_thread_self ();
363
364   GST_DEBUG ("rusage: main thread=%p", self->main_thread_id);
365 }