1111#include < algorithm>
1212#include < tuple>
1313#include < sstream>
14+ #include < unordered_set>
1415
1516#include < netinet/if_ether.h>
1617#include " net/if.h"
@@ -41,10 +42,11 @@ extern BufferOrch *gBufferOrch;
4142#define VLAN_PREFIX " Vlan"
4243#define DEFAULT_VLAN_ID 1
4344#define MAX_VALID_VLAN_ID 4094
44- #define PORT_FLEX_STAT_COUNTER_POLL_MSECS " 1000"
45- #define QUEUE_FLEX_STAT_COUNTER_POLL_MSECS " 10000"
45+
46+ #define PORT_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS 1000
47+ #define QUEUE_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS 10000
4648#define QUEUE_WATERMARK_FLEX_STAT_COUNTER_POLL_MSECS " 10000"
47- #define PG_WATERMARK_FLEX_STAT_COUNTER_POLL_MSECS " 10000"
49+ #define PG_WATERMARK_FLEX_STAT_COUNTER_POLL_MSECS " 10000"
4850
4951
5052static map<string, sai_port_fec_mode_t > fec_mode_map =
@@ -70,7 +72,7 @@ static map<string, sai_bridge_port_fdb_learning_mode_t> learn_mode_map =
7072 { " notification" , SAI_BRIDGE_PORT_FDB_LEARNING_MODE_FDB_NOTIFICATION}
7173};
7274
73- const vector<sai_port_stat_t > portStatIds =
75+ const vector<sai_port_stat_t > port_stat_ids =
7476{
7577 SAI_PORT_STAT_IF_IN_OCTETS,
7678 SAI_PORT_STAT_IF_IN_UCAST_PKTS,
@@ -113,7 +115,7 @@ const vector<sai_port_stat_t> portStatIds =
113115 SAI_PORT_STAT_ETHER_IN_PKTS_128_TO_255_OCTETS,
114116};
115117
116- static const vector<sai_queue_stat_t > queueStatIds =
118+ static const vector<sai_queue_stat_t > queue_stat_ids =
117119{
118120 SAI_QUEUE_STAT_PACKETS,
119121 SAI_QUEUE_STAT_BYTES,
@@ -151,7 +153,9 @@ static char* hostif_vlan_tag[] = {
151153 * default VLAN and all ports removed from .1Q bridge.
152154 */
153155PortsOrch::PortsOrch (DBConnector *db, vector<table_name_with_pri_t > &tableNames) :
154- Orch(db, tableNames)
156+ Orch(db, tableNames),
157+ port_stat_manager(PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, PORT_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS, true ),
158+ queue_stat_manager(QUEUE_STAT_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, QUEUE_STAT_FLEX_COUNTER_POLLING_INTERVAL_MS, true )
155159{
156160 SWSS_LOG_ENTER ();
157161
@@ -177,15 +181,6 @@ PortsOrch::PortsOrch(DBConnector *db, vector<table_name_with_pri_t> &tableNames)
177181 m_flexCounterTable = unique_ptr<ProducerTable>(new ProducerTable (m_flex_db.get (), FLEX_COUNTER_TABLE));
178182 m_flexCounterGroupTable = unique_ptr<ProducerTable>(new ProducerTable (m_flex_db.get (), FLEX_COUNTER_GROUP_TABLE));
179183
180- vector<FieldValueTuple> fields;
181- fields.emplace_back (POLL_INTERVAL_FIELD, PORT_FLEX_STAT_COUNTER_POLL_MSECS);
182- fields.emplace_back (STATS_MODE_FIELD, STATS_MODE_READ);
183- m_flexCounterGroupTable->set (PORT_STAT_COUNTER_FLEX_COUNTER_GROUP, fields);
184-
185- fields.emplace_back (POLL_INTERVAL_FIELD, QUEUE_FLEX_STAT_COUNTER_POLL_MSECS);
186- fields.emplace_back (STATS_MODE_FIELD, STATS_MODE_READ);
187- m_flexCounterGroupTable->set (QUEUE_STAT_COUNTER_FLEX_COUNTER_GROUP, fields);
188-
189184 string queueWmSha, pgWmSha;
190185 string queueWmPluginName = " watermark_queue.lua" ;
191186 string pgWmPluginName = " watermark_pg.lua" ;
@@ -1461,16 +1456,6 @@ bool PortsOrch::removePort(sai_object_id_t port_id)
14611456 return true ;
14621457}
14631458
1464- string PortsOrch::getPortFlexCounterTableKey (string key)
1465- {
1466- return string (PORT_STAT_COUNTER_FLEX_COUNTER_GROUP) + " :" + key;
1467- }
1468-
1469- string PortsOrch::getQueueFlexCounterTableKey (string key)
1470- {
1471- return string (QUEUE_STAT_COUNTER_FLEX_COUNTER_GROUP) + " :" + key;
1472- }
1473-
14741459string PortsOrch::getQueueWatermarkFlexCounterTableKey (string key)
14751460{
14761461 return string (QUEUE_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP) + " :" + key;
@@ -1514,22 +1499,15 @@ bool PortsOrch::initPort(const string &alias, const set<int> &lane_set)
15141499 fields.push_back (tuple);
15151500 m_counterTable->set (" " , fields);
15161501
1517- /* Add port to flex_counter for updating stat counters */
1518- string key = getPortFlexCounterTableKey (sai_serialize_object_id (p.m_port_id ));
1519- std::string delimiter = " " ;
1520- std::ostringstream counters_stream;
1521- for (const auto &id: portStatIds)
1502+ // Install a flex counter for this port to track stats
1503+ std::unordered_set<std::string> counter_stats;
1504+ for (const auto & it: port_stat_ids)
15221505 {
1523- counters_stream << delimiter << sai_serialize_port_stat (id);
1524- delimiter = comma;
1506+ counter_stats.emplace (sai_serialize_port_stat (it));
15251507 }
1508+ port_stat_manager.setCounterIdList (p.m_port_id , CounterType::PORT, counter_stats);
15261509
1527- fields.clear ();
1528- fields.emplace_back (PORT_COUNTER_ID_LIST, counters_stream.str ());
1529-
1530- m_flexCounterTable->set (key, fields);
1531-
1532- PortUpdate update = {p, true };
1510+ PortUpdate update = { p, true };
15331511 notify (SUBJECT_TYPE_PORT_CHANGE, static_cast <void *>(&update));
15341512
15351513 SWSS_LOG_NOTICE (" Initialized port %s" , alias.c_str ());
@@ -3385,34 +3363,26 @@ void PortsOrch::generateQueueMapPerPort(const Port& port)
33853363 queueIndexVector.emplace_back (id, to_string (queueRealIndex));
33863364 }
33873365
3388- /* add ordinary Queue stat counters */
3389- string key = getQueueFlexCounterTableKey (id);
3390-
3391- std::string delimiter = " " ;
3392- std::ostringstream counters_stream;
3393- for (const auto & it: queueStatIds)
3366+ // Install a flex counter for this queue to track stats
3367+ std::unordered_set<string> counter_stats;
3368+ for (const auto & it: queue_stat_ids)
33943369 {
3395- counters_stream << delimiter << sai_serialize_queue_stat (it);
3396- delimiter = comma;
3370+ counter_stats.emplace (sai_serialize_queue_stat (it));
33973371 }
3398-
3399- vector<FieldValueTuple> fieldValues;
3400- fieldValues.emplace_back (QUEUE_COUNTER_ID_LIST, counters_stream.str ());
3401-
3402- m_flexCounterTable->set (key, fieldValues);
3372+ queue_stat_manager.setCounterIdList (port.m_queue_ids [queueIndex], CounterType::QUEUE, counter_stats);
34033373
34043374 /* add watermark queue counters */
3405- key = getQueueWatermarkFlexCounterTableKey (id);
3375+ string key = getQueueWatermarkFlexCounterTableKey (id);
34063376
3407- delimiter = " " ;
3408- counters_stream. str ( " " ) ;
3377+ string delimiter ( " " ) ;
3378+ std::ostringstream counters_stream;
34093379 for (const auto & it: queueWatermarkStatIds)
34103380 {
34113381 counters_stream << delimiter << sai_serialize_queue_stat (it);
34123382 delimiter = comma;
34133383 }
34143384
3415- fieldValues. clear () ;
3385+ vector<FieldValueTuple> fieldValues;
34163386 fieldValues.emplace_back (QUEUE_COUNTER_ID_LIST, counters_stream.str ());
34173387
34183388 m_flexCounterTable->set (key, fieldValues);
0 commit comments