summaryrefslogtreecommitdiff
path: root/src/http_header_v1.cpp
blob: 7680bc864b529a01b1e3d44edf4e5ae3a24955eb (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <algorithm> 
#include <sys/stat.h>
#include <sys/types.h>

#include <MESA/MESA_htable.h>
#include <MESA/MESA_handle_logger.h>
#include <MESA/MESA_prof_load.h>
#include <MESA/field_stat2.h>
#include <MESA/MESA_list_queue.h>

#include "http_header_v1.hpp"

BaseFS::~BaseFS()
{
    FS_stop(&fsstat_handle);
}

BaseFS::BaseFS(const char * header_name, const char *logdir)
{
    char fsfile[256], commd[256];
    int  value;

    if(0 != access(logdir, 0))
    {
        snprintf(commd, 256, "mkdir -p %s", logdir);
        system(commd);
    }
    snprintf(fsfile, 256, "%s/%s.txt", logdir, header_name);
    fsstat_handle = FS_create_handle();
    FS_set_para(fsstat_handle, OUTPUT_DEVICE, fsfile, strlen(fsfile)+1);
    value = 1;
    FS_set_para(fsstat_handle, PRINT_MODE, &(value), sizeof(value));
    value = 1;
    FS_set_para(fsstat_handle, STAT_CYCLE, &(value), sizeof(value));
    //value = 1;
    //FS_set_para(fsstat_handle, CREATE_THREAD, &value, sizeof(value));
    value = 1;
    FS_set_para(fsstat_handle, FLUSH_BY_DATE, &value, sizeof(value));
    char appname[64];
    FS_set_para(fsstat_handle, APP_NAME, appname, strlen(appname)+1);
    char dst_ip[16] = "127.0.0.1";
    int dst_port = 80;
    FS_set_para(fsstat_handle, STATS_SERVER_IP, dst_ip, strlen(dst_ip)+1);
    FS_set_para(fsstat_handle, STATS_SERVER_PORT, &dst_port, sizeof(dst_port));   
}

void BaseFS::start(void)
{
    FS_start(fsstat_handle);    
}

void BaseFS::passive_output(void)
{
    FS_passive_output(fsstat_handle);    
}

ContentTable::~ContentTable()
{
    FS_stop(&fsstat_handle);
}

ContentTable::ContentTable(const char * header_name, const char *logdir):BaseFS(header_name, logdir)
{
    char total_num[64], no_head_num[64], head_num[64];
    total_status_id = 0;
    no_header_id = 0;
    num_column_id = 0;
    ratio_column_id = 0;
    header_total = 0;
    no_header_num = 0;
    
    snprintf(total_num, 64, "%s_total", header_name);
    snprintf(no_head_num, 64, "no_%s", header_name);
    snprintf(head_num, 64, "%s_num", header_name);
    if(0 != pthread_mutex_init(&mutex_lock, NULL))
        printf("init mutex error!!!!!!!!\n");

    total_status_id = FS_register(fsstat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, total_num);
    no_header_id = FS_register(fsstat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, no_head_num);
    num_column_id = FS_register(fsstat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, head_num);
    ratio_column_id = FS_register(fsstat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, "RATIO/milli");
}

int ContentTable::operate(enum field_op op, string line_name, long long value)
{
    map<string, http_header_t>::iterator iter;
    int line_id;
    int ret = 0;
    http_header_t hht;
    string::size_type idx;
    iter = dynamic_line_id.find(line_name);
    if(iter == dynamic_line_id.end())
    {
        pthread_mutex_lock(&mutex_lock);
        if(iter == dynamic_line_id.end()) //防止多个线程竞争锁导致的dynamic_line_id不是最新多加一层判断
        {
            line_id = FS_register(fsstat_handle, FS_STYLE_LINE, FS_CALC_CURRENT, line_name.c_str());
            hht.header_id = line_id;
            hht.header_num = 1;
            dynamic_line_id[line_name] = hht;
        }
        else
        {
            iter = dynamic_line_id.find(line_name);
            if(iter == dynamic_line_id.end())
            {
                line_id = FS_register(fsstat_handle, FS_STYLE_LINE, FS_CALC_CURRENT, line_name.c_str());
                hht.header_id = line_id;
                hht.header_num = 1;
                dynamic_line_id[line_name] = hht;
            }
            else
            {
                line_id = iter->second.header_id;
                __sync_add_and_fetch(&(iter->second.header_num), 1);
            }
        }
        pthread_mutex_unlock(&mutex_lock);
    }
    else
    {
        line_id = iter->second.header_id;
        __sync_add_and_fetch(&(iter->second.header_num), 1);
    }
    __sync_add_and_fetch(&header_total, 1);
    return ret;
}

void ContentTable::operate_ratio()
{
    map<string, http_header_t>::iterator iter;
    http_header_t hht;
    long long ratio = 0;
    pthread_mutex_lock(&mutex_lock);
    FS_operate(fsstat_handle, total_status_id, 0, FS_OP_SET, header_total);
    FS_operate(fsstat_handle, no_header_id, 0, FS_OP_SET, no_header_num);

    iter = dynamic_line_id.begin();
    while(iter != dynamic_line_id.end())
    {
        hht = iter->second;
        ratio = (hht.header_num*1000)/header_total;
        FS_operate(fsstat_handle, hht.header_id, num_column_id, FS_OP_SET, hht.header_num);
        FS_operate(fsstat_handle, hht.header_id, ratio_column_id, FS_OP_SET, ratio);
        iter ++;
    }
    FS_passive_output(fsstat_handle);
    pthread_mutex_unlock(&mutex_lock); 
}

long long ContentTable::set_nocontent_num()
{
    return __sync_add_and_fetch(&no_header_num, 1);
}

long long ContentTable::get_head_total()
{
    return header_total;
}

ContentHistogram::~ContentHistogram()
{
    FS_stop(&fsstat_handle);
}

ContentHistogram::ContentHistogram(const char * header_name, const char *logdir):BaseFS(header_name, logdir)
{
    if(0 != pthread_mutex_init(&mutex_lock, NULL))
        printf("init mutex error!!!!!!!!\n");
    const char* histogram_format="0.25,0.5,0.75,0.9,0.99000";
    FS_set_para(fsstat_handle, HISTOGRAM_GLOBAL_BINS, histogram_format, strlen(histogram_format)+10);
    histogram_id = FS_register_histogram(fsstat_handle, FS_CALC_CURRENT, "CONTENT_LENGHT" ,1,9999999999,3);
}

int ContentHistogram::content_lenght_operate(long long value)
{
    int ret = 0;
    ret = FS_operate(fsstat_handle, histogram_id, 0, FS_OP_SET, value);
    //FS_passive_output(fsstat_handle);    
    return ret;
}