spa: add spa_ratelimit

This commit is contained in:
Wim Taymans 2023-07-11 19:25:13 +02:00
parent 17bc9d520e
commit dc07c2321b
8 changed files with 76 additions and 69 deletions

View file

@ -0,0 +1,42 @@
/* Ratelimit */
/* SPDX-FileCopyrightText: Copyright © 2023 Wim Taymans */
/* SPDX-License-Identifier: MIT */
#ifndef SPA_RATELIMIT_H
#define SPA_RATELIMIT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
struct spa_ratelimit {
uint64_t interval;
uint64_t begin;
unsigned burst;
unsigned n_printed;
unsigned n_missed;
};
static inline int spa_ratelimit_test(struct spa_ratelimit *r, uint64_t now)
{
unsigned missed = 0;
if (r->begin + r->interval < now) {
missed = r->n_missed;
r->begin = now;
r->n_printed = 0;
r->n_missed = 0;
} else if (r->n_printed >= r->burst) {
r->n_missed++;
return -1;
}
r->n_printed++;
return missed;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_RATELIMIT_H */

View file

@ -1980,7 +1980,7 @@ static int get_avail(struct state *state, uint64_t current_time, snd_pcm_uframes
if ((res = alsa_recover(state, avail)) < 0)
return res;
if ((avail = snd_pcm_avail(state->hndl)) < 0) {
if ((missed = ratelimit_test(&state->rate_limit, current_time)) >= 0) {
if ((missed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) {
spa_log_warn(state->log, "%s: (%d missed) snd_pcm_avail after recover: %s",
state->props.device, missed, snd_strerror(avail));
}
@ -1997,7 +1997,7 @@ static int get_avail(struct state *state, uint64_t current_time, snd_pcm_uframes
uint64_t then;
if ((res = snd_pcm_htimestamp(state->hndl, &havail, &tstamp)) < 0) {
if ((missed = ratelimit_test(&state->rate_limit, current_time)) >= 0) {
if ((missed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) {
spa_log_warn(state->log, "%s: (%d missed) snd_pcm_htimestamp error: %s",
state->props.device, missed, snd_strerror(res));
}
@ -2025,7 +2025,7 @@ static int get_avail(struct state *state, uint64_t current_time, snd_pcm_uframes
state->htimestamp_error = 0;
state->htimestamp = false;
}
else if ((missed = ratelimit_test(&state->rate_limit, current_time)) >= 0) {
else if ((missed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) {
spa_log_warn(state->log, "%s: (%d missed) impossible htimestamp diff:%"PRIi64,
state->props.device, missed, diff);
}
@ -2253,7 +2253,7 @@ int spa_alsa_write(struct state *state)
else
lev = SPA_LOG_LEVEL_INFO;
if ((missed = ratelimit_test(&state->rate_limit, current_time)) >= 0) {
if ((missed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) {
spa_log_lev(state->log, lev, "%s: follower avail:%lu delay:%ld "
"target:%ld thr:%u, resync (%d missed)",
state->props.device, avail, delay,
@ -2490,7 +2490,7 @@ int spa_alsa_read(struct state *state)
else
lev = SPA_LOG_LEVEL_INFO;
if ((missed = ratelimit_test(&state->rate_limit, current_time)) >= 0) {
if ((missed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) {
spa_log_lev(state->log, lev, "%s: follower delay:%ld target:%ld thr:%u, "
"resync (%d missed)", state->props.device, delay,
target, state->threshold, missed);
@ -2743,7 +2743,7 @@ done:
if (!state->disable_tsched &&
(state->next_time > current_time + SPA_NSEC_PER_SEC ||
current_time > state->next_time + SPA_NSEC_PER_SEC)) {
if ((missed = ratelimit_test(&state->rate_limit, current_time)) >= 0) {
if ((missed = spa_ratelimit_test(&state->rate_limit, current_time)) >= 0) {
spa_log_error(state->log, "%s: impossible timeout %lu %lu %lu %"
PRIu64" %"PRIu64" %"PRIi64" %d %"PRIi64" (%d missed)",
state->props.device, avail, delay, target,

View file

@ -21,6 +21,7 @@ extern "C" {
#include <spa/utils/list.h>
#include <spa/utils/json.h>
#include <spa/utils/dll.h>
#include <spa/utils/ratelimit.h>
#include <spa/node/node.h>
#include <spa/node/utils.h>
@ -81,13 +82,6 @@ struct card {
uint32_t rate;
};
struct ratelimit {
uint64_t interval;
uint64_t begin;
unsigned burst;
unsigned n_printed, n_missed;
};
struct state {
struct spa_handle handle;
struct spa_node node;
@ -97,7 +91,7 @@ struct state {
struct spa_loop *data_loop;
FILE *log_file;
struct ratelimit rate_limit;
struct spa_ratelimit rate_limit;
uint32_t card_index;
struct card *card;
@ -348,22 +342,6 @@ static inline uint32_t spa_alsa_get_iec958_codecs(struct state *state, uint32_t
return i;
}
static inline int ratelimit_test(struct ratelimit *r, uint64_t now)
{
unsigned missed = 0;
if (r->begin + r->interval < now) {
missed = r->n_missed;
r->begin = now;
r->n_printed = 0;
r->n_missed = 0;
} else if (r->n_printed >= r->burst) {
r->n_missed++;
return -1;
}
r->n_printed++;
return missed;
}
/* This function is also as snd_pcm_channel_area_addr() since 1.2.6 which is not yet
* in ubuntu and I can't figure out how to do the ALSA version check. */
static inline void *channel_area_addr(const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset)