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