1 /*****************************************************************************
2 * stats.c: Statistics handling
3 *****************************************************************************
4 * Copyright (C) 1998-2005 the VideoLAN team
5 * $Id: messages.c 12729 2005-10-02 08:00:06Z courmisch $
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 * 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 if( p_sample->val.psz_string )
64 free( p_sample->val.psz_string );
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_counter, 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, 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, char *psz_name, vlc_value_t val )
131 counter_t *p_counter;
133 /* Get stats handler singleton */
134 stats_handler_t *p_handler;
135 if( p_this->p_libvlc->b_stats == VLC_FALSE )
139 p_handler = stats_HandlerGet( p_this );
140 if( !p_handler ) return VLC_ENOMEM;
142 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, char *psz_name, vlc_value_t *val )
170 counter_t *p_counter;
172 /* Get stats handler singleton */
173 stats_handler_t *p_handler;
174 if( p_this->p_libvlc->b_stats == VLC_FALSE )
178 p_handler = stats_HandlerGet( p_this );
179 if( !p_handler ) return VLC_ENOMEM;
180 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 );
193 if( p_counter->i_samples == 0 )
195 vlc_mutex_unlock( &p_handler->object_lock );
196 val->i_int = val->f_float = 0.0;
200 switch( p_counter->i_compute_type )
206 *val = p_counter->pp_samples[0]->value;
208 case STATS_DERIVATIVE:
210 if( p_counter->i_samples < 2 )
212 vlc_mutex_unlock( &p_handler->object_lock );
213 val->i_int = 0; val->f_float = 0.0;
216 if( p_counter->i_type == VLC_VAR_INTEGER )
218 float f = ( p_counter->pp_samples[0]->value.i_int -
219 p_counter->pp_samples[1]->value.i_int ) /
220 (float)( p_counter->pp_samples[0]->date -
221 p_counter->pp_samples[1]->date );
226 float f = (float)( p_counter->pp_samples[0]->value.f_float -
227 p_counter->pp_samples[1]->value.f_float ) /
228 (float)( p_counter->pp_samples[0]->date -
229 p_counter->pp_samples[1]->date );
234 vlc_object_release( p_handler );
236 vlc_mutex_unlock( &p_handler->object_lock );
240 /** Get a statistics counter structure. This allows for low-level modifications
241 * \param p_this a parent object
242 * \param i_object_id the object from which to retrieve data
243 * \param psz_name the name
244 * \return the counter, or NULL if not found (or handler not created yet)
246 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
249 counter_t *p_counter;
251 stats_handler_t *p_handler;
252 if( p_this->p_libvlc->b_stats == VLC_FALSE )
256 p_handler = stats_HandlerGet( p_this );
257 if( !p_handler ) return NULL;
259 vlc_mutex_lock( &p_handler->object_lock );
261 /* Look for existing element */
262 p_counter = GetCounter( p_handler, p_this->i_object_id,
264 vlc_mutex_unlock( &p_handler->object_lock );
265 vlc_object_release( p_handler );
271 void stats_ComputeInputStats( input_thread_t *p_input,
272 input_stats_t *p_stats )
277 vlc_mutex_lock( &p_stats->lock );
280 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
281 &p_stats->i_read_packets );
282 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
283 &p_stats->i_read_bytes );
284 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
285 &p_stats->f_input_bitrate );
287 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
288 &p_stats->i_demux_read_bytes );
289 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
290 &p_stats->f_demux_bitrate );
292 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
293 &p_stats->i_decoded_video );
294 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
295 &p_stats->i_decoded_audio );
298 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
301 p_stats->i_displayed_pictures = 0 ;
302 p_stats->i_lost_pictures = 0;
303 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
305 int i_displayed = 0, i_lost = 0;
306 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
307 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
309 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
311 p_stats->i_displayed_pictures += i_displayed;
312 p_stats->i_lost_pictures += i_lost;
314 vlc_list_release( p_list );
316 vlc_mutex_unlock( &p_stats->lock );
319 void stats_ReinitInputStats( input_stats_t *p_stats )
321 p_stats->i_read_packets = p_stats->i_read_bytes =
322 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
323 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
324 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
325 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
326 p_stats->i_decoded_video = p_stats->i_decoded_audio = 0;
329 void stats_DumpInputStats( input_stats_t *p_stats )
331 vlc_mutex_lock( &p_stats->lock );
332 /* f_bitrate is in bytes / microsecond
333 * *1000 => bytes / millisecond => kbytes / seconds */
334 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
335 "Demux : %i (%i bytes) - %f kB/s - Vout : %i/%i\n",
336 p_stats->i_read_packets, p_stats->i_read_bytes,
337 p_stats->f_input_bitrate * 1000,
338 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
339 p_stats->f_demux_bitrate * 1000,
340 p_stats->i_displayed_pictures, p_stats->i_lost_pictures );
341 vlc_mutex_unlock( &p_stats->lock );
345 /********************************************************************
346 * Following functions are local
347 ********************************************************************/
350 * Update a statistics counter, according to its type
351 * If needed, perform a bit of computation (derivative, mostly)
352 * This function must be entered with stats handler lock
353 * \param p_handler stats handler singleton
354 * \param p_counter the counter to update
355 * \param val the "new" value
356 * \return an error code
358 static int stats_CounterUpdate( stats_handler_t *p_handler,
359 counter_t *p_counter,
362 switch( p_counter->i_compute_type )
367 if( p_counter->i_samples > 1)
369 msg_Err( p_handler, "LAST counter has several samples !" );
372 if( p_counter->i_type != VLC_VAR_FLOAT &&
373 p_counter->i_type != VLC_VAR_INTEGER &&
374 p_counter->i_compute_type != STATS_LAST )
376 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
380 if( p_counter->i_samples == 0 )
382 counter_sample_t *p_new = (counter_sample_t*)malloc(
383 sizeof( counter_sample_t ) );
384 p_new->value.psz_string = NULL;
386 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
387 p_counter->i_samples, p_new );
389 if( p_counter->i_samples == 1 )
391 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
392 if( p_counter->i_compute_type == STATS_LAST ||
393 ( p_counter->i_compute_type == STATS_MAX &&
394 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
395 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
396 ( p_counter->i_type == VLC_VAR_FLOAT &&
397 p_counter->pp_samples[0]->value.f_float > val.f_float )
399 ( p_counter->i_compute_type == STATS_MIN &&
400 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
401 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
402 ( p_counter->i_type == VLC_VAR_FLOAT &&
403 p_counter->pp_samples[0]->value.f_float < val.f_float )
406 if( p_counter->i_type == VLC_VAR_STRING &&
407 p_counter->pp_samples[0]->value.psz_string )
409 free( p_counter->pp_samples[0]->value.psz_string );
411 p_counter->pp_samples[0]->value = val;
415 case STATS_DERIVATIVE:
417 counter_sample_t *p_new, *p_old;
418 if( mdate() - p_counter->last_update < p_counter->update_interval )
422 p_counter->last_update = mdate();
423 if( p_counter->i_type != VLC_VAR_FLOAT &&
424 p_counter->i_type != VLC_VAR_INTEGER )
426 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
429 /* Insert the new one at the beginning */
430 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
432 p_new->date = p_counter->last_update;
433 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
436 if( p_counter->i_samples == 3 )
438 p_old = p_counter->pp_samples[2];
439 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
445 if( p_counter->i_samples > 1)
447 msg_Err( p_handler, "LAST counter has several samples !" );
450 if( p_counter->i_samples == 0 )
452 counter_sample_t *p_new = (counter_sample_t*)malloc(
453 sizeof( counter_sample_t ) );
454 p_new->value.psz_string = NULL;
456 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
457 p_counter->i_samples, p_new );
459 if( p_counter->i_samples == 1 )
461 switch( p_counter->i_type )
463 case VLC_VAR_INTEGER:
465 p_counter->pp_samples[0]->value.i_int += val.i_int;
468 msg_Err( p_handler, "Trying to increment invalid variable %s",
469 p_counter->psz_name );
478 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
482 for( i = 0; i< p_handler->i_counters; i++ )
484 counter_t *p_counter = p_handler->pp_counters[i];
485 if( p_counter->i_source_object == i_object_id &&
486 !strcmp( p_counter->psz_name, psz_name ) )
495 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
497 stats_handler_t *p_handler = (stats_handler_t*)
498 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
502 p_handler = stats_HandlerCreate( p_this );
507 vlc_object_yield( p_handler );
513 * Initialize statistics handler
515 * This function initializes the global statistics handler singleton,
516 * \param p_this the parent VLC object
518 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
520 stats_handler_t *p_handler;
522 msg_Dbg( p_this, "creating statistics handler" );
524 p_handler = (stats_handler_t*) vlc_object_create( p_this,
529 msg_Err( p_this, "out of memory" );
532 p_handler->i_counters = 0;
533 p_handler->pp_counters = NULL;
535 /// \bug is it p_vlc or p_libvlc ?
536 vlc_object_attach( p_handler, p_this->p_vlc );