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 */
31 #include "input/input_internal.h"
33 /*****************************************************************************
35 *****************************************************************************/
36 static int CounterUpdate( vlc_object_t *p_this,
38 vlc_value_t val, vlc_value_t * );
39 static void TimerDump( vlc_object_t *p_this, counter_t *p_counter, vlc_bool_t);
41 /*****************************************************************************
43 *****************************************************************************/
46 * Create a statistics counter
47 * \param p_this a VLC object
48 * \param i_type the type of stored data. One of VLC_VAR_STRING,
49 * VLC_VAR_INTEGER, VLC_VAR_FLOAT
50 * \param i_compute_type the aggregation type. One of STATS_LAST (always
51 * keep the last value), STATS_COUNTER (increment by the passed value),
52 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
53 * (keep a time derivative of the value)
55 counter_t * __stats_CounterCreate( vlc_object_t *p_this,
56 int i_type, int i_compute_type )
58 counter_t *p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
60 p_counter->i_compute_type = i_compute_type;
61 p_counter->i_type = i_type;
62 p_counter->i_samples = 0;
63 p_counter->pp_samples = NULL;
64 p_counter->psz_name = NULL;
66 p_counter->update_interval = 0;
67 p_counter->last_update = 0;
72 /** Update a counter element with new values
73 * \param p_this a VLC object
74 * \param p_counter the counter to update
75 * \param val the vlc_value union containing the new value to aggregate. For
76 * more information on how data is aggregated, \see __stats_Create
77 * \param val_new a pointer that will be filled with new data
79 int __stats_Update( vlc_object_t *p_this, counter_t *p_counter,
80 vlc_value_t val, vlc_value_t *val_new )
82 if( !p_this->p_libvlc->b_stats || !p_counter ) return VLC_EGENERIC;
83 return CounterUpdate( p_this, p_counter, val, val_new );
86 /** Get the aggregated value for a counter
87 * \param p_this an object
88 * \param p_counter the counter
89 * \param val a pointer to an initialized vlc_value union. It will contain the
91 * \return an error code
93 int __stats_Get( vlc_object_t *p_this, counter_t *p_counter, vlc_value_t *val )
95 if( !p_this->p_libvlc->b_stats || !p_counter || p_counter->i_samples == 0 )
97 val->i_int = val->f_float = 0.0;
101 switch( p_counter->i_compute_type )
107 *val = p_counter->pp_samples[0]->value;
109 case STATS_DERIVATIVE:
111 if( p_counter->i_samples < 2 )
113 val->i_int = 0; val->f_float = 0.0;
116 if( p_counter->i_type == VLC_VAR_INTEGER )
118 float f = ( p_counter->pp_samples[0]->value.i_int -
119 p_counter->pp_samples[1]->value.i_int ) /
120 (float)( p_counter->pp_samples[0]->date -
121 p_counter->pp_samples[1]->date );
126 float f = (float)( p_counter->pp_samples[0]->value.f_float -
127 p_counter->pp_samples[1]->value.f_float ) /
128 (float)( p_counter->pp_samples[0]->date -
129 p_counter->pp_samples[1]->date );
137 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
139 if( !p_input->p_libvlc->b_stats ) return;
141 vlc_mutex_lock( &p_input->p->counters.counters_lock );
142 vlc_mutex_lock( &p_stats->lock );
145 stats_GetInteger( p_input, p_input->p->counters.p_read_packets,
146 &p_stats->i_read_packets );
147 stats_GetInteger( p_input, p_input->p->counters.p_read_bytes,
148 &p_stats->i_read_bytes );
149 stats_GetFloat( p_input, p_input->p->counters.p_input_bitrate,
150 &p_stats->f_input_bitrate );
151 stats_GetInteger( p_input, p_input->p->counters.p_demux_read,
152 &p_stats->i_demux_read_bytes );
153 stats_GetFloat( p_input, p_input->p->counters.p_demux_bitrate,
154 &p_stats->f_demux_bitrate );
157 stats_GetInteger( p_input, p_input->p->counters.p_decoded_video,
158 &p_stats->i_decoded_video );
159 stats_GetInteger( p_input, p_input->p->counters.p_decoded_audio,
160 &p_stats->i_decoded_audio );
163 if( p_input->p->counters.p_sout_send_bitrate )
165 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_packets,
166 &p_stats->i_sent_packets );
167 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
168 &p_stats->i_sent_bytes );
169 stats_GetFloat ( p_input, p_input->p->counters.p_sout_send_bitrate,
170 &p_stats->f_send_bitrate );
174 stats_GetInteger( p_input, p_input->p->counters.p_played_abuffers,
175 &p_stats->i_played_abuffers );
176 stats_GetInteger( p_input, p_input->p->counters.p_lost_abuffers,
177 &p_stats->i_lost_abuffers );
180 stats_GetInteger( p_input, p_input->p->counters.p_displayed_pictures,
181 &p_stats->i_displayed_pictures );
182 stats_GetInteger( p_input, p_input->p->counters.p_lost_pictures,
183 &p_stats->i_lost_pictures );
185 vlc_mutex_unlock( &p_stats->lock );
186 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
189 void stats_ReinitInputStats( input_stats_t *p_stats )
191 vlc_mutex_lock( &p_stats->lock );
192 p_stats->i_read_packets = p_stats->i_read_bytes =
193 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
194 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
195 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
196 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
197 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
198 p_stats->i_decoded_video = p_stats->i_decoded_audio =
199 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
201 vlc_mutex_unlock( &p_stats->lock );
204 void stats_DumpInputStats( input_stats_t *p_stats )
206 vlc_mutex_lock( &p_stats->lock );
207 /* f_bitrate is in bytes / microsecond
208 * *1000 => bytes / millisecond => kbytes / seconds */
209 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
210 "Demux : %i (%i bytes) - %f kB/s\n"
211 " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
212 p_stats->i_read_packets, p_stats->i_read_bytes,
213 p_stats->f_input_bitrate * 1000,
214 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
215 p_stats->f_demux_bitrate * 1000,
216 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
217 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
218 p_stats->f_send_bitrate );
219 vlc_mutex_unlock( &p_stats->lock );
222 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
227 if( !p_obj->p_libvlc->b_stats ) return;
229 vlc_mutex_lock( &p_stats->lock );
231 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
234 float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
235 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
237 float f_in = 0, f_out = 0, f_demux = 0;
238 input_thread_t *p_input = (input_thread_t *)
239 p_list->p_values[i_index].p_object;
240 vlc_mutex_lock( &p_input->p->counters.counters_lock );
241 stats_GetFloat( p_obj, p_input->p->counters.p_input_bitrate, &f_in );
242 if( p_input->p->counters.p_sout_send_bitrate )
243 stats_GetFloat( p_obj, p_input->p->counters.p_sout_send_bitrate,
245 stats_GetFloat( p_obj, p_input->p->counters.p_demux_bitrate,
247 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
248 f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
250 p_stats->f_input_bitrate = f_total_in;
251 p_stats->f_output_bitrate = f_total_out;
252 p_stats->f_demux_bitrate = f_total_demux;
253 vlc_list_release( p_list );
256 vlc_mutex_unlock( &p_stats->lock );
259 void stats_ReinitGlobalStats( global_stats_t *p_stats )
261 vlc_mutex_lock( &p_stats->lock );
262 p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
263 vlc_mutex_unlock( &p_stats->lock );
267 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
271 counter_t *p_counter = NULL;
272 if( !p_obj->p_libvlc->b_stats ) return;
273 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
275 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
277 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
279 p_counter = p_obj->p_libvlc->pp_timers[i];
285 counter_sample_t *p_sample;
286 p_counter = stats_CounterCreate( p_obj->p_libvlc_global, VLC_VAR_TIME,
290 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
293 p_counter->psz_name = strdup( psz_name );
294 p_counter->i_id = i_id;
295 INSERT_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers,
296 p_obj->p_libvlc->i_timers, p_counter );
298 /* 1st sample : if started: start_date, else last_time, b_started */
299 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
300 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
301 p_counter->i_samples, p_sample );
302 p_sample->date = 0; p_sample->value.b_bool = 0;
303 /* 2nd sample : global_time, i_samples */
304 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
305 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
306 p_counter->i_samples, p_sample );
307 p_sample->date = 0; p_sample->value.i_int = 0;
309 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
311 msg_Warn( p_obj, "timer %s was already started !", psz_name );
312 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
315 p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
316 p_counter->pp_samples[0]->date = mdate();
317 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
320 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
322 counter_t *p_counter = NULL;
324 if( !p_obj->p_libvlc->b_stats ) return;
325 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
326 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
328 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
330 p_counter = p_obj->p_libvlc->pp_timers[i];
334 if( !p_counter || p_counter->i_samples != 2 )
336 msg_Err( p_obj, "timer does not exist" );
337 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
340 p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
341 p_counter->pp_samples[1]->value.i_int += 1;
342 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
343 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
344 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
347 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
349 counter_t *p_counter = NULL;
351 if( !p_obj->p_libvlc->b_stats ) return;
352 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
353 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
355 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
357 p_counter = p_obj->p_libvlc->pp_timers[i];
361 TimerDump( p_obj, p_counter, VLC_TRUE );
362 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
365 void __stats_TimersDumpAll( vlc_object_t *p_obj )
368 if( !p_obj->p_libvlc->b_stats ) return;
369 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
370 for ( i = 0 ; i< p_obj->p_libvlc->i_timers ; i++ )
371 TimerDump( p_obj, p_obj->p_libvlc->pp_timers[i], VLC_FALSE );
372 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
375 void __stats_TimersClean( vlc_object_t *p_obj )
378 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
379 for ( i = p_obj->p_libvlc->i_timers -1 ; i >= 0; i-- )
381 counter_t *p_counter = p_obj->p_libvlc->pp_timers[i];
382 REMOVE_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers, i );
383 stats_CounterClean( p_counter );
385 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
388 void stats_CounterClean( counter_t *p_c )
393 i = p_c->i_samples - 1 ;
396 counter_sample_t *p_s = p_c->pp_samples[i];
397 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
401 if( p_c->psz_name ) free( p_c->psz_name );
407 /********************************************************************
408 * Following functions are local
409 ********************************************************************/
412 * Update a statistics counter, according to its type
413 * If needed, perform a bit of computation (derivative, mostly)
414 * This function must be entered with stats handler lock
415 * \param p_counter the counter to update
416 * \param val the "new" value
417 * \return an error code
419 static int CounterUpdate( vlc_object_t *p_handler,
420 counter_t *p_counter,
421 vlc_value_t val, vlc_value_t *new_val )
423 switch( p_counter->i_compute_type )
428 if( p_counter->i_samples > 1)
430 msg_Err( p_handler, "LAST counter has several samples !" );
433 if( p_counter->i_type != VLC_VAR_FLOAT &&
434 p_counter->i_type != VLC_VAR_INTEGER &&
435 p_counter->i_compute_type != STATS_LAST )
437 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
441 if( p_counter->i_samples == 0 )
443 counter_sample_t *p_new = (counter_sample_t*)malloc(
444 sizeof( counter_sample_t ) );
445 p_new->value.psz_string = NULL;
447 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
448 p_counter->i_samples, p_new );
450 if( p_counter->i_samples == 1 )
452 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
453 if( p_counter->i_compute_type == STATS_LAST ||
454 ( p_counter->i_compute_type == STATS_MAX &&
455 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
456 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
457 ( p_counter->i_type == VLC_VAR_FLOAT &&
458 p_counter->pp_samples[0]->value.f_float > val.f_float )
460 ( p_counter->i_compute_type == STATS_MIN &&
461 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
462 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
463 ( p_counter->i_type == VLC_VAR_FLOAT &&
464 p_counter->pp_samples[0]->value.f_float < val.f_float )
467 if( p_counter->i_type == VLC_VAR_STRING &&
468 p_counter->pp_samples[0]->value.psz_string )
470 free( p_counter->pp_samples[0]->value.psz_string );
472 p_counter->pp_samples[0]->value = val;
473 *new_val = p_counter->pp_samples[0]->value;
477 case STATS_DERIVATIVE:
479 counter_sample_t *p_new, *p_old;
480 if( mdate() - p_counter->last_update < p_counter->update_interval )
484 p_counter->last_update = mdate();
485 if( p_counter->i_type != VLC_VAR_FLOAT &&
486 p_counter->i_type != VLC_VAR_INTEGER )
488 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
491 /* Insert the new one at the beginning */
492 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
494 p_new->date = p_counter->last_update;
495 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
498 if( p_counter->i_samples == 3 )
500 p_old = p_counter->pp_samples[2];
501 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
507 if( p_counter->i_samples > 1)
509 msg_Err( p_handler, "LAST counter has several samples !" );
512 if( p_counter->i_samples == 0 )
514 counter_sample_t *p_new = (counter_sample_t*)malloc(
515 sizeof( counter_sample_t ) );
516 p_new->value.psz_string = NULL;
518 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
519 p_counter->i_samples, p_new );
521 if( p_counter->i_samples == 1 )
523 switch( p_counter->i_type )
525 case VLC_VAR_INTEGER:
526 p_counter->pp_samples[0]->value.i_int += val.i_int;
528 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
531 p_counter->pp_samples[0]->value.f_float += val.f_float;
533 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
535 msg_Err( p_handler, "Trying to increment invalid variable %s",
536 p_counter->psz_name );
545 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
550 if( !p_counter || p_counter->i_samples != 2 )
552 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
555 i_total = p_counter->pp_samples[1]->value.i_int;
556 total = p_counter->pp_samples[1]->date;
557 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
559 last = mdate() - p_counter->pp_samples[0]->date;
565 last = p_counter->pp_samples[0]->date;
570 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
571 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
572 (float)(total)/(1000*(float)i_total ) );
577 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
578 p_counter->psz_name, (float)total/1000, i_total,
579 (float)(total)/(1000*(float)i_total ) );