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 * Create a statistics counter
49 * \param p_this the object for which to create the counter
50 * \param psz_name the name
51 * \param i_type the type of stored data. One of VLC_VAR_STRING,
52 * VLC_VAR_INTEGER, VLC_VAR_FLOAT
53 * \param i_compute_type the aggregation type. One of STATS_LAST (always
54 * keep the last value), STATS_COUNTER (increment by the passed value),
55 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
56 * (keep a time derivative of the value)
58 int __stats_Create( vlc_object_t *p_this, char *psz_name, int i_type,
62 stats_handler_t *p_handler;
64 if( p_this->p_libvlc->b_stats == VLC_FALSE )
68 p_handler = stats_HandlerGet( p_this );
69 if( !p_handler ) return VLC_ENOMEM;
71 vlc_mutex_lock( &p_handler->object_lock );
73 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
75 p_counter->psz_name = strdup( psz_name );
76 p_counter->i_source_object = p_this->i_object_id;
77 p_counter->i_compute_type = i_compute_type;
78 p_counter->i_type = i_type;
79 p_counter->i_samples = 0;
80 p_counter->pp_samples = NULL;
82 p_counter->update_interval = 0;
83 p_counter->last_update = 0;
85 INSERT_ELEM( p_handler->pp_counters,
86 p_handler->i_counters,
87 p_handler->i_counters,
90 vlc_mutex_unlock( &p_handler->object_lock );
95 /** Update a counter element with new values
96 * \param p_this the object in which to update
97 * \param psz_name the name
98 * \param val the vlc_value union containing the new value to aggregate. For
99 * more information on how data is aggregated, \see __stats_Create
101 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
104 counter_t *p_counter;
106 /* Get stats handler singleton */
107 stats_handler_t *p_handler;
108 if( p_this->p_libvlc->b_stats == VLC_FALSE )
112 p_handler = stats_HandlerGet( p_this );
113 if( !p_handler ) return VLC_ENOMEM;
115 vlc_mutex_lock( &p_handler->object_lock );
117 /* Look for existing element */
118 p_counter = GetCounter( p_handler, p_this->i_object_id,
122 vlc_mutex_unlock( &p_handler->object_lock );
123 vlc_object_release( p_handler );
127 i_ret = stats_CounterUpdate( p_handler, p_counter, val );
128 vlc_mutex_unlock( &p_handler->object_lock );
133 /** Get the aggregated value for a counter
134 * \param p_this an object
135 * \param i_object_id the object id from which we want the data
136 * \param psz_name the name of the couner
137 * \param val a pointer to an initialized vlc_value union. It will contain the
139 * \return an error code
141 int __stats_Get( vlc_object_t *p_this, int i_object_id, char *psz_name, vlc_value_t *val )
143 counter_t *p_counter;
145 /* Get stats handler singleton */
146 stats_handler_t *p_handler;
147 if( p_this->p_libvlc->b_stats == VLC_FALSE )
151 p_handler = stats_HandlerGet( p_this );
152 if( !p_handler ) return VLC_ENOMEM;
153 vlc_mutex_lock( &p_handler->object_lock );
156 /* Look for existing element */
157 p_counter = GetCounter( p_handler, i_object_id,
161 vlc_mutex_unlock( &p_handler->object_lock );
162 vlc_object_release( p_handler );
166 if( p_counter->i_samples == 0 )
168 vlc_mutex_unlock( &p_handler->object_lock );
169 val->i_int = val->f_float = 0.0;
173 switch( p_counter->i_compute_type )
179 *val = p_counter->pp_samples[0]->value;
181 case STATS_DERIVATIVE:
183 if( p_counter->i_samples < 2 )
185 vlc_mutex_unlock( &p_handler->object_lock );
186 val->i_int = 0; val->f_float = 0.0;
189 if( p_counter->i_type == VLC_VAR_INTEGER )
191 float f = ( p_counter->pp_samples[0]->value.i_int -
192 p_counter->pp_samples[1]->value.i_int ) /
193 (float)( p_counter->pp_samples[0]->date -
194 p_counter->pp_samples[1]->date );
199 float f = (float)( p_counter->pp_samples[0]->value.f_float -
200 p_counter->pp_samples[1]->value.f_float ) /
201 (float)( p_counter->pp_samples[0]->date -
202 p_counter->pp_samples[1]->date );
208 vlc_object_release( p_handler );
210 vlc_mutex_unlock( &p_handler->object_lock );
214 /** Get a statistics counter structure. This allows for low-level modifications
215 * \param p_this a parent object
216 * \param i_object_id the object from which to retrieve data
217 * \param psz_name the name
218 * \return the counter, or NULL if not found (or handler not created yet)
220 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
223 counter_t *p_counter;
225 stats_handler_t *p_handler;
226 if( p_this->p_libvlc->b_stats == VLC_FALSE )
230 p_handler = stats_HandlerGet( p_this );
231 if( !p_handler ) return NULL;
233 vlc_mutex_lock( &p_handler->object_lock );
235 /* Look for existing element */
236 p_counter = GetCounter( p_handler, p_this->i_object_id,
238 vlc_mutex_unlock( &p_handler->object_lock );
239 vlc_object_release( p_handler );
245 void stats_ComputeInputStats( input_thread_t *p_input,
246 input_stats_t *p_stats )
251 vlc_mutex_lock( &p_stats->lock );
254 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
255 &p_stats->i_read_packets );
256 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
257 &p_stats->i_read_bytes );
258 stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
259 &p_stats->f_input_bitrate );
261 stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
262 &p_stats->i_demux_read_bytes );
263 stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
264 &p_stats->f_demux_bitrate );
266 stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
267 &p_stats->i_decoded_video );
268 stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
269 &p_stats->i_decoded_audio );
272 p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
275 p_stats->i_displayed_pictures = 0 ;
276 p_stats->i_lost_pictures = 0;
277 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
279 int i_displayed = 0, i_lost = 0;
280 p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
281 stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
283 stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
285 p_stats->i_displayed_pictures += i_displayed;
286 p_stats->i_lost_pictures += i_lost;
288 vlc_list_release( p_list );
290 vlc_mutex_unlock( &p_stats->lock );
293 void stats_ReinitInputStats( input_stats_t *p_stats )
295 p_stats->i_read_packets = p_stats->i_read_bytes =
296 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
297 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
298 p_stats->i_decoded_video = p_stats->i_decoded_audio = 0;
301 void stats_DumpInputStats( input_stats_t *p_stats )
303 vlc_mutex_lock( &p_stats->lock );
304 /* f_bitrate is in bytes / microsecond
305 * *1000 => bytes / millisecond => kbytes / seconds */
306 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Vout : %i/%i\n",
307 p_stats->i_read_packets, p_stats->i_read_bytes,
308 p_stats->f_input_bitrate * 1000,
309 p_stats->i_displayed_pictures, p_stats->i_lost_pictures );
310 vlc_mutex_unlock( &p_stats->lock );
314 /********************************************************************
315 * Following functions are local
316 ********************************************************************/
319 * Update a statistics counter, according to its type
320 * If needed, perform a bit of computation (derivative, mostly)
321 * This function must be entered with stats handler lock
322 * \param p_handler stats handler singleton
323 * \param p_counter the counter to update
324 * \param val the "new" value
325 * \return an error code
327 static int stats_CounterUpdate( stats_handler_t *p_handler,
328 counter_t *p_counter,
331 switch( p_counter->i_compute_type )
336 if( p_counter->i_samples > 1)
338 msg_Err( p_handler, "LAST counter has several samples !" );
341 if( p_counter->i_type != VLC_VAR_FLOAT &&
342 p_counter->i_type != VLC_VAR_INTEGER &&
343 p_counter->i_compute_type != STATS_LAST )
345 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
349 if( p_counter->i_samples == 0 )
351 counter_sample_t *p_new = (counter_sample_t*)malloc(
352 sizeof( counter_sample_t ) );
353 p_new->value.psz_string = NULL;
355 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
356 p_counter->i_samples, p_new );
358 if( p_counter->i_samples == 1 )
360 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
361 if( p_counter->i_compute_type == STATS_LAST ||
362 ( p_counter->i_compute_type == STATS_MAX &&
363 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
364 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
365 ( p_counter->i_type == VLC_VAR_FLOAT &&
366 p_counter->pp_samples[0]->value.f_float > val.f_float )
368 ( p_counter->i_compute_type == STATS_MIN &&
369 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
370 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
371 ( p_counter->i_type == VLC_VAR_FLOAT &&
372 p_counter->pp_samples[0]->value.f_float < val.f_float )
375 if( p_counter->i_type == VLC_VAR_STRING &&
376 p_counter->pp_samples[0]->value.psz_string )
378 free( p_counter->pp_samples[0]->value.psz_string );
380 p_counter->pp_samples[0]->value = val;
384 case STATS_DERIVATIVE:
386 counter_sample_t *p_new, *p_old;
387 if( mdate() - p_counter->last_update < p_counter->update_interval )
391 p_counter->last_update = mdate();
392 if( p_counter->i_type != VLC_VAR_FLOAT &&
393 p_counter->i_type != VLC_VAR_INTEGER )
395 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
398 /* Insert the new one at the beginning */
399 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
401 p_new->date = p_counter->last_update;
402 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
405 if( p_counter->i_samples == 3 )
407 p_old = p_counter->pp_samples[2];
408 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
414 if( p_counter->i_samples > 1)
416 msg_Err( p_handler, "LAST counter has several samples !" );
419 if( p_counter->i_samples == 0 )
421 counter_sample_t *p_new = (counter_sample_t*)malloc(
422 sizeof( counter_sample_t ) );
423 p_new->value.psz_string = NULL;
425 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
426 p_counter->i_samples, p_new );
428 if( p_counter->i_samples == 1 )
430 switch( p_counter->i_type )
432 case VLC_VAR_INTEGER:
434 p_counter->pp_samples[0]->value.i_int += val.i_int;
437 msg_Err( p_handler, "Trying to increment invalid variable %s",
438 p_counter->psz_name );
447 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
451 for( i = 0; i< p_handler->i_counters; i++ )
453 counter_t *p_counter = p_handler->pp_counters[i];
454 if( p_counter->i_source_object == i_object_id &&
455 !strcmp( p_counter->psz_name, psz_name ) )
464 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
466 stats_handler_t *p_handler = (stats_handler_t*)
467 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
471 p_handler = stats_HandlerCreate( p_this );
476 vlc_object_yield( p_handler );
482 * Initialize statistics handler
484 * This function initializes the global statistics handler singleton,
485 * \param p_this the parent VLC object
487 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
489 stats_handler_t *p_handler;
491 msg_Dbg( p_this, "creating statistics handler" );
493 p_handler = (stats_handler_t*) vlc_object_create( p_this,
498 msg_Err( p_this, "out of memory" );
501 p_handler->i_counters = 0;
502 p_handler->pp_counters = NULL;
504 /// \bug is it p_vlc or p_libvlc ?
505 vlc_object_attach( p_handler, p_this->p_vlc );