Skip to content

zperf time handling fixes #30237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions samples/net/zperf/src/zperf_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,6 @@

#define PACKET_SIZE_MAX 1024

#define HW_CYCLES_TO_USEC(__hw_cycle__) \
( \
((uint64_t)(__hw_cycle__) * (uint64_t)USEC_PER_SEC) / \
((uint64_t)sys_clock_hw_cycles_per_sec()) \
)

#define HW_CYCLES_TO_SEC(__hw_cycle__) \
( \
((uint64_t)(HW_CYCLES_TO_USEC(__hw_cycle__))) / \
((uint64_t)USEC_PER_SEC) \
)

#define USEC_TO_HW_CYCLES(__usec__) \
( \
((uint64_t)(__usec__) * (uint64_t)sys_clock_hw_cycles_per_sec()) / \
((uint64_t)USEC_PER_SEC) \
)

#define SEC_TO_HW_CYCLES(__sec__) \
USEC_TO_HW_CYCLES((uint64_t)(__sec__) * \
(uint64_t)USEC_PER_SEC)

#define MSEC_TO_HW_CYCLES(__msec__) \
USEC_TO_HW_CYCLES((uint64_t)(__msec__) * \
(uint64_t)MSEC_PER_SEC)

struct zperf_udp_datagram {
int32_t id;
uint32_t tv_sec;
Expand Down
2 changes: 1 addition & 1 deletion samples/net/zperf/src/zperf_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct session {
uint32_t outorder;
uint32_t error;
uint64_t length;
uint32_t start_time;
int64_t start_time;
uint32_t last_time;
int32_t jitter;
int32_t last_transit_time;
Expand Down
23 changes: 13 additions & 10 deletions samples/net/zperf/src/zperf_tcp_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ static void tcp_received(struct net_context *context,
{
const struct shell *shell = tcp_shell;
struct session *session;
uint32_t time;
int64_t time;

if (!shell) {
printk("Shell is not set!\n");
return;
}

time = k_cycle_get_32();
time = k_uptime_ticks();

session = get_tcp_session(context);
if (!session) {
Expand All @@ -63,7 +63,7 @@ static void tcp_received(struct net_context *context,
shell_fprintf(shell, SHELL_NORMAL,
"New TCP session started\n");
zperf_reset_session_stats(session);
session->start_time = k_cycle_get_32();
session->start_time = k_uptime_ticks();
session->state = STATE_ONGOING;
__fallthrough;
case STATE_ONGOING:
Expand All @@ -75,18 +75,19 @@ static void tcp_received(struct net_context *context,

if (pkt == NULL && status == 0) { /* EOF */
uint32_t rate_in_kbps;
uint32_t duration = HW_CYCLES_TO_USEC(
time_delta(session->start_time, time));
uint32_t duration;

duration = k_ticks_to_us_ceil32(time -
session->start_time);

session->state = STATE_COMPLETED;

/* Compute baud rate */
if (duration != 0U) {
rate_in_kbps = (uint32_t)
(((uint64_t)session->length *
(uint64_t)8 *
((session->length * 8ULL *
(uint64_t)USEC_PER_SEC) /
((uint64_t)duration * 1024U));
((uint64_t)duration * 1024ULL));
} else {
rate_in_kbps = 0U;
}
Expand Down Expand Up @@ -177,9 +178,10 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv4\n");
return;
goto use_existing_ipv4;
}
} else {
use_existing_ipv4:
/* Use existing IP */
in4_addr = zperf_get_default_if_in4_addr();
if (!in4_addr) {
Expand Down Expand Up @@ -214,9 +216,10 @@ void zperf_tcp_receiver_init(const struct shell *shell, int port)
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv6\n");
return;
goto use_existing_ipv6;
}
} else {
use_existing_ipv6:
/* Use existing IP */
in6_addr = zperf_get_default_if_in6_addr();
if (!in6_addr) {
Expand Down
28 changes: 8 additions & 20 deletions samples/net/zperf/src/zperf_tcp_uploader.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ void zperf_tcp_upload(const struct shell *shell,
unsigned int packet_size,
struct zperf_results *results)
{
uint32_t duration = MSEC_TO_HW_CYCLES(duration_in_ms);
int64_t duration = z_timeout_end_calc(K_MSEC(duration_in_ms));
int64_t start_time, last_print_time, end_time, remaining;
uint32_t nb_packets = 0U, nb_errors = 0U;
uint32_t start_time, last_print_time, end_time;
uint8_t time_elapsed = 0U, finished = 0U;
uint32_t alloc_errors = 0U;

if (packet_size > PACKET_SIZE_MAX) {
Expand All @@ -41,7 +40,7 @@ void zperf_tcp_upload(const struct shell *shell,
}

/* Start the loop */
start_time = k_cycle_get_32();
start_time = k_uptime_ticks();
last_print_time = start_time;

shell_fprintf(shell, SHELL_NORMAL,
Expand All @@ -57,10 +56,6 @@ void zperf_tcp_upload(const struct shell *shell,

do {
int ret = 0;
uint32_t loop_time;

/* Timestamps */
loop_time = k_cycle_get_32();

/* Send the packet */
ret = net_context_send(ctx, sample_packet,
Expand All @@ -87,30 +82,23 @@ void zperf_tcp_upload(const struct shell *shell,
}
} else {
nb_packets++;

if (time_elapsed) {
finished = 1U;
}
}

if (!time_elapsed && time_delta(start_time,
loop_time) > duration) {
time_elapsed = 1U;
}

#if defined(CONFIG_ARCH_POSIX)
k_busy_wait(100 * USEC_PER_MSEC);
#else
k_yield();
#endif
} while (!finished);

end_time = k_cycle_get_32();
remaining = duration - k_uptime_ticks();
} while (remaining > 0);

end_time = k_uptime_ticks();

/* Add result coming from the client */
results->nb_packets_sent = nb_packets;
results->client_time_in_us =
HW_CYCLES_TO_USEC(time_delta(start_time, end_time));
k_ticks_to_us_ceil32(end_time - start_time);
results->packet_size = packet_size;
results->nb_packets_errors = nb_errors;

Expand Down
27 changes: 15 additions & 12 deletions samples/net/zperf/src/zperf_udp_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static void udp_received(struct net_context *context,
struct zperf_udp_datagram *hdr;
struct session *session;
int32_t transit_time;
uint32_t time;
int64_t time;
int32_t id;

if (!pkt) {
Expand All @@ -139,7 +139,7 @@ static void udp_received(struct net_context *context,
goto out;
}

time = k_cycle_get_32();
time = k_uptime_ticks();

session = get_session(pkt, ip_hdr, proto_hdr, SESSION_UDP);
if (!session) {
Expand Down Expand Up @@ -180,17 +180,18 @@ static void udp_received(struct net_context *context,

shell_fprintf(shell, SHELL_NORMAL, "End of session!\n");

duration = HW_CYCLES_TO_USEC(
time_delta(session->start_time, time));
duration = k_ticks_to_us_ceil32(time -
session->start_time);

/* Update state machine */
session->state = STATE_COMPLETED;

/* Compute baud rate */
if (duration != 0U) {
rate_in_kbps = (uint32_t)
(((uint64_t)session->length * (uint64_t)8 *
((session->length * 8ULL *
(uint64_t)USEC_PER_SEC) /
((uint64_t)duration * 1024U));
((uint64_t)duration * 1024ULL));
} else {
rate_in_kbps = 0U;
}
Expand Down Expand Up @@ -246,10 +247,10 @@ static void udp_received(struct net_context *context,
session->length += net_pkt_remaining_data(pkt);

/* Compute jitter */
transit_time = time_delta(HW_CYCLES_TO_USEC(time),
ntohl(hdr->tv_sec) *
USEC_PER_SEC +
ntohl(hdr->tv_usec));
transit_time = time_delta(
k_ticks_to_us_ceil32(time),
ntohl(hdr->tv_sec) * USEC_PER_SEC +
ntohl(hdr->tv_usec));
if (session->last_transit_time != 0) {
int32_t delta_transit = transit_time -
session->last_transit_time;
Expand Down Expand Up @@ -318,9 +319,10 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv4\n");
return;
goto use_existing_ipv4;
}
} else {
use_existing_ipv4:
/* Use existing IP */
in4_addr = zperf_get_default_if_in4_addr();
if (!in4_addr) {
Expand Down Expand Up @@ -368,9 +370,10 @@ void zperf_udp_receiver_init(const struct shell *shell, int port)
if (ret < 0) {
shell_fprintf(shell, SHELL_WARNING,
"Unable to set IPv6\n");
return;
goto use_existing_ipv6;
}
} else {
use_existing_ipv6:
/* Use existing IP */
in6_addr = zperf_get_default_if_in6_addr();
if (!in6_addr) {
Expand Down
Loading