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