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 = malloc( sizeof(input_stats_t) );
151 memset( p_stats, 0, sizeof(*p_stats) );
152 vlc_mutex_init( &p_stats->lock );
153 stats_ReinitInputStats( p_stats );
158 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
160 if( !libvlc_stats (p_input) ) return;
162 vlc_mutex_lock( &p_input->p->counters.counters_lock );
163 vlc_mutex_lock( &p_stats->lock );
166 stats_GetInteger( p_input, p_input->p->counters.p_read_packets,
167 &p_stats->i_read_packets );
168 stats_GetInteger( p_input, p_input->p->counters.p_read_bytes,
169 &p_stats->i_read_bytes );
170 stats_GetFloat( p_input, p_input->p->counters.p_input_bitrate,
171 &p_stats->f_input_bitrate );
172 stats_GetInteger( p_input, p_input->p->counters.p_demux_read,
173 &p_stats->i_demux_read_bytes );
174 stats_GetFloat( p_input, p_input->p->counters.p_demux_bitrate,
175 &p_stats->f_demux_bitrate );
178 stats_GetInteger( p_input, p_input->p->counters.p_decoded_video,
179 &p_stats->i_decoded_video );
180 stats_GetInteger( p_input, p_input->p->counters.p_decoded_audio,
181 &p_stats->i_decoded_audio );
184 if( p_input->p->counters.p_sout_send_bitrate )
186 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_packets,
187 &p_stats->i_sent_packets );
188 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
189 &p_stats->i_sent_bytes );
190 stats_GetFloat ( p_input, p_input->p->counters.p_sout_send_bitrate,
191 &p_stats->f_send_bitrate );
195 stats_GetInteger( p_input, p_input->p->counters.p_played_abuffers,
196 &p_stats->i_played_abuffers );
197 stats_GetInteger( p_input, p_input->p->counters.p_lost_abuffers,
198 &p_stats->i_lost_abuffers );
201 stats_GetInteger( p_input, p_input->p->counters.p_displayed_pictures,
202 &p_stats->i_displayed_pictures );
203 stats_GetInteger( p_input, p_input->p->counters.p_lost_pictures,
204 &p_stats->i_lost_pictures );
206 vlc_mutex_unlock( &p_stats->lock );
207 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
210 void stats_ReinitInputStats( input_stats_t *p_stats )
212 vlc_mutex_lock( &p_stats->lock );
213 p_stats->i_read_packets = p_stats->i_read_bytes =
214 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
215 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
216 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
217 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
218 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
219 p_stats->i_decoded_video = p_stats->i_decoded_audio =
220 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
222 vlc_mutex_unlock( &p_stats->lock );
225 void stats_DumpInputStats( input_stats_t *p_stats )
227 vlc_mutex_lock( &p_stats->lock );
228 /* f_bitrate is in bytes / microsecond
229 * *1000 => bytes / millisecond => kbytes / seconds */
230 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
231 "Demux : %i (%i bytes) - %f kB/s\n"
232 " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
233 p_stats->i_read_packets, p_stats->i_read_bytes,
234 p_stats->f_input_bitrate * 1000,
235 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
236 p_stats->f_demux_bitrate * 1000,
237 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
238 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
239 p_stats->f_send_bitrate );
240 vlc_mutex_unlock( &p_stats->lock );
243 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
248 if( !libvlc_stats (p_obj) ) return;
250 vlc_mutex_lock( &p_stats->lock );
252 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
255 float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
256 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
258 float f_in = 0, f_out = 0, f_demux = 0;
259 input_thread_t *p_input = (input_thread_t *)
260 p_list->p_values[i_index].p_object;
261 vlc_mutex_lock( &p_input->p->counters.counters_lock );
262 stats_GetFloat( p_obj, p_input->p->counters.p_input_bitrate, &f_in );
263 if( p_input->p->counters.p_sout_send_bitrate )
264 stats_GetFloat( p_obj, p_input->p->counters.p_sout_send_bitrate,
266 stats_GetFloat( p_obj, p_input->p->counters.p_demux_bitrate,
268 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
269 f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
271 p_stats->f_input_bitrate = f_total_in;
272 p_stats->f_output_bitrate = f_total_out;
273 p_stats->f_demux_bitrate = f_total_demux;
274 vlc_list_release( p_list );
277 vlc_mutex_unlock( &p_stats->lock );
280 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
283 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
284 counter_t *p_counter = NULL;
286 if( !priv->b_stats ) return;
288 vlc_mutex_lock( &priv->timer_lock );
290 for( int i = 0 ; i < priv->i_timers; i++ )
292 if( priv->pp_timers[i]->i_id == i_id
293 && priv->pp_timers[i]->p_obj == p_obj )
295 p_counter = priv->pp_timers[i];
301 counter_sample_t *p_sample;
302 p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
306 p_counter->psz_name = strdup( psz_name );
307 p_counter->i_id = i_id;
308 p_counter->p_obj = p_obj;
309 INSERT_ELEM( priv->pp_timers, priv->i_timers,
310 priv->i_timers, p_counter );
312 /* 1st sample : if started: start_date, else last_time, b_started */
313 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
314 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
315 p_counter->i_samples, p_sample );
316 p_sample->date = 0; p_sample->value.b_bool = 0;
317 /* 2nd sample : global_time, i_samples */
318 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
319 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
320 p_counter->i_samples, p_sample );
321 p_sample->date = 0; p_sample->value.i_int = 0;
323 if( p_counter->pp_samples[0]->value.b_bool == true )
325 msg_Warn( p_obj, "timer '%s' was already started !", psz_name );
328 p_counter->pp_samples[0]->value.b_bool = true;
329 p_counter->pp_samples[0]->date = mdate();
331 vlc_mutex_unlock( &priv->timer_lock );
334 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
336 counter_t *p_counter = NULL;
337 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
339 if( !priv->b_stats ) return;
340 vlc_mutex_lock( &priv->timer_lock );
341 for( int i = 0 ; i < priv->i_timers; i++ )
343 if( priv->pp_timers[i]->i_id == i_id
344 && priv->pp_timers[i]->p_obj == p_obj )
346 p_counter = priv->pp_timers[i];
350 if( !p_counter || p_counter->i_samples != 2 )
352 msg_Err( p_obj, "timer does not exist" );
355 p_counter->pp_samples[0]->value.b_bool = false;
356 p_counter->pp_samples[1]->value.i_int += 1;
357 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
358 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
360 vlc_mutex_unlock( &priv->timer_lock );
363 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
365 counter_t *p_counter = NULL;
366 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
368 if( !priv->b_stats ) return;
369 vlc_mutex_lock( &priv->timer_lock );
370 for( int i = 0 ; i < priv->i_timers; i++ )
372 if( priv->pp_timers[i]->i_id == i_id
373 && priv->pp_timers[i]->p_obj == p_obj )
375 p_counter = priv->pp_timers[i];
379 TimerDump( p_obj, p_counter, true );
380 vlc_mutex_unlock( &priv->timer_lock );
383 void __stats_TimersDumpAll( vlc_object_t *p_obj )
385 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
387 if( !priv->b_stats ) return;
388 vlc_mutex_lock( &priv->timer_lock );
389 for ( int i = 0 ; i < priv->i_timers ; i++ )
390 TimerDump( p_obj, priv->pp_timers[i], false );
391 vlc_mutex_unlock( &priv->timer_lock );
394 void __stats_TimerClean( vlc_object_t *p_obj, unsigned int i_id )
396 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
398 vlc_mutex_lock( &priv->timer_lock );
399 for ( int i = priv->i_timers -1 ; i >= 0; i-- )
401 counter_t *p_counter = priv->pp_timers[i];
402 if( p_counter->i_id == i_id && p_counter->p_obj == p_obj )
404 REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
405 stats_CounterClean( p_counter );
408 vlc_mutex_unlock( &priv->timer_lock );
411 void __stats_TimersCleanAll( vlc_object_t *p_obj )
413 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
415 vlc_mutex_lock( &priv->timer_lock );
416 for ( int i = priv->i_timers -1 ; i >= 0; i-- )
418 counter_t *p_counter = priv->pp_timers[i];
419 REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
420 stats_CounterClean( p_counter );
422 vlc_mutex_unlock( &priv->timer_lock );
425 void stats_CounterClean( counter_t *p_c )
429 int i = p_c->i_samples - 1 ;
432 counter_sample_t *p_s = p_c->pp_samples[i];
433 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
437 free( p_c->psz_name );
443 /********************************************************************
444 * Following functions are local
445 ********************************************************************/
448 * Update a statistics counter, according to its type
449 * If needed, perform a bit of computation (derivative, mostly)
450 * This function must be entered with stats handler lock
451 * \param p_counter the counter to update
452 * \param val the "new" value
453 * \return an error code
455 static int CounterUpdate( vlc_object_t *p_handler,
456 counter_t *p_counter,
457 vlc_value_t val, vlc_value_t *new_val )
459 switch( p_counter->i_compute_type )
464 if( p_counter->i_samples > 1)
466 msg_Err( p_handler, "LAST counter has several samples !" );
469 if( p_counter->i_type != VLC_VAR_FLOAT &&
470 p_counter->i_type != VLC_VAR_INTEGER &&
471 p_counter->i_compute_type != STATS_LAST )
473 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
477 if( p_counter->i_samples == 0 )
479 counter_sample_t *p_new = (counter_sample_t*)malloc(
480 sizeof( counter_sample_t ) );
481 p_new->value.psz_string = NULL;
483 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
484 p_counter->i_samples, p_new );
486 if( p_counter->i_samples == 1 )
488 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
489 if( p_counter->i_compute_type == STATS_LAST ||
490 ( p_counter->i_compute_type == STATS_MAX &&
491 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
492 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
493 ( p_counter->i_type == VLC_VAR_FLOAT &&
494 p_counter->pp_samples[0]->value.f_float > val.f_float )
496 ( p_counter->i_compute_type == STATS_MIN &&
497 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
498 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
499 ( p_counter->i_type == VLC_VAR_FLOAT &&
500 p_counter->pp_samples[0]->value.f_float < val.f_float )
503 if( p_counter->i_type == VLC_VAR_STRING &&
504 p_counter->pp_samples[0]->value.psz_string )
506 free( p_counter->pp_samples[0]->value.psz_string );
508 p_counter->pp_samples[0]->value = val;
509 *new_val = p_counter->pp_samples[0]->value;
513 case STATS_DERIVATIVE:
515 counter_sample_t *p_new, *p_old;
516 if( mdate() - p_counter->last_update < p_counter->update_interval )
520 p_counter->last_update = mdate();
521 if( p_counter->i_type != VLC_VAR_FLOAT &&
522 p_counter->i_type != VLC_VAR_INTEGER )
524 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
527 /* Insert the new one at the beginning */
528 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
530 p_new->date = p_counter->last_update;
531 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
534 if( p_counter->i_samples == 3 )
536 p_old = p_counter->pp_samples[2];
537 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
543 if( p_counter->i_samples > 1)
545 msg_Err( p_handler, "LAST counter has several samples !" );
548 if( p_counter->i_samples == 0 )
550 counter_sample_t *p_new = (counter_sample_t*)malloc(
551 sizeof( counter_sample_t ) );
552 p_new->value.psz_string = NULL;
554 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
555 p_counter->i_samples, p_new );
557 if( p_counter->i_samples == 1 )
559 switch( p_counter->i_type )
561 case VLC_VAR_INTEGER:
562 p_counter->pp_samples[0]->value.i_int += val.i_int;
564 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
567 p_counter->pp_samples[0]->value.f_float += val.f_float;
569 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
571 msg_Err( p_handler, "Trying to increment invalid variable %s",
572 p_counter->psz_name );
581 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
589 if( p_counter->i_samples != 2 )
591 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
594 i_total = p_counter->pp_samples[1]->value.i_int;
595 total = p_counter->pp_samples[1]->date;
596 if( p_counter->pp_samples[0]->value.b_bool == true )
598 last = mdate() - p_counter->pp_samples[0]->date;
604 last = p_counter->pp_samples[0]->date;
609 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
610 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
611 (float)(total)/(1000*(float)i_total ) );
616 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
617 p_counter->psz_name, (float)total/1000, i_total,
618 (float)(total)/(1000*(float)i_total ) );