88#include " schema.h"
99#include " directory.h"
1010#include " flow_counter_handler.h"
11+ #include " timer.h"
1112
1213#include < inttypes.h>
1314#include < sstream>
@@ -26,6 +27,8 @@ extern PortsOrch* gPortsOrch;
2627extern Directory<Orch*> gDirectory ;
2728extern bool gIsNatSupported ;
2829
30+ #define FLEX_COUNTER_UPD_INTERVAL 1
31+
2932static map<string, sai_meter_type_t > policer_meter_map = {
3033 {" packets" , SAI_METER_TYPE_PACKETS},
3134 {" bytes" , SAI_METER_TYPE_BYTES}
@@ -124,11 +127,18 @@ CoppOrch::CoppOrch(DBConnector* db, string tableName) :
124127 Orch(db, tableName),
125128 m_counter_db(std::shared_ptr<DBConnector>(new DBConnector(" COUNTERS_DB" , 0 ))),
126129 m_flex_db(std::shared_ptr<DBConnector>(new DBConnector(" FLEX_COUNTER_DB" , 0 ))),
130+ m_asic_db(std::shared_ptr<DBConnector>(new DBConnector(" ASIC_DB" , 0 ))),
127131 m_counter_table(std::unique_ptr<Table>(new Table(m_counter_db.get(), COUNTERS_TRAP_NAME_MAP))),
132+ m_vidToRidTable(std::unique_ptr<Table>(new Table(m_asic_db.get(), "VIDTORID"))),
128133 m_flex_counter_group_table(std::unique_ptr<ProducerTable>(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_GROUP_TABLE))),
129134 m_trap_counter_manager(HOSTIF_TRAP_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, HOSTIF_TRAP_COUNTER_POLLING_INTERVAL_MS, false )
130135{
131136 SWSS_LOG_ENTER ();
137+ auto intervT = timespec { .tv_sec = FLEX_COUNTER_UPD_INTERVAL , .tv_nsec = 0 };
138+ m_FlexCounterUpdTimer = new SelectableTimer (intervT);
139+ auto executorT = new ExecutableTimer (m_FlexCounterUpdTimer, this , " FLEX_COUNTER_UPD_TIMER" );
140+ Orch::addExecutor (executorT);
141+
132142 initDefaultHostIntfTable ();
133143 initDefaultTrapGroup ();
134144 initDefaultTrapIds ();
@@ -734,6 +744,36 @@ void CoppOrch::doTask(Consumer &consumer)
734744 }
735745}
736746
747+ void CoppOrch::doTask (SelectableTimer &timer)
748+ {
749+ SWSS_LOG_ENTER ();
750+ SWSS_LOG_DEBUG (" Registering %" PRId64 " new trap counters" , m_pendingAddToFlexCntr.size ());
751+
752+ string value;
753+ for (auto it = m_pendingAddToFlexCntr.begin (); it != m_pendingAddToFlexCntr.end (); )
754+ {
755+ const auto id = sai_serialize_object_id (it->first );
756+ if (m_vidToRidTable->hget (" " , id, value))
757+ {
758+ SWSS_LOG_INFO (" Registering %s, id %s" , it->second .c_str (), id.c_str ());
759+
760+ std::unordered_set<std::string> counter_stats;
761+ FlowCounterHandler::getGenericCounterIdList (counter_stats);
762+ m_trap_counter_manager.setCounterIdList (it->first , CounterType::HOSTIF_TRAP, counter_stats);
763+ it = m_pendingAddToFlexCntr.erase (it);
764+ }
765+ else
766+ {
767+ ++it;
768+ }
769+ }
770+
771+ if (m_pendingAddToFlexCntr.empty ())
772+ {
773+ m_FlexCounterUpdTimer->stop ();
774+ }
775+ }
776+
737777void CoppOrch::getTrapAddandRemoveList (string trap_group_name,
738778 vector<sai_hostif_trap_type_t > &trap_ids,
739779 vector<sai_hostif_trap_type_t > &add_trap_ids,
@@ -1183,15 +1223,18 @@ bool CoppOrch::bindTrapCounter(sai_object_id_t hostif_trap_id, sai_hostif_trap_t
11831223
11841224 // Update COUNTERS_TRAP_NAME_MAP
11851225 auto trap_name = get_trap_name_by_type (trap_type);
1186- FieldValueTuple tuple (trap_name, sai_serialize_object_id (counter_id));
1187- vector<FieldValueTuple> fields;
1188- fields.push_back (tuple);
1189- m_counter_table->set (" " , fields);
1190-
1191- // Update FLEX_COUNTER table
1192- std::unordered_set<std::string> counter_stats;
1193- FlowCounterHandler::getGenericCounterStatIdList (counter_stats);
1194- m_trap_counter_manager.setCounterIdList (counter_id, CounterType::HOSTIF_TRAP, counter_stats);
1226+ vector<FieldValueTuple> nameMapFvs;
1227+ nameMapFvs.emplace_back (trap_name, sai_serialize_object_id (counter_id));
1228+ m_counter_table->set (" " , nameMapFvs);
1229+
1230+ auto was_empty = m_pendingAddToFlexCntr.empty ();
1231+ m_pendingAddToFlexCntr[counter_id] = trap_name;
1232+
1233+ if (was_empty)
1234+ {
1235+ m_FlexCounterUpdTimer->start ();
1236+ }
1237+
11951238 m_trap_obj_name_map.emplace (hostif_trap_id, trap_name);
11961239 return true ;
11971240}
@@ -1210,7 +1253,15 @@ void CoppOrch::unbindTrapCounter(sai_object_id_t hostif_trap_id)
12101253 // Clear FLEX_COUNTER table
12111254 sai_object_id_t counter_id;
12121255 sai_deserialize_object_id (counter_oid_str, counter_id);
1213- m_trap_counter_manager.clearCounterIdList (counter_id);
1256+ auto update_iter = m_pendingAddToFlexCntr.find (counter_id);
1257+ if (update_iter == m_pendingAddToFlexCntr.end ())
1258+ {
1259+ m_trap_counter_manager.clearCounterIdList (counter_id);
1260+ }
1261+ else
1262+ {
1263+ m_pendingAddToFlexCntr.erase (update_iter);
1264+ }
12141265
12151266 // Remove trap from COUNTERS_TRAP_NAME_MAP
12161267 m_counter_table->hdel (" " , iter->second );
0 commit comments