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 );
176 stats_GetInteger( p_input, p_input->p->counters.p_decoded_video,
177 &p_stats->i_decoded_video );
178 stats_GetInteger( p_input, p_input->p->counters.p_decoded_audio,
179 &p_stats->i_decoded_audio );
182 if( p_input->p->counters.p_sout_send_bitrate )
184 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_packets,
185 &p_stats->i_sent_packets );
186 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
187 &p_stats->i_sent_bytes );
188 stats_GetFloat ( p_input, p_input->p->counters.p_sout_send_bitrate,
189 &p_stats->f_send_bitrate );
193 stats_GetInteger( p_input, p_input->p->counters.p_played_abuffers,
194 &p_stats->i_played_abuffers );
195 stats_GetInteger( p_input, p_input->p->counters.p_lost_abuffers,
196 &p_stats->i_lost_abuffers );
199 stats_GetInteger( p_input, p_input->p->counters.p_displayed_pictures,
200 &p_stats->i_displayed_pictures );
201 stats_GetInteger( p_input, p_input->p->counters.p_lost_pictures,
202 &p_stats->i_lost_pictures );
204 vlc_mutex_unlock( &p_stats->lock );
205 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
208 void stats_ReinitInputStats( input_stats_t *p_stats )
210 vlc_mutex_lock( &p_stats->lock );
211 p_stats->i_read_packets = p_stats->i_read_bytes =
212 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
213 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
214 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
215 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
216 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
217 p_stats->i_decoded_video = p_stats->i_decoded_audio =
218 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
220 vlc_mutex_unlock( &p_stats->lock );
223 void stats_DumpInputStats( input_stats_t *p_stats )
225 vlc_mutex_lock( &p_stats->lock );
226 /* f_bitrate is in bytes / microsecond
227 * *1000 => bytes / millisecond => kbytes / seconds */
228 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
229 "Demux : %i (%i bytes) - %f kB/s\n"
230 " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
231 p_stats->i_read_packets, p_stats->i_read_bytes,
232 p_stats->f_input_bitrate * 1000,
233 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
234 p_stats->f_demux_bitrate * 1000,
235 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
236 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
237 p_stats->f_send_bitrate );
238 vlc_mutex_unlock( &p_stats->lock );
241 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
246 if( !libvlc_stats (p_obj) ) return;
248 vlc_mutex_lock( &p_stats->lock );
250 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
253 float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
254 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
256 float f_in = 0, f_out = 0, f_demux = 0;
257 input_thread_t *p_input = (input_thread_t *)
258 p_list->p_values[i_index].p_object;
259 vlc_mutex_lock( &p_input->p->counters.counters_lock );
260 stats_GetFloat( p_obj, p_input->p->counters.p_input_bitrate, &f_in );
261 if( p_input->p->counters.p_sout_send_bitrate )
262 stats_GetFloat( p_obj, p_input->p->counters.p_sout_send_bitrate,
264 stats_GetFloat( p_obj, p_input->p->counters.p_demux_bitrate,
266 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
267 f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
269 p_stats->f_input_bitrate = f_total_in;
270 p_stats->f_output_bitrate = f_total_out;
271 p_stats->f_demux_bitrate = f_total_demux;
272 vlc_list_release( p_list );
275 vlc_mutex_unlock( &p_stats->lock );
278 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
281 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
282 counter_t *p_counter = NULL;
284 if( !priv->b_stats ) return;
286 vlc_mutex_lock( &priv->timer_lock );
288 for( int i = 0 ; i < priv->i_timers; i++ )
290 if( priv->pp_timers[i]->i_id == i_id
291 && priv->pp_timers[i]->p_obj == p_obj )
293 p_counter = priv->pp_timers[i];
299 counter_sample_t *p_sample;
300 p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
304 p_counter->psz_name = strdup( psz_name );
305 p_counter->i_id = i_id;
306 p_counter->p_obj = p_obj;
307 INSERT_ELEM( priv->pp_timers, priv->i_timers,
308 priv->i_timers, p_counter );
310 /* 1st sample : if started: start_date, else last_time, b_started */
311 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
312 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
313 p_counter->i_samples, p_sample );
314 p_sample->date = 0; p_sample->value.b_bool = 0;
315 /* 2nd sample : global_time, i_samples */
316 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
317 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
318 p_counter->i_samples, p_sample );
319 p_sample->date = 0; p_sample->value.i_int = 0;
321 if( p_counter->pp_samples[0]->value.b_bool == true )
323 msg_Warn( p_obj, "timer '%s' was already started !", psz_name );
326 p_counter->pp_samples[0]->value.b_bool = true;
327 p_counter->pp_samples[0]->date = mdate();
329 vlc_mutex_unlock( &priv->timer_lock );
332 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
334 counter_t *p_counter = NULL;
335 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
337 if( !priv->b_stats ) return;
338 vlc_mutex_lock( &priv->timer_lock );
339 for( int i = 0 ; i < priv->i_timers; i++ )
341 if( priv->pp_timers[i]->i_id == i_id
342 && priv->pp_timers[i]->p_obj == p_obj )
344 p_counter = priv->pp_timers[i];
348 if( !p_counter || p_counter->i_samples != 2 )
350 msg_Err( p_obj, "timer does not exist" );
353 p_counter->pp_samples[0]->value.b_bool = false;
354 p_counter->pp_samples[1]->value.i_int += 1;
355 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
356 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
358 vlc_mutex_unlock( &priv->timer_lock );
361 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
363 counter_t *p_counter = NULL;
364 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
366 if( !priv->b_stats ) return;
367 vlc_mutex_lock( &priv->timer_lock );
368 for( int i = 0 ; i < priv->i_timers; i++ )
370 if( priv->pp_timers[i]->i_id == i_id
371 && priv->pp_timers[i]->p_obj == p_obj )
373 p_counter = priv->pp_timers[i];
377 TimerDump( p_obj, p_counter, true );
378 vlc_mutex_unlock( &priv->timer_lock );
381 void __stats_TimersDumpAll( vlc_object_t *p_obj )
383 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
385 if( !priv->b_stats ) return;
386 vlc_mutex_lock( &priv->timer_lock );
387 for ( int i = 0 ; i < priv->i_timers ; i++ )
388 TimerDump( p_obj, priv->pp_timers[i], false );
389 vlc_mutex_unlock( &priv->timer_lock );
392 void __stats_TimerClean( vlc_object_t *p_obj, unsigned int i_id )
394 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
396 vlc_mutex_lock( &priv->timer_lock );
397 for ( int i = priv->i_timers -1 ; i >= 0; i-- )
399 counter_t *p_counter = priv->pp_timers[i];
400 if( p_counter->i_id == i_id && p_counter->p_obj == p_obj )
402 REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
403 stats_CounterClean( p_counter );
406 vlc_mutex_unlock( &priv->timer_lock );
409 void __stats_TimersCleanAll( vlc_object_t *p_obj )
411 libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
413 vlc_mutex_lock( &priv->timer_lock );
414 for ( int i = priv->i_timers -1 ; i >= 0; i-- )
416 counter_t *p_counter = priv->pp_timers[i];
417 REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
418 stats_CounterClean( p_counter );
420 vlc_mutex_unlock( &priv->timer_lock );
423 void stats_CounterClean( counter_t *p_c )
427 int i = p_c->i_samples - 1 ;
430 counter_sample_t *p_s = p_c->pp_samples[i];
431 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
435 free( p_c->psz_name );
441 /********************************************************************
442 * Following functions are local
443 ********************************************************************/
446 * Update a statistics counter, according to its type
447 * If needed, perform a bit of computation (derivative, mostly)
448 * This function must be entered with stats handler lock
449 * \param p_counter the counter to update
450 * \param val the "new" value
451 * \return an error code
453 static int CounterUpdate( vlc_object_t *p_handler,
454 counter_t *p_counter,
455 vlc_value_t val, vlc_value_t *new_val )
457 switch( p_counter->i_compute_type )
462 if( p_counter->i_samples > 1)
464 msg_Err( p_handler, "LAST counter has several samples !" );
467 if( p_counter->i_type != VLC_VAR_FLOAT &&
468 p_counter->i_type != VLC_VAR_INTEGER &&
469 p_counter->i_compute_type != STATS_LAST )
471 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
475 if( p_counter->i_samples == 0 )
477 counter_sample_t *p_new = (counter_sample_t*)malloc(
478 sizeof( counter_sample_t ) );
479 p_new->value.psz_string = NULL;
481 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
482 p_counter->i_samples, p_new );
484 if( p_counter->i_samples == 1 )
486 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
487 if( p_counter->i_compute_type == STATS_LAST ||
488 ( p_counter->i_compute_type == STATS_MAX &&
489 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
490 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
491 ( p_counter->i_type == VLC_VAR_FLOAT &&
492 p_counter->pp_samples[0]->value.f_float > val.f_float )
494 ( p_counter->i_compute_type == STATS_MIN &&
495 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
496 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
497 ( p_counter->i_type == VLC_VAR_FLOAT &&
498 p_counter->pp_samples[0]->value.f_float < val.f_float )
501 if( p_counter->i_type == VLC_VAR_STRING &&
502 p_counter->pp_samples[0]->value.psz_string )
504 free( p_counter->pp_samples[0]->value.psz_string );
506 p_counter->pp_samples[0]->value = val;
507 *new_val = p_counter->pp_samples[0]->value;
511 case STATS_DERIVATIVE:
513 counter_sample_t *p_new, *p_old;
514 mtime_t now = mdate();
515 if( now - p_counter->last_update < p_counter->update_interval )
519 p_counter->last_update = now;
520 if( p_counter->i_type != VLC_VAR_FLOAT &&
521 p_counter->i_type != VLC_VAR_INTEGER )
523 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
526 /* Insert the new one at the beginning */
527 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
529 p_new->date = p_counter->last_update;
530 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
533 if( p_counter->i_samples == 3 )
535 p_old = p_counter->pp_samples[2];
536 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
542 if( p_counter->i_samples > 1)
544 msg_Err( p_handler, "LAST counter has several samples !" );
547 if( p_counter->i_samples == 0 )
549 counter_sample_t *p_new = (counter_sample_t*)malloc(
550 sizeof( counter_sample_t ) );
551 p_new->value.psz_string = NULL;
553 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
554 p_counter->i_samples, p_new );
556 if( p_counter->i_samples == 1 )
558 switch( p_counter->i_type )
560 case VLC_VAR_INTEGER:
561 p_counter->pp_samples[0]->value.i_int += val.i_int;
563 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
566 p_counter->pp_samples[0]->value.f_float += val.f_float;
568 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
570 msg_Err( p_handler, "Trying to increment invalid variable %s",
571 p_counter->psz_name );
580 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
588 if( p_counter->i_samples != 2 )
590 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
593 i_total = p_counter->pp_samples[1]->value.i_int;
594 total = p_counter->pp_samples[1]->date;
595 if( p_counter->pp_samples[0]->value.b_bool == true )
597 last = mdate() - p_counter->pp_samples[0]->date;
603 last = p_counter->pp_samples[0]->date;
608 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
609 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
610 (float)(total)/(1000*(float)i_total ) );
615 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
616 p_counter->psz_name, (float)total/1000, i_total,
617 (float)(total)/(1000*(float)i_total ) );