1 /*****************************************************************************
2 * stats.c: Statistics handling
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
6 * Authors: Clément Stenac <zorglub@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
30 #include <vlc_common.h>
31 #include "input/input_internal.h"
34 * Create a statistics counter
36 static void input_rate_Init(input_rate_t
*rate
)
38 vlc_mutex_init(&rate
->lock
);
41 rate
->samples
[0].date
= VLC_TICK_INVALID
;
42 rate
->samples
[1].date
= VLC_TICK_INVALID
;
45 static float stats_GetRate(const input_rate_t
*rate
)
47 if (rate
->samples
[1].date
== VLC_TICK_INVALID
)
50 return (rate
->samples
[0].value
- rate
->samples
[1].value
)
51 / (float)(rate
->samples
[0].date
- rate
->samples
[1].date
);
54 struct input_stats
*input_stats_Create(void)
56 struct input_stats
*stats
= malloc(sizeof (*stats
));
57 if (unlikely(stats
== NULL
))
60 input_rate_Init(&stats
->input_bitrate
);
61 input_rate_Init(&stats
->demux_bitrate
);
62 atomic_init(&stats
->demux_corrupted
, 0);
63 atomic_init(&stats
->demux_discontinuity
, 0);
64 atomic_init(&stats
->decoded_audio
, 0);
65 atomic_init(&stats
->decoded_video
, 0);
66 atomic_init(&stats
->played_abuffers
, 0);
67 atomic_init(&stats
->lost_abuffers
, 0);
68 atomic_init(&stats
->displayed_pictures
, 0);
69 atomic_init(&stats
->late_pictures
, 0);
70 atomic_init(&stats
->lost_pictures
, 0);
74 void input_stats_Destroy(struct input_stats
*stats
)
79 void input_stats_Compute(struct input_stats
*stats
, input_stats_t
*st
)
82 vlc_mutex_lock(&stats
->input_bitrate
.lock
);
83 st
->i_read_packets
= stats
->input_bitrate
.updates
;
84 st
->i_read_bytes
= stats
->input_bitrate
.value
;
85 st
->f_input_bitrate
= stats_GetRate(&stats
->input_bitrate
);
86 vlc_mutex_unlock(&stats
->input_bitrate
.lock
);
88 vlc_mutex_lock(&stats
->demux_bitrate
.lock
);
89 st
->i_demux_read_bytes
= stats
->demux_bitrate
.value
;
90 st
->f_demux_bitrate
= stats_GetRate(&stats
->demux_bitrate
);
91 vlc_mutex_unlock(&stats
->demux_bitrate
.lock
);
92 st
->i_demux_corrupted
= atomic_load_explicit(&stats
->demux_corrupted
,
93 memory_order_relaxed
);
94 st
->i_demux_discontinuity
= atomic_load_explicit(
95 &stats
->demux_discontinuity
, memory_order_relaxed
);
98 st
->i_decoded_audio
= atomic_load_explicit(&stats
->decoded_audio
,
99 memory_order_relaxed
);
100 st
->i_played_abuffers
= atomic_load_explicit(&stats
->played_abuffers
,
101 memory_order_relaxed
);
102 st
->i_lost_abuffers
= atomic_load_explicit(&stats
->lost_abuffers
,
103 memory_order_relaxed
);
106 st
->i_decoded_video
= atomic_load_explicit(&stats
->decoded_video
,
107 memory_order_relaxed
);
108 st
->i_displayed_pictures
= atomic_load_explicit(&stats
->displayed_pictures
,
109 memory_order_relaxed
);
110 st
->i_late_pictures
= atomic_load_explicit(&stats
->late_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
)
123 vlc_mutex_lock(&counter
->lock
);
125 counter
->value
+= val
;
127 /* Ignore samples within a second of another */
128 vlc_tick_t now
= vlc_tick_now();
129 if (counter
->samples
[0].date
!= VLC_TICK_INVALID
130 && (now
- counter
->samples
[0].date
) < VLC_TICK_FROM_SEC(1))
132 vlc_mutex_unlock(&counter
->lock
);
136 memcpy(counter
->samples
+ 1, counter
->samples
,
137 sizeof (counter
->samples
[0]));
139 counter
->samples
[0].value
= counter
->value
;
140 counter
->samples
[0].date
= now
;
141 vlc_mutex_unlock(&counter
->lock
);