Skip to content

Commit

Permalink
Merge pull request #380 from lf-lang/readable-time
Browse files Browse the repository at this point in the history
Use SI units in reporting human-readable time
  • Loading branch information
edwardalee authored Mar 3, 2024
2 parents 406b1bc + 6a8c179 commit bb3b158
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
7 changes: 4 additions & 3 deletions core/federated/RTI/rti_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ void notify_advance_grant_if_safe(scheduling_node_t* e);
void notify_provisional_tag_advance_grant(scheduling_node_t* e, tag_t tag);

/**
* Determine whether the specified scheduling node is eligible for a tag advance grant,
* (TAG) and, if so, return the details. This is called upon receiving a LTC, NET
* or resign from an upstream node.
* @brief Determine whether the specified scheduling node is eligible for a tag advance grant,
* (TAG) and, if so, return the details.
*
* This is called upon receiving a LTC, NET or resign from an upstream node.
*
* This function calculates the minimum M over
* all upstream scheduling nodes of the "after" delay plus the most recently
Expand Down
4 changes: 2 additions & 2 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2699,8 +2699,8 @@ int lf_send_tagged_message(environment_t* env,
if (message_type == MSG_TYPE_P2P_TAGGED_MESSAGE) {
lf_print_warning("Failed to send message to %s. Dropping the message.", next_destination_str);
} else {
lf_print_error_system_failure("Failed to send message to %s. Connection lost to the RTI.",
next_destination_str);
lf_print_error_system_failure("Failed to send message to %s with error code %d (%s). Connection lost to the RTI.",
next_destination_str, errno, strerror(errno));
}
}
LF_MUTEX_UNLOCK(&lf_outbound_socket_mutex);
Expand Down
30 changes: 16 additions & 14 deletions core/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ instant_t lf_time_start(void) {
}

size_t lf_readable_time(char* buffer, instant_t time) {
if (time <= (instant_t)0) {
snprintf(buffer, 2, "0");
return 1;
}
char* original_buffer = buffer;
bool lead = false; // Set to true when first clause has been printed.
if (time > WEEKS(1)) {
Expand All @@ -141,8 +145,8 @@ size_t lf_readable_time(char* buffer, instant_t time) {
size_t printed = lf_comma_separated_time(buffer, time / DAYS(1));
time = time % DAYS(1);
buffer += printed;
snprintf(buffer, 6, " days");
buffer += 5;
snprintf(buffer, 3, " d");
buffer += 2;
}
if (time > HOURS(1)) {
if (lead == true) {
Expand All @@ -153,8 +157,8 @@ size_t lf_readable_time(char* buffer, instant_t time) {
size_t printed = lf_comma_separated_time(buffer, time / HOURS(1));
time = time % HOURS(1);
buffer += printed;
snprintf(buffer, 7, " hours");
buffer += 6;
snprintf(buffer, 3, " h");
buffer += 2;
}
if (time > MINUTES(1)) {
if (lead == true) {
Expand All @@ -165,8 +169,8 @@ size_t lf_readable_time(char* buffer, instant_t time) {
size_t printed = lf_comma_separated_time(buffer, time / MINUTES(1));
time = time % MINUTES(1);
buffer += printed;
snprintf(buffer, 9, " minutes");
buffer += 8;
snprintf(buffer, 5, " min");
buffer += 4;
}
if (time > SECONDS(1)) {
if (lead == true) {
Expand All @@ -177,28 +181,26 @@ size_t lf_readable_time(char* buffer, instant_t time) {
size_t printed = lf_comma_separated_time(buffer, time / SECONDS(1));
time = time % SECONDS(1);
buffer += printed;
snprintf(buffer, 9, " seconds");
buffer += 8;
snprintf(buffer, 3, " s");
buffer += 2;
}
if (time > (instant_t)0) {
if (lead == true) {
snprintf(buffer, 3, ", ");
buffer += 2;
}
const char* units = "nanoseconds";
const char* units = "ns";
if (time % MSEC(1) == (instant_t) 0) {
units = "milliseconds";
units = "ms";
time = time / MSEC(1);
} else if (time % USEC(1) == (instant_t) 0) {
units = "microseconds";
units = "us";
time = time / USEC(1);
}
size_t printed = lf_comma_separated_time(buffer, time);
buffer += printed;
snprintf(buffer, 14, " %s", units);
snprintf(buffer, 3, " %s", units);
buffer += strlen(units) + 1;
} else {
snprintf(buffer, 2, "0");
}
return (buffer - original_buffer);
}
Expand Down
2 changes: 2 additions & 0 deletions core/threaded/reactor_threaded.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ void _lf_initialize_start_tag(environment_t *env) {
LF_PRINT_LOG("Waiting for start time " PRINTF_TIME " plus STA " PRINTF_TIME ".",
start_time, lf_fed_STA_offset);
#else
// For other than federated decentralized execution, there is no lf_fed_STA_offset variable defined.
// To use uniform code below, we define it here as a local variable.
instant_t lf_fed_STA_offset = 0;
LF_PRINT_LOG("Waiting for start time " PRINTF_TIME ".",
start_time);
Expand Down
13 changes: 7 additions & 6 deletions tag/api/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,22 @@ instant_t lf_time_start(void);
* Maximum number of nanoseconds is 999,999,999
* Maximum number of microsteps is 4,294,967,295
* Total number of characters for the above is 24.
* Text descriptions and spaces add an additional 55,
* for a total of 79. One more allows for a null terminator.
* Text descriptions and spaces add an additional 30,
* for a total of 54. One more allows for a null terminator.
* Round up to a power of two.
*/
#define LF_TIME_BUFFER_LENGTH 80
#define LF_TIME_BUFFER_LENGTH 64

/**
* Store into the specified buffer a string giving a human-readable
* rendition of the specified time. The buffer must have length at least
* equal to LF_TIME_BUFFER_LENGTH. The format is:
* ```
* x weeks, x days, x hours, x minutes, x seconds, x unit
* x weeks, x d, x hr, x min, x s, x unit
* ```
* where each `x` is a string of numbers with commas inserted if needed
* every three numbers and `unit` is nanoseconds, microseconds, or
* milliseconds.
* every three numbers and `unit` is ns, us, or
* ms.
* @param buffer The buffer into which to write the string.
* @param time The time to write.
* @return The number of characters written (not counting the null terminator).
Expand Down

0 comments on commit bb3b158

Please sign in to comment.