1 /*****************************************************************************
2 * stats.c: Statistics handling
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include "input/input_internal.h"
35 * Create a statistics counter
37 static void input_rate_Init(input_rate_t
*rate
)
39 vlc_mutex_init(&rate
->lock
);
42 rate
->samples
[0].date
= VLC_TICK_INVALID
;
43 rate
->samples
[1].date
= VLC_TICK_INVALID
;
46 static float stats_GetRate(const input_rate_t
*rate
)
48 if (rate
->samples
[1].date
== VLC_TICK_INVALID
)
51 return (rate
->samples
[0].value
- rate
->samples
[1].value
)
52 / (float)(rate
->samples
[0].date
- rate
->samples
[1].date
);
55 struct input_stats
*input_stats_Create(void)
57 struct input_stats
*stats
= malloc(sizeof (*stats
));
58 if (unlikely(stats
== NULL
))
61 input_rate_Init(&stats
->input_bitrate
);
62 input_rate_Init(&stats
->demux_bitrate
);
63 atomic_init(&stats
->demux_corrupted
, 0);
64 atomic_init(&stats
->demux_discontinuity
, 0);
65 atomic_init(&stats
->decoded_audio
, 0);
66 atomic_init(&stats
->decoded_video
, 0);
67 atomic_init(&stats
->played_abuffers
, 0);
68 atomic_init(&stats
->lost_abuffers
, 0);
69 atomic_init(&stats
->displayed_pictures
, 0);
70 atomic_init(&stats
->lost_pictures
, 0);
74 void input_stats_Destroy(struct input_stats
*stats
)
76 vlc_mutex_destroy(&stats
->demux_bitrate
.lock
);
77 vlc_mutex_destroy(&stats
->input_bitrate
.lock
);
81 void input_stats_Compute(struct input_stats
*stats
, input_stats_t
*st
)
84 vlc_mutex_lock(&stats
->input_bitrate
.lock
);
85 st
->i_read_packets
= stats
->input_bitrate
.updates
;
86 st
->i_read_bytes
= stats
->input_bitrate
.value
;
87 st
->f_input_bitrate
= stats_GetRate(&stats
->input_bitrate
);
88 vlc_mutex_unlock(&stats
->input_bitrate
.lock
);
90 vlc_mutex_lock(&stats
->demux_bitrate
.lock
);
91 st
->i_demux_read_bytes
= stats
->demux_bitrate
.value
;
92 st
->f_demux_bitrate
= stats_GetRate(&stats
->demux_bitrate
);
93 vlc_mutex_unlock(&stats
->demux_bitrate
.lock
);
94 st
->i_demux_corrupted
= atomic_load_explicit(&stats
->demux_corrupted
,
95 memory_order_relaxed
);
96 st
->i_demux_discontinuity
= atomic_load_explicit(
97 &stats
->demux_discontinuity
, memory_order_relaxed
);
100 st
->i_decoded_audio
= atomic_load_explicit(&stats
->decoded_audio
,
101 memory_order_relaxed
);
102 st
->i_played_abuffers
= atomic_load_explicit(&stats
->played_abuffers
,
103 memory_order_relaxed
);
104 st
->i_lost_abuffers
= atomic_load_explicit(&stats
->lost_abuffers
,
105 memory_order_relaxed
);
108 st
->i_decoded_video
= atomic_load_explicit(&stats
->decoded_video
,
109 memory_order_relaxed
);
110 st
->i_displayed_pictures
= atomic_load_explicit(&stats
->displayed_pictures
,
111 memory_order_relaxed
);
112 st
->i_lost_pictures
= atomic_load_explicit(&stats
->lost_pictures
,
113 memory_order_relaxed
);
116 /** Update a counter element with new values
117 * \param p_counter the counter to update
118 * \param val the vlc_value union containing the new value to aggregate. For
119 * more information on how data is aggregated, \see stats_Create
121 void input_rate_Add(input_rate_t
*counter
, uintmax_t val
)
124 counter
->value
+= val
;
126 /* Ignore samples within a second of another */
127 vlc_tick_t now
= vlc_tick_now();
128 if (counter
->samples
[0].date
!= VLC_TICK_INVALID
129 && (now
- counter
->samples
[0].date
) < VLC_TICK_FROM_SEC(1))
132 memcpy(counter
->samples
+ 1, counter
->samples
,
133 sizeof (counter
->samples
[0]));
135 counter
->samples
[0].value
= counter
->value
;
136 counter
->samples
[0].date
= now
;