Line data Source code
1 : //
2 : // Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3 : // Copyright Contributors to the OpenSDN project
4 : //
5 :
6 : #include "gendb_statistics.h"
7 :
8 : namespace GenDb {
9 :
10 : // DbTableStatistics::TableStats
11 220010 : void DbTableStatistics::TableStats::Update(bool write, bool fail,
12 : bool back_pressure, uint64_t num) {
13 220010 : if (write) {
14 40 : if (fail) {
15 8 : num_write_fails_ += num;
16 32 : } else if (back_pressure) {
17 0 : num_write_back_pressure_fails_ += num;
18 : } else {
19 32 : num_writes_ += num;
20 : }
21 : } else {
22 219970 : if (fail) {
23 66 : num_read_fails_ += num;
24 : } else {
25 219904 : num_reads_ += num;
26 : }
27 : }
28 220010 : }
29 :
30 179 : void DbTableStatistics::TableStats::Get(const std::string &table_name,
31 : DbTableInfo *info) const {
32 179 : info->set_table_name(table_name);
33 179 : info->set_reads(num_reads_);
34 179 : info->set_read_fails(num_read_fails_);
35 179 : info->set_writes(num_writes_);
36 179 : info->set_write_fails(num_write_fails_);
37 179 : info->set_write_back_pressure_fails(num_write_back_pressure_fails_);
38 179 : }
39 :
40 : // DbTableStatistics
41 110005 : void DbTableStatistics::Update(const std::string &table_name,
42 : bool write, bool fail, bool back_pressure, uint64_t num) {
43 110005 : TableStatsMap::iterator it = table_stats_map_.find(table_name);
44 110005 : if (it == table_stats_map_.end()) {
45 190 : it = (table_stats_map_.insert(table_name, new TableStats)).first;
46 : }
47 110005 : TableStats *table_stats = it->second;
48 110005 : table_stats->Update(write, fail, back_pressure, num);
49 : // Update cumulative table stats as well
50 110005 : it = table_stats_cumulative_map_.find(table_name);
51 110004 : if (it == table_stats_cumulative_map_.end()) {
52 169 : it = (table_stats_cumulative_map_.insert(table_name, new TableStats)).first;
53 : }
54 110004 : table_stats = it->second;
55 110005 : table_stats->Update(write, fail, back_pressure, num);
56 110005 : }
57 :
58 64 : void DbTableStatistics::GetInternal(std::vector<GenDb::DbTableInfo> *vdbti,
59 : bool cumulative) const {
60 64 : TableStatsMap stats_map;
61 64 : if (cumulative) {
62 11 : stats_map = table_stats_cumulative_map_ ;
63 : } else {
64 53 : stats_map = table_stats_map_;
65 : }
66 64 : for (TableStatsMap::const_iterator it = stats_map.begin();
67 243 : it != stats_map.end(); it++) {
68 179 : const TableStats *table_stats(it->second);
69 179 : GenDb::DbTableInfo dbti;
70 179 : table_stats->Get(it->first, &dbti);
71 179 : vdbti->push_back(dbti);
72 179 : }
73 64 : }
74 :
75 53 : void DbTableStatistics::GetDiffs(std::vector<GenDb::DbTableInfo> *vdbti) {
76 53 : GetInternal(vdbti, false);
77 53 : table_stats_map_.clear();
78 53 : }
79 :
80 11 : void DbTableStatistics::GetCumulative(std::vector<GenDb::DbTableInfo> *vdbti)
81 : const {
82 11 : GetInternal(vdbti, true);
83 11 : }
84 :
85 : // IfErrors
86 35 : void IfErrors::GetInternal(DbErrors *db_errors) const {
87 35 : db_errors->set_write_tablespace_fails(write_tablespace_fails_);
88 35 : db_errors->set_read_tablespace_fails(read_tablespace_fails_);
89 35 : db_errors->set_write_table_fails(write_column_family_fails_);
90 35 : db_errors->set_read_table_fails(read_column_family_fails_);
91 35 : db_errors->set_write_column_fails(write_column_fails_);
92 35 : db_errors->set_write_batch_column_fails(write_batch_column_fails_);
93 35 : db_errors->set_read_column_fails(read_column_fails_);
94 35 : }
95 :
96 28 : void IfErrors::Clear() {
97 28 : write_tablespace_fails_ = 0;
98 28 : read_tablespace_fails_ = 0;
99 28 : write_column_family_fails_ = 0;
100 28 : read_column_family_fails_ = 0;
101 28 : write_column_fails_ = 0;
102 28 : write_batch_column_fails_ = 0;
103 28 : read_column_fails_ = 0;
104 28 : }
105 :
106 28 : void IfErrors::GetDiffs(DbErrors *db_errors) {
107 28 : GetInternal(db_errors);
108 28 : Clear();
109 28 : }
110 :
111 7 : void IfErrors::GetCumulative(DbErrors *db_errors) const {
112 7 : GetInternal(db_errors);
113 7 : }
114 :
115 :
116 108 : void IfErrors::Increment(IfErrors::Type type) {
117 108 : switch (type) {
118 10 : case IfErrors::ERR_WRITE_TABLESPACE:
119 10 : write_tablespace_fails_++;
120 10 : break;
121 8 : case IfErrors::ERR_READ_TABLESPACE:
122 8 : read_tablespace_fails_++;
123 8 : break;
124 8 : case IfErrors::ERR_WRITE_COLUMN_FAMILY:
125 8 : write_column_family_fails_++;
126 8 : break;
127 58 : case IfErrors::ERR_READ_COLUMN_FAMILY:
128 58 : read_column_family_fails_++;
129 58 : break;
130 8 : case IfErrors::ERR_WRITE_COLUMN:
131 8 : write_column_fails_++;
132 8 : break;
133 8 : case IfErrors::ERR_WRITE_BATCH_COLUMN:
134 8 : write_batch_column_fails_++;
135 8 : break;
136 8 : case IfErrors::ERR_READ_COLUMN:
137 8 : read_column_fails_++;
138 8 : break;
139 0 : default:
140 0 : break;
141 : }
142 108 : }
143 :
144 : // GenDbIfStats
145 108391 : void GenDbIfStats::IncrementTableStatsInternal(const std::string &table_name,
146 : bool write, bool fail, bool back_pressure, uint64_t num) {
147 108391 : table_stats_.Update(table_name, write, fail, back_pressure, num);
148 108391 : }
149 :
150 0 : void GenDbIfStats::IncrementTableStats(GenDbIfStats::TableOp op,
151 : const std::string &table_name) {
152 0 : switch (op) {
153 0 : case GenDbIfStats::TABLE_OP_NONE:
154 0 : break;
155 0 : case GenDbIfStats::TABLE_OP_WRITE:
156 0 : IncrementTableStatsInternal(table_name, true, false, false, 1);
157 0 : break;
158 0 : case GenDbIfStats::TABLE_OP_WRITE_FAIL:
159 0 : IncrementTableStatsInternal(table_name, true, true, false, 1);
160 0 : break;
161 0 : case GenDbIfStats::TABLE_OP_WRITE_BACK_PRESSURE_FAIL:
162 0 : IncrementTableStatsInternal(table_name, true, true, true, 1);
163 0 : break;
164 0 : case GenDbIfStats::TABLE_OP_READ:
165 0 : IncrementTableStatsInternal(table_name, false, false, false, 1);
166 0 : break;
167 0 : case GenDbIfStats::TABLE_OP_READ_FAIL:
168 0 : IncrementTableStatsInternal(table_name, false, true, false, 1);
169 0 : break;
170 0 : default:
171 0 : break;
172 : }
173 0 : }
174 :
175 5 : void GenDbIfStats::IncrementTableWrite(const std::string &table_name) {
176 5 : IncrementTableStatsInternal(table_name, true, false, false, 1);
177 5 : }
178 :
179 0 : void GenDbIfStats::IncrementTableWrite(const std::string &table_name,
180 : uint64_t num_writes) {
181 0 : IncrementTableStatsInternal(table_name, true, false, false, num_writes);
182 0 : }
183 :
184 4 : void GenDbIfStats::IncrementTableWriteFail(const std::string &table_name) {
185 4 : IncrementTableStatsInternal(table_name, true, true, false, 1);
186 4 : }
187 :
188 0 : void GenDbIfStats::IncrementTableWriteFail(const std::string &table_name,
189 : uint64_t num_writes) {
190 0 : IncrementTableStatsInternal(table_name, true, true, false, num_writes);
191 0 : }
192 :
193 0 : void GenDbIfStats::IncrementTableWriteBackPressureFail(
194 : const std::string &table_name) {
195 0 : IncrementTableStatsInternal(table_name, true, false, true, 1);
196 0 : }
197 :
198 108346 : void GenDbIfStats::IncrementTableRead(const std::string &table_name) {
199 108346 : IncrementTableStatsInternal(table_name, false, false, false, 1);
200 108346 : }
201 :
202 7 : void GenDbIfStats::IncrementTableRead(const std::string &table_name,
203 : uint64_t num_reads) {
204 7 : IncrementTableStatsInternal(table_name, false, false, false, num_reads);
205 7 : }
206 :
207 29 : void GenDbIfStats::IncrementTableReadFail(const std::string &table_name) {
208 29 : IncrementTableStatsInternal(table_name, false, true, false, 1);
209 29 : }
210 :
211 0 : void GenDbIfStats::IncrementTableReadFail(const std::string &table_name,
212 : uint64_t num_reads) {
213 0 : IncrementTableStatsInternal(table_name, false, true, false, num_reads);
214 0 : }
215 :
216 0 : void GenDbIfStats::IncrementTableReadBackPressureFail(
217 : const std::string &table_name) {
218 0 : IncrementTableStatsInternal(table_name, false, false, true, 1);
219 0 : }
220 :
221 54 : void GenDbIfStats::IncrementErrors(IfErrors::Type etype) {
222 54 : errors_.Increment(etype);
223 54 : cumulative_errors_.Increment(etype);
224 54 : }
225 :
226 28 : void GenDbIfStats::GetDiffs(std::vector<DbTableInfo> *vdbti, DbErrors *dbe) {
227 : // Get diff cfstats
228 28 : table_stats_.GetDiffs(vdbti);
229 : // Get diff errors
230 28 : errors_.GetDiffs(dbe);
231 28 : }
232 :
233 7 : void GenDbIfStats::GetCumulative(std::vector<DbTableInfo> *vdbti,
234 : DbErrors *dbe) const {
235 : // Get cumulative cfstats
236 7 : table_stats_.GetCumulative(vdbti);
237 : // Get cumulative errors
238 7 : cumulative_errors_.GetCumulative(dbe);
239 7 : }
240 :
241 : } // namespace GenDb
|