summaryrefslogtreecommitdiff
path: root/shaping/src/main.cpp
blob: b526ff72966c9df93730ca0b59e68c0408b1cd26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>

#include "log.h"
#include "utils.h"
#include "shaper.h"
#include "shaper_stat.h"
#include "shaper_marsio.h"
#include "shaper_session.h"
#include "shaper_swarmkv.h"
#include "shaper_global_stat.h"

static int quit = 0;

static void *shaper_thread_loop(void *data)
{
    struct shaping_thread_ctx *ctx = (struct shaping_thread_ctx *)data;

    marsio_thread_init(ctx->marsio_info->instance);

    //loop to process pkts
    while(!quit) {
        shaper_packet_recv_and_process(ctx);
        if (__atomic_load_n(&ctx->session_need_reset, __ATOMIC_SEQ_CST) > 0) {
            session_table_reset_with_callback(ctx->session_table, shaper_session_data_free_cb, ctx);
            __atomic_fetch_and(&ctx->session_need_reset, 0, __ATOMIC_SEQ_CST);
        }
        marsio_poll_wait(ctx->marsio_info->instance, &ctx->marsio_info->mr_dev, 1, ctx->thread_index, ctx->global_stat->output_interval_s * 1000);
    }

    return NULL;
}

static void sig_handler(int signo)
{
    if (signo == SIGHUP)
    {
        LOG_INFO("%s: recv SIGHUP, reload zlog.conf and swarmkv log level", LOG_TAG_SHAPING);
        LOG_RELOAD();
        swarmkv_reload_log_level();
    }

    if (signo == SIGQUIT) {
        quit = 1;
    }

    return;
}

int main(int argc, char **argv)
{
    struct shaping_ctx *ctx = NULL;
    time_t last_update_time = time(NULL);

    if (LOG_INIT("./conf/zlog.conf") == -1)
    {
        return -1;
    }

    if (signal(SIGHUP, sig_handler) == SIG_ERR)
    {
        LOG_ERROR("%s: unable to register SIGHUP signal handler, error %d: %s", LOG_TAG_SHAPING, errno, strerror(errno));
        LOG_CLOSE();
        return -1;
    }

    if (signal(SIGQUIT, sig_handler) == SIG_ERR)
    {
        LOG_ERROR("%s: unable to register SIGQUIT signal handler, error %d: %s", LOG_TAG_SHAPING, errno, strerror(errno));
        LOG_CLOSE();
        return -1;
    }

    ctx = shaping_engine_init();
    if (!ctx) {
        return -1;
    }

    for (int i = 0; i < ctx->thread_num; i++) {
        pthread_create(&ctx->thread_ctx[i].tid, NULL, shaper_thread_loop, &ctx->thread_ctx[i]);
    }

    while(!quit) {
        time_t curr_time = time(NULL);
        if (curr_time - last_update_time >= ctx->global_stat->output_interval_s) {
            shaper_global_stat_refresh(ctx->global_stat);
            last_update_time = curr_time;
        }
        sleep(ctx->global_stat->output_interval_s);
    }

    for (int i = 0; i < ctx->thread_num; i++) {
        pthread_join(ctx->thread_ctx[i].tid, NULL);
    }
    shaping_engine_destroy(ctx);

    return 0;
}