]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Add a timing facility (Refs:#473)
[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 /*****************************************************************************
44  * Exported functions
45  *****************************************************************************/
46
47 /**
48  * Cleanup statistics handler stuff
49  * \param p_stats the handler to clean
50  * \return nothing
51  */
52 void stats_HandlerDestroy( stats_handler_t *p_stats )
53 {
54     int i;
55     for ( i =  p_stats->i_counters - 1 ; i >= 0 ; i-- )
56     {
57         int j;
58         counter_t * p_counter = p_stats->pp_counters[i];
59
60         for( j = p_counter->i_samples -1; j >= 0 ; j-- )
61         {
62             counter_sample_t *p_sample = p_counter->pp_samples[j];
63             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, j );
64             free( p_sample );
65         }
66         free( p_counter->psz_name );
67         REMOVE_ELEM( p_stats->pp_counters, p_stats->i_counters, i );
68         free( p_counter );
69     }
70 }
71
72 /**
73  * Create a statistics counter
74  * \param p_this the object for which to create the counter
75  * \param psz_name the name
76  * \param i_type the type of stored data. One of VLC_VAR_STRING,
77  * VLC_VAR_INTEGER, VLC_VAR_FLOAT
78  * \param i_compute_type the aggregation type. One of STATS_LAST (always
79  * keep the last value), STATS_COUNTER (increment by the passed value),
80  * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
81  * (keep a time derivative of the value)
82  */
83 int __stats_Create( vlc_object_t *p_this, const char *psz_name, int i_type,
84                     int i_compute_type )
85 {
86     counter_t *p_counter;
87     stats_handler_t *p_handler;
88
89     if( p_this->p_libvlc->b_stats == VLC_FALSE )
90     {
91         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_source_object = p_this->i_object_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,
111                  p_handler->i_counters,
112                  p_handler->i_counters,
113                  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, const char *psz_name,
127                     vlc_value_t val )
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,
144                             psz_name );
145     if( !p_counter )
146     {
147         vlc_mutex_unlock( &p_handler->object_lock );
148         vlc_object_release( p_handler );
149         return VLC_ENOOBJ;
150     }
151
152     i_ret = stats_CounterUpdate( p_handler, p_counter, val );
153     vlc_mutex_unlock( &p_handler->object_lock );
154
155     return i_ret;
156 }
157
158 /** Get the aggregated value for a counter
159  * \param p_this an object
160  * \param i_object_id the object id from which we want the data
161  * \param psz_name the name of the couner
162  * \param val a pointer to an initialized vlc_value union. It will contain the
163  * retrieved value
164  * \return an error code
165  */
166 int __stats_Get( vlc_object_t *p_this, int i_object_id,
167                  const char *psz_name, vlc_value_t *val )
168 {
169     counter_t *p_counter;
170
171     /* Get stats handler singleton */
172     stats_handler_t *p_handler;
173     if( p_this->p_libvlc->b_stats == VLC_FALSE )
174     {
175         return VLC_EGENERIC;
176     }
177     p_handler = stats_HandlerGet( p_this );
178     if( !p_handler ) return VLC_ENOMEM;
179     vlc_mutex_lock( &p_handler->object_lock );
180
181     /* Look for existing element */
182     p_counter = GetCounter( p_handler, i_object_id,
183                             psz_name );
184     if( !p_counter )
185     {
186         vlc_mutex_unlock( &p_handler->object_lock );
187         vlc_object_release( p_handler );
188         val->i_int = val->f_float = 0.0;
189         return VLC_ENOOBJ;
190     }
191
192     if( p_counter->i_samples == 0 )
193     {
194         vlc_mutex_unlock( &p_handler->object_lock );
195         val->i_int = val->f_float = 0.0;
196         return VLC_EGENERIC;
197     }
198
199     switch( p_counter->i_compute_type )
200     {
201     case STATS_LAST:
202     case STATS_MIN:
203     case STATS_MAX:
204     case STATS_COUNTER:
205         *val = p_counter->pp_samples[0]->value;
206         break;
207     case STATS_DERIVATIVE:
208         /* Not ready yet */
209         if( p_counter->i_samples < 2 )
210         {
211             vlc_mutex_unlock( &p_handler->object_lock );
212             val->i_int = 0; val->f_float = 0.0;
213             return VLC_EGENERIC;
214         }
215         if( p_counter->i_type == VLC_VAR_INTEGER )
216         {
217             float f = ( p_counter->pp_samples[0]->value.i_int -
218                         p_counter->pp_samples[1]->value.i_int ) /
219                     (float)(  p_counter->pp_samples[0]->date -
220                               p_counter->pp_samples[1]->date );
221             val->i_int = (int)f;
222         }
223         else
224         {
225             float f = (float)( p_counter->pp_samples[0]->value.f_float -
226                                p_counter->pp_samples[1]->value.f_float ) /
227                       (float)( p_counter->pp_samples[0]->date -
228                                p_counter->pp_samples[1]->date );
229             val->f_float = f;
230         }
231         break;
232     }
233     vlc_object_release( p_handler );
234
235     vlc_mutex_unlock( &p_handler->object_lock );
236     return VLC_SUCCESS;;
237 }
238
239 /** Get a statistics counter structure. This allows for low-level modifications
240  * \param p_this a parent object
241  * \param i_object_id the object from which to retrieve data
242  * \param psz_name the name
243  * \return the counter, or NULL if not found (or handler not created yet)
244  */
245 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
246                                const char *psz_name )
247 {
248     counter_t *p_counter;
249
250     stats_handler_t *p_handler;
251     if( p_this->p_libvlc->b_stats == VLC_FALSE )
252     {
253         return NULL;
254     }
255     p_handler = stats_HandlerGet( p_this );
256     if( !p_handler ) return NULL;
257
258     vlc_mutex_lock( &p_handler->object_lock );
259
260     /* Look for existing element */
261     p_counter = GetCounter( p_handler, i_object_id,
262                             psz_name );
263     vlc_mutex_unlock( &p_handler->object_lock );
264     vlc_object_release( p_handler );
265
266     return p_counter;
267 }
268
269
270 void stats_ComputeInputStats( input_thread_t *p_input,
271                               input_stats_t *p_stats )
272 {
273     vlc_object_t *p_obj;
274     vlc_list_t *p_list;
275     int i_index;
276     vlc_mutex_lock( &p_stats->lock );
277
278     /* Input */
279     stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
280                        &p_stats->i_read_packets );
281     stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
282                        &p_stats->i_read_bytes );
283     stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
284                        &p_stats->f_input_bitrate );
285
286     stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
287                       &p_stats->i_demux_read_bytes );
288     stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
289                       &p_stats->f_demux_bitrate );
290
291     stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
292                       &p_stats->i_decoded_video );
293     stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
294                       &p_stats->i_decoded_audio );
295
296     /* Sout */
297     stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_packets",
298                       &p_stats->i_sent_packets );
299     stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_bytes",
300                       &p_stats->i_sent_bytes );
301     stats_GetFloat  ( p_input, p_input->i_object_id, "sout_send_bitrate",
302                       &p_stats->f_send_bitrate );
303
304     /* Aout - We store in p_input because aout is shared */
305     stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
306                       &p_stats->i_played_abuffers );
307     stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
308                       &p_stats->i_lost_abuffers );
309
310     /* Vouts - FIXME: Store all in input */
311     p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
312     if( p_list )
313     {
314         p_stats->i_displayed_pictures  = 0 ;
315         p_stats->i_lost_pictures = 0;
316         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
317         {
318             int i_displayed = 0, i_lost = 0;
319             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
320             stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
321                               &i_displayed );
322             stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
323                               &i_lost );
324             p_stats->i_displayed_pictures += i_displayed;
325             p_stats->i_lost_pictures += i_lost;
326          }
327         vlc_list_release( p_list );
328     }
329
330     vlc_mutex_unlock( &p_stats->lock );
331 }
332
333
334 void stats_ReinitInputStats( input_stats_t *p_stats )
335 {
336     p_stats->i_read_packets = p_stats->i_read_bytes =
337     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
338     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
339     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
340     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
341     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
342     p_stats->i_decoded_video = p_stats->i_decoded_audio =
343     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
344      = 0;
345 }
346
347 void stats_DumpInputStats( input_stats_t *p_stats  )
348 {
349     vlc_mutex_lock( &p_stats->lock );
350     /* f_bitrate is in bytes / microsecond
351      * *1000 => bytes / millisecond => kbytes / seconds */
352     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
353                      " - Vout : %i/%i - Aout : %i/%i - Vout : %f\n",
354                     p_stats->i_read_packets, p_stats->i_read_bytes,
355                     p_stats->f_input_bitrate * 1000,
356                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
357                     p_stats->f_demux_bitrate * 1000,
358                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
359                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
360                     p_stats->f_send_bitrate );
361     vlc_mutex_unlock( &p_stats->lock );
362 }
363
364 void __stats_ComputeGlobalStats( vlc_object_t *p_obj,
365                                 global_stats_t *p_stats )
366 {
367     vlc_list_t *p_list;
368     int i_index;
369     vlc_mutex_lock( &p_stats->lock );
370
371     p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_CHILD );
372     if( p_list )
373     {
374         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
375         {
376             float f_in = 0, f_out = 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, "input_bitrate",
379                             &f_in );
380             stats_GetFloat( p_obj, p_obj->i_object_id, "sout_send_bitrate",
381                             &f_out );
382             p_stats->f_input_bitrate += f_in;
383             p_stats->f_output_bitrate += f_out;
384         }
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 {
399     counter_t *p_counter = stats_CounterGet( p_obj,
400                                              p_obj->p_vlc->i_object_id,
401                                              psz_name );
402     if( !p_counter )
403     {
404         counter_sample_t *p_sample;
405         stats_Create( p_obj->p_vlc, psz_name, VLC_VAR_TIME, STATS_TIMER );
406         p_counter = stats_CounterGet( p_obj,  p_obj->p_vlc->i_object_id,
407                                       psz_name );
408         if( !p_counter ) return;
409         /* 1st sample : if started: start_date, else last_time, b_started */
410         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
411         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
412                      p_counter->i_samples, p_sample );
413         p_sample->date = 0; p_sample->value.b_bool = 0;
414         /* 2nd sample : global_time, i_samples */
415         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
416         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
417                      p_counter->i_samples, p_sample );
418         p_sample->date = 0; p_sample->value.i_int = 0;
419     }
420     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
421     {
422         msg_Warn( p_obj, "timer %s was already started !", psz_name );
423         return;
424     }
425     p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
426     p_counter->pp_samples[0]->date = mdate();
427 }
428
429 void __stats_TimerStop( vlc_object_t *p_obj, const char *psz_name )
430 {
431     counter_t *p_counter = stats_CounterGet( p_obj,
432                                               p_obj->p_vlc->i_object_id,
433                                              psz_name );
434     if( !p_counter || p_counter->i_samples != 2 )
435     {
436         msg_Err( p_obj, "timer %s does not exist", psz_name );
437         return;
438     }
439     p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
440     p_counter->pp_samples[1]->value.i_int += 1;
441     p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
442     p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
443 }
444
445 void __stats_TimerDump( vlc_object_t *p_obj, const char *psz_name )
446 {
447     mtime_t last, total;
448     int i_total;
449     counter_t *p_counter = stats_CounterGet( p_obj,
450                                              p_obj->p_vlc->i_object_id,
451                                              psz_name );
452     if( !p_counter || p_counter->i_samples != 2 )
453     {
454         msg_Err( p_obj, "timer %s does not exist", psz_name );
455         return;
456     }
457     i_total = p_counter->pp_samples[1]->value.i_int;
458     total = p_counter->pp_samples[1]->date;
459     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
460     {
461         last = mdate() - p_counter->pp_samples[0]->date;
462         i_total += 1;
463         total += last;
464     }
465     else
466     {
467         last = p_counter->pp_samples[0]->date;
468     }
469     msg_Dbg( p_obj, "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
470              psz_name, (float)last/1000, (float)total/1000, i_total,
471              (float)(total)/(1000*(float)i_total ) );
472 }
473
474 void __stats_TimersDumpAll( vlc_object_t *p_obj )
475 {
476
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    for( i = 0; i< p_handler->i_counters; i++ )
618     {
619         counter_t *p_counter = p_handler->pp_counters[i];
620         if( p_counter->i_source_object == i_object_id &&
621             !strcmp( p_counter->psz_name, psz_name ) )
622         {
623             return p_counter;
624         }
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 = (stats_handler_t*)
633                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
634                                            FIND_ANYWHERE );
635     if( !p_handler )
636     {
637         p_handler = stats_HandlerCreate( p_this );
638         if( !p_handler )
639         {
640             return NULL;
641         }
642         vlc_object_yield( p_handler );
643     }
644     return p_handler;
645 }
646
647 /**
648  * Initialize statistics handler
649  *
650  * This function initializes the global statistics handler singleton,
651  * \param p_this the parent VLC object
652  */
653 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
654 {
655     stats_handler_t *p_handler;
656
657     msg_Dbg( p_this, "creating statistics handler" );
658
659     p_handler = (stats_handler_t*) vlc_object_create( p_this,
660                                                       VLC_OBJECT_STATS );
661
662     if( !p_handler )
663     {
664         msg_Err( p_this, "out of memory" );
665         return NULL;
666     }
667     p_handler->i_counters = 0;
668     p_handler->pp_counters = NULL;
669
670     /// \bug is it p_vlc or p_libvlc ?
671     vlc_object_attach( p_handler, p_this->p_vlc );
672
673     return p_handler;
674 }