summaryrefslogtreecommitdiff
path: root/infra/monitor/monitor_stat.c
blob: 2216b094de459b93cba8a62f865ae93733802845 (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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <arpa/inet.h>
#include <getopt.h>
#include <evhttp.h>
#include "monitor_private.h"
#include <fieldstat/fieldstat_easy.h>

static const char *stm_stat_field_name[] = {
    "connection_new",
    "connection_close",
    "request_succ",
    "request_err",
    "response_succ",
    "response_err",
    NULL,
};

long long stm_get_stat_count(struct stm_stat *stat, enum stm_stat_type type)
{
    if ((int)type < 0 || type >= STM_STAT_MAX)
    {
        return 0;
    }
    return stat->counters[type].count;
}

long long stm_get_stat_bytes(struct stm_stat *stat, enum stm_stat_type type)
{
    if ((int)type < 0 || type >= STM_STAT_MAX)
    {
        return 0;
    }
    return stat->counters[type].bytes;
}

void stm_stat_update(struct stm_stat *stat, int thread_idx, enum stm_stat_type type, long long value)
{
    if ((int)type < 0 || type >= STM_STAT_MAX)
    {
        return;
    }
    fieldstat_easy_counter_incrby(stat->fs4_ins, thread_idx, stat->counters[type].counter_id, NULL, 0, value);
}

struct stm_stat *stm_stat_init(struct stellar_monitor *stm)
{
    const struct stellar_monitor_config *config = stm->config;
    assert(sizeof(stm_stat_field_name) / sizeof(stm_stat_field_name[0]) == STM_STAT_MAX + 1);
    struct stm_stat *stat = CALLOC(struct stm_stat, 1);
    /* worker thread count + 1, reserved for libevent callback thread context */
    stat->fs4_ins = fieldstat_easy_new(stm->worker_thread_num + 1, "monitor", NULL, 0);
    for (int i = 0; stm_stat_field_name[i] != NULL; i++)
    {
        stat->counters[i].counter_id = fieldstat_easy_register_counter(stat->fs4_ins, stm_stat_field_name[i]);
    }
    fieldstat_easy_enable_auto_output(stat->fs4_ins, config->output_path, MAX(config->output_interval_ms / 1000, 1));
    return stat;
}

void stm_stat_free(struct stm_stat *stat)
{
    if (NULL == stat)
    {
        return;
    }
    if (stat->fs4_ins)
    {
        fieldstat_easy_free(stat->fs4_ins);
    }
    FREE(stat);
}