]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Add support for several message queues - please test
[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
31 /*****************************************************************************
32  * Local prototypes
33  *****************************************************************************/
34 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
35                             char *psz_name );
36 static int stats_CounterUpdate( stats_handler_t *p_handler,
37                                 counter_t *p_counter,
38                                 vlc_value_t val );
39 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this );
40 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this );
41
42 /*****************************************************************************
43  * Exported functions
44  *****************************************************************************/
45
46 int __stats_Create( vlc_object_t *p_this, char *psz_name, int i_type,
47                     int i_compute_type )
48 {
49     counter_t *p_counter;
50     stats_handler_t *p_handler = stats_HandlerGet( p_this );
51
52     p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
53
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;
60
61     INSERT_ELEM( p_handler->pp_counters,
62                  p_handler->i_counters,
63                  p_handler->i_counters,
64                  p_counter );
65
66     fprintf (stderr, "Counter created\n");
67     
68     return VLC_SUCCESS;
69 }
70
71
72
73 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
74 {
75     counter_t *p_counter;
76
77     fprintf( stderr, "Updating\n");
78
79     /* Get stats handler singleton */
80     stats_handler_t *p_handler = stats_HandlerGet( p_this );
81     if( !p_handler ) return VLC_ENOMEM;
82     
83     fprintf( stderr, "Got handler\n");
84
85     /* Look for existing element */
86     p_counter = stats_GetCounter( p_handler, p_this->i_object_id,
87                                   psz_name );
88     if( !p_counter )
89     {
90         vlc_object_release( p_handler );
91         return VLC_ENOOBJ;
92     }
93
94     fprintf (stderr, "Got counter, updating it\n");
95
96     return stats_CounterUpdate( p_handler, p_counter, val );
97 }
98
99 static int stats_CounterUpdate( stats_handler_t *p_handler,
100                                 counter_t *p_counter,
101                                 vlc_value_t val )
102 {
103     switch( p_counter->i_compute_type )
104     {
105     case STATS_LAST:
106         if( p_counter->i_samples > 1)
107         {
108             msg_Err( p_handler, "LAST counter has several samples !" );
109             return VLC_EGENERIC;
110         }
111         if( p_counter->i_samples == 0 )
112         {
113             counter_sample_t *p_new = (counter_sample_t*)malloc(
114                                                sizeof( counter_sample_t ) );
115             p_new->value.psz_string = NULL;
116
117             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
118                          p_counter->i_samples, p_new );
119         }
120         if( p_counter->i_samples == 1 )
121         {
122             if( p_counter->i_type == VLC_VAR_STRING &&
123                 p_counter->pp_samples[0]->value.psz_string )
124             {
125                 free( p_counter->pp_samples[0]->value.psz_string );
126             }
127             p_counter->pp_samples[0]->value = val;
128         }
129         break;
130     case STATS_COUNTER:
131         if( p_counter->i_samples > 1)
132         {
133             msg_Err( p_handler, "LAST counter has several samples !" );
134             return VLC_EGENERIC;
135         }
136         if( p_counter->i_samples == 0 )
137         {
138             counter_sample_t *p_new = (counter_sample_t*)malloc(
139                                                sizeof( counter_sample_t ) );
140             p_new->value.psz_string = NULL;
141
142             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
143                          p_counter->i_samples, p_new );
144         }
145         if( p_counter->i_samples == 1 )
146         {
147             switch( p_counter->i_type )
148             {
149             case VLC_VAR_INTEGER:
150             case VLC_VAR_FLOAT:
151                 p_counter->pp_samples[0]->value.i_int += val.i_int;
152                 break;
153             default:
154                 msg_Err( p_handler, "Trying to increment invalid variable %s",
155                          p_counter->psz_name );
156                 return VLC_EGENERIC;
157             }
158         }
159         break;
160     }
161     fprintf (stderr, "Counter value is %i\n", p_counter->pp_samples[0]->value.i_int );
162     return VLC_SUCCESS;
163 }
164
165
166 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
167                                     char *psz_name )
168 {
169     int i;
170     fprintf( stderr, "Looking through %i counters\n", p_handler->i_counters );
171     for( i = 0; i< p_handler->i_counters; i++ )
172     {
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 ) )
177         {
178             return p_counter;
179         }
180     }
181     return NULL;
182 }
183
184 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
185 {
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,
189                                            FIND_ANYWHERE );
190     fprintf( stderr, "Got it %p\n", p_handler );
191     if( !p_handler )
192     {
193         p_handler = stats_HandlerCreate( p_this );
194         if( !p_handler )
195         {
196             return NULL;
197         }
198         vlc_object_yield( p_handler );
199     }
200     return p_handler;
201 }
202
203 /**
204  * Initialize statistics handler
205  *
206  * This function initializes the global statistics handler singleton,
207  * \param p_this the parent VLC object
208  */
209 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
210 {
211     stats_handler_t *p_handler;
212
213     msg_Dbg( p_this, "creating statistics handler" );
214
215     p_handler = (stats_handler_t*) vlc_object_create( p_this,
216                                                       VLC_OBJECT_STATS );
217
218     if( !p_handler )
219     {
220         msg_Err( p_this, "out of memory" );
221         return NULL;
222     }
223     p_handler->i_counters = 0;
224     p_handler->pp_counters = NULL;
225
226     /// \bug is it p_vlc or p_libvlc ?
227     vlc_object_attach( p_handler, p_this->p_vlc );
228
229     return p_handler;
230 }
231