samples: net: echo-server: Print receive statistics periodically

Print receive statistics of how many KiB we receive / sec.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2020-11-10 15:00:49 +02:00 committed by Jukka Rissanen
commit 533aaed7a0
3 changed files with 65 additions and 0 deletions

View file

@ -21,6 +21,7 @@
#endif
#define RECV_BUFFER_SIZE 1280
#define STATS_TIMER 60 /* How often to print statistics (in seconds) */
#if defined(CONFIG_USERSPACE)
#include <app_memory/app_memdomain.h>
@ -40,10 +41,14 @@ struct data {
int sock;
char recv_buffer[RECV_BUFFER_SIZE];
uint32_t counter;
atomic_t bytes_received;
struct k_delayed_work stats_print;
} udp;
struct {
int sock;
atomic_t bytes_received;
struct k_delayed_work stats_print;
struct {
int sock;

View file

@ -146,6 +146,8 @@ static void handle_data(void *ptr1, void *ptr2, void *ptr3)
errno);
ret = -errno;
break;
} else {
atomic_add(&data->tcp.bytes_received, received);
}
offset += received;
@ -303,6 +305,9 @@ static void process_tcp4(void)
return;
}
k_delayed_work_submit(&conf.ipv4.tcp.stats_print,
K_SECONDS(STATS_TIMER));
while (ret == 0) {
ret = process_tcp(&conf.ipv4);
if (ret < 0) {
@ -329,6 +334,9 @@ static void process_tcp6(void)
return;
}
k_delayed_work_submit(&conf.ipv6.tcp.stats_print,
K_SECONDS(STATS_TIMER));
while (ret == 0) {
ret = process_tcp(&conf.ipv6);
if (ret != 0) {
@ -339,6 +347,26 @@ static void process_tcp6(void)
quit();
}
static void print_stats(struct k_work *work)
{
struct data *data = CONTAINER_OF(work, struct data, tcp.stats_print);
int total_received = atomic_get(&data->tcp.bytes_received);
if (total_received) {
if ((total_received / STATS_TIMER) < 1024) {
LOG_INF("%s TCP: Received %d B/sec", data->proto,
total_received / STATS_TIMER);
} else {
LOG_INF("%s TCP: Received %d KiB/sec", data->proto,
total_received / 1024 / STATS_TIMER);
}
atomic_set(&data->tcp.bytes_received, 0);
}
k_delayed_work_submit(&data->tcp.stats_print, K_SECONDS(STATS_TIMER));
}
void start_tcp(void)
{
int i;
@ -367,6 +395,7 @@ void start_tcp(void)
}
#endif
k_delayed_work_init(&conf.ipv6.tcp.stats_print, print_stats);
k_thread_start(tcp6_thread_id);
#endif
@ -382,6 +411,7 @@ void start_tcp(void)
}
#endif
k_delayed_work_init(&conf.ipv4.tcp.stats_print, print_stats);
k_thread_start(tcp4_thread_id);
#endif
}

View file

@ -115,6 +115,8 @@ static int process_udp(struct data *data)
errno);
ret = -errno;
break;
} else if (received) {
atomic_add(&data->udp.bytes_received, received);
}
ret = sendto(data->udp.sock, data->udp.recv_buffer, received, 0,
@ -154,6 +156,9 @@ static void process_udp4(void)
return;
}
k_delayed_work_submit(&conf.ipv4.udp.stats_print,
K_SECONDS(STATS_TIMER));
while (ret == 0) {
ret = process_udp(&conf.ipv4);
if (ret < 0) {
@ -178,6 +183,9 @@ static void process_udp6(void)
return;
}
k_delayed_work_submit(&conf.ipv6.udp.stats_print,
K_SECONDS(STATS_TIMER));
while (ret == 0) {
ret = process_udp(&conf.ipv6);
if (ret < 0) {
@ -186,6 +194,26 @@ static void process_udp6(void)
}
}
static void print_stats(struct k_work *work)
{
struct data *data = CONTAINER_OF(work, struct data, udp.stats_print);
int total_received = atomic_get(&data->udp.bytes_received);
if (total_received) {
if ((total_received / STATS_TIMER) < 1024) {
LOG_INF("%s UDP: Received %d B/sec", data->proto,
total_received / STATS_TIMER);
} else {
LOG_INF("%s UDP: Received %d KiB/sec", data->proto,
total_received / 1024 / STATS_TIMER);
}
atomic_set(&data->udp.bytes_received, 0);
}
k_delayed_work_submit(&data->udp.stats_print, K_SECONDS(STATS_TIMER));
}
void start_udp(void)
{
if (IS_ENABLED(CONFIG_NET_IPV6)) {
@ -193,6 +221,7 @@ void start_udp(void)
k_mem_domain_add_thread(&app_domain, udp6_thread_id);
#endif
k_delayed_work_init(&conf.ipv6.udp.stats_print, print_stats);
k_thread_name_set(udp6_thread_id, "udp6");
k_thread_start(udp6_thread_id);
}
@ -202,6 +231,7 @@ void start_udp(void)
k_mem_domain_add_thread(&app_domain, udp4_thread_id);
#endif
k_delayed_work_init(&conf.ipv4.udp.stats_print, print_stats);
k_thread_name_set(udp4_thread_id, "udp4");
k_thread_start(udp4_thread_id);
}