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 *****************************************************************************/
27 #include <stdio.h> /* required */
30 #include <vlc_input.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
36 const char *psz_name );
37 static int stats_CounterUpdate( stats_handler_t *p_handler,
40 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this );
41 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this );
43 static void TimerDump( vlc_object_t *p_this, counter_t *p_counter, vlc_bool_t);
45 /*****************************************************************************
47 *****************************************************************************/
50 * Cleanup statistics handler stuff
51 * \param p_stats the handler to clean
54 void stats_HandlerDestroy( stats_handler_t *p_stats )
57 for ( i = p_stats->i_counters - 1 ; i >= 0 ; i-- )
60 counter_t * p_counter = p_stats->pp_counters[i];
62 for( j = p_counter->i_samples -1; j >= 0 ; j-- )
64 counter_sample_t *p_sample = p_counter->pp_samples[j];
65 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, j );
68 free( p_counter->psz_name );
69 REMOVE_ELEM( p_stats->pp_counters, p_stats->i_counters, i );
75 * Create a statistics counter
76 * \param p_this the object for which to create the counter
77 * \param psz_name the name
78 * \param i_type the type of stored data. One of VLC_VAR_STRING,
79 * VLC_VAR_INTEGER, VLC_VAR_FLOAT
80 * \param i_compute_type the aggregation type. One of STATS_LAST (always
81 * keep the last value), STATS_COUNTER (increment by the passed value),
82 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
83 * (keep a time derivative of the value)
85 int __stats_Create( vlc_object_t *p_this, const char *psz_name, int i_type,
89 stats_handler_t *p_handler;
91 if( p_this->p_libvlc->b_stats == VLC_FALSE )
95 p_handler = stats_HandlerGet( p_this );
96 if( !p_handler ) return VLC_ENOMEM;
98 vlc_mutex_lock( &p_handler->object_lock );
100 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
102 p_counter->psz_name = strdup( psz_name );
103 p_counter->i_source_object = p_this->i_object_id;
104 p_counter->i_compute_type = i_compute_type;
105 p_counter->i_type = i_type;
106 p_counter->i_samples = 0;
107 p_counter->pp_samples = NULL;
109 p_counter->update_interval = 0;
110 p_counter->last_update = 0;
112 INSERT_ELEM( p_handler->pp_counters,
113 p_handler->i_counters,
114 p_handler->i_counters,
117 vlc_mutex_unlock( &p_handler->object_lock );
122 /** Update a counter element with new values
123 * \param p_this the object in which to update
124 * \param psz_name the name
125 * \param val the vlc_value union containing the new value to aggregate. For
126 * more information on how data is aggregated, \see __stats_Create
128 int __stats_Update( vlc_object_t *p_this, const char *psz_name,
132 counter_t *p_counter;
134 /* Get stats handler singleton */
135 stats_handler_t *p_handler;
136 if( p_this->p_libvlc->b_stats == VLC_FALSE )
140 p_handler = stats_HandlerGet( p_this );
141 if( !p_handler ) return VLC_ENOMEM;
143 vlc_mutex_lock( &p_handler->object_lock );
144 /* Look for existing element */
145 p_counter = GetCounter( p_handler, p_this->i_object_id,
149 vlc_mutex_unlock( &p_handler->object_lock );
150 vlc_object_release( p_handler );
154 i_ret = stats_CounterUpdate( p_handler, p_counter, val );
155 vlc_mutex_unlock( &p_handler->object_lock );
160 /** Get the aggregated value for a counter
161 * \param p_this an object
162 * \param i_object_id the object id from which we want the data
163 * \param psz_name the name of the couner
164 * \param val a pointer to an initialized vlc_value union. It will contain the
166 * \return an error code
168 int __stats_Get( vlc_object_t *p_this, int i_object_id,
169 const char *psz_name, vlc_value_t *val )
171 counter_t *p_counter;
173 /* Get stats handler singleton */
174 stats_handler_t *p_handler;
175 if( p_this->p_libvlc->b_stats == VLC_FALSE )
179 p_handler = stats_HandlerGet( p_this );
180 if( !p_handler ) return VLC_ENOMEM;
181 vlc_mutex_lock( &p_handler->object_lock );
183 /* Look for existing element */
184 p_counter = GetCounter( p_handler, i_object_id,
188 vlc_mutex_unlock( &p_handler->object_lock );
189 vlc_object_release( p_handler );
190 val->i_int = val->f_float = 0.0;
194 if( p_counter->i_samples == 0 )
196 vlc_mutex_unlock( &p_handler->object_lock );
197 val->i_int = val->f_float = 0.0;
201 switch( p_counter->i_compute_type )
207 *val = p_counter->pp_samples[0]->value;
209 case STATS_DERIVATIVE:
211 if( p_counter->i_samples < 2 )
213 vlc_mutex_unlock( &p_handler->object_lock );
214 val->i_int = 0; val->f_float = 0.0;
217 if( p_counter->i_type == VLC_VAR_INTEGER )
219 float f = ( p_counter->pp_samples[0]->value.i_int -
220 p_counter->pp_samples[1]->value.i_int ) /
221 (float)( p_counter->pp_samples[0]->date -
222 p_counter->pp_samples[1]->date );
227 float f = (float)( p_counter->pp_samples[0]->value.f_float -
228 p_counter->pp_samples[1]->value.f_float ) /
229 (float)( p_counter->pp_samples[0]->date -
230 p_counter->pp_samples[1]->date );
235 vlc_object_release( p_handler );
237 vlc_mutex_unlock( &p_handler->object_lock );
241 /** Get a statistics counter structure. This allows for low-level modifications
242 * \param p_this a parent object
243 * \param i_object_id the object from which to retrieve data
244 * \param psz_name the name
245 * \return the counter, or NULL if not found (or handler not created yet)
247 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
248 const char *psz_name )
250 counter_t *p_counter;
252 stats_handler_t *p_handler;
253 if( p_this->p_libvlc->b_stats == VLC_FALSE )
257 p_handler = stats_HandlerGet( p_this );
258 if( !p_handler ) return NULL;
260 vlc_mutex_lock( &p_handler->object_lock );
262 /* Look for existing element */
263 p_counter = GetCounter( p_handler, i_object_id,
265 vlc_mutex_unlock( &p_handler->object_lock );
266 vlc_object_release( p_handler );
272 void stats_ComputeInputStats( input_thread_t *p_input,
273 input_stats_t *p_stats )
278 vlc_mutex_lock( &p_stats->lock );
281 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
282 &p_stats->i_read_packets );
283 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
284 &p_stats->i_read_bytes );
285 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
286 &p_stats->f_input_bitrate );
288 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
289 &p_stats->i_demux_read_bytes );
290 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
291 &p_stats->f_demux_bitrate );
293 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
294 &p_stats->i_decoded_video );
295 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
296 &p_stats->i_decoded_audio );
299 stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_packets",
300 &p_stats->i_sent_packets );
301 stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_bytes",
302 &p_stats->i_sent_bytes );
303 stats_GetFloat ( p_input, p_input->i_object_id, "sout_send_bitrate",
304 &p_stats->f_send_bitrate );
306 /* Aout - We store in p_input because aout is shared */
307 stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
308 &p_stats->i_played_abuffers );
309 stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
310 &p_stats->i_lost_abuffers );
312 /* Vouts - FIXME: Store all in input */
313 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
316 p_stats->i_displayed_pictures = 0 ;
317 p_stats->i_lost_pictures = 0;
318 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
320 int i_displayed = 0, i_lost = 0;
321 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
322 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
324 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
326 p_stats->i_displayed_pictures += i_displayed;
327 p_stats->i_lost_pictures += i_lost;
329 vlc_list_release( p_list );
332 vlc_mutex_unlock( &p_stats->lock );
336 void stats_ReinitInputStats( input_stats_t *p_stats )
338 p_stats->i_read_packets = p_stats->i_read_bytes =
339 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
340 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
341 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
342 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
343 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
344 p_stats->i_decoded_video = p_stats->i_decoded_audio =
345 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
349 void stats_DumpInputStats( input_stats_t *p_stats )
351 vlc_mutex_lock( &p_stats->lock );
352 /* f_bitrate is in bytes / microsecond
353 * *1000 => bytes / millisecond => kbytes / seconds */
354 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
355 " - Vout : %i/%i - Aout : %i/%i - Vout : %f\n",
356 p_stats->i_read_packets, p_stats->i_read_bytes,
357 p_stats->f_input_bitrate * 1000,
358 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
359 p_stats->f_demux_bitrate * 1000,
360 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
361 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
362 p_stats->f_send_bitrate );
363 vlc_mutex_unlock( &p_stats->lock );
366 void __stats_ComputeGlobalStats( vlc_object_t *p_obj,
367 global_stats_t *p_stats )
371 vlc_mutex_lock( &p_stats->lock );
373 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_CHILD );
376 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
378 float f_in = 0, f_out = 0;
379 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
380 stats_GetFloat( p_obj, p_obj->i_object_id, "input_bitrate",
382 stats_GetFloat( p_obj, p_obj->i_object_id, "sout_send_bitrate",
384 p_stats->f_input_bitrate += f_in;
385 p_stats->f_output_bitrate += f_out;
387 vlc_list_release( p_list );
390 vlc_mutex_unlock( &p_stats->lock );
393 void stats_ReinitGlobalStats( global_stats_t *p_stats )
395 p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
399 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name )
401 counter_t *p_counter = stats_CounterGet( p_obj,
402 p_obj->p_vlc->i_object_id,
406 counter_sample_t *p_sample;
407 stats_Create( p_obj->p_vlc, psz_name, VLC_VAR_TIME, STATS_TIMER );
408 p_counter = stats_CounterGet( p_obj, p_obj->p_vlc->i_object_id,
410 if( !p_counter ) return;
411 /* 1st sample : if started: start_date, else last_time, b_started */
412 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
413 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
414 p_counter->i_samples, p_sample );
415 p_sample->date = 0; p_sample->value.b_bool = 0;
416 /* 2nd sample : global_time, i_samples */
417 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
418 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
419 p_counter->i_samples, p_sample );
420 p_sample->date = 0; p_sample->value.i_int = 0;
422 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
424 msg_Warn( p_obj, "timer %s was already started !", psz_name );
427 p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
428 p_counter->pp_samples[0]->date = mdate();
431 void __stats_TimerStop( vlc_object_t *p_obj, const char *psz_name )
433 counter_t *p_counter = stats_CounterGet( p_obj,
434 p_obj->p_vlc->i_object_id,
436 if( !p_counter || p_counter->i_samples != 2 )
438 msg_Err( p_obj, "timer %s does not exist", psz_name );
441 p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
442 p_counter->pp_samples[1]->value.i_int += 1;
443 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
444 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
447 void __stats_TimerDump( vlc_object_t *p_obj, const char *psz_name )
449 counter_t *p_counter = stats_CounterGet( p_obj,
450 p_obj->p_vlc->i_object_id,
452 TimerDump( p_obj, p_counter, VLC_TRUE );
456 void __stats_TimersDumpAll( vlc_object_t *p_obj )
459 stats_handler_t *p_handler = stats_HandlerGet( p_obj );
460 if( !p_handler ) return;
462 vlc_mutex_lock( &p_handler->object_lock );
463 for ( i = 0 ; i< p_handler->i_counters; i++ )
465 if( p_handler->pp_counters[i]->i_compute_type == STATS_TIMER )
467 TimerDump( p_obj, p_handler->pp_counters[i], VLC_FALSE );
470 vlc_mutex_unlock( &p_handler->object_lock );
474 /********************************************************************
475 * Following functions are local
476 ********************************************************************/
479 * Update a statistics counter, according to its type
480 * If needed, perform a bit of computation (derivative, mostly)
481 * This function must be entered with stats handler lock
482 * \param p_handler stats handler singleton
483 * \param p_counter the counter to update
484 * \param val the "new" value
485 * \return an error code
487 static int stats_CounterUpdate( stats_handler_t *p_handler,
488 counter_t *p_counter,
491 switch( p_counter->i_compute_type )
496 if( p_counter->i_samples > 1)
498 msg_Err( p_handler, "LAST counter has several samples !" );
501 if( p_counter->i_type != VLC_VAR_FLOAT &&
502 p_counter->i_type != VLC_VAR_INTEGER &&
503 p_counter->i_compute_type != STATS_LAST )
505 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
509 if( p_counter->i_samples == 0 )
511 counter_sample_t *p_new = (counter_sample_t*)malloc(
512 sizeof( counter_sample_t ) );
513 p_new->value.psz_string = NULL;
515 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
516 p_counter->i_samples, p_new );
518 if( p_counter->i_samples == 1 )
520 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
521 if( p_counter->i_compute_type == STATS_LAST ||
522 ( p_counter->i_compute_type == STATS_MAX &&
523 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
524 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
525 ( p_counter->i_type == VLC_VAR_FLOAT &&
526 p_counter->pp_samples[0]->value.f_float > val.f_float )
528 ( p_counter->i_compute_type == STATS_MIN &&
529 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
530 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
531 ( p_counter->i_type == VLC_VAR_FLOAT &&
532 p_counter->pp_samples[0]->value.f_float < val.f_float )
535 if( p_counter->i_type == VLC_VAR_STRING &&
536 p_counter->pp_samples[0]->value.psz_string )
538 free( p_counter->pp_samples[0]->value.psz_string );
540 p_counter->pp_samples[0]->value = val;
544 case STATS_DERIVATIVE:
546 counter_sample_t *p_new, *p_old;
547 if( mdate() - p_counter->last_update < p_counter->update_interval )
551 p_counter->last_update = mdate();
552 if( p_counter->i_type != VLC_VAR_FLOAT &&
553 p_counter->i_type != VLC_VAR_INTEGER )
555 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
558 /* Insert the new one at the beginning */
559 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
561 p_new->date = p_counter->last_update;
562 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
565 if( p_counter->i_samples == 3 )
567 p_old = p_counter->pp_samples[2];
568 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
574 if( p_counter->i_samples > 1)
576 msg_Err( p_handler, "LAST counter has several samples !" );
579 if( p_counter->i_samples == 0 )
581 counter_sample_t *p_new = (counter_sample_t*)malloc(
582 sizeof( counter_sample_t ) );
583 p_new->value.psz_string = NULL;
585 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
586 p_counter->i_samples, p_new );
588 if( p_counter->i_samples == 1 )
590 switch( p_counter->i_type )
592 case VLC_VAR_INTEGER:
594 p_counter->pp_samples[0]->value.i_int += val.i_int;
597 msg_Err( p_handler, "Trying to increment invalid variable %s",
598 p_counter->psz_name );
607 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
608 const char *psz_name )
611 for( i = 0; i< p_handler->i_counters; i++ )
613 counter_t *p_counter = p_handler->pp_counters[i];
614 if( p_counter->i_source_object == i_object_id &&
615 !strcmp( p_counter->psz_name, psz_name ) )
624 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
626 stats_handler_t *p_handler = (stats_handler_t*)
627 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
631 p_handler = stats_HandlerCreate( p_this );
636 vlc_object_yield( p_handler );
642 * Initialize statistics handler
644 * This function initializes the global statistics handler singleton,
645 * \param p_this the parent VLC object
647 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
649 stats_handler_t *p_handler;
651 msg_Dbg( p_this, "creating statistics handler" );
653 p_handler = (stats_handler_t*) vlc_object_create( p_this,
658 msg_Err( p_this, "out of memory" );
661 p_handler->i_counters = 0;
662 p_handler->pp_counters = NULL;
664 /// \bug is it p_vlc or p_libvlc ?
665 vlc_object_attach( p_handler, p_this->p_vlc );
670 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
675 if( !p_counter || p_counter->i_samples != 2 )
677 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
680 i_total = p_counter->pp_samples[1]->value.i_int;
681 total = p_counter->pp_samples[1]->date;
682 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
684 last = mdate() - p_counter->pp_samples[0]->date;
690 last = p_counter->pp_samples[0]->date;
695 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
696 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
697 (float)(total)/(1000*(float)i_total ) );
702 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
703 p_counter->psz_name, (float)total/1000, i_total,
704 (float)(total)/(1000*(float)i_total ) );