predictor_metric.h 9.7 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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.
W
wangguibao 已提交
14

W
wangguibao 已提交
15 16 17 18 19 20
#pragma once
#include <butil/containers/flat_map.h>  // FlatMap
#include <butil/memory/singleton.h>     // DefaultSingletonTraits
#include <butil/scoped_lock.h>          // BAIDU_SCOPED_LOCK
#include <bvar/bvar.h>                  // bvar
#include <string>
W
wangguibao 已提交
21 22 23 24 25

namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
26 27 28 29
static const char* WORKFLOW_METRIC_PREFIX = "workflow_";
static const char* STAGE_METRIC_PREFIX = "stage_";
static const char* OP_METRIC_PREFIX = "op_";
static const char* NAME_DELIMITER = "_";
W
wangguibao 已提交
30

W
wangguibao 已提交
31
typedef ::bvar::Window<::bvar::Adder<int>> AdderWindow;
W
wangguibao 已提交
32 33 34
typedef ::bvar::Window<::bvar::IntRecorder> RecorderWindow;

class AdderWindowMetric {
W
wangguibao 已提交
35 36
 public:
  AdderWindowMetric() : sum_window(&sum, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
37

W
wangguibao 已提交
38 39 40
  explicit AdderWindowMetric(const std::string& name)
      : sum_window(
            name + "_sum_window", &sum, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
41

W
wangguibao 已提交
42 43 44 45
  inline AdderWindowMetric& operator<<(int count) {
    sum << count;
    return *this;
  }
W
wangguibao 已提交
46

W
wangguibao 已提交
47 48 49
 public:
  ::bvar::Adder<int> sum;
  AdderWindow sum_window;
W
wangguibao 已提交
50 51 52 53
};

static float g_get_rate(void* arg);
class RateBaseMetric {
W
wangguibao 已提交
54 55 56 57 58 59 60 61 62 63 64 65
 public:
  explicit 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;
W
wangguibao 已提交
66 67 68
};

static float g_get_rate(void* arg) {
W
wangguibao 已提交
69 70 71 72 73 74
  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 /
         static_cast<float>(rate_metric->rhs.sum_window.get_value());
W
wangguibao 已提交
75 76
}

W
wangguibao 已提交
77
// 计算平均值时取整
W
wangguibao 已提交
78
class AvgWindowMetric {
W
wangguibao 已提交
79 80
 public:
  AvgWindowMetric() : avg_window(&avg, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
81

W
wangguibao 已提交
82 83 84
  explicit AvgWindowMetric(const std::string& name)
      : avg_window(
            name + "_avg_window", &avg, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
85

W
wangguibao 已提交
86 87 88 89
  inline AvgWindowMetric& operator<<(int64_t value) {
    avg << value;
    return *this;
  }
W
wangguibao 已提交
90

W
wangguibao 已提交
91 92 93
 public:
  ::bvar::IntRecorder avg;
  RecorderWindow avg_window;
W
wangguibao 已提交
94 95
};

W
wangguibao 已提交
96
// 计算平均值时不取整
W
wangguibao 已提交
97 98
static double g_get_double_avg(void* arg);
class AvgDoubleWindowMetric {
W
wangguibao 已提交
99 100 101
 public:
  explicit AvgDoubleWindowMetric(const std::string& name)
      : avg_value(name + "_avg_double_window", g_get_double_avg, this) {}
W
wangguibao 已提交
102

W
wangguibao 已提交
103 104 105 106
  inline AvgDoubleWindowMetric& operator<<(int64_t value) {
    recorder << value;
    return *this;
  }
W
wangguibao 已提交
107

W
wangguibao 已提交
108 109 110
 public:
  ::bvar::PassiveStatus<double> avg_value;
  AvgWindowMetric recorder;
W
wangguibao 已提交
111 112 113
};

static double g_get_double_avg(void* arg) {
W
wangguibao 已提交
114 115
  AvgDoubleWindowMetric* avg_metric = static_cast<AvgDoubleWindowMetric*>(arg);
  return avg_metric->recorder.avg_window.get_value().get_average_double();
W
wangguibao 已提交
116 117 118
}

class PredictorMetric {
W
wangguibao 已提交
119 120
 public:
  static PredictorMetric* GetInstance();
W
wangguibao 已提交
121

W
wangguibao 已提交
122 123 124 125 126 127
  ~PredictorMetric() {
    for (::butil::FlatMap<std::string, bvar::LatencyRecorder*>::iterator iter =
             latency_recorder_map.begin();
         iter != latency_recorder_map.end();
         ++iter) {
      delete iter->second;
W
wangguibao 已提交
128
    }
W
wangguibao 已提交
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
    for (::butil::FlatMap<std::string, AdderWindowMetric*>::iterator iter =
             adder_window_map.begin();
         iter != adder_window_map.end();
         ++iter) {
      delete iter->second;
    }
    for (::butil::FlatMap<std::string, AvgWindowMetric*>::iterator iter =
             avg_window_map.begin();
         iter != avg_window_map.end();
         ++iter) {
      delete iter->second;
    }
    for (::butil::FlatMap<std::string, AvgDoubleWindowMetric*>::iterator iter =
             avg_double_window_map.begin();
         iter != avg_double_window_map.end();
         ++iter) {
      delete iter->second;
    }
    for (::butil::FlatMap<std::string, RateBaseMetric*>::iterator iter =
             rate_map.begin();
         iter != rate_map.end();
         ++iter) {
      delete iter->second;
    }
  }
W
wangguibao 已提交
154

W
wangguibao 已提交
155 156 157 158 159 160 161 162 163 164
  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 << "].";
      }
W
wangguibao 已提交
165
    }
W
wangguibao 已提交
166
  }
W
wangguibao 已提交
167

W
wangguibao 已提交
168 169 170 171 172 173 174 175 176 177 178
  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
                  << "].";
      }
W
wangguibao 已提交
179
    }
