access: http: only warn on deflate errors
[vlc.git] / src / input / stats.c
blobba13f0558cb84b82419a77eecf7acb27f5fc3c26
1 /*****************************************************************************
2 * stats.c: Statistics handling
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include "input/input_internal.h"
31 /**
32 * Create a statistics counter
33 * \param i_compute_type the aggregation type. One of STATS_LAST (always
34 * keep the last value), STATS_COUNTER (increment by the passed value),
35 * STATS_MAX (keep the maximum passed value), STATS_MIN, or STATS_DERIVATIVE
36 * (keep a time derivative of the value)
38 counter_t * stats_CounterCreate( int i_compute_type )
40 counter_t *p_counter = (counter_t*) malloc( sizeof( counter_t ) ) ;
42 if( !p_counter ) return NULL;
43 p_counter->i_compute_type = i_compute_type;
44 p_counter->i_samples = 0;
45 p_counter->pp_samples = NULL;
47 p_counter->last_update = 0;
49 return p_counter;
52 static inline int64_t stats_GetTotal(const counter_t *counter)
54 if (counter == NULL || counter->i_samples == 0)
55 return 0;
56 return counter->pp_samples[0]->value;
59 static inline float stats_GetRate(const counter_t *counter)
61 if (counter == NULL || counter->i_samples < 2)
62 return 0.;
64 return (counter->pp_samples[0]->value - counter->pp_samples[1]->value)
65 / (float)(counter->pp_samples[0]->date - counter->pp_samples[1]->date);
68 input_stats_t *stats_NewInputStats( input_thread_t *p_input )
70 (void)p_input;
71 input_stats_t *p_stats = calloc( 1, sizeof(input_stats_t) );
72 if( !p_stats )
73 return NULL;
75 vlc_mutex_init( &p_stats->lock );
76 stats_ReinitInputStats( p_stats );
78 return p_stats;
81 void stats_ComputeInputStats(input_thread_t *input, input_stats_t *st)
83 if (!libvlc_stats(input))
84 return;
86 vlc_mutex_lock(&input->p->counters.counters_lock);
87 vlc_mutex_lock(&st->lock);
89 /* Input */
90 st->i_read_packets = stats_GetTotal(input->p->counters.p_read_packets);
91 st->i_read_bytes = stats_GetTotal(input->p->counters.p_read_bytes);
92 st->f_input_bitrate = stats_GetRate(input->p->counters.p_input_bitrate);
93 st->i_demux_read_bytes = stats_GetTotal(input->p->counters.p_demux_read);
94 st->f_demux_bitrate = stats_GetRate(input->p->counters.p_demux_bitrate);
95 st->i_demux_corrupted = stats_GetTotal(input->p->counters.p_demux_corrupted);
96 st->i_demux_discontinuity = stats_GetTotal(input->p->counters.p_demux_discontinuity);
98 /* Decoders */
99 st->i_decoded_video = stats_GetTotal(input->p->counters.p_decoded_video);
100 st->i_decoded_audio = stats_GetTotal(input->p->counters.p_decoded_audio);
102 /* Sout */
103 if (input->p->counters.p_sout_send_bitrate)
105 st->i_sent_packets = stats_GetTotal(input->p->counters.p_sout_sent_packets);
106 st->i_sent_bytes = stats_GetTotal(input->p->counters.p_sout_sent_bytes);
107 st->f_send_bitrate = stats_GetRate(input->p->counters.p_sout_send_bitrate);
110 /* Aout */
111 st->i_played_abuffers = stats_GetTotal(input->p->counters.p_played_abuffers);
112 st->i_lost_abuffers = stats_GetTotal(input->p->counters.p_lost_abuffers);
114 /* Vouts */
115 st->i_displayed_pictures = stats_GetTotal(input->p->counters.p_displayed_pictures);
116 st->i_lost_pictures = stats_GetTotal(input->p->counters.p_lost_pictures);
118 vlc_mutex_unlock(&st->lock);
119 vlc_mutex_unlock(&input->p->counters.counters_lock);
122 void stats_ReinitInputStats( input_stats_t *p_stats )
124 vlc_mutex_lock( &p_stats->lock );
125 p_stats->i_read_packets = p_stats->i_read_bytes =
126 p_stats->f_input_bitrate = p_stats->f_average_input_bitrate =
127 p_stats->i_demux_read_packets = p_stats->i_demux_read_bytes =
128 p_stats->f_demux_bitrate = p_stats->f_average_demux_bitrate =
129 p_stats->i_demux_corrupted = p_stats->i_demux_discontinuity =
130 p_stats->i_displayed_pictures = p_stats->i_lost_pictures =
131 p_stats->i_played_abuffers = p_stats->i_lost_abuffers =
132 p_stats->i_decoded_video = p_stats->i_decoded_audio =
133 p_stats->i_sent_bytes = p_stats->i_sent_packets = p_stats->f_send_bitrate
134 = 0;
135 vlc_mutex_unlock( &p_stats->lock );
138 void stats_CounterClean( counter_t *p_c )
140 if( p_c )
142 int i = p_c->i_samples - 1 ;
143 while( i >= 0 )
145 counter_sample_t *p_s = p_c->pp_samples[i];
146 REMOVE_ELEM( p_c->pp_samples, p_c->i_samples, i );
147 free( p_s );
148 i--;
150 free( p_c );
155 /** Update a counter element with new values
156 * \param p_counter the counter to update
157 * \param val the vlc_value union containing the new value to aggregate. For
158 * more information on how data is aggregated, \see stats_Create
159 * \param val_new a pointer that will be filled with new data
161 void stats_Update( counter_t *p_counter, uint64_t val, uint64_t *new_val )
163 if( !p_counter )
164 return;
166 switch( p_counter->i_compute_type )
168 case STATS_DERIVATIVE:
170 counter_sample_t *p_new, *p_old;
171 mtime_t now = mdate();
172 if( now - p_counter->last_update < CLOCK_FREQ )
173 return;
174 p_counter->last_update = now;
175 /* Insert the new one at the beginning */
176 p_new = (counter_sample_t*)malloc( sizeof( counter_sample_t ) );
177 p_new->value = val;
178 p_new->date = p_counter->last_update;
179 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
180 0, p_new );
182 if( p_counter->i_samples == 3 )
184 p_old = p_counter->pp_samples[2];
185 REMOVE_ELEM( p_counter->pp_samples, p_counter->i_samples, 2 );
186 free( p_old );
188 break;
190 case STATS_COUNTER:
191 if( p_counter->i_samples == 0 )
193 counter_sample_t *p_new = (counter_sample_t*)malloc(
194 sizeof( counter_sample_t ) );
195 p_new->value = 0;
197 INSERT_ELEM( p_counter->pp_samples, p_counter->i_samples,
198 p_counter->i_samples, p_new );
200 if( p_counter->i_samples == 1 )
202 p_counter->pp_samples[0]->value += val;
203 if( new_val )
204 *new_val = p_counter->pp_samples[0]->value;
206 break;