]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Use a hash for stats. Not finished
[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                               const char *psz_name );
37 static int stats_CounterUpdate( stats_handler_t *p_handler,
38                                 counter_t *p_counter,
39                                 vlc_value_t val );
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         hashtable_entry_t p_entry = p_stats->p_counters[i];
61         counter_t * p_counter = p_entry.p_data;
62
63         for( j = p_counter->i_samples -1; j >= 0 ; j-- )
64         {
65             counter_sample_t *p_sample = p_counter->pp_samples[j];
66             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, j );
67             free( p_sample );
68         }
69         free( p_counter->psz_name );
70         free( p_entry.psz_name );
71         REMOVE_ELEM( p_stats->p_counters, p_stats->i_counters, i );
72         free( p_counter );
73     }
74 }
75
76 /**
77  * Create a statistics counter
78  * \param p_this the object for which to create the counter
79  * \param psz_name the name
80  * \param i_type the type of stored data. One of VLC_VAR_STRING,
81  * VLC_VAR_INTEGER, VLC_VAR_FLOAT
82  * \param i_compute_type the aggregation type. One of STATS_LAST (always
83  * keep the last value), STATS_COUNTER (increment by the passed value),
84  * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
85  * (keep a time derivative of the value)
86  */
87 int __stats_Create( vlc_object_t *p_this, const char *psz_name, int i_type,
88                     int i_compute_type )
89 {
90     counter_t *p_counter;
91     stats_handler_t *p_handler;
92
93     if( p_this->p_libvlc->b_stats == VLC_FALSE )
94     {
95         return VLC_EGENERIC;
96     }
97     p_handler = stats_HandlerGet( p_this );
98     if( !p_handler ) return VLC_ENOMEM;
99
100     vlc_mutex_lock( &p_handler->object_lock );
101
102     p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
103
104     p_counter->psz_name = strdup( psz_name );
105     p_counter->i_source_object = p_this->i_object_id;
106     p_counter->i_compute_type = i_compute_type;
107     p_counter->i_type = i_type;
108     p_counter->i_samples = 0;
109     p_counter->pp_samples = NULL;
110
111     p_counter->update_interval = 0;
112     p_counter->last_update = 0;
113
114     vlc_HashInsert( &p_handler->p_counters, &p_handler->i_counters, p_this->i_object_id,
115                     psz_name, p_counter );
116
117     vlc_mutex_unlock( &p_handler->object_lock );
118
119     return VLC_SUCCESS;
120 }
121
122 /** Update a counter element with new values
123  * \param p_this the object in which to update
124  * \param psz_name the name
125  * \param val the vlc_value union containing the new value to aggregate. For
126  * more information on how data is aggregated, \see __stats_Create
127  */
128 int __stats_Update( vlc_object_t *p_this, const char *psz_name,
129                     vlc_value_t val )
130 {
131     int i_ret;
132     counter_t *p_counter;
133
134     /* Get stats handler singleton */
135     stats_handler_t *p_handler;
136     if( p_this->p_libvlc->b_stats == VLC_FALSE )
137     {
138         return VLC_EGENERIC;
139     }
140     p_handler = stats_HandlerGet( p_this );
141     if( !p_handler ) return VLC_ENOMEM;
142
143     vlc_mutex_lock( &p_handler->object_lock );
144     /* Look for existing element */
145     p_counter = GetCounter( p_handler, p_this->i_object_id,
146                             psz_name );
147     if( !p_counter )
148     {
149         vlc_mutex_unlock( &p_handler->object_lock );
150         vlc_object_release( p_handler );
151         return VLC_ENOOBJ;
152     }
153
154     i_ret = stats_CounterUpdate( p_handler, p_counter, val );
155     vlc_mutex_unlock( &p_handler->object_lock );
156
157     return i_ret;
158 }
159
160 /** Get the aggregated value for a counter
161  * \param p_this an object
162  * \param i_object_id the object id from which we want the data
163  * \param psz_name the name of the couner
164  * \param val a pointer to an initialized vlc_value union. It will contain the
165  * retrieved value
166  * \return an error code
167  */
168 int __stats_Get( vlc_object_t *p_this, int i_object_id,
169                  const char *psz_name, vlc_value_t *val )
170 {
171     counter_t *p_counter;
172
173     /* Get stats handler singleton */
174     stats_handler_t *p_handler;
175     if( p_this->p_libvlc->b_stats == VLC_FALSE )
176     {
177         return VLC_EGENERIC;
178     }
179     p_handler = stats_HandlerGet( p_this );
180     if( !p_handler ) return VLC_ENOMEM;
181     vlc_mutex_lock( &p_handler->object_lock );
182
183     /* Look for existing element */
184     p_counter = GetCounter( p_handler, i_object_id,
185                             psz_name );
186     if( !p_counter )
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_ENOOBJ;
192     }
193
194     if( p_counter->i_samples == 0 )
195     {
196         vlc_mutex_unlock( &p_handler->object_lock );
197         val->i_int = val->f_float = 0.0;
198         return VLC_EGENERIC;
199     }
200
201     switch( p_counter->i_compute_type )
202     {
203     case STATS_LAST:
204     case STATS_MIN:
205     case STATS_MAX:
206     case STATS_COUNTER:
207         *val = p_counter->pp_samples[0]->value;
208         break;
209     case STATS_DERIVATIVE:
210         /* Not ready yet */
211         if( p_counter->i_samples < 2 )
212         {
213             vlc_mutex_unlock( &p_handler->object_lock );
214             val->i_int = 0; val->f_float = 0.0;
215             return VLC_EGENERIC;
216         }
217         if( p_counter->i_type == VLC_VAR_INTEGER )
218         {
219             float f = ( p_counter->pp_samples[0]->value.i_int -
220                         p_counter->pp_samples[1]->value.i_int ) /
221                     (float)(  p_counter->pp_samples[0]->date -
222                               p_counter->pp_samples[1]->date );
223             val->i_int = (int)f;
224         }
225         else
226         {
227             float f = (float)( p_counter->pp_samples[0]->value.f_float -
228                                p_counter->pp_samples[1]->value.f_float ) /
229                       (float)( p_counter->pp_samples[0]->date -
230                                p_counter->pp_samples[1]->date );
231             val->f_float = f;
232         }
233         break;
234     }
235     vlc_object_release( p_handler );
236
237     vlc_mutex_unlock( &p_handler->object_lock );
238     return VLC_SUCCESS;;
239 }
240
241 /** Get a statistics counter structure. This allows for low-level modifications
242  * \param p_this a parent object
243  * \param i_object_id the object from which to retrieve data
244  * \param psz_name the name
245  * \return the counter, or NULL if not found (or handler not created yet)
246  */
247 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
248                                const char *psz_name )
249 {
250     counter_t *p_counter;
251
252     stats_handler_t *p_handler;
253     if( p_this->p_libvlc->b_stats == VLC_FALSE )
254     {
255         return NULL;
256     }
257     p_handler = stats_HandlerGet( p_this );
258     if( !p_handler ) return NULL;
259
260     vlc_mutex_lock( &p_handler->object_lock );
261
262     /* Look for existing element */
263     p_counter = GetCounter( p_handler, i_object_id,
264                             psz_name );
265     vlc_mutex_unlock( &p_handler->object_lock );
266     vlc_object_release( p_handler );
267
268     return p_counter;
269 }
270
271
272 void stats_ComputeInputStats( input_thread_t *p_input,
273                               input_stats_t *p_stats )
274 {
275     vlc_object_t *p_obj;
276     vlc_list_t *p_list;
277     int i_index;
278     vlc_mutex_lock( &p_stats->lock );
279
280     /* Input */
281     stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
282                        &p_stats->i_read_packets );
283     stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
284                        &p_stats->i_read_bytes );
285     stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
286                        &p_stats->f_input_bitrate );
287
288     stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
289                       &p_stats->i_demux_read_bytes );
290     stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
291                       &p_stats->f_demux_bitrate );
292
293     stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
294                       &p_stats->i_decoded_video );
295     stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
296                       &p_stats->i_decoded_audio );
297
298     /* Sout */
299     stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_packets",
300                       &p_stats->i_sent_packets );
301     stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_bytes",
302                       &p_stats->i_sent_bytes );
303     stats_GetFloat  ( p_input, p_input->i_object_id, "sout_send_bitrate",
304                       &p_stats->f_send_bitrate );
305
306     /* Aout - We store in p_input because aout is shared */
307     stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
308                       &p_stats->i_played_abuffers );
309     stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
310                       &p_stats->i_lost_abuffers );
311
312     /* Vouts - FIXME: Store all in input */
313     p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
314     if( p_list )
315     {
316         p_stats->i_displayed_pictures  = 0 ;
317         p_stats->i_lost_pictures = 0;
318         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
319         {
320             int i_displayed = 0, i_lost = 0;
321             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
322             stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
323                               &i_displayed );
324             stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
325                               &i_lost );
326             p_stats->i_displayed_pictures += i_displayed;
327             p_stats->i_lost_pictures += i_lost;
328          }
329         vlc_list_release( p_list );
330     }
331
332     vlc_mutex_unlock( &p_stats->lock );
333 }
334
335
336 void stats_ReinitInputStats( input_stats_t *p_stats )
337 {
338     p_stats->i_read_packets = p_stats->i_read_bytes =
339     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
340     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
341     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
342     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
343     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
344     p_stats->i_decoded_video = p_stats->i_decoded_audio =
345     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
346      = 0;
347 }
348
349 void stats_DumpInputStats( input_stats_t *p_stats  )
350 {
351     vlc_mutex_lock( &p_stats->lock );
352     /* f_bitrate is in bytes / microsecond
353      * *1000 => bytes / millisecond => kbytes / seconds */
354     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
355                      " - Vout : %i/%i - Aout : %i/%i - Vout : %f\n",
356                     p_stats->i_read_packets, p_stats->i_read_bytes,
357                     p_stats->f_input_bitrate * 1000,
358                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
359                     p_stats->f_demux_bitrate * 1000,
360                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
361                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
362                     p_stats->f_send_bitrate );
363     vlc_mutex_unlock( &p_stats->lock );
364 }
365
366 void __stats_ComputeGlobalStats( vlc_object_t *p_obj,
367                                 global_stats_t *p_stats )
368 {
369     vlc_list_t *p_list;
370     int i_index;
371     vlc_mutex_lock( &p_stats->lock );
372
373     p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
374     if( p_list )
375     {
376         float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
377         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
378         {
379             float f_in = 0, f_out = 0, f_demux = 0;
380             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
381             stats_GetFloat( p_obj, p_obj->i_object_id, "input_bitrate",
382                             &f_in );
383             stats_GetFloat( p_obj, p_obj->i_object_id, "sout_send_bitrate",
384                             &f_out );
385             stats_GetFloat( p_obj, p_obj->i_object_id, "demux_bitrate",
386                             &f_demux );
387             f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
388         }
389         p_stats->f_input_bitrate = f_total_in;
390         p_stats->f_output_bitrate = f_total_out;
391         p_stats->f_demux_bitrate = f_total_demux;
392         vlc_list_release( p_list );
393     }
394
395     vlc_mutex_unlock( &p_stats->lock );
396 }
397
398 void stats_ReinitGlobalStats( global_stats_t *p_stats )
399 {
400     p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
401 }
402
403
404 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name )
405 {
406     counter_t *p_counter = stats_CounterGet( p_obj,
407                                              p_obj->p_vlc->i_object_id,
408                                              psz_name );
409     if( !p_counter )
410     {
411         counter_sample_t *p_sample;
412         stats_Create( p_obj->p_vlc, psz_name, VLC_VAR_TIME, STATS_TIMER );
413         p_counter = stats_CounterGet( p_obj,  p_obj->p_vlc->i_object_id,
414                                       psz_name );
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, const char *psz_name )
437 {
438     counter_t *p_counter = stats_CounterGet( p_obj,
439                                               p_obj->p_vlc->i_object_id,
440                                              psz_name );
441     if( !p_counter || p_counter->i_samples != 2 )
442     {
443         msg_Err( p_obj, "timer %s does not exist", psz_name );
444         return;
445     }
446     p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
447     p_counter->pp_samples[1]->value.i_int += 1;
448     p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
449     p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
450 }
451
452 void __stats_TimerDump( vlc_object_t *p_obj, const char *psz_name )
453 {
454     counter_t *p_counter = stats_CounterGet( p_obj,
455                                              p_obj->p_vlc->i_object_id,
456                                              psz_name );
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 = (counter_t *)(p_handler->p_counters[i].p_data);
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 )
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             }
548         }
549         break;
550     case STATS_DERIVATIVE:
551     {
552         counter_sample_t *p_new, *p_old;
553         if( mdate() - p_counter->last_update < p_counter->update_interval )
554         {
555             return VLC_EGENERIC;
556         }
557         p_counter->last_update = mdate();
558         if( p_counter->i_type != VLC_VAR_FLOAT &&
559             p_counter->i_type != VLC_VAR_INTEGER )
560         {
561             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
562             return VLC_EGENERIC;
563         }
564         /* Insert the new one at the beginning */
565         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
566         p_new->value = val;
567         p_new->date = p_counter->last_update;
568         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
569                      0, p_new );
570
571         if( p_counter->i_samples == 3 )
572         {
573             p_old = p_counter->pp_samples[2];
574             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
575             free( p_old );
576         }
577         break;
578     }
579     case STATS_COUNTER:
580         if( p_counter->i_samples > 1)
581         {
582             msg_Err( p_handler, "LAST counter has several samples !" );
583             return VLC_EGENERIC;
584         }
585         if( p_counter->i_samples == 0 )
586         {
587             counter_sample_t *p_new = (counter_sample_t*)malloc(
588                                                sizeof( counter_sample_t ) );
589             p_new->value.psz_string = NULL;
590
591             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
592                          p_counter->i_samples, p_new );
593         }
594         if( p_counter->i_samples == 1 )
595         {
596             switch( p_counter->i_type )
597             {
598             case VLC_VAR_INTEGER:
599             case VLC_VAR_FLOAT:
600                 p_counter->pp_samples[0]->value.i_int += val.i_int;
601                 break;
602             default:
603                 msg_Err( p_handler, "Trying to increment invalid variable %s",
604                          p_counter->psz_name );
605                 return VLC_EGENERIC;
606             }
607         }
608         break;
609     }
610     return VLC_SUCCESS;
611 }
612
613 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
614                               const char *psz_name )
615 {
616     int i;
617     return (counter_t *)vlc_HashRetrieve( p_handler->p_counters, p_handler->i_counters,
618                                           i_object_id, psz_name );
619 }
620
621
622 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
623 {
624     stats_handler_t *p_handler = (stats_handler_t*)
625                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
626                                            FIND_ANYWHERE );
627     if( !p_handler )
628     {
629         p_handler = stats_HandlerCreate( p_this );
630         if( !p_handler )
631         {
632             return NULL;
633         }
634         vlc_object_yield( p_handler );
635     }
636     return p_handler;
637 }
638
639 /**
640  * Initialize statistics handler
641  *
642  * This function initializes the global statistics handler singleton,
643  * \param p_this the parent VLC object
644  */
645 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
646 {
647     stats_handler_t *p_handler;
648
649     msg_Dbg( p_this, "creating statistics handler" );
650
651     p_handler = (stats_handler_t*) vlc_object_create( p_this,
652                                                       VLC_OBJECT_STATS );
653
654     if( !p_handler )
655     {
656         msg_Err( p_this, "out of memory" );
657         return NULL;
658     }
659     p_handler->i_counters = 0;
660     p_handler->p_counters = (hashtable_entry_t *) malloc( 5 * sizeof( variable_t ) );
661
662     /// \bug is it p_vlc or p_libvlc ?
663     vlc_object_attach( p_handler, p_this->p_vlc );
664
665     return p_handler;
666 }
667
668 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
669                        vlc_bool_t b_total )
670 {
671     mtime_t last, total;
672     int i_total;
673     if( !p_counter || p_counter->i_samples != 2 )
674     {
675         msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
676         return;
677     }
678     i_total = p_counter->pp_samples[1]->value.i_int;
679     total = p_counter->pp_samples[1]->date;
680     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
681     {
682         last = mdate() - p_counter->pp_samples[0]->date;
683         i_total += 1;
684         total += last;
685     }
686     else
687     {
688         last = p_counter->pp_samples[0]->date;
689     }
690     if( b_total )
691     {
692         msg_Dbg( p_obj,
693              "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
694              p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
695              (float)(total)/(1000*(float)i_total ) );
696     }
697     else
698     {
699         msg_Dbg( p_obj,
700              "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
701              p_counter->psz_name, (float)total/1000, i_total,
702              (float)(total)/(1000*(float)i_total ) );
703     }
704 }