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_TimerStart( vlc_object_t *p_obj, const char *psz_name,
263 counter_t *p_counter = NULL;
264 if( !p_obj->p_libvlc->b_stats ) return;
265 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
267 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
269 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
271 p_counter = p_obj->p_libvlc->pp_timers[i];
277 counter_sample_t *p_sample;
278 p_counter = stats_CounterCreate( p_obj->p_libvlc_global, VLC_VAR_TIME,
282 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
285 p_counter->psz_name = strdup( psz_name );
286 p_counter->i_id = i_id;
287 INSERT_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers,
288 p_obj->p_libvlc->i_timers, p_counter );
290 /* 1st sample : if started: start_date, else last_time, b_started */
291 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
292 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
293 p_counter->i_samples, p_sample );
294 p_sample->date = 0; p_sample->value.b_bool = 0;
295 /* 2nd sample : global_time, i_samples */
296 p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
297 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
298 p_counter->i_samples, p_sample );
299 p_sample->date = 0; p_sample->value.i_int = 0;
301 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
303 msg_Warn( p_obj, "timer %s was already started !", psz_name );
304 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
307 p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
308 p_counter->pp_samples[0]->date = mdate();
309 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
312 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
314 counter_t *p_counter = NULL;
316 if( !p_obj->p_libvlc->b_stats ) return;
317 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
318 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
320 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
322 p_counter = p_obj->p_libvlc->pp_timers[i];
326 if( !p_counter || p_counter->i_samples != 2 )
328 msg_Err( p_obj, "timer does not exist" );
329 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
332 p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
333 p_counter->pp_samples[1]->value.i_int += 1;
334 p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
335 p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
336 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
339 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
341 counter_t *p_counter = NULL;
343 if( !p_obj->p_libvlc->b_stats ) return;
344 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
345 for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
347 if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
349 p_counter = p_obj->p_libvlc->pp_timers[i];
353 TimerDump( p_obj, p_counter, VLC_TRUE );
354 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
357 void __stats_TimersDumpAll( vlc_object_t *p_obj )
360 if( !p_obj->p_libvlc->b_stats ) return;
361 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
362 for ( i = 0 ; i< p_obj->p_libvlc->i_timers ; i++ )
363 TimerDump( p_obj, p_obj->p_libvlc->pp_timers[i], VLC_FALSE );
364 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
367 void __stats_TimersClean( vlc_object_t *p_obj )
370 vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
371 for ( i = p_obj->p_libvlc->i_timers -1 ; i >= 0; i-- )
373 counter_t *p_counter = p_obj->p_libvlc->pp_timers[i];
374 REMOVE_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers, i );
375 stats_CounterClean( p_counter );
377 vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
380 void stats_CounterClean( counter_t *p_c )
385 i = p_c->i_samples - 1 ;
388 counter_sample_t *p_s = p_c->pp_samples[i];
389 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
393 if( p_c->psz_name ) free( p_c->psz_name );
399 /********************************************************************
400 * Following functions are local
401 ********************************************************************/
404 * Update a statistics counter, according to its type
405 * If needed, perform a bit of computation (derivative, mostly)
406 * This function must be entered with stats handler lock
407 * \param p_counter the counter to update
408 * \param val the "new" value
409 * \return an error code
411 static int CounterUpdate( vlc_object_t *p_handler,
412 counter_t *p_counter,
413 vlc_value_t val, vlc_value_t *new_val )
415 switch( p_counter->i_compute_type )
420 if( p_counter->i_samples > 1)
422 msg_Err( p_handler, "LAST counter has several samples !" );
425 if( p_counter->i_type != VLC_VAR_FLOAT &&
426 p_counter->i_type != VLC_VAR_INTEGER &&
427 p_counter->i_compute_type != STATS_LAST )
429 msg_Err( p_handler, "unable to compute MIN or MAX for this type");
433 if( p_counter->i_samples == 0 )
435 counter_sample_t *p_new = (counter_sample_t*)malloc(
436 sizeof( counter_sample_t ) );
437 p_new->value.psz_string = NULL;
439 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
440 p_counter->i_samples, p_new );
442 if( p_counter->i_samples == 1 )
444 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
445 if( p_counter->i_compute_type == STATS_LAST ||
446 ( p_counter->i_compute_type == STATS_MAX &&
447 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
448 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
449 ( p_counter->i_type == VLC_VAR_FLOAT &&
450 p_counter->pp_samples[0]->value.f_float > val.f_float )
452 ( p_counter->i_compute_type == STATS_MIN &&
453 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
454 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
455 ( p_counter->i_type == VLC_VAR_FLOAT &&
456 p_counter->pp_samples[0]->value.f_float < val.f_float )
459 if( p_counter->i_type == VLC_VAR_STRING &&
460 p_counter->pp_samples[0]->value.psz_string )
462 free( p_counter->pp_samples[0]->value.psz_string );
464 p_counter->pp_samples[0]->value = val;
465 *new_val = p_counter->pp_samples[0]->value;
469 case STATS_DERIVATIVE:
471 counter_sample_t *p_new, *p_old;
472 if( mdate() - p_counter->last_update < p_counter->update_interval )
476 p_counter->last_update = mdate();
477 if( p_counter->i_type != VLC_VAR_FLOAT &&
478 p_counter->i_type != VLC_VAR_INTEGER )
480 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
483 /* Insert the new one at the beginning */
484 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
486 p_new->date = p_counter->last_update;
487 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
490 if( p_counter->i_samples == 3 )
492 p_old = p_counter->pp_samples[2];
493 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
499 if( p_counter->i_samples > 1)
501 msg_Err( p_handler, "LAST counter has several samples !" );
504 if( p_counter->i_samples == 0 )
506 counter_sample_t *p_new = (counter_sample_t*)malloc(
507 sizeof( counter_sample_t ) );
508 p_new->value.psz_string = NULL;
510 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
511 p_counter->i_samples, p_new );
513 if( p_counter->i_samples == 1 )
515 switch( p_counter->i_type )
517 case VLC_VAR_INTEGER:
518 p_counter->pp_samples[0]->value.i_int += val.i_int;
520 new_val->i_int = p_counter->pp_samples[0]->value.i_int;
523 p_counter->pp_samples[0]->value.f_float += val.f_float;
525 new_val->f_float = p_counter->pp_samples[0]->value.f_float;
527 msg_Err( p_handler, "Trying to increment invalid variable %s",
528 p_counter->psz_name );
537 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
542 if( !p_counter || p_counter->i_samples != 2 )
544 msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
547 i_total = p_counter->pp_samples[1]->value.i_int;
548 total = p_counter->pp_samples[1]->date;
549 if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
551 last = mdate() - p_counter->pp_samples[0]->date;
557 last = p_counter->pp_samples[0]->date;
562 "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
563 p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
564 (float)(total)/(1000*(float)i_total ) );
569 "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
570 p_counter->psz_name, (float)total/1000, i_total,
571 (float)(total)/(1000*(float)i_total ) );