Skip to content

Commit

Permalink
Merge pull request #5944 from hoopoepg/topic/inf-auto-time-units
Browse files Browse the repository at this point in the history
UCS/TIME-UNITS: added "inf" and "auto" values
  • Loading branch information
yosefe authored Nov 26, 2020
2 parents 71f7fbd + 9a71bb7 commit 8f21972
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/ucp/core/ucp_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ static ucs_config_field_t ucp_config_table[] = {
"Experimental: enable new protocol selection logic",
ucs_offsetof(ucp_config_t, ctx.proto_enable), UCS_CONFIG_TYPE_BOOL},

{"KEEPALIVE_TIMEOUT", "0us",
"Time period between keepalive rounds (0 - disabled).",
{"KEEPALIVE_TIMEOUT", "inf",
"Time period between keepalive rounds (\"inf\" - disabled).",
ucs_offsetof(ucp_config_t, ctx.keepalive_timeout), UCS_CONFIG_TYPE_TIME_UNITS},

{"KEEPALIVE_NUM_EPS", "0",
Expand Down
2 changes: 1 addition & 1 deletion src/ucp/core/ucp_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -2666,7 +2666,7 @@ void ucp_worker_print_info(ucp_worker_h worker, FILE *stream)

static int ucp_worker_keepalive_is_enabled(ucp_worker_h worker)
{
return (worker->context->config.ext.keepalive_timeout != 0) &&
return (worker->context->config.ext.keepalive_timeout != UCS_TIME_INFINITY) &&
(worker->context->config.ext.keepalive_num_eps != 0);
}

Expand Down
20 changes: 17 additions & 3 deletions src/ucs/config/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,22 @@ int ucs_config_sscanf_time(const char *buf, void *dest, const void *arg)
int ucs_config_sprintf_time(char *buf, size_t max,
const void *src, const void *arg)
{
snprintf(buf, max, "%.2fus", *(double*)src * UCS_USEC_PER_SEC);
return 1;
return snprintf(buf, max, "%.2fus", *(double*)src * UCS_USEC_PER_SEC);
}

int ucs_config_sscanf_time_units(const char *buf, void *dest, const void *arg)
{
double value;
int ret;

if (!strcmp(buf, "inf")) {
*(ucs_time_t*)dest = UCS_TIME_INFINITY;
return 1;
} else if (!strcmp(buf, "auto")) {
*(ucs_time_t*)dest = UCS_TIME_AUTO;
return 1;
}

ret = ucs_config_sscanf_time(buf, &value, arg);
if (ret == 0) {
return 0;
Expand All @@ -454,8 +461,15 @@ int ucs_config_sscanf_time_units(const char *buf, void *dest, const void *arg)
int ucs_config_sprintf_time_units(char *buf, size_t max,
const void *src, const void *arg)
{
double value = ucs_time_to_sec(*(ucs_time_t*)src);
double value;

if (*(ucs_time_t*)src == UCS_TIME_INFINITY) {
return snprintf(buf, max, "inf");
} else if (*(ucs_time_t*)src == UCS_TIME_AUTO) {
return snprintf(buf, max, "auto");
}

value = ucs_time_to_sec(*(ucs_time_t*)src);
return ucs_config_sprintf_time(buf, max, &value, arg);
}

Expand Down
3 changes: 2 additions & 1 deletion src/ucs/config/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ void ucs_config_help_generic(char *buf, size_t max, const void *arg);

#define UCS_CONFIG_TYPE_TIME_UNITS {ucs_config_sscanf_time_units, ucs_config_sprintf_time_units, \
ucs_config_clone_ulong, ucs_config_release_nop, \
ucs_config_help_generic, "time value: <number>[s|us|ms|ns]"}
ucs_config_help_generic, \
"time value: <number>[s|us|ms|ns], \"inf\", or \"auto\""}

#define UCS_CONFIG_TYPE_BW {ucs_config_sscanf_bw, ucs_config_sprintf_bw, \
ucs_config_clone_double, ucs_config_release_nop, \
Expand Down
1 change: 1 addition & 0 deletions src/ucs/time/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef uint32_t ucs_short_time_t;


#define UCS_TIME_INFINITY ULLONG_MAX
#define UCS_TIME_AUTO (UCS_TIME_INFINITY - 1)

#define UCS_MSEC_PER_SEC 1000ull /* Milli */
#define UCS_USEC_PER_SEC 1000000ul /* Micro */
Expand Down
25 changes: 21 additions & 4 deletions test/gtest/ucs/test_config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ typedef struct {
int air_conditioning;
int abs;
int transmission;

ucs_time_t time_value;
ucs_time_t time_auto;
ucs_time_t time_inf;
} car_opts_t;


Expand Down Expand Up @@ -205,6 +209,15 @@ ucs_config_field_t car_opts_table[] = {
{"TRANSMISSION", "auto", "Transmission mode",
ucs_offsetof(car_opts_t, transmission), UCS_CONFIG_TYPE_ON_OFF_AUTO},

{"TIME_VAL", "1s", "Time value 1 sec",
ucs_offsetof(car_opts_t, time_value), UCS_CONFIG_TYPE_TIME_UNITS},

{"TIME_AUTO", "auto", "Time value \"auto\"",
ucs_offsetof(car_opts_t, time_auto), UCS_CONFIG_TYPE_TIME_UNITS},

{"TIME_INF", "inf", "Time value \"inf\"",
ucs_offsetof(car_opts_t, time_inf), UCS_CONFIG_TYPE_TIME_UNITS},

{NULL}
};

Expand Down Expand Up @@ -397,6 +410,10 @@ UCS_TEST_F(test_config, parse_default) {
EXPECT_EQ(UCS_CONFIG_ON, opts->air_conditioning);
EXPECT_EQ(UCS_CONFIG_OFF, opts->abs);
EXPECT_EQ(UCS_CONFIG_AUTO, opts->transmission);

EXPECT_EQ(ucs_time_from_sec(1.0), opts->time_value);
EXPECT_EQ(UCS_TIME_AUTO, opts->time_auto);
EXPECT_EQ(UCS_TIME_INFINITY, opts->time_inf);
}

UCS_TEST_F(test_config, clone) {
Expand Down Expand Up @@ -523,27 +540,27 @@ UCS_TEST_F(test_config, unused) {

UCS_TEST_F(test_config, dump) {
/* aliases must not be counted here */
test_config_print_opts(UCS_CONFIG_PRINT_CONFIG, 28u);
test_config_print_opts(UCS_CONFIG_PRINT_CONFIG, 31u);
}

UCS_TEST_F(test_config, dump_hidden) {
/* aliases must be counted here */
test_config_print_opts((UCS_CONFIG_PRINT_CONFIG |
UCS_CONFIG_PRINT_HIDDEN),
35u);
38u);
}

UCS_TEST_F(test_config, dump_hidden_check_alias_name) {
/* aliases must be counted here */
test_config_print_opts((UCS_CONFIG_PRINT_CONFIG |
UCS_CONFIG_PRINT_HIDDEN |
UCS_CONFIG_PRINT_DOC),
35u);
38u);

test_config_print_opts((UCS_CONFIG_PRINT_CONFIG |
UCS_CONFIG_PRINT_HIDDEN |
UCS_CONFIG_PRINT_DOC),
35u, "TEST_");
38u, "TEST_");
}

UCS_TEST_F(test_config, deprecated) {
Expand Down

0 comments on commit 8f21972

Please sign in to comment.