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

Fix cnex to deal with certain Linux variations on time/clock handling. #444

Merged
merged 1 commit into from
Jan 30, 2024
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
7 changes: 7 additions & 0 deletions exec/cnex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
lib/runtime_posix.c
)
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
# Not all flavors of Linx will have the function clock_gettime(), and some have it in a different include.
# See exec/cnex/lib/time_linux.c for more information.
include(CheckSymbolExists)
check_symbol_exists(clock_gettime "time.h;sys/time.h" HAVE_CLOCK_GETTIME)
if(HAVE_CLOCK_GETTIME)
add_definitions(-DHAVE_CLOCK_GETTIME)
endif()
set(platform_cnex
rtl_posix.c
lib/file_posix.c
Expand Down
17 changes: 17 additions & 0 deletions exec/cnex/lib/time_linux.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#ifdef HAVE_CLOCK_GETTIME
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work. On a system that does have clock_gettime (and cmake detects so), this compiler define still isn't set. Cmake variables are not the same thing as compiler defines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added define in CMakeLists.txt

#include <time.h>
#else
#include <sys/time.h>
#endif

#include "cell.h"
#include "exec.h"
Expand All @@ -9,17 +13,30 @@


static Number NANOSECONDS_PER_SECOND;
static Number MICROSECONDS_PER_SECOND;

void time_initModule()
{
NANOSECONDS_PER_SECOND = number_from_uint64(1000000000LL);
MICROSECONDS_PER_SECOND = number_from_uint64(1000000LL);
}

void time_tick(TExecutor *exec)
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;

clock_gettime(CLOCK_MONOTONIC, &ts);

push(exec->stack, cell_fromNumber(number_add(number_from_uint64(ts.tv_sec), number_divide(number_from_uint64(ts.tv_nsec), NANOSECONDS_PER_SECOND))));
#else
// Not all Linux distros will have clock_gettime(), so we'll resort to gettimeofday() in those cases. Note the difference between
// between timespec and timeval structures as well; where timeval has a tv_usec (Microseconds), vs. tv_nsec (Nanoseconds) member.
// This means we have to divide by 1,000,000, not 1,000,000,000, when using timeval and gettimeofday().
struct timeval tv;

gettimeofday(&tv, NULL);

push(exec->stack, cell_fromNumber(number_add(number_from_uint64(tv.tv_sec), number_divide(number_from_uint64(tv.tv_usec), MICROSECONDS_PER_SECOND))));
#endif
}
Loading