diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 248b236..167a6e8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,4 @@ add_subdirectory(log) -add_subdirectory(times) add_subdirectory(tuple) add_subdirectory(packet) add_subdirectory(packet_io) diff --git a/src/stellar/CMakeLists.txt b/src/stellar/CMakeLists.txt index 5f665eb..f7b3826 100644 --- a/src/stellar/CMakeLists.txt +++ b/src/stellar/CMakeLists.txt @@ -1,12 +1,14 @@ set(SOURCE stellar_config.cpp stellar_stat.cpp stellar_core.cpp) -set(LIBRARY times session_manager plugin_manager ip_reassembly packet_io packet pthread fieldstat4 toml) +set(LIBRARY session_manager plugin_manager ip_reassembly packet_io packet pthread fieldstat4 toml) add_library(stellar_core STATIC ${SOURCE}) target_link_libraries(stellar_core PUBLIC ${LIBRARY}) +target_include_directories(stellar_core PUBLIC ${CMAKE_SOURCE_DIR}/src/utils) add_library(stellar_devel SHARED ${SOURCE}) set_target_properties(stellar_devel PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map") target_link_libraries(stellar_devel PRIVATE -Wl,--whole-archive ${LIBRARY} -Wl,--no-whole-archive) +target_include_directories(stellar_devel PUBLIC ${CMAKE_SOURCE_DIR}/src/utils) add_executable(stellar main.cpp) target_link_libraries(stellar PRIVATE -Wl,--whole-archive stellar_core ${LIBRARY} -Wl,--no-whole-archive) diff --git a/src/stellar/stellar_core.cpp b/src/stellar/stellar_core.cpp index 898a794..a403428 100644 --- a/src/stellar/stellar_core.cpp +++ b/src/stellar/stellar_core.cpp @@ -10,7 +10,7 @@ #include "log.h" #include "logo.h" -#include "times.h" +#include "utils.h" #include "packet_io.h" #include "packet_def.h" #include "packet_utils.h" @@ -209,7 +209,7 @@ static void *work_thread(void *arg) * Note: Modifying the system time will affect the timing wheel, impacting session expiration, IP reassembly expiration, and TCP reassembly expiration. * Suggestion: After modifying the system time, restart the service to ensure consistent timing. */ - now_ms = stellar_get_real_time_msec(); + now_ms = clock_get_real_time_ms(); nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX); if (nr_recv == 0) { @@ -398,7 +398,7 @@ static int all_session_have_freed(struct stellar_runtime *runtime, struct stella static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config) { - uint64_t now_ms = stellar_get_real_time_msec(); + uint64_t now_ms = clock_get_real_time_ms(); for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; @@ -543,12 +543,12 @@ int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unu goto error_out; } - runtime->stat_last_output_ts = stellar_get_real_time_msec(); + runtime->stat_last_output_ts = clock_get_real_time_ms(); while (!ATOMIC_READ(&need_exit)) { - if (stellar_get_real_time_msec() - runtime->stat_last_output_ts > config->sched_opts.output_stat_interval) + if (clock_get_real_time_ms() - runtime->stat_last_output_ts > config->sched_opts.output_stat_interval) { - runtime->stat_last_output_ts = stellar_get_real_time_msec(); + runtime->stat_last_output_ts = clock_get_real_time_ms(); stellar_stat_output(runtime->stat); } usleep(1000); // 1ms diff --git a/src/times/CMakeLists.txt b/src/times/CMakeLists.txt deleted file mode 100644 index 864d16f..0000000 --- a/src/times/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(times times.cpp) -target_include_directories(times PUBLIC ${CMAKE_CURRENT_LIST_DIR}) -target_include_directories(times PUBLIC ${CMAKE_SOURCE_DIR}/src/stellar) -target_link_libraries(times) - -add_subdirectory(test) \ No newline at end of file diff --git a/src/times/test/CMakeLists.txt b/src/times/test/CMakeLists.txt deleted file mode 100644 index da85e5c..0000000 --- a/src/times/test/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable(gtest_times gtest_times.cpp) -target_link_libraries(gtest_times times gtest) - -include(GoogleTest) -gtest_discover_tests(gtest_times) \ No newline at end of file diff --git a/src/times/test/gtest_times.cpp b/src/times/test/gtest_times.cpp deleted file mode 100644 index 9ec7988..0000000 --- a/src/times/test/gtest_times.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include - -#include "times.h" - -TEST(TIMES, TEST) -{ - uint64_t last_sec = 0; - uint64_t last_msec = 0; - uint64_t curr_sec = 0; - uint64_t curr_msec = 0; - - last_sec = stellar_get_monotonic_time_sec(); - last_msec = stellar_get_monotonic_time_msec(); - - usleep(1000); // 1ms - - curr_sec = stellar_get_monotonic_time_sec(); - curr_msec = stellar_get_monotonic_time_msec(); - printf("After usleep(1000)\n"); - printf("last_sec: %lu, last_msec: %lu\n", last_sec, last_msec); - printf("curr_sec: %lu, curr_msec: %lu\n", curr_sec, curr_msec); - EXPECT_TRUE(curr_sec == last_sec); - EXPECT_TRUE(curr_msec - last_msec >= 1); - - usleep(1000 * 1000); // 1s - - last_sec = curr_sec; - last_msec = curr_msec; - curr_sec = stellar_get_monotonic_time_sec(); - curr_msec = stellar_get_monotonic_time_msec(); - printf("After usleep(1000 * 1000)\n"); - printf("last_sec: %lu, last_msec: %lu\n", last_sec, last_msec); - printf("curr_sec: %lu, curr_msec: %lu\n", curr_sec, curr_msec); - EXPECT_TRUE(curr_sec - last_sec == 1); - EXPECT_TRUE(curr_msec - last_msec >= 1000); -} - -int main(int argc, char **argv) -{ - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} diff --git a/src/times/times.cpp b/src/times/times.cpp deleted file mode 100644 index 70343c5..0000000 --- a/src/times/times.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include - -#include "times.h" - -// 1 s = 1000 ms -// 1 ms = 1000 us -// 1 us = 1000 ns - -/* - * The maximum number of seconds that can be stored in the time_t value is 2147483647 –- a little over 68 years. - * - * struct timespec - * { - * time_t tv_sec; // seconds - * long tv_nsec; // nanoseconds - * }; - */ - -/* - * CLOCK_MONOTONIC - * - * On Linux, that point corresponds to the number of sec‐ - * onds that the system has been running since it was booted. - * - * The CLOCK_MONOTONIC clock is not affected by discontinuous - * jumps in the system time (e.g., if the system administrator - * manually changes the clock), but is affected by the incremen‐ - * tal adjustments performed by adjtime(3) and NTP. - */ - -#define TIMESPEC_TO_MSEC(ts) ((ts).tv_sec * 1000 + (ts).tv_nsec / 1000000) -#define TIMESPEC_TO_SEC(ts) ((ts).tv_sec + (ts).tv_nsec / 1000000000) - -uint64_t stellar_get_monotonic_time_sec() -{ - struct timespec now; - clock_gettime(CLOCK_MONOTONIC_COARSE, &now); - - return TIMESPEC_TO_SEC(now); -} - -uint64_t stellar_get_monotonic_time_msec() -{ - struct timespec now; - clock_gettime(CLOCK_MONOTONIC_COARSE, &now); - - return TIMESPEC_TO_MSEC(now); -} - -uint64_t stellar_get_real_time_sec() -{ - struct timespec now; - clock_gettime(CLOCK_REALTIME_COARSE, &now); - - return TIMESPEC_TO_SEC(now); -} - -uint64_t stellar_get_real_time_msec() -{ - struct timespec now; - clock_gettime(CLOCK_REALTIME_COARSE, &now); - - return TIMESPEC_TO_MSEC(now); -} diff --git a/src/times/times.h b/src/times/times.h deleted file mode 100644 index b4d6cf1..0000000 --- a/src/times/times.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include - -uint64_t stellar_get_monotonic_time_sec(); -uint64_t stellar_get_monotonic_time_msec(); - -uint64_t stellar_get_real_time_sec(); -uint64_t stellar_get_real_time_msec(); - -#ifdef __cplusplus -} -#endif diff --git a/src/utils/utils.h b/src/utils/utils.h index 7155d9f..42ee31e 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -7,6 +7,31 @@ extern "C" #include #include +#include + +/* + * The maximum number of seconds that can be stored in the time_t value is 2147483647 –- a little over 68 years. + * + * struct timespec + * { + * time_t tv_sec; // seconds + * long tv_nsec; // nanoseconds + * }; + * + * 1 s = 1000 ms + * 1 ms = 1000 us + * 1 us = 1000 ns + */ + +#define TIMESPEC_TO_MSEC(ts) ((ts).tv_sec * 1000 + (ts).tv_nsec / 1000000) + +static inline uint64_t clock_get_real_time_ms() +{ + struct timespec now; + clock_gettime(CLOCK_REALTIME_COARSE, &now); + + return TIMESPEC_TO_MSEC(now); +} static inline void hexdump_to_fd(int fd, uint32_t idx, const char *data, uint16_t len) {