]> git.sesse.net Git - vlc/blob - src/misc/stats.c
Some more stats preliminary work
[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     return VLC_SUCCESS;
67 }
68
69
70
71 int __stats_Update( vlc_object_t *p_this, char *psz_name, vlc_value_t val )
72 {
73     counter_t *p_counter;
74
75     /* Get stats handler singleton */
76     stats_handler_t *p_handler = stats_HandlerGet( p_this );
77     if( !p_handler ) return VLC_ENOMEM;
78
79     /* Look for existing element */
80     p_counter = stats_GetCounter( p_handler, p_this->i_object_id,
81                                   psz_name );
82     if( !p_counter )
83     {
84         vlc_object_release( p_handler );
85         return VLC_ENOOBJ;
86     }
87
88     return stats_CounterUpdate( p_handler, p_counter, val );
89 }
90
91 static int stats_CounterUpdate( stats_handler_t *p_handler,
92                                 counter_t *p_counter,
93                                 vlc_value_t val )
94 {
95     switch( p_counter->i_compute_type )
96     {
97     case STATS_LAST:
98     case STATS_MIN:
99     case STATS_LAST:
100         if( p_counter->i_samples > 1)
101         {
102             msg_Err( p_handler, "LAST counter has several samples !" );
103             return VLC_EGENERIC;
104         }
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 )
108         {
109             msg_Err( p_handler, "Unable to compute MIN or MAX for this type");
110             return VLC_EGENERIC;
111         }
112
113         if( p_counter->i_samples == 0 )
114         {
115             counter_sample_t *p_new = (counter_sample_t*)malloc(
116                                                sizeof( counter_sample_t ) );
117             p_new->value.psz_string = NULL;
118
119             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
120                          p_counter->i_samples, p_new );
121         }
122         if( p_counter->i_samples == 1 )
123         {
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 )
131                    ) ) ||
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 )
137                    ) ) )
138             {
139                 if( p_counter->i_type == VLC_VAR_STRING &&
140                     p_counter->pp_samples[0]->value.psz_string )
141                 {
142                     free( p_counter->pp_samples[0]->value.psz_string );
143                 }
144                 p_counter->pp_samples[0]->value = val;
145             }
146         }
147         break;
148
149     case STATS_COUNTER:
150         if( p_counter->i_samples > 1)
151         {
152             msg_Err( p_handler, "LAST counter has several samples !" );
153             return VLC_EGENERIC;
154         }
155         if( p_counter->i_samples == 0 )
156         {
157             counter_sample_t *p_new = (counter_sample_t*)malloc(
158                                                sizeof( counter_sample_t ) );
159             p_new->value.psz_string = NULL;
160
161             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
162                          p_counter->i_samples, p_new );
163         }
164         if( p_counter->i_samples == 1 )
165         {
166             switch( p_counter->i_type )
167             {
168             case VLC_VAR_INTEGER:
169             case VLC_VAR_FLOAT:
170                 p_counter->pp_samples[0]->value.i_int += val.i_int;
171                 break;
172             default:
173                 msg_Err( p_handler, "Trying to increment invalid variable %s",
174                          p_counter->psz_name );
175                 return VLC_EGENERIC;
176             }
177         }
178         break;
179     }
180     return VLC_SUCCESS;
181 }
182
183
184 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
185                                     char *psz_name )
186 {
187     int i;
188     for( i = 0; i< p_handler->i_counters; i++ )
189     {
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 ) )
193         {
194             return p_counter;
195         }
196     }
197     return NULL;
198 }
199
200 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
201 {
202     stats_handler_t *p_handler = (stats_handler_t*)
203                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
204                                            FIND_ANYWHERE );
205     if( !p_handler )
206     {
207         p_handler = stats_HandlerCreate( p_this );
208         if( !p_handler )
209         {
210             return NULL;
211         }
212         vlc_object_yield( p_handler );
213     }
214     return p_handler;
215 }
216
217 /**
218  * Initialize statistics handler
219  *
220  * This function initializes the global statistics handler singleton,
221  * \param p_this the parent VLC object
222  */
223 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
224 {
225     stats_handler_t *p_handler;
226
227     msg_Dbg( p_this, "creating statistics handler" );
228
229     p_handler = (stats_handler_t*) vlc_object_create( p_this,
230                                                       VLC_OBJECT_STATS );
231
232     if( !p_handler )
233     {
234         msg_Err( p_this, "out of memory" );
235         return NULL;
236     }
237     p_handler->i_counters = 0;
238     p_handler->pp_counters = NULL;
239
240     /// \bug is it p_vlc or p_libvlc ?
241     vlc_object_attach( p_handler, p_this->p_vlc );
242
243     return p_handler;
244 }
245
246
247 void stats_ComputeInputStats( input_thread_t *p_input,
248                               input_stats_t *p_stats )
249 {
250     int i;
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" );
254
255 }