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,
66 fprintf (stderr, "Counter created\n");
73 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
77 fprintf( stderr, "Updating\n");
79 /* Get stats handler singleton */
80 stats_handler_t *p_handler = stats_HandlerGet( p_this );
81 if( !p_handler ) return VLC_ENOMEM;
83 fprintf( stderr, "Got handler\n");
85 /* Look for existing element */
86 p_counter = stats_GetCounter( p_handler, p_this->i_object_id,
90 vlc_object_release( p_handler );
94 fprintf (stderr, "Got counter, updating it\n");
96 return stats_CounterUpdate( p_handler, p_counter, val );
99 static int stats_CounterUpdate( stats_handler_t *p_handler,
100 counter_t *p_counter,
103 switch( p_counter->i_compute_type )
106 if( p_counter->i_samples > 1)
108 msg_Err( p_handler, "LAST counter has several samples !" );
111 if( p_counter->i_samples == 0 )
113 counter_sample_t *p_new = (counter_sample_t*)malloc(
114 sizeof( counter_sample_t ) );
115 p_new->value.psz_string = NULL;
117 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
118 p_counter->i_samples, p_new );
120 if( p_counter->i_samples == 1 )
122 if( p_counter->i_type == VLC_VAR_STRING &&
123 p_counter->pp_samples[0]->value.psz_string )
125 free( p_counter->pp_samples[0]->value.psz_string );
127 p_counter->pp_samples[0]->value = val;
131 if( p_counter->i_samples > 1)
133 msg_Err( p_handler, "LAST counter has several samples !" );
136 if( p_counter->i_samples == 0 )
138 counter_sample_t *p_new = (counter_sample_t*)malloc(
139 sizeof( counter_sample_t ) );
140 p_new->value.psz_string = NULL;
142 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
143 p_counter->i_samples, p_new );
145 if( p_counter->i_samples == 1 )
147 switch( p_counter->i_type )
149 case VLC_VAR_INTEGER:
151 p_counter->pp_samples[0]->value.i_int += val.i_int;
154 msg_Err( p_handler, "Trying to increment invalid variable %s",
155 p_counter->psz_name );
161 fprintf (stderr, "Counter value is %i\n", p_counter->pp_samples[0]->value.i_int );
166 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
170 fprintf( stderr, "Looking through %i counters\n", p_handler->i_counters );
171 for( i = 0; i< p_handler->i_counters; i++ )
173 counter_t *p_counter = p_handler->pp_counters[i];
174 fprintf( stderr, "%i - %s\n", p_counter->i_source_object, p_counter->psz_name );
175 if( p_counter->i_source_object == i_object_id &&
176 !strcmp( p_counter->psz_name, psz_name ) )
184 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
186 fprintf (stderr, "Getting handler\n");
187 stats_handler_t *p_handler = (stats_handler_t*)
188 vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
190 fprintf( stderr, "Got it %p\n", p_handler );
193 p_handler = stats_HandlerCreate( p_this );
198 vlc_object_yield( p_handler );
204 * Initialize statistics handler
206 * This function initializes the global statistics handler singleton,
207 * \param p_this the parent VLC object
209 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
211 stats_handler_t *p_handler;
213 msg_Dbg( p_this, "creating statistics handler" );
215 p_handler = (stats_handler_t*) vlc_object_create( p_this,
220 msg_Err( p_this, "out of memory" );
223 p_handler->i_counters = 0;
224 p_handler->pp_counters = NULL;
226 /// \bug is it p_vlc or p_libvlc ?
227 vlc_object_attach( p_handler, p_this->p_vlc );