summaryrefslogtreecommitdiff
path: root/src/http_decoder_stat.cpp
blob: ffa01ebb1d3d562602d61d36f80fb9cfb8432f4a (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
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include "http_decoder_inc.h"

static __thread struct http_decoder_stat _th_stat;

int http_decoder_stat_init(struct http_decoder_context *ctx, int thread_num)
{
    ctx->fse = fieldstat_easy_new(thread_num, "http_decoder_statistics", NULL, 0);
    if (NULL == ctx->fse)
    {
        fprintf(stderr, "fieldstat_easy_new failed.");
        return -1;
    }

    ctx->fs_incoming_bytes_id =
        fieldstat_easy_register_counter(ctx->fse, "incoming_bytes");
    if (ctx->fs_incoming_bytes_id < 0)
    {
        fprintf(stderr, "fieldstat_easy_register_counter incoming_bytes failed.");
        return -1;
    }

    ctx->fs_incoming_trans_id =
        fieldstat_easy_register_counter(ctx->fse, "incoming_trans");
    if (ctx->fs_incoming_trans_id < 0)
    {
        fprintf(stderr, "fieldstat_easy_register_counter incoming_trans failed.");
        return -1;
    }

    ctx->fs_incoming_pkts_id =
        fieldstat_easy_register_counter(ctx->fse, "incoming_pkts");
    if (ctx->fs_incoming_pkts_id < 0)
    {
        fprintf(stderr, "fieldstat_easy_register_counter incoming_pkts failed.");
        return -1;
    }

    ctx->fs_err_pkts_id = fieldstat_easy_register_counter(ctx->fse, "err_pkts");
    if (ctx->fs_err_pkts_id < 0)
    {
        fprintf(stderr, "fieldstat_easy_register_counter err_pkts failed.");
        return -1;
    }

    int stat_output_interval = DEFAULT_STAT_OUTPUT_INTERVAL;
    if (ctx->hd_cfg.stat_output_interval > 0)
    {
        stat_output_interval = ctx->hd_cfg.stat_output_interval;
    }

    int ret = fieldstat_easy_enable_auto_output(ctx->fse, FILEDSTAT_OUTPUT_FILE,
                                                stat_output_interval);
    if (ret < 0)
    {
        fprintf(stderr, "fieldstat_easy_enable_auto_output failed.");
        return -1;
    }

    return 0;
}

void http_decoder_stat_output(struct http_decoder_context *ctx, int thread_id)
{
    assert(ctx != NULL);

    int stat_interval_pkts = DEFAULT_STAT_INTERVAL_PKTS;
    if (ctx->hd_cfg.stat_interval_pkts > 0)
    {
        stat_interval_pkts = ctx->hd_cfg.stat_interval_pkts;
    }

    if (_th_stat.counter >= stat_interval_pkts)
    {
        fieldstat_easy_counter_incrby(ctx->fse, thread_id,
                                      ctx->fs_incoming_bytes_id, NULL, 0,
                                      _th_stat.incoming_bytes);

        fieldstat_easy_counter_incrby(ctx->fse, thread_id,
                                      ctx->fs_incoming_pkts_id, NULL, 0,
                                      _th_stat.incoming_pkts);

        fieldstat_easy_counter_incrby(ctx->fse, thread_id,
                                      ctx->fs_incoming_trans_id, NULL, 0,
                                      _th_stat.incoming_trans);

        fieldstat_easy_counter_incrby(ctx->fse, thread_id,
                                      ctx->fs_err_pkts_id, NULL, 0,
                                      _th_stat.err_pkts);

        _th_stat.counter = 0;
        _th_stat.err_pkts = 0;
        _th_stat.incoming_bytes = 0;
        _th_stat.incoming_pkts = 0;
        _th_stat.incoming_trans = 0;
    }
}