metrics.cpp 4.3 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021/4/19.
//

#include "common/metrics/metrics.h"
#include "common/lang/mutex.h"

namespace common {
19 20
Meter::Meter()
{
羽飞's avatar
羽飞 已提交
21 22 23 24 25 26 27
  struct timeval start_time;
  gettimeofday(&start_time, NULL);

  snapshot_tick_ = start_time.tv_sec * 1000000 + start_time.tv_usec;
  value_.store(0l);
}

28 29
Meter::~Meter()
{
羽飞's avatar
羽飞 已提交
30 31 32 33 34 35
  if (snapshot_value_ != NULL) {
    delete snapshot_value_;
    snapshot_value_ = NULL;
  }
}

36 37 38 39 40 41 42 43 44
void Meter::inc(long increase)
{
  value_.fetch_add(increase);
}

void Meter::inc()
{
  inc(1l);
}
羽飞's avatar
羽飞 已提交
45

46 47
void Meter::snapshot()
{
羽飞's avatar
羽飞 已提交
48 49 50 51 52 53 54
  // lock here

  struct timeval now;
  gettimeofday(&now, NULL);

  long now_tick = now.tv_sec * 1000000 + now.tv_usec;

55
  double temp_value = ((double)value_.exchange(0l)) / ((now_tick - snapshot_tick_) / 1000000);
羽飞's avatar
羽飞 已提交
56 57 58 59 60 61 62 63
  snapshot_tick_ = now_tick;

  if (snapshot_value_ == NULL) {
    snapshot_value_ = new SnapshotBasic<double>();
  }
  ((SnapshotBasic<double> *)snapshot_value_)->setValue(temp_value);
}

64 65
SimpleTimer::~SimpleTimer()
{
羽飞's avatar
羽飞 已提交
66 67 68 69 70 71
  if (snapshot_value_ != NULL) {
    delete snapshot_value_;
    snapshot_value_ = NULL;
  }
}

72 73
void SimpleTimer::inc(long increase)
{
羽飞's avatar
羽飞 已提交
74 75 76 77
  value_.fetch_add(increase);
  times_.fetch_add(1);
}

78 79 80 81
void SimpleTimer::update(long one)
{
  inc(one);
}
羽飞's avatar
羽飞 已提交
82

83 84
void SimpleTimer::snapshot()
{
羽飞's avatar
羽飞 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98

  // lock here
  struct timeval now;
  gettimeofday(&now, NULL);

  long now_tick = now.tv_sec * 1000000 + now.tv_usec;

  long value_snapshot = value_.exchange(0l);
  long times_snapshot = times_.exchange(0l);

  double tps = 0;
  double mean = 0;

  if (times_snapshot > 0) {
99
    tps = ((double)times_snapshot) / ((now_tick - snapshot_tick_) / 1000000);
羽飞's avatar
羽飞 已提交
100 101 102 103 104 105 106 107 108 109 110
    mean = ((double)value_snapshot) / times_snapshot;
  }

  snapshot_tick_ = now_tick;

  if (snapshot_value_ == NULL) {
    snapshot_value_ = new SimplerTimerSnapshot();
  }
  ((SimplerTimerSnapshot *)snapshot_value_)->setValue(mean, tps);
}

111 112
Histogram::Histogram(RandomGenerator &random) : UniformReservoir(random)
{}
羽飞's avatar
羽飞 已提交
113

114 115
Histogram::Histogram(RandomGenerator &random, size_t size) : UniformReservoir(random, size)
{}
羽飞's avatar
羽飞 已提交
116

117 118
Histogram::~Histogram()
{}
羽飞's avatar
羽飞 已提交
119

120 121
void Histogram::snapshot()
{
羽飞's avatar
羽飞 已提交
122 123 124
  UniformReservoir::snapshot();
}

125 126
Timer::Timer(RandomGenerator &random) : UniformReservoir(random)
{
羽飞's avatar
羽飞 已提交
127 128 129 130 131 132 133
  struct timeval start_time;
  gettimeofday(&start_time, NULL);

  snapshot_tick_ = start_time.tv_sec * 1000000 + start_time.tv_usec;
  value_.store(0l);
}

134 135
Timer::Timer(RandomGenerator &random, size_t size) : UniformReservoir(random, size)
{
羽飞's avatar
羽飞 已提交
136 137 138 139 140 141 142
  struct timeval start_time;
  gettimeofday(&start_time, NULL);

  snapshot_tick_ = start_time.tv_sec * 1000000 + start_time.tv_usec;
  value_.store(0l);
}

143 144
Timer::~Timer()
{
羽飞's avatar
羽飞 已提交
145 146 147 148 149 150
  if (snapshot_value_ == NULL) {
    delete snapshot_value_;
    snapshot_value_ = NULL;
  }
}

151 152
void Timer::update(double ms)
{
羽飞's avatar
羽飞 已提交
153 154 155 156
  UniformReservoir::update(ms);
  value_.fetch_add(1l);
}

157 158
void Timer::snapshot()
{
羽飞's avatar
羽飞 已提交
159 160 161 162 163 164 165 166 167 168
  if (snapshot_value_ == NULL) {
    snapshot_value_ = new TimerSnapshot();
  }
  TimerSnapshot *timer_snapshot = (TimerSnapshot *)snapshot_value_;

  struct timeval now;
  gettimeofday(&now, NULL);

  long now_tick = now.tv_sec * 1000000 + now.tv_usec;

169
  double tps = ((double)value_.exchange(0l)) / ((now_tick - snapshot_tick_) / 1000000);
羽飞's avatar
羽飞 已提交
170 171 172 173 174 175 176 177 178 179
  snapshot_tick_ = now_tick;

  MUTEX_LOCK(&mutex);
  std::vector<double> output = data;
  MUTEX_UNLOCK(&mutex);

  timer_snapshot->set_collection(output);
  timer_snapshot->set_tps(tps);
}

180 181
TimerStat::TimerStat(SimpleTimer &other_st) : st_(other_st), start_tick_(0), end_tick_(0)
{
羽飞's avatar
羽飞 已提交
182 183 184 185

  start();
}

186 187
TimerStat::~TimerStat()
{
羽飞's avatar
羽飞 已提交
188 189 190 191 192 193 194
  if (end_tick_ == 0) {
    end();
  }

  st_.update((end_tick_ - start_tick_) / 1000);
}

195 196
void TimerStat::start()
{
羽飞's avatar
羽飞 已提交
197 198 199 200 201 202
  struct timeval now;
  gettimeofday(&now, NULL);

  start_tick_ = now.tv_sec * 1000000 + now.tv_usec;
}

203 204
void TimerStat::end()
{
羽飞's avatar
羽飞 已提交
205 206 207 208 209 210
  struct timeval now;
  gettimeofday(&now, NULL);

  end_tick_ = now.tv_sec * 1000000 + now.tv_usec;
}

211
}  // namespace common