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 */
31 /*****************************************************************************
33 *****************************************************************************/
34 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
36 static int stats_CounterUpdate( stats_handler_t *p_handler,
39 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this );
40 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this );
42 /*****************************************************************************
44 *****************************************************************************/
46 int __stats_Create( vlc_object_t *p_this, char *psz_name, int i_type,
50 stats_handler_t *p_handler = stats_HandlerGet( p_this );
52 p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
54 p_counter->psz_name = strdup( psz_name );
55 p_counter->i_source_object = p_this->i_object_id;
56 p_counter->i_compute_type = i_compute_type;
57 p_counter->i_type = i_type;
58 p_counter->i_samples = 0;
59 p_counter->pp_samples = NULL;
61 INSERT_ELEM( p_handler->pp_counters,
62 p_handler->i_counters,
63 p_handler->i_counters,
71 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
75 /* Get stats handler singleton */
76 stats_handler_t *p_handler = stats_HandlerGet( p_this );
77 if( !p_handler ) return VLC_ENOMEM;
79 /* Look for existing element */
80 p_counter = stats_GetCounter( p_handler, p_this->i_object_id,
84 vlc_object_release( p_handler );
88 return stats_CounterUpdate( p_handler, p_counter, val );
91 static int stats_CounterUpdate( stats_handler_t *p_handler,
95 switch( p_counter->i_compute_type )
100 if( p_counter->i_samples > 1)
102 msg_Err( p_handler, "LAST counter has several samples !" );
105 if( p_counter->i_type != VLC_VAR_FLOAT &&
106 p_counter->i_type != VLC_VAR_INTEGER &&
107 p_counter->i_compute_type != STATS_LAST )
109 msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
113 if( p_counter->i_samples == 0 )
115 counter_sample_t *p_new = (counter_sample_t*)malloc(
116 sizeof( counter_sample_t ) );
117 p_new->value.psz_string = NULL;
119 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
120 p_counter->i_samples, p_new );
122 if( p_counter->i_samples == 1 )
124 /* Update if : LAST or (MAX and bigger) or (MIN and bigger) */
125 if( p_counter->i_compute_type == STATS_LAST ||
126 ( p_counter->i_compute_type == STATS_MAX &&
127 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
128 p_counter->pp_samples[0]->value.i_int > val.i_int ) ||
129 ( p_counter->i_type == VLC_VAR_FLOAT &&
130 p_counter->pp_samples[0]->value.f_float > val.f_float )
132 ( p_counter->i_compute_type == STATS_MIN &&
133 ( ( p_counter->i_type == VLC_VAR_INTEGER &&
134 p_counter->pp_samples[0]->value.i_int < val.i_int ) ||
135 ( p_counter->i_type == VLC_VAR_FLOAT &&
136 p_counter->pp_samples[0]->value.f_float < val.f_float )
139 if( p_counter->i_type == VLC_VAR_STRING &&
140 p_counter->pp_samples[0]->value.psz_string )
142 free( p_counter->pp_samples[0]->value.psz_string );
144 p_counter->pp_samples[0]->value = val;
150 if( p_counter->i_samples > 1)
152 msg_Err( p_handler, "LAST counter has several samples !" );
155 if( p_counter->i_samples == 0 )
157 counter_sample_t *p_new = (counter_sample_t*)malloc(
158 sizeof( counter_sample_t ) );
159 p_new->value.psz_string = NULL;
161 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
162 p_counter->i_samples, p_new );
164 if( p_counter->i_samples == 1 )
166 switch( p_counter->i_type )
168 case VLC_VAR_INTEGER:
170 p_counter->pp_samples[0]->value.i_int += val.i_int;
173 msg_Err( p_handler, "Trying to increment invalid variable %s",
174 p_counter->psz_name );
184 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
188 for( i = 0; i< p_handler->i_counters; i++ )
190 counter_t *p_counter = p_handler->pp_counters[i];
191 if( p_counter->i_source_object == i_object_id &&
192 !strcmp( p_counter->psz_name, psz_name ) )
200 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
202 stats_handler_t *p_handler = (stats_handler_t*)
203 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
207 p_handler = stats_HandlerCreate( p_this );
212 vlc_object_yield( p_handler );
218 * Initialize statistics handler
220 * This function initializes the global statistics handler singleton,
221 * \param p_this the parent VLC object
223 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
225 stats_handler_t *p_handler;
227 msg_Dbg( p_this, "creating statistics handler" );
229 p_handler = (stats_handler_t*) vlc_object_create( p_this,
234 msg_Err( p_this, "out of memory" );
237 p_handler->i_counters = 0;
238 p_handler->pp_counters = NULL;
240 /// \bug is it p_vlc or p_libvlc ?
241 vlc_object_attach( p_handler, p_this->p_vlc );
247 void stats_ComputeInputStats( input_thread_t *p_input,
248 input_stats_t *p_stats )
251 /* read_packets and read_bytes are common to all streams */
252 //p_stats->i_read_packets = stats_GetInteger( p_input, "read_packets" );
253 // p_stats->i_read_bytes = stats_GetInteger( p_input, "read_bytes" );