Skip to content
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

Add support for sleep and time primitives (similar to zenoh-pico) #256

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
122 changes: 121 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ maintenance = { status = "actively-developed" }
[dependencies]
async-std = "=1.12.0"
async-trait = "0.1.66"
chrono = "0.4.34"
env_logger = "0.10.0"
futures = "0.3.26"
json5 = "0.4.1"
lazy_static = "1.4.0"
libc = "0.2.139"
log = "0.4.17"
rand = "0.8.5"
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml.in
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ maintenance = { status = "actively-developed" }
[dependencies]
async-std = "=1.12.0"
async-trait = "0.1.66"
chrono = "0.4.34"
env_logger = "0.10.0"
futures = "0.3.26"
json5 = "0.4.1"
lazy_static = "1.4.0"
libc = "0.2.139"
log = "0.4.17"
rand = "0.8.5"
Expand Down
8 changes: 1 addition & 7 deletions examples/z_liveliness.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@

#include <stdio.h>
#include <string.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define sleep(x) Sleep(x * 1000)
#else
#include <unistd.h>
#endif
#include "zenoh.h"

int main(int argc, char **argv) {
Expand Down Expand Up @@ -63,7 +57,7 @@ int main(int argc, char **argv) {
while (c != 'q') {
c = getchar();
if (c == -1) {
sleep(1);
z_sleep_s(1);
} else if (c == 'd') {
printf("Undeclaring liveliness token...\n");
z_drop(z_move(token));
Expand Down
19 changes: 5 additions & 14 deletions examples/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "zenoh.h"

Expand Down Expand Up @@ -58,35 +57,27 @@ int main(int argc, char** argv) {
z_mutex_lock(&mutex);
if (args.warmup_ms) {
printf("Warming up for %dms...\n", args.warmup_ms);
struct timespec wmup_start, wmup_stop, wmup_timeout;
clock_gettime(CLOCK_MONOTONIC, &wmup_start);
z_clock_t warmup_start = z_clock_now();

unsigned long elapsed_us = 0;
while (elapsed_us < args.warmup_ms * 1000) {
clock_gettime(CLOCK_REALTIME, &wmup_timeout);
wmup_timeout.tv_sec += PING_TIMEOUT_SEC;
z_publisher_put(z_loan(pub), data, args.size, NULL);
int s = z_condvar_wait(&cond, &mutex);
if (s != 0) {
handle_error_en(s, "z_condvar_wait");
}
clock_gettime(CLOCK_MONOTONIC, &wmup_stop);
elapsed_us =
(1000000 * (wmup_stop.tv_sec - wmup_start.tv_sec) + (wmup_stop.tv_nsec - wmup_start.tv_nsec) / 1000);
elapsed_us = z_clock_elapsed_us(&warmup_start);
}
}
struct timespec t_start, t_stop, t_timeout;
unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings);
for (int i = 0; i < args.number_of_pings; i++) {
clock_gettime(CLOCK_REALTIME, &t_timeout);
t_timeout.tv_sec += PING_TIMEOUT_SEC;
clock_gettime(CLOCK_MONOTONIC, &t_start);
z_clock_t measure_start = z_clock_now();
z_publisher_put(z_loan(pub), data, args.size, NULL);
int s = z_condvar_wait(&cond, &mutex);
if (s != 0) {
handle_error_en(s, "z_condvar_wait");
}
clock_gettime(CLOCK_MONOTONIC, &t_stop);
results[i] = (1000000 * (t_stop.tv_sec - t_start.tv_sec) + (t_stop.tv_nsec - t_start.tv_nsec) / 1000);
results[i] = z_clock_elapsed_us(&measure_start);
}
for (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);
Expand Down
8 changes: 1 addition & 7 deletions examples/z_pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include <string.h>

#include "zenoh.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define sleep(x) Sleep(x * 1000)
#else
#include <unistd.h>
#endif

void matching_status_handler(const zcu_matching_status_t *matching_status, void *arg) {
if (matching_status->matching) {
Expand Down Expand Up @@ -72,7 +66,7 @@ int main(int argc, char **argv) {

char buf[256];
for (int idx = 0; 1; ++idx) {
sleep(1);
z_sleep_s(1);
sprintf(buf, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, buf);
z_publisher_put_options_t options = z_publisher_put_options_default();
Expand Down
8 changes: 1 addition & 7 deletions examples/z_pub_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include <string.h>

#include "zenoh.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define sleep(x) Sleep(x * 1000)
#else
#include <unistd.h>
#endif

int main(int argc, char **argv) {
char *keyexpr = "demo/example/zenoh-c-pub";
Expand Down Expand Up @@ -69,7 +63,7 @@ int main(int argc, char **argv) {
char buf[256];
char buf_ind[16];
for (int idx = 0; 1; ++idx) {
sleep(1);
z_sleep_s(1);

// add some other attachment value
sprintf(buf_ind, "%d", idx);
Expand Down
8 changes: 1 addition & 7 deletions examples/z_pub_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include <string.h>

#include "zenoh.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#define sleep(x) Sleep(x * 1000)
#else
#include <unistd.h>
#endif

int main(int argc, char **argv) {
char *keyexpr = "demo/example/zenoh-c-pub";
Expand Down Expand Up @@ -66,7 +60,7 @@ int main(int argc, char **argv) {

char buf[256];
for (int idx = 0; 1; ++idx) {
sleep(1);
z_sleep_s(1);
sprintf(buf, "[%4d] %s", idx, value);
printf("Putting Data ('%s': '%s')...\n", keyexpr, buf);
z_put(z_loan(s), z_keyexpr(keyexpr), (const uint8_t *)buf, strlen(buf), NULL);
Expand Down
Loading
Loading