]> git.sesse.net Git - vlc/blob - src/misc/stats.c
qt4: Catch INPUT_EVENT_PROGRAM updates and show icon when "program-scrambled" is...
[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_common.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( !libvlc_stats (p_this) || !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( !libvlc_stats (p_this) || !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     (void)p_input;
146     input_stats_t *p_stats = calloc( 1, sizeof(input_stats_t) );
147     if( !p_stats )
148         return NULL;
149
150     vlc_mutex_init( &p_stats->lock );
151     stats_ReinitInputStats( p_stats );
152
153     return p_stats;
154 }
155
156 void stats_ComputeInputStats( input_thread_t *p_input, input_stats_t *p_stats )
157 {
158     if( !libvlc_stats (p_input) ) return;
159
160     vlc_mutex_lock( &p_input->p->counters.counters_lock );
161     vlc_mutex_lock( &p_stats->lock );
162
163     /* Input */
164     stats_GetInteger( p_input, p_input->p->counters.p_read_packets,
165                       &p_stats->i_read_packets );
166     stats_GetInteger( p_input, p_input->p->counters.p_read_bytes,
167                       &p_stats->i_read_bytes );
168     stats_GetFloat( p_input, p_input->p->counters.p_input_bitrate,
169                     &p_stats->f_input_bitrate );
170     stats_GetInteger( p_input, p_input->p->counters.p_demux_read,
171                       &p_stats->i_demux_read_bytes );
172     stats_GetFloat( p_input, p_input->p->counters.p_demux_bitrate,
173                     &p_stats->f_demux_bitrate );
174
175     /* Decoders */
176     stats_GetInteger( p_input, p_input->p->counters.p_decoded_video,
177                       &p_stats->i_decoded_video );
178     stats_GetInteger( p_input, p_input->p->counters.p_decoded_audio,
179                       &p_stats->i_decoded_audio );
180
181     /* Sout */
182     if( p_input->p->counters.p_sout_send_bitrate )
183     {
184         stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_packets,
185                           &p_stats->i_sent_packets );
186         stats_GetInteger( p_input, p_input->p->counters.p_sout_sent_bytes,
187                           &p_stats->i_sent_bytes );
188         stats_GetFloat  ( p_input, p_input->p->counters.p_sout_send_bitrate,
189                           &p_stats->f_send_bitrate );
190     }
191
192     /* Aout */
193     stats_GetInteger( p_input, p_input->p->counters.p_played_abuffers,
194                       &p_stats->i_played_abuffers );
195     stats_GetInteger( p_input, p_input->p->counters.p_lost_abuffers,
196                       &p_stats->i_lost_abuffers );
197
198     /* Vouts */
199     stats_GetInteger( p_input, p_input->p->counters.p_displayed_pictures,
200                       &p_stats->i_displayed_pictures );
201     stats_GetInteger( p_input, p_input->p->counters.p_lost_pictures,
202                       &p_stats->i_lost_pictures );
203
204     vlc_mutex_unlock( &p_stats->lock );
205     vlc_mutex_unlock( &p_input->p->counters.counters_lock );
206 }
207
208 void stats_ReinitInputStats( input_stats_t *p_stats )
209 {
210     vlc_mutex_lock( &p_stats->lock );
211     p_stats->i_read_packets = p_stats->i_read_bytes =
212     p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
213     p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
214     p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
215     p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
216     p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
217     p_stats->i_decoded_video = p_stats->i_decoded_audio =
218     p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
219      = 0;
220     vlc_mutex_unlock( &p_stats->lock );
221 }
222
223 void stats_DumpInputStats( input_stats_t *p_stats  )
224 {
225     vlc_mutex_lock( &p_stats->lock );
226     /* f_bitrate is in bytes / microsecond
227      * *1000 => bytes / millisecond => kbytes / seconds */
228     fprintf( stderr, "Input : %i (%i bytes) - %f kB/s - "
229                      "Demux : %i (%i bytes) - %f kB/s\n"
230                      " - Vout : %i/%i - Aout : %i/%i - Sout : %f\n",
231                     p_stats->i_read_packets, p_stats->i_read_bytes,
232                     p_stats->f_input_bitrate * 1000,
233                     p_stats->i_demux_read_packets, p_stats->i_demux_read_bytes,
234                     p_stats->f_demux_bitrate * 1000,
235                     p_stats->i_displayed_pictures, p_stats->i_lost_pictures,
236                     p_stats->i_played_abuffers, p_stats->i_lost_abuffers,
237                     p_stats->f_send_bitrate );
238     vlc_mutex_unlock( &p_stats->lock );
239 }
240
241 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
242                          unsigned int i_id )
243 {
244     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
245     counter_t *p_counter = NULL;
246
247     if( !priv->b_stats ) return;
248
249     vlc_mutex_lock( &priv->timer_lock );
250
251     for( int i = 0 ; i < priv->i_timers; i++ )
252     {
253         if( priv->pp_timers[i]->i_id == i_id
254             && priv->pp_timers[i]->p_obj == p_obj )
255         {
256             p_counter = priv->pp_timers[i];
257             break;
258         }
259     }
260     if( !p_counter )
261     {
262         counter_sample_t *p_sample;
263         p_counter = stats_CounterCreate( p_obj->p_libvlc, VLC_VAR_TIME,
264                                          STATS_TIMER );
265         if( !p_counter )
266             goto out;
267         p_counter->psz_name = strdup( psz_name );
268         p_counter->i_id = i_id;
269         p_counter->p_obj = p_obj;
270         INSERT_ELEM( priv->pp_timers, priv->i_timers,
271                      priv->i_timers, p_counter );
272
273         /* 1st sample : if started: start_date, else last_time, b_started */
274         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
275         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
276                      p_counter->i_samples, p_sample );
277         p_sample->date = 0; p_sample->value.b_bool = 0;
278         /* 2nd sample : global_time, i_samples */
279         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
280         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
281                      p_counter->i_samples, p_sample );
282         p_sample->date = 0; p_sample->value.i_int = 0;
283     }
284     if( p_counter->pp_samples[0]->value.b_bool == true )
285     {
286         msg_Warn( p_obj, "timer '%s' was already started !", psz_name );
287         goto out;
288     }
289     p_counter->pp_samples[0]->value.b_bool = true;
290     p_counter->pp_samples[0]->date = mdate();
291 out:
292     vlc_mutex_unlock( &priv->timer_lock );
293 }
294
295 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
296 {
297     counter_t *p_counter = NULL;
298     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
299
300     if( !priv->b_stats ) return;
301     vlc_mutex_lock( &priv->timer_lock );
302     for( int i = 0 ; i < priv->i_timers; i++ )
303     {
304         if( priv->pp_timers[i]->i_id == i_id
305             && priv->pp_timers[i]->p_obj == p_obj )
306         {
307             p_counter = priv->pp_timers[i];
308             break;
309         }
310     }
311     if( !p_counter || p_counter->i_samples != 2 )
312     {
313         msg_Err( p_obj, "timer does not exist" );
314         goto out;
315     }
316     p_counter->pp_samples[0]->value.b_bool = false;
317     p_counter->pp_samples[1]->value.i_int += 1;
318     p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
319     p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
320 out:
321     vlc_mutex_unlock( &priv->timer_lock );
322 }
323
324 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
325 {
326     counter_t *p_counter = NULL;
327     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
328
329     if( !priv->b_stats ) return;
330     vlc_mutex_lock( &priv->timer_lock );
331     for( int i = 0 ; i < priv->i_timers; i++ )
332     {
333         if( priv->pp_timers[i]->i_id == i_id
334             && priv->pp_timers[i]->p_obj == p_obj )
335         {
336             p_counter = priv->pp_timers[i];
337             break;
338         }
339     }
340     TimerDump( p_obj, p_counter, true );
341     vlc_mutex_unlock( &priv->timer_lock );
342 }
343
344 void __stats_TimersDumpAll( vlc_object_t *p_obj )
345 {
346     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
347
348     if( !priv->b_stats ) return;
349     vlc_mutex_lock( &priv->timer_lock );
350     for ( int i = 0 ; i < priv->i_timers ; i++ )
351         TimerDump( p_obj, priv->pp_timers[i], false );
352     vlc_mutex_unlock( &priv->timer_lock );
353 }
354
355 void __stats_TimerClean( vlc_object_t *p_obj, unsigned int i_id )
356 {
357     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
358
359     vlc_mutex_lock( &priv->timer_lock );
360     for ( int i = priv->i_timers -1 ; i >= 0; i-- )
361     {
362         counter_t *p_counter = priv->pp_timers[i];
363         if( p_counter->i_id == i_id && p_counter->p_obj == p_obj )
364         {
365             REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
366             stats_CounterClean( p_counter );
367         }
368     }
369     vlc_mutex_unlock( &priv->timer_lock );
370 }
371
372 void __stats_TimersCleanAll( vlc_object_t *p_obj )
373 {
374     libvlc_priv_t *priv = libvlc_priv (p_obj->p_libvlc);
375
376     vlc_mutex_lock( &priv->timer_lock );
377     for ( int i = priv->i_timers -1 ; i >= 0; i-- )
378     {
379         counter_t *p_counter = priv->pp_timers[i];
380         REMOVE_ELEM( priv->pp_timers, priv->i_timers, i );
381         stats_CounterClean( p_counter );
382     }
383     vlc_mutex_unlock( &priv->timer_lock );
384 }
385
386 void stats_CounterClean( counter_t *p_c )
387 {
388     if( p_c )
389     {
390         int i = p_c->i_samples - 1 ;
391         while( i >= 0 )
392         {
393             counter_sample_t *p_s = p_c->pp_samples[i];
394             REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
395             free( p_s );
396             i--;
397         }
398         free( p_c->psz_name );
399         free( p_c );
400     }
401 }
402
403
404 /********************************************************************
405  * Following functions are local
406  ********************************************************************/
407
408 /**
409  * Update a statistics counter, according to its type
410  * If needed, perform a bit of computation (derivative, mostly)
411  * This function must be entered with stats handler lock
412  * \param p_counter the counter to update
413  * \param val the "new" value
414  * \return an error code
415  */
416 static int CounterUpdate( vlc_object_t *p_handler,
417                           counter_t *p_counter,
418                           vlc_value_t val, vlc_value_t *new_val )
419 {
420     switch( p_counter->i_compute_type )
421     {
422     case STATS_LAST:
423     case STATS_MIN:
424     case STATS_MAX:
425         if( p_counter->i_samples > 1)
426         {
427             msg_Err( p_handler, "LAST counter has several samples !" );
428             return VLC_EGENERIC;
429         }
430         if( p_counter->i_type != VLC_VAR_FLOAT &&
431             p_counter->i_type != VLC_VAR_INTEGER &&
432             p_counter->i_compute_type != STATS_LAST )
433         {
434             msg_Err( p_handler, "unable to compute MIN or MAX for this type");
435             return VLC_EGENERIC;
436         }
437
438         if( p_counter->i_samples == 0 )
439         {
440             counter_sample_t *p_new = (counter_sample_t*)malloc(
441                                                sizeof( counter_sample_t ) );
442             p_new->value.psz_string = NULL;
443
444             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
445                          p_counter->i_samples, p_new );
446         }
447         if( p_counter->i_samples == 1 )
448         {
449             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
450             if( p_counter->i_compute_type == STATS_LAST ||
451                 ( p_counter->i_compute_type == STATS_MAX &&
452                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
453                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
454                      ( p_counter->i_type == VLC_VAR_FLOAT &&
455                        p_counter->pp_samples[0]->value.f_float > val.f_float )
456                    ) ) ||
457                 ( p_counter->i_compute_type == STATS_MIN &&
458                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
459                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
460                      ( p_counter->i_type == VLC_VAR_FLOAT &&
461                        p_counter->pp_samples[0]->value.f_float < val.f_float )
462                    ) ) )
463             {
464                 if( p_counter->i_type == VLC_VAR_STRING &&
465                     p_counter->pp_samples[0]->value.psz_string )
466                 {
467                     free( p_counter->pp_samples[0]->value.psz_string );
468                 }
469                 p_counter->pp_samples[0]->value = val;
470                 *new_val = p_counter->pp_samples[0]->value;
471             }
472         }
473         break;
474     case STATS_DERIVATIVE:
475     {
476         counter_sample_t *p_new, *p_old;
477         mtime_t now = mdate();
478         if( now - p_counter->last_update < p_counter->update_interval )
479         {
480             return VLC_EGENERIC;
481         }
482         p_counter->last_update = now;
483         if( p_counter->i_type != VLC_VAR_FLOAT &&
484             p_counter->i_type != VLC_VAR_INTEGER )
485         {
486             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
487             return VLC_EGENERIC;
488         }
489         /* Insert the new one at the beginning */
490         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
491         p_new->value = val;
492         p_new->date = p_counter->last_update;
493         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
494                      0, p_new );
495
496         if( p_counter->i_samples == 3 )
497         {
498             p_old = p_counter->pp_samples[2];
499             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
500             free( p_old );
501         }
502         break;
503     }
504     case STATS_COUNTER:
505         if( p_counter->i_samples > 1)
506         {
507             msg_Err( p_handler, "LAST counter has several samples !" );
508             return VLC_EGENERIC;
509         }
510         if( p_counter->i_samples == 0 )
511         {
512             counter_sample_t *p_new = (counter_sample_t*)malloc(
513                                                sizeof( counter_sample_t ) );
514             p_new->value.psz_string = NULL;
515
516             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
517                          p_counter->i_samples, p_new );
518         }
519         if( p_counter->i_samples == 1 )
520         {
521             switch( p_counter->i_type )
522             {
523             case VLC_VAR_INTEGER:
524                 p_counter->pp_samples[0]->value.i_int += val.i_int;
525                 if( new_val )
526                     new_val->i_int = p_counter->pp_samples[0]->value.i_int;
527                 break;
528             case VLC_VAR_FLOAT:
529                 p_counter->pp_samples[0]->value.f_float += val.f_float;
530                 if( new_val )
531                     new_val->f_float = p_counter->pp_samples[0]->value.f_float;
532             default:
533                 msg_Err( p_handler, "Trying to increment invalid variable %s",
534                          p_counter->psz_name );
535                 return VLC_EGENERIC;
536             }
537         }
538         break;
539     }
540     return VLC_SUCCESS;
541 }
542
543 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
544                        bool b_total )
545 {
546     if( !p_counter )
547         return;
548
549     mtime_t last, total;
550     int i_total;
551     if( p_counter->i_samples != 2 )
552     {
553         msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
554         return;
555     }
556     i_total = p_counter->pp_samples[1]->value.i_int;
557     total = p_counter->pp_samples[1]->date;
558     if( p_counter->pp_samples[0]->value.b_bool == true )
559     {
560         last = mdate() - p_counter->pp_samples[0]->date;
561         i_total += 1;
562         total += last;
563     }
564     else
565     {
566         last = p_counter->pp_samples[0]->date;
567     }
568     if( b_total )
569     {
570         msg_Dbg( p_obj,
571              "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
572              p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
573              (float)(total)/(1000*(float)i_total ) );
574     }
575     else
576     {
577         msg_Dbg( p_obj,
578              "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
579              p_counter->psz_name, (float)total/1000, i_total,
580              (float)(total)/(1000*(float)i_total ) );
581     }
582 }