optimizate: pass the current timeout to the ID generator as a parameter, instead of getting the time from the ID generator itself
This commit is contained in:
@@ -111,13 +111,13 @@ static inline void free_evicted_sessions(struct session_manager *sess_mgr, uint6
|
||||
}
|
||||
}
|
||||
|
||||
static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now)
|
||||
static inline void free_expired_sessions(struct session_manager *sess_mgr, uint64_t max_free, uint64_t now_ms)
|
||||
{
|
||||
void *plugin_ctx = NULL;
|
||||
struct session *sess = NULL;
|
||||
for (uint64_t i = 0; i < max_free; i++)
|
||||
{
|
||||
sess = session_manager_get_expired_session(sess_mgr, now);
|
||||
sess = session_manager_get_expired_session(sess_mgr, now_ms);
|
||||
if (sess)
|
||||
{
|
||||
plugin_ctx = session_get_user_data(sess);
|
||||
@@ -131,7 +131,7 @@ static inline void free_expired_sessions(struct session_manager *sess_mgr, uint6
|
||||
}
|
||||
}
|
||||
|
||||
static inline void merge_thread_stat(struct stellar_thread *thread, uint64_t now)
|
||||
static inline void merge_thread_stat(struct stellar_thread *thread)
|
||||
{
|
||||
struct stellar_runtime *runtime = thread->runtime;
|
||||
struct thread_stat thr_stat = {
|
||||
@@ -145,7 +145,7 @@ static inline void merge_thread_stat(struct stellar_thread *thread, uint64_t now
|
||||
static void *work_thread(void *arg)
|
||||
{
|
||||
int nr_recv;
|
||||
uint64_t now = 0;
|
||||
uint64_t now_ms = 0;
|
||||
char thd_name[16] = {0};
|
||||
void *plugin_ctx = NULL;
|
||||
struct packet *pkt = NULL;
|
||||
@@ -176,7 +176,15 @@ static void *work_thread(void *arg)
|
||||
|
||||
while (ATOMIC_READ(&need_exit) == 0)
|
||||
{
|
||||
now = stellar_get_monotonic_time_msec();
|
||||
/*
|
||||
* We use the system's real time instead of monotonic time for the following reasons:
|
||||
* -> Session creation/closure times require real time (e.g., for logging session activities).
|
||||
* -> Session ID generation relies on real time (e.g., for reverse calculating session creation time from the session ID).
|
||||
*
|
||||
* 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();
|
||||
memset(packets, 0, sizeof(packets));
|
||||
nr_recv = packet_io_ingress(packet_io, thr_idx, packets, RX_BURST_MAX);
|
||||
if (nr_recv == 0)
|
||||
@@ -193,7 +201,7 @@ static void *work_thread(void *arg)
|
||||
plugin_manager_on_packet(plug_mgr, pkt);
|
||||
if (packet_is_fragment(pkt))
|
||||
{
|
||||
defraged_pkt = ip_reassembly_packet(ip_reass, pkt, now);
|
||||
defraged_pkt = ip_reassembly_packet(ip_reass, pkt, now_ms);
|
||||
if (defraged_pkt == NULL)
|
||||
{
|
||||
goto fast_path;
|
||||
@@ -205,10 +213,10 @@ static void *work_thread(void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
sess = session_manager_lookup_session(sess_mgr, pkt, now);
|
||||
sess = session_manager_lookup_session(sess_mgr, pkt, now_ms);
|
||||
if (sess == NULL)
|
||||
{
|
||||
sess = session_manager_new_session(sess_mgr, pkt, now);
|
||||
sess = session_manager_new_session(sess_mgr, pkt, now_ms);
|
||||
if (sess == NULL)
|
||||
{
|
||||
goto fast_path;
|
||||
@@ -218,7 +226,7 @@ static void *work_thread(void *arg)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (session_manager_update_session(sess_mgr, sess, pkt, now) == -1)
|
||||
if (session_manager_update_session(sess_mgr, sess, pkt, now_ms) == -1)
|
||||
{
|
||||
goto fast_path;
|
||||
}
|
||||
@@ -269,14 +277,14 @@ static void *work_thread(void *arg)
|
||||
free_evicted_sessions(sess_mgr, nr_recv);
|
||||
|
||||
// per 5 ms, atmost free 8 expired session
|
||||
if (now - thread->timing_wheel_last_update_ts > 5)
|
||||
if (now_ms - thread->timing_wheel_last_update_ts > 5)
|
||||
{
|
||||
free_expired_sessions(sess_mgr, 8, now);
|
||||
thread->timing_wheel_last_update_ts = now;
|
||||
free_expired_sessions(sess_mgr, 8, now_ms);
|
||||
thread->timing_wheel_last_update_ts = now_ms;
|
||||
}
|
||||
|
||||
merge_thread_stat(thread, now);
|
||||
ip_reassembly_expire(ip_reass, now);
|
||||
merge_thread_stat(thread);
|
||||
ip_reassembly_expire(ip_reass, now_ms);
|
||||
plugin_manager_on_polling(runtime->plug_mgr);
|
||||
|
||||
if (nr_recv == 0)
|
||||
@@ -345,14 +353,14 @@ 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 = stellar_get_monotonic_time_msec();
|
||||
uint64_t now_ms = stellar_get_real_time_msec();
|
||||
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
|
||||
{
|
||||
struct stellar_thread *thread = &runtime->threads[i];
|
||||
thread->idx = i;
|
||||
thread->is_runing = 0;
|
||||
thread->timing_wheel_last_update_ts = now;
|
||||
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now);
|
||||
thread->timing_wheel_last_update_ts = now_ms;
|
||||
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms);
|
||||
if (thread->sess_mgr == NULL)
|
||||
{
|
||||
STELLAR_LOG_ERROR("unable to create session manager");
|
||||
@@ -418,8 +426,6 @@ int stellar_run(int argc, char **argv)
|
||||
struct stellar_runtime *runtime = &st.runtime;
|
||||
struct stellar_config *config = &st.config;
|
||||
|
||||
stellar_update_time_cache();
|
||||
|
||||
signal(SIGINT, signal_handler);
|
||||
signal(SIGQUIT, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
@@ -480,13 +486,12 @@ int stellar_run(int argc, char **argv)
|
||||
goto error_out;
|
||||
}
|
||||
|
||||
runtime->stat_last_output_ts = stellar_get_monotonic_time_msec();
|
||||
runtime->stat_last_output_ts = stellar_get_real_time_msec();
|
||||
while (!ATOMIC_READ(&need_exit))
|
||||
{
|
||||
stellar_update_time_cache();
|
||||
if (stellar_get_monotonic_time_msec() - runtime->stat_last_output_ts > 2000)
|
||||
if (stellar_get_real_time_msec() - runtime->stat_last_output_ts > 2000)
|
||||
{
|
||||
runtime->stat_last_output_ts = stellar_get_monotonic_time_msec();
|
||||
runtime->stat_last_output_ts = stellar_get_real_time_msec();
|
||||
stellar_stat_output(runtime->stat);
|
||||
}
|
||||
usleep(1000); // 1ms
|
||||
@@ -545,11 +550,10 @@ uint16_t stellar_get_current_thread_index()
|
||||
// only send user crafted packet, can't send packet which come from network
|
||||
void stellar_send_crafted_packet(struct stellar *st, struct packet *pkt)
|
||||
{
|
||||
uint64_t time_ms = stellar_get_monotonic_time_msec();
|
||||
uint16_t thr_idx = stellar_get_current_thread_index();
|
||||
struct packet_io *packet_io = stellar_get_packet_io(st);
|
||||
struct session_manager *sess_mgr = stellar_get_session_manager(st);
|
||||
session_manager_record_duplicated_packet(sess_mgr, pkt, time_ms);
|
||||
session_manager_record_duplicated_packet(sess_mgr, pkt);
|
||||
|
||||
if (packet_get_origin_ctx(pkt))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user