1 /*****************************************************************************
2 * stats.c: Statistics handling
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <stdio.h> /* required */
35 #include "input/input_internal.h"
37 /*****************************************************************************
39 *****************************************************************************/
40 static int CounterUpdate( vlc_object_t *p_this,
42 vlc_value_t val, vlc_value_t * );
43 static void TimerDump( vlc_object_t *p_this, counter_t *p_counter, bool);
45 /*****************************************************************************
47 *****************************************************************************/
50 * Create a statistics counter
51 * \param p_this a VLC object
52 * \param i_type the type of stored data. One of VLC_VAR_STRING,
53 * VLC_VAR_INTEGER, VLC_VAR_FLOAT
54 * \param i_compute_type the aggregation type. One of STATS_LAST (always
55 * keep the last value), STATS_COUNTER (increment by the passed value),
56 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
57 * (keep a time derivative of the value)
59 counter_t * __stats_CounterCreate( vlc_object_t *p_this,
60 int i_type, int i_compute_type )
62 counter_t *p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
65 if( !p_counter ) return NULL;
66 p_counter->i_compute_type = i_compute_type;
67 p_counter->i_type = i_type;
68 p_counter->i_samples = 0;
69 p_counter->pp_samples = NULL;
70 p_counter->psz_name = NULL;
72 p_counter->update_interval = 0;
73 p_counter->last_update = 0;
78 /** Update a counter element with new values
79 * \param p_this a VLC object
80 * \param p_counter the counter to update
81 * \param val the vlc_value union containing the new value to aggregate. For
82 * more information on how data is aggregated, \see __stats_Create
83 * \param val_new a pointer that will be filled with new data
85 int __stats_Update( vlc_object_t *p_this, counter_t *p_counter,
86 vlc_value_t val, vlc_value_t *val_new )
88 if( !libvlc_stats (p_this) || !p_counter ) return VLC_EGENERIC;
89 return CounterUpdate( p_this, p_counter, val, val_new );
92 /** Get the aggregated value for a counter
93 * \param p_this an object
94 * \param p_counter the counter
95 * \param val a pointer to an initialized vlc_value union. It will contain the
97 * \return an error code
99 int __stats_Get( vlc_object_t *p_this, counter_t *p_counter, vlc_value_t *val )
101 if( !libvlc_stats (p_this) || !p_counter || p_counter->i_samples == 0 )
103 val->i_int = val->f_float = 0.0;
107 switch( p_counter->i_compute_type )
113 *val = p_counter->pp_samples[0]->value;
115 case STATS_DERIVATIVE:
117 if( p_counter->i_samples < 2 )
119 val->i_int = 0; val->f_float = 0.0;
122 if( p_counter->i_type == VLC_VAR_INTEGER )
124 float f = ( p_counter->pp_samples[0]->value.i_int -
125 p_counter->pp_samples[1]->value.i_int ) /
126 (float)( p_counter->pp_samples[0]->date -
127 p_counter->pp_samples[1]->date );
132 float f = (float)( p_counter->pp_samples[0]->value.f_float -
133 p_counter->pp_samples[1]->value.f_float ) /
134 (float)( p_counter->pp_samples[0]->date -
135 p_counter->pp_samples[1]->date );
143 input_stats_t *stats_NewInputStats( input_thread_t *p_input )
146 input_stats_t *p_stats = calloc( 1, sizeof(input_stats_t) );
150 vlc_mutex_init( &p_stats->lock );
151 stats_ReinitInputStats( p_stats );
156 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
158 if( !libvlc_stats (p_input) ) return;
160 vlc_mutex_lock( &p_input->p->counters.counters_lock );
161 vlc_mutex_lock( &p_stats->lock );
164 stats_GetInteger( p_input, p_input->p->counters.p_read_packets,
165 &p_stats->i_read_packets );
166 stats_GetInteger( p_input, p_input->p->counters.p_read_bytes,
167 &p_stats->i_read_bytes );
168 stats_GetFloat( p_input, p_input->p->counters.p_input_bitrate,
169 &p_stats->f_input_bitrate );
170 stats_GetInteger( p_input, p_input->p->counters.p_demux_read,
171 &p_stats->i_demux_read_bytes );
172 stats_GetFloat( p_input, p_input->p->counters.p_demux_bitrate,
173 &p_stats->f_demux_bitrate );
174 stats_GetInteger( p_input, p_input->p->counters.p_demux_corrupted,
175 &p_stats->i_demux_corrupted );
176 stats_GetInteger( p_input, p_input->p->counters.p_demux_discontinuity,
177 &p_stats->i_demux_discontinuity );
180 stats_GetInteger( p_input, p_input->p->counters.p_decoded_video,
181 &p_stats->i_decoded_video );
182 stats_GetInteger( p_input, p_input->p->counters.p_decoded_audio,
183 &p_stats->i_decoded_audio );
186 if( p_input->p->counters.p_sout_send_bitrate )
188 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_packets,
189 &p_stats->i_sent_packets );
190 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
191 &p_stats->i_sent_bytes );
192 stats_GetFloat ( p_input, p_input->p->counters.p_sout_send_bitrate,
193 &p_stats->f_send_bitrate );
197 stats_GetInteger( p_input, p_input->p->counters.p_played_abuffers,
198 &p_stats->i_played_abuffers );
199 stats_GetInteger( p_input, p_input->p->counters.p_lost_abuffers,
200 &p_stats->i_lost_abuffers );
203 stats_GetInteger( p_input, p_input->p->counters.p_displayed_pictures,
204 &p_stats->i_displayed_pictures );
205 stats_GetInteger( p_input, p_input->p->counters.p_lost_pictures,
206 &p_stats->i_lost_pictures );
208 vlc_mutex_unlock( &p_stats->lock );
209 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
212 void stats_ReinitInputStats( input_stats_t *p_stats )
214 vlc_mutex_lock( &p_stats->lock );
215 p_stats->i_read_packets = p_stats->i_read_bytes =
216 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
217 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
218 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
219 p_stats->i_demux_corrupted = p_stats->i_demux_discontinuity =
220 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
221 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
222 p_stats->i_decoded_video = p_stats->i_decoded_audio =
223 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
225 vlc_mutex_unlock( &p_stats->lock );
228 void stats_DumpInputStats( input_stats_t *p_stats )
230 vlc_mutex_lock( &p_stats->lock );
231 /* f_bitrate is in bytes / microsecond
232 * *1000 => bytes / millisecond => kbytes / seconds */
233 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
234 "Demux : %i (%i bytes) - %f kB/s\n"
235 " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
236 p_stats->i_read_packets, p_stats->i_read_bytes,
237 p_stats->f_input_bitrate * 1000,
238 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
239 p_stats->f_demux_bitrate * 1000,
240 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
241 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
242 p_stats->f_send_bitrate );
243 vlc_mutex_unlock( &p_stats->lock );
246 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
249 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
250 counter_t *p_counter = NULL;
252 if( !priv->b_stats ) return;
254 vlc_mutex_lock( &priv->timer_lock );
256 for( int i = 0 ; i < priv->i_timers; i++ )
258 if( priv->pp_timers[i]->i_id == i_id
259 && priv->pp_timers[i]->p_obj == p_obj )
261 p_counter = priv->pp_timers[i];
267 counter_sample_t *p_sample;
268 p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
272 p_counter->psz_name = strdup( psz_name );
273 p_counter->i_id = i_id;
274 p_counter->p_obj = p_obj;
275 INSERT_ELEM( priv->pp_timers, priv->i_timers,
276 priv->i_timers, p_counter );
278 /* 1st sample : if started: start_date, else last_time, b_started */
279 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
280 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
281 p_counter->i_samples, p_sample );
282 p_sample->date = 0; p_sample->value.b_bool = 0;
283 /* 2nd sample : global_time, i_samples */
284 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
285 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
286 p_counter->i_samples, p_sample );
287 p_sample->date = 0; p_sample->value.i_int = 0;
289 if( p_counter->pp_samples[0]->value.b_bool == true )
291 msg_Warn( p_obj, "timer '%s' was already started !", psz_name );
294 p_counter->pp_samples[0]->value.b_bool = true;
295 p_counter->pp_samples[0]->date = mdate();
297 vlc_mutex_unlock( &priv->timer_lock );
300 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
302 counter_t *p_counter = NULL;
303 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
305 if( !priv->b_stats ) return;
306 vlc_mutex_lock( &priv->timer_lock );
307 for( int i = 0 ; i < priv->i_timers; i++ )
309 if( priv->pp_timers[i]->i_id == i_id
310 && priv->pp_timers[i]->p_obj == p_obj )
312 p_counter = priv->pp_timers[i];
316 if( !p_counter || p_counter->i_samples != 2 )
318 msg_Err( p_obj, "timer does not exist" );
321 p_counter->pp_samples[0]->value.b_bool = false;
322 p_counter->pp_samples[1]->value.i_int += 1;
323 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
324 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
326 vlc_mutex_unlock( &priv->timer_lock );
329 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
331 counter_t *p_counter = NULL;
332 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
334 if( !priv->b_stats ) return;
335 vlc_mutex_lock( &priv->timer_lock );
336 for( int i = 0 ; i < priv->i_timers; i++ )
338 if( priv->pp_timers[i]->i_id == i_id
339 && priv->pp_timers[i]->p_obj == p_obj )
341 p_counter = priv->pp_timers[i];
345 TimerDump( p_obj, p_counter, true );
346 vlc_mutex_unlock( &priv->timer_lock );
349 void __stats_TimersDumpAll( vlc_object_t *p_obj )
351 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
353 if( !priv->b_stats ) return;
354 vlc_mutex_lock( &priv->timer_lock );
355 for ( int i = 0 ; i < priv->i_timers ; i++ )
356 TimerDump( p_obj, priv->pp_timers[i], false );
357 vlc_mutex_unlock( &priv->timer_lock );
360 void __stats_TimerClean( vlc_object_t *p_obj, unsigned int i_id )
362 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
364 vlc_mutex_lock( &priv->timer_lock );
365 for ( int i = priv->i_timers -1 ; i >= 0; i-- )
367 counter_t *p_counter = priv->pp_timers[i];
368 if( p_counter->i_id == i_id && p_counter->p_obj == p_obj )
370 REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
371 stats_CounterClean( p_counter );
374 vlc_mutex_unlock( &priv->timer_lock );
377 void __stats_TimersCleanAll( vlc_object_t *p_obj )
379 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
381 vlc_mutex_lock( &priv->timer_lock );
382 for ( int i = priv->i_timers -1 ; i >= 0; i-- )
384 counter_t *p_counter = priv->pp_timers[i];
385 REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
386 stats_CounterClean( p_counter );
388 vlc_mutex_unlock( &priv->timer_lock );
391 void stats_CounterClean( counter_t *p_c )
395 int i = p_c->i_samples - 1 ;
398 counter_sample_t *p_s = p_c->pp_samples[i];
399 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
403 free( p_c->psz_name );
409 /********************************************************************
410 * Following functions are local
411 ********************************************************************/
414 * Update a statistics counter, according to its type
415 * If needed, perform a bit of computation (derivative, mostly)
416 * This function must be entered with stats handler lock
417 * \param p_counter the counter to update
418 * \param val the "new" value
419 * \return an error code
421 static int CounterUpdate( vlc_object_t *p_handler,
422 counter_t *p_counter,
423 vlc_value_t val, vlc_value_t *new_val )
425 switch( p_counter->i_compute_type )
430 if( p_counter->i_samples > 1)
432 msg_Err( p_handler, "LAST counter has several samples !" );
435 if( p_counter->i_type != VLC_VAR_FLOAT &&
436 p_counter->i_type != VLC_VAR_INTEGER &&
437 p_counter->i_compute_type != STATS_LAST )
439 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
443 if( p_counter->i_samples == 0 )
445 counter_sample_t *p_new = (counter_sample_t*)malloc(
446 sizeof( counter_sample_t ) );
447 p_new->value.psz_string = NULL;
449 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
450 p_counter->i_samples, p_new );
452 if( p_counter->i_samples == 1 )
454 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
455 if( p_counter->i_compute_type == STATS_LAST ||
456 ( p_counter->i_compute_type == STATS_MAX &&
457 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
458 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
459 ( p_counter->i_type == VLC_VAR_FLOAT &&
460 p_counter->pp_samples[0]->value.f_float > val.f_float )
462 ( p_counter->i_compute_type == STATS_MIN &&
463 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
464 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
465 ( p_counter->i_type == VLC_VAR_FLOAT &&
466 p_counter->pp_samples[0]->value.f_float < val.f_float )
469 if( p_counter->i_type == VLC_VAR_STRING &&
470 p_counter->pp_samples[0]->value.psz_string )
472 free( p_counter->pp_samples[0]->value.psz_string );
474 p_counter->pp_samples[0]->value = val;
475 *new_val = p_counter->pp_samples[0]->value;
479 case STATS_DERIVATIVE:
481 counter_sample_t *p_new, *p_old;
482 mtime_t now = mdate();
483 if( now - p_counter->last_update < p_counter->update_interval )
487 p_counter->last_update = now;
488 if( p_counter->i_type != VLC_VAR_FLOAT &&
489 p_counter->i_type != VLC_VAR_INTEGER )
491 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
494 /* Insert the new one at the beginning */
495 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
497 p_new->date = p_counter->last_update;
498 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
501 if( p_counter->i_samples == 3 )
503 p_old = p_counter->pp_samples[2];
504 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
510 if( p_counter->i_samples > 1)
512 msg_Err( p_handler, "LAST counter has several samples !" );
515 if( p_counter->i_samples == 0 )
517 counter_sample_t *p_new = (counter_sample_t*)malloc(
518 sizeof( counter_sample_t ) );
519 p_new->value.psz_string = NULL;
521 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
522 p_counter->i_samples, p_new );
524 if( p_counter->i_samples == 1 )
526 switch( p_counter->i_type )
528 case VLC_VAR_INTEGER:
529 p_counter->pp_samples[0]->value.i_int += val.i_int;
531 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
534 p_counter->pp_samples[0]->value.f_float += val.f_float;
536 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
538 msg_Err( p_handler, "Trying to increment invalid variable %s",
539 p_counter->psz_name );
548 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
556 if( p_counter->i_samples != 2 )
558 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
561 i_total = p_counter->pp_samples[1]->value.i_int;
562 total = p_counter->pp_samples[1]->date;
563 if( p_counter->pp_samples[0]->value.b_bool == true )
565 last = mdate() - p_counter->pp_samples[0]->date;
571 last = p_counter->pp_samples[0]->date;
576 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
577 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
578 (float)(total)/(1000*(float)i_total ) );
583 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
584 p_counter->psz_name, (float)total/1000, i_total,
585 (float)(total)/(1000*(float)i_total ) );