stellar support monotonic time and real time

This commit is contained in:
luwenpeng
2024-05-16 10:13:43 +08:00
parent 1ef82a0c6a
commit fdc65067b2
30 changed files with 201 additions and 124 deletions

View File

@@ -113,6 +113,7 @@ enum session_stat
MAX_STAT, MAX_STAT,
}; };
// realtime in seconds
enum session_timestamp enum session_timestamp
{ {
SESSION_TIMESTAMP_START, SESSION_TIMESTAMP_START,

View File

@@ -1,5 +1,5 @@
add_subdirectory(log) add_subdirectory(log)
add_subdirectory(timestamp) add_subdirectory(times)
add_subdirectory(tuple) add_subdirectory(tuple)
add_subdirectory(packet) add_subdirectory(packet)
add_subdirectory(packet_io) add_subdirectory(packet_io)

View File

@@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include "macro.h" #include "macro.h"
#include "times.h"
#include "stellar_priv.h" #include "stellar_priv.h"
#include "id_generator.h" #include "id_generator.h"
@@ -62,13 +63,11 @@ uint64_t id_generator_alloc()
#define MAX_ID_PER_THREAD (32768) #define MAX_ID_PER_THREAD (32768)
#define MAX_ID_BASE_TIME (268435456L) #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 thr_idx = stellar_get_current_thread_index();
uint64_t global_id = 0; uint64_t global_id = 0;
uint64_t id_per_thread = (global_id_generator.thread_volatile[thr_idx]++) % MAX_ID_PER_THREAD; 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) | global_id = (global_id_generator.device_id << 51) |
(thr_idx << 43) | (thr_idx << 43) |
(id_base_time << 15) | (id_base_time << 15) |

View File

@@ -3,6 +3,7 @@
#include <assert.h> #include <assert.h>
#include "macro.h" #include "macro.h"
#include "times.h"
#include "tcp_utils.h" #include "tcp_utils.h"
#include "udp_utils.h" #include "udp_utils.h"
#include "id_generator.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) 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; uint64_t real_sec = stellar_get_real_time_sec();
clock_gettime(CLOCK_REALTIME, &real); // must be realtime
if (session_get_state(sess) == SESSION_STATE_INIT) if (session_get_state(sess) == SESSION_STATE_INIT)
{ {
session_set_id(sess, id_generator_alloc()); 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)); 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) switch (key->ip_proto)
{ {
case IPPROTO_TCP: 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_current_packet(sess, pkt);
session_set_flow_direction(sess, dir); 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); session_set_state(sess, next_state);
} }

View File

