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 *****************************************************************************/
29 #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 ) ) ;
61 p_counter->i_compute_type = i_compute_type;
62 p_counter->i_type = i_type;
63 p_counter->i_samples = 0;
64 p_counter->pp_samples = NULL;
65 p_counter->psz_name = NULL;
67 p_counter->update_interval = 0;
68 p_counter->last_update = 0;
73 /** Update a counter element with new values
74 * \param p_this a VLC object
75 * \param p_counter the counter to update
76 * \param val the vlc_value union containing the new value to aggregate. For
77 * more information on how data is aggregated, \see __stats_Create
78 * \param val_new a pointer that will be filled with new data
80 int __stats_Update( vlc_object_t *p_this, counter_t *p_counter,
81 vlc_value_t val, vlc_value_t *val_new )
83 if( !p_this->p_libvlc->b_stats || !p_counter ) return VLC_EGENERIC;
84 return CounterUpdate( p_this, p_counter, val, val_new );
87 /** Get the aggregated value for a counter
88 * \param p_this an object
89 * \param p_counter the counter
90 * \param val a pointer to an initialized vlc_value union. It will contain the
92 * \return an error code
94 int __stats_Get( vlc_object_t *p_this, counter_t *p_counter, vlc_value_t *val )
96 if( !p_this->p_libvlc->b_stats || !p_counter || p_counter->i_samples == 0 )
98 val->i_int = val->f_float = 0.0;
102 switch( p_counter->i_compute_type )
108 *val = p_counter->pp_samples[0]->value;
110 case STATS_DERIVATIVE:
112 if( p_counter->i_samples < 2 )
114 val->i_int = 0; val->f_float = 0.0;
117 if( p_counter->i_type == VLC_VAR_INTEGER )
119 float f = ( p_counter->pp_samples[0]->value.i_int -
120 p_counter->pp_samples[1]->value.i_int ) /
121 (float)( p_counter->pp_samples[0]->date -
122 p_counter->pp_samples[1]->date );
127 float f = (float)( p_counter->pp_samples[0]->value.f_float -
128 p_counter->pp_samples[1]->value.f_float ) /
129 (float)( p_counter->pp_samples[0]->date -
130 p_counter->pp_samples[1]->date );
138 input_stats_t *stats_NewInputStats( input_thread_t *p_input )
140 input_stats_t *p_stats = malloc( sizeof(input_stats_t) );
145 memset( p_stats, 0, sizeof(*p_stats) );
146 vlc_mutex_init( p_input, &p_stats->lock );
147 stats_ReinitInputStats( p_stats );
152 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
154 if( !p_input->p_libvlc->b_stats ) return;
156 vlc_mutex_lock( &p_input->p->counters.counters_lock );
157 vlc_mutex_lock( &p_stats->lock );
160 stats_GetInteger( p_input, p_input->p->counters.p_read_packets,
161 &p_stats->i_read_packets );
162 stats_GetInteger( p_input, p_input->p->counters.p_read_bytes,
163 &p_stats->i_read_bytes );
164 stats_GetFloat( p_input, p_input->p->counters.p_input_bitrate,
165 &p_stats->f_input_bitrate );
166 stats_GetInteger( p_input, p_input->p->counters.p_demux_read,
167 &p_stats->i_demux_read_bytes );
168 stats_GetFloat( p_input, p_input->p->counters.p_demux_bitrate,
169 &p_stats->f_demux_bitrate );
172 stats_GetInteger( p_input, p_input->p->counters.p_decoded_video,
173 &p_stats->i_decoded_video );
174 stats_GetInteger( p_input, p_input->p->counters.p_decoded_audio,
175 &p_stats->i_decoded_audio );
178 if( p_input->p->counters.p_sout_send_bitrate )
180 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_packets,
181 &p_stats->i_sent_packets );
182 stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
183 &p_stats->i_sent_bytes );
184 stats_GetFloat ( p_input, p_input->p->counters.p_sout_send_bitrate,
185 &p_stats->f_send_bitrate );
189 stats_GetInteger( p_input, p_input->p->counters.p_played_abuffers,
190 &p_stats->i_played_abuffers );
191 stats_GetInteger( p_input, p_input->p->counters.p_lost_abuffers,
192 &p_stats->i_lost_abuffers );
195 stats_GetInteger( p_input, p_input->p->counters.p_displayed_pictures,
196 &p_stats->i_displayed_pictures );
197 stats_GetInteger( p_input, p_input->p->counters.p_lost_pictures,
198 &p_stats->i_lost_pictures );
200 vlc_mutex_unlock( &p_stats->lock );
201 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
204 void stats_ReinitInputStats( input_stats_t *p_stats )
206 vlc_mutex_lock( &p_stats->lock );
207 p_stats->i_read_packets = p_stats->i_read_bytes =
208 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
209 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
210 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
211 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
212 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
213 p_stats->i_decoded_video = p_stats->i_decoded_audio =
214 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
216 vlc_mutex_unlock( &p_stats->lock );
219 void stats_DumpInputStats( input_stats_t *p_stats )
221 vlc_mutex_lock( &p_stats->lock );
222 /* f_bitrate is in bytes / microsecond
223 * *1000 => bytes / millisecond => kbytes / seconds */
224 fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
225 "Demux : %i (%i bytes) - %f kB/s\n"
226 " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
227 p_stats->i_read_packets, p_stats->i_read_bytes,
228 p_stats->f_input_bitrate * 1000,
229 p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
230 p_stats->f_demux_bitrate * 1000,
231 p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
232 p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
233 p_stats->f_send_bitrate );
234 vlc_mutex_unlock( &p_stats->lock );
237 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
242 if( !p_obj->p_libvlc->b_stats ) return;
244 vlc_mutex_lock( &p_stats->lock );
246 p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
249 float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
250 for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
252 float f_in = 0, f_out = 0, f_demux = 0;
253 input_thread_t *p_input = (input_thread_t *)
254 p_list->p_values[i_index].p_object;
255 vlc_mutex_lock( &p_input->p->counters.counters_lock );
256 stats_GetFloat( p_obj, p_input->p->counters.p_input_bitrate, &f_in );
257 if( p_input->p->counters.p_sout_send_bitrate )
258 stats_GetFloat( p_obj, p_input->p->counters.p_sout_send_bitrate,
260 stats_GetFloat( p_obj, p_input->p->counters.p_demux_bitrate,
262 vlc_mutex_unlock( &p_input->p->counters.counters_lock );
263 f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
265 p_stats->f_input_bitrate = f_total_in;
266 p_stats->f_output_bitrate = f_total_out;
267 p_stats->f_demux_bitrate = f_total_demux;
268 vlc_list_release( p_list );
271 vlc_mutex_unlock( &p_stats->lock );
274 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
278 counter_t *p_counter = NULL;
279 if( !p_obj->p_libvlc->b_stats ) return;
280 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
282 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
284 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
286 p_counter = p_obj->p_libvlc->pp_timers[i];
292 counter_sample_t *p_sample;
293 p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
297 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
300 p_counter->psz_name = strdup( psz_name );
301 p_counter->i_id = i_id;
302 INSERT_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers,
303 p_obj->p_libvlc->i_timers, p_counter );
305 /* 1st sample : if started: start_date, else last_time, b_started */
306 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
307 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
308 p_counter->i_samples, p_sample );
309 p_sample->date = 0; p_sample->value.b_bool = 0;
310 /* 2nd sample : global_time, i_samples */
311 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
312 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
313 p_counter->i_samples, p_sample );
314 p_sample->date = 0; p_sample->value.i_int = 0;
316 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
318 msg_Warn( p_obj, "timer %s was already started !", psz_name );
319 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
322 p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
323 p_counter->pp_samples[0]->date = mdate();
324 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
327 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
329 counter_t *p_counter = NULL;
331 if( !p_obj->p_libvlc->b_stats ) return;
332 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
333 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
335 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
337 p_counter = p_obj->p_libvlc->pp_timers[i];
341 if( !p_counter || p_counter->i_samples != 2 )
343 msg_Err( p_obj, "timer does not exist" );
344 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
347 p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
348 p_counter->pp_samples[1]->value.i_int += 1;
349 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
350 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
351 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
354 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
356 counter_t *p_counter = NULL;
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++ )
362 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
364 p_counter = p_obj->p_libvlc->pp_timers[i];
368 TimerDump( p_obj, p_counter, VLC_TRUE );
369 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
372 void __stats_TimersDumpAll( vlc_object_t *p_obj )
375 if( !p_obj->p_libvlc->b_stats ) return;
376 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
377 for ( i = 0 ; i< p_obj->p_libvlc->i_timers ; i++ )
378 TimerDump( p_obj, p_obj->p_libvlc->pp_timers[i], VLC_FALSE );
379 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
382 void __stats_TimersClean( vlc_object_t *p_obj )
385 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
386 for ( i = p_obj->p_libvlc->i_timers -1 ; i >= 0; i-- )
388 counter_t *p_counter = p_obj->p_libvlc->pp_timers[i];
389 REMOVE_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers, i );
390 stats_CounterClean( p_counter );
392 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
395 void stats_CounterClean( counter_t *p_c )
400 i = p_c->i_samples - 1 ;
403 counter_sample_t *p_s = p_c->pp_samples[i];
404 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
408 if( p_c->psz_name ) free( p_c->psz_name );
414 /********************************************************************
415 * Following functions are local
416 ********************************************************************/
419 * Update a statistics counter, according to its type
420 * If needed, perform a bit of computation (derivative, mostly)
421 * This function must be entered with stats handler lock
422 * \param p_counter the counter to update
423 * \param val the "new" value
424 * \return an error code
426 static int CounterUpdate( vlc_object_t *p_handler,
427 counter_t *p_counter,
428 vlc_value_t val, vlc_value_t *new_val )
430 switch( p_counter->i_compute_type )
435 if( p_counter->i_samples > 1)
437 msg_Err( p_handler, "LAST counter has several samples !" );
440 if( p_counter->i_type != VLC_VAR_FLOAT &&
441 p_counter->i_type != VLC_VAR_INTEGER &&
442 p_counter->i_compute_type != STATS_LAST )
444 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
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 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
460 if( p_counter->i_compute_type == STATS_LAST ||
461 ( p_counter->i_compute_type == STATS_MAX &&
462 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
463 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
464 ( p_counter->i_type == VLC_VAR_FLOAT &&
465 p_counter->pp_samples[0]->value.f_float > val.f_float )
467 ( p_counter->i_compute_type == STATS_MIN &&
468 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
469 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
470 ( p_counter->i_type == VLC_VAR_FLOAT &&
471 p_counter->pp_samples[0]->value.f_float < val.f_float )
474 if( p_counter->i_type == VLC_VAR_STRING &&
475 p_counter->pp_samples[0]->value.psz_string )
477 free( p_counter->pp_samples[0]->value.psz_string );
479 p_counter->pp_samples[0]->value = val;
480 *new_val = p_counter->pp_samples[0]->value;
484 case STATS_DERIVATIVE:
486 counter_sample_t *p_new, *p_old;
487 if( mdate() - p_counter->last_update < p_counter->update_interval )
491 p_counter->last_update = mdate();
492 if( p_counter->i_type != VLC_VAR_FLOAT &&
493 p_counter->i_type != VLC_VAR_INTEGER )
495 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
498 /* Insert the new one at the beginning */
499 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
501 p_new->date = p_counter->last_update;
502 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
505 if( p_counter->i_samples == 3 )
507 p_old = p_counter->pp_samples[2];
508 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
514 if( p_counter->i_samples > 1)
516 msg_Err( p_handler, "LAST counter has several samples !" );
519 if( p_counter->i_samples == 0 )
521 counter_sample_t *p_new = (counter_sample_t*)malloc(
522 sizeof( counter_sample_t ) );
523 p_new->value.psz_string = NULL;
525 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
526 p_counter->i_samples, p_new );
528 if( p_counter->i_samples == 1 )
530 switch( p_counter->i_type )
532 case VLC_VAR_INTEGER:
533 p_counter->pp_samples[0]->value.i_int += val.i_int;
535 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
538 p_counter->pp_samples[0]->value.f_float += val.f_float;
540 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
542 msg_Err( p_handler, "Trying to increment invalid variable %s",
543 p_counter->psz_name );
552 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
557 if( !p_counter || p_counter->i_samples != 2 )
559 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
562 i_total = p_counter->pp_samples[1]->value.i_int;
563 total = p_counter->pp_samples[1]->date;
564 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
566 last = mdate() - p_counter->pp_samples[0]->date;
572 last = p_counter->pp_samples[0]->date;
577 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
578 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
579 (float)(total)/(1000*(float)i_total ) );
584 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
585 p_counter->psz_name, (float)total/1000, i_total,
586 (float)(total)/(1000*(float)i_total ) );