]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Gather some sout stats (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         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                              char *psz_name )
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,
260                             psz_name );
261     vlc_mutex_unlock( &p_handler->object_lock );
262     vlc_object_release( p_handler );
263
264     return p_counter;
265 }
266
267
268 void stats_ComputeInputStats( input_thread_t *p_input,
269                               input_stats_t *p_stats )
270 {
271     vlc_object_t *p_obj;
272     vlc_list_t *p_list;
273     int i_index;
274     vlc_mutex_lock( &p_stats->lock );
275
276     /* Input */
277     stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
278                        &p_stats->i_read_packets );
279     stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
280                        &p_stats->i_read_bytes );
281     stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
282                        &p_stats->f_input_bitrate );
283
284     stats_GetInteger( p_input, p_input->i_object_id, "demux_read",
285                       &p_stats->i_demux_read_bytes );
286     stats_GetFloat( p_input, p_input->i_object_id, "demux_bitrate",
287                       &p_stats->f_demux_bitrate );
288
289     stats_GetInteger( p_input, p_input->i_object_id, "decoded_video",
290                       &p_stats->i_decoded_video );
291     stats_GetInteger( p_input, p_input->i_object_id, "decoded_audio",
292                       &p_stats->i_decoded_audio );
293
294     /* Sout */
295     stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_packets",
296                       &p_stats->i_sent_packets );
297     stats_GetInteger( p_input, p_input->i_object_id, "sout_sent_bytes",
298                       &p_stats->i_sent_bytes );
299     stats_GetFloat  ( p_input, p_input->i_object_id, "sout_send_bitrate",
300                       &p_stats->f_send_bitrate );
301
302     /* Aout - We store in p_input because aout is shared */
303     stats_GetInteger( p_input, p_input->i_object_id, "played_abuffers",
304                       &p_stats->i_played_abuffers );
305     stats_GetInteger( p_input, p_input->i_object_id, "lost_abuffers",
306                       &p_stats->i_lost_abuffers );
307
308     /* Vouts - FIXME: Store all in input */
309     p_list = vlc_list_find( p_input, VLC_OBJECT_VOUT, FIND_CHILD );
310     if( p_list )
311     {
312         p_stats->i_displayed_pictures  = 0 ;
313         p_stats->i_lost_pictures = 0;
314         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
315         {
316             int i_displayed = 0, i_lost = 0;
317             p_obj = (vlc_object_t *)p_list->p_values[i_index].p_object;
318             stats_GetInteger( p_obj, p_obj->i_object_id, "displayed_pictures",
319                               &i_displayed );
320             stats_GetInteger( p_obj, p_obj->i_object_id, "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 void stats_ReinitInputStats( input_stats_t *p_stats )
332 {
333     p_stats->i_read_packets = p_stats->i_read_bytes =
334     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
335     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
336     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
337     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
338     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
339     p_stats->i_decoded_video = p_stats->i_decoded_audio =
340     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
341      = 0;
342 }
343
344 void stats_DumpInputStats( input_stats_t *p_stats  )
345 {
346     vlc_mutex_lock( &p_stats->lock );
347     /* f_bitrate is in bytes / microsecond
348      * *1000 => bytes / millisecond => kbytes / seconds */
349     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - Demux : %i (%i bytes) - %f kB/s\n"
350                      " - Vout : %i/%i - Aout : %i/%i - Vout : %f\n",
351                     p_stats->i_read_packets, p_stats->i_read_bytes,
352                     p_stats->f_input_bitrate * 1000,
353                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
354                     p_stats->f_demux_bitrate * 1000,
355                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
356                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
357                     p_stats->f_send_bitrate );
358     vlc_mutex_unlock( &p_stats->lock );
359 }
360
361
362 /********************************************************************
363  * Following functions are local
364  ********************************************************************/
365
366 /**
367  * Update a statistics counter, according to its type
368  * If needed, perform a bit of computation (derivative, mostly)
369  * This function must be entered with stats handler lock
370  * \param p_handler stats handler singleton
371  * \param p_counter the counter to update
372  * \param val the "new" value
373  * \return an error code
374  */
375 static int stats_CounterUpdate( stats_handler_t *p_handler,
376                                 counter_t *p_counter,
377                                 vlc_value_t val )
378 {
379     switch( p_counter->i_compute_type )
380     {
381     case STATS_LAST:
382     case STATS_MIN:
383     case STATS_MAX:
384         if( p_counter->i_samples > 1)
385         {
386             msg_Err( p_handler, "LAST counter has several samples !" );
387             return VLC_EGENERIC;
388         }
389         if( p_counter->i_type != VLC_VAR_FLOAT &&
390             p_counter->i_type != VLC_VAR_INTEGER &&
391             p_counter->i_compute_type != STATS_LAST )
392         {
393             msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
394             return VLC_EGENERIC;
395         }
396
397         if( p_counter->i_samples == 0 )
398         {
399             counter_sample_t *p_new = (counter_sample_t*)malloc(
400                                                sizeof( counter_sample_t ) );
401             p_new->value.psz_string = NULL;
402
403             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
404                          p_counter->i_samples, p_new );
405         }
406         if( p_counter->i_samples == 1 )
407         {
408             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
409             if( p_counter->i_compute_type == STATS_LAST ||
410                 ( p_counter->i_compute_type == STATS_MAX &&
411                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
412                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
413                      ( p_counter->i_type == VLC_VAR_FLOAT &&
414                        p_counter->pp_samples[0]->value.f_float > val.f_float )
415                    ) ) ||
416                 ( p_counter->i_compute_type == STATS_MIN &&
417                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
418                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
419                      ( p_counter->i_type == VLC_VAR_FLOAT &&
420                        p_counter->pp_samples[0]->value.f_float < val.f_float )
421                    ) ) )
422             {
423                 if( p_counter->i_type == VLC_VAR_STRING &&
424                     p_counter->pp_samples[0]->value.psz_string )
425                 {
426                     free( p_counter->pp_samples[0]->value.psz_string );
427                 }
428                 p_counter->pp_samples[0]->value = val;
429             }
430         }
431         break;
432     case STATS_DERIVATIVE:
433     {
434         counter_sample_t *p_new, *p_old;
435         if( mdate() - p_counter->last_update < p_counter->update_interval )
436         {
437             return VLC_EGENERIC;
438         }
439         p_counter->last_update = mdate();
440         if( p_counter->i_type != VLC_VAR_FLOAT &&
441             p_counter->i_type != VLC_VAR_INTEGER )
442         {
443             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
444             return VLC_EGENERIC;
445         }
446         /* Insert the new one at the beginning */
447         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
448         p_new->value = val;
449         p_new->date = p_counter->last_update;
450         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
451                      0, p_new );
452
453         if( p_counter->i_samples == 3 )
454         {
455             p_old = p_counter->pp_samples[2];
456             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
457             free( p_old );
458         }
459         break;
460     }
461     case STATS_COUNTER:
462         if( p_counter->i_samples > 1)
463         {
464             msg_Err( p_handler, "LAST counter has several samples !" );
465             return VLC_EGENERIC;
466         }
467         if( p_counter->i_samples == 0 )
468         {
469             counter_sample_t *p_new = (counter_sample_t*)malloc(
470                                                sizeof( counter_sample_t ) );
471             p_new->value.psz_string = NULL;
472
473             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
474                          p_counter->i_samples, p_new );
475         }
476         if( p_counter->i_samples == 1 )
477         {
478             switch( p_counter->i_type )
479             {
480             case VLC_VAR_INTEGER:
481             case VLC_VAR_FLOAT:
482                 p_counter->pp_samples[0]->value.i_int += val.i_int;
483                 break;
484             default:
485                 msg_Err( p_handler, "Trying to increment invalid variable %s",
486                          p_counter->psz_name );
487                 return VLC_EGENERIC;
488             }
489         }
490         break;
491     }
492     return VLC_SUCCESS;
493 }
494
495 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
496                              char *psz_name )
497 {
498     int i;
499    for( i = 0; i< p_handler->i_counters; i++ )
500     {
501         counter_t *p_counter = p_handler->pp_counters[i];
502         if( p_counter->i_source_object == i_object_id &&
503             !strcmp( p_counter->psz_name, psz_name ) )
504         {
505             return p_counter;
506         }
507     }
508     return NULL;
509 }
510
511
512 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
513 {
514     stats_handler_t *p_handler = (stats_handler_t*)
515                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
516                                            FIND_ANYWHERE );
517     if( !p_handler )
518     {
519         p_handler = stats_HandlerCreate( p_this );
520         if( !p_handler )
521         {
522             return NULL;
523         }
524         vlc_object_yield( p_handler );
525     }
526     return p_handler;
527 }
528
529 /**
530  * Initialize statistics handler
531  *
532  * This function initializes the global statistics handler singleton,
533  * \param p_this the parent VLC object
534  */
535 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
536 {
537     stats_handler_t *p_handler;
538
539     msg_Dbg( p_this, "creating statistics handler" );
540
541     p_handler = (stats_handler_t*) vlc_object_create( p_this,
542                                                       VLC_OBJECT_STATS );
543
544     if( !p_handler )
545     {
546         msg_Err( p_this, "out of memory" );
547         return NULL;
548     }
549     p_handler->i_counters = 0;
550     p_handler->pp_counters = NULL;
551
552     /// \bug is it p_vlc or p_libvlc ?
553     vlc_object_attach( p_handler, p_this->p_vlc );
554
555     return p_handler;
556 }