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 /*****************************************************************************
45 *****************************************************************************/
48 * Cleanup statistics handler stuff
49 * \param p_stats the handler to clean
52 void stats_HandlerDestroy( stats_handler_t *p_stats )
55 for ( i = p_stats->i_counters - 1 ; i >= 0 ; i-- )
58 counter_t * p_counter = p_stats->pp_counters[i];
60 for( j = p_counter->i_samples -1; j >= 0 ; j-- )
62 counter_sample_t *p_sample = p_counter->pp_samples[j];
63 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, j );
66 free( p_counter->psz_name );
67 REMOVE_ELEM( p_stats->pp_counters, p_stats->i_counters, i );
73 * Create a statistics counter
74 * \param p_this the object for which to create the counter
75 * \param psz_name the name
76 * \param i_type the type of stored data. One of VLC_VAR_STRING,
77 * VLC_VAR_INTEGER, VLC_VAR_FLOAT
78 * \param i_compute_type the aggregation type. One of STATS_LAST (always
79 * keep the last value), STATS_COUNTER (increment by the passed value),
80 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
81 * (keep a time derivative of the value)
83 int __stats_Create( vlc_object_t *p_this, const char *psz_name, int i_type,
87 stats_handler_t *p_handler;
89 if( p_this->p_libvlc->b_stats == VLC_FALSE )
93 p_handler = stats_HandlerGet( p_this );
94 if( !p_handler ) return VLC_ENOMEM;
96 vlc_mutex_lock( &p_handler->object_lock );
98 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
100 p_counter->psz_name = strdup( psz_name );
101 p_counter->i_source_object = p_this->i_object_id;
102 p_counter->i_compute_type = i_compute_type;
103 p_counter->i_type = i_type;
104 p_counter->i_samples = 0;
105 p_counter->pp_samples = NULL;
107 p_counter->update_interval = 0;
108 p_counter->last_update = 0;
110 INSERT_ELEM( p_handler->pp_counters,
111 p_handler->i_counters,
112 p_handler->i_counters,
115 vlc_mutex_unlock( &p_handler->object_lock );
120 /** Update a counter element with new values
121 * \param p_this the object in which to update
122 * \param psz_name the name
123 * \param val the vlc_value union containing the new value to aggregate. For
124 * more information on how data is aggregated, \see __stats_Create
126 int __stats_Update( vlc_object_t *p_this, const char *psz_name,
130 counter_t *p_counter;
132 /* Get stats handler singleton */
133 stats_handler_t *p_handler;
134 if( p_this->p_libvlc->b_stats == VLC_FALSE )
138 p_handler = stats_HandlerGet( p_this );
139 if( !p_handler ) return VLC_ENOMEM;
141 vlc_mutex_lock( &p_handler->object_lock );
142 /* Look for existing element */
143 p_counter = GetCounter( p_handler, p_this->i_object_id,
147 vlc_mutex_unlock( &p_handler->object_lock );
148 vlc_object_release( p_handler );
152 i_ret = stats_CounterUpdate( p_handler, p_counter, val );
153 vlc_mutex_unlock( &p_handler->object_lock );
158 /** Get the aggregated value for a counter
159 * \param p_this an object
160 * \param i_object_id the object id from which we want the data
161 * \param psz_name the name of the couner
162 * \param val a pointer to an initialized vlc_value union. It will contain the
164 * \return an error code
166 int __stats_Get( vlc_object_t *p_this, int i_object_id,
167 const char *psz_name, vlc_value_t *val )
169 counter_t *p_counter;
171 /* Get stats handler singleton */
172 stats_handler_t *p_handler;
173 if( p_this->p_libvlc->b_stats == VLC_FALSE )
177 p_handler = stats_HandlerGet( p_this );
178 if( !p_handler ) return VLC_ENOMEM;
179 vlc_mutex_lock( &p_handler->object_lock );
181 /* Look for existing element */
182 p_counter = GetCounter( p_handler, i_object_id,
186 vlc_mutex_unlock( &p_handler->object_lock );
187 vlc_object_release( p_handler );
188 val->i_int = val->f_float = 0.0;
192 if( p_counter->i_samples == 0 )
194 vlc_mutex_unlock( &p_handler->object_lock );
195 val->i_int = val->f_float = 0.0;
199 switch( p_counter->i_compute_type )
205 *val = p_counter->pp_samples[0]->value;
207 case STATS_DERIVATIVE:
209 if( p_counter->i_samples < 2 )
211 vlc_mutex_unlock( &p_handler->object_lock );
212 val->i_int = 0; val->f_float = 0.0;
215 if( p_counter->i_type == VLC_VAR_INTEGER )
217 float f = ( p_counter->pp_samples[0]->value.i_int -
218 p_counter->pp_samples[1]->value.i_int ) /
219 (float)( p_counter->pp_samples[0]->date -
220 p_counter->pp_samples[1]->date );
225 float f = (float)( p_counter->pp_samples[0]->value.f_float -
226 p_counter->pp_samples[1]->value.f_float ) /
227 (float)( p_counter->pp_samples[0]->date -
228 p_counter->pp_samples[1]->date );
233 vlc_object_release( p_handler );
235 vlc_mutex_unlock( &p_handler->object_lock );
239 /** Get a statistics counter structure. This allows for low-level modifications
240 * \param p_this a parent object
241 * \param i_object_id the object from which to retrieve data
242 * \param psz_name the name
243 * \return the counter, or NULL if not found (or handler not created yet)
245 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
246 const char *psz_name )
248 counter_t *p_counter;
250 stats_handler_t *p_handler;
251 if( p_this->p_libvlc->b_stats == VLC_FALSE )
255 p_handler = stats_HandlerGet( p_this );
256 if( !p_handler ) return NULL;
258 vlc_mutex_lock( &p_handler->object_lock );
260 /* Look for existing element */
261 p_counter = GetCounter( p_handler, i_object_id,
263 vlc_mutex_unlock( &p_handler->object_lock );
264 vlc_object_release( p_handler );
270 void stats_ComputeInputStats( input_thread_t *p_input,
271 input_stats_t *p_stats )
276 vlc_mutex_lock( &p_stats->lock );
279 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
280 &p_stats->i_read_packets );
281 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
282 &p_stats->i_read_bytes );
283 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
284 &p_stats->f_input_bitrate );
286 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
287 &p_stats->i_demux_read_bytes );
288 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
289 &p_stats->f_demux_bitrate );
291 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
292 &p_stats->i_decoded_video );
293 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
294 &p_stats->i_decoded_audio );
297 stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_packets",
298 &p_stats->i_sent_packets );
299 stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_bytes",
300 &p_stats->i_sent_bytes );
301 stats_GetFloat ( p_input, p_input->i_object_id, "sout_send_bitrate",
302 &p_stats->f_send_bitrate );
304 /* Aout - We store in p_input because aout is shared */
305 stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
306 &p_stats->i_played_abuffers );
307 stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
308 &p_stats->i_lost_abuffers );
310 /* Vouts - FIXME: Store all in input */
311 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
314 p_stats->i_displayed_pictures = 0 ;
315 p_stats->i_lost_pictures = 0;
316 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
318 int i_displayed = 0, i_lost = 0;
319 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
320 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
322 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
324 p_stats->i_displayed_pictures += i_displayed;
325 p_stats->i_lost_pictures += i_lost;
327 vlc_list_release( p_list );
330 vlc_mutex_unlock( &p_stats->lock );
334 void stats_ReinitInputStats( input_stats_t *p_stats )
336 p_stats->i_read_packets = p_stats->i_read_bytes =
337 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
338 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
339 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
340 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
341 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
342 p_stats->i_decoded_video = p_stats->i_decoded_audio =
343 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
347 void stats_DumpInputStats( input_stats_t *p_stats )
349 vlc_mutex_lock( &p_stats->lock );
350 /* f_bitrate is in bytes / microsecond
351 * *1000 => bytes / millisecond => kbytes / seconds */
352 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
353 " - Vout : %i/%i - Aout : %i/%i - Vout : %f\n",
354 p_stats->i_read_packets, p_stats->i_read_bytes,
355 p_stats->f_input_bitrate * 1000,
356 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
357 p_stats->f_demux_bitrate * 1000,
358 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
359 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
360 p_stats->f_send_bitrate );
361 vlc_mutex_unlock( &p_stats->lock );
364 void __stats_ComputeGlobalStats( vlc_object_t *p_obj,
365 global_stats_t *p_stats )
369 vlc_mutex_lock( &p_stats->lock );
371 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_CHILD );
374 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
376 float f_in = 0, f_out = 0;
377 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
378 stats_GetFloat( p_obj, p_obj->i_object_id, "input_bitrate",
380 stats_GetFloat( p_obj, p_obj->i_object_id, "sout_send_bitrate",
382 p_stats->f_input_bitrate += f_in;
383 p_stats->f_output_bitrate += f_out;
385 vlc_list_release( p_list );
388 vlc_mutex_unlock( &p_stats->lock );
391 void stats_ReinitGlobalStats( global_stats_t *p_stats )
393 p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
397 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name )
399 counter_t *p_counter = stats_CounterGet( p_obj,
400 p_obj->p_vlc->i_object_id,
404 counter_sample_t *p_sample;
405 stats_Create( p_obj->p_vlc, psz_name, VLC_VAR_TIME, STATS_TIMER );
406 p_counter = stats_CounterGet( p_obj, p_obj->p_vlc->i_object_id,
408 if( !p_counter ) return;
409 /* 1st sample : if started: start_date, else last_time, b_started */
410 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
411 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
412 p_counter->i_samples, p_sample );
413 p_sample->date = 0; p_sample->value.b_bool = 0;
414 /* 2nd sample : global_time, i_samples */
415 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
416 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
417 p_counter->i_samples, p_sample );
418 p_sample->date = 0; p_sample->value.i_int = 0;
420 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
422 msg_Warn( p_obj, "timer %s was already started !", psz_name );
425 p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
426 p_counter->pp_samples[0]->date = mdate();
429 void __stats_TimerStop( vlc_object_t *p_obj, const char *psz_name )
431 counter_t *p_counter = stats_CounterGet( p_obj,
432 p_obj->p_vlc->i_object_id,
434 if( !p_counter || p_counter->i_samples != 2 )
436 msg_Err( p_obj, "timer %s does not exist", psz_name );
439 p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
440 p_counter->pp_samples[1]->value.i_int += 1;
441 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
442 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
445 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 if( !p_counter || p_counter->i_samples != 2 )
454 msg_Err( p_obj, "timer %s does not exist", psz_name );
457 i_total = p_counter->pp_samples[1]->value.i_int;
458 total = p_counter->pp_samples[1]->date;
459 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
461 last = mdate() - p_counter->pp_samples[0]->date;
467 last = p_counter->pp_samples[0]->date;
469 msg_Dbg( p_obj, "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
470 psz_name, (float)last/1000, (float)total/1000, i_total,
471 (float)(total)/(1000*(float)i_total ) );
474 void __stats_TimersDumpAll( vlc_object_t *p_obj )
480 /********************************************************************
481 * Following functions are local
482 ********************************************************************/
485 * Update a statistics counter, according to its type
486 * If needed, perform a bit of computation (derivative, mostly)
487 * This function must be entered with stats handler lock
488 * \param p_handler stats handler singleton
489 * \param p_counter the counter to update
490 * \param val the "new" value
491 * \return an error code
493 static int stats_CounterUpdate( stats_handler_t *p_handler,
494 counter_t *p_counter,
497 switch( p_counter->i_compute_type )
502 if( p_counter->i_samples > 1)
504 msg_Err( p_handler, "LAST counter has several samples !" );
507 if( p_counter->i_type != VLC_VAR_FLOAT &&
508 p_counter->i_type != VLC_VAR_INTEGER &&
509 p_counter->i_compute_type != STATS_LAST )
511 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
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 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
527 if( p_counter->i_compute_type == STATS_LAST ||
528 ( p_counter->i_compute_type == STATS_MAX &&
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 )
534 ( p_counter->i_compute_type == STATS_MIN &&
535 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
536 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
537 ( p_counter->i_type == VLC_VAR_FLOAT &&
538 p_counter->pp_samples[0]->value.f_float < val.f_float )
541 if( p_counter->i_type == VLC_VAR_STRING &&
542 p_counter->pp_samples[0]->value.psz_string )
544 free( p_counter->pp_samples[0]->value.psz_string );
546 p_counter->pp_samples[0]->value = val;
550 case STATS_DERIVATIVE:
552 counter_sample_t *p_new, *p_old;
553 if( mdate() - p_counter->last_update < p_counter->update_interval )
557 p_counter->last_update = mdate();
558 if( p_counter->i_type != VLC_VAR_FLOAT &&
559 p_counter->i_type != VLC_VAR_INTEGER )
561 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
564 /* Insert the new one at the beginning */
565 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
567 p_new->date = p_counter->last_update;
568 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
571 if( p_counter->i_samples == 3 )
573 p_old = p_counter->pp_samples[2];
574 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
580 if( p_counter->i_samples > 1)
582 msg_Err( p_handler, "LAST counter has several samples !" );
585 if( p_counter->i_samples == 0 )
587 counter_sample_t *p_new = (counter_sample_t*)malloc(
588 sizeof( counter_sample_t ) );
589 p_new->value.psz_string = NULL;
591 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
592 p_counter->i_samples, p_new );
594 if( p_counter->i_samples == 1 )
596 switch( p_counter->i_type )
598 case VLC_VAR_INTEGER:
600 p_counter->pp_samples[0]->value.i_int += val.i_int;
603 msg_Err( p_handler, "Trying to increment invalid variable %s",
604 p_counter->psz_name );
613 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
614 const char *psz_name )
617 for( i = 0; i< p_handler->i_counters; i++ )
619 counter_t *p_counter = p_handler->pp_counters[i];
620 if( p_counter->i_source_object == i_object_id &&
621 !strcmp( p_counter->psz_name, psz_name ) )
630 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
632 stats_handler_t *p_handler = (stats_handler_t*)
633 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
637 p_handler = stats_HandlerCreate( p_this );
642 vlc_object_yield( p_handler );
648 * Initialize statistics handler
650 * This function initializes the global statistics handler singleton,
651 * \param p_this the parent VLC object
653 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
655 stats_handler_t *p_handler;
657 msg_Dbg( p_this, "creating statistics handler" );
659 p_handler = (stats_handler_t*) vlc_object_create( p_this,
664 msg_Err( p_this, "out of memory" );
667 p_handler->i_counters = 0;
668 p_handler->pp_counters = NULL;
670 /// \bug is it p_vlc or p_libvlc ?
671 vlc_object_attach( p_handler, p_this->p_vlc );