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