Skip to content

Commit c7650a8

Browse files
daalllguohan
authored andcommitted
[portsorch] Refactor portsorch to use FlexCounterManager to setup port and queue stats (#1170)
- Updates portsorch to use FlexCounterManager for port and queue stats instead of direct redis operations - Updates FlexCounterManager to allow clients to enable the group in the constructor, for convenience - Updates the makefile for cfgmgr to include new flex_counter dependency from portsorch Signed-off-by: Danny Allen <[email protected]>
1 parent bc2450f commit c7650a8

8 files changed

Lines changed: 47 additions & 67 deletions

File tree

cfgmgr/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/orchagent -I $(top_srcdir)/warmrestart
1+
INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/orchagent -I $(top_srcdir)/warmrestart -I $(top_srcdir)/orchagent/flex_counter
22
CFLAGS_SAI = -I /usr/include/sai
33
LIBNL_CFLAGS = -I/usr/include/libnl3
44
LIBNL_LIBS = -lnl-genl-3 -lnl-route-3 -lnl-3

orchagent/debugcounterorch.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static const unordered_map<string, CounterType> flex_counter_type_lookup = {
2525
// object should only be initialized once.
2626
DebugCounterOrch::DebugCounterOrch(DBConnector *db, const vector<string>& table_names, int poll_interval) :
2727
Orch(db, table_names),
28-
flex_counter_manager(DEBUG_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, poll_interval),
28+
flex_counter_manager(DEBUG_COUNTER_FLEX_COUNTER_GROUP, StatsMode::READ, poll_interval, true),
2929
m_stateDb(new DBConnector("STATE_DB", 0)),
3030
m_debugCapabilitiesTable(new Table(m_stateDb.get(), STATE_DEBUG_COUNTER_CAPABILITIES_NAME)),
3131
m_countersDb(new DBConnector("COUNTERS_DB", 0)),
@@ -34,7 +34,6 @@ DebugCounterOrch::DebugCounterOrch(DBConnector *db, const vector<string>& table_
3434
{
3535
SWSS_LOG_ENTER();
3636
publishDropCounterCapabilities();
37-
flex_counter_manager.enableFlexCounterGroup();
3837
}
3938

4039
DebugCounterOrch::~DebugCounterOrch(void)

orchagent/flex_counter/flex_counter_manager.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,19 @@ const unordered_map<CounterType, string> FlexCounterManager::counter_id_field_lo
3434
{
3535
{ CounterType::PORT_DEBUG, PORT_DEBUG_COUNTER_ID_LIST },
3636
{ CounterType::SWITCH_DEBUG, SWITCH_DEBUG_COUNTER_ID_LIST },
37+
{ CounterType::PORT, PORT_COUNTER_ID_LIST },
38+
{ CounterType::QUEUE, QUEUE_COUNTER_ID_LIST }
3739
};
3840

39-
// This constructor will create a group that is disabled by default.
4041
FlexCounterManager::FlexCounterManager(
4142
const string& group_name,
4243
const StatsMode stats_mode,
43-
const uint polling_interval) :
44+
const uint polling_interval,
45+
const bool enabled) :
4446
group_name(group_name),
4547
stats_mode(stats_mode),
4648
polling_interval(polling_interval),
47-
enabled(false),
49+
enabled(enabled),
4850
flex_counter_db(new DBConnector("FLEX_COUNTER_DB", 0)),
4951
flex_counter_group_table(new ProducerTable(flex_counter_db.get(), FLEX_COUNTER_GROUP_TABLE)),
5052
flex_counter_table(new ProducerTable(flex_counter_db.get(), FLEX_COUNTER_TABLE))
@@ -72,6 +74,8 @@ FlexCounterManager::~FlexCounterManager()
7274

7375
void FlexCounterManager::applyGroupConfiguration()
7476
{
77+
SWSS_LOG_ENTER();
78+
7579
vector<FieldValueTuple> field_values =
7680
{
7781
FieldValueTuple(STATS_MODE_FIELD, stats_mode_lookup.at(stats_mode)),

orchagent/flex_counter/flex_counter_manager.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ enum class StatsMode
1818

1919
enum class CounterType
2020
{
21+
PORT,
22+
QUEUE,
2123
PORT_DEBUG,
2224
SWITCH_DEBUG
2325
};
@@ -33,7 +35,8 @@ class FlexCounterManager
3335
FlexCounterManager(
3436
const std::string& group_name,
3537
const StatsMode stats_mode,
36-
const uint polling_interval);
38+
const uint polling_interval,
39+
const bool enabled);
3740

3841
FlexCounterManager(const FlexCounterManager&) = delete;
3942
FlexCounterManager& operator=(const FlexCounterManager&) = delete;

orchagent/flex_counter/flex_counter_stat_manager.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ using swss::FieldValueTuple;
1313
FlexCounterStatManager::FlexCounterStatManager(
1414
const string& group_name,
1515
const StatsMode stats_mode,
16-
const int polling_interval) :
17-
FlexCounterManager(group_name, stats_mode, polling_interval)
16+
const int polling_interval,
17+
const bool enabled) :
18+
FlexCounterManager(group_name, stats_mode, polling_interval, enabled)
1819
{
1920
SWSS_LOG_ENTER();
2021
}

orchagent/flex_counter/flex_counter_stat_manager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class FlexCounterStatManager : public FlexCounterManager
1111
FlexCounterStatManager(
1212
const std::string& group_name,
1313
const StatsMode stats_mode,
14-
const int polling_interval);
14+
const int polling_interval,
15+
const bool enabled);
1516

1617
FlexCounterStatManager(const FlexCounterStatManager&) = delete;
1718
FlexCounterStatManager& operator=(const FlexCounterStatManager&) = delete;

orchagent/portsorch.cpp

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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

5052
static 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
*/
153155
PortsOrch::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-
14741459
string 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);

orchagent/portsorch.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "observer.h"
1010
#include "macaddress.h"
1111
#include "producertable.h"
12+
#include "flex_counter_manager.h"
1213

1314
#define FCS_LEN 4
1415
#define VLAN_TAG_LEN 4
@@ -104,14 +105,15 @@ class PortsOrch : public Orch, public Subject
104105
unique_ptr<ProducerTable> m_flexCounterTable;
105106
unique_ptr<ProducerTable> m_flexCounterGroupTable;
106107

107-
std::string getQueueFlexCounterTableKey(std::string s);
108108
std::string getQueueWatermarkFlexCounterTableKey(std::string s);
109-
std::string getPortFlexCounterTableKey(std::string s);
110109
std::string getPriorityGroupWatermarkFlexCounterTableKey(std::string s);
111110

112111
shared_ptr<DBConnector> m_counter_db;
113112
shared_ptr<DBConnector> m_flex_db;
114113

114+
FlexCounterManager port_stat_manager;
115+
FlexCounterManager queue_stat_manager;
116+
115117
std::map<sai_object_id_t, PortSupportedSpeeds> m_portSupportedSpeeds;
116118

117119
bool m_initDone = false;

0 commit comments

Comments
 (0)