@@ -46,7 +46,7 @@ struct session
{ {
uint64_t id; uint64_t id;
uint64_t stats[MAX_FLOW_DIRECTION][MAX_STAT]; 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 tcp_half tcp_halfs[MAX_FLOW_DIRECTION];
struct timeout timeout; struct timeout timeout;
struct list_head lru; struct list_head lru;

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.h" #include "session_manager.h"
@@ -302,6 +303,7 @@ TEST(CASE, TCP_FAST_OPEN)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// TCP state machine test: active -> closing // TCP state machine test: active -> closing
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -176,6 +178,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_C2S_RST)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -262,6 +265,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_S2C_RST)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -346,6 +350,7 @@ TEST(TCP_ACTIVE_TO_CLOSING, BY_DATA_TIMEOUT)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// TCP state machine test: init -> opening // TCP state machine test: init -> opening
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.h" #include "session_manager.h"
@@ -59,6 +60,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -136,6 +138,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -213,6 +216,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_SYNACK)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -402,6 +407,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -497,6 +503,7 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -591,6 +598,7 @@ TEST(TCP_INIT_TO_OPENING, BY_C2S_ASMMETRIC)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -679,6 +687,7 @@ TEST(TCP_INIT_TO_OPENING, BY_S2C_ASMMETRIC)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// TCP state machine test: init -> opening -> active -> closing -> closed // TCP state machine test: init -> opening -> active -> closing -> closed
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// TCP state machine test: opening -> active // TCP state machine test: opening -> active
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// TCP state machine test: opening -> closing // TCP state machine test: opening -> closing
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -157,6 +159,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_C2S_RST)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -251,6 +254,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_S2C_RST)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -344,6 +348,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_INIT_TIMEOUT)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -401,6 +406,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_HANDSHAKE_TIMEOUT)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -489,6 +495,7 @@ TEST(TCP_OPENING_TO_CLOSING, BY_DATA_TIMEOUT)
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// UDP state machine test: init -> opening -> active -> closing // UDP state machine test: init -> opening -> active -> closing
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,6 +1,7 @@
// UDP state machine test: init -> opening -> closing // UDP state machine test: init -> opening -> closing
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); 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 *mgr = NULL;
struct session_manager_stat *stat = NULL; struct session_manager_stat *stat = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.h" #include "session_manager.h"
@@ -51,6 +52,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_DATA)
struct session *sess = NULL; struct session *sess = NULL;
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.h" #include "session_manager.h"
@@ -52,6 +53,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_HANDSHAKE)
struct session *sess = NULL; struct session *sess = NULL;
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.h" #include "session_manager.h"
@@ -52,6 +53,7 @@ TEST(TIMEOUT, TCP_TIMEOUT_INIT)
struct session *sess = NULL; struct session *sess = NULL;
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,5 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "times.h"
#include "session_priv.h" #include "session_priv.h"
#include "session_manager.h" #include "session_manager.h"
@@ -51,6 +52,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA1)
struct session *sess = NULL; struct session *sess = NULL;
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);
@@ -86,6 +88,7 @@ TEST(TIMEOUT, UDP_TIMEOUT_DATA2)
struct session *sess = NULL; struct session *sess = NULL;
struct session_manager *mgr = NULL; struct session_manager *mgr = NULL;
stellar_update_time_cache();
mgr = session_manager_new(&opts, 1); mgr = session_manager_new(&opts, 1);
EXPECT_TRUE(mgr != NULL); EXPECT_TRUE(mgr != NULL);

View File

@@ -1,5 +1,5 @@
add_library(core config.cpp stat.cpp stellar.cpp inject.cpp) 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) add_executable(stellar main.cpp)
target_link_libraries(stellar core plugin_manager) target_link_libraries(stellar core plugin_manager)

View File

@@ -7,8 +7,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "logo.h" #include "logo.h"
#include "times.h"
#include "config.h" #include "config.h"
#include "timestamp.h"
#include "id_generator.h" #include "id_generator.h"
#include "stellar_priv.h" #include "stellar_priv.h"
@@ -65,7 +65,7 @@ static int all_session_have_freed(void)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
timestamp_update(); stellar_update_time_cache();
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler); signal(SIGQUIT, signal_handler);
@@ -126,13 +126,13 @@ int main(int argc, char **argv)
goto error_out; 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)) while (!ATOMIC_READ(&runtime->need_exit))
{ {
timestamp_update(); stellar_update_time_cache();
if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000) 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); stellar_stat_output(runtime->stat);
} }
usleep(1000); // 1ms usleep(1000); // 1ms

View File

@@ -4,8 +4,8 @@
#include <pthread.h> #include <pthread.h>
#include <sys/prctl.h> #include <sys/prctl.h>
#include "times.h"
#include "config.h" #include "config.h"
#include "timestamp.h"
#include "stellar_priv.h" #include "stellar_priv.h"
struct stellar_runtime __runtime = {0}; struct stellar_runtime __runtime = {0};
@@ -122,7 +122,7 @@ static void *work_thread(void *arg)
while (ATOMIC_READ(&runtime->need_exit) == 0) while (ATOMIC_READ(&runtime->need_exit) == 0)
{ {
now = timestamp_get_msec(); now = stellar_get_monotonic_time_msec();
memset(packets, 0, sizeof(packets)); memset(packets, 0, sizeof(packets));
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)
@@ -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) 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++) for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
{ {
struct stellar_thread *thread = &runtime->threads[i]; struct stellar_thread *thread = &runtime->threads[i];

6
src/times/CMakeLists.txt Normal file
View File

@@ -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)

View File

@@ -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)

