predictor_metric.h 9.9 KB
Newer Older
W
wangguibao 已提交
1 2 3 4
#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_FRAMEWORK_PREDICTOR_METRIC_H
#define BAIDU_PADDLE_SERVING_PREDICTOR_FRAMEWORK_PREDICTOR_METRIC_H 

#include <bvar/bvar.h> // bvar
W
wangguibao 已提交
5 6 7
#include <butil/scoped_lock.h> // BAIDU_SCOPED_LOCK
#include <butil/containers/flat_map.h> // FlatMap
#include <butil/memory/singleton.h> // DefaultSingletonTraits
W
wangguibao 已提交
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

namespace baidu {
namespace paddle_serving {
namespace predictor {

static const std::string WORKFLOW_METRIC_PREFIX = "workflow_";
static const std::string STAGE_METRIC_PREFIX = "stage_";
static const std::string OP_METRIC_PREFIX = "op_";
static const std::string NAME_DELIMITER = "_";

typedef ::bvar::Window<::bvar::Adder<int> > AdderWindow;
typedef ::bvar::Window<::bvar::IntRecorder> RecorderWindow;

class AdderWindowMetric {
public:
    AdderWindowMetric() :
        sum_window(&sum, ::bvar::FLAGS_bvar_dump_interval) {
    }

    AdderWindowMetric(const std::string& name) :
        sum_window(name + "_sum_window", &sum, ::bvar::FLAGS_bvar_dump_interval) {
    }

    inline AdderWindowMetric& operator<<(int count) {
        sum << count;
W
wangguibao 已提交
33
        return *this;
W
wangguibao 已提交
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
    } 

public:
    ::bvar::Adder<int> sum;
    AdderWindow sum_window;
};

static float g_get_rate(void* arg);
class RateBaseMetric {
public:
    RateBaseMetric(const std::string& name) :
            rate_value(name + "_rate", g_get_rate, this) {
    }
    
    void update_lhs(int count) { lhs.sum << count; }
                
    void update_rhs(int count) { rhs.sum << count; }
                    
public:
    ::bvar::PassiveStatus<float> rate_value;
    AdderWindowMetric lhs;
    AdderWindowMetric rhs;
};

static float g_get_rate(void* arg) {
    RateBaseMetric* rate_metric = static_cast<RateBaseMetric*>(arg); 
    if (rate_metric->rhs.sum_window.get_value() <= 0) {
        return 0;
    }
    return rate_metric->lhs.sum_window.get_value() * 100
            / (float) rate_metric->rhs.sum_window.get_value();
}

// 计算平均值时取整 
class AvgWindowMetric {
public:
    AvgWindowMetric() :
        avg_window(&avg, ::bvar::FLAGS_bvar_dump_interval) {
    }

    AvgWindowMetric(const std::string& name) :
        avg_window(name + "_avg_window", &avg, ::bvar::FLAGS_bvar_dump_interval) {
    }

    inline AvgWindowMetric& operator<<(int64_t value) {
        avg << value;
W
wangguibao 已提交
80
        return *this;
W
wangguibao 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    } 

public:
    ::bvar::IntRecorder avg;
    RecorderWindow avg_window;
};

// 计算平均值时不取整 
static double g_get_double_avg(void* arg);
class AvgDoubleWindowMetric {
public:
    AvgDoubleWindowMetric(const std::string& name) :
        avg_value(name + "_avg_double_window", g_get_double_avg, this) {
    }

    inline AvgDoubleWindowMetric& operator<<(int64_t value) {
        recorder << value;
W
wangguibao 已提交
98
        return *this;
W
wangguibao 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    } 

public:
    ::bvar::PassiveStatus<double> avg_value;
    AvgWindowMetric recorder;
};

static double g_get_double_avg(void* arg) {
    AvgDoubleWindowMetric* avg_metric = static_cast<AvgDoubleWindowMetric*>(arg); 
    return avg_metric->recorder.avg_window.get_value().get_average_double(); 
}

class PredictorMetric {
public:
    static PredictorMetric* GetInstance();

    ~PredictorMetric() {
W
wangguibao 已提交
116
        for (::butil::FlatMap<std::string, bvar::LatencyRecorder*>::iterator iter
W
wangguibao 已提交
117 118 119 120 121
                    = latency_recorder_map.begin();
                iter != latency_recorder_map.end();
                ++iter) {
            delete iter->second;
        }
W
wangguibao 已提交
122
        for (::butil::FlatMap<std::string, AdderWindowMetric*>::iterator iter
W
wangguibao 已提交
123 124 125 126 127
                    = adder_window_map.begin();
                iter != adder_window_map.end();
                ++iter) {
            delete iter->second;
        }
W
wangguibao 已提交
128
        for (::butil::FlatMap<std::string, AvgWindowMetric*>::iterator iter
W
wangguibao 已提交
129 130 131 132 133
                    = avg_window_map.begin();
                iter != avg_window_map.end();
                ++iter) {
            delete iter->second;
        }
W
wangguibao 已提交
134
        for (::butil::FlatMap<std::string, AvgDoubleWindowMetric*>::iterator iter
W
wangguibao 已提交
135 136 137 138 139
                    = avg_double_window_map.begin();
                iter != avg_double_window_map.end();
                ++iter) {
            delete iter->second;
        }
W
wangguibao 已提交
140
        for (::butil::FlatMap<std::string, RateBaseMetric*>::iterator iter
W
wangguibao 已提交
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
                    = rate_map.begin();
                iter != rate_map.end();
                ++iter) {
            delete iter->second;
        }
    }

