Stellar output statistics

This commit is contained in:
luwenpeng
2024-04-16 14:12:41 +08:00
parent 17f5d338de
commit d878849c3a
18 changed files with 485 additions and 30 deletions

View File

@@ -9,6 +9,7 @@
#include <sys/prctl.h>
#include "logo.h"
#include "stat.h"
#include "stellar.h"
#include "config.h"
#include "packet_private.h"
@@ -23,9 +24,6 @@
#define STELLAR_LOG_ERROR(format, ...) LOG_ERROR("stellar", format, ##__VA_ARGS__)
#define STELLAR_LOG_DEBUG(format, ...) LOG_DEBUG("stellar", format, ##__VA_ARGS__)
#define ATOMIC_SET(x, y) __atomic_store_n(x, y, __ATOMIC_RELAXED)
#define ATOMIC_READ(x) __atomic_load_n(x, __ATOMIC_RELAXED)
struct thread_ctx
{
pthread_t tid;
@@ -38,6 +36,7 @@ struct thread_ctx
struct stellar_runtime
{
uint64_t need_exit;
struct stellar_stat *stat;
struct packet_io *packet_io;
struct plugin_manager *plug_mgr;
struct thread_ctx threads[MAX_THREAD_NUM];
@@ -129,23 +128,37 @@ static inline void thread_set_name(const char *thd_symbol, uint16_t thd_idx)
prctl(PR_SET_NAME, (unsigned long long)thd_name, NULL, NULL, NULL);
}
static inline void stellar_thread_cron(struct thread_ctx *thr_ctx)
{
thread_local uint64_t last = 0;
if (timestamp_get_msec() - last > 2000)
{
struct thread_stat thr_stat = {
ip_reassembly_get_stat(thr_ctx->ip_mgr),
session_manager_get_stat(thr_ctx->sess_mgr),
};
stellar_peek_thr_stat(runtime->stat, &thr_stat, thr_ctx->idx);
last = timestamp_get_msec();
}
}
static void *work_thread(void *arg)
{
int nr_recv;
uint64_t now = 0;
uint16_t thr_idx = 0;
void *plugin_ctx;
struct packet *pkt;
struct packet packets[RX_BURST_MAX];
struct session *sess;
struct session *evicted_sess;
struct session *expired_sess;
struct packet_io *packet_io = runtime->packet_io;
struct plugin_manager *plug_mgr = runtime->plug_mgr;
struct thread_ctx *thr_ctx = (struct thread_ctx *)arg;
struct ip_reassembly *ip_reass = thr_ctx->ip_mgr;
struct session_manager *sess_mgr = thr_ctx->sess_mgr;
struct packet_io *packet_io = runtime->packet_io;
struct plugin_manager *plug_mgr = runtime->plug_mgr;
int nr_recv;
uint64_t now = 0;
uint16_t thr_idx = thr_ctx->idx;
thr_idx = thr_ctx->idx;
if (packet_io_init(packet_io, thr_idx) != 0)
{
@@ -231,6 +244,8 @@ static void *work_thread(void *arg)
}
ip_reassembly_expire(ip_reass, now);
stellar_thread_cron(thr_ctx);
// TODO
// plugin_manager_cron();
// poll_non_packet_events();
@@ -317,6 +332,8 @@ static void stellar_thread_join(struct stellar_runtime *ctx, uint8_t nr_threads)
int main(int argc, char **argv)
{
uint8_t nr_threads;
uint64_t last_stat = 0;
struct io_stat *io_stat;
memset(runtime, 0, sizeof(struct stellar_runtime));
memset(config, 0, sizeof(struct stellar_config));
timestamp_update();
@@ -339,6 +356,7 @@ int main(int argc, char **argv)
goto error_out;
}
stellar_config_print(config);
nr_threads = config->io_opts.nr_threads;
if (id_generator_init(config->dev_opts.base, config->dev_opts.offset) != 0)
{
@@ -346,6 +364,13 @@ int main(int argc, char **argv)
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)
{
@@ -353,7 +378,6 @@ int main(int argc, char **argv)
goto error_out;
}
nr_threads = config->io_opts.nr_threads;
runtime->packet_io = packet_io_new(&config->io_opts);
if (runtime->packet_io == NULL)
{
@@ -373,9 +397,18 @@ int main(int argc, char **argv)
goto error_out;
}
io_stat = packet_io_get_stat(runtime->packet_io);
last_stat = timestamp_get_msec();
while (!ATOMIC_READ(&runtime->need_exit))
{
timestamp_update();
if (timestamp_get_msec() - last_stat > 2000)
{
stellar_peek_io_stat(runtime->stat, io_stat);
stellar_stat_output(runtime->stat);
last_stat = timestamp_get_msec();
}
usleep(5 * 1000);
}
@@ -384,6 +417,7 @@ error_out:
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();