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