    void regist_latency_metric(const std::string& metric_name) {
        {
            BAIDU_SCOPED_LOCK(_mutex);
            LOG(INFO) << "try to regist latency metric[" << metric_name << "].";
            if (latency_recorder_map.seek(metric_name) == NULL) {
                bvar::LatencyRecorder* metric = new (std::nothrow) bvar::LatencyRecorder(metric_name);
                latency_recorder_map.insert(metric_name, metric);
                LOG(INFO) << "succ to regist latency metric[" << metric_name << "].";
            }
        }
    }

    void regist_adder_window_metric(const std::string& metric_name) {
        {
            BAIDU_SCOPED_LOCK(_mutex);
            LOG(INFO) << "try to regist adder window metric[" << metric_name << "].";
            if (adder_window_map.seek(metric_name) == NULL) {
                AdderWindowMetric* metric = new (std::nothrow) AdderWindowMetric(metric_name);
                adder_window_map.insert(metric_name, metric);
                LOG(INFO) << "succ to regist adder window metric[" << metric_name << "].";
            }
        }
    }

    void regist_avg_window_metric(const std::string& metric_name) {
        {
            BAIDU_SCOPED_LOCK(_mutex);
            LOG(INFO) << "try to regist avg window metric[" << metric_name << "].";
            if (avg_window_map.seek(metric_name) == NULL) {
                AvgWindowMetric* metric = new (std::nothrow) AvgWindowMetric(metric_name);
                avg_window_map.insert(metric_name, metric);
                LOG(INFO) << "succ to regist avg window metric[" << metric_name << "].";
            }
        }
    }

    void regist_avg_double_window_metric(const std::string& metric_name) {
        {
            BAIDU_SCOPED_LOCK(_mutex);
            LOG(INFO) << "try to regist avg double window metric[" << metric_name << "].";
            if (avg_double_window_map.seek(metric_name) == NULL) {
                AvgDoubleWindowMetric* metric = new (std::nothrow) AvgDoubleWindowMetric(metric_name);
                avg_double_window_map.insert(metric_name, metric);
                LOG(INFO) << "succ to regist avg double window metric[" << metric_name << "].";
            }
        }
    }

    void regist_rate_metric(const std::string& metric_name) {
        {
            BAIDU_SCOPED_LOCK(_mutex);
            LOG(INFO) << "try to regist rate metric[" << metric_name << "].";
            if (rate_map.seek(metric_name) == NULL) {
                RateBaseMetric* metric = new (std::nothrow) RateBaseMetric(metric_name);
                rate_map.insert(metric_name, metric);
                LOG(INFO) << "succ to regist rate metric[" << metric_name << "].";
            }
        }
    }

    inline void update_latency_metric(const std::string& metric_name, int64_t latency) {
        bvar::LatencyRecorder** metric = latency_recorder_map.seek(metric_name);
        if (metric != NULL) {
            **metric << latency;
        } else {
213
            LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
214 215 216 217 218 219 220 221
        }
    }
    
    inline void update_adder_window_metric(const std::string& metric_name, int count) {
        AdderWindowMetric** metric = adder_window_map.seek(metric_name);
        if (metric != NULL) {
            **metric << count;
        } else {
222
            LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
223 224 225 226 227 228 229 230
        }
    }
    
    inline void update_avg_window_metric(const std::string& metric_name, int64_t value) {
        AvgWindowMetric** metric = avg_window_map.seek(metric_name);
        if (metric != NULL) {
            **metric << value;
        } else {
231
            LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
232 233 234 235 236 237 238 239
        }
    }

    inline void update_avg_double_window_metric(const std::string& metric_name, int64_t value) {
        AvgDoubleWindowMetric** metric = avg_double_window_map.seek(metric_name);
        if (metric != NULL) {
            **metric << value;
        } else {
240
            LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
241 242 243 244 245 246 247 248
        }
    }

    inline void update_rate_metric_lhs(const std::string& name, int count) {
        RateBaseMetric** metric = rate_map.seek(name);
        if (metric != NULL) {
            (*metric)->update_lhs(count);
        } else {
249
            LOG(ERROR) << "impossible, check if you regist[" << name << "].";
W
wangguibao 已提交
250 251 252 253 254 255 256 257
        }
    }
    
    inline void update_rate_metric_rhs(const std::string& name, int count) {
        RateBaseMetric** metric = rate_map.seek(name);
        if (metric != NULL) {
            (*metric)->update_rhs(count);
        } else {
258
            LOG(ERROR) << "impossible, check if you regist[" << name << "].";
W
wangguibao 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
        }
    }
    
private:
    PredictorMetric() : 
            bucket_count(300) {
        latency_recorder_map.init(bucket_count);
        adder_window_map.init(bucket_count);
        avg_window_map.init(bucket_count);
        avg_double_window_map.init(bucket_count);
        rate_map.init(bucket_count);
    }
    
private:
    const size_t bucket_count;
W
wangguibao 已提交
274 275 276 277 278
    ::butil::FlatMap<std::string, bvar::LatencyRecorder*> latency_recorder_map;
    ::butil::FlatMap<std::string, AdderWindowMetric*> adder_window_map;
    ::butil::FlatMap<std::string, AvgWindowMetric*> avg_window_map;
    ::butil::FlatMap<std::string, AvgDoubleWindowMetric*> avg_double_window_map;
    ::butil::FlatMap<std::string, RateBaseMetric*> rate_map;
W
wangguibao 已提交
279 280
   
    friend struct DefaultSingletonTraits<PredictorMetric>; 
W
wangguibao 已提交
281
    mutable butil::Mutex _mutex;
W
wangguibao 已提交
282 283 284 285 286 287 288 289 290
    DISALLOW_COPY_AND_ASSIGN(PredictorMetric);
};

} // namespace predictor
} // namespace paddle_serving 
} // namespace baidu

#endif // BAIDU_PADDLE_SERVING_PREDICTOR_FRAMEWORK_PREDICTOR_METRIC_H