summaryrefslogtreecommitdiffstats
path: root/src/time_histogram.cpp
blob: b36fe164e77322dde9f3af83aa1098097759b7eb (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 Hanoh Haim
 Ido Barnea
 Cisco Systems, Inc.
*/

/*
Copyright (c) 2015-2016 Cisco Systems, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <stdint.h>
#include <string.h>
#include "utl_json.h"
#include "pal/linux/sanb_atomic.h"
#include "time_histogram.h"

void CTimeHistogram::Reset() {
    m_period_data[0].reset();
    m_period_data[1].reset();
    m_period = 0;
    m_total_cnt = 0;
    m_total_cnt_high = 0;
    m_max_dt = 0;
    m_average = 0;
    memset(&m_max_ar[0],0,sizeof(m_max_ar));
    m_win_cnt = 0;

    int i;
    int j;
    for (i = 0; i < HISTOGRAM_SIZE; i++) {
        for (j = 0; j < HISTOGRAM_SIZE_LOG; j++) {
            m_hcnt[j][i] = 0;
        }
    }
}

bool CTimeHistogram::Create() {
    Reset();
    m_min_delta =10.0/1000000.0;
    return (true);
}

void CTimeHistogram::Delete() {
}

bool CTimeHistogram::Add(dsec_t dt) {
    CTimeHistogramPerPeriodData &period_elem = m_period_data[m_period];

    period_elem.inc_cnt();
    period_elem.update_sum(dt);
    period_elem.update_max(dt);

    // values smaller then certain threshold do not get into the histogram
    if (dt < m_min_delta) {
        return false;
    }
    period_elem.inc_high_cnt();

    uint32_t d_10usec = (uint32_t)(dt*100000.0);
    // 1 10-19 usec
    //,2 -20-29 usec
    //,3,

    int j;
    for (j = 0; j < HISTOGRAM_SIZE_LOG; j++) {
        uint32_t low  = d_10usec % 10;
        uint32_t high = d_10usec / 10;
        if (high == 0) {
            if (low > 0) {
                low = low - 1;
            }
            m_hcnt[j][low]++;
            break;
        } else {
            d_10usec = high;
        }
    }

    return true;
}

void CTimeHistogram::update() {
    // switch period, and get values for pervious period
    CTimeHistogramPerPeriodData &period_elem = m_period_data[m_period];
    uint8_t new_period;

    if (m_period == 0) {
        new_period = 1;
    } else {
        new_period = 0;
    }
    m_period_data[new_period].reset();
    sanb_smp_memory_barrier();
    m_period = new_period;
    sanb_smp_memory_barrier();

    m_max_ar[m_win_cnt] = period_elem.get_max();
    m_win_cnt++;
    if (m_win_cnt == HISTOGRAM_QUEUE_SIZE) {
        m_win_cnt = 0;
    }
    update_average(period_elem);
    m_total_cnt += period_elem.get_cnt();
    m_total_cnt_high += period_elem.get_high_cnt();
    if ( m_max_dt < period_elem.get_max()) {
        m_max_dt = period_elem.get_max();
    }
}

void  CTimeHistogram::update_average(CTimeHistogramPerPeriodData &period_elem) {
    double c_average;

    if (period_elem.get_cnt() != 0)
        c_average = period_elem.get_sum() / period_elem.get_cnt();
    else
        c_average = 0;

    // low pass filter
    m_average = 0.5 * m_average + 0.5 * c_average;
}

dsec_t  CTimeHistogram::get_average_latency() {
    return (m_average);
}


uint32_t CTimeHistogram::get_usec(dsec_t d) {
    return (uint32_t)(d*1000000.0);
}

void CTimeHistogram::DumpWinMax(FILE *fd) {
    int i;
    uint32_t ci=m_win_cnt;

    for (i=0; i<HISTOGRAM_QUEUE_SIZE-1; i++) {
        dsec_t d=get_usec(m_max_ar[ci]);
        ci++;
        if (ci>HISTOGRAM_QUEUE_SIZE-1) {
            ci=0;
        }
        fprintf(fd," %.0f ",d);
    }
}

void CTimeHistogram::Dump(FILE *fd) {
    CTimeHistogramPerPeriodData &period_elem = m_period_data[get_read_period_index()];

    fprintf (fd," min_delta  : %lu usec \n", (ulong)get_usec(m_min_delta));
    fprintf (fd," cnt        : %lu \n", period_elem.get_cnt());
    fprintf (fd," high_cnt   : %lu \n", period_elem.get_high_cnt());
    fprintf (fd," max_d_time : %lu usec\n", (ulong)get_usec(m_max_dt));
    fprintf (fd," sliding_average    : %.0f usec\n", get_average_latency());
    fprintf (fd," precent    : %.1f %%\n",(100.0*(double)period_elem.get_high_cnt()/(double)period_elem.get_cnt()));

    fprintf (fd," histogram \n");
    fprintf (fd," -----------\n");
    int i;
    int j;
    int base=10;
    for (j = 0; j < HISTOGRAM_SIZE_LOG; j++) {
        for (i = 0; i < HISTOGRAM_SIZE; i++) {
            if (m_hcnt[j][i] > 0) {
                fprintf (fd," h[%u]  :  %llu \n",(base*(i+1)),(unsigned long long)m_hcnt[j][i]);
            }
        }
        base=base*10;
    }
}

// Used in statefull
void CTimeHistogram::dump_json(std::string name,std::string & json ) {
    char buff[200];
    if (name != "")
        sprintf(buff,"\"%s\":{",name.c_str());
    else
        sprintf(buff,"{");
    json+=std::string(buff);

    json += add_json("min_usec", get_usec(m_min_delta));
    json += add_json("max_usec", get_usec(m_max_dt));
    json += add_json("high_cnt", m_total_cnt_high);
    json += add_json("cnt", m_total_cnt);
    json+=add_json("s_avg", get_average_latency());
    int i;
    int j;
    uint32_t base=10;

    json+=" \"histogram\": [";
    bool first=true;
    for (j = 0; j < HISTOGRAM_SIZE_LOG; j++) {
        for (i = 0; i < HISTOGRAM_SIZE; i++) {
            if (m_hcnt[j][i] > 0) {
                if ( first ) {
                    first = false;
                }else{
                    json += ",";
                }
                json += "{";
                json += add_json("key",(base*(i+1)));
                json += add_json("val",m_hcnt[j][i],true);
                json += "}";
            }
        }
        base = base * 10;
    }
    json+="  ] } ,";
}

// Used in stateless
void CTimeHistogram::dump_json(Json::Value & json, bool add_histogram) {
    int i, j;
    uint32_t base=10;
    CTimeHistogramPerPeriodData &period_elem = m_period_data[get_read_period_index()];

    json["total_max"] = get_usec(m_max_dt);
    json["last_max"] = get_usec(period_elem.get_max());
    json["average"] = get_average_latency();

    if (add_histogram) {
        for (j = 0; j < HISTOGRAM_SIZE_LOG; j++) {
            for (i = 0; i < HISTOGRAM_SIZE; i++) {
                if (m_hcnt[j][i] > 0) {
                    std::string key = static_cast<std::ostringstream*>( &(std::ostringstream()
                                                                          << int(base * (i + 1)) ) )->str();
                    json["histogram"][key] = Json::Value::UInt64(m_hcnt[j][i]);
                }
            }
            base = base * 10;
        }
        if (m_total_cnt != m_total_cnt_high) {
            json["histogram"]["0"] = Json::Value::UInt64(m_total_cnt - m_total_cnt_high);
        }
    }
}