refactor: move times API to utils.h
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
add_subdirectory(log)
|
add_subdirectory(log)
|
||||||
add_subdirectory(times)
|
|
||||||
add_subdirectory(tuple)
|
add_subdirectory(tuple)
|
||||||
add_subdirectory(packet)
|
add_subdirectory(packet)
|
||||||
add_subdirectory(packet_io)
|
add_subdirectory(packet_io)
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
set(SOURCE stellar_config.cpp stellar_stat.cpp stellar_core.cpp)
|
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})
|
add_library(stellar_core STATIC ${SOURCE})
|
||||||
target_link_libraries(stellar_core PUBLIC ${LIBRARY})
|
target_link_libraries(stellar_core PUBLIC ${LIBRARY})
|
||||||
|
target_include_directories(stellar_core PUBLIC ${CMAKE_SOURCE_DIR}/src/utils)
|
||||||
|
|
||||||
add_library(stellar_devel SHARED ${SOURCE})
|
add_library(stellar_devel SHARED ${SOURCE})
|
||||||
set_target_properties(stellar_devel PROPERTIES LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/version.map")
|
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_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)
|
add_executable(stellar main.cpp)
|
||||||
target_link_libraries(stellar PRIVATE -Wl,--whole-archive stellar_core ${LIBRARY} -Wl,--no-whole-archive)
|
target_link_libraries(stellar PRIVATE -Wl,--whole-archive stellar_core ${LIBRARY} -Wl,--no-whole-archive)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "logo.h"
|
#include "logo.h"
|
||||||
#include "times.h"
|
#include "utils.h"
|
||||||
#include "packet_io.h"
|
#include "packet_io.h"
|
||||||
#include "packet_def.h"
|
#include "packet_def.h"
|
||||||
#include "packet_utils.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.
|
* 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.
|
* 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);
|
nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX);
|
||||||
if (nr_recv == 0)
|
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)
|
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++)
|
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||||
{
|
{
|
||||||
struct stellar_thread *thread = &runtime->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;
|
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))
|
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);
|
stellar_stat_output(runtime->stat);
|
||||||
}
|
}
|
||||||
usleep(1000); // 1ms
|
usleep(1000); // 1ms
|
||||||
|
|||||||
@@ -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)
|
|
||||||
@@ -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)
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
#include <gtest/gtest.h>
|
|
||||||
|
|
||||||
#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();
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
#include <time.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C"
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
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
|
|
||||||
@@ -7,6 +7,31 @@ extern "C"
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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)
|
static inline void hexdump_to_fd(int fd, uint32_t idx, const char *data, uint16_t len)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user