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 int CounterUpdate( vlc_object_t *p_this,
37 vlc_value_t val, vlc_value_t * );
38 static void TimerDump( vlc_object_t *p_this, counter_t *p_counter, vlc_bool_t);
40 /*****************************************************************************
42 *****************************************************************************/
45 * Create a statistics counter
46 * \param p_this a VLC object
47 * \param i_type the type of stored data. One of VLC_VAR_STRING,
48 * VLC_VAR_INTEGER, VLC_VAR_FLOAT
49 * \param i_compute_type the aggregation type. One of STATS_LAST (always
50 * keep the last value), STATS_COUNTER (increment by the passed value),
51 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
52 * (keep a time derivative of the value)
54 counter_t * __stats_CounterCreate( vlc_object_t *p_this,
55 int i_type, int i_compute_type )
58 if( !p_this->p_libvlc->b_stats ) return NULL;
60 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
62 p_counter->i_compute_type = i_compute_type;
63 p_counter->i_type = i_type;
64 p_counter->i_samples = 0;
65 p_counter->pp_samples = NULL;
66 p_counter->psz_name = NULL;
68 p_counter->update_interval = 0;
69 p_counter->last_update = 0;
74 /** Update a counter element with new values
75 * \param p_this a VLC object
76 * \param p_counter the counter to update
77 * \param val the vlc_value union containing the new value to aggregate. For
78 * more information on how data is aggregated, \see __stats_Create
79 * \param val_new a pointer that will be filled with new data
81 int __stats_Update( vlc_object_t *p_this, counter_t *p_counter,
82 vlc_value_t val, vlc_value_t *val_new )
84 if( !p_this->p_libvlc->b_stats ) return VLC_EGENERIC;
85 return CounterUpdate( p_this, p_counter, val, val_new );
88 /** Get the aggregated value for a counter
89 * \param p_this an object
90 * \param p_counter the counter
91 * \param val a pointer to an initialized vlc_value union. It will contain the
93 * \return an error code
95 int __stats_Get( vlc_object_t *p_this, counter_t *p_counter, vlc_value_t *val )
97 if( !p_this->p_libvlc->b_stats || p_counter->i_samples == 0 )
99 val->i_int = val->f_float = 0.0;
103 switch( p_counter->i_compute_type )
109 *val = p_counter->pp_samples[0]->value;
111 case STATS_DERIVATIVE:
113 if( p_counter->i_samples < 2 )
115 val->i_int = 0; val->f_float = 0.0;
118 if( p_counter->i_type == VLC_VAR_INTEGER )
120 float f = ( p_counter->pp_samples[0]->value.i_int -
121 p_counter->pp_samples[1]->value.i_int ) /
122 (float)( p_counter->pp_samples[0]->date -
123 p_counter->pp_samples[1]->date );
128 float f = (float)( p_counter->pp_samples[0]->value.f_float -
129 p_counter->pp_samples[1]->value.f_float ) /
130 (float)( p_counter->pp_samples[0]->date -
131 p_counter->pp_samples[1]->date );
139 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
141 if( !p_input->p_libvlc->b_stats ) return;
143 vlc_mutex_lock( &p_stats->lock );
146 stats_GetInteger( p_input, p_input->counters.p_read_packets,
147 &p_stats->i_read_packets );
148 stats_GetInteger( p_input, p_input->counters.p_read_bytes,
149 &p_stats->i_read_bytes );
150 stats_GetFloat( p_input, p_input->counters.p_input_bitrate,
151 &p_stats->f_input_bitrate );
152 stats_GetInteger( p_input, p_input->counters.p_demux_read,
153 &p_stats->i_demux_read_bytes );
154 stats_GetFloat( p_input, p_input->counters.p_demux_bitrate,
155 &p_stats->f_demux_bitrate );
158 stats_GetInteger( p_input, p_input->counters.p_decoded_video,
159 &p_stats->i_decoded_video );
160 stats_GetInteger( p_input, p_input->counters.p_decoded_audio,
161 &p_stats->i_decoded_audio );
164 if( p_input->counters.p_sout_send_bitrate )
166 stats_GetInteger( p_input, p_input->counters.p_sout_sent_packets,
167 &p_stats->i_sent_packets );
168 stats_GetInteger( p_input, p_input->counters.p_sout_sent_bytes,
169 &p_stats->i_sent_bytes );
170 stats_GetFloat ( p_input, p_input->counters.p_sout_send_bitrate,
171 &p_stats->f_send_bitrate );
175 stats_GetInteger( p_input, p_input->counters.p_played_abuffers,
176 &p_stats->i_played_abuffers );
177 stats_GetInteger( p_input, p_input->counters.p_lost_abuffers,
178 &p_stats->i_lost_abuffers );
181 stats_GetInteger( p_input, p_input->counters.p_displayed_pictures,
182 &p_stats->i_displayed_pictures );
183 stats_GetInteger( p_input, p_input->counters.p_lost_pictures,
184 &p_stats->i_lost_pictures );
186 vlc_mutex_unlock( &p_stats->lock );
189 void stats_ReinitInputStats( input_stats_t *p_stats )
191 p_stats->i_read_packets = p_stats->i_read_bytes =
192 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
193 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
194 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
195 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
196 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
197 p_stats->i_decoded_video = p_stats->i_decoded_audio =
198 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
202 void stats_DumpInputStats( input_stats_t *p_stats )
204 vlc_mutex_lock( &p_stats->lock );
205 /* f_bitrate is in bytes / microsecond
206 * *1000 => bytes / millisecond => kbytes / seconds */
207 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
208 "Demux : %i (%i bytes) - %f kB/s\n"
209 " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
210 p_stats->i_read_packets, p_stats->i_read_bytes,
211 p_stats->f_input_bitrate * 1000,
212 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
213 p_stats->f_demux_bitrate * 1000,
214 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
215 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
216 p_stats->f_send_bitrate );
217 vlc_mutex_unlock( &p_stats->lock );
220 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
225 if( !p_obj->p_libvlc->b_stats ) return;
227 vlc_mutex_lock( &p_stats->lock );
229 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
232 float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
233 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
235 float f_in = 0, f_out = 0, f_demux = 0;
236 input_thread_t *p_input = (input_thread_t *)
237 p_list->p_values[i_index].p_object;
238 stats_GetFloat( p_obj, p_input->counters.p_input_bitrate, &f_in );
239 if( p_input->counters.p_sout_send_bitrate )
240 stats_GetFloat( p_obj, p_input->counters.p_sout_send_bitrate,
242 stats_GetFloat( p_obj, p_input->counters.p_demux_bitrate,
244 f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
246 p_stats->f_input_bitrate = f_total_in;
247 p_stats->f_output_bitrate = f_total_out;
248 p_stats->f_demux_bitrate = f_total_demux;
249 vlc_list_release( p_list );
252 vlc_mutex_unlock( &p_stats->lock );
255 void stats_ReinitGlobalStats( global_stats_t *p_stats )
257 p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
261 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
265 counter_t *p_counter = NULL;
266 if( !p_obj->p_libvlc->b_stats ) return;
267 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
269 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
271 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
273 p_counter = p_obj->p_libvlc->pp_timers[i];
279 counter_sample_t *p_sample;
280 p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
282 if( !p_counter ) return;
283 p_counter->psz_name = strdup( psz_name );
284 p_counter->i_id = i_id;
285 INSERT_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers,
286 p_obj->p_libvlc->i_timers, p_counter );
288 /* 1st sample : if started: start_date, else last_time, b_started */
289 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
290 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
291 p_counter->i_samples, p_sample );
292 p_sample->date = 0; p_sample->value.b_bool = 0;
293 /* 2nd sample : global_time, i_samples */
294 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
295 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
296 p_counter->i_samples, p_sample );
297 p_sample->date = 0; p_sample->value.i_int = 0;
299 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
301 msg_Warn( p_obj, "timer %s was already started !", psz_name );
304 p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
305 p_counter->pp_samples[0]->date = mdate();
306 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
309 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
311 counter_t *p_counter = NULL;
313 if( !p_obj->p_libvlc->b_stats ) return;
314 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
315 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
317 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
319 p_counter = p_obj->p_libvlc->pp_timers[i];
323 if( !p_counter || p_counter->i_samples != 2 )
325 msg_Err( p_obj, "timer does not exist" );
326 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
329 p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
330 p_counter->pp_samples[1]->value.i_int += 1;
331 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
332 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
333 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
336 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
338 counter_t *p_counter = NULL;
340 if( !p_obj->p_libvlc->b_stats ) return;
341 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
342 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
344 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
346 p_counter = p_obj->p_libvlc->pp_timers[i];
350 TimerDump( p_obj, p_counter, VLC_TRUE );
351 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
355 void __stats_TimersDumpAll( vlc_object_t *p_obj )
358 if( !p_obj->p_libvlc->b_stats ) return;
359 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
360 for ( i = 0 ; i< p_obj->p_libvlc->i_timers ; i++ )
361 TimerDump( p_obj, p_obj->p_libvlc->pp_timers[i], VLC_FALSE );
362 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
365 void __stats_TimersClean( vlc_object_t *p_obj )
368 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
369 for ( i = p_obj->p_libvlc->i_timers -1 ; i >= 0; i-- )
371 counter_t *p_counter = p_obj->p_libvlc->pp_timers[i];
372 REMOVE_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers, i );
373 stats_CounterClean( p_counter );
375 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
378 void stats_CounterClean( counter_t *p_c )
383 i = p_c->i_samples - 1 ;
386 counter_sample_t *p_s = p_c->pp_samples[i];
387 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
391 if( p_c->psz_name ) free( p_c->psz_name );
397 /********************************************************************
398 * Following functions are local
399 ********************************************************************/
402 * Update a statistics counter, according to its type
403 * If needed, perform a bit of computation (derivative, mostly)
404 * This function must be entered with stats handler lock
405 * \param p_counter the counter to update
406 * \param val the "new" value
407 * \return an error code
409 static int CounterUpdate( vlc_object_t *p_handler,
410 counter_t *p_counter,
411 vlc_value_t val, vlc_value_t *new_val )
413 switch( p_counter->i_compute_type )
418 if( p_counter->i_samples > 1)
420 msg_Err( p_handler, "LAST counter has several samples !" );
423 if( p_counter->i_type != VLC_VAR_FLOAT &&
424 p_counter->i_type != VLC_VAR_INTEGER &&
425 p_counter->i_compute_type != STATS_LAST )
427 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
431 if( p_counter->i_samples == 0 )
433 counter_sample_t *p_new = (counter_sample_t*)malloc(
434 sizeof( counter_sample_t ) );
435 p_new->value.psz_string = NULL;
437 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
438 p_counter->i_samples, p_new );
440 if( p_counter->i_samples == 1 )
442 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
443 if( p_counter->i_compute_type == STATS_LAST ||
444 ( p_counter->i_compute_type == STATS_MAX &&
445 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
446 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
447 ( p_counter->i_type == VLC_VAR_FLOAT &&
448 p_counter->pp_samples[0]->value.f_float > val.f_float )
450 ( p_counter->i_compute_type == STATS_MIN &&
451 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
452 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
453 ( p_counter->i_type == VLC_VAR_FLOAT &&
454 p_counter->pp_samples[0]->value.f_float < val.f_float )
457 if( p_counter->i_type == VLC_VAR_STRING &&
458 p_counter->pp_samples[0]->value.psz_string )
460 free( p_counter->pp_samples[0]->value.psz_string );
462 p_counter->pp_samples[0]->value = val;
463 *new_val = p_counter->pp_samples[0]->value;
467 case STATS_DERIVATIVE:
469 counter_sample_t *p_new, *p_old;
470 if( mdate() - p_counter->last_update < p_counter->update_interval )
474 p_counter->last_update = mdate();
475 if( p_counter->i_type != VLC_VAR_FLOAT &&
476 p_counter->i_type != VLC_VAR_INTEGER )
478 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
481 /* Insert the new one at the beginning */
482 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
484 p_new->date = p_counter->last_update;
485 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
488 if( p_counter->i_samples == 3 )
490 p_old = p_counter->pp_samples[2];
491 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
497 if( p_counter->i_samples > 1)
499 msg_Err( p_handler, "LAST counter has several samples !" );
502 if( p_counter->i_samples == 0 )
504 counter_sample_t *p_new = (counter_sample_t*)malloc(
505 sizeof( counter_sample_t ) );
506 p_new->value.psz_string = NULL;
508 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
509 p_counter->i_samples, p_new );
511 if( p_counter->i_samples == 1 )
513 switch( p_counter->i_type )
515 case VLC_VAR_INTEGER:
516 p_counter->pp_samples[0]->value.i_int += val.i_int;
518 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
521 p_counter->pp_samples[0]->value.f_float += val.f_float;
523 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
525 msg_Err( p_handler, "Trying to increment invalid variable %s",
526 p_counter->psz_name );
535 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
540 if( !p_counter || p_counter->i_samples != 2 )
542 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
545 i_total = p_counter->pp_samples[1]->value.i_int;
546 total = p_counter->pp_samples[1]->date;
547 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
549 last = mdate() - p_counter->pp_samples[0]->date;
555 last = p_counter->pp_samples[0]->date;
560 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
561 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
562 (float)(total)/(1000*(float)i_total ) );
567 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
568 p_counter->psz_name, (float)total/1000, i_total,
569 (float)(total)/(1000*(float)i_total ) );