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_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 );
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, char *psz_name, vlc_value_t *val )
168 counter_t *p_counter;
170 /* Get stats handler singleton */
171 stats_handler_t *p_handler;
172 if( p_this->p_libvlc->b_stats == VLC_FALSE )
176 p_handler = stats_HandlerGet( p_this );
177 if( !p_handler ) return VLC_ENOMEM;
178 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 );
191 if( p_counter->i_samples == 0 )
193 vlc_mutex_unlock( &p_handler->object_lock );
194 val->i_int = val->f_float = 0.0;
198 switch( p_counter->i_compute_type )
204 *val = p_counter->pp_samples[0]->value;
206 case STATS_DERIVATIVE:
208 if( p_counter->i_samples < 2 )
210 vlc_mutex_unlock( &p_handler->object_lock );
211 val->i_int = 0; val->f_float = 0.0;
214 if( p_counter->i_type == VLC_VAR_INTEGER )
216 float f = ( p_counter->pp_samples[0]->value.i_int -
217 p_counter->pp_samples[1]->value.i_int ) /
218 (float)( p_counter->pp_samples[0]->date -
219 p_counter->pp_samples[1]->date );
224 float f = (float)( p_counter->pp_samples[0]->value.f_float -
225 p_counter->pp_samples[1]->value.f_float ) /
226 (float)( p_counter->pp_samples[0]->date -
227 p_counter->pp_samples[1]->date );
232 vlc_object_release( p_handler );
234 vlc_mutex_unlock( &p_handler->object_lock );
238 /** Get a statistics counter structure. This allows for low-level modifications
239 * \param p_this a parent object
240 * \param i_object_id the object from which to retrieve data
241 * \param psz_name the name
242 * \return the counter, or NULL if not found (or handler not created yet)
244 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
247 counter_t *p_counter;
249 stats_handler_t *p_handler;
250 if( p_this->p_libvlc->b_stats == VLC_FALSE )
254 p_handler = stats_HandlerGet( p_this );
255 if( !p_handler ) return NULL;
257 vlc_mutex_lock( &p_handler->object_lock );
259 /* Look for existing element */
260 p_counter = GetCounter( p_handler, p_this->i_object_id,
262 vlc_mutex_unlock( &p_handler->object_lock );
263 vlc_object_release( p_handler );
269 void stats_ComputeInputStats( input_thread_t *p_input,
270 input_stats_t *p_stats )
275 vlc_mutex_lock( &p_stats->lock );
278 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
279 &p_stats->i_read_packets );
280 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
281 &p_stats->i_read_bytes );
282 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
283 &p_stats->f_input_bitrate );
285 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
286 &p_stats->i_demux_read_bytes );
287 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
288 &p_stats->f_demux_bitrate );
290 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
291 &p_stats->i_decoded_video );
292 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
293 &p_stats->i_decoded_audio );
296 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
299 p_stats->i_displayed_pictures = 0 ;
300 p_stats->i_lost_pictures = 0;
301 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
303 int i_displayed = 0, i_lost = 0;
304 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
305 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
307 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
309 p_stats->i_displayed_pictures += i_displayed;
310 p_stats->i_lost_pictures += i_lost;
312 vlc_list_release( p_list );
314 vlc_mutex_unlock( &p_stats->lock );
317 void stats_ReinitInputStats( input_stats_t *p_stats )
319 p_stats->i_read_packets = p_stats->i_read_bytes =
320 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
321 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
322 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
323 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
324 p_stats->i_decoded_video = p_stats->i_decoded_audio = 0;
327 void stats_DumpInputStats( input_stats_t *p_stats )
329 vlc_mutex_lock( &p_stats->lock );
330 /* f_bitrate is in bytes / microsecond
331 * *1000 => bytes / millisecond => kbytes / seconds */
332 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
333 "Demux : %i (%i bytes) - %f kB/s - Vout : %i/%i\n",
334 p_stats->i_read_packets, p_stats->i_read_bytes,
335 p_stats->f_input_bitrate * 1000,
336 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
337 p_stats->f_demux_bitrate * 1000,
338 p_stats->i_displayed_pictures, p_stats->i_lost_pictures );
339 vlc_mutex_unlock( &p_stats->lock );
343 /********************************************************************
344 * Following functions are local
345 ********************************************************************/
348 * Update a statistics counter, according to its type
349 * If needed, perform a bit of computation (derivative, mostly)
350 * This function must be entered with stats handler lock
351 * \param p_handler stats handler singleton
352 * \param p_counter the counter to update
353 * \param val the "new" value
354 * \return an error code
356 static int stats_CounterUpdate( stats_handler_t *p_handler,
357 counter_t *p_counter,
360 switch( p_counter->i_compute_type )
365 if( p_counter->i_samples > 1)
367 msg_Err( p_handler, "LAST counter has several samples !" );
370 if( p_counter->i_type != VLC_VAR_FLOAT &&
371 p_counter->i_type != VLC_VAR_INTEGER &&
372 p_counter->i_compute_type != STATS_LAST )
374 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
378 if( p_counter->i_samples == 0 )
380 counter_sample_t *p_new = (counter_sample_t*)malloc(
381 sizeof( counter_sample_t ) );
382 p_new->value.psz_string = NULL;
384 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
385 p_counter->i_samples, p_new );
387 if( p_counter->i_samples == 1 )
389 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
390 if( p_counter->i_compute_type == STATS_LAST ||
391 ( p_counter->i_compute_type == STATS_MAX &&
392 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
393 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
394 ( p_counter->i_type == VLC_VAR_FLOAT &&
395 p_counter->pp_samples[0]->value.f_float > val.f_float )
397 ( p_counter->i_compute_type == STATS_MIN &&
398 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
399 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
400 ( p_counter->i_type == VLC_VAR_FLOAT &&
401 p_counter->pp_samples[0]->value.f_float < val.f_float )
404 if( p_counter->i_type == VLC_VAR_STRING &&
405 p_counter->pp_samples[0]->value.psz_string )
407 free( p_counter->pp_samples[0]->value.psz_string );
409 p_counter->pp_samples[0]->value = val;
413 case STATS_DERIVATIVE:
415 counter_sample_t *p_new, *p_old;
416 if( mdate() - p_counter->last_update < p_counter->update_interval )
420 p_counter->last_update = mdate();
421 if( p_counter->i_type != VLC_VAR_FLOAT &&
422 p_counter->i_type != VLC_VAR_INTEGER )
424 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
427 /* Insert the new one at the beginning */
428 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
430 p_new->date = p_counter->last_update;
431 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
434 if( p_counter->i_samples == 3 )
436 p_old = p_counter->pp_samples[2];
437 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
443 if( p_counter->i_samples > 1)
445 msg_Err( p_handler, "LAST counter has several samples !" );
448 if( p_counter->i_samples == 0 )
450 counter_sample_t *p_new = (counter_sample_t*)malloc(
451 sizeof( counter_sample_t ) );
452 p_new->value.psz_string = NULL;
454 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
455 p_counter->i_samples, p_new );
457 if( p_counter->i_samples == 1 )
459 switch( p_counter->i_type )
461 case VLC_VAR_INTEGER:
463 p_counter->pp_samples[0]->value.i_int += val.i_int;
466 msg_Err( p_handler, "Trying to increment invalid variable %s",
467 p_counter->psz_name );
476 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
480 for( i = 0; i< p_handler->i_counters; i++ )
482 counter_t *p_counter = p_handler->pp_counters[i];
483 if( p_counter->i_source_object == i_object_id &&
484 !strcmp( p_counter->psz_name, psz_name ) )
493 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
495 stats_handler_t *p_handler = (stats_handler_t*)
496 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
500 p_handler = stats_HandlerCreate( p_this );
505 vlc_object_yield( p_handler );
511 * Initialize statistics handler
513 * This function initializes the global statistics handler singleton,
514 * \param p_this the parent VLC object
516 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
518 stats_handler_t *p_handler;
520 msg_Dbg( p_this, "creating statistics handler" );
522 p_handler = (stats_handler_t*) vlc_object_create( p_this,
527 msg_Err( p_this, "out of memory" );
530 p_handler->i_counters = 0;
531 p_handler->pp_counters = NULL;
533 /// \bug is it p_vlc or p_libvlc ?
534 vlc_object_attach( p_handler, p_this->p_vlc );