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 *stats_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 *****************************************************************************/
47 int __stats_Create( vlc_object_t *p_this, char *psz_name, int i_type,
51 stats_handler_t *p_handler = stats_HandlerGet( p_this );
52 if( !p_handler ) return VLC_ENOMEM;
54 vlc_mutex_lock( &p_handler->object_lock );
56 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
58 p_counter->psz_name = strdup( psz_name );
59 p_counter->i_source_object = p_this->i_object_id;
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;
65 INSERT_ELEM( p_handler->pp_counters,
66 p_handler->i_counters,
67 p_handler->i_counters,
70 vlc_mutex_unlock( &p_handler->object_lock );
77 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
82 /* Get stats handler singleton */
83 stats_handler_t *p_handler = stats_HandlerGet( p_this );
84 if( !p_handler ) return VLC_ENOMEM;
86 vlc_mutex_lock( &p_handler->object_lock );
88 /* Look for existing element */
89 p_counter = stats_GetCounter( p_handler, p_this->i_object_id,
93 vlc_mutex_unlock( &p_handler->object_lock );
94 vlc_object_release( p_handler );
98 i_ret = stats_CounterUpdate( p_handler, p_counter, val );
99 vlc_mutex_unlock( &p_handler->object_lock );
104 int __stats_Get( vlc_object_t *p_this, int i_object_id, char *psz_name, vlc_value_t *val )
106 counter_t *p_counter;
108 /* Get stats handler singleton */
109 stats_handler_t *p_handler = stats_HandlerGet( p_this );
110 if( !p_handler ) return VLC_ENOMEM;
111 vlc_mutex_lock( &p_handler->object_lock );
114 /* Look for existing element */
115 p_counter = stats_GetCounter( p_handler, i_object_id,
119 vlc_mutex_unlock( &p_handler->object_lock );
120 vlc_object_release( p_handler );
124 if( p_counter->i_samples == 0 )
126 vlc_mutex_unlock( &p_handler->object_lock );
130 /* FIXME: Does not work for all types, maybe */
131 *val = p_counter->pp_samples[0]->value;
132 vlc_object_release( p_handler );
134 vlc_mutex_unlock( &p_handler->object_lock );
138 void stats_ComputeInputStats( input_thread_t *p_input,
139 input_stats_t *p_stats )
141 vlc_mutex_lock( &p_stats->lock );
142 /* read_packets and read_bytes are common to all streams */
143 stats_GetInteger( p_input, p_input->i_object_id, "read_packets",
144 &p_stats->i_read_packets );
145 stats_GetInteger( p_input, p_input->i_object_id, "read_bytes",
146 &p_stats->i_read_bytes );
147 vlc_mutex_unlock( &p_stats->lock );
150 void stats_ReinitInputStats( input_stats_t *p_stats )
152 p_stats->i_read_packets = p_stats->i_read_bytes =
153 p_stats->f_last_bitrate = p_stats->f_average_bitrate =
154 p_stats->i_displayed_pictures = p_stats->i_lost_pictures = 0;
157 void stats_DumpInputStats( input_stats_t *p_stats )
159 vlc_mutex_lock( &p_stats->lock );
160 fprintf( stderr, "Read packets : %i (%i bytes)\n",
161 p_stats->i_read_packets, p_stats->i_read_bytes );
162 vlc_mutex_unlock( &p_stats->lock );
166 /********************************************************************
167 * Following functions are local
168 ********************************************************************/
171 * Update a statistics counter, according to its type
172 * If needed, perform a bit of computation (derivative, mostly)
173 * This function must be entered with stats handler lock
174 * \param p_handler stats handler singleton
175 * \param p_counter the counter to update
176 * \param val the "new" value
177 * \return an error code
179 static int stats_CounterUpdate( stats_handler_t *p_handler,
180 counter_t *p_counter,
183 switch( p_counter->i_compute_type )
188 if( p_counter->i_samples > 1)
190 msg_Err( p_handler, "LAST counter has several samples !" );
193 if( p_counter->i_type != VLC_VAR_FLOAT &&
194 p_counter->i_type != VLC_VAR_INTEGER &&
195 p_counter->i_compute_type != STATS_LAST )
197 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
201 if( p_counter->i_samples == 0 )
203 counter_sample_t *p_new = (counter_sample_t*)malloc(
204 sizeof( counter_sample_t ) );
205 p_new->value.psz_string = NULL;
207 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
208 p_counter->i_samples, p_new );
210 if( p_counter->i_samples == 1 )
212 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
213 if( p_counter->i_compute_type == STATS_LAST ||
214 ( p_counter->i_compute_type == STATS_MAX &&
215 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
216 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
217 ( p_counter->i_type == VLC_VAR_FLOAT &&
218 p_counter->pp_samples[0]->value.f_float > val.f_float )
220 ( p_counter->i_compute_type == STATS_MIN &&
221 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
222 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
223 ( p_counter->i_type == VLC_VAR_FLOAT &&
224 p_counter->pp_samples[0]->value.f_float < val.f_float )
227 if( p_counter->i_type == VLC_VAR_STRING &&
228 p_counter->pp_samples[0]->value.psz_string )
230 free( p_counter->pp_samples[0]->value.psz_string );
232 p_counter->pp_samples[0]->value = val;
238 if( p_counter->i_samples > 1)
240 msg_Err( p_handler, "LAST counter has several samples !" );
243 if( p_counter->i_samples == 0 )
245 counter_sample_t *p_new = (counter_sample_t*)malloc(
246 sizeof( counter_sample_t ) );
247 p_new->value.psz_string = NULL;
249 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
250 p_counter->i_samples, p_new );
252 if( p_counter->i_samples == 1 )
254 switch( p_counter->i_type )
256 case VLC_VAR_INTEGER:
258 p_counter->pp_samples[0]->value.i_int += val.i_int;
261 msg_Err( p_handler, "Trying to increment invalid variable %s",
262 p_counter->psz_name );
272 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
276 for( i = 0; i< p_handler->i_counters; i++ )
278 counter_t *p_counter = p_handler->pp_counters[i];
279 if( p_counter->i_source_object == i_object_id &&
280 !strcmp( p_counter->psz_name, psz_name ) )
288 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
290 stats_handler_t *p_handler = (stats_handler_t*)
291 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
295 p_handler = stats_HandlerCreate( p_this );
300 vlc_object_yield( p_handler );
306 * Initialize statistics handler
308 * This function initializes the global statistics handler singleton,
309 * \param p_this the parent VLC object
311 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
313 stats_handler_t *p_handler;
315 msg_Dbg( p_this, "creating statistics handler" );
317 p_handler = (stats_handler_t*) vlc_object_create( p_this,
322 msg_Err( p_this, "out of memory" );
325 p_handler->i_counters = 0;
326 p_handler->pp_counters = NULL;
328 /// \bug is it p_vlc or p_libvlc ?
329 vlc_object_attach( p_handler, p_this->p_vlc );