View File

@@ -1,22 +1,22 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "timestamp.h" #include "times.h"
TEST(TIMESTAMP, GET) TEST(TIMES, TEST)
{ {
uint64_t last_sec = 0; uint64_t last_sec = 0;
uint64_t last_msec = 0; uint64_t last_msec = 0;
uint64_t curr_sec = 0; uint64_t curr_sec = 0;
uint64_t curr_msec = 0; uint64_t curr_msec = 0;
timestamp_update(); stellar_update_time_cache();
last_sec = timestamp_get_sec(); last_sec = stellar_get_monotonic_time_sec();
last_msec = timestamp_get_msec(); last_msec = stellar_get_monotonic_time_msec();
usleep(1000); // 1ms usleep(1000); // 1ms
timestamp_update(); stellar_update_time_cache();
curr_sec = timestamp_get_sec(); curr_sec = stellar_get_monotonic_time_sec();
curr_msec = timestamp_get_msec(); curr_msec = stellar_get_monotonic_time_msec();
printf("After usleep(1000)\n"); printf("After usleep(1000)\n");
printf("last_sec: %lu, last_msec: %lu\n", last_sec, last_msec); printf("last_sec: %lu, last_msec: %lu\n", last_sec, last_msec);
printf("curr_sec: %lu, curr_msec: %lu\n", curr_sec, curr_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); EXPECT_TRUE(curr_msec - last_msec >= 1);
usleep(1000 * 1000); // 1s usleep(1000 * 1000); // 1s
timestamp_update(); stellar_update_time_cache();
last_sec = curr_sec; last_sec = curr_sec;
last_msec = curr_msec; last_msec = curr_msec;
curr_sec = timestamp_get_sec(); curr_sec = stellar_get_monotonic_time_sec();
curr_msec = timestamp_get_msec(); curr_msec = stellar_get_monotonic_time_msec();
printf("After usleep(1000 * 1000)\n"); printf("After usleep(1000 * 1000)\n");
printf("last_sec: %lu, last_msec: %lu\n", last_sec, last_msec); printf("last_sec: %lu, last_msec: %lu\n", last_sec, last_msec);
printf("curr_sec: %lu, curr_msec: %lu\n", curr_sec, curr_msec); printf("curr_sec: %lu, curr_msec: %lu\n", curr_sec, curr_msec);

85
src/times/times.cpp Normal file
View File

@@ -0,0 +1,85 @@
#include <time.h>
#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);
}

23
src/times/times.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef _TIMES_H
#define _TIMES_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
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

View File

@@ -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)

View File

@@ -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)

View File

@@ -1,57 +0,0 @@
#include <time.h>
#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);
}

View File

@@ -1,19 +0,0 @@
#ifndef _TIMESTAMP_H
#define _TIMESTAMP_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
void timestamp_update();
uint64_t timestamp_get_sec();
uint64_t timestamp_get_msec();
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -8,8 +8,8 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include "logo.h" #include "logo.h"
#include "times.h"
#include "config.h" #include "config.h"
#include "timestamp.h"
#include "id_generator.h" #include "id_generator.h"
#include "stellar_priv.h" #include "stellar_priv.h"
#include "session_priv.h" #include "session_priv.h"
@@ -377,7 +377,7 @@ int main(int argc, char **argv)
return -1; return -1;
} }
timestamp_update(); stellar_update_time_cache();
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler); signal(SIGQUIT, signal_handler);
@@ -437,13 +437,13 @@ int main(int argc, char **argv)
goto error_out; 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)) while (!ATOMIC_READ(&runtime->need_exit))
{ {
timestamp_update(); stellar_update_time_cache();
if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000) 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); stellar_stat_output(runtime->stat);
} }
usleep(1000); // 1ms usleep(1000); // 1ms