diff --git a/include/stellar/session.h b/include/stellar/session.h index db027a1..7280062 100644 --- a/include/stellar/session.h +++ b/include/stellar/session.h @@ -113,6 +113,7 @@ enum session_stat MAX_STAT, }; +// realtime in seconds enum session_timestamp { SESSION_TIMESTAMP_START, diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 73b8e80..248b236 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,5 @@ add_subdirectory(log) -add_subdirectory(timestamp) +add_subdirectory(times) add_subdirectory(tuple) add_subdirectory(packet) add_subdirectory(packet_io) diff --git a/src/id_generator/id_generator.cpp b/src/id_generator/id_generator.cpp index e4b94b1..d66bd45 100644 --- a/src/id_generator/id_generator.cpp +++ b/src/id_generator/id_generator.cpp @@ -2,6 +2,7 @@ #include #include "macro.h" +#include "times.h" #include "stellar_priv.h" #include "id_generator.h" @@ -62,13 +63,11 @@ uint64_t id_generator_alloc() #define MAX_ID_PER_THREAD (32768) #define MAX_ID_BASE_TIME (268435456L) - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); // must be realtime uint64_t thr_idx = stellar_get_current_thread_index(); uint64_t global_id = 0; uint64_t id_per_thread = (global_id_generator.thread_volatile[thr_idx]++) % MAX_ID_PER_THREAD; - uint64_t id_base_time = ts.tv_sec % MAX_ID_BASE_TIME; + uint64_t id_base_time = stellar_get_real_time_sec() % MAX_ID_BASE_TIME; global_id = (global_id_generator.device_id << 51) | (thr_idx << 43) | (id_base_time << 15) | diff --git a/src/session/session_manager.cpp b/src/session/session_manager.cpp index d1c9657..cf4749e 100644 --- a/src/session/session_manager.cpp +++ b/src/session/session_manager.cpp @@ -3,6 +3,7 @@ #include #include "macro.h" +#include "times.h" #include "tcp_utils.h" #include "udp_utils.h" #include "id_generator.h" @@ -513,9 +514,7 @@ static int duplicated_packet_bypass(struct session_manager *mgr, struct session static void session_update(struct session *sess, enum session_state next_state, const struct packet *pkt, const struct tuple6 *key, enum flow_direction dir) { - struct timespec real; - clock_gettime(CLOCK_REALTIME, &real); // must be realtime - + uint64_t real_sec = stellar_get_real_time_sec(); if (session_get_state(sess) == SESSION_STATE_INIT) { session_set_id(sess, id_generator_alloc()); @@ -548,7 +547,7 @@ static void session_update(struct session *sess, enum session_state next_state, } tuple6_to_str(key, sess->tuple_str, sizeof(sess->tuple_str)); - session_set_timestamp(sess, SESSION_TIMESTAMP_START, real.tv_sec); + session_set_timestamp(sess, SESSION_TIMESTAMP_START, real_sec); switch (key->ip_proto) { case IPPROTO_TCP: @@ -580,7 +579,7 @@ static void session_update(struct session *sess, enum session_state next_state, session_set_current_packet(sess, pkt); session_set_flow_direction(sess, dir); - session_set_timestamp(sess, SESSION_TIMESTAMP_LAST, real.tv_sec); + session_set_timestamp(sess, SESSION_TIMESTAMP_LAST, real_sec); session_set_state(sess, next_state); } diff --git a/src/session/session_priv.h b/src/session/session_priv.h index 9b294ff..8261649 100644 --- a/src/session/session_priv.h +++ b/src/session/session_priv.h @@ -46,7 +46,7 @@ struct session { uint64_t id; uint64_t stats[MAX_FLOW_DIRECTION][MAX_STAT]; - uint64_t timestamps[MAX_TIMESTAMP]; + uint64_t timestamps[MAX_TIMESTAMP]; // realtime seconds struct tcp_half tcp_halfs[MAX_FLOW_DIRECTION]; struct timeout timeout; struct list_head lru; diff --git a/src/session/test/gtest_case_tcp_fast_open.cpp b/src/session/test/gtest_case_tcp_fast_open.cpp index 013b786..ed03b09 100644 --- a/src/session/test/gtest_case_tcp_fast_open.cpp +++ b/src/session/test/gtest_case_tcp_fast_open.cpp @@ -1,5 +1,6 @@ #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -302,6 +303,7 @@ TEST(CASE, TCP_FAST_OPEN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_tcp_active_to_closing.cpp b/src/session/test/gtest_state_tcp_active_to_closing.cpp index da16c32..35bf5fc 100644 --- a/src/session/test/gtest_state_tcp_active_to_closing.cpp +++ b/src/session/test/gtest_state_tcp_active_to_closing.cpp @@ -1,6 +1,7 @@ // TCP state machine test: active -> closing #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -86,6 +87,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_FIN_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -176,6 +178,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -262,6 +265,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -346,6 +350,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_DATA_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -395,6 +400,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_HALF_CLOSED_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -474,6 +480,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_HALF_CLOSED_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_tcp_init_to_opening.cpp b/src/session/test/gtest_state_tcp_init_to_opening.cpp index 506f516..bc98620 100644 --- a/src/session/test/gtest_state_tcp_init_to_opening.cpp +++ b/src/session/test/gtest_state_tcp_init_to_opening.cpp @@ -1,6 +1,7 @@ // TCP state machine test: init -> opening #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -59,6 +60,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -136,6 +138,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -213,6 +216,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -301,6 +305,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK_ACK) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -402,6 +407,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -497,6 +503,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -591,6 +598,7 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -679,6 +687,7 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp b/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp index 1cd3803..98088d1 100644 --- a/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp +++ b/src/session/test/gtest_state_tcp_init_to_opening_to_active_to_closing_to_closed.cpp @@ -1,6 +1,7 @@ // TCP state machine test: init -> opening -> active -> closing -> closed #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -54,6 +55,7 @@ TEST(TCP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING_TO_CLOSED, TEST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_tcp_opening_to_active.cpp b/src/session/test/gtest_state_tcp_opening_to_active.cpp index c317888..6583eba 100644 --- a/src/session/test/gtest_state_tcp_opening_to_active.cpp +++ b/src/session/test/gtest_state_tcp_opening_to_active.cpp @@ -1,6 +1,7 @@ // TCP state machine test: opening -> active #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -58,6 +59,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYN_C2S_DATA) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -146,6 +148,7 @@ TEST(TCP_OPENING_TO_ACTIVE, BY_SYNACK_S2C_DATA) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_tcp_opening_to_closing.cpp b/src/session/test/gtest_state_tcp_opening_to_closing.cpp index 72d69c3..cd16221 100644 --- a/src/session/test/gtest_state_tcp_opening_to_closing.cpp +++ b/src/session/test/gtest_state_tcp_opening_to_closing.cpp @@ -1,6 +1,7 @@ // TCP state machine test: opening -> closing #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -59,6 +60,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_FIN_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -157,6 +159,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -251,6 +254,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -344,6 +348,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_INIT_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -401,6 +406,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -489,6 +495,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -588,6 +595,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_HALF_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -675,6 +683,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_HALF_FIN) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp b/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp index 8d6854d..5874ce6 100644 --- a/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp +++ b/src/session/test/gtest_state_udp_init_to_opening_to_active_to_closing.cpp @@ -1,6 +1,7 @@ // UDP state machine test: init -> opening -> active -> closing #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -54,6 +55,7 @@ TEST(UDP_INIT_TO_OPENING_TO_ACTIVE_TO_CLOSING, TEST) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp b/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp index 5339f14..9786fa3 100644 --- a/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp +++ b/src/session/test/gtest_state_udp_init_to_opening_to_closing.cpp @@ -1,6 +1,7 @@ // UDP state machine test: init -> opening -> closing #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -59,6 +60,7 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_C2S) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -137,6 +139,7 @@ TEST(UDP_INIT_TO_OPENING_TO_CLOSING, BY_S2C) struct session_manager *mgr = NULL; struct session_manager_stat *stat = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_tcp_data.cpp b/src/session/test/gtest_timeout_tcp_data.cpp index 322b5e4..83ce092 100644 --- a/src/session/test/gtest_timeout_tcp_data.cpp +++ b/src/session/test/gtest_timeout_tcp_data.cpp @@ -1,5 +1,6 @@ #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -51,6 +52,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA) struct session *sess = NULL; struct session_manager *mgr = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_tcp_handshake.cpp b/src/session/test/gtest_timeout_tcp_handshake.cpp index c704773..31c80a5 100644 --- a/src/session/test/gtest_timeout_tcp_handshake.cpp +++ b/src/session/test/gtest_timeout_tcp_handshake.cpp @@ -1,5 +1,6 @@ #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -52,6 +53,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_HANDSHAKE) struct session *sess = NULL; struct session_manager *mgr = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_tcp_init.cpp b/src/session/test/gtest_timeout_tcp_init.cpp index 1d685fb..b6c987b 100644 --- a/src/session/test/gtest_timeout_tcp_init.cpp +++ b/src/session/test/gtest_timeout_tcp_init.cpp @@ -1,5 +1,6 @@ #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -52,6 +53,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_INIT) struct session *sess = NULL; struct session_manager *mgr = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/session/test/gtest_timeout_udp_data.cpp b/src/session/test/gtest_timeout_udp_data.cpp index 81c4115..5f4571e 100644 --- a/src/session/test/gtest_timeout_udp_data.cpp +++ b/src/session/test/gtest_timeout_udp_data.cpp @@ -1,5 +1,6 @@ #include +#include "times.h" #include "session_priv.h" #include "session_manager.h" @@ -51,6 +52,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA1) struct session *sess = NULL; struct session_manager *mgr = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); @@ -86,6 +88,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2) struct session *sess = NULL; struct session_manager *mgr = NULL; + stellar_update_time_cache(); mgr = session_manager_new(&opts, 1); EXPECT_TRUE(mgr != NULL); diff --git a/src/stellar/CMakeLists.txt b/src/stellar/CMakeLists.txt index a8ddb9e..2eecfb3 100644 --- a/src/stellar/CMakeLists.txt +++ b/src/stellar/CMakeLists.txt @@ -1,5 +1,5 @@ add_library(core config.cpp stat.cpp stellar.cpp inject.cpp) -target_link_libraries(core timestamp plugin_manager session_manager ip_reassembly packet_io pthread fieldstat4 toml) +target_link_libraries(core times plugin_manager session_manager ip_reassembly packet_io pthread fieldstat4 toml) add_executable(stellar main.cpp) target_link_libraries(stellar core plugin_manager) diff --git a/src/stellar/main.cpp b/src/stellar/main.cpp index f16edc3..543d39f 100644 --- a/src/stellar/main.cpp +++ b/src/stellar/main.cpp @@ -7,8 +7,8 @@ #include #include "logo.h" +#include "times.h" #include "config.h" -#include "timestamp.h" #include "id_generator.h" #include "stellar_priv.h" @@ -65,7 +65,7 @@ static int all_session_have_freed(void) int main(int argc, char **argv) { - timestamp_update(); + stellar_update_time_cache(); signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); @@ -126,13 +126,13 @@ int main(int argc, char **argv) goto error_out; } - runtime->stat_last_output_ts = timestamp_get_msec(); + runtime->stat_last_output_ts = stellar_get_monotonic_time_msec(); while (!ATOMIC_READ(&runtime->need_exit)) { - timestamp_update(); - if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000) + stellar_update_time_cache(); + if (stellar_get_monotonic_time_msec() - runtime->stat_last_output_ts > 2000) { - runtime->stat_last_output_ts = timestamp_get_msec(); + runtime->stat_last_output_ts = stellar_get_monotonic_time_msec(); stellar_stat_output(runtime->stat); } usleep(1000); // 1ms diff --git a/src/stellar/stellar.cpp b/src/stellar/stellar.cpp index 219fa22..d1bf30d 100644 --- a/src/stellar/stellar.cpp +++ b/src/stellar/stellar.cpp @@ -4,8 +4,8 @@ #include #include +#include "times.h" #include "config.h" -#include "timestamp.h" #include "stellar_priv.h" struct stellar_runtime __runtime = {0}; @@ -122,7 +122,7 @@ static void *work_thread(void *arg) while (ATOMIC_READ(&runtime->need_exit) == 0) { - now = timestamp_get_msec(); + now = stellar_get_monotonic_time_msec(); memset(packets, 0, sizeof(packets)); nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX); if (nr_recv == 0) @@ -247,7 +247,7 @@ void stellar_set_current_thread_index(uint16_t idx) int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config) { - uint64_t now = timestamp_get_msec(); + uint64_t now = stellar_get_monotonic_time_msec(); for (uint16_t i = 0; i < config->io_opts.nr_threads; i++) { struct stellar_thread *thread = &runtime->threads[i]; diff --git a/src/times/CMakeLists.txt b/src/times/CMakeLists.txt new file mode 100644 index 0000000..864d16f --- /dev/null +++ b/src/times/CMakeLists.txt @@ -0,0 +1,6 @@ +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 new file mode 100644 index 0000000..da85e5c --- /dev/null +++ b/src/times/test/CMakeLists.txt @@ -0,0 +1,5 @@ +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/timestamp/test/gtest_timestamp.cpp b/src/times/test/gtest_times.cpp similarity index 67% rename from src/timestamp/test/gtest_timestamp.cpp rename to src/times/test/gtest_times.cpp index 758444d..dc413a9 100644 --- a/src/timestamp/test/gtest_timestamp.cpp +++ b/src/times/test/gtest_times.cpp @@ -1,22 +1,22 @@ #include -#include "timestamp.h" +#include "times.h" -TEST(TIMESTAMP, GET) +TEST(TIMES, TEST) { uint64_t last_sec = 0; uint64_t last_msec = 0; uint64_t curr_sec = 0; uint64_t curr_msec = 0; - timestamp_update(); - last_sec = timestamp_get_sec(); - last_msec = timestamp_get_msec(); + stellar_update_time_cache(); + last_sec = stellar_get_monotonic_time_sec(); + last_msec = stellar_get_monotonic_time_msec(); usleep(1000); // 1ms - timestamp_update(); - curr_sec = timestamp_get_sec(); - curr_msec = timestamp_get_msec(); + stellar_update_time_cache(); + 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); @@ -24,11 +24,11 @@ TEST(TIMESTAMP, GET) EXPECT_TRUE(curr_msec - last_msec >= 1); usleep(1000 * 1000); // 1s - timestamp_update(); + stellar_update_time_cache(); last_sec = curr_sec; last_msec = curr_msec; - curr_sec = timestamp_get_sec(); - curr_msec = timestamp_get_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); diff --git a/src/times/times.cpp b/src/times/times.cpp new file mode 100644 index 0000000..1a1854c --- /dev/null +++ b/src/times/times.cpp @@ -0,0 +1,85 @@ +#include + +#include "macro.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 + * }; + */ + +struct +{ + struct timespec real_time_spec; + uint64_t real_time_msec; + uint64_t real_time_sec; + + struct timespec monotonic_time_spec; + uint64_t monotonic_time_msec; + uint64_t monotonic_time_sec; +} global_time; + +void stellar_update_time_cache() +{ + uint64_t time_msec; + uint64_t time_sec; + /* + * 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. + */ + clock_gettime(CLOCK_MONOTONIC, &global_time.monotonic_time_spec); + + time_msec = global_time.monotonic_time_spec.tv_sec * 1000 + + global_time.monotonic_time_spec.tv_nsec / 1000000; + time_sec = global_time.monotonic_time_spec.tv_sec + + global_time.monotonic_time_spec.tv_nsec / 1000000000; + + ATOMIC_SET(&global_time.monotonic_time_msec, time_msec); + ATOMIC_SET(&global_time.monotonic_time_sec, time_sec); + + clock_gettime(CLOCK_REALTIME, &global_time.real_time_spec); + + time_msec = global_time.real_time_spec.tv_sec * 1000 + + global_time.real_time_spec.tv_nsec / 1000000; + time_sec = global_time.real_time_spec.tv_sec + + global_time.real_time_spec.tv_nsec / 1000000000; + + ATOMIC_SET(&global_time.real_time_msec, time_msec); + ATOMIC_SET(&global_time.real_time_sec, time_sec); +} + +uint64_t stellar_get_monotonic_time_sec() +{ + return ATOMIC_READ(&global_time.monotonic_time_sec); +} + +uint64_t stellar_get_monotonic_time_msec() +{ + return ATOMIC_READ(&global_time.monotonic_time_msec); +} + +uint64_t stellar_get_real_time_sec() +{ + return ATOMIC_READ(&global_time.real_time_sec); +} + +uint64_t stellar_get_real_time_msec() +{ + return ATOMIC_READ(&global_time.real_time_msec); +} diff --git a/src/times/times.h b/src/times/times.h new file mode 100644 index 0000000..73549fe --- /dev/null +++ b/src/times/times.h @@ -0,0 +1,23 @@ +#ifndef _TIMES_H +#define _TIMES_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +void stellar_update_time_cache(); + +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 + +#endif diff --git a/src/timestamp/CMakeLists.txt b/src/timestamp/CMakeLists.txt deleted file mode 100644 index 691002f..0000000 --- a/src/timestamp/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_library(timestamp timestamp.cpp) -target_include_directories(timestamp PUBLIC ${CMAKE_CURRENT_LIST_DIR}) -target_include_directories(timestamp PUBLIC ${CMAKE_SOURCE_DIR}/src/stellar) -target_link_libraries(timestamp) - -add_subdirectory(test) \ No newline at end of file diff --git a/src/timestamp/test/CMakeLists.txt b/src/timestamp/test/CMakeLists.txt deleted file mode 100644 index 09d145e..0000000 --- a/src/timestamp/test/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_executable(gtest_timestamp gtest_timestamp.cpp) -target_link_libraries(gtest_timestamp timestamp gtest) - -include(GoogleTest) -gtest_discover_tests(gtest_timestamp) \ No newline at end of file diff --git a/src/timestamp/timestamp.cpp b/src/timestamp/timestamp.cpp deleted file mode 100644 index 3e90e48..0000000 --- a/src/timestamp/timestamp.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include - -#include "macro.h" -#include "timestamp.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 - * }; - */ - -struct timestamp -{ - struct timespec ts; - uint64_t ts_in_msec; - uint64_t ts_in_sec; -} g_timestamp; - -void timestamp_update() -{ - /* - * 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. - */ - clock_gettime(CLOCK_MONOTONIC, &g_timestamp.ts); - - uint64_t current_timestamp_ms = g_timestamp.ts.tv_sec * 1000 + g_timestamp.ts.tv_nsec / 1000000; - uint64_t current_timestamp_sec = g_timestamp.ts.tv_sec + g_timestamp.ts.tv_nsec / 1000000000; - - ATOMIC_SET(&g_timestamp.ts_in_msec, current_timestamp_ms); - ATOMIC_SET(&g_timestamp.ts_in_sec, current_timestamp_sec); -} - -uint64_t timestamp_get_sec() -{ - return ATOMIC_READ(&g_timestamp.ts_in_sec); -} - -uint64_t timestamp_get_msec() -{ - return ATOMIC_READ(&g_timestamp.ts_in_msec); -} \ No newline at end of file diff --git a/src/timestamp/timestamp.h b/src/timestamp/timestamp.h deleted file mode 100644 index f30b2fd..0000000 --- a/src/timestamp/timestamp.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _TIMESTAMP_H -#define _TIMESTAMP_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include - -void timestamp_update(); -uint64_t timestamp_get_sec(); -uint64_t timestamp_get_msec(); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/test/packet_injector.cpp b/test/packet_injector.cpp index d174fa0..c52187b 100644 --- a/test/packet_injector.cpp +++ b/test/packet_injector.cpp @@ -8,8 +8,8 @@ #include #include "logo.h" +#include "times.h" #include "config.h" -#include "timestamp.h" #include "id_generator.h" #include "stellar_priv.h" #include "session_priv.h" @@ -377,7 +377,7 @@ int main(int argc, char **argv) return -1; } - timestamp_update(); + stellar_update_time_cache(); signal(SIGINT, signal_handler); signal(SIGQUIT, signal_handler); @@ -437,13 +437,13 @@ int main(int argc, char **argv) goto error_out; } - runtime->stat_last_output_ts = timestamp_get_msec(); + runtime->stat_last_output_ts = stellar_get_monotonic_time_msec(); while (!ATOMIC_READ(&runtime->need_exit)) { - timestamp_update(); - if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000) + stellar_update_time_cache(); + if (stellar_get_monotonic_time_msec() - runtime->stat_last_output_ts > 2000) { - runtime->stat_last_output_ts = timestamp_get_msec(); + runtime->stat_last_output_ts = stellar_get_monotonic_time_msec(); stellar_stat_output(runtime->stat); } usleep(1000); // 1ms