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 $
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdio.h> /* required */
30 #include <vlc_input.h>
32 /*****************************************************************************
34 *****************************************************************************/
35 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
37 static int stats_CounterUpdate( stats_handler_t *p_handler,
40 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this );
41 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this );
43 /*****************************************************************************
45 *****************************************************************************/
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)
58 int __stats_Create( vlc_object_t *p_this, char *psz_name, int i_type,
62 stats_handler_t *p_handler = stats_HandlerGet( p_this );
63 if( !p_handler ) return VLC_ENOMEM;
65 vlc_mutex_lock( &p_handler->object_lock );
67 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
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;
76 p_counter->update_interval = 0;
77 p_counter->last_update = 0;
79 INSERT_ELEM( p_handler->pp_counters,
80 p_handler->i_counters,
81 p_handler->i_counters,
84 vlc_mutex_unlock( &p_handler->object_lock );
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
95 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
100 /* Get stats handler singleton */
101 stats_handler_t *p_handler = stats_HandlerGet( p_this );
102 if( !p_handler ) return VLC_ENOMEM;
104 vlc_mutex_lock( &p_handler->object_lock );
106 /* Look for existing element */
107 p_counter = GetCounter( p_handler, p_this->i_object_id,
111 vlc_mutex_unlock( &p_handler->object_lock );
112 vlc_object_release( p_handler );
116 i_ret = stats_CounterUpdate( p_handler, p_counter, val );
117 vlc_mutex_unlock( &p_handler->object_lock );
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
128 * \return an error code
130 int __stats_Get( vlc_object_t *p_this, int i_object_id, char *psz_name, vlc_value_t *val )
132 counter_t *p_counter;
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 );
140 /* Look for existing element */
141 p_counter = GetCounter( p_handler, i_object_id,
145 vlc_mutex_unlock( &p_handler->object_lock );
146 vlc_object_release( p_handler );
150 if( p_counter->i_samples == 0 )
152 vlc_mutex_unlock( &p_handler->object_lock );
156 switch( p_counter->i_compute_type )
162 *val = p_counter->pp_samples[0]->value;
164 case STATS_DERIVATIVE:
165 if( p_counter->i_type == VLC_VAR_INTEGER )
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 );
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 );
184 vlc_object_release( p_handler );
186 vlc_mutex_unlock( &p_handler->object_lock );
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)
196 counter_t *__stats_CounterGet( vlc_object_t *p_this, int i_object_id,
199 counter_t *p_counter;
201 /* Get stats handler singleton */
202 stats_handler_t *p_handler = stats_HandlerGet( p_this );
203 if( !p_handler ) return NULL;
205 vlc_mutex_lock( &p_handler->object_lock );
207 /* Look for existing element */
208 p_counter = GetCounter( p_handler, p_this->i_object_id,
210 vlc_mutex_unlock( &p_handler->object_lock );
211 vlc_object_release( p_handler );
217 void stats_ComputeInputStats( input_thread_t *p_input,
218 input_stats_t *p_stats )
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 );
231 void stats_ReinitInputStats( input_stats_t *p_stats )
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;
238 void stats_DumpInputStats( input_stats_t *p_stats )
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 );
250 /********************************************************************
251 * Following functions are local
252 ********************************************************************/
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
263 static int stats_CounterUpdate( stats_handler_t *p_handler,
264 counter_t *p_counter,
267 switch( p_counter->i_compute_type )
272 if( p_counter->i_samples > 1)
274 msg_Err( p_handler, "LAST counter has several samples !" );
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 )
281 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
285 if( p_counter->i_samples == 0 )
287 counter_sample_t *p_new = (counter_sample_t*)malloc(
288 sizeof( counter_sample_t ) );
289 p_new->value.psz_string = NULL;
291 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
292 p_counter->i_samples, p_new );
294 if( p_counter->i_samples == 1 )
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 )
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 )
311 if( p_counter->i_type == VLC_VAR_STRING &&
312 p_counter->pp_samples[0]->value.psz_string )
314 free( p_counter->pp_samples[0]->value.psz_string );
316 p_counter->pp_samples[0]->value = val;
320 case STATS_DERIVATIVE:
322 counter_sample_t *p_new, *p_old;
323 if( mdate() - p_counter->last_update < p_counter->update_interval )
327 p_counter->last_update = mdate();
328 if( p_counter->i_type != VLC_VAR_FLOAT &&
329 p_counter->i_type != VLC_VAR_INTEGER )
331 msg_Err( p_handler, "Unable to compute DERIVATIVE for this type");
334 /* Insert the new one at the beginning */
335 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
337 p_new->date = p_counter->last_update;
338 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
341 if( p_counter->i_samples == 3 )
343 p_old = p_counter->pp_samples[2];
344 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
350 if( p_counter->i_samples > 1)
352 msg_Err( p_handler, "LAST counter has several samples !" );
355 if( p_counter->i_samples == 0 )
357 counter_sample_t *p_new = (counter_sample_t*)malloc(
358 sizeof( counter_sample_t ) );
359 p_new->value.psz_string = NULL;
361 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
362 p_counter->i_samples, p_new );
364 if( p_counter->i_samples == 1 )
366 switch( p_counter->i_type )
368 case VLC_VAR_INTEGER:
370 p_counter->pp_samples[0]->value.i_int += val.i_int;
373 msg_Err( p_handler, "Trying to increment invalid variable %s",
374 p_counter->psz_name );
383 static counter_t *GetCounter( stats_handler_t *p_handler, int i_object_id,
387 for( i = 0; i< p_handler->i_counters; i++ )
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 ) )
402 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
404 stats_handler_t *p_handler = (stats_handler_t*)
405 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
409 p_handler = stats_HandlerCreate( p_this );
414 vlc_object_yield( p_handler );
420 * Initialize statistics handler
422 * This function initializes the global statistics handler singleton,
423 * \param p_this the parent VLC object
425 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
427 stats_handler_t *p_handler;
429 msg_Dbg( p_this, "creating statistics handler" );
431 p_handler = (stats_handler_t*) vlc_object_create( p_this,
436 msg_Err( p_this, "out of memory" );
439 p_handler->i_counters = 0;
440 p_handler->pp_counters = NULL;
442 /// \bug is it p_vlc or p_libvlc ?
443 vlc_object_attach( p_handler, p_this->p_vlc );