predictor_metric.h 10.0 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
#pragma once
W
wangguibao 已提交
16 17 18 19 20 21

#ifdef BCLOUD
#include <base/containers/flat_map.h>  // FlatMap
#include <base/memory/singleton.h>     // DefaultSingletonTraits
#include <base/scoped_lock.h>          // BAIDU_SCOPED_LOCK
#else
W
wangguibao 已提交
22 23 24
#include <butil/containers/flat_map.h>  // FlatMap
#include <butil/memory/singleton.h>     // DefaultSingletonTraits
#include <butil/scoped_lock.h>          // BAIDU_SCOPED_LOCK
W
wangguibao 已提交
25 26
#endif

W
wangguibao 已提交
27 28
#include <bvar/bvar.h>                  // bvar
#include <string>
W
wangguibao 已提交
29

W
wangguibao 已提交
30 31 32 33
#ifdef BCLOUD
namespace butil = base;
#endif

W
wangguibao 已提交
34 35 36 37
namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
38 39 40 41
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 已提交
42

W
wangguibao 已提交
43
typedef ::bvar::Window<::bvar::Adder<int>> AdderWindow;
W
wangguibao 已提交
44 45 46
typedef ::bvar::Window<::bvar::IntRecorder> RecorderWindow;

class AdderWindowMetric {
W
wangguibao 已提交
47 48
 public:
  AdderWindowMetric() : sum_window(&sum, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
49

W
wangguibao 已提交
50 51 52
  explicit AdderWindowMetric(const std::string& name)
      : sum_window(
            name + "_sum_window", &sum, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
53

W
wangguibao 已提交
54 55 56 57
  inline AdderWindowMetric& operator<<(int count) {
    sum << count;
    return *this;
  }
W
wangguibao 已提交
58

W
wangguibao 已提交
59 60 61
 public:
  ::bvar::Adder<int> sum;
  AdderWindow sum_window;
W
wangguibao 已提交
62 63 64 65
};

static float g_get_rate(void* arg);
class RateBaseMetric {
W
wangguibao 已提交
66 67 68 69 70 71 72 73 74 75 76 77
 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 已提交
78 79 80
};

static float g_get_rate(void* arg) {
W
wangguibao 已提交
81 82 83 84 85 86
  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 已提交
87 88
}

W
wangguibao 已提交
89
// 计算平均值时取整
W
wangguibao 已提交
90
class AvgWindowMetric {
W
wangguibao 已提交
91 92
 public:
  AvgWindowMetric() : avg_window(&avg, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
93

W
wangguibao 已提交
94 95 96
  explicit AvgWindowMetric(const std::string& name)
      : avg_window(
            name + "_avg_window", &avg, ::bvar::FLAGS_bvar_dump_interval) {}
W
wangguibao 已提交
97

W
wangguibao 已提交
98 99 100 101
  inline AvgWindowMetric& operator<<(int64_t value) {
    avg << value;
    return *this;
  }
W
wangguibao 已提交
102

W
wangguibao 已提交
103 104 105
 public:
  ::bvar::IntRecorder avg;
  RecorderWindow avg_window;
W
wangguibao 已提交
106 107
};

W
wangguibao 已提交
108
// 计算平均值时不取整
W
wangguibao 已提交
109 110
static double g_get_double_avg(void* arg);
class AvgDoubleWindowMetric {
W
wangguibao 已提交
111 112 113
 public:
  explicit AvgDoubleWindowMetric(const std::string& name)
      : avg_value(name + "_avg_double_window", g_get_double_avg, this) {}
W
wangguibao 已提交
114

W
wangguibao 已提交
115 116 117 118
  inline AvgDoubleWindowMetric& operator<<(int64_t value) {
    recorder << value;
    return *this;
  }
W
wangguibao 已提交
119

W
wangguibao 已提交
120 121 122
 public:
  ::bvar::PassiveStatus<double> avg_value;
  AvgWindowMetric recorder;
W
wangguibao 已提交
123 124 125
};

static double g_get_double_avg(void* arg) {
W
wangguibao 已提交
126 127
  AvgDoubleWindowMetric* avg_metric = static_cast<AvgDoubleWindowMetric*>(arg);
  return avg_metric->recorder.avg_window.get_value().get_average_double();
W
wangguibao 已提交
128 129 130
}

class PredictorMetric {
W
wangguibao 已提交
131 132
 public:
  static PredictorMetric* GetInstance();
W
wangguibao 已提交
133

W
wangguibao 已提交
134 135 136 137 138 139
  ~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 已提交
140
    }
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
    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 已提交
166

W
wangguibao 已提交
167 168 169 170 171 172 173 174 175 176
  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 已提交
177
    }
W
wangguibao 已提交
178
  }
W
wangguibao 已提交
179

W
wangguibao 已提交
180 181 182 183 184 185 186 187 188 189 190
  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 已提交
191
    }
W
wangguibao 已提交
192
  }
W
wangguibao 已提交
193

W
wangguibao 已提交
194 195 196 197 198 199 200 201 202 203
  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 已提交
204
    }
W
wangguibao 已提交
205
  }
W
wangguibao 已提交
206

W
wangguibao 已提交
207 208 209 210 211 212 213 214 215 216 217 218
  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 已提交
219
    }
W
wangguibao 已提交
220
  }
W
wangguibao 已提交
221

W
wangguibao 已提交
222 223 224 225 226 227 228 229 230
  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 已提交
231
    }
W
wangguibao 已提交
232 233 234 235 236 237 238 239 240
  }

  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 已提交
241
    }
W
wangguibao 已提交
242 243 244 245 246 247 248 249 250
  }

  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 已提交
251
    }
W
wangguibao 已提交
252
  }
W
wangguibao 已提交
253

W
wangguibao 已提交
254 255 256 257 258 259 260
  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 已提交
261
    }
W
wangguibao 已提交
262
  }
W
wangguibao 已提交
263

W
wangguibao 已提交
264 265 266 267 268 269 270
  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 已提交
271
    }
W
wangguibao 已提交
272 273 274 275 276 277 278 279
  }

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

  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 已提交
289
    }
W
wangguibao 已提交
290
  }
W
wangguibao 已提交
291

W
wangguibao 已提交
292 293 294 295 296 297 298 299
 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 已提交
300

W
wangguibao 已提交
301 302 303 304 305 306 307 308 309 310 311 312
 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 已提交
313

W
wangguibao 已提交
314 315 316
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu