forked from OpenAtomFoundation/pikiwidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpika_statistic.cc
111 lines (96 loc) · 3.16 KB
/
pika_statistic.cc
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
// Copyright (c) 2015-present, Qihoo, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
#include "include/pika_statistic.h"
#include "pstd/include/env.h"
#include "include/pika_command.h"
/* QpsStatistic */
QpsStatistic::QpsStatistic()
: querynum(0),
write_querynum(0),
last_querynum(0),
last_write_querynum(0),
last_sec_querynum(0),
last_sec_write_querynum(0),
last_time_us(0) {}
QpsStatistic::QpsStatistic(const QpsStatistic& other) {
querynum = other.querynum.load();
write_querynum = other.write_querynum.load();
last_querynum = other.last_querynum.load();
last_write_querynum = other.last_write_querynum.load();
last_sec_querynum = other.last_sec_querynum.load();
last_sec_write_querynum = other.last_sec_write_querynum.load();
last_time_us = other.last_time_us.load();
}
void QpsStatistic::IncreaseQueryNum(bool is_write) {
querynum++;
if (is_write) {
write_querynum++;
}
}
void QpsStatistic::ResetLastSecQuerynum() {
uint64_t last_query = last_querynum.load();
uint64_t last_write_query = last_write_querynum.load();
uint64_t cur_query = querynum.load();
uint64_t cur_write_query = write_querynum.load();
uint64_t last_time = last_time_us.load();
if (cur_write_query < last_write_query) {
cur_write_query = last_write_query;
}
if (cur_query < last_query) {
cur_query = last_query;
}
uint64_t delta_query = cur_query - last_query;
uint64_t delta_write_query = cur_write_query - last_write_query;
uint64_t cur_time_us = pstd::NowMicros();
if (cur_time_us <= last_time) {
cur_time_us = last_time + 1;
}
uint64_t delta_time_us = cur_time_us - last_time;
last_sec_querynum.store(delta_query * 1000000 / (delta_time_us));
last_sec_write_querynum.store(delta_write_query * 1000000 / (delta_time_us));
last_querynum.store(cur_query);
last_write_querynum.store(cur_write_query);
last_time_us.store(cur_time_us);
}
/* Statistic */
Statistic::Statistic() {
pthread_rwlockattr_t db_stat_rw_attr;
pthread_rwlockattr_init(&db_stat_rw_attr);
}
QpsStatistic Statistic::DBStat(const std::string& db_name) {
std::shared_lock l(db_stat_rw);
return db_stat[db_name];
}
std::unordered_map<std::string, QpsStatistic> Statistic::AllDBStat() {
std::shared_lock l(db_stat_rw);
return db_stat;
}
void Statistic::UpdateDBQps(const std::string& db_name, const std::string& command, bool is_write) {
bool db_exist = true;
std::unordered_map<std::string, QpsStatistic>::iterator iter;
{
std::shared_lock l(db_stat_rw);
auto search = db_stat.find(db_name);
if (search == db_stat.end()) {
db_exist = false;
} else {
iter = search;
}
}
if (db_exist) {
iter->second.IncreaseQueryNum(is_write);
} else {
{
std::lock_guard l(db_stat_rw);
db_stat[db_name].IncreaseQueryNum(is_write);
}
}
}
void Statistic::ResetDBLastSecQuerynum() {
std::shared_lock l(db_stat_rw);
for (auto& stat : db_stat) {
stat.second.ResetLastSecQuerynum();
}
}