From 74f4cd1c60f6f99ee514f51d3c82471f952c81fd Mon Sep 17 00:00:00 2001 From: erlingrj Date: Sun, 28 Apr 2024 15:50:29 +0200 Subject: [PATCH 01/10] Add a unit test for the scheduling API --- test/Tests.cmake | 67 ++----------------- test/multithreaded/scheduling_api_test.c | 82 ++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 test/multithreaded/scheduling_api_test.c diff --git a/test/Tests.cmake b/test/Tests.cmake index 2ec3a90ba..9ffd95003 100644 --- a/test/Tests.cmake +++ b/test/Tests.cmake @@ -1,10 +1,10 @@ # This adds all tests in the test directory. include(CTest) -set(TestLib test-lib) set(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test) set(TEST_SUFFIX test.c) # Files that are tests must have names ending with TEST_SUFFIX. set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/..) +set(TEST_MOCK_SRCS ${TEST_DIR}/src_gen_stub.c ${TEST_DIR}/rand_utils.c) # Add the test files found in DIR to TEST_FILES. function(add_test_dir DIR) @@ -29,67 +29,14 @@ endif(NUMBER_OF_WORKERS) # Create executables for each test. foreach(FILE ${TEST_FILES}) string(REGEX REPLACE "[./]" "_" NAME ${FILE}) - add_executable(${NAME} ${TEST_DIR}/${FILE}) + add_executable(${NAME} ${TEST_DIR}/${FILE} ${TEST_MOCK_SRCS}) add_test(NAME ${NAME} COMMAND ${NAME}) + # This is needed for the tests to use the threading API declared in + # low_level_platform.h. Ideally this would not be needed. + target_link_libraries(${NAME} PRIVATE lf::low-level-platform-impl) target_link_libraries( - ${NAME} PUBLIC - ${CoreLib} ${Lib} ${TestLib} + ${NAME} PRIVATE + ${CoreLib} ${Lib} ) target_include_directories(${NAME} PRIVATE ${TEST_DIR}) endforeach(FILE ${TEST_FILES}) - -# Add the test for the RTI. -if (NOT DEFINED LF_SINGLE_THREADED) - # Check which system we are running on to select the correct platform support - # file and assign the file's path to LF_PLATFORM_FILE - # FIXME: This is effectively a second build script for the RTI that we have to maintain. This is code duplication. - # FIXME: We should not be reaching into the platform directory and bypassing its CMake build. - if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - set(LF_PLATFORM_FILE ${LF_ROOT}/low_level_platform/impl/src/lf_linux_support.c) - elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") - set(LF_PLATFORM_FILE ${LF_ROOT}/low_level_platform/impl/src/lf_macos_support.c) - else() - message(FATAL_ERROR "Your platform is not supported! RTI supports Linux and MacOS.") - endif() - - set(IncludeDir include/core) - - set(RTI_DIR ${CoreLibPath}/federated/RTI) - add_executable( - rti_common_test - ${TEST_DIR}/RTI/rti_common_test.c - ${RTI_DIR}/rti_common.c - ${RTI_DIR}/rti_remote.c - ${CoreLibPath}/tracepoint.c - ${LF_PLATFORM_FILE} - ${LF_ROOT}/low_level_platform/impl/src/lf_platform_util.c - ${LF_ROOT}/low_level_platform/impl/src/lf_atomic_gcc_clang.c - ${LF_ROOT}/low_level_platform/impl/src/lf_unix_clock_support.c - ${CoreLibPath}/utils/util.c - ${CoreLibPath}/tag.c - ${CoreLibPath}/clock.c - ${CoreLibPath}/federated/network/net_util.c - ${CoreLibPath}/utils/pqueue_base.c - ${CoreLibPath}/utils/pqueue_tag.c - ${CoreLibPath}/utils/pqueue.c - ) - add_test(NAME rti_common_test COMMAND rti_common_test) - target_include_directories(rti_common_test PUBLIC ${RTI_DIR}) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}/federated) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}/modal_models) - target_link_libraries(rti_common_test lf::low-level-platform-api) - target_link_libraries(rti_common_test lf::logging-api) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}/utils) - # Set the STANDALONE_RTI flag to include the rti_remote and rti_common. - target_compile_definitions(rti_common_test PUBLIC STANDALONE_RTI=1) - - # Set FEDERATED to get federated compilation support - target_compile_definitions(rti_common_test PUBLIC FEDERATED=1) - - target_compile_definitions(rti_common_test PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) - - # Find threads and link to it - find_package(Threads REQUIRED) - target_link_libraries(rti_common_test Threads::Threads) -endif() diff --git a/test/multithreaded/scheduling_api_test.c b/test/multithreaded/scheduling_api_test.c new file mode 100644 index 000000000..1a8615812 --- /dev/null +++ b/test/multithreaded/scheduling_api_test.c @@ -0,0 +1,82 @@ + +#include +#include +#include "core/utils/util.h" +#include "low_level_platform.h" + +int main(int argc, char** argv) { + int res; + + // Set the CPU Set of the current thread. + res = lf_thread_set_cpu(lf_thread_self(), lf_available_cores() - 1); + if (res != 0) { + lf_print_error_and_exit("lf_thread_set_cpu failed with %d", res); + } + + // // Pick SCHED_FIFO + { + lf_scheduling_policy_t cfg; + cfg.policy = LF_SCHED_PRIORITY; + cfg.priority = 99; + cfg.time_slice = 0; + res = lf_thread_set_scheduling_policy(lf_thread_self(), &cfg); + if (res != 0) { + lf_print_error_and_exit("lf_thread_set_scheduling_policy FIFO failed with %d", res); + } + } + + // Set SCHED_RR + { + lf_scheduling_policy_t cfg; + cfg.policy = LF_SCHED_TIMESLICE; + cfg.priority = 99; + cfg.time_slice = 0; + res = lf_thread_set_scheduling_policy(lf_thread_self(), &cfg); + if (res != 0) { + lf_print_error_and_exit("lf_thread_set_scheduling_policy RR failed with %d", res); + } + } + + // Try illegal priority + { + lf_scheduling_policy_t cfg; + cfg.policy = LF_SCHED_TIMESLICE; + cfg.time_slice = 0; + cfg.priority = 10000; + res = lf_thread_set_scheduling_policy(lf_thread_self(), &cfg); + if (res == 0) { + lf_print_error_and_exit("lf_thread_set_scheduling_policy should have failed with illegal priority"); + } + } + + // Set the priority + res = lf_thread_set_priority(lf_thread_self(), 50); + if (res != 0) { + lf_print_error_and_exit("lf_thread_set_priority failed with %d", res); + } + + res = lf_thread_set_priority(lf_thread_self(), -50); + if (res == 0) { + lf_print_error_and_exit("lf_thread_set_priority should have failed for -50"); + } + + { + lf_scheduling_policy_t cfg; + cfg.policy = LF_SCHED_FAIR; + res = lf_thread_set_scheduling_policy(lf_thread_self(), &cfg); + if (res != 0) { + lf_print_error_and_exit("lf_thread_set_scheduling_policy RR failed with %d", res); + } + } + + // Try to high CPU + res = lf_thread_set_cpu(lf_thread_self(), lf_available_cores()); + if (res == 0) { + lf_print_error_and_exit("lf_thread_set_cpu should fail for too high CPU id"); + } + + res = lf_thread_set_cpu(lf_thread_self(), -1); + if (res == 0) { + lf_print_error_and_exit("lf_thread_set_cpu should fail for too low CPU id"); + } +} From 1a28a1ae39c3512f886c37bccf2cca9eff9b6b05 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Sun, 28 Apr 2024 15:52:46 +0200 Subject: [PATCH 02/10] Some additional thread scheduling api fixes: - Only accept unsigned CPU ids - Set priority to 0 for SCHED_OTHER - Only cal lf_thread_set_priority for SCHED_RR and SCHED_FIFO --- .github/workflows/unit-tests.yml | 10 ++++++-- low_level_platform/api/low_level_platform.h | 2 +- .../impl/src/lf_linux_support.c | 22 ++++++++++++------ .../impl/src/lf_macos_support.c | 2 +- .../impl/src/lf_windows_support.c | 2 +- .../impl/src/lf_zephyr_support.c | 2 +- test/Tests.cmake | 4 +++- .../scheduling_api_test.c | 7 ++++-- trace/impl/lib/lf-trace-impl.a | Bin 0 -> 10034 bytes 9 files changed, 35 insertions(+), 16 deletions(-) rename test/{multithreaded => scheduling}/scheduling_api_test.c (95%) create mode 100644 trace/impl/lib/lf-trace-impl.a diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index fc8a7123d..2b55420f5 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -18,10 +18,16 @@ jobs: - name: Check out reactor-c repository uses: actions/checkout@v2 - - name: Build and run unit tests ${{ inputs.cmake-args }} + - name: Build unit tests${{ inputs.cmake-args }} run: | mkdir build cd build cmake .. ${{ inputs.cmake-args }} cmake --build . - make test + - name: Run unit tests with sudo. (Needed for changing prio/policy) + run: sudo make test + if: matrix.platform == 'ubuntu-latest' + - name: Run unit tests normal. + run: cd build && make test + if: matrix.platform != 'ubuntu-latest' + diff --git a/low_level_platform/api/low_level_platform.h b/low_level_platform/api/low_level_platform.h index 2867aa0f4..8494a4d85 100644 --- a/low_level_platform/api/low_level_platform.h +++ b/low_level_platform/api/low_level_platform.h @@ -162,7 +162,7 @@ typedef struct { * @param cpu_number the CPU ID * @return 0 on success, platform-specific error number otherwise. */ -int lf_thread_set_cpu(lf_thread_t thread, int cpu_number); +int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number); /** * @brief Set the priority of a thread. diff --git a/low_level_platform/impl/src/lf_linux_support.c b/low_level_platform/impl/src/lf_linux_support.c index 3f28dcb3b..30ef30bdf 100644 --- a/low_level_platform/impl/src/lf_linux_support.c +++ b/low_level_platform/impl/src/lf_linux_support.c @@ -45,9 +45,9 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #else #include "lf_POSIX_threads_support.c" -int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) { - // First verify that we have num_cores>cpu_number - if (lf_available_cores() <= cpu_number) { +int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number) { + // Sanitize input + if (lf_available_cores() <= 0 || cpu_number >= (size_t)lf_available_cores()) { return -1; } @@ -89,6 +89,7 @@ int lf_thread_set_priority(lf_thread_t thread, int priority) { int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* policy) { int posix_policy, res; + bool set_priority; struct sched_param schedparam; // Get the current scheduling policy @@ -103,14 +104,18 @@ int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* switch (policy->policy) { case LF_SCHED_FAIR: posix_policy = SCHED_OTHER; + schedparam.sched_priority = 0; + set_priority = false; break; case LF_SCHED_TIMESLICE: posix_policy = SCHED_RR; schedparam.sched_priority = sched_get_priority_max(SCHED_RR); + set_priority = true; break; case LF_SCHED_PRIORITY: posix_policy = SCHED_FIFO; schedparam.sched_priority = sched_get_priority_max(SCHED_FIFO); + set_priority = true; break; default: return -1; @@ -123,10 +128,13 @@ int lf_thread_set_scheduling_policy(lf_thread_t thread, lf_scheduling_policy_t* return res; } - // Set the priority - res = lf_thread_set_priority(thread, policy->priority); - if (res != 0) - return res; + // Set the priority of we chose a RT scheduler + if (set_priority) { + res = lf_thread_set_priority(thread, policy->priority); + if (res != 0) { + return res; + } + } return 0; } diff --git a/low_level_platform/impl/src/lf_macos_support.c b/low_level_platform/impl/src/lf_macos_support.c index d6b59c4c9..bf96362cb 100644 --- a/low_level_platform/impl/src/lf_macos_support.c +++ b/low_level_platform/impl/src/lf_macos_support.c @@ -42,7 +42,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /** * Real-time scheduling API not implemented for macOS. */ -int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) { return -1; } +int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number) { return -1; } int lf_thread_set_priority(lf_thread_t thread, int priority) { return -1; } diff --git a/low_level_platform/impl/src/lf_windows_support.c b/low_level_platform/impl/src/lf_windows_support.c index a0f1fe4dc..1d48bc6c7 100644 --- a/low_level_platform/impl/src/lf_windows_support.c +++ b/low_level_platform/impl/src/lf_windows_support.c @@ -190,7 +190,7 @@ int lf_thread_join(lf_thread_t thread, void** thread_return) { /** * Real-time scheduling API not implemented for Windows. */ -int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) { return -1; } +int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number) { return -1; } int lf_thread_set_priority(lf_thread_t thread, int priority) { return -1; } diff --git a/low_level_platform/impl/src/lf_zephyr_support.c b/low_level_platform/impl/src/lf_zephyr_support.c index f3470edbb..1a564d1c7 100644 --- a/low_level_platform/impl/src/lf_zephyr_support.c +++ b/low_level_platform/impl/src/lf_zephyr_support.c @@ -157,7 +157,7 @@ int lf_thread_id() { return *((int*)k_thread_custom_data_get()); } lf_thread_t lf_thread_self() { return k_current_get(); } -int lf_thread_set_cpu(lf_thread_t thread, int cpu_number) { return k_thread_cpu_pin(thread, cpu_number); } +int lf_thread_set_cpu(lf_thread_t thread, size_t cpu_number) { return k_thread_cpu_pin(thread, cpu_number); } /** * Real-time scheduling API diff --git a/test/Tests.cmake b/test/Tests.cmake index 9ffd95003..e401bec56 100644 --- a/test/Tests.cmake +++ b/test/Tests.cmake @@ -21,7 +21,9 @@ endfunction() # Add the appropriate directories for the provided build parameters. add_test_dir(${TEST_DIR}/general) if(NUMBER_OF_WORKERS) - add_test_dir(${TEST_DIR}/multithreaded) + if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + add_test_dir(${TEST_DIR}/scheduling) + endif() else() add_test_dir(${TEST_DIR}/single-threaded) endif(NUMBER_OF_WORKERS) diff --git a/test/multithreaded/scheduling_api_test.c b/test/scheduling/scheduling_api_test.c similarity index 95% rename from test/multithreaded/scheduling_api_test.c rename to test/scheduling/scheduling_api_test.c index 1a8615812..46c408693 100644 --- a/test/multithreaded/scheduling_api_test.c +++ b/test/scheduling/scheduling_api_test.c @@ -1,9 +1,12 @@ - #include #include #include "core/utils/util.h" #include "low_level_platform.h" +#if !defined PLATFORM_Linux +#error scheduling_api_test.c should only be compiled on Linux +#endif + int main(int argc, char** argv) { int res; @@ -79,4 +82,4 @@ int main(int argc, char** argv) { if (res == 0) { lf_print_error_and_exit("lf_thread_set_cpu should fail for too low CPU id"); } -} +} \ No newline at end of file diff --git a/trace/impl/lib/lf-trace-impl.a b/trace/impl/lib/lf-trace-impl.a new file mode 100644 index 0000000000000000000000000000000000000000..131083279272a66b87162e7d9171ba5f37434869 GIT binary patch literal 10034 zcmcIqeQaCR6~B&~5;~fs+X`iTJu1?ru$r_hqeX?eA1{6DN-1qEs&1X@*w2l@v4j2G zG^GJ=X)BBsscf1A`(vxNN+9hEZLqO!D@iF0P;Dz8LIqL57)+@WX=Mx+N>O&sedpN6 z4?hhFxYF~x_jf+;x#!+@?@g{;7R$xEZ<=wfQCm7h)2+*2!&rFz65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ix warnings --- CMakeLists.txt | 3 --- test/RTI/rti_common_test.c | 2 +- test/Tests.cmake | 4 ++++ test/general/tag_test.c | 2 +- test/general/utils/hashmap_test.c | 2 +- test/general/utils/hashset_test.c | 4 +--- test/general/utils/pqueue_test.c | 4 ++-- test/rand_utils.c | 2 +- test/scheduling/scheduling_api_test.c | 2 +- test/src_gen_stub.c | 4 ++-- 10 files changed, 14 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a916023f..7a088cc0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,9 +22,6 @@ if(DEFINED LF_SINGLE_THREADED) add_compile_definitions(LF_SINGLE_THREADED=1) endif() -# Warnings as errors -add_compile_options(-Werror) - set(Test test) set(Lib lib) set(CoreLibPath core) diff --git a/test/RTI/rti_common_test.c b/test/RTI/rti_common_test.c index 3d2c73af9..6bf21b312 100644 --- a/test/RTI/rti_common_test.c +++ b/test/RTI/rti_common_test.c @@ -246,7 +246,7 @@ static void multiple_nodes() { assert(lf_tag_compare(test_rti.scheduling_nodes[3]->min_delays[0].min_delay, (tag_t){NSEC(3), 0}) == 0); } -int main(int argc, char** argv) { +int main() { initialize_rti_common(&test_rti); // Tests for the function update_min_delays_upstream() diff --git a/test/Tests.cmake b/test/Tests.cmake index e401bec56..3b4a135ff 100644 --- a/test/Tests.cmake +++ b/test/Tests.cmake @@ -6,6 +6,8 @@ set(TEST_SUFFIX test.c) # Files that are tests must have names ending with TEST set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/..) set(TEST_MOCK_SRCS ${TEST_DIR}/src_gen_stub.c ${TEST_DIR}/rand_utils.c) +include(${LF_ROOT}/core/lf_utils.cmake) + # Add the test files found in DIR to TEST_FILES. function(add_test_dir DIR) file( @@ -41,4 +43,6 @@ foreach(FILE ${TEST_FILES}) ${CoreLib} ${Lib} ) target_include_directories(${NAME} PRIVATE ${TEST_DIR}) + # Warnings as errors + lf_enable_compiler_warnings(${NAME}) endforeach(FILE ${TEST_FILES}) diff --git a/test/general/tag_test.c b/test/general/tag_test.c index aff2d6875..24fb7b174 100644 --- a/test/general/tag_test.c +++ b/test/general/tag_test.c @@ -2,7 +2,7 @@ #include #include "lf_types.h" -int main(int argc, char** argv) { +int main() { char* buf = malloc(sizeof(char) * 128); lf_readable_time(buf, 0); printf("%s", buf); diff --git a/test/general/utils/hashmap_test.c b/test/general/utils/hashmap_test.c index 28e8deb27..c0fd25443 100644 --- a/test/general/utils/hashmap_test.c +++ b/test/general/utils/hashmap_test.c @@ -35,7 +35,7 @@ void test_get(hashmap_object2int_t* h) { if (desired.value != found) { // It is possible that two distinct values were associated with the same key. Search the // "mock" array to check if this is the case. - for (size_t i = mock_size - 1; i >= 0; i--) { + for (int i = (int)mock_size - 1; i >= 0; i--) { if (mock[i].key == desired.key) { if (mock[i].value == found) return; // Everything is OK. diff --git a/test/general/utils/hashset_test.c b/test/general/utils/hashset_test.c index 4b425b848..1fffcf4c1 100644 --- a/test/general/utils/hashset_test.c +++ b/test/general/utils/hashset_test.c @@ -199,7 +199,7 @@ static void test_fill_with_deleted_items() { hashset_destroy(set); } -int main(int argc, char* argv[]) { +int main() { trivial(); test_gaps(); test_exceptions(); @@ -207,8 +207,6 @@ int main(int argc, char* argv[]) { test_iterating(); test_fill_with_deleted_items(); - (void)argc; - (void)argv; printf("Tests passed.\n"); return 0; } diff --git a/test/general/utils/pqueue_test.c b/test/general/utils/pqueue_test.c index da8e2c6b7..665c4e13f 100644 --- a/test/general/utils/pqueue_test.c +++ b/test/general/utils/pqueue_test.c @@ -47,7 +47,7 @@ static void find_from_queue(pqueue_tag_t* q) { } static void insert_if_no_match(pqueue_tag_t* q) { - int size = pqueue_tag_size(q); + size_t size = pqueue_tag_size(q); tag_t t1 = {.time = USEC(3), .microstep = 0}; tag_t t4 = {.time = USEC(1), .microstep = 2}; // Return value is non-zero on failure to insert: @@ -84,7 +84,7 @@ static void remove_from_queue(pqueue_tag_t* q, pqueue_tag_element_t* e1, pqueue_ assert(pqueue_tag_size(q) == 1); } -int main(int argc, char* argv[]) { +int main() { trivial(); // Create an event queue. pqueue_tag_t* q = pqueue_tag_init(2); diff --git a/test/rand_utils.c b/test/rand_utils.c index 49c1b5230..39b0aadab 100644 --- a/test/rand_utils.c +++ b/test/rand_utils.c @@ -12,7 +12,7 @@ */ void perturb(int* src, size_t size, int* out) { out[size - 1] = src[size - 1]; - for (int a = 0; a < size - 1; a += 2) { + for (size_t a = 0; a < size - 1; a += 2) { int min = src[a] < src[a + 1] ? src[a] : src[a + 1]; int diff = rand() % (min * 2) - min; out[a] = src[a] + diff; diff --git a/test/scheduling/scheduling_api_test.c b/test/scheduling/scheduling_api_test.c index 46c408693..0078cff6b 100644 --- a/test/scheduling/scheduling_api_test.c +++ b/test/scheduling/scheduling_api_test.c @@ -7,7 +7,7 @@ #error scheduling_api_test.c should only be compiled on Linux #endif -int main(int argc, char** argv) { +int main() { int res; // Set the CPU Set of the current thread. diff --git a/test/src_gen_stub.c b/test/src_gen_stub.c index 1e4630a6e..919d14b43 100644 --- a/test/src_gen_stub.c +++ b/test/src_gen_stub.c @@ -14,8 +14,8 @@ environment_t _env; void _lf_initialize_trigger_objects(void) {} void lf_terminate_execution(void) {} void lf_set_default_command_line_options(void) {} -void _lf_initialize_watchdogs(environment_t** envs) {} -void logical_tag_complete(tag_t tag_to_send) {} +void _lf_initialize_watchdogs(environment_t** envs) { (void)envs; } +void logical_tag_complete(tag_t tag_to_send) { (void)tag_to_send; } int _lf_get_environments(environment_t** envs) { *envs = &_env; return 1; From 508a98a050a333ea21f799922b36fe4ad969c900 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Sun, 28 Apr 2024 20:38:01 +0200 Subject: [PATCH 04/10] Cleanup and formatting --- .github/workflows/unit-tests.yml | 2 +- CMakeLists.txt | 2 -- test/CMakeLists.txt | 2 -- test/Tests.cmake | 57 +++++++++++++++++++++++++++++++ trace/impl/lib/lf-trace-impl.a | Bin 10034 -> 0 bytes 5 files changed, 58 insertions(+), 5 deletions(-) delete mode 100644 test/CMakeLists.txt delete mode 100644 trace/impl/lib/lf-trace-impl.a diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 2b55420f5..84683136c 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -18,7 +18,7 @@ jobs: - name: Check out reactor-c repository uses: actions/checkout@v2 - - name: Build unit tests${{ inputs.cmake-args }} + - name: Build unit tests ${{ inputs.cmake-args }} run: | mkdir build cd build diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a088cc0c..186e5b670 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,6 @@ if(DEFINED LF_SINGLE_THREADED) add_compile_definitions(LF_SINGLE_THREADED=1) endif() -set(Test test) set(Lib lib) set(CoreLibPath core) set(CoreLib reactor-c) @@ -39,7 +38,6 @@ include_directories(${CMAKE_SOURCE_DIR}/include/core/utils) include_directories(${CMAKE_SOURCE_DIR}/include/api) enable_testing() -add_subdirectory(${Test}) add_subdirectory(${Lib}) add_subdirectory(${CoreLibPath}) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt deleted file mode 100644 index 89ed5d967..000000000 --- a/test/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_library(test-lib STATIC src_gen_stub.c rand_utils.c) -target_link_libraries(test-lib PRIVATE lf::low-level-platform-api) diff --git a/test/Tests.cmake b/test/Tests.cmake index 3b4a135ff..2d5fca40e 100644 --- a/test/Tests.cmake +++ b/test/Tests.cmake @@ -46,3 +46,60 @@ foreach(FILE ${TEST_FILES}) # Warnings as errors lf_enable_compiler_warnings(${NAME}) endforeach(FILE ${TEST_FILES}) + +# Add the test for the RTI. +if (NOT DEFINED LF_SINGLE_THREADED) + # Check which system we are running on to select the correct platform support + # file and assign the file's path to LF_PLATFORM_FILE + # FIXME: This is effectively a second build script for the RTI that we have to maintain. This is code duplication. + # FIXME: We should not be reaching into the platform directory and bypassing its CMake build. + if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") + set(LF_PLATFORM_FILE ${LF_ROOT}/low_level_platform/impl/src/lf_linux_support.c) + elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") + set(LF_PLATFORM_FILE ${LF_ROOT}/low_level_platform/impl/src/lf_macos_support.c) + else() + message(FATAL_ERROR "Your platform is not supported! RTI supports Linux and MacOS.") + endif() + + set(IncludeDir include/core) + + set(RTI_DIR ${CoreLibPath}/federated/RTI) + add_executable( + rti_common_test + ${TEST_DIR}/RTI/rti_common_test.c + ${RTI_DIR}/rti_common.c + ${RTI_DIR}/rti_remote.c + ${CoreLibPath}/tracepoint.c + ${LF_PLATFORM_FILE} + ${LF_ROOT}/low_level_platform/impl/src/lf_platform_util.c + ${LF_ROOT}/low_level_platform/impl/src/lf_atomic_gcc_clang.c + ${LF_ROOT}/low_level_platform/impl/src/lf_unix_clock_support.c + ${CoreLibPath}/utils/util.c + ${CoreLibPath}/tag.c + ${CoreLibPath}/clock.c + ${CoreLibPath}/federated/network/net_util.c + ${CoreLibPath}/utils/pqueue_base.c + ${CoreLibPath}/utils/pqueue_tag.c + ${CoreLibPath}/utils/pqueue.c + ) + add_test(NAME rti_common_test COMMAND rti_common_test) + target_include_directories(rti_common_test PUBLIC ${RTI_DIR}) + target_include_directories(rti_common_test PUBLIC ${IncludeDir}) + target_include_directories(rti_common_test PUBLIC ${IncludeDir}/federated) + target_include_directories(rti_common_test PUBLIC ${IncludeDir}/modal_models) + target_link_libraries(rti_common_test lf::low-level-platform-api) + target_link_libraries(rti_common_test lf::logging-api) + target_include_directories(rti_common_test PUBLIC ${IncludeDir}/utils) + # Set the STANDALONE_RTI flag to include the rti_remote and rti_common. + target_compile_definitions(rti_common_test PUBLIC STANDALONE_RTI=1) + + # Set FEDERATED to get federated compilation support + target_compile_definitions(rti_common_test PUBLIC FEDERATED=1) + + target_compile_definitions(rti_common_test PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) + + # Find threads and link to it + find_package(Threads REQUIRED) + target_link_libraries(rti_common_test Threads::Threads) +endif() + \ No newline at end of file diff --git a/trace/impl/lib/lf-trace-impl.a b/trace/impl/lib/lf-trace-impl.a deleted file mode 100644 index 131083279272a66b87162e7d9171ba5f37434869..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10034 zcmcIqeQaCR6~B&~5;~fs+X`iTJu1?ru$r_hqeX?eA1{6DN-1qEs&1X@*w2l@v4j2G zG^GJ=X)BBsscf1A`(vxNN+9hEZLqO!D@iF0P;Dz8LIqL57)+@WX=Mx+N>O&sedpN6 z4?hhFxYF~x_jf+;x#!+@?@g{;7R$xEZ<=wfQCm7h)2+*2!&rFz65`l4Ln$>Kp7-iK z>11?=oy(`PnW&SC#Z#H>3LvURbN2RB-m!BUu%q@4JL5gi2~;-T!dhF@>Lws^7b<qliQKgD=tM#z1hE=ngsorV1{A|Q3y=4tfbXcWg(`H2&Gyv-Uk$HzM|hc~Z7!h2wTP;lBk=KLOC>(KFhyxbm^-Y9~niIIk~>f@Io6LG$$yN!>voriQRvxu~$0wgjfT~Aen*DtD1DN(eNA$(|&94X#2Oe%KX>C z0@ag;iojr%j@5iRhOR@g8mQz3jXpYihYNG{3SNfZx)hQrjaCy~gcc&JryQ8%`BnxK zqb5ue2+Y?J$NYnN<%GCVj-2B#@uYKRAqdRwcwPOe{;JbrOU)anV0W+smKKjg)Eqgq22_1y3ow2g3kT z#J_R zoJMW6y7=PijU1Ni;r?ol@jA2>CVQo?w0LiQ5>uGUZOb>T`^LJ}HyF#~aXX(koUEa@ z`$jUAwvA*AeiI=$vvlejlKXNg$0nw3JC?9>A!8GsJY12!R61>R*+wc8&%%kpcI=Sq zQ>tR8swmrQXN+`qJIguUhMmi0b4EOyu#L7vXkAv5StA;D>8sKCzF zz$F))*R&s!`clC0!BRwKv$=I&!^)Y{gMC3@(X?fns?-ZO2`cnw15E!m(M^0aeK%5- zGT_+_4;G!ROg9IE4X?O7X#W5*K*{3|^EBM98IV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D8Bx#IZ~C8LTJ^Q!PUMW&(Ae zoBCUXIU24YZlwt)FO;V#zfld!(z2_+sYu7Q8gbe_BBaX?J$4!Q+Q&JGI=%7-NPeuy zlJ486ZK{RPMxB@!vameo0n^xjZsWNR<#7(l(mvB|@%Mvi>_5L(0Dk|^^oOpPlctP zZN!(f;IC2-pmHb|z=MAr^va`7@7M#RPJ97>mAc;3F;92YEa0gp-!xZ?EBMPrP4UYA E51afwHvj+t From 67445051393f00920cf1ebf26b18333b0cfcc497 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Sun, 28 Apr 2024 20:44:10 +0200 Subject: [PATCH 05/10] Document the unit test --- README.md | 5 +++++ test/scheduling/scheduling_api_test.c | 16 ++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3d1313a1f..8c714a165 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,11 @@ To run tests for the multithreaded runtime, provide a nonzero number of workers when invoking `cmake`. For example: - `cmake .. -DNUMBER_OF_WORKERS=2` +- `cmake --build .` +- `sudo make test` + +Note that one of the tests in the multithreaded test suite requires sudo because +it changes the scheduling policy and priorities. To define/undefine other preprocessor definitions such as `LOG_LEVEL`, pass them as arguments to `cmake` in the same way as with `NUMBER_OF_WORKERS`, using the same diff --git a/test/scheduling/scheduling_api_test.c b/test/scheduling/scheduling_api_test.c index 0078cff6b..a49117590 100644 --- a/test/scheduling/scheduling_api_test.c +++ b/test/scheduling/scheduling_api_test.c @@ -1,3 +1,6 @@ +/** + * This tests the real-time scheduling API implementation in Linux. + */ #include #include #include "core/utils/util.h" @@ -16,7 +19,7 @@ int main() { lf_print_error_and_exit("lf_thread_set_cpu failed with %d", res); } - // // Pick SCHED_FIFO + // Configure SCHED_FIFO { lf_scheduling_policy_t cfg; cfg.policy = LF_SCHED_PRIORITY; @@ -28,7 +31,7 @@ int main() { } } - // Set SCHED_RR + // Configure SCHED_RR { lf_scheduling_policy_t cfg; cfg.policy = LF_SCHED_TIMESLICE; @@ -58,11 +61,13 @@ int main() { lf_print_error_and_exit("lf_thread_set_priority failed with %d", res); } + // Try negative priority res = lf_thread_set_priority(lf_thread_self(), -50); if (res == 0) { lf_print_error_and_exit("lf_thread_set_priority should have failed for -50"); } + // Configure back to SCHED_OTHER { lf_scheduling_policy_t cfg; cfg.policy = LF_SCHED_FAIR; @@ -72,14 +77,9 @@ int main() { } } - // Try to high CPU + // Try pinning to non-existant CPU core. res = lf_thread_set_cpu(lf_thread_self(), lf_available_cores()); if (res == 0) { lf_print_error_and_exit("lf_thread_set_cpu should fail for too high CPU id"); } - - res = lf_thread_set_cpu(lf_thread_self(), -1); - if (res == 0) { - lf_print_error_and_exit("lf_thread_set_cpu should fail for too low CPU id"); - } } \ No newline at end of file From edcf3003e1e523ded3d85bed6b8976ab712c798b Mon Sep 17 00:00:00 2001 From: erlingrj Date: Sun, 28 Apr 2024 21:20:33 +0200 Subject: [PATCH 06/10] Move RTI unit test to the RTI folder which is already somewhat standalone --- .github/workflows/unit-tests.yml | 23 ++++-- .gitignore | 3 + core/federated/RTI/CMakeLists.txt | 81 +++++++++++-------- core/federated/RTI/README.md | 6 ++ .../federated/RTI/test}/rti_common_test.c | 4 +- test/Tests.cmake | 57 ------------- 6 files changed, 77 insertions(+), 97 deletions(-) rename {test/RTI => core/federated/RTI/test}/rti_common_test.c (99%) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 84683136c..52ae82ad4 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -18,16 +18,29 @@ jobs: - name: Check out reactor-c repository uses: actions/checkout@v2 - - name: Build unit tests ${{ inputs.cmake-args }} + - name: Build and run unit tests ${{ inputs.cmake-args }} (linux) run: | mkdir build cd build cmake .. ${{ inputs.cmake-args }} cmake --build . - - name: Run unit tests with sudo. (Needed for changing prio/policy) - run: sudo make test + sudo make test if: matrix.platform == 'ubuntu-latest' - - name: Run unit tests normal. - run: cd build && make test + - name: Build and run unit tests ${{ inputs.cmake-args }} (macos/win) + run: | + mkdir build + cd build + cmake .. ${{ inputs.cmake-args }} + cmake --build . + make test if: matrix.platform != 'ubuntu-latest' + - name: Run RTI unit tests + run: | + cd core/federated/RTI + mkdir build + cd build + cmake .. + cmake --build . + ctest + if: matrix.platform == 'ubuntu-latest' || matrix.platform == 'macos-latest' diff --git a/.gitignore b/.gitignore index b8237101d..d11ccaa7a 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ util/tracing/trace_to_csv.o util/tracing/trace_to_influxdb util/tracing/trace_to_influxdb.o util/tracing/trace_util.o + +# Generated trace lib +trace/**/*.a diff --git a/core/federated/RTI/CMakeLists.txt b/core/federated/RTI/CMakeLists.txt index 9c4f996d2..ae029588b 100644 --- a/core/federated/RTI/CMakeLists.txt +++ b/core/federated/RTI/CMakeLists.txt @@ -39,20 +39,13 @@ project(RTI VERSION 1.0.0 LANGUAGES C) set(CoreLib ../../../core) set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../..) - set(IncludeDir ../../../include/core) -include_directories(../../../include) -include_directories(${IncludeDir}) -include_directories(${IncludeDir}/federated) -include_directories(${IncludeDir}/federated/network) -include_directories(${IncludeDir}/modal_models) -include_directories(${IncludeDir}/utils) - - -# Declare a new executable target and list all its sources -add_executable( - RTI - main.c +set(RTI_LIB rti_lib) +set(RTI_EXE RTI) + + +# Add common RTI functionality to a static library +add_library(${RTI_LIB} rti_common.c rti_remote.c ${CoreLib}/tracepoint.c @@ -64,6 +57,15 @@ add_executable( ${CoreLib}/utils/pqueue_tag.c ${CoreLib}/utils/pqueue.c ) + +target_include_directories(${RTI_LIB} PUBLIC ../../../include) +target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}) +target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/federated) +target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/federated/network) +target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/modal_models) +target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/utils) + +add_executable(${RTI_EXE} main.c) if (NOT DEFINED LOG_LEVEL) set(LOG_LEVEL 0) ENDIF(NOT DEFINED LOG_LEVEL) @@ -73,62 +75,77 @@ IF(CMAKE_BUILD_TYPE MATCHES DEBUG) message("-- Building RTI with DEBUG messages enabled") set(LOG_LEVEL 4) ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG) -target_compile_definitions(RTI PUBLIC LOG_LEVEL=${LOG_LEVEL}) +target_compile_definitions(${RTI_EXE} PUBLIC LOG_LEVEL=${LOG_LEVEL}) include(${LF_ROOT}/version/api/CMakeLists.txt) -target_link_libraries(RTI lf::version-api) +target_link_libraries(${RTI_LIB} PUBLIC lf::version-api) include(${LF_ROOT}/logging/api/CMakeLists.txt) -target_link_libraries(RTI lf::logging-api) +target_link_libraries(${RTI_LIB} PUBLIC lf::logging-api) include(${LF_ROOT}/tag/api/CMakeLists.txt) -target_link_libraries(RTI lf::tag-api) +target_link_libraries(${RTI_LIB} PUBLIC lf::tag-api) include(${LF_ROOT}/platform/api/CMakeLists.txt) -target_link_libraries(RTI lf::platform-api) +target_link_libraries(${RTI_LIB} PUBLIC lf::platform-api) include(${LF_ROOT}/platform/impl/CMakeLists.txt) -target_link_libraries(RTI lf::platform-impl) +target_link_libraries(${RTI_LIB} PUBLIC lf::platform-impl) include(${LF_ROOT}/trace/api/CMakeLists.txt) -target_link_libraries(RTI lf::trace-api) +target_link_libraries(${RTI_LIB} PUBLIC lf::trace-api) include(${LF_ROOT}/trace/impl/CMakeLists.txt) -target_link_libraries(RTI lf::trace-impl) +target_link_libraries(${RTI_LIB} PUBLIC lf::trace-impl) include(${LF_ROOT}/low_level_platform/impl/CMakeLists.txt) -target_link_libraries(RTI lf::low-level-platform-impl) +target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-impl) include(${LF_ROOT}/low_level_platform/api/CMakeLists.txt) -target_link_libraries(RTI lf::low-level-platform-api) +target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-api) + +target_link_libraries(${RTI_EXE} PRIVATE ${RTI_LIB}) # Set the STANDALONE_RTI flag to include the rti_remote and rti_common. -target_compile_definitions(RTI PUBLIC STANDALONE_RTI=1) +target_compile_definitions(${RTI_LIB} PUBLIC STANDALONE_RTI=1) # Set FEDERATED to get federated compilation support -target_compile_definitions(RTI PUBLIC FEDERATED=1) - -target_compile_definitions(RTI PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) +target_compile_definitions(${RTI_LIB} PUBLIC FEDERATED=1) +target_compile_definitions(${RTI_LIB} PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) # Set RTI Tracing -target_compile_definitions(RTI PUBLIC RTI_TRACE) +target_compile_definitions(${RTI_LIB} PUBLIC RTI_TRACE) # Warnings as errors -target_compile_options(RTI PUBLIC -Werror) +target_compile_options(${RTI_LIB} PUBLIC -Werror) # Find threads and link to it find_package(Threads REQUIRED) -target_link_libraries(RTI Threads::Threads) +target_link_libraries(${RTI_LIB} PUBLIC Threads::Threads) # Option for enabling federate authentication by RTI. option(AUTH "Federate authentication by RTI enabled." OFF) IF(AUTH MATCHES ON) - add_compile_definitions(__RTI_AUTH__) + target_compile_definitions(${RTI_LIB} PUBLIC __RTI_AUTH__) # Find OpenSSL and link to it find_package(OpenSSL REQUIRED) - target_link_libraries(RTI OpenSSL::SSL) + target_link_libraries(${RTI_LIB} PUBLIC OpenSSL::SSL) ENDIF(AUTH MATCHES ON) install( TARGETS RTI DESTINATION bin ) + +# Build unit tests +enable_testing() +set(TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test) +set(TEST_SRCS + ${TEST_DIR}/rti_common_test.c +) +foreach(TEST_SRC ${TEST_SRCS}) + get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE) + add_executable(${TEST_NAME} ${TEST_SRC}) + add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) + target_link_libraries(${TEST_NAME} PUBLIC ${RTI_LIB}) + target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +endforeach() diff --git a/core/federated/RTI/README.md b/core/federated/RTI/README.md index 916959f6e..cea8581c5 100644 --- a/core/federated/RTI/README.md +++ b/core/federated/RTI/README.md @@ -8,6 +8,11 @@ make sudo make install ``` +To run the unit tests +```bash +make test +``` + **Note:** To enable DEBUG messages, use the following build commands instead: ```bash @@ -47,3 +52,4 @@ docker login -u [username] ``` To authenticate, request a PAT on [DockerHub](https://hub.docker.com/settings/security). + diff --git a/test/RTI/rti_common_test.c b/core/federated/RTI/test/rti_common_test.c similarity index 99% rename from test/RTI/rti_common_test.c rename to core/federated/RTI/test/rti_common_test.c index 6bf21b312..2937048a2 100644 --- a/test/RTI/rti_common_test.c +++ b/core/federated/RTI/test/rti_common_test.c @@ -1,4 +1,3 @@ -#if defined STANDALONE_RTI #include #include #include @@ -256,5 +255,4 @@ int main() { two_nodes_zero_delay(); two_nodes_normal_delay(); multiple_nodes(); -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/test/Tests.cmake b/test/Tests.cmake index 2d5fca40e..3b4a135ff 100644 --- a/test/Tests.cmake +++ b/test/Tests.cmake @@ -46,60 +46,3 @@ foreach(FILE ${TEST_FILES}) # Warnings as errors lf_enable_compiler_warnings(${NAME}) endforeach(FILE ${TEST_FILES}) - -# Add the test for the RTI. -if (NOT DEFINED LF_SINGLE_THREADED) - # Check which system we are running on to select the correct platform support - # file and assign the file's path to LF_PLATFORM_FILE - # FIXME: This is effectively a second build script for the RTI that we have to maintain. This is code duplication. - # FIXME: We should not be reaching into the platform directory and bypassing its CMake build. - if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - set(LF_PLATFORM_FILE ${LF_ROOT}/low_level_platform/impl/src/lf_linux_support.c) - elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") - set(LF_PLATFORM_FILE ${LF_ROOT}/low_level_platform/impl/src/lf_macos_support.c) - else() - message(FATAL_ERROR "Your platform is not supported! RTI supports Linux and MacOS.") - endif() - - set(IncludeDir include/core) - - set(RTI_DIR ${CoreLibPath}/federated/RTI) - add_executable( - rti_common_test - ${TEST_DIR}/RTI/rti_common_test.c - ${RTI_DIR}/rti_common.c - ${RTI_DIR}/rti_remote.c - ${CoreLibPath}/tracepoint.c - ${LF_PLATFORM_FILE} - ${LF_ROOT}/low_level_platform/impl/src/lf_platform_util.c - ${LF_ROOT}/low_level_platform/impl/src/lf_atomic_gcc_clang.c - ${LF_ROOT}/low_level_platform/impl/src/lf_unix_clock_support.c - ${CoreLibPath}/utils/util.c - ${CoreLibPath}/tag.c - ${CoreLibPath}/clock.c - ${CoreLibPath}/federated/network/net_util.c - ${CoreLibPath}/utils/pqueue_base.c - ${CoreLibPath}/utils/pqueue_tag.c - ${CoreLibPath}/utils/pqueue.c - ) - add_test(NAME rti_common_test COMMAND rti_common_test) - target_include_directories(rti_common_test PUBLIC ${RTI_DIR}) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}/federated) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}/modal_models) - target_link_libraries(rti_common_test lf::low-level-platform-api) - target_link_libraries(rti_common_test lf::logging-api) - target_include_directories(rti_common_test PUBLIC ${IncludeDir}/utils) - # Set the STANDALONE_RTI flag to include the rti_remote and rti_common. - target_compile_definitions(rti_common_test PUBLIC STANDALONE_RTI=1) - - # Set FEDERATED to get federated compilation support - target_compile_definitions(rti_common_test PUBLIC FEDERATED=1) - - target_compile_definitions(rti_common_test PUBLIC PLATFORM_${CMAKE_SYSTEM_NAME}) - - # Find threads and link to it - find_package(Threads REQUIRED) - target_link_libraries(rti_common_test Threads::Threads) -endif() - \ No newline at end of file From 76bce7de5ffa85931ec072acc8e0752244e2f303 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Mon, 29 Apr 2024 00:33:20 +0200 Subject: [PATCH 07/10] Dont do unit tests on Windows (they arent running in master either) --- .github/workflows/unit-tests.yml | 14 ++------------ test/Tests.cmake | 2 -- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 52ae82ad4..1ced71f16 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -11,29 +11,20 @@ jobs: run: strategy: matrix: - platform: [ubuntu-latest, macos-latest, windows-latest] + platform: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.platform }} steps: - name: Check out reactor-c repository uses: actions/checkout@v2 - - name: Build and run unit tests ${{ inputs.cmake-args }} (linux) + - name: Build and run unit tests ${{ inputs.cmake-args }} run: | mkdir build cd build cmake .. ${{ inputs.cmake-args }} cmake --build . sudo make test - if: matrix.platform == 'ubuntu-latest' - - name: Build and run unit tests ${{ inputs.cmake-args }} (macos/win) - run: | - mkdir build - cd build - cmake .. ${{ inputs.cmake-args }} - cmake --build . - make test - if: matrix.platform != 'ubuntu-latest' - name: Run RTI unit tests run: | cd core/federated/RTI @@ -42,5 +33,4 @@ jobs: cmake .. cmake --build . ctest - if: matrix.platform == 'ubuntu-latest' || matrix.platform == 'macos-latest' diff --git a/test/Tests.cmake b/test/Tests.cmake index 3b4a135ff..a5b8c2ccc 100644 --- a/test/Tests.cmake +++ b/test/Tests.cmake @@ -26,8 +26,6 @@ if(NUMBER_OF_WORKERS) if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") add_test_dir(${TEST_DIR}/scheduling) endif() -else() - add_test_dir(${TEST_DIR}/single-threaded) endif(NUMBER_OF_WORKERS) # Create executables for each test. From 0fe300f9ae4aa658ec1a0c5dda31ec59eaab7509 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Mon, 29 Apr 2024 10:07:40 +0200 Subject: [PATCH 08/10] Format and document the RTI CMakeLists --- core/federated/RTI/CMakeLists.txt | 56 +++++++------------------------ 1 file changed, 12 insertions(+), 44 deletions(-) diff --git a/core/federated/RTI/CMakeLists.txt b/core/federated/RTI/CMakeLists.txt index ae029588b..5bfbf0196 100644 --- a/core/federated/RTI/CMakeLists.txt +++ b/core/federated/RTI/CMakeLists.txt @@ -1,39 +1,3 @@ -# This is a cmake build script providing a solution for compiling -# the RTI in this directory.. -# -# Usage: -# -# To compile with cmake, run the following commands: -# -# $> mkdir build && cd build -# $> cmake ../ -# $> make -# $> sudo make install -# -# This create a binary RTI in the current working directory. Please put this in -# a directory that is on the path. -# -# To enable DEBUG messages, use the following build commands instead: -# -# $> mkdir build && cd build -# $> cmake -DCMAKE_BUILD_TYPE=DEBUG ../ -# $> make -# $> sudo make install -# -# If you would like to go back to non-DEBUG mode, you would have to remove all -# contents of the `build` folder. - -# To enable simple HMAC-based authentication of federates, -# add `-DAUTH=ON` option to the cmake command as shown below: -# -# $> mkdir build && cd build -# $> cmake -DAUTH=ON ../ -# $> make -# $> sudo make install -# -# If you would like to go back to non-AUTH mode, you would have to remove all -# contents of the `build` folder. - cmake_minimum_required(VERSION 3.12) project(RTI VERSION 1.0.0 LANGUAGES C) @@ -41,11 +5,11 @@ set(CoreLib ../../../core) set(LF_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../..) set(IncludeDir ../../../include/core) set(RTI_LIB rti_lib) -set(RTI_EXE RTI) - +set(RTI_MAIN RTI) -# Add common RTI functionality to a static library -add_library(${RTI_LIB} +# Add common RTI functionality to a static library. This is done to simplify +# the building of unit tests. +add_library(${RTI_LIB} STATIC rti_common.c rti_remote.c ${CoreLib}/tracepoint.c @@ -58,6 +22,9 @@ add_library(${RTI_LIB} ${CoreLib}/utils/pqueue.c ) +# Add the main target which will link with the library. +add_executable(${RTI_MAIN} main.c) + target_include_directories(${RTI_LIB} PUBLIC ../../../include) target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}) target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/federated) @@ -65,7 +32,6 @@ target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/federated/network) target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/modal_models) target_include_directories(${RTI_LIB} PUBLIC ${IncludeDir}/utils) -add_executable(${RTI_EXE} main.c) if (NOT DEFINED LOG_LEVEL) set(LOG_LEVEL 0) ENDIF(NOT DEFINED LOG_LEVEL) @@ -75,7 +41,7 @@ IF(CMAKE_BUILD_TYPE MATCHES DEBUG) message("-- Building RTI with DEBUG messages enabled") set(LOG_LEVEL 4) ENDIF(CMAKE_BUILD_TYPE MATCHES DEBUG) -target_compile_definitions(${RTI_EXE} PUBLIC LOG_LEVEL=${LOG_LEVEL}) +target_compile_definitions(${RTI_LIB} PUBLIC LOG_LEVEL=${LOG_LEVEL}) include(${LF_ROOT}/version/api/CMakeLists.txt) target_link_libraries(${RTI_LIB} PUBLIC lf::version-api) @@ -104,8 +70,6 @@ target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-impl) include(${LF_ROOT}/low_level_platform/api/CMakeLists.txt) target_link_libraries(${RTI_LIB} PUBLIC lf::low-level-platform-api) -target_link_libraries(${RTI_EXE} PRIVATE ${RTI_LIB}) - # Set the STANDALONE_RTI flag to include the rti_remote and rti_common. target_compile_definitions(${RTI_LIB} PUBLIC STANDALONE_RTI=1) @@ -118,6 +82,7 @@ target_compile_definitions(${RTI_LIB} PUBLIC RTI_TRACE) # Warnings as errors target_compile_options(${RTI_LIB} PUBLIC -Werror) + # Find threads and link to it find_package(Threads REQUIRED) target_link_libraries(${RTI_LIB} PUBLIC Threads::Threads) @@ -131,6 +96,9 @@ IF(AUTH MATCHES ON) target_link_libraries(${RTI_LIB} PUBLIC OpenSSL::SSL) ENDIF(AUTH MATCHES ON) +# Link the main target with the library. +target_link_libraries(${RTI_MAIN} PRIVATE ${RTI_LIB}) + install( TARGETS RTI DESTINATION bin From 83f9e7a81f99115da1564a5123791abfc4f1378d Mon Sep 17 00:00:00 2001 From: erlingrj Date: Mon, 29 Apr 2024 17:54:33 +0200 Subject: [PATCH 09/10] Add newline at EOF --- test/scheduling/scheduling_api_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scheduling/scheduling_api_test.c b/test/scheduling/scheduling_api_test.c index a49117590..3315fe0e8 100644 --- a/test/scheduling/scheduling_api_test.c +++ b/test/scheduling/scheduling_api_test.c @@ -82,4 +82,4 @@ int main() { if (res == 0) { lf_print_error_and_exit("lf_thread_set_cpu should fail for too high CPU id"); } -} \ No newline at end of file +} From a1e043b5572e49af8e4e639f02e55d2fc8d09f7a Mon Sep 17 00:00:00 2001 From: erling Date: Mon, 29 Apr 2024 18:20:41 +0200 Subject: [PATCH 10/10] Update core/federated/RTI/test/rti_common_test.c Co-authored-by: Marten Lohstroh --- core/federated/RTI/test/rti_common_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/federated/RTI/test/rti_common_test.c b/core/federated/RTI/test/rti_common_test.c index 2937048a2..107d08057 100644 --- a/core/federated/RTI/test/rti_common_test.c +++ b/core/federated/RTI/test/rti_common_test.c @@ -255,4 +255,4 @@ int main() { two_nodes_zero_delay(); two_nodes_normal_delay(); multiple_nodes(); -} \ No newline at end of file +}