LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/database - gendb_statistics.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 129 169 76.3 %
Date: 2026-08-03 02:19:58 Functions: 20 26 76.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14