From 9405247990056939e1f62467f421d6cdbcff549c Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Wed, 3 Jan 2024 12:11:41 +0100 Subject: [PATCH 1/4] feat: switch z_clock to Windows QueryPerformanceCounter --- include/zenoh-pico/system/platform/windows.h | 2 +- src/system/windows/system.c | 46 +++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/include/zenoh-pico/system/platform/windows.h b/include/zenoh-pico/system/platform/windows.h index fa5aff62d..f829c6a98 100644 --- a/include/zenoh-pico/system/platform/windows.h +++ b/include/zenoh-pico/system/platform/windows.h @@ -27,7 +27,7 @@ typedef SRWLOCK _z_mutex_t; typedef CONDITION_VARIABLE _z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef struct timeb z_clock_t; +typedef LARGE_INTEGER z_clock_t; typedef struct timeb z_time_t; typedef struct { diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 581b42291..c4c583114 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -177,26 +177,52 @@ int z_sleep_s(size_t time) { /*------------------ Instant ------------------*/ z_clock_t z_clock_now(void) { z_clock_t now; - ftime(&now); + QueryPerformanceCounter(&now); return now; } -unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms(instant) * 1000; } +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; + LARGE_INTEGER frequency; + QueryPerformanceCounter(&now); + QueryPerformanceFrequency(&frequency); // ticks per second + + // Hardware not supporting QueryPerformanceFrequency + if (frequency.QuadPart == 0) { + return 0; + } + double elapsed = (double)(instant->QuadPart - now.QuadPart) * 1000000.0; + elapsed /= frequency.QuadPart; + return (unsigned long)elapsed; +} unsigned long z_clock_elapsed_ms(z_clock_t *instant) { - z_time_t now; - ftime(&now); + z_clock_t now; + LARGE_INTEGER frequency; + QueryPerformanceCounter(&now); + QueryPerformanceFrequency(&frequency); // ticks per second - unsigned long elapsed = ((unsigned long)(now.time - instant->time) * 1000) + (now.millitm - instant->millitm); - return elapsed; + // Hardware not supporting QueryPerformanceFrequency + if (frequency.QuadPart == 0) { + return 0; + } + double elapsed = (double)(instant->QuadPart - now.QuadPart) * 1000.0; + elapsed /= frequency.QuadPart; + return (unsigned long)elapsed; } unsigned long z_clock_elapsed_s(z_clock_t *instant) { - z_time_t now; - ftime(&now); + z_clock_t now; + LARGE_INTEGER frequency; + QueryPerformanceCounter(&now); + QueryPerformanceFrequency(&frequency); // ticks per second - unsigned long elapsed = (unsigned long)(now.time - instant->time); - return elapsed; + // Hardware not supporting QueryPerformanceFrequency + if (frequency.QuadPart == 0) { + return 0; + } + double elapsed = (double)(instant->QuadPart - now.QuadPart) / frequency.QuadPart; + return (unsigned long)elapsed; } /*------------------ Time ------------------*/ From 8f18901c7ff0d37489cf7a367dff271ab6ab96cc Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Thu, 4 Jan 2024 16:53:32 +0100 Subject: [PATCH 2/4] fix: unicode parsing issue --- examples/windows/z_ping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index f2334804f..7a5387321 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -113,7 +113,7 @@ int main(int argc, char** argv) { results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { - printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2); + printf("%d bytes: seq=%d rtt=%luus, lat=%luus\n", args.size, i, results[i], results[i] / 2); } _z_mutex_unlock(&mutex); z_free(results); From 090a964b0a32a40de24c60c13621f3e41c9b507a Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Thu, 4 Jan 2024 16:55:26 +0100 Subject: [PATCH 3/4] fix: switch past and present in measurement --- src/system/windows/system.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/system/windows/system.c b/src/system/windows/system.c index c4c583114..47eca4206 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -191,7 +191,7 @@ unsigned long z_clock_elapsed_us(z_clock_t *instant) { if (frequency.QuadPart == 0) { return 0; } - double elapsed = (double)(instant->QuadPart - now.QuadPart) * 1000000.0; + double elapsed = (double)(now.QuadPart - instant->QuadPart) * 1000000.0; elapsed /= frequency.QuadPart; return (unsigned long)elapsed; } @@ -206,7 +206,7 @@ unsigned long z_clock_elapsed_ms(z_clock_t *instant) { if (frequency.QuadPart == 0) { return 0; } - double elapsed = (double)(instant->QuadPart - now.QuadPart) * 1000.0; + double elapsed = (double)(now.QuadPart - instant->QuadPart) * 1000.0; elapsed /= frequency.QuadPart; return (unsigned long)elapsed; } @@ -221,7 +221,7 @@ unsigned long z_clock_elapsed_s(z_clock_t *instant) { if (frequency.QuadPart == 0) { return 0; } - double elapsed = (double)(instant->QuadPart - now.QuadPart) / frequency.QuadPart; + double elapsed = (double)(now.QuadPart - instant->QuadPart) / frequency.QuadPart; return (unsigned long)elapsed; } From bf74340779861b1108d7e671d4d75052975ba35f Mon Sep 17 00:00:00 2001 From: Jean-Roland Date: Thu, 4 Jan 2024 17:02:22 +0100 Subject: [PATCH 4/4] fix: switch scout timeout to z_clock --- src/session/scout.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/session/scout.c b/src/session/scout.c index 689dd8a53..efdd80938 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -55,8 +55,8 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig // The receiving buffer _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); - z_time_t start = z_time_now(); - while (z_time_elapsed_ms(&start) < period) { + z_clock_t start = z_clock_now(); + while (z_clock_elapsed_ms(&start) < period) { // Eventually read hello messages _z_zbuf_reset(&zbf);