]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Support for moving averages (Refs:#473)
[vlc] / src / misc / stats.c
1 /*****************************************************************************
2  * stats.c: Statistics handling
3  *****************************************************************************
4  * Copyright (C) 1998-2005 the VideoLAN team
5  * $Id: messages.c 12729 2005-10-02 08:00:06Z courmisch $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdio.h>                                               /* required */
28
29 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31
32 /*****************************************************************************
33  * Local prototypes
34  *****************************************************************************/
35 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
36                             char *psz_name );
37 static int stats_CounterUpdate( stats_handler_t *p_handler,
38                                 counter_t *p_counter,
39                                 vlc_value_t val );
40 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this );
41 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this );
42
43 /*****************************************************************************
44  * Exported functions
45  *****************************************************************************/
46
47 /**
48  * Create a statistics counter
49  * \param p_this the object for which to create the counter
50  * \param psz_name the name
51  * \param i_type the type of stored data. One of VLC_VAR_STRING,
52  * VLC_VAR_INTEGER, VLC_VAR_FLOAT
53  * \param i_compute_type the aggregation type. One of STATS_LAST (always
54  * keep the last value), STATS_COUNTER (increment by the passed value),
55  * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
56  * (keep a time derivative of the value)
57  */
58 int __stats_Create( vlc_object_t *p_this, char *psz_name, int i_type,
59                     int i_compute_type )
60 {
61     counter_t *p_counter;
62     stats_handler_t *p_handler = stats_HandlerGet( p_this );
63     if( !p_handler ) return VLC_ENOMEM;
64
65     vlc_mutex_lock( &p_handler->object_lock );
66
67     p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
68
69     p_counter->psz_name = strdup( psz_name );
70     p_counter->i_source_object = p_this->i_object_id;
71     p_counter->i_compute_type = i_compute_type;
72     p_counter->i_type = i_type;
73     p_counter->i_samples = 0;
74     p_counter->pp_samples = NULL;
75
76     p_counter->update_interval = 0;
77     p_counter->last_update = 0;
78
79     INSERT_ELEM( p_handler->pp_counters,
80                  p_handler->i_counters,
81                  p_handler->i_counters,
82                  p_counter );
83
84     vlc_mutex_unlock( &p_handler->object_lock );
85
86     return VLC_SUCCESS;
87 }
88
89 /** Update a counter element with new values
90  * \param p_this the object in which to update
91  * \param psz_name the name
92  * \param val the vlc_value union containing the new value to aggregate. For
93  * more information on how data is aggregated, \see __stats_Create
94  */
95 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
96 {
97     int i_ret;
98     counter_t *p_counter;
99
100     /* Get stats handler singleton */
101     stats_handler_t *p_handler = stats_HandlerGet( p_this );
102     if( !p_handler ) return VLC_ENOMEM;
103
104     vlc_mutex_lock( &p_handler->object_lock );
105
106     /* Look for existing element */
107     p_counter = GetCounter( p_handler, p_this->i_object_id,
108                             psz_name );
109     if( !p_counter )
110     {
111         vlc_mutex_unlock( &p_handler->object_lock );
112         vlc_object_release( p_handler );
113         return VLC_ENOOBJ;
114     }
115
116     i_ret = stats_CounterUpdate( p_handler, p_counter, val );
117     vlc_mutex_unlock( &p_handler->object_lock );
118
119     return i_ret;
120 }
121
122 /** Get the aggregated value for a counter
123  * \param p_this an object
124  * \param i_object_id the object id from which we want the data
125  * \param psz_name the name of the couner
126  * \param val a pointer to an initialized vlc_value union. It will contain the
127  * retrieved value
128  * \return an error code
129  */
130 int __stats_Get( vlc_object_t *p_this, int i_object_id, char *psz_name, vlc_value_t *val )
131 {
132     counter_t *p_counter;
133
134     /* Get stats handler singleton */
135     stats_handler_t *p_handler = stats_HandlerGet( p_this );
136     if( !p_handler ) return VLC_ENOMEM;
137     vlc_mutex_lock( &p_handler->object_lock );
138
139
140     /* Look for existing element */
141     p_counter = GetCounter( p_handler, i_object_id,
142                             psz_name );
143     if( !p_counter )
144     {
145         vlc_mutex_unlock( &p_handler->object_lock );
146         vlc_object_release( p_handler );
147         return VLC_ENOOBJ;
148     }
149
150     if( p_counter->i_samples == 0 )
151     {
152         vlc_mutex_unlock( &p_handler->object_lock );
153         return VLC_EGENERIC;
154     }
155
156     switch( p_counter->i_compute_type )
157     {
158     case STATS_LAST:
159     case STATS_MIN:
160     case STATS_MAX:
161     case STATS_COUNTER:
162         *val = p_counter->pp_samples[0]->value;
163         break;
164     case STATS_DERIVATIVE:
165        if( p_counter->i_type == VLC_VAR_INTEGER )
166         {
167             float f = ( p_counter->pp_samples[0]->value.i_int -
168                         p_counter->pp_samples[1]->value.i_int ) /
169                     (float)(  p_counter->pp_samples[0]->date -
170                               p_counter->pp_samples[1]->date );
171             val->i_int = (int)f;
172         }
173         else
174         {
175             float f = (float)( p_counter->pp_samples[0]->value.f_float -
176                                p_counter->pp_samples[1]->value.f_float ) /
177                       (float)( p_counter->pp_samples[0]->date -
178                                p_counter->pp_samples[1]->date );
179             val->i_int = (int)f;
180             val->f_float = f;
181         }
182         break;
183     }
184     vlc_object_release( p_handler );
185
186     vlc_mutex_unlock( &p_handler->object_lock );
187     return VLC_SUCCESS;;
188 }
189
190 /** Get a statistics counter structure. This allows for low-level modifications
191  * \param p_this a parent object
192  * \param i_object_id the object from which to retrieve data
193  * \param psz_name the name
194  * \return the counter, or NULL if not found (or handler not created yet)
195  */
196 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
197                              char *psz_name )
198 {
199     counter_t *p_counter;
200
201     /* Get stats handler singleton */
202     stats_handler_t *p_handler = stats_HandlerGet( p_this );
203     if( !p_handler ) return NULL;
204
205     vlc_mutex_lock( &p_handler->object_lock );
206
207     /* Look for existing element */
208     p_counter = GetCounter( p_handler, p_this->i_object_id,
209                             psz_name );
210     vlc_mutex_unlock( &p_handler->object_lock );
211     vlc_object_release( p_handler );
212
213     return p_counter;
214 }
215
216
217 void stats_ComputeInputStats( input_thread_t *p_input,
218                               input_stats_t *p_stats )
219 {
220     vlc_mutex_lock( &p_stats->lock );
221     /* read_packets and read_bytes are common to all streams */
222     stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
223                        &p_stats->i_read_packets );
224     stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
225                        &p_stats->i_read_bytes );
226     stats_GetFloat( p_input, p_input->i_object_id, "input_bitrate",
227                        &p_stats->f_bitrate );
228     vlc_mutex_unlock( &p_stats->lock );
229 }
230
231 void stats_ReinitInputStats( input_stats_t *p_stats )
232 {
233     p_stats->i_read_packets = p_stats->i_read_bytes =
234         p_stats->f_bitrate = p_stats->f_average_bitrate =
235         p_stats->i_displayed_pictures = p_stats->i_lost_pictures = 0;
236 }
237
238 void stats_DumpInputStats( input_stats_t *p_stats  )
239 {
240     vlc_mutex_lock( &p_stats->lock );
241     /* f_bitrate is in bytes / microsecond
242      * *1000 => bytes / millisecond => kbytes / seconds */
243     fprintf( stderr, "Read packets : %i (%i bytes) - %f kB/s\n",
244                     p_stats->i_read_packets, p_stats->i_read_bytes,
245                     p_stats->f_bitrate * 1000 );
246     vlc_mutex_unlock( &p_stats->lock );
247 }
248
249
250 /********************************************************************
251  * Following functions are local
252  ********************************************************************/
253
254 /**
255  * Update a statistics counter, according to its type
256  * If needed, perform a bit of computation (derivative, mostly)
257  * This function must be entered with stats handler lock
258  * \param p_handler stats handler singleton
259  * \param p_counter the counter to update
260  * \param val the "new" value
261  * \return an error code
262  */
263 static int stats_CounterUpdate( stats_handler_t *p_handler,
264                                 counter_t *p_counter,
265                                 vlc_value_t val )
266 {
267     switch( p_counter->i_compute_type )
268     {
269     case STATS_LAST:
270     case STATS_MIN:
271     case STATS_MAX:
272         if( p_counter->i_samples > 1)
273         {
274             msg_Err( p_handler, "LAST counter has several samples !" );
275             return VLC_EGENERIC;
276         }
277         if( p_counter->i_type != VLC_VAR_FLOAT &&
278             p_counter->i_type != VLC_VAR_INTEGER &&
279             p_counter->i_compute_type != STATS_LAST )
280         {
281             msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
282             return VLC_EGENERIC;
283         }
284
285         if( p_counter->i_samples == 0 )
286         {
287             counter_sample_t *p_new = (counter_sample_t*)malloc(
288                                                sizeof( counter_sample_t ) );
289             p_new->value.psz_string = NULL;
290
291             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
292                          p_counter->i_samples, p_new );
293         }
294         if( p_counter->i_samples == 1 )
295         {
296             /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
297             if( p_counter->i_compute_type == STATS_LAST ||
298                 ( p_counter->i_compute_type == STATS_MAX &&
299                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
300                        p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
301                      ( p_counter->i_type == VLC_VAR_FLOAT &&
302                        p_counter->pp_samples[0]->value.f_float > val.f_float )
303                    ) ) ||
304                 ( p_counter->i_compute_type == STATS_MIN &&
305                    ( ( p_counter->i_type == VLC_VAR_INTEGER &&
306                        p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
307                      ( p_counter->i_type == VLC_VAR_FLOAT &&
308                        p_counter->pp_samples[0]->value.f_float < val.f_float )
309                    ) ) )
310             {
311                 if( p_counter->i_type == VLC_VAR_STRING &&
312                     p_counter->pp_samples[0]->value.psz_string )
313                 {
314                     free( p_counter->pp_samples[0]->value.psz_string );
315                 }
316                 p_counter->pp_samples[0]->value = val;
317             }
318         }
319         break;
320     case STATS_DERIVATIVE:
321     {
322         counter_sample_t *p_new, *p_old;
323         if( mdate() - p_counter->last_update < p_counter->update_interval )
324         {
325             return VLC_EGENERIC;
326         }
327         p_counter->last_update = mdate();
328         if( p_counter->i_type != VLC_VAR_FLOAT &&
329             p_counter->i_type != VLC_VAR_INTEGER )
330         {
331             msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
332             return VLC_EGENERIC;
333         }
334         /* Insert the new one at the beginning */
335         p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
336         p_new->value = val;
337         p_new->date = p_counter->last_update;
338         INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
339                      0, p_new );
340
341         if( p_counter->i_samples == 3 )
342         {
343             p_old = p_counter->pp_samples[2];
344             REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
345             free( p_old );
346         }
347         break;
348     }
349     case STATS_COUNTER:
350         if( p_counter->i_samples > 1)
351         {
352             msg_Err( p_handler, "LAST counter has several samples !" );
353             return VLC_EGENERIC;
354         }
355         if( p_counter->i_samples == 0 )
356         {
357             counter_sample_t *p_new = (counter_sample_t*)malloc(
358                                                sizeof( counter_sample_t ) );
359             p_new->value.psz_string = NULL;
360
361             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
362                          p_counter->i_samples, p_new );
363         }
364         if( p_counter->i_samples == 1 )
365         {
366             switch( p_counter->i_type )
367             {
368             case VLC_VAR_INTEGER:
369             case VLC_VAR_FLOAT:
370                 p_counter->pp_samples[0]->value.i_int += val.i_int;
371                 break;
372             default:
373                 msg_Err( p_handler, "Trying to increment invalid variable %s",
374                          p_counter->psz_name );
375                 return VLC_EGENERIC;
376             }
377         }
378         break;
379     }
380     return VLC_SUCCESS;
381 }
382
383 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
384                              char *psz_name )
385 {
386     int i;
387     for( i = 0; i< p_handler->i_counters; i++ )
388     {
389         counter_t *p_counter = p_handler->pp_counters[i];
390         if( p_counter->i_source_object == i_object_id &&
391             !strcmp( p_counter->psz_name, psz_name ) )
392         {
393             return p_counter;
394         }
395     }
396     return NULL;
397 }
398
399
400
401
402 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
403 {
404     stats_handler_t *p_handler = (stats_handler_t*)
405                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
406                                            FIND_ANYWHERE );
407     if( !p_handler )
408     {
409         p_handler = stats_HandlerCreate( p_this );
410         if( !p_handler )
411         {
412             return NULL;
413         }
414         vlc_object_yield( p_handler );
415     }
416     return p_handler;
417 }
418
419 /**
420  * Initialize statistics handler
421  *
422  * This function initializes the global statistics handler singleton,
423  * \param p_this the parent VLC object
424  */
425 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
426 {
427     stats_handler_t *p_handler;
428
429     msg_Dbg( p_this, "creating statistics handler" );
430
431     p_handler = (stats_handler_t*) vlc_object_create( p_this,
432                                                       VLC_OBJECT_STATS );
433
434     if( !p_handler )
435     {
436         msg_Err( p_this, "out of memory" );
437         return NULL;
438     }
439     p_handler->i_counters = 0;
440     p_handler->pp_counters = NULL;
441
442     /// \bug is it p_vlc or p_libvlc ?
443     vlc_object_attach( p_handler, p_this->p_vlc );
444
445     return p_handler;
446 }
447
448