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