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