]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Support for creating object with custom type name and size.
[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_ReinitGlobalStats( global_stats_t *p_stats )
260 {
261     vlc_mutex_lock( &p_stats->lock );
262     p_stats->f_input_bitrate = p_stats->f_output_bitrate = 0.0;
263     vlc_mutex_unlock( &p_stats->lock );
264 }
265
266
267 void __stats_TimerStart( vlc_object_t *p_obj, const char *psz_name,
268                          unsigned int i_id )
269 {
270     int i;
271     counter_t *p_counter = NULL;
272     if( !p_obj->p_libvlc->b_stats ) return;
273     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
274
275     for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
276     {
277         if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
278         {
279             p_counter = p_obj->p_libvlc->pp_timers[i];
280             break;
281         }
282     }
283     if( !p_counter )
284     {
285         counter_sample_t *p_sample;
286         p_counter = stats_CounterCreate( p_obj->p_libvlc_global, VLC_VAR_TIME,
287                                          STATS_TIMER );
288         if( !p_counter )
289         {
290             vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
291             return;
292         }
293         p_counter->psz_name = strdup( psz_name );
294         p_counter->i_id = i_id;
295         INSERT_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers,
296                      p_obj->p_libvlc->i_timers, p_counter );
297
298         /* 1st sample : if started: start_date, else last_time, b_started */
299         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
300         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
301                      p_counter->i_samples, p_sample );
302         p_sample->date = 0; p_sample->value.b_bool = 0;
303         /* 2nd sample : global_time, i_samples */
304         p_sample = (counter_sample_t *)malloc( sizeof( counter_sample_t ) );
305         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
306                      p_counter->i_samples, p_sample );
307         p_sample->date = 0; p_sample->value.i_int = 0;
308     }
309     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
310     {
311         msg_Warn( p_obj, "timer %s was already started !", psz_name );
312         vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
313         return;
314     }
315     p_counter->pp_samples[0]->value.b_bool = VLC_TRUE;
316     p_counter->pp_samples[0]->date = mdate();
317     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
318 }
319
320 void __stats_TimerStop( vlc_object_t *p_obj, unsigned int i_id )
321 {
322     counter_t *p_counter = NULL;
323     int i;
324     if( !p_obj->p_libvlc->b_stats ) return;
325     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
326     for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
327     {
328         if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
329         {
330             p_counter = p_obj->p_libvlc->pp_timers[i];
331             break;
332         }
333     }
334     if( !p_counter || p_counter->i_samples != 2 )
335     {
336         msg_Err( p_obj, "timer does not exist" );
337         vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
338         return;
339     }
340     p_counter->pp_samples[0]->value.b_bool = VLC_FALSE;
341     p_counter->pp_samples[1]->value.i_int += 1;
342     p_counter->pp_samples[0]->date = mdate() - p_counter->pp_samples[0]->date;
343     p_counter->pp_samples[1]->date += p_counter->pp_samples[0]->date;
344     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
345 }
346
347 void __stats_TimerDump( vlc_object_t *p_obj, unsigned int i_id )
348 {
349     counter_t *p_counter = NULL;
350     int i;
351     if( !p_obj->p_libvlc->b_stats ) return;
352     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
353     for( i = 0 ; i < p_obj->p_libvlc->i_timers; i++ )
354     {
355         if( p_obj->p_libvlc->pp_timers[i]->i_id == i_id )
356         {
357             p_counter = p_obj->p_libvlc->pp_timers[i];
358             break;
359         }
360     }
361     TimerDump( p_obj, p_counter, VLC_TRUE );
362     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
363 }
364
365 void __stats_TimersDumpAll( vlc_object_t *p_obj )
366 {
367     int i;
368     if( !p_obj->p_libvlc->b_stats ) return;
369     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
370     for ( i = 0 ; i< p_obj->p_libvlc->i_timers ; i++ )
371         TimerDump( p_obj, p_obj->p_libvlc->pp_timers[i], VLC_FALSE );
372     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
373 }
374
375 void __stats_TimersClean( vlc_object_t *p_obj )
376 {
377     int i;
378     vlc_mutex_lock( &p_obj->p_libvlc->timer_lock );
379     for ( i = p_obj->p_libvlc->i_timers -1 ; i >= 0; i-- )
380     {
381         counter_t *p_counter = p_obj->p_libvlc->pp_timers[i];
382         REMOVE_ELEM( p_obj->p_libvlc->pp_timers, p_obj->p_libvlc->i_timers, i );
383         stats_CounterClean( p_counter );
384     }
385     vlc_mutex_unlock( &p_obj->p_libvlc->timer_lock );
386 }
387
388 void stats_CounterClean( counter_t *p_c )
389 {
390     int i;
391     if( p_c )
392     {
393         i = p_c->i_samples - 1 ;
394         while( i >= 0 )
395         {
396             counter_sample_t *p_s = p_c->pp_samples[i];
397             REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
398             free( p_s );
399             i--;
400         }
401         if( p_c->psz_name ) free( p_c->psz_name );
402         free( p_c );
403     }
404 }
405
406
407 /********************************************************************
408  * Following functions are local
409  ********************************************************************/
410
411 /**
412  * Update a statistics counter, according to its type
413  * If needed, perform a bit of computation (derivative, mostly)
414  * This function must be entered with stats handler lock
415  * \param p_counter the counter to update
416  * \param val the "new" value
417  * \return an error code
418  */
419 static int CounterUpdate( vlc_object_t *p_handler,
420                           counter_t *p_counter,
421                           vlc_value_t val, vlc_value_t *new_val )
422 {
423     switch( p_counter->i_compute_type )
424     {
425     case STATS_LAST:
426     case STATS_MIN:
427     case STATS_MAX:
428         if( p_counter->i_samples > 1)
429         {
430             msg_Err( p_handler, "LAST counter has several samples !" );
431             return VLC_EGENERIC;
432         }
433         if( p_counter->i_type != VLC_VAR_FLOAT &&
434             p_counter->i_type != VLC_VAR_INTEGER &&
435             p_counter->i_compute_type != STATS_LAST )
436         {
437             msg_Err( p_handler, "unable to compute MIN or MAX for this type");
438             return VLC_EGENERIC;
439         }
440
441         if( p_counter->i_samples == 0 )
442         {
443             counter_sample_t *p_new = (counter_sample_t*)malloc(
444                                                sizeof( counter_sample_t ) );
445             p_new->value.psz_string = NULL;
446
447             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
448                          p_counter->i_samples, p_new );
449         }
450         if( p_counter->i_samples == 1 )
451         {
452             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
453             if( p_counter->i_compute_type == STATS_LAST ||
454                 ( p_counter->i_compute_type == STATS_MAX &&
455                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
456                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
457                      ( p_counter->i_type == VLC_VAR_FLOAT &&
458                        p_counter->pp_samples[0]->value.f_float > val.f_float )
459                    ) ) ||
460                 ( p_counter->i_compute_type == STATS_MIN &&
461                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
462                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
463                      ( p_counter->i_type == VLC_VAR_FLOAT &&
464                        p_counter->pp_samples[0]->value.f_float < val.f_float )
465                    ) ) )
466             {
467                 if( p_counter->i_type == VLC_VAR_STRING &&
468                     p_counter->pp_samples[0]->value.psz_string )
469                 {
470                     free( p_counter->pp_samples[0]->value.psz_string );
471                 }
472                 p_counter->pp_samples[0]->value = val;
473                 *new_val = p_counter->pp_samples[0]->value;
474             }
475         }
476         break;
477     case STATS_DERIVATIVE:
478     {
479         counter_sample_t *p_new, *p_old;
480         if( mdate() - p_counter->last_update < p_counter->update_interval )
481         {
482             return VLC_EGENERIC;
483         }
484         p_counter->last_update = mdate();
485         if( p_counter->i_type != VLC_VAR_FLOAT &&
486             p_counter->i_type != VLC_VAR_INTEGER )
487         {
488             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
489             return VLC_EGENERIC;
490         }
491         /* Insert the new one at the beginning */
492         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
493         p_new->value = val;
494         p_new->date = p_counter->last_update;
495         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
496                      0, p_new );
497
498         if( p_counter->i_samples == 3 )
499         {
500             p_old = p_counter->pp_samples[2];
501             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
502             free( p_old );
503         }
504         break;
505     }
506     case STATS_COUNTER:
507         if( p_counter->i_samples > 1)
508         {
509             msg_Err( p_handler, "LAST counter has several samples !" );
510             return VLC_EGENERIC;
511         }
512         if( p_counter->i_samples == 0 )
513         {
514             counter_sample_t *p_new = (counter_sample_t*)malloc(
515                                                sizeof( counter_sample_t ) );
516             p_new->value.psz_string = NULL;
517
518             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
519                          p_counter->i_samples, p_new );
520         }
521         if( p_counter->i_samples == 1 )
522         {
523             switch( p_counter->i_type )
524             {
525             case VLC_VAR_INTEGER:
526                 p_counter->pp_samples[0]->value.i_int += val.i_int;
527                 if( new_val )
528                     new_val->i_int = p_counter->pp_samples[0]->value.i_int;
529                 break;
530             case VLC_VAR_FLOAT:
531                 p_counter->pp_samples[0]->value.f_float += val.f_float;
532                 if( new_val )
533                     new_val->f_float = p_counter->pp_samples[0]->value.f_float;
534             default:
535                 msg_Err( p_handler, "Trying to increment invalid variable %s",
536                          p_counter->psz_name );
537                 return VLC_EGENERIC;
538             }
539         }
540         break;
541     }
542     return VLC_SUCCESS;
543 }
544
545 static void TimerDump( vlc_object_t *p_obj, counter_t *p_counter,
546                        vlc_bool_t b_total )
547 {
548     mtime_t last, total;
549     int i_total;
550     if( !p_counter || p_counter->i_samples != 2 )
551     {
552         msg_Err( p_obj, "timer %s does not exist", p_counter->psz_name );
553         return;
554     }
555     i_total = p_counter->pp_samples[1]->value.i_int;
556     total = p_counter->pp_samples[1]->date;
557     if( p_counter->pp_samples[0]->value.b_bool == VLC_TRUE )
558     {
559         last = mdate() - p_counter->pp_samples[0]->date;
560         i_total += 1;
561         total += last;
562     }
563     else
564     {
565         last = p_counter->pp_samples[0]->date;
566     }
567     if( b_total )
568     {
569         msg_Dbg( p_obj,
570              "TIMER %s : %.3f ms - Total %.3f ms / %i intvls (Avg %.3f ms)",
571              p_counter->psz_name, (float)last/1000, (float)total/1000, i_total,
572              (float)(total)/(1000*(float)i_total ) );
573     }
574     else
575     {
576         msg_Dbg( p_obj,
577              "TIMER %s : Total %.3f ms / %i intvls (Avg %.3f ms)",
578              p_counter->psz_name, (float)total/1000, i_total,
579              (float)(total)/(1000*(float)i_total ) );
580     }
581 }