W
wangguibao 已提交
180
  }
W
wangguibao 已提交
181

W
wangguibao 已提交
182 183 184 185 186 187 188 189 190 191
  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 << "].";
      }
W
wangguibao 已提交
192
    }
W
wangguibao 已提交
193
  }
W
wangguibao 已提交
194

W
wangguibao 已提交
195 196 197 198 199 200 201 202 203 204 205 206
  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
                  << "].";
      }
W
wangguibao 已提交
207
    }
W
wangguibao 已提交
208
  }
W
wangguibao 已提交
209

W
wangguibao 已提交
210 211 212 213 214 215 216 217 218
  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 << "].";
      }
W
wangguibao 已提交
219
    }
W
wangguibao 已提交
220 221 222 223 224 225 226 227 228
  }

  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 {
      LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
229
    }
W
wangguibao 已提交
230 231 232 233 234 235 236 237 238
  }

  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 {
      LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
239
    }
W
wangguibao 已提交
240
  }
W
wangguibao 已提交
241

W
wangguibao 已提交
242 243 244 245 246 247 248
  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 {
      LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
249
    }
W
wangguibao 已提交
250
  }
W
wangguibao 已提交
251

W
wangguibao 已提交
252 253 254 255 256 257 258
  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 {
      LOG(ERROR) << "impossible, check if you regist[" << metric_name << "].";
W
wangguibao 已提交
259
    }
W
wangguibao 已提交
260 261 262 263 264 265 266 267
  }

  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 {
      LOG(ERROR) << "impossible, check if you regist[" << name << "].";
W
wangguibao 已提交
268
    }
W
wangguibao 已提交
269 270 271 272 273 274 275 276
  }

  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 {
      LOG(ERROR) << "impossible, check if you regist[" << name << "].";
W
wangguibao 已提交
277
    }
W
wangguibao 已提交
278
  }
W
wangguibao 已提交
279

W
wangguibao 已提交
280 281 282 283 284 285 286 287
 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);
  }
W
wangguibao 已提交
288

W
wangguibao 已提交
289 290 291 292 293 294 295 296 297 298 299 300
 private:
  const size_t bucket_count;
  ::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;

  friend struct DefaultSingletonTraits<PredictorMetric>;
  mutable butil::Mutex _mutex;
  DISALLOW_COPY_AND_ASSIGN(PredictorMetric);
};
W
wangguibao 已提交
301

W
wangguibao 已提交
302 303 304
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu