Adjust thread index type to uint16 for future expansion & Organize stellar directory files

This commit is contained in:
luwenpeng
2024-04-25 16:48:50 +08:00
parent 17ca476c24
commit 611fda598f
22 changed files with 232 additions and 222 deletions

View File

@@ -1,84 +1,18 @@
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/prctl.h>
#include "logo.h"
#include "stat.h"
#include "config.h"
#include "packet_priv.h"
#include "packet_io.h"
#include "timestamp.h"
#include "thread_idx.h"
#include "id_generator.h"
#include "ip_reassembly.h"
#include "session_manager.h"
#include "plugin_manager.h"
#include "stellar_priv.h"
#define STELLAR_LOG_STATE(format, ...) LOG_STATE("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
struct stellar_thread
{
pthread_t tid;
uint16_t idx;
uint64_t is_runing;
uint64_t timing_wheel_last_update_ts;
struct ip_reassembly *ip_mgr;
struct session_manager *sess_mgr;
};
struct stellar_runtime
{
uint64_t need_exit;
uint64_t stat_last_output_ts;
struct stellar_stat *stat;
struct packet_io *packet_io;
struct plugin_manager *plug_mgr;
struct stellar_thread threads[MAX_THREAD_NUM];
};
struct stellar_runtime __runtime = {0};
struct stellar_runtime *runtime = &__runtime;
struct stellar_config __config = {0};
struct stellar_config *config = &__config;
static const char *log_config_file = "./conf/log.toml";
static const char *stellar_config_file = "./conf/stellar.toml";
static void signal_handler(int signo)
{
if (signo == SIGINT)
{
STELLAR_LOG_STATE("SIGINT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGQUIT)
{
STELLAR_LOG_STATE("SIGQUIT received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGTERM)
{
STELLAR_LOG_STATE("SIGTERM received, notify threads to exit !!!");
ATOMIC_SET(&runtime->need_exit, 1);
}
if (signo == SIGHUP)
{
STELLAR_LOG_STATE("SIGHUP received, reload log level !!!");
log_reload_level(log_config_file);
}
}
static void update_session_stat(struct session *sess, struct packet *pkt)
{
if (sess)
@@ -275,12 +209,24 @@ static void *work_thread(void *arg)
return NULL;
}
static int stellar_thread_init(struct stellar_runtime *ctx, uint8_t nr_threads)
static thread_local uint16_t __thread_id = 0;
uint16_t stellar_get_current_thread_index()
{
return __thread_id;
}
void stellar_set_current_thread_index(uint16_t idx)
{
__thread_id = idx;
}
int stellar_thread_init(struct stellar_runtime *runtime, struct stellar_config *config)
{
uint64_t now = timestamp_get_msec();
for (uint8_t i = 0; i < nr_threads; i++)
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &ctx->threads[i];
struct stellar_thread *thread = &runtime->threads[i];
thread->idx = i;
thread->is_runing = 0;
thread->timing_wheel_last_update_ts = now;
@@ -301,11 +247,11 @@ static int stellar_thread_init(struct stellar_runtime *ctx, uint8_t nr_threads)
return 0;
}
static void stellar_thread_clean(struct stellar_runtime *ctx, uint8_t nr_threads)
void stellar_thread_clean(struct stellar_runtime *runtime, struct stellar_config *config)
{
for (uint8_t i = 0; i < nr_threads; i++)
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &ctx->threads[i];
struct stellar_thread *thread = &runtime->threads[i];
if (ATOMIC_READ(&thread->is_runing) == 0)
{
STELLAR_LOG_STATE("wait worker thread %d free context", i);
@@ -315,11 +261,11 @@ static void stellar_thread_clean(struct stellar_runtime *ctx, uint8_t nr_threads
}
}
static int stellar_thread_run(struct stellar_runtime *ctx, uint8_t nr_threads)
int stellar_thread_run(struct stellar_runtime *runtime, struct stellar_config *config)
{
for (uint8_t i = 0; i < nr_threads; i++)
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &ctx->threads[i];
struct stellar_thread *thread = &runtime->threads[i];
if (pthread_create(&thread->tid, NULL, work_thread, (void *)thread) < 0)
{
STELLAR_LOG_ERROR("unable to create worker thread, error %d: %s", errno, strerror(errno));
@@ -330,11 +276,11 @@ static int stellar_thread_run(struct stellar_runtime *ctx, uint8_t nr_threads)
return 0;
}
static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
void stellar_thread_join(struct stellar_runtime *runtime, struct stellar_config *config)
{
for (uint8_t i = 0; i < nr_threads; i++)
for (uint16_t i = 0; i < config->io_opts.nr_threads; i++)
{
struct stellar_thread *thread = &ctx->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);
@@ -342,92 +288,3 @@ static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
}
}
}
int main(int argc, char **argv)
{
uint8_t nr_threads = 0;
timestamp_update();
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGHUP, signal_handler);
if (log_init(log_config_file) != 0)
{
STELLAR_LOG_ERROR("unable to init log");
goto error_out;
}
STELLAR_LOG_STATE("start stellar (version: %s)\n %s", __stellar_version, logo_str);
if (stellar_load_config(stellar_config_file, config) != 0)
{
STELLAR_LOG_ERROR("unable to load config file");
goto error_out;
}
stellar_print_config(config);
STELLAR_LOG_DEBUG("sizeof(struct session) = %lu bytes", sizeof(struct session));
nr_threads = config->io_opts.nr_threads;
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
{
STELLAR_LOG_ERROR("unable to init id generator");
goto error_out;
}
runtime->stat = stellar_stat_new(nr_threads);
if (runtime->stat == NULL)
{
STELLAR_LOG_ERROR("unable to create stellar stat");
goto error_out;
}
runtime->plug_mgr = plugin_manager_new();
if (runtime->plug_mgr == NULL)
{
STELLAR_LOG_ERROR("unable to create plugin manager");
goto error_out;
}
runtime->packet_io = packet_io_new(&config->io_opts);
if (runtime->packet_io == NULL)
{
STELLAR_LOG_ERROR("unable to create packet io");
goto error_out;
}
if (stellar_thread_init(runtime, nr_threads) != 0)
{
STELLAR_LOG_ERROR("unable to init thread context");
goto error_out;
}
if (stellar_thread_run(runtime, nr_threads) != 0)
{
STELLAR_LOG_ERROR("unable to create worker thread");
goto error_out;
}
runtime->stat_last_output_ts = timestamp_get_msec();
while (!ATOMIC_READ(&runtime->need_exit))
{
timestamp_update();
if (timestamp_get_msec() - runtime->stat_last_output_ts > 2000)
{
runtime->stat_last_output_ts = timestamp_get_msec();
stellar_stat_output(runtime->stat);
}
usleep(1000); // 1ms
}
error_out:
stellar_thread_join(runtime, nr_threads);
stellar_thread_clean(runtime, nr_threads);
packet_io_free(runtime->packet_io);
plugin_manager_free(runtime->plug_mgr);
stellar_stat_free(runtime->stat);
STELLAR_LOG_STATE("stellar exit !!!\n");
log_free();
return 0;
}