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
|
/* GStreamer
* Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
*
* gstrusage.c: tracing module that logs resource usage stats
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:tracer-rusage
* @short_description: log resource usage stats
*
* A tracing module that take `rusage()` snapshots and logs them.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <unistd.h>
#include "gstrusage.h"
#ifdef HAVE_SYS_RESOURCE_H
#ifndef __USE_GNU
# define __USE_GNU /* RUSAGE_THREAD */
#endif
#include <sys/resource.h>
#endif
GST_DEBUG_CATEGORY_STATIC (gst_rusage_debug);
#define GST_CAT_DEFAULT gst_rusage_debug
G_LOCK_DEFINE (_proc);
#define _do_init \
GST_DEBUG_CATEGORY_INIT (gst_rusage_debug, "rusage", 0, "rusage tracer");
#define gst_rusage_tracer_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstRUsageTracer, gst_rusage_tracer, GST_TYPE_TRACER,
_do_init);
/* remember x measurements per self->window */
#define WINDOW_SUBDIV 100
/* number of cpus to scale cpu-usage in threads */
static glong num_cpus = 1;
static GstTracerRecord *tr_proc, *tr_thread;
typedef struct
{
/* time spent in this thread */
GstClockTime tthread;
GstTraceValues *tvs_thread;
} GstThreadStats;
static void free_thread_stats (gpointer data);
static GPrivate thread_stats_key = G_PRIVATE_INIT (free_thread_stats);
/* data helper */
static void
free_trace_value (gpointer data)
{
g_free (data);
}
static GstTraceValues *
make_trace_values (GstClockTime window)
{
GstTraceValues *self = g_new0 (GstTraceValues, 1);
self->window = window;
g_queue_init (&self->values);
return self;
}
static void
free_trace_values (GstTraceValues * self)
{
g_queue_foreach (&self->values, (GFunc) free_trace_value, NULL);
g_queue_clear (&self->values);
g_free (self);
}
static gboolean
update_trace_value (GstTraceValues * self, GstClockTime nts,
GstClockTime nval, GstClockTime * dts, GstClockTime * dval)
{
GstTraceValue *lv;
GstClockTimeDiff dt;
GstClockTime window = self->window;
GQueue *q = &self->values;
GList *node = q->tail;
gboolean ret = FALSE;
/* search from the tail of the queue for a good GstTraceValue */
while (node) {
lv = node->data;
dt = GST_CLOCK_DIFF (lv->ts, nts);
if (dt < window) {
break;
} else {
node = g_list_previous (node);
}
}
if (node) {
/* calculate the windowed value */
*dts = dt;
*dval = GST_CLOCK_DIFF (lv->val, nval);
/* drop all older measurements */
while (q->tail != node) {
free_trace_value (g_queue_pop_tail (q));
}
ret = TRUE;
} else {
*dts = nts;
*dval = nval;
}
/* don't push too many data items */
lv = q->head ? q->head->data : NULL;
if (!lv || (GST_CLOCK_DIFF (lv->ts, nts) > (window / WINDOW_SUBDIV))) {
/* push the new measurement */
lv = g_new0 (GstTraceValue, 1);
lv->ts = nts;
lv->val = nval;
g_queue_push_head (q, lv);
}
return ret;
}
static void
free_thread_stats (gpointer data)
{
GstThreadStats *stats = data;
free_trace_values (stats->tvs_thread);
g_free (stats);
}
static void
do_stats (GstTracer * obj, guint64 ts)
{
GstRUsageTracer *self = GST_RUSAGE_TRACER_CAST (obj);
GstThreadStats *stats;
gpointer thread_id = g_thread_self ();
guint avg_cpuload, cur_cpuload;
struct rusage ru;
GstClockTime tproc = G_GUINT64_CONSTANT (0);
GstClockTime tthread = G_GUINT64_CONSTANT (0);
GstClockTime dts, dtproc;
#ifdef HAVE_CLOCK_GETTIME
{
struct timespec now;
if (!clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &now)) {
tproc = GST_TIMESPEC_TO_TIME (now);
} else {
GST_WARNING_OBJECT (obj,
"clock_gettime (CLOCK_PROCESS_CPUTIME_ID,...) failed: %s",
g_strerror (errno));
getrusage (RUSAGE_SELF, &ru);
tproc =
GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
}
/* cpu time per thread */
if (!clock_gettime (CLOCK_THREAD_CPUTIME_ID, &now)) {
tthread = GST_TIMESPEC_TO_TIME (now);
} else {
GST_WARNING_OBJECT (obj,
"clock_gettime (CLOCK_THREAD_CPUTIME_ID,...) failed: %s",
g_strerror (errno));
#ifdef RUSAGE_THREAD
getrusage (RUSAGE_THREAD, &ru);
tthread =
GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
#endif
}
}
#else
getrusage (RUSAGE_SELF, &ru);
tproc = GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
#ifdef RUSAGE_THREAD
getrusage (RUSAGE_THREAD, &ru);
tthread =
GST_TIMEVAL_TO_TIME (ru.ru_utime) + GST_TIMEVAL_TO_TIME (ru.ru_stime);
#endif
#endif
/* get stats record for current thread */
if (!(stats = g_private_get (&thread_stats_key))) {
stats = g_new0 (GstThreadStats, 1);
stats->tvs_thread = make_trace_values (GST_SECOND);
g_private_set (&thread_stats_key, stats);
}
stats->tthread = tthread;
/* Calibrate ts for the process and main thread. For tthread[main] and tproc
* the time is larger than ts, as our base-ts is taken after the process has
* started.
*/
if (G_UNLIKELY (thread_id == self->main_thread_id)) {
self->main_thread_id = NULL;
/* when the registry gets updated, the tproc is less than the debug time ? */
/* TODO(ensonic): we still see cases where tproc overtakes ts, especially
* when with sync=false, can this be due to multiple cores in use? */
if (tproc > ts) {
self->tproc_base = tproc - ts;
GST_DEBUG ("rusage: calibrating by %" G_GUINT64_FORMAT ", thread: %"
G_GUINT64_FORMAT ", proc: %" G_GUINT64_FORMAT,
self->tproc_base, stats->tthread, tproc);
stats->tthread -= self->tproc_base;
}
}
/* we always need to correct proc time */
tproc -= self->tproc_base;
/* FIXME: how can we take cpu-frequency scaling into account?
* - looking at /sys/devices/system/cpu/cpu0/cpufreq/
* scale_factor=scaling_max_freq/scaling_cur_freq
* - as a workaround we can switch it via /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
* cpufreq-selector -g performance
* cpufreq-selector -g ondemand
*/
/* *INDENT-OFF* */
avg_cpuload = (guint) gst_util_uint64_scale (stats->tthread,
G_GINT64_CONSTANT (1000), ts);
update_trace_value (stats->tvs_thread, ts, stats->tthread, &dts, &dtproc);
cur_cpuload = (guint) gst_util_uint64_scale (dtproc,
G_GINT64_CONSTANT (1000), dts);
gst_tracer_record_log (tr_thread, (guint64) (guintptr) thread_id, ts,
MIN (avg_cpuload, 1000), MIN (cur_cpuload, 1000), stats->tthread);
avg_cpuload = (guint) gst_util_uint64_scale (tproc / num_cpus,
G_GINT64_CONSTANT (1000), ts);
G_LOCK (_proc);
update_trace_value (self->tvs_proc, ts, tproc, &dts, &dtproc);
G_UNLOCK (_proc);
cur_cpuload = (guint) gst_util_uint64_scale (dtproc / num_cpus,
G_GINT64_CONSTANT (1000), dts);
gst_tracer_record_log (tr_proc, (guint64) getpid (), ts,
MIN (avg_cpuload, 1000), MIN (cur_cpuload, 1000), tproc);
/* *INDENT-ON* */
}
/* tracer class */
static void
gst_rusage_tracer_finalize (GObject * obj)
{
GstRUsageTracer *self = GST_RUSAGE_TRACER (obj);
free_trace_values (self->tvs_proc);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
static void
gst_rusage_tracer_class_init (GstRUsageTracerClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gst_tracer_class_set_use_structure_params (GST_TRACER_CLASS (klass), TRUE);
gobject_class->finalize = gst_rusage_tracer_finalize;
if ((num_cpus = sysconf (_SC_NPROCESSORS_ONLN)) == -1) {
GST_WARNING ("failed to get number of cpus online");
if ((num_cpus = sysconf (_SC_NPROCESSORS_CONF)) == -1) {
GST_WARNING ("failed to get number of cpus, assuming 1");
num_cpus = 1;
}
}
GST_DEBUG ("rusage: num_cpus=%ld", num_cpus);
/* announce trace formats */
/* *INDENT-OFF* */
tr_thread = gst_tracer_record_new ("thread-rusage.class",
"thread-id", GST_TYPE_STRUCTURE, gst_structure_new ("scope",
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
"related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_THREAD,
NULL),
"ts", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
"description", G_TYPE_STRING, "event ts",
NULL),
"average-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT,
"description", G_TYPE_STRING, "average cpu usage per thread in ‰",
"flags", GST_TYPE_TRACER_VALUE_FLAGS, GST_TRACER_VALUE_FLAGS_AGGREGATED,
"min", G_TYPE_UINT, 0,
"max", G_TYPE_UINT, 1000,
NULL),
"current-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT,
"description", G_TYPE_STRING, "current cpu usage per thread in ‰",
"min", G_TYPE_UINT, 0,
"max", G_TYPE_UINT, 1000,
NULL),
"time", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
"description", G_TYPE_STRING, "time spent in thread in ns",
"flags", GST_TYPE_TRACER_VALUE_FLAGS, GST_TRACER_VALUE_FLAGS_AGGREGATED,
"min", G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
"max", G_TYPE_UINT64, G_MAXUINT64,
NULL),
NULL);
tr_proc = gst_tracer_record_new ("proc-rusage.class",
"process-id", GST_TYPE_STRUCTURE, gst_structure_new ("scope",
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
"related-to", GST_TYPE_TRACER_VALUE_SCOPE, GST_TRACER_VALUE_SCOPE_PROCESS,
NULL),
"ts", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
"description", G_TYPE_STRING, "event ts",
NULL),
"average-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT,
"description", G_TYPE_STRING, "average cpu usage per process in ‰",
"flags", GST_TYPE_TRACER_VALUE_FLAGS, GST_TRACER_VALUE_FLAGS_AGGREGATED,
"min", G_TYPE_UINT, 0,
"max", G_TYPE_UINT, 1000,
NULL),
"current-cpuload", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT,
"description", G_TYPE_STRING, "current cpu usage per process in ‰",
"min", G_TYPE_UINT, 0,
"max", G_TYPE_UINT, 1000,
NULL),
"time", GST_TYPE_STRUCTURE, gst_structure_new ("value",
"type", G_TYPE_GTYPE, G_TYPE_UINT64,
"description", G_TYPE_STRING, "time spent in process in ns",
"flags", GST_TYPE_TRACER_VALUE_FLAGS, GST_TRACER_VALUE_FLAGS_AGGREGATED,
"min", G_TYPE_UINT64, G_GUINT64_CONSTANT (0),
"max", G_TYPE_UINT64, G_MAXUINT64,
NULL),
NULL);
/* *INDENT-ON* */
GST_OBJECT_FLAG_SET (tr_thread, GST_OBJECT_FLAG_MAY_BE_LEAKED);
GST_OBJECT_FLAG_SET (tr_proc, GST_OBJECT_FLAG_MAY_BE_LEAKED);
}
static void
gst_rusage_tracer_init (GstRUsageTracer * self)
{
GstTracer *tracer = GST_TRACER (self);
gint i;
const gchar *hooks[] = { "pad-push-pre", "pad-push-post", "pad-push-list-pre",
"pad-push-list-post", "pad-pull-range-pre", "pad-pull-range-post",
"pad-push-event-pre", "pad-push-event-post", "pad-query-pre",
"pad-query-post", "element-post-message-pre", "element-post-message-post",
"element-query-pre", "element-query-post", "element-new", "element-add-pad",
"element-remove-pad", "element-change-state-pre",
"element-change-state-post", "bin-add-pre", "bin-add-post",
"bin-remove-pre", "bin-remove-post", "pad-link-pre", "pad-link-post",
"pad-unlink-pre", "pad-unlink-post"
};
for (i = 0; i < G_N_ELEMENTS (hooks); i++) {
gst_tracing_register_hook (tracer, hooks[i], G_CALLBACK (do_stats));
}
self->tvs_proc = make_trace_values (GST_SECOND);
self->main_thread_id = g_thread_self ();
GST_DEBUG ("rusage: main thread=%p", self->main_thread_id);
}
|