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