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 );
189 if( p_counter->i_samples == 0 )
191 vlc_mutex_unlock( &p_handler->object_lock );
192 val->i_int = val->f_float = 0.0;
196 switch( p_counter->i_compute_type )
202 *val = p_counter->pp_samples[0]->value;
204 case STATS_DERIVATIVE:
206 if( p_counter->i_samples < 2 )
208 vlc_mutex_unlock( &p_handler->object_lock );
209 val->i_int = 0; val->f_float = 0.0;
212 if( p_counter->i_type == VLC_VAR_INTEGER )
214 float f = ( p_counter->pp_samples[0]->value.i_int -
215 p_counter->pp_samples[1]->value.i_int ) /
216 (float)( p_counter->pp_samples[0]->date -
217 p_counter->pp_samples[1]->date );
222 float f = (float)( p_counter->pp_samples[0]->value.f_float -
223 p_counter->pp_samples[1]->value.f_float ) /
224 (float)( p_counter->pp_samples[0]->date -
225 p_counter->pp_samples[1]->date );
230 vlc_object_release( p_handler );
232 vlc_mutex_unlock( &p_handler->object_lock );
236 /** Get a statistics counter structure. This allows for low-level modifications
237 * \param p_this a parent object
238 * \param i_object_id the object from which to retrieve data
239 * \param psz_name the name
240 * \return the counter, or NULL if not found (or handler not created yet)
242 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
245 counter_t *p_counter;
247 stats_handler_t *p_handler;
248 if( p_this->p_libvlc->b_stats == VLC_FALSE )
252 p_handler = stats_HandlerGet( p_this );
253 if( !p_handler ) return NULL;
255 vlc_mutex_lock( &p_handler->object_lock );
257 /* Look for existing element */
258 p_counter = GetCounter( p_handler, p_this->i_object_id,
260 vlc_mutex_unlock( &p_handler->object_lock );
261 vlc_object_release( p_handler );
267 void stats_ComputeInputStats( input_thread_t *p_input,
268 input_stats_t *p_stats )
273 vlc_mutex_lock( &p_stats->lock );
276 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
277 &p_stats->i_read_packets );
278 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
279 &p_stats->i_read_bytes );
280 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
281 &p_stats->f_input_bitrate );
283 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
284 &p_stats->i_demux_read_bytes );
285 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
286 &p_stats->f_demux_bitrate );
288 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
289 &p_stats->i_decoded_video );
290 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
291 &p_stats->i_decoded_audio );
293 /* Aout - We store in p_input because aout is shared */
294 stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
295 &p_stats->i_played_abuffers );
296 stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
297 &p_stats->i_lost_abuffers );
300 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
303 p_stats->i_displayed_pictures = 0 ;
304 p_stats->i_lost_pictures = 0;
305 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
307 int i_displayed = 0, i_lost = 0;
308 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
309 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
311 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
313 p_stats->i_displayed_pictures += i_displayed;
314 p_stats->i_lost_pictures += i_lost;
316 vlc_list_release( p_list );
319 vlc_mutex_unlock( &p_stats->lock );
322 void stats_ReinitInputStats( input_stats_t *p_stats )
324 p_stats->i_read_packets = p_stats->i_read_bytes =
325 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
326 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
327 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
328 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
329 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
330 p_stats->i_decoded_video = p_stats->i_decoded_audio = 0;
333 void stats_DumpInputStats( input_stats_t *p_stats )
335 vlc_mutex_lock( &p_stats->lock );
336 /* f_bitrate is in bytes / microsecond
337 * *1000 => bytes / millisecond => kbytes / seconds */
338 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
339 " - Vout : %i/%i - Aout : %i/%i\n",
340 p_stats->i_read_packets, p_stats->i_read_bytes,
341 p_stats->f_input_bitrate * 1000,
342 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
343 p_stats->f_demux_bitrate * 1000,
344 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
345 p_stats->i_played_abuffers, p_stats->i_lost_abuffers );
346 vlc_mutex_unlock( &p_stats->lock );
350 /********************************************************************
351 * Following functions are local
352 ********************************************************************/
355 * Update a statistics counter, according to its type
356 * If needed, perform a bit of computation (derivative, mostly)
357 * This function must be entered with stats handler lock
358 * \param p_handler stats handler singleton
359 * \param p_counter the counter to update
360 * \param val the "new" value
361 * \return an error code
363 static int stats_CounterUpdate( stats_handler_t *p_handler,
364 counter_t *p_counter,
367 switch( p_counter->i_compute_type )
372 if( p_counter->i_samples > 1)
374 msg_Err( p_handler, "LAST counter has several samples !" );
377 if( p_counter->i_type != VLC_VAR_FLOAT &&
378 p_counter->i_type != VLC_VAR_INTEGER &&
379 p_counter->i_compute_type != STATS_LAST )
381 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
385 if( p_counter->i_samples == 0 )
387 counter_sample_t *p_new = (counter_sample_t*)malloc(
388 sizeof( counter_sample_t ) );
389 p_new->value.psz_string = NULL;
391 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
392 p_counter->i_samples, p_new );
394 if( p_counter->i_samples == 1 )
396 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
397 if( p_counter->i_compute_type == STATS_LAST ||
398 ( p_counter->i_compute_type == STATS_MAX &&
399 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
400 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
401 ( p_counter->i_type == VLC_VAR_FLOAT &&
402 p_counter->pp_samples[0]->value.f_float > val.f_float )
404 ( p_counter->i_compute_type == STATS_MIN &&
405 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
406 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
407 ( p_counter->i_type == VLC_VAR_FLOAT &&
408 p_counter->pp_samples[0]->value.f_float < val.f_float )
411 if( p_counter->i_type == VLC_VAR_STRING &&
412 p_counter->pp_samples[0]->value.psz_string )
414 free( p_counter->pp_samples[0]->value.psz_string );
416 p_counter->pp_samples[0]->value = val;
420 case STATS_DERIVATIVE:
422 counter_sample_t *p_new, *p_old;
423 if( mdate() - p_counter->last_update < p_counter->update_interval )
427 p_counter->last_update = mdate();
428 if( p_counter->i_type != VLC_VAR_FLOAT &&
429 p_counter->i_type != VLC_VAR_INTEGER )
431 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
434 /* Insert the new one at the beginning */
435 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
437 p_new->date = p_counter->last_update;
438 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
441 if( p_counter->i_samples == 3 )
443 p_old = p_counter->pp_samples[2];
444 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
450 if( p_counter->i_samples > 1)
452 msg_Err( p_handler, "LAST counter has several samples !" );
455 if( p_counter->i_samples == 0 )
457 counter_sample_t *p_new = (counter_sample_t*)malloc(
458 sizeof( counter_sample_t ) );
459 p_new->value.psz_string = NULL;
461 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
462 p_counter->i_samples, p_new );
464 if( p_counter->i_samples == 1 )
466 switch( p_counter->i_type )
468 case VLC_VAR_INTEGER:
470 p_counter->pp_samples[0]->value.i_int += val.i_int;
473 msg_Err( p_handler, "Trying to increment invalid variable %s",
474 p_counter->psz_name );
483 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
487 for( i = 0; i< p_handler->i_counters; i++ )
489 counter_t *p_counter = p_handler->pp_counters[i];
490 if( p_counter->i_source_object == i_object_id &&
491 !strcmp( p_counter->psz_name, psz_name ) )
500 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
502 stats_handler_t *p_handler = (stats_handler_t*)
503 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
507 p_handler = stats_HandlerCreate( p_this );
512 vlc_object_yield( p_handler );
518 * Initialize statistics handler
520 * This function initializes the global statistics handler singleton,
521 * \param p_this the parent VLC object
523 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
525 stats_handler_t *p_handler;
527 msg_Dbg( p_this, "creating statistics handler" );
529 p_handler = (stats_handler_t*) vlc_object_create( p_this,
534 msg_Err( p_this, "out of memory" );
537 p_handler->i_counters = 0;
538 p_handler->pp_counters = NULL;
540 /// \bug is it p_vlc or p_libvlc ?
541 vlc_object_attach( p_handler, p_this->p_vlc );