]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Audio - 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                             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, 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, char *psz_name, vlc_value_t val )
127 {
128     int i_ret;
129     counter_t *p_counter;
130
131     /* Get stats handler singleton */
132     stats_handler_t *p_handler;
133     if( p_this->p_libvlc->b_stats == VLC_FALSE )
134     {
135         return VLC_EGENERIC;
136     }
137     p_handler = stats_HandlerGet( p_this );
138     if( !p_handler ) return VLC_ENOMEM;
139
140     vlc_mutex_lock( &p_handler->object_lock );
141     /* Look for existing element */
142     p_counter = GetCounter( p_handler, p_this->i_object_id,
143                             psz_name );
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 );
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, char *psz_name, vlc_value_t *val )
166 {
167     counter_t *p_counter;
168
169     /* Get stats handler singleton */
170     stats_handler_t *p_handler;
171     if( p_this->p_libvlc->b_stats == VLC_FALSE )
172     {
173         return VLC_EGENERIC;
174     }
175     p_handler = stats_HandlerGet( p_this );
176     if( !p_handler ) return VLC_ENOMEM;
177     vlc_mutex_lock( &p_handler->object_lock );
178
179     /* Look for existing element */
180     p_counter = GetCounter( p_handler, i_object_id,
181                             psz_name );
182     if( !p_counter )
183     {
184         vlc_mutex_unlock( &p_handler->object_lock );
185         vlc_object_release( p_handler );
186         return VLC_ENOOBJ;
187     }
188
189     if( p_counter->i_samples == 0 )
190     {
191         vlc_mutex_unlock( &p_handler->object_lock );
192         val->i_int = val->f_float = 0.0;
193         return VLC_EGENERIC;
194     }
195
196     switch( p_counter->i_compute_type )
197     {
198     case STATS_LAST:
199     case STATS_MIN:
200     case STATS_MAX:
201     case STATS_COUNTER:
202         *val = p_counter->pp_samples[0]->value;
203         break;
204     case STATS_DERIVATIVE:
205         /* Not ready yet */
206         if( p_counter->i_samples < 2 )
207         {
208             vlc_mutex_unlock( &p_handler->object_lock );
209             val->i_int = 0; val->f_float = 0.0;
210             return VLC_EGENERIC;
211         }
212         if( p_counter->i_type == VLC_VAR_INTEGER )
213         {
214             float f = ( p_counter->pp_samples[0]->value.i_int -
215                         p_counter->pp_samples[1]->value.i_int ) /
216                     (float)(  p_counter->pp_samples[0]->date -
217                               p_counter->pp_samples[1]->date );
218             val->i_int = (int)f;
219         }
220         else
221         {
222             float f = (float)( p_counter->pp_samples[0]->value.f_float -
223                                p_counter->pp_samples[1]->value.f_float ) /
224                       (float)( p_counter->pp_samples[0]->date -
225                                p_counter->pp_samples[1]->date );
226             val->f_float = f;
227         }
228         break;
229     }
230     vlc_object_release( p_handler );
231
232     vlc_mutex_unlock( &p_handler->object_lock );
233     return VLC_SUCCESS;;
234 }
235
236 /** Get a statistics counter structure. This allows for low-level modifications
237  * \param p_this a parent object
238  * \param i_object_id the object from which to retrieve data
239  * \param psz_name the name
240  * \return the counter, or NULL if not found (or handler not created yet)
241  */
242 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
243                              char *psz_name )
244 {
245     counter_t *p_counter;
246
247     stats_handler_t *p_handler;
248     if( p_this->p_libvlc->b_stats == VLC_FALSE )
249     {
250         return NULL;
251     }
252     p_handler = stats_HandlerGet( p_this );
253     if( !p_handler ) return NULL;
254
255     vlc_mutex_lock( &p_handler->object_lock );
256
257     /* Look for existing element */
258     p_counter = GetCounter( p_handler, p_this->i_object_id,
259                             psz_name );
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, "read_packets",
277                        &p_stats->i_read_packets );
278     stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
279                        &p_stats->i_read_bytes );
280     stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
281                        &p_stats->f_input_bitrate );
282
283     stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
284                       &p_stats->i_demux_read_bytes );
285     stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
286                       &p_stats->f_demux_bitrate );
287
288     stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
289                       &p_stats->i_decoded_video );
290     stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
291                       &p_stats->i_decoded_audio );
292
293     /* Aout - We store in p_input because aout is shared */
294     stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
295                       &p_stats->i_played_abuffers );
296     stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
297                       &p_stats->i_lost_abuffers );
298
299     /* Vouts */
300     p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
301     if( p_list )
302     {
303         p_stats->i_displayed_pictures  = 0 ;
304         p_stats->i_lost_pictures = 0;
305         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
306         {
307             int i_displayed = 0, i_lost = 0;
308             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
309             stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
310                               &i_displayed );
311             stats_GetInteger( p_obj, p_obj->i_object_id, "lost_pictures",
312                               &i_lost );
313             p_stats->i_displayed_pictures += i_displayed;
314             p_stats->i_lost_pictures += i_lost;
315          }
316         vlc_list_release( p_list );
317     }
318
319     vlc_mutex_unlock( &p_stats->lock );
320 }
321
322 void stats_ReinitInputStats( input_stats_t *p_stats )
323 {
324     p_stats->i_read_packets = p_stats->i_read_bytes =
325     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
326     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
327     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
328     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
329     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
330     p_stats->i_decoded_video = p_stats->i_decoded_audio = 0;
331 }
332
333 void stats_DumpInputStats( input_stats_t *p_stats  )
334 {
335     vlc_mutex_lock( &p_stats->lock );
336     /* f_bitrate is in bytes / microsecond
337      * *1000 => bytes / millisecond => kbytes / seconds */
338     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
339                      " - Vout : %i/%i - Aout : %i/%i\n",
340                     p_stats->i_read_packets, p_stats->i_read_bytes,
341                     p_stats->f_input_bitrate * 1000,
342                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
343                     p_stats->f_demux_bitrate * 1000,
344                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
345                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers );
346     vlc_mutex_unlock( &p_stats->lock );
347 }
348
349
350 /********************************************************************
351  * Following functions are local
352  ********************************************************************/
353
354 /**
355  * Update a statistics counter, according to its type
356  * If needed, perform a bit of computation (derivative, mostly)
357  * This function must be entered with stats handler lock
358  * \param p_handler stats handler singleton
359  * \param p_counter the counter to update
360  * \param val the "new" value
361  * \return an error code
362  */
363 static int stats_CounterUpdate( stats_handler_t *p_handler,
364                                 counter_t *p_counter,
365                                 vlc_value_t val )
366 {
367     switch( p_counter->i_compute_type )
368     {
369     case STATS_LAST:
370     case STATS_MIN:
371     case STATS_MAX:
372         if( p_counter->i_samples > 1)
373         {
374             msg_Err( p_handler, "LAST counter has several samples !" );
375             return VLC_EGENERIC;
376         }
377         if( p_counter->i_type != VLC_VAR_FLOAT &&
378             p_counter->i_type != VLC_VAR_INTEGER &&
379             p_counter->i_compute_type != STATS_LAST )
380         {
381             msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
382             return VLC_EGENERIC;
383         }
384
385         if( p_counter->i_samples == 0 )
386         {
387             counter_sample_t *p_new = (counter_sample_t*)malloc(
388                                                sizeof( counter_sample_t ) );
389             p_new->value.psz_string = NULL;
390
391             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
392                          p_counter->i_samples, p_new );
393         }
394         if( p_counter->i_samples == 1 )
395         {
396             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
397             if( p_counter->i_compute_type == STATS_LAST ||
398                 ( p_counter->i_compute_type == STATS_MAX &&
399                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
400                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
401                      ( p_counter->i_type == VLC_VAR_FLOAT &&
402                        p_counter->pp_samples[0]->value.f_float > val.f_float )
403                    ) ) ||
404                 ( p_counter->i_compute_type == STATS_MIN &&
405                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
406                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
407                      ( p_counter->i_type == VLC_VAR_FLOAT &&
408                        p_counter->pp_samples[0]->value.f_float < val.f_float )
409                    ) ) )
410             {
411                 if( p_counter->i_type == VLC_VAR_STRING &&
412                     p_counter->pp_samples[0]->value.psz_string )
413                 {
414                     free( p_counter->pp_samples[0]->value.psz_string );
415                 }
416                 p_counter->pp_samples[0]->value = val;
417             }
418         }
419         break;
420     case STATS_DERIVATIVE:
421     {
422         counter_sample_t *p_new, *p_old;
423         if( mdate() - p_counter->last_update < p_counter->update_interval )
424         {
425             return VLC_EGENERIC;
426         }
427         p_counter->last_update = mdate();
428         if( p_counter->i_type != VLC_VAR_FLOAT &&
429             p_counter->i_type != VLC_VAR_INTEGER )
430         {
431             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
432             return VLC_EGENERIC;
433         }
434         /* Insert the new one at the beginning */
435         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
436         p_new->value = val;
437         p_new->date = p_counter->last_update;
438         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
439                      0, p_new );
440
441         if( p_counter->i_samples == 3 )
442         {
443             p_old = p_counter->pp_samples[2];
444             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
445             free( p_old );
446         }
447         break;
448     }
449     case STATS_COUNTER:
450         if( p_counter->i_samples > 1)
451         {
452             msg_Err( p_handler, "LAST counter has several samples !" );
453             return VLC_EGENERIC;
454         }
455         if( p_counter->i_samples == 0 )
456         {
457             counter_sample_t *p_new = (counter_sample_t*)malloc(
458                                                sizeof( counter_sample_t ) );
459             p_new->value.psz_string = NULL;
460
461             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
462                          p_counter->i_samples, p_new );
463         }
464         if( p_counter->i_samples == 1 )
465         {
466             switch( p_counter->i_type )
467             {
468             case VLC_VAR_INTEGER:
469             case VLC_VAR_FLOAT:
470                 p_counter->pp_samples[0]->value.i_int += val.i_int;
471                 break;
472             default:
473                 msg_Err( p_handler, "Trying to increment invalid variable %s",
474                          p_counter->psz_name );
475                 return VLC_EGENERIC;
476             }
477         }
478         break;
479     }
480     return VLC_SUCCESS;
481 }
482
483 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
484                              char *psz_name )
485 {
486     int i;
487    for( i = 0; i< p_handler->i_counters; i++ )
488     {
489         counter_t *p_counter = p_handler->pp_counters[i];
490         if( p_counter->i_source_object == i_object_id &&
491             !strcmp( p_counter->psz_name, psz_name ) )
492         {
493             return p_counter;
494         }
495     }
496     return NULL;
497 }
498
499
500 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
501 {
502     stats_handler_t *p_handler = (stats_handler_t*)
503                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
504                                            FIND_ANYWHERE );
505     if( !p_handler )
506     {
507         p_handler = stats_HandlerCreate( p_this );
508         if( !p_handler )
509         {
510             return NULL;
511         }
512         vlc_object_yield( p_handler );
513     }
514     return p_handler;
515 }
516
517 /**
518  * Initialize statistics handler
519  *
520  * This function initializes the global statistics handler singleton,
521  * \param p_this the parent VLC object
522  */
523 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
524 {
525     stats_handler_t *p_handler;
526
527     msg_Dbg( p_this, "creating statistics handler" );
528
529     p_handler = (stats_handler_t*) vlc_object_create( p_this,
530                                                       VLC_OBJECT_STATS );
531
532     if( !p_handler )
533     {
534         msg_Err( p_this, "out of memory" );
535         return NULL;
536     }
537     p_handler->i_counters = 0;
538     p_handler->pp_counters = NULL;
539
540     /// \bug is it p_vlc or p_libvlc ?
541     vlc_object_attach( p_handler, p_this->p_vlc );
542
543     return p_handler;
544 }