summaryrefslogtreecommitdiff
path: root/src/stat.cc
blob: 21b266412b224b19a5a10dcc29e4f8a7526a6051 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * \brief 事件计数统计与状态暴露
 * \author Lu Qiuwen<[email protected]>
 */

#include <field_stat2.h>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <MESA_prof_load.h>
#include <cassert>

#include "stat.h"

#ifndef __TFE_STRING_MAX
#define __TFE_STRING_MAX                      2048
#endif

static const char * __str_stat_entry[] =
{
#define __TFE_STAT_ENTRY_GEN(x, s, t)    (s),
    STAT_ENTRY_MAP(__TFE_STAT_ENTRY_GEN)
#undef __TFE_STAT_ENTRY_GEN
};

static int __stat_type[] =
{
#define __TFE_STAT_ENTRY_GEN(x, s, t)    (t),
    STAT_ENTRY_MAP(__TFE_STAT_ENTRY_GEN)
#undef __TFE_STAT_ENTRY_GEN
};

const static enum field_dsp_style_t __dsp_styple_map[] =
{
    [TFE_STAT_ENTRY_TYPE_GLOBAL] = FS_STYLE_FIELD,
    [TFE_STAT_ENTRY_TYPE_PER_THREAD] = FS_STYLE_COLUMN
};

struct tfe_stat_ctx
{
    /* Feedback Server Address */
    char srv_addr[__TFE_STRING_MAX];
    uint16_t srv_port;

    /* Stat Log file */
    char fs_log[__TFE_STRING_MAX];

    /* Entry ID */
    int entry_id[TFE_STAT_ENTRY_MAX];

    /* handle */
    screen_stat_handle_t fs_handle;
};

struct tfe_stat_ctx * tfe_stat_create(const char * cfgfile)
{
    struct tfe_stat_ctx * __ctx = (struct tfe_stat_ctx *) malloc(sizeof(struct tfe_stat_ctx));
    memset(__ctx, 0, sizeof(struct tfe_stat_ctx));

    __ctx->fs_handle = FS_create_handle();

    MESA_load_profile_string_def(cfgfile, "fs", "stats_server_addr", __ctx->srv_addr, sizeof(__ctx->srv_addr), "");
    MESA_load_profile_uint_def(cfgfile, "fs", "stats_server_port", (unsigned *) &__ctx->srv_port, 0);

    /* Operation Setup */
    if (__ctx->srv_port)
    {
        FS_set_para(__ctx->fs_handle, STATS_SERVER_IP, __ctx->srv_addr, sizeof(__ctx->srv_addr));
        FS_set_para(__ctx->fs_handle, STATS_SERVER_PORT, (const void *)&__ctx->srv_port, sizeof(__ctx->srv_port));
    }

    MESA_load_profile_string_def(cfgfile, "fs", "logfile", __ctx->fs_log, sizeof(__ctx->fs_log), "fs_output.log");
    FS_set_para(__ctx->fs_handle, OUTPUT_DEVICE, __ctx->fs_log, (int)(strlen(__ctx->fs_log) + 1));

    uint32_t __option = 1;
    FS_set_para(__ctx->fs_handle, CREATE_THREAD, (const void *)(&__option), sizeof(__option));

    char __app_name[] = "tfe";
    FS_set_para(__ctx->fs_handle, APP_NAME, (const void *)(__app_name), sizeof(__app_name));

    /* Stat entry setup */
    for(int i = 0; i < TFE_STAT_ENTRY_MAX; i++)
    {
        __ctx->entry_id[i] = FS_register(__ctx->fs_handle, __dsp_styple_map[__stat_type[i]],
            FS_CALC_SPEED, __str_stat_entry[i]);

        assert(__ctx->entry_id[i] >= 0);
    }

    FS_start(__ctx->fs_handle);
    return __ctx;
}

void tfe_stat_destroy(struct tfe_stat_ctx * ctx)
{
    FS_stop(&ctx->fs_handle);
    free(ctx);
}

#include "pxythrmgr.h"

void tfe_stat_incrase_global(struct tfe_stat_ctx * ctx, enum tfe_stat_entry id, int num)
{
    FS_operate(ctx->fs_handle, ctx->entry_id[id], tfe_thread_current_thread_id(), FS_OP_ADD, num);
}

void tfe_stat_incrase_thread(struct tfe_stat_ctx * ctx, enum tfe_stat_entry id, unsigned thread_id, int num)
{
    FS_operate(ctx->fs_handle, ctx->entry_id[id], thread_id, FS_OP_ADD, num);
}

void tfe_stat_set_global(struct tfe_stat_ctx * ctx, enum tfe_stat_entry id, int num)
{
    FS_operate(ctx->fs_handle, ctx->entry_id[id], 0, FS_OP_SET, num);
}

void tfe_stat_set_global(struct tfe_stat_ctx * ctx, enum tfe_stat_entry id, unsigned thread_id, int num)
{
    FS_operate(ctx->fs_handle, ctx->entry_id[id], thread_id, FS_OP_SET, num);
}