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,
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, 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, char *psz_name, vlc_value_t val )
129 counter_t *p_counter;
131 /* Get stats handler singleton */
132 stats_handler_t *p_handler;
133 if( p_this->p_libvlc->b_stats == VLC_FALSE )
137 p_handler = stats_HandlerGet( p_this );
138 if( !p_handler ) return VLC_ENOMEM;
140 vlc_mutex_lock( &p_handler->object_lock );
141 /* Look for existing element */
142 p_counter = GetCounter( p_handler, p_this->i_object_id,
146 vlc_mutex_unlock( &p_handler->object_lock );
147 vlc_object_release( p_handler );
151 i_ret = stats_CounterUpdate( p_handler, p_counter, val );
152 vlc_mutex_unlock( &p_handler->object_lock );
157 /** Get the aggregated value for a counter
158 * \param p_this an object
159 * \param i_object_id the object id from which we want the data
160 * \param psz_name the name of the couner
161 * \param val a pointer to an initialized vlc_value union. It will contain the
163 * \return an error code
165 int __stats_Get( vlc_object_t *p_this, int i_object_id, char *psz_name, vlc_value_t *val )
167 counter_t *p_counter;
169 /* Get stats handler singleton */
170 stats_handler_t *p_handler;
171 if( p_this->p_libvlc->b_stats == VLC_FALSE )
175 p_handler = stats_HandlerGet( p_this );
176 if( !p_handler ) return VLC_ENOMEM;
177 vlc_mutex_lock( &p_handler->object_lock );
179 /* Look for existing element */
180 p_counter = GetCounter( p_handler, i_object_id,
184 vlc_mutex_unlock( &p_handler->object_lock );
185 vlc_object_release( p_handler );
186 val->i_int = val->f_float = 0.0;
190 if( p_counter->i_samples == 0 )
192 vlc_mutex_unlock( &p_handler->object_lock );
193 val->i_int = val->f_float = 0.0;
197 switch( p_counter->i_compute_type )
203 *val = p_counter->pp_samples[0]->value;
205 case STATS_DERIVATIVE:
207 if( p_counter->i_samples < 2 )
209 vlc_mutex_unlock( &p_handler->object_lock );
210 val->i_int = 0; val->f_float = 0.0;
213 if( p_counter->i_type == VLC_VAR_INTEGER )
215 float f = ( p_counter->pp_samples[0]->value.i_int -
216 p_counter->pp_samples[1]->value.i_int ) /
217 (float)( p_counter->pp_samples[0]->date -
218 p_counter->pp_samples[1]->date );
223 float f = (float)( p_counter->pp_samples[0]->value.f_float -
224 p_counter->pp_samples[1]->value.f_float ) /
225 (float)( p_counter->pp_samples[0]->date -
226 p_counter->pp_samples[1]->date );
231 vlc_object_release( p_handler );
233 vlc_mutex_unlock( &p_handler->object_lock );
237 /** Get a statistics counter structure. This allows for low-level modifications
238 * \param p_this a parent object
239 * \param i_object_id the object from which to retrieve data
240 * \param psz_name the name
241 * \return the counter, or NULL if not found (or handler not created yet)
243 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
246 counter_t *p_counter;
248 stats_handler_t *p_handler;
249 if( p_this->p_libvlc->b_stats == VLC_FALSE )
253 p_handler = stats_HandlerGet( p_this );
254 if( !p_handler ) return NULL;
256 vlc_mutex_lock( &p_handler->object_lock );
258 /* Look for existing element */
259 p_counter = GetCounter( p_handler, i_object_id,
261 vlc_mutex_unlock( &p_handler->object_lock );
262 vlc_object_release( p_handler );
268 void stats_ComputeInputStats( input_thread_t *p_input,
269 input_stats_t *p_stats )
274 vlc_mutex_lock( &p_stats->lock );
277 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
278 &p_stats->i_read_packets );
279 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
280 &p_stats->i_read_bytes );
281 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
282 &p_stats->f_input_bitrate );
284 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
285 &p_stats->i_demux_read_bytes );
286 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
287 &p_stats->f_demux_bitrate );
289 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
290 &p_stats->i_decoded_video );
291 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
292 &p_stats->i_decoded_audio );
295 stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_packets",
296 &p_stats->i_sent_packets );
297 stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_bytes",
298 &p_stats->i_sent_bytes );
299 stats_GetFloat ( p_input, p_input->i_object_id, "sout_send_bitrate",
300 &p_stats->f_send_bitrate );
302 /* Aout - We store in p_input because aout is shared */
303 stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
304 &p_stats->i_played_abuffers );
305 stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
306 &p_stats->i_lost_abuffers );
308 /* Vouts - FIXME: Store all in input */
309 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
312 p_stats->i_displayed_pictures = 0 ;
313 p_stats->i_lost_pictures = 0;
314 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
316 int i_displayed = 0, i_lost = 0;
317 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
318 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
320 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
322 p_stats->i_displayed_pictures += i_displayed;
323 p_stats->i_lost_pictures += i_lost;
325 vlc_list_release( p_list );
328 vlc_mutex_unlock( &p_stats->lock );
331 void stats_ReinitInputStats( input_stats_t *p_stats )
333 p_stats->i_read_packets = p_stats->i_read_bytes =
334 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
335 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
336 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
337 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
338 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
339 p_stats->i_decoded_video = p_stats->i_decoded_audio =
340 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
344 void stats_DumpInputStats( input_stats_t *p_stats )
346 vlc_mutex_lock( &p_stats->lock );
347 /* f_bitrate is in bytes / microsecond
348 * *1000 => bytes / millisecond => kbytes / seconds */
349 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
350 " - Vout : %i/%i - Aout : %i/%i - Vout : %f\n",
351 p_stats->i_read_packets, p_stats->i_read_bytes,
352 p_stats->f_input_bitrate * 1000,
353 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
354 p_stats->f_demux_bitrate * 1000,
355 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
356 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
357 p_stats->f_send_bitrate );
358 vlc_mutex_unlock( &p_stats->lock );
362 /********************************************************************
363 * Following functions are local
364 ********************************************************************/
367 * Update a statistics counter, according to its type
368 * If needed, perform a bit of computation (derivative, mostly)
369 * This function must be entered with stats handler lock
370 * \param p_handler stats handler singleton
371 * \param p_counter the counter to update
372 * \param val the "new" value
373 * \return an error code
375 static int stats_CounterUpdate( stats_handler_t *p_handler,
376 counter_t *p_counter,
379 switch( p_counter->i_compute_type )
384 if( p_counter->i_samples > 1)
386 msg_Err( p_handler, "LAST counter has several samples !" );
389 if( p_counter->i_type != VLC_VAR_FLOAT &&
390 p_counter->i_type != VLC_VAR_INTEGER &&
391 p_counter->i_compute_type != STATS_LAST )
393 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
397 if( p_counter->i_samples == 0 )
399 counter_sample_t *p_new = (counter_sample_t*)malloc(
400 sizeof( counter_sample_t ) );
401 p_new->value.psz_string = NULL;
403 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
404 p_counter->i_samples, p_new );
406 if( p_counter->i_samples == 1 )
408 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
409 if( p_counter->i_compute_type == STATS_LAST ||
410 ( p_counter->i_compute_type == STATS_MAX &&
411 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
412 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
413 ( p_counter->i_type == VLC_VAR_FLOAT &&
414 p_counter->pp_samples[0]->value.f_float > val.f_float )
416 ( p_counter->i_compute_type == STATS_MIN &&
417 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
418 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
419 ( p_counter->i_type == VLC_VAR_FLOAT &&
420 p_counter->pp_samples[0]->value.f_float < val.f_float )
423 if( p_counter->i_type == VLC_VAR_STRING &&
424 p_counter->pp_samples[0]->value.psz_string )
426 free( p_counter->pp_samples[0]->value.psz_string );
428 p_counter->pp_samples[0]->value = val;
432 case STATS_DERIVATIVE:
434 counter_sample_t *p_new, *p_old;
435 if( mdate() - p_counter->last_update < p_counter->update_interval )
439 p_counter->last_update = mdate();
440 if( p_counter->i_type != VLC_VAR_FLOAT &&
441 p_counter->i_type != VLC_VAR_INTEGER )
443 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
446 /* Insert the new one at the beginning */
447 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
449 p_new->date = p_counter->last_update;
450 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
453 if( p_counter->i_samples == 3 )
455 p_old = p_counter->pp_samples[2];
456 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
462 if( p_counter->i_samples > 1)
464 msg_Err( p_handler, "LAST counter has several samples !" );
467 if( p_counter->i_samples == 0 )
469 counter_sample_t *p_new = (counter_sample_t*)malloc(
470 sizeof( counter_sample_t ) );
471 p_new->value.psz_string = NULL;
473 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
474 p_counter->i_samples, p_new );
476 if( p_counter->i_samples == 1 )
478 switch( p_counter->i_type )
480 case VLC_VAR_INTEGER:
482 p_counter->pp_samples[0]->value.i_int += val.i_int;
485 msg_Err( p_handler, "Trying to increment invalid variable %s",
486 p_counter->psz_name );
495 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
499 for( i = 0; i< p_handler->i_counters; i++ )
501 counter_t *p_counter = p_handler->pp_counters[i];
502 if( p_counter->i_source_object == i_object_id &&
503 !strcmp( p_counter->psz_name, psz_name ) )
512 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
514 stats_handler_t *p_handler = (stats_handler_t*)
515 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
519 p_handler = stats_HandlerCreate( p_this );
524 vlc_object_yield( p_handler );
530 * Initialize statistics handler
532 * This function initializes the global statistics handler singleton,
533 * \param p_this the parent VLC object
535 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
537 stats_handler_t *p_handler;
539 msg_Dbg( p_this, "creating statistics handler" );
541 p_handler = (stats_handler_t*) vlc_object_create( p_this,
546 msg_Err( p_this, "out of memory" );
549 p_handler->i_counters = 0;
550 p_handler->pp_counters = NULL;
552 /// \bug is it p_vlc or p_libvlc ?
553 vlc_object_attach( p_handler, p_this->p_vlc );