]> git.sesse.net Git - vlc/blob - src/misc/stats.c
c7ad2cbc57aa351847ad40dfbf30d8416ad7e48c
[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         if( p_counter->i_samples > 1)
99         {
100             msg_Err( p_handler, "LAST counter has several samples !" );
101             return VLC_EGENERIC;
102         }
103         if( p_counter->i_samples == 0 )
104         {
105             counter_sample_t *p_new = (counter_sample_t*)malloc(
106                                                sizeof( counter_sample_t ) );
107             p_new->value.psz_string = NULL;
108
109             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
110                          p_counter->i_samples, p_new );
111         }
112         if( p_counter->i_samples == 1 )
113         {
114             if( p_counter->i_type == VLC_VAR_STRING &&
115                 p_counter->pp_samples[0]->value.psz_string )
116             {
117                 free( p_counter->pp_samples[0]->value.psz_string );
118             }
119             p_counter->pp_samples[0]->value = val;
120         }
121         break;
122     case STATS_COUNTER:
123         if( p_counter->i_samples > 1)
124         {
125             msg_Err( p_handler, "LAST counter has several samples !" );
126             return VLC_EGENERIC;
127         }
128         if( p_counter->i_samples == 0 )
129         {
130             counter_sample_t *p_new = (counter_sample_t*)malloc(
131                                                sizeof( counter_sample_t ) );
132             p_new->value.psz_string = NULL;
133
134             INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
135                          p_counter->i_samples, p_new );
136         }
137         if( p_counter->i_samples == 1 )
138         {
139             switch( p_counter->i_type )
140             {
141             case VLC_VAR_INTEGER:
142             case VLC_VAR_FLOAT:
143                 p_counter->pp_samples[0]->value.i_int += val.i_int;
144                 break;
145             default:
146                 msg_Err( p_handler, "Trying to increment invalid variable %s",
147                          p_counter->psz_name );
148                 return VLC_EGENERIC;
149             }
150         }
151         break;
152     }
153     return VLC_SUCCESS;
154 }
155
156
157 static counter_t *stats_GetCounter( stats_handler_t *p_handler, int i_object_id,
158                                     char *psz_name )
159 {
160     int i;
161     for( i = 0; i< p_handler->i_counters; i++ )
162     {
163         counter_t *p_counter = p_handler->pp_counters[i];
164         if( p_counter->i_source_object == i_object_id &&
165             !strcmp( p_counter->psz_name, psz_name ) )
166         {
167             return p_counter;
168         }
169     }
170     return NULL;
171 }
172
173 static stats_handler_t *stats_HandlerGet( vlc_object_t *p_this )
174 {
175     stats_handler_t *p_handler = (stats_handler_t*)
176                           vlc_object_find( p_this->p_vlc, VLC_OBJECT_STATS,
177                                            FIND_ANYWHERE );
178     if( !p_handler )
179     {
180         p_handler = stats_HandlerCreate( p_this );
181         if( !p_handler )
182         {
183             return NULL;
184         }
185         vlc_object_yield( p_handler );
186     }
187     return p_handler;
188 }
189
190 /**
191  * Initialize statistics handler
192  *
193  * This function initializes the global statistics handler singleton,
194  * \param p_this the parent VLC object
195  */
196 static stats_handler_t* stats_HandlerCreate( vlc_object_t *p_this )
197 {
198     stats_handler_t *p_handler;
199
200     msg_Dbg( p_this, "creating statistics handler" );
201
202     p_handler = (stats_handler_t*) vlc_object_create( p_this,
203                                                       VLC_OBJECT_STATS );
204
205     if( !p_handler )
206     {
207         msg_Err( p_this, "out of memory" );
208         return NULL;
209     }
210     p_handler->i_counters = 0;
211     p_handler->pp_counters = NULL;
212
213     /// \bug is it p_vlc or p_libvlc ?
214     vlc_object_attach( p_handler, p_this->p_vlc );
215
216     return p_handler;
217 }
218