enhance: add configuration items to adjust the scheduling parameters of the main loop

This commit is contained in:
luwenpeng
2024-08-15 19:03:48 +08:00
parent 42f44b53b1
commit b0e08133b7
8 changed files with 239 additions and 26 deletions

View File

@@ -26,12 +26,28 @@
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
struct schedule_data
{
uint64_t last_free_expired_session_timestamp;
uint64_t last_free_expired_ip_frag_timestamp;
uint64_t last_merge_thread_stat_timestamp;
uint64_t free_expired_session_interval;
uint64_t free_expired_session_batch;
uint64_t free_expired_ip_frag_interval;
uint64_t free_expired_ip_frag_batch;
uint64_t merge_stat_interval;
uint64_t packet_io_yield_interval;
};
struct stellar_thread
{
pthread_t tid;
uint16_t idx;
uint64_t is_runing;
uint64_t timing_wheel_last_update_ts;
struct schedule_data sched_data;
struct ip_reassembly *ip_mgr;
struct session_manager *sess_mgr;
struct stellar_runtime *runtime;
@@ -158,9 +174,9 @@ static void *work_thread(void *arg)
struct ip_reassembly *ip_reass = thread->ip_mgr;
struct session_manager *sess_mgr = thread->sess_mgr;
struct stellar_runtime *runtime = thread->runtime;
struct schedule_data *sched_data = &thread->sched_data;
struct packet_io *packet_io = runtime->packet_io;
struct plugin_manager_schema *plug_mgr = runtime->plug_mgr;
uint16_t thr_idx = thread->idx;
__thread_id = thr_idx;
@@ -293,26 +309,37 @@ static void *work_thread(void *arg)
idle_tasks:
// nr_recv packet atmost trigger nr_recv session evicted
free_evicted_sessions(sess_mgr, nr_recv);
plugin_manager_on_polling(runtime->plug_mgr);
// per 5 ms, atmost free 8 expired session
if (now_ms - thread->timing_wheel_last_update_ts > 5)
// per free_expired_session_interval MAX free_expired_session_batch sessions are released
if (now_ms - sched_data->last_free_expired_session_timestamp > sched_data->free_expired_session_interval)
{
free_expired_sessions(sess_mgr, 8, now_ms);
thread->timing_wheel_last_update_ts = now_ms;
free_expired_sessions(sess_mgr, sched_data->free_expired_session_batch, now_ms);
sched_data->last_free_expired_session_timestamp = now_ms;
}
merge_thread_stat(thread);
ip_reassembly_expire(ip_reass, now_ms);
plugin_manager_on_polling(runtime->plug_mgr);
// per merge_stat_interval merge thread stat
if (now_ms - sched_data->last_merge_thread_stat_timestamp > sched_data->merge_stat_interval)
{
merge_thread_stat(thread);
sched_data->last_merge_thread_stat_timestamp = now_ms;
}
// per free_expired_ip_frag_interval MAX free_expired_ip_frag_batch ip fragments are released
if (now_ms - sched_data->last_free_expired_ip_frag_timestamp > sched_data->free_expired_ip_frag_interval)
{
ip_reassembly_expire(ip_reass, sched_data->free_expired_ip_frag_batch, now_ms);
sched_data->last_free_expired_ip_frag_timestamp = now_ms;
}
if (nr_recv == 0)
{
packet_io_yield(packet_io, thr_idx, 900);
packet_io_yield(packet_io, thr_idx, sched_data->packet_io_yield_interval);
}
}
ATOMIC_SET(&thread->is_runing, 0);
STELLAR_LOG_STATE("worker thread %d exit !!!", thr_idx);
STELLAR_LOG_STATE("worker thread %d exit", thr_idx);
return NULL;
}
@@ -325,19 +352,19 @@ static void signal_handler(int signo)
{
if (signo == SIGINT)
{
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
STELLAR_LOG_STATE("SIGINT received, notify threads to exit");
ATOMIC_SET(&need_exit, 1);
}
if (signo == SIGQUIT)
{
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit");
ATOMIC_SET(&need_exit, 1);
}
if (signo == SIGTERM)
{
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit");
ATOMIC_SET(&need_exit, 1);
}
@@ -377,7 +404,20 @@ static int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_c
struct stellar_thread *thread = &runtime->threads[i];
thread->idx = i;
thread->is_runing = 0;
thread->timing_wheel_last_update_ts = now_ms;
thread->sched_data.last_free_expired_session_timestamp = now_ms;
thread->sched_data.last_free_expired_ip_frag_timestamp = now_ms;
thread->sched_data.last_merge_thread_stat_timestamp = now_ms;
thread->sched_data.free_expired_session_interval = config->sched_opts.free_expired_session_interval;
thread->sched_data.free_expired_session_batch = config->sched_opts.free_expired_session_batch;
thread->sched_data.free_expired_ip_frag_interval = config->sched_opts.free_expired_ip_frag_interval;
thread->sched_data.free_expired_ip_frag_batch = config->sched_opts.free_expired_ip_frag_batch;
thread->sched_data.merge_stat_interval = config->sched_opts.merge_stat_interval;
thread->sched_data.packet_io_yield_interval = config->sched_opts.packet_io_yield_interval;
thread->sess_mgr = session_manager_new(&config->sess_mgr_opts, now_ms);
if (thread->sess_mgr == NULL)
{
@@ -403,7 +443,6 @@ static void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar
struct stellar_thread *thread = &runtime->threads[i];
if (ATOMIC_READ(&thread->is_runing) == 0)
{
STELLAR_LOG_STATE("clean worker thread %d context", i);
session_manager_free(thread->sess_mgr);
ip_reassembly_free(thread->ip_mgr);
}
@@ -427,13 +466,13 @@ static int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_co
static void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config *config)
{
STELLAR_LOG_STATE("wait worker thread exit ...");
for (uint16_t i = 0; i < config->pkt_io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &runtime->threads[i];
while (ATOMIC_READ(&thread->is_runing) == 1)
{
STELLAR_LOG_STATE("wait worker thread %d stop", i);
sleep(1);
usleep(1000); // 1ms
}
}
}
@@ -507,7 +546,7 @@ int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unu
runtime->stat_last_output_ts = stellar_get_real_time_msec();
while (!ATOMIC_READ(&need_exit))
{
if (stellar_get_real_time_msec() - runtime->stat_last_output_ts > 2000)
if (stellar_get_real_time_msec() - runtime->stat_last_output_ts > config->sched_opts.output_stat_interval)
{
runtime->stat_last_output_ts = stellar_get_real_time_msec();
stellar_stat_output(runtime->stat);
@@ -518,7 +557,7 @@ int stellar_run(int argc __attribute__((unused)), char **argv __attribute__((unu
if (packet_io_wait_exit(runtime->packet_io) && all_session_have_freed(runtime, config))
{
stellar_stat_output(runtime->stat); // flush stat
STELLAR_LOG_STATE("all sessions have been released, notify threads to exit !!!");
STELLAR_LOG_STATE("all sessions have been released, notify threads to exit");
ATOMIC_SET(&need_exit, 1);
}
}
@@ -529,7 +568,7 @@ error_out:
packet_io_free(runtime->packet_io);
plugin_manager_exit(runtime->plug_mgr);
stellar_stat_free(runtime->stat);
STELLAR_LOG_STATE("stellar exit !!!\n");
STELLAR_LOG_STATE("stellar exit\n");
log_free();
return 0;