]> git.sesse.net Git - vlc/blob - src/misc/stats.c
* src/misc/stats.c: fix no-stats mode.
[vlc] / src / misc / stats.c
1 /*****************************************************************************
2  * stats.c: Statistics handling
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>                                               /* required */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
36                               unsigned int i_counter );
37 static int stats_CounterUpdate( stats_handler_t *p_handler,
38                                 counter_t *p_counter,
39                                 vlc_value_t val, vlc_value_t * );
40 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this );
41 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this );
42
43 static void TimerDump( vlc_object_t *p_this, counter_t *p_counter, vlc_bool_t);
44
45 /*****************************************************************************
46  * Exported functions
47  *****************************************************************************/
48
49 /**
50  * Cleanup statistics handler stuff
51  * \param p_stats the handler to clean
52  * \return nothing
53  */
54 void stats_HandlerDestroy( stats_handler_t *p_stats )
55 {
56     int i;
57     for ( i =  p_stats->i_counters - 1 ; i >= 0 ; i-- )
58     {
59         int j;
60         counter_t *p_counter = p_stats->pp_counters[i];
61
62         for( j = p_counter->i_samples -1; j >= 0 ; j-- )
63         {
64             counter_sample_t *p_sample = p_counter->pp_samples[j];
65             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, j );
66             free( p_sample );
67         }
68         free( p_counter->psz_name );
69         REMOVE_ELEM( p_stats->pp_counters, p_stats->i_counters, i );
70         free( p_counter );
71     }
72 }
73
74 /**
75  * Create a statistics counter
76  * \param p_this the object for which to create the counter
77  * \param psz_name the name
78  * \param i_type the type of stored data. One of VLC_VAR_STRING,
79  * VLC_VAR_INTEGER, VLC_VAR_FLOAT
80  * \param i_compute_type the aggregation type. One of STATS_LAST (always
81  * keep the last value), STATS_COUNTER (increment by the passed value),
82  * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
83  * (keep a time derivative of the value)
84  */
85 int __stats_Create( vlc_object_t *p_this, const char *psz_name, unsigned int i_id,
86                     int i_type, int i_compute_type )
87 {
88     counter_t *p_counter;
89     stats_handler_t *p_handler;
90
91     if( !p_this->p_libvlc->b_stats ) return VLC_EGENERIC;
92
93     p_handler = stats_HandlerGet( p_this );
94     if( !p_handler ) return VLC_ENOMEM;
95
96     vlc_mutex_lock( &p_handler->object_lock );
97
98     p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
99
100     p_counter->psz_name = strdup( psz_name );
101     p_counter->i_index = ((uint64_t)p_this->i_object_id << 32 ) + i_id;
102     p_counter->i_compute_type = i_compute_type;
103     p_counter->i_type = i_type;
104     p_counter->i_samples = 0;
105     p_counter->pp_samples = NULL;
106
107     p_counter->update_interval = 0;
108     p_counter->last_update = 0;
109
110     INSERT_ELEM( p_handler->pp_counters, p_handler->i_counters,
111                  p_handler->i_counters, p_counter );
112
113     vlc_mutex_unlock( &p_handler->object_lock );
114
115     return VLC_SUCCESS;
116 }
117
118 /** Update a counter element with new values
119  * \param p_this the object in which to update
120  * \param psz_name the name
121  * \param val the vlc_value union containing the new value to aggregate. For
122  * more information on how data is aggregated, \see __stats_Create
123  */
124 int __stats_Update( vlc_object_t *p_this, unsigned int i_counter,
125                     vlc_value_t val, vlc_value_t *val_new )
126 {
127     int i_ret;
128     counter_t *p_counter;
129     stats_handler_t *p_handler;
130
131     if( !p_this->p_libvlc->b_stats ) return VLC_EGENERIC;
132
133     /* Get stats handler singleton */
134     p_handler = stats_HandlerGet( p_this );
135     if( !p_handler ) return VLC_ENOMEM;
136
137     vlc_mutex_lock( &p_handler->object_lock );
138     /* Look for existing element */
139     p_counter = GetCounter( p_handler, p_this->i_object_id, i_counter );
140     if( !p_counter )
141     {
142         vlc_mutex_unlock( &p_handler->object_lock );
143         vlc_object_release( p_handler );
144         return VLC_ENOOBJ;
145     }
146
147     i_ret = stats_CounterUpdate( p_handler, p_counter, val, val_new );
148     vlc_mutex_unlock( &p_handler->object_lock );
149
150     return i_ret;
151 }
152
153 /** Get the aggregated value for a counter
154  * \param p_this an object
155  * \param i_object_id the object id from which we want the data
156  * \param psz_name the name of the couner
157  * \param val a pointer to an initialized vlc_value union. It will contain the
158  * retrieved value
159  * \return an error code
160  */
161 int __stats_Get( vlc_object_t *p_this, int i_object_id,
162                  unsigned int i_counter, vlc_value_t *val )
163 {
164     counter_t *p_counter;
165     stats_handler_t *p_handler;
166
167     if( !p_this->p_libvlc->b_stats ) return VLC_EGENERIC;
168
169     /* Get stats handler singleton */
170     p_handler = stats_HandlerGet( p_this );
171     if( !p_handler ) return VLC_ENOMEM;
172     vlc_mutex_lock( &p_handler->object_lock );
173
174     /* Look for existing element */
175     p_counter = GetCounter( p_handler, i_object_id, i_counter );
176     if( !p_counter )
177     {
178         vlc_mutex_unlock( &p_handler->object_lock );
179         vlc_object_release( p_handler );
180         val->i_int = val->f_float = 0.0;
181         return VLC_ENOOBJ;
182     }
183
184     if( p_counter->i_samples == 0 )
185     {
186         vlc_mutex_unlock( &p_handler->object_lock );
187         val->i_int = val->f_float = 0.0;
188         return VLC_EGENERIC;
189     }
190
191     switch( p_counter->i_compute_type )
192     {
193     case STATS_LAST:
194     case STATS_MIN:
195     case STATS_MAX:
196     case STATS_COUNTER:
197         *val = p_counter->pp_samples[0]->value;
198         break;
199     case STATS_DERIVATIVE:
200         /* Not ready yet */
201         if( p_counter->i_samples < 2 )
202         {
203             vlc_mutex_unlock( &p_handler->object_lock );
204             val->i_int = 0; val->f_float = 0.0;
205             return VLC_EGENERIC;
206         }
207         if( p_counter->i_type == VLC_VAR_INTEGER )
208         {
209             float f = ( p_counter->pp_samples[0]->value.i_int -
210                         p_counter->pp_samples[1]->value.i_int ) /
211                     (float)(  p_counter->pp_samples[0]->date -
212                               p_counter->pp_samples[1]->date );
213             val->i_int = (int)f;
214         }
215         else
216         {
217             float f = (float)( p_counter->pp_samples[0]->value.f_float -
218                                p_counter->pp_samples[1]->value.f_float ) /
219                       (float)( p_counter->pp_samples[0]->date -
220                                p_counter->pp_samples[1]->date );
221             val->f_float = f;
222         }
223         break;
224     }
225     vlc_object_release( p_handler );
226
227     vlc_mutex_unlock( &p_handler->object_lock );
228     return VLC_SUCCESS;;
229 }
230
231 /** Get a statistics counter structure. This allows for low-level modifications
232  * \param p_this a parent object
233  * \param i_object_id the object from which to retrieve data
234  * \param psz_name the name
235  * \return the counter, or NULL if not found (or handler not created yet)
236  */
237 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
238                                unsigned int i_counter )
239 {
240     counter_t *p_counter;
241     stats_handler_t *p_handler;
242
243     if( !p_this->p_libvlc->b_stats ) return NULL;
244
245     p_handler = stats_HandlerGet( p_this );
246     if( !p_handler ) return NULL;
247
248     vlc_mutex_lock( &p_handler->object_lock );
249
250     /* Look for existing element */
251     p_counter = GetCounter( p_handler, i_object_id, i_counter );
252     vlc_mutex_unlock( &p_handler->object_lock );
253     vlc_object_release( p_handler );
254
255     return p_counter;
256 }
257
258
259 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
260 {
261     vlc_object_t *p_obj;
262     vlc_list_t *p_list;
263     int i_index;
264
265     if( !p_input->p_libvlc->b_stats ) return;
266
267     vlc_mutex_lock( &p_stats->lock );
268
269     /* Input */
270     stats_GetInteger( p_input, p_input->i_object_id, STATS_READ_PACKETS,
271                        &p_stats->i_read_packets );
272     stats_GetInteger( p_input, p_input->i_object_id, STATS_READ_BYTES,
273                        &p_stats->i_read_bytes );
274     stats_GetFloat( p_input, p_input->i_object_id, STATS_INPUT_BITRATE,
275                        &p_stats->f_input_bitrate );
276
277     stats_GetInteger( p_input, p_input->i_object_id, STATS_DEMUX_READ,
278                       &p_stats->i_demux_read_bytes );
279     stats_GetFloat( p_input, p_input->i_object_id, STATS_DEMUX_BITRATE,
280                       &p_stats->f_demux_bitrate );
281
282     stats_GetInteger( p_input, p_input->i_object_id, STATS_DECODED_VIDEO,
283                       &p_stats->i_decoded_video );
284     stats_GetInteger( p_input, p_input->i_object_id, STATS_DECODED_AUDIO,
285                       &p_stats->i_decoded_audio );
286
287     /* Sout */
288     stats_GetInteger( p_input, p_input->i_object_id, STATS_SOUT_SENT_PACKETS,
289                       &p_stats->i_sent_packets );
290     stats_GetInteger( p_input, p_input->i_object_id, STATS_SOUT_SENT_BYTES,
291                       &p_stats->i_sent_bytes );
292     stats_GetFloat  ( p_input, p_input->i_object_id, STATS_SOUT_SEND_BITRATE,
293                       &p_stats->f_send_bitrate );
294
295     /* Aout - We store in p_input because aout is shared */
296     stats_GetInteger( p_input, p_input->i_object_id, STATS_PLAYED_ABUFFERS,
297                       &p_stats->i_played_abuffers );
298     stats_GetInteger( p_input, p_input->i_object_id, STATS_LOST_ABUFFERS,
299                       &p_stats->i_lost_abuffers );
300
301     /* Vouts - FIXME: Store all in input */
302     p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
303     if( p_list )
304     {
305         p_stats->i_displayed_pictures  = 0 ;
306         p_stats->i_lost_pictures = 0;
307         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
308         {
309             int i_displayed = 0, i_lost = 0;
310             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
311             stats_GetInteger( p_obj, p_obj->i_object_id,
312                               STATS_DISPLAYED_PICTURES,
313                               &i_displayed );
314             stats_GetInteger( p_obj, p_obj->i_object_id, STATS_LOST_PICTURES,
315                               &i_lost );
316             p_stats->i_displayed_pictures += i_displayed;
317             p_stats->i_lost_pictures += i_lost;
318          }
319         vlc_list_release( p_list );
320     }
321
322     vlc_mutex_unlock( &p_stats->lock );
323 }
324
325
326 void stats_ReinitInputStats( input_stats_t *p_stats )
327 {
328     p_stats->i_read_packets = p_stats->i_read_bytes =
329     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
330     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
331     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
332     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
333     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
334     p_stats->i_decoded_video = p_stats->i_decoded_audio =
335     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
336      = 0;
337 }
338
339 void stats_DumpInputStats( input_stats_t *p_stats  )
340 {
341     vlc_mutex_lock( &p_stats->lock );
342     /* f_bitrate is in bytes / microsecond
343      * *1000 => bytes / millisecond => kbytes / seconds */
344     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
345                      "Demux : %i (%i bytes) - %f kB/s\n"
346                      " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
347                     p_stats->i_read_packets, p_stats->i_read_bytes,
348                     p_stats->f_input_bitrate * 1000,
349                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
350                     p_stats->f_demux_bitrate * 1000,
351                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
352                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
353                     p_stats->f_send_bitrate );
354     vlc_mutex_unlock( &p_stats->lock );
355 }
356
357 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
358 {
359     vlc_list_t *p_list;
360     int i_index;
361
362     if( !p_obj->p_libvlc->b_stats ) return;
363
364     vlc_mutex_lock( &p_stats->lock );
365
366     p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
367     if( p_list )
368     {
369         float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
370         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
371         {
372             float f_in = 0, f_out = 0, f_demux = 0;
373             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
374             stats_GetFloat( p_obj, p_obj->i_object_id, STATS_INPUT_BITRATE,
375                             &f_in );
376             stats_GetFloat( p_obj, p_obj->i_object_id, STATS_SOUT_SEND_BITRATE,
377                             &f_out );
378             stats_GetFloat( p_obj, p_obj->i_object_id, STATS_DEMUX_BITRATE,
379                             &f_demux );
380             f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
381         }
382         p_stats->f_input_bitrate = f_total_in;
383         p_stats->f_output_bitrate = f_total_out;
384         p_stats->f_demux_bitrate = f_total_demux;
385         vlc_list_release( p_list );
386     }
387
388     vlc_mutex_unlock( &p_stats->lock );
389 }
390
391 void stats_ReinitGlobalStats( global_stats_t *p_stats )
392 {
393     p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
394 }
395
396
397 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
398                          unsigned int i_id )
399 {
400     counter_t *p_counter;
401
402     if( !p_obj->p_libvlc->b_stats ) return;
403
404     p_counter = stats_CounterGet( p_obj, p_obj->p_vlc->i_object_id, i_id );
405     if( !p_counter )
406     {
407         counter_sample_t *p_sample;
408         stats_Create( p_obj->p_vlc, psz_name, i_id, VLC_VAR_TIME, STATS_TIMER );
409         p_counter = stats_CounterGet( p_obj,  p_obj->p_vlc->i_object_id,
410                                       i_id );
411         if( !p_counter ) return;
412         /* 1st sample : if started: start_date, else last_time, b_started */
413         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
414         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
415                      p_counter->i_samples, p_sample );
416         p_sample->date = 0; p_sample->value.b_bool = 0;
417         /* 2nd sample : global_time, i_samples */
418         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
419         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
420                      p_counter->i_samples, p_sample );
421         p_sample->date = 0; p_sample->value.i_int = 0;
422     }
423     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
424     {
425         msg_Warn( p_obj, "timer %s was already started !", psz_name );
426         return;
427     }
428     p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
429     p_counter->pp_samples[0]->date = mdate();
430 }
431
432 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
433 {
434     counter_t *p_counter;
435
436     if( !p_obj->p_libvlc->b_stats ) return;
437
438     p_counter = stats_CounterGet( p_obj, p_obj->p_vlc->i_object_id, i_id );
439     if( !p_counter || p_counter->i_samples != 2 )
440     {
441         msg_Err( p_obj, "Timer does not exist" );
442         return;
443     }
444     p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
445     p_counter->pp_samples[1]->value.i_int += 1;
446     p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
447     p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
448 }
449
450 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
451 {
452     counter_t *p_counter;
453
454     if( !p_obj->p_libvlc->b_stats ) return;
455
456     p_counter = stats_CounterGet( p_obj, p_obj->p_vlc->i_object_id, i_id );
457     TimerDump( p_obj, p_counter, VLC_TRUE );
458 }
459
460
461 void __stats_TimersDumpAll( vlc_object_t *p_obj )
462 {
463     int i;
464     stats_handler_t *p_handler = stats_HandlerGet( p_obj );
465     if( !p_handler ) return;
466
467     vlc_mutex_lock( &p_handler->object_lock );
468     for ( i = 0 ; i< p_handler->i_counters; i++ )
469     {
470         counter_t * p_counter = p_handler->pp_counters[i];
471         if( p_counter->i_compute_type == STATS_TIMER )
472         {
473             TimerDump( p_obj, p_counter, VLC_FALSE );
474         }
475     }
476     vlc_mutex_unlock( &p_handler->object_lock );
477 }
478
479
480 /********************************************************************
481  * Following functions are local
482  ********************************************************************/
483
484 /**
485  * Update a statistics counter, according to its type
486  * If needed, perform a bit of computation (derivative, mostly)
487  * This function must be entered with stats handler lock
488  * \param p_handler stats handler singleton
489  * \param p_counter the counter to update
490  * \param val the "new" value
491  * \return an error code
492  */
493 static int stats_CounterUpdate( stats_handler_t *p_handler,
494                                 counter_t *p_counter,
495                                 vlc_value_t val, vlc_value_t *new_val )
496 {
497     switch( p_counter->i_compute_type )
498     {
499     case STATS_LAST:
500     case STATS_MIN:
501     case STATS_MAX:
502         if( p_counter->i_samples > 1)
503         {
504             msg_Err( p_handler, "LAST counter has several samples !" );
505             return VLC_EGENERIC;
506         }
507         if( p_counter->i_type != VLC_VAR_FLOAT &&
508             p_counter->i_type != VLC_VAR_INTEGER &&
509             p_counter->i_compute_type != STATS_LAST )
510         {
511             msg_Err( p_handler, "unable to compute MIN or MAX for this type");
512             return VLC_EGENERIC;
513         }
514
515         if( p_counter->i_samples == 0 )
516         {
517             counter_sample_t *p_new = (counter_sample_t*)malloc(
518                                                sizeof( counter_sample_t ) );
519             p_new->value.psz_string = NULL;
520
521             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
522                          p_counter->i_samples, p_new );
523         }
524         if( p_counter->i_samples == 1 )
525         {
526             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
527             if( p_counter->i_compute_type == STATS_LAST ||
528                 ( p_counter->i_compute_type == STATS_MAX &&
529                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
530                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
531                      ( p_counter->i_type == VLC_VAR_FLOAT &&
532                        p_counter->pp_samples[0]->value.f_float > val.f_float )
533                    ) ) ||
534                 ( p_counter->i_compute_type == STATS_MIN &&
535                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
536                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
537                      ( p_counter->i_type == VLC_VAR_FLOAT &&
538                        p_counter->pp_samples[0]->value.f_float < val.f_float )
539                    ) ) )
540             {
541                 if( p_counter->i_type == VLC_VAR_STRING &&
542                     p_counter->pp_samples[0]->value.psz_string )
543                 {
544                     free( p_counter->pp_samples[0]->value.psz_string );
545                 }
546                 p_counter->pp_samples[0]->value = val;
547                 *new_val = p_counter->pp_samples[0]->value;
548             }
549         }
550         break;
551     case STATS_DERIVATIVE:
552     {
553         counter_sample_t *p_new, *p_old;
554         if( mdate() - p_counter->last_update < p_counter->update_interval )
555         {
556             return VLC_EGENERIC;
557         }
558         p_counter->last_update = mdate();
559         if( p_counter->i_type != VLC_VAR_FLOAT &&
560             p_counter->i_type != VLC_VAR_INTEGER )
561         {
562             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
563             return VLC_EGENERIC;
564         }
565         /* Insert the new one at the beginning */
566         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
567         p_new->value = val;
568         p_new->date = p_counter->last_update;
569         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
570                      0, p_new );
571
572         if( p_counter->i_samples == 3 )
573         {
574             p_old = p_counter->pp_samples[2];
575             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
576             free( p_old );
577         }
578         break;
579     }
580     case STATS_COUNTER:
581         if( p_counter->i_samples > 1)
582         {
583             msg_Err( p_handler, "LAST counter has several samples !" );
584             return VLC_EGENERIC;
585         }
586         if( p_counter->i_samples == 0 )
587         {
588             counter_sample_t *p_new = (counter_sample_t*)malloc(
589                                                sizeof( counter_sample_t ) );
590             p_new->value.psz_string = NULL;
591
592             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
593                          p_counter->i_samples, p_new );
594         }
595         if( p_counter->i_samples == 1 )
596         {
597             switch( p_counter->i_type )
598             {
599             case VLC_VAR_INTEGER:
600                 p_counter->pp_samples[0]->value.i_int += val.i_int;
601                 if( new_val )
602                     new_val->i_int = p_counter->pp_samples[0]->value.i_int;
603                 break;
604             case VLC_VAR_FLOAT:
605                 p_counter->pp_samples[0]->value.f_float += val.f_float;
606                 if( new_val )
607                     new_val->f_float = p_counter->pp_samples[0]->value.f_float;
608             default:
609                 msg_Err( p_handler, "Trying to increment invalid variable %s",
610                          p_counter->psz_name );
611                 return VLC_EGENERIC;
612             }
613         }
614         break;
615     }
616     return VLC_SUCCESS;
617 }
618
619 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
620                               unsigned int i_counter )
621 {
622     int i;
623     uint64_t i_index = ((uint64_t) i_object_id << 32 ) + i_counter;
624     for (i = 0 ; i < p_handler->i_counters ; i++ )
625     {
626          if( i_index == p_handler->pp_counters[i]->i_index )
627              return p_handler->pp_counters[i];
628     }
629     return NULL;
630 }
631
632
633 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
634 {
635     stats_handler_t *p_handler = p_this->p_libvlc->p_stats;
636     if( !p_handler )
637     {
638         p_handler = stats_HandlerCreate( p_this );
639         if( !p_handler )
640         {
641             return NULL;
642         }
643     }
644     vlc_object_yield( p_handler );
645     return p_handler;
646 }
647
648 /**
649  * Initialize statistics handler
650  *
651  * This function initializes the global statistics handler singleton,
652  * \param p_this the parent VLC object
653  */
654 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
655 {
656     stats_handler_t *p_handler;
657
658     msg_Dbg( p_this, "creating statistics handler" );
659
660     p_handler = (stats_handler_t*) vlc_object_create( p_this,
661                                                       VLC_OBJECT_STATS );
662
663     if( !p_handler )
664     {
665         msg_Err( p_this, "out of memory" );
666         return NULL;
667     }
668     p_handler->i_counters = 0;
669     p_handler->pp_counters = NULL;
670
671     /// \bug is it p_vlc or p_libvlc ?
672     vlc_object_attach( p_handler, p_this->p_vlc );
673
674     p_this->p_libvlc->p_stats = p_handler;
675
676     return p_handler;
677 }
678
679 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
680                        vlc_bool_t b_total )
681 {
682     mtime_t last, total;
683     int i_total;
684     if( !p_counter || p_counter->i_samples != 2 )
685     {
686         msg_Err( p_obj, "Timer %s does not exist", p_counter->psz_name );
687         return;
688     }
689     i_total = p_counter->pp_samples[1]->value.i_int;
690     total = p_counter->pp_samples[1]->date;
691     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
692     {
693         last = mdate() - p_counter->pp_samples[0]->date;
694         i_total += 1;
695         total += last;
696     }
697     else
698     {
699         last = p_counter->pp_samples[0]->date;
700     }
701     if( b_total )
702     {
703         msg_Dbg( p_obj,
704              "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
705              p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
706              (float)(total)/(1000*(float)i_total ) );
707     }
708     else
709     {
710         msg_Dbg( p_obj,
711              "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
712              p_counter->psz_name, (float)total/1000, i_total,
713              (float)(total)/(1000*(float)i_total ) );
714     }
715 }