stats: replace packet statistic counters with libosmocore rate counters

In struct mgcp_rtp_end one finds unsigned int counters. Those should
be replaced with libosmocore rate counters

- replace packets_rx, octets_rx, packets_tx, octets_tx and
  dropped_packets with libosmocore rate counters.

Change-Id: I47c5c9006df5044e59ddebb895e62adb849d72d5
Related: OS#2517
This commit is contained in:
Philipp Maier 2018-07-03 14:14:21 +02:00
parent 337209a6ea
commit cede2a4b7c
9 changed files with 86 additions and 55 deletions

View File

@ -27,6 +27,17 @@
#include <osmocom/core/linuxlist.h>
#include <inttypes.h>
/* RTP connection related counters */
enum {
IN_STREAM_ERR_TSTMP_CTR,
OUT_STREAM_ERR_TSTMP_CTR,
RTP_PACKETS_RX_CTR,
RTP_OCTETS_RX_CTR,
RTP_PACKETS_TX_CTR,
RTP_OCTETS_TX_CTR,
RTP_DROPPED_PACKETS_CTR
};
struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
enum mgcp_conn_type type, char *name);
struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id);

View File

@ -99,15 +99,6 @@ struct mgcp_rtp_codec {
/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
struct mgcp_rtp_end {
/* statistics */
struct {
unsigned int packets_rx;
unsigned int octets_rx;
unsigned int packets_tx;
unsigned int octets_tx;
unsigned int dropped_packets;
} stats;
/* local IP address of the RTP socket */
struct in_addr addr;

View File

@ -30,8 +30,7 @@
void mgcp_format_stats(char *str, size_t str_len, struct mgcp_conn *conn);
/* Exposed for test purposes only, do not use actively */
void calc_loss(struct mgcp_rtp_state *s, struct mgcp_rtp_end *,
uint32_t *expected, int *loss);
void calc_loss(struct mgcp_conn_rtp *conn, uint32_t *expected, int *loss);
/* Exposed for test purposes only, do not use actively */
uint32_t calc_jitter(struct mgcp_rtp_state *);

View File

@ -31,22 +31,21 @@
#include <osmocom/core/rate_ctr.h>
#include <ctype.h>
enum {
IN_STREAM_ERR_TSTMP_CTR,
OUT_STREAM_ERR_TSTMP_CTR,
};
static const struct rate_ctr_desc rate_ctr_desc[] = {
[IN_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:in", "Inbound rtp-stream timestamp errors."},
[OUT_STREAM_ERR_TSTMP_CTR] = {"stream_err_tstmp:out", "Outbound rtp-stream timestamp errors."},
[RTP_PACKETS_RX_CTR] = {"rtp:packets_rx", "Inbound rtp packets."},
[RTP_OCTETS_RX_CTR] = {"rtp:octets_rx", "Inbound rtp octets."},
[RTP_PACKETS_TX_CTR] = {"rtp:packets_tx", "Outbound rtp packets."},
[RTP_OCTETS_TX_CTR] = {"rtp:octets_rx", "Outbound rtp octets."},
[RTP_DROPPED_PACKETS_CTR] = {"rtp:dropped", "dropped rtp packets."}
};
const static struct rate_ctr_group_desc rate_ctr_group_desc = {
.group_name_prefix = "conn_rtp",
.group_description = "rtp connection statistics",
.class_id = 1,
.num_ctr = 2,
.num_ctr = ARRAY_SIZE(rate_ctr_desc),
.ctr_desc = rate_ctr_desc
};
@ -107,7 +106,6 @@ static void mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn
end->rtp.fd = -1;
end->rtcp.fd = -1;
memset(&end->stats, 0, sizeof(end->stats));
end->rtp_port = end->rtcp_port = 0;
talloc_free(end->fmtp_extra);
end->fmtp_extra = NULL;

View File

@ -694,7 +694,7 @@ int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
dest_name = conn_dst->conn->name;
if (!rtp_end->output_enabled) {
rtp_end->stats.dropped_packets += 1;
rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR]);
LOGP(DRTP, LOGL_DEBUG,
"endpoint:0x%x output disabled, drop to %s %s "
"rtp_port:%u rtcp_port:%u\n",
@ -749,8 +749,8 @@ int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
if (len <= 0)
return len;
conn_dst->end.stats.packets_tx += 1;
conn_dst->end.stats.octets_tx += len;
rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
nbytes += len;
buflen = cont;
@ -769,8 +769,8 @@ int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
&rtp_end->addr,
rtp_end->rtcp_port, buf, len);
conn_dst->end.stats.packets_tx += 1;
conn_dst->end.stats.octets_tx += len;
rate_ctr_inc(&conn_dst->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
rate_ctr_add(&conn_dst->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], len);
return len;
}
@ -939,8 +939,8 @@ static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
}
/* Increment RX statistics */
conn->end.stats.packets_rx += 1;
conn->end.stats.octets_rx += rc;
rate_ctr_inc(&conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR]);
rate_ctr_add(&conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR], rc);
/* Forward a copy of the RTP data to a debug ip/port */
forward_data(fd->fd, &conn->tap_in, buf, rc);

View File

@ -256,8 +256,8 @@ static void scheduled_tx_net_cb(struct msgb *msg, void *data)
.sin_port = conn_net->end.rtp_port,
};
conn_bts->end.stats.octets_tx += msg->len;
conn_bts->end.stats.packets_tx++;
rate_ctr_inc(&conn_bts->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
rate_ctr_add(&conn_bts->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], msg->len);
/* Send RTP data to NET */
/* FIXME: Get rid of conn_bts and conn_net! */
@ -283,8 +283,8 @@ static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
.sin_port = conn_bts->end.rtp_port,
};
conn_net->end.stats.octets_tx += msg->len;
conn_net->end.stats.packets_tx++;
rate_ctr_inc(&conn_net->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR]);
rate_ctr_add(&conn_net->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR], msg->len);
/* Send RTP data to BTS */
/* FIXME: Get rid of conn_bts and conn_net! */

View File

@ -27,10 +27,11 @@
#include <limits.h>
/* Helper function for mgcp_format_stats_rtp() to calculate packet loss */
void calc_loss(struct mgcp_rtp_state *state,
struct mgcp_rtp_end *end, uint32_t *expected,
int *loss)
void calc_loss(struct mgcp_conn_rtp *conn, uint32_t *expected, int *loss)
{
struct mgcp_rtp_state *state = &conn->state;
struct rate_ctr *packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
*expected = state->stats.cycles + state->stats.max_seq;
*expected = *expected - state->stats.base_seq + 1;
@ -44,8 +45,8 @@ void calc_loss(struct mgcp_rtp_state *state,
* Make sure the sign is correct and use the biggest
* positive/negative number that fits.
*/
*loss = *expected - end->stats.packets_rx;
if (*expected < end->stats.packets_rx) {
*loss = *expected - packets_rx->current;
if (*expected < packets_rx->current) {
if (*loss > 0)
*loss = INT_MIN;
} else {
@ -70,13 +71,18 @@ static void mgcp_format_stats_rtp(char *str, size_t str_len,
int ploss;
int nchars;
calc_loss(&conn->state, &conn->end, &expected, &ploss);
struct rate_ctr *packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
struct rate_ctr *octets_rx = &conn->rate_ctr_group->ctr[RTP_OCTETS_RX_CTR];
struct rate_ctr *packets_tx = &conn->rate_ctr_group->ctr[RTP_PACKETS_TX_CTR];
struct rate_ctr *octets_tx = &conn->rate_ctr_group->ctr[RTP_OCTETS_TX_CTR];
calc_loss(conn, &expected, &ploss);
jitter = calc_jitter(&conn->state);
nchars = snprintf(str, str_len,
"\r\nP: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
conn->end.stats.packets_tx, conn->end.stats.octets_tx,
conn->end.stats.packets_rx, conn->end.stats.octets_rx,
"\r\nP: PS=%lu, OS=%lu, PR=%lu, OR=%lu, PL=%d, JI=%u",
packets_tx->current, octets_tx->current,
packets_rx->current, octets_rx->current,
ploss, jitter);
if (nchars < 0 || nchars >= str_len)
goto truncate;

View File

@ -154,14 +154,18 @@ static int config_write_mgcp(struct vty *vty)
return CMD_SUCCESS;
}
static void dump_rtp_end(struct vty *vty, struct mgcp_rtp_state *state,
struct mgcp_rtp_end *end)
static void dump_rtp_end(struct vty *vty, struct mgcp_conn_rtp *conn)
{
struct mgcp_rtp_state *state = &conn->state;
struct mgcp_rtp_end *end = &conn->end;
struct mgcp_rtp_codec *codec = end->codec;
struct rate_ctr *dropped_packets;
dropped_packets = &conn->rate_ctr_group->ctr[RTP_DROPPED_PACKETS_CTR];
vty_out(vty,
" Timestamp Errs: %lu->%lu%s"
" Dropped Packets: %d%s"
" Dropped Packets: %lu%s"
" Payload Type: %d Rate: %u Channels: %d %s"
" Frame Duration: %u Frame Denominator: %u%s"
" FPP: %d Packet Duration: %u%s"
@ -170,7 +174,7 @@ static void dump_rtp_end(struct vty *vty, struct mgcp_rtp_state *state,
state->in_stream.err_ts_ctr->current,
state->out_stream.err_ts_ctr->current,
VTY_NEWLINE,
end->stats.dropped_packets, VTY_NEWLINE,
dropped_packets->current, VTY_NEWLINE,
codec->payload_type, codec->rate, codec->channels, VTY_NEWLINE,
codec->frame_duration_num, codec->frame_duration_den,
VTY_NEWLINE, end->frames_per_packet, end->packet_duration_ms,
@ -208,8 +212,7 @@ static void dump_trunk(struct vty *vty, struct mgcp_trunk_config *cfg,
* connection types (E1) as soon as
* the implementation is available */
if (conn->type == MGCP_CONN_TYPE_RTP) {
dump_rtp_end(vty, &conn->u.rtp.state,
&conn->u.rtp.end);
dump_rtp_end(vty, &conn->u.rtp);
}
}
}

View File

@ -937,23 +937,43 @@ static const struct pl_test pl_test_dat[] = {
static void test_packet_loss_calc(void)
{
int i;
struct mgcp_endpoint endp;
struct mgcp_trunk_config trunk;
printf("Testing packet loss calculation.\n");
memset(&endp, 0, sizeof(endp));
memset(&trunk, 0, sizeof(trunk));
endp.type = &ep_typeset.rtp;
trunk.vty_number_endpoints = 1;
trunk.endpoints = &endp;
endp.tcfg = &trunk;
INIT_LLIST_HEAD(&endp.conns);
for (i = 0; i < ARRAY_SIZE(pl_test_dat); ++i) {
uint32_t expected;
int loss;
struct mgcp_rtp_state state;
struct mgcp_rtp_end rtp;
memset(&state, 0, sizeof(state));
memset(&rtp, 0, sizeof(rtp));
state.stats.initialized = 1;
state.stats.base_seq = pl_test_dat[i].base_seq;
state.stats.max_seq = pl_test_dat[i].max_seq;
state.stats.cycles = pl_test_dat[i].cycles;
struct mgcp_conn_rtp *conn = NULL;
struct mgcp_conn *_conn = NULL;
struct mgcp_rtp_state *state;
struct rate_ctr *packets_rx;
rtp.stats.packets_rx = pl_test_dat[i].packets;
calc_loss(&state, &rtp, &expected, &loss);
_conn =
mgcp_conn_alloc(NULL, &endp, MGCP_CONN_TYPE_RTP,
"test-connection");
conn = mgcp_conn_get_rtp(&endp, _conn->id);
state = &conn->state;
packets_rx = &conn->rate_ctr_group->ctr[RTP_PACKETS_RX_CTR];
state->stats.initialized = 1;
state->stats.base_seq = pl_test_dat[i].base_seq;
state->stats.max_seq = pl_test_dat[i].max_seq;
state->stats.cycles = pl_test_dat[i].cycles;
packets_rx->current = pl_test_dat[i].packets;
calc_loss(conn, &expected, &loss);
if (loss != pl_test_dat[i].loss
|| expected != pl_test_dat[i].expected) {
@ -962,7 +982,10 @@ static void test_packet_loss_calc(void)
i, loss, pl_test_dat[i].loss, expected,
pl_test_dat[i].expected);
}
mgcp_conn_free_all(&endp);
}
}
int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,