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