]> git.sesse.net Git - vlc/blob - src/misc/stats.c
e6a273573e84a98361c3cbb743060e651c34eb71
[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 int CounterUpdate( vlc_object_t *p_this,
36                           counter_t *p_counter,
37                           vlc_value_t val, vlc_value_t * );
38 static void TimerDump( vlc_object_t *p_this, counter_t *p_counter, vlc_bool_t);
39
40 /*****************************************************************************
41  * Exported functions
42  *****************************************************************************/
43
44 /**
45  * Create a statistics counter
46  * \param p_this a VLC object
47  * \param i_type the type of stored data. One of VLC_VAR_STRING,
48  * VLC_VAR_INTEGER, VLC_VAR_FLOAT
49  * \param i_compute_type the aggregation type. One of STATS_LAST (always
50  * keep the last value), STATS_COUNTER (increment by the passed value),
51  * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
52  * (keep a time derivative of the value)
53  */
54 counter_t * __stats_CounterCreate( vlc_object_t *p_this,
55                                    int i_type, int i_compute_type )
56 {
57     counter_t *p_counter;
58     if( !p_this->p_libvlc->b_stats ) return NULL;
59
60     p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
61
62     p_counter->i_compute_type = i_compute_type;
63     p_counter->i_type = i_type;
64     p_counter->i_samples = 0;
65     p_counter->pp_samples = NULL;
66     p_counter->psz_name = NULL;
67
68     p_counter->update_interval = 0;
69     p_counter->last_update = 0;
70
71     return p_counter;
72 }
73
74 /** Update a counter element with new values
75  * \param p_this a VLC object
76  * \param p_counter the counter to update
77  * \param val the vlc_value union containing the new value to aggregate. For
78  * more information on how data is aggregated, \see __stats_Create
79  * \param val_new a pointer that will be filled with new data
80  */
81 int __stats_Update( vlc_object_t *p_this, counter_t *p_counter,
82                     vlc_value_t val, vlc_value_t *val_new )
83 {
84     if( !p_this->p_libvlc->b_stats || !p_counter ) return VLC_EGENERIC;
85     return CounterUpdate( p_this, p_counter, val, val_new );
86 }
87
88 /** Get the aggregated value for a counter
89  * \param p_this an object
90  * \param p_counter the counter
91  * \param val a pointer to an initialized vlc_value union. It will contain the
92  * retrieved value
93  * \return an error code
94  */
95 int __stats_Get( vlc_object_t *p_this, counter_t *p_counter, vlc_value_t *val )
96 {
97     if( !p_this->p_libvlc->b_stats || !p_counter || p_counter->i_samples == 0 )
98     {
99         val->i_int = val->f_float = 0.0;
100         return VLC_EGENERIC;
101     }
102
103     switch( p_counter->i_compute_type )
104     {
105     case STATS_LAST:
106     case STATS_MIN:
107     case STATS_MAX:
108     case STATS_COUNTER:
109         *val = p_counter->pp_samples[0]->value;
110         break;
111     case STATS_DERIVATIVE:
112         /* Not ready yet */
113         if( p_counter->i_samples < 2 )
114         {
115             val->i_int = 0; val->f_float = 0.0;
116             return VLC_EGENERIC;
117         }
118         if( p_counter->i_type == VLC_VAR_INTEGER )
119         {
120             float f = ( p_counter->pp_samples[0]->value.i_int -
121                         p_counter->pp_samples[1]->value.i_int ) /
122                     (float)(  p_counter->pp_samples[0]->date -
123                               p_counter->pp_samples[1]->date );
124             val->i_int = (int)f;
125         }
126         else
127         {
128             float f = (float)( p_counter->pp_samples[0]->value.f_float -
129                                p_counter->pp_samples[1]->value.f_float ) /
130                       (float)( p_counter->pp_samples[0]->date -
131                                p_counter->pp_samples[1]->date );
132             val->f_float = f;
133         }
134         break;
135     }
136     return VLC_SUCCESS;;
137 }
138
139 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
140 {
141     if( !p_input->p_libvlc->b_stats ) return;
142
143     vlc_mutex_lock( &p_stats->lock );
144
145     /* Input */
146     stats_GetInteger( p_input, p_input->counters.p_read_packets,
147                       &p_stats->i_read_packets );
148     stats_GetInteger( p_input, p_input->counters.p_read_bytes,
149                       &p_stats->i_read_bytes );
150     stats_GetFloat( p_input, p_input->counters.p_input_bitrate,
151                     &p_stats->f_input_bitrate );
152     stats_GetInteger( p_input, p_input->counters.p_demux_read,
153                       &p_stats->i_demux_read_bytes );
154     stats_GetFloat( p_input, p_input->counters.p_demux_bitrate,
155                     &p_stats->f_demux_bitrate );
156
157     /* Decoders */
158     stats_GetInteger( p_input, p_input->counters.p_decoded_video,
159                       &p_stats->i_decoded_video );
160     stats_GetInteger( p_input, p_input->counters.p_decoded_audio,
161                       &p_stats->i_decoded_audio );
162
163     /* Sout */
164     if( p_input->counters.p_sout_send_bitrate )
165     {
166         stats_GetInteger( p_input, p_input->counters.p_sout_sent_packets,
167                           &p_stats->i_sent_packets );
168         stats_GetInteger( p_input, p_input->counters.p_sout_sent_bytes,
169                           &p_stats->i_sent_bytes );
170         stats_GetFloat  ( p_input, p_input->counters.p_sout_send_bitrate,
171                           &p_stats->f_send_bitrate );
172     }
173
174     /* Aout */
175     stats_GetInteger( p_input, p_input->counters.p_played_abuffers,
176                       &p_stats->i_played_abuffers );
177     stats_GetInteger( p_input, p_input->counters.p_lost_abuffers,
178                       &p_stats->i_lost_abuffers );
179
180     /* Vouts */
181     stats_GetInteger( p_input, p_input->counters.p_displayed_pictures,
182                       &p_stats->i_displayed_pictures );
183     stats_GetInteger( p_input, p_input->counters.p_lost_pictures,
184                       &p_stats->i_lost_pictures );
185
186     vlc_mutex_unlock( &p_stats->lock );
187 }
188
189 void stats_ReinitInputStats( input_stats_t *p_stats )
190 {
191     p_stats->i_read_packets = p_stats->i_read_bytes =
192     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
193     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
194     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
195     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
196     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
197     p_stats->i_decoded_video = p_stats->i_decoded_audio =
198     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
199      = 0;
200 }
201
202 void stats_DumpInputStats( input_stats_t *p_stats  )
203 {
204     vlc_mutex_lock( &p_stats->lock );
205     /* f_bitrate is in bytes / microsecond
206      * *1000 => bytes / millisecond => kbytes / seconds */
207     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
208                      "Demux : %i (%i bytes) - %f kB/s\n"
209                      " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
210                     p_stats->i_read_packets, p_stats->i_read_bytes,
211                     p_stats->f_input_bitrate * 1000,
212                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
213                     p_stats->f_demux_bitrate * 1000,
214                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
215                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
216                     p_stats->f_send_bitrate );
217     vlc_mutex_unlock( &p_stats->lock );
218 }
219
220 void __stats_ComputeGlobalStats( vlc_object_t *p_obj, global_stats_t *p_stats )
221 {
222     vlc_list_t *p_list;
223     int i_index;
224
225     if( !p_obj->p_libvlc->b_stats ) return;
226
227     vlc_mutex_lock( &p_stats->lock );
228
229     p_list = vlc_list_find( p_obj, VLC_OBJECT_INPUT, FIND_ANYWHERE );
230     if( p_list )
231     {
232         float f_total_in = 0, f_total_out = 0,f_total_demux = 0;
233         for( i_index = 0; i_index < p_list->i_count ; i_index ++ )
234         {
235             float f_in = 0, f_out = 0, f_demux = 0;
236             input_thread_t *p_input = (input_thread_t *)
237                              p_list->p_values[i_index].p_object;
238             stats_GetFloat( p_obj, p_input->counters.p_input_bitrate, &f_in );
239             if( p_input->counters.p_sout_send_bitrate )
240                 stats_GetFloat( p_obj, p_input->counters.p_sout_send_bitrate,
241                                     &f_out );
242             stats_GetFloat( p_obj, p_input->counters.p_demux_bitrate,
243                                 &f_demux );
244             f_total_in += f_in; f_total_out += f_out;f_total_demux += f_demux;
245         }
246         p_stats->f_input_bitrate = f_total_in;
247         p_stats->f_output_bitrate = f_total_out;
248         p_stats->f_demux_bitrate = f_total_demux;
249         vlc_list_release( p_list );
250     }
251
252     vlc_mutex_unlock( &p_stats->lock );
253 }
254
255 void stats_ReinitGlobalStats( global_stats_t *p_stats )
256 {
257     p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
258 }
259
260
261 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
262                          unsigned int i_id )
263 {
264     int i;
265     counter_t *p_counter = NULL;
266     if( !p_obj->p_libvlc->b_stats ) return;
267     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
268
269     for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
270     {
271         if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
272         {
273             p_counter = p_obj->p_libvlc->pp_timers[i];
274             break;
275         }
276     }
277     if( !p_counter )
278     {
279         counter_sample_t *p_sample;
280         p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
281                                          STATS_TIMER );
282         if( !p_counter ) return;
283         p_counter->psz_name = strdup( psz_name );
284         p_counter->i_id = i_id;
285         INSERT_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers,
286                      p_obj->p_libvlc->i_timers, p_counter );
287
288         /* 1st sample : if started: start_date, else last_time, b_started */
289         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
290         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
291                      p_counter->i_samples, p_sample );
292         p_sample->date = 0; p_sample->value.b_bool = 0;
293         /* 2nd sample : global_time, i_samples */
294         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
295         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
296                      p_counter->i_samples, p_sample );
297         p_sample->date = 0; p_sample->value.i_int = 0;
298     }
299     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
300     {
301         msg_Warn( p_obj, "timer %s was already started !", psz_name );
302         return;
303     }
304     p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
305     p_counter->pp_samples[0]->date = mdate();
306     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
307 }
308
309 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
310 {
311     counter_t *p_counter = NULL;
312     int i;
313     if( !p_obj->p_libvlc->b_stats ) return;
314     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
315     for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
316     {
317         if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
318         {
319             p_counter = p_obj->p_libvlc->pp_timers[i];
320             break;
321         }
322     }
323     if( !p_counter || p_counter->i_samples != 2 )
324     {
325         msg_Err( p_obj, "timer does not exist" );
326         vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
327         return;
328     }
329     p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
330     p_counter->pp_samples[1]->value.i_int += 1;
331     p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
332     p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
333     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
334 }
335
336 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
337 {
338     counter_t *p_counter = NULL;
339     int i;
340     if( !p_obj->p_libvlc->b_stats ) return;
341     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
342     for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
343     {
344         if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
345         {
346             p_counter = p_obj->p_libvlc->pp_timers[i];
347             break;
348         }
349     }
350     TimerDump( p_obj, p_counter, VLC_TRUE );
351     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
352 }
353
354
355 void __stats_TimersDumpAll( vlc_object_t *p_obj )
356 {
357     int i;
358     if( !p_obj->p_libvlc->b_stats ) return;
359     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
360     for ( i = 0 ; i< p_obj->p_libvlc->i_timers ; i++ )
361         TimerDump( p_obj, p_obj->p_libvlc->pp_timers[i], VLC_FALSE );
362     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
363 }
364
365 void __stats_TimersClean( vlc_object_t *p_obj )
366 {
367     int i;
368     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
369     for ( i = p_obj->p_libvlc->i_timers -1 ; i >= 0; i-- )
370     {
371         counter_t *p_counter = p_obj->p_libvlc->pp_timers[i];
372         REMOVE_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers, i );
373         stats_CounterClean( p_counter );
374     }
375     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
376 }
377
378 void stats_CounterClean( counter_t *p_c )
379 {
380     int i;
381     if( p_c )
382     {
383         i = p_c->i_samples - 1 ;
384         while( i >= 0)
385         {
386             counter_sample_t *p_s = p_c->pp_samples[i];
387             REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
388             free( p_s );
389             i--;
390         }
391     if( p_c->psz_name ) free( p_c->psz_name );
392     free( p_c );
393     }
394 }
395
396
397 /********************************************************************
398  * Following functions are local
399  ********************************************************************/
400
401 /**
402  * Update a statistics counter, according to its type
403  * If needed, perform a bit of computation (derivative, mostly)
404  * This function must be entered with stats handler lock
405  * \param p_counter the counter to update
406  * \param val the "new" value
407  * \return an error code
408  */
409 static int CounterUpdate( vlc_object_t *p_handler,
410                           counter_t *p_counter,
411                           vlc_value_t val, vlc_value_t *new_val )
412 {
413     switch( p_counter->i_compute_type )
414     {
415     case STATS_LAST:
416     case STATS_MIN:
417     case STATS_MAX:
418         if( p_counter->i_samples > 1)
419         {
420             msg_Err( p_handler, "LAST counter has several samples !" );
421             return VLC_EGENERIC;
422         }
423         if( p_counter->i_type != VLC_VAR_FLOAT &&
424             p_counter->i_type != VLC_VAR_INTEGER &&
425             p_counter->i_compute_type != STATS_LAST )
426         {
427             msg_Err( p_handler, "unable to compute MIN or MAX for this type");
428             return VLC_EGENERIC;
429         }
430
431         if( p_counter->i_samples == 0 )
432         {
433             counter_sample_t *p_new = (counter_sample_t*)malloc(
434                                                sizeof( counter_sample_t ) );
435             p_new->value.psz_string = NULL;
436
437             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
438                          p_counter->i_samples, p_new );
439         }
440         if( p_counter->i_samples == 1 )
441         {
442             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
443             if( p_counter->i_compute_type == STATS_LAST ||
444                 ( p_counter->i_compute_type == STATS_MAX &&
445                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
446                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
447                      ( p_counter->i_type == VLC_VAR_FLOAT &&
448                        p_counter->pp_samples[0]->value.f_float > val.f_float )
449                    ) ) ||
450                 ( p_counter->i_compute_type == STATS_MIN &&
451                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
452                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
453                      ( p_counter->i_type == VLC_VAR_FLOAT &&
454                        p_counter->pp_samples[0]->value.f_float < val.f_float )
455                    ) ) )
456             {
457                 if( p_counter->i_type == VLC_VAR_STRING &&
458                     p_counter->pp_samples[0]->value.psz_string )
459                 {
460                     free( p_counter->pp_samples[0]->value.psz_string );
461                 }
462                 p_counter->pp_samples[0]->value = val;
463                 *new_val = p_counter->pp_samples[0]->value;
464             }
465         }
466         break;
467     case STATS_DERIVATIVE:
468     {
469         counter_sample_t *p_new, *p_old;
470         if( mdate() - p_counter->last_update < p_counter->update_interval )
471         {
472             return VLC_EGENERIC;
473         }
474         p_counter->last_update = mdate();
475         if( p_counter->i_type != VLC_VAR_FLOAT &&
476             p_counter->i_type != VLC_VAR_INTEGER )
477         {
478             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
479             return VLC_EGENERIC;
480         }
481         /* Insert the new one at the beginning */
482         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
483         p_new->value = val;
484         p_new->date = p_counter->last_update;
485         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
486                      0, p_new );
487
488         if( p_counter->i_samples == 3 )
489         {
490             p_old = p_counter->pp_samples[2];
491             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
492             free( p_old );
493         }
494         break;
495     }
496     case STATS_COUNTER:
497         if( p_counter->i_samples > 1)
498         {
499             msg_Err( p_handler, "LAST counter has several samples !" );
500             return VLC_EGENERIC;
501         }
502         if( p_counter->i_samples == 0 )
503         {
504             counter_sample_t *p_new = (counter_sample_t*)malloc(
505                                                sizeof( counter_sample_t ) );
506             p_new->value.psz_string = NULL;
507
508             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
509                          p_counter->i_samples, p_new );
510         }
511         if( p_counter->i_samples == 1 )
512         {
513             switch( p_counter->i_type )
514             {
515             case VLC_VAR_INTEGER:
516                 p_counter->pp_samples[0]->value.i_int += val.i_int;
517                 if( new_val )
518                     new_val->i_int = p_counter->pp_samples[0]->value.i_int;
519                 break;
520             case VLC_VAR_FLOAT:
521                 p_counter->pp_samples[0]->value.f_float += val.f_float;
522                 if( new_val )
523                     new_val->f_float = p_counter->pp_samples[0]->value.f_float;
524             default:
525                 msg_Err( p_handler, "Trying to increment invalid variable %s",
526                          p_counter->psz_name );
527                 return VLC_EGENERIC;
528             }
529         }
530         break;
531     }
532     return VLC_SUCCESS;
533 }
534
535 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
536                        vlc_bool_t b_total )
537 {
538     mtime_t last, total;
539     int i_total;
540     if( !p_counter || p_counter->i_samples != 2 )
541     {
542         msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
543         return;
544     }
545     i_total = p_counter->pp_samples[1]->value.i_int;
546     total = p_counter->pp_samples[1]->date;
547     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
548     {
549         last = mdate() - p_counter->pp_samples[0]->date;
550         i_total += 1;
551         total += last;
552     }
553     else
554     {
555         last = p_counter->pp_samples[0]->date;
556     }
557     if( b_total )
558     {
559         msg_Dbg( p_obj,
560              "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
561              p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
562              (float)(total)/(1000*(float)i_total ) );
563     }
564     else
565     {
566         msg_Dbg( p_obj,
567              "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
568              p_counter->psz_name, (float)total/1000, i_total,
569              (float)(total)/(1000*(float)i_total ) );
570     }
571 }