@@ -132,6 +132,20 @@ void FlexCounter::updateFlexCounterStatsMode(
132
132
}
133
133
}
134
134
135
+ void FlexCounter::addCollectCountersHandler (const std::string &key, const collect_counters_handler_t &handler)
136
+ {
137
+ SWSS_LOG_ENTER ();
138
+
139
+ m_collectCountersHandlers.emplace (key, handler);
140
+ }
141
+
142
+ void FlexCounter::removeCollectCountersHandler (const std::string &key)
143
+ {
144
+ SWSS_LOG_ENTER ();
145
+
146
+ m_collectCountersHandlers.erase (key);
147
+ }
148
+
135
149
/* The current implementation of 'setPortCounterList' and 'setQueueCounterList' are
136
150
* not the same. Need to refactor these two functions to have the similar logic.
137
151
* Either the full SAI attributes are queried once, or each of the needed counters
@@ -188,6 +202,8 @@ void FlexCounter::setPortCounterList(
188
202
auto portCounterIds = std::make_shared<PortCounterIds>(portId, supportedIds);
189
203
fc.m_portCounterIdsMap .emplace (portVid, portCounterIds);
190
204
205
+ fc.addCollectCountersHandler (PORT_COUNTER_ID_LIST, &FlexCounter::collectPortCounters);
206
+
191
207
// Start flex counter thread in case it was not running due to empty counter IDs map
192
208
if (fc.m_pollInterval > 0 )
193
209
{
@@ -263,6 +279,8 @@ void FlexCounter::setQueueCounterList(
263
279
auto queueCounterIds = std::make_shared<QueueCounterIds>(queueId, supportedIds);
264
280
fc.m_queueCounterIdsMap .emplace (queueVid, queueCounterIds);
265
281
282
+ fc.addCollectCountersHandler (QUEUE_COUNTER_ID_LIST, &FlexCounter::collectQueueCounters);
283
+
266
284
// Start flex counter thread in case it was not running due to empty counter IDs map
267
285
if (fc.m_pollInterval > 0 )
268
286
{
@@ -292,6 +310,8 @@ void FlexCounter::setQueueAttrList(
292
310
auto queueAttrIds = std::make_shared<QueueAttrIds>(queueId, attrIds);
293
311
fc.m_queueAttrIdsMap .emplace (queueVid, queueAttrIds);
294
312
313
+ fc.addCollectCountersHandler (QUEUE_ATTR_ID_LIST, &FlexCounter::collectQueueAttrs);
314
+
295
315
// Start flex counter thread in case it was not running due to empty counter IDs map
296
316
if (fc.m_pollInterval > 0 )
297
317
{
@@ -367,6 +387,8 @@ void FlexCounter::setPriorityGroupCounterList(
367
387
auto priorityGroupCounterIds = std::make_shared<IngressPriorityGroupCounterIds>(priorityGroupId, supportedIds);
368
388
fc.m_priorityGroupCounterIdsMap .emplace (priorityGroupVid, priorityGroupCounterIds);
369
389
390
+ fc.addCollectCountersHandler (PG_COUNTER_ID_LIST, &FlexCounter::collectPriorityGroupCounters);
391
+
370
392
// Start flex counter thread in case it was not running due to empty counter IDs map
371
393
if (fc.m_pollInterval > 0 )
372
394
{
@@ -396,6 +418,8 @@ void FlexCounter::setPriorityGroupAttrList(
396
418
auto priorityGroupAttrIds = std::make_shared<IngressPriorityGroupAttrIds>(priorityGroupId, attrIds);
397
419
fc.m_priorityGroupAttrIdsMap .emplace (priorityGroupVid, priorityGroupAttrIds);
398
420
421
+ fc.addCollectCountersHandler (PG_ATTR_ID_LIST, &FlexCounter::collectPriorityGroupAttrs);
422
+
399
423
// Start flex counter thread in case it was not running due to empty counter IDs map
400
424
if (fc.m_pollInterval > 0 )
401
425
{
@@ -454,6 +478,8 @@ void FlexCounter::setRifCounterList(
454
478
auto rifCounterIds = std::make_shared<RifCounterIds>(rifId, supportedIds);
455
479
fc.m_rifCounterIdsMap .emplace (rifVid, rifCounterIds);
456
480
481
+ fc.addCollectCountersHandler (RIF_COUNTER_ID_LIST, &FlexCounter::collectRifCounters);
482
+
457
483
// Start flex counter thread in case it was not running due to empty counter IDs map
458
484
if (fc.m_pollInterval > 0 )
459
485
{
@@ -523,6 +549,8 @@ void FlexCounter::setBufferPoolCounterList(
523
549
auto bufferPoolCounterIds = std::make_shared<BufferPoolCounterIds>(bufferPoolId, supportedIds, bufferPoolStatsMode);
524
550
fc.m_bufferPoolCounterIdsMap .emplace (bufferPoolVid, bufferPoolCounterIds);
525
551
552
+ fc.addCollectCountersHandler (BUFFER_POOL_COUNTER_ID_LIST, &FlexCounter::collectBufferPoolCounters);
553
+
526
554
// Start flex counter thread in case it was not running due to empty counter IDs map
527
555
if (fc.m_pollInterval > 0 )
528
556
{
@@ -544,6 +572,7 @@ void FlexCounter::removePort(
544
572
if (it == fc.m_portCounterIdsMap .end ())
545
573
{
546
574
SWSS_LOG_NOTICE (" Trying to remove nonexisting port counter Ids 0x%lx" , portVid);
575
+
547
576
// Remove flex counter if all counter IDs and plugins are unregistered
548
577
if (fc.isEmpty ())
549
578
{
@@ -554,6 +583,10 @@ void FlexCounter::removePort(
554
583
}
555
584
556
585
fc.m_portCounterIdsMap .erase (it);
586
+ if (fc.m_portCounterIdsMap .empty ())
587
+ {
588
+ fc.removeCollectCountersHandler (PORT_COUNTER_ID_LIST);
589
+ }
557
590
558
591
// Remove flex counter if all counter IDs and plugins are unregistered
559
592
if (fc.isEmpty ())
@@ -578,13 +611,21 @@ void FlexCounter::removeQueue(
578
611
if (counterIter != fc.m_queueCounterIdsMap .end ())
579
612
{
580
613
fc.m_queueCounterIdsMap .erase (counterIter);
614
+ if (fc.m_queueCounterIdsMap .empty ())
615
+ {
616
+ fc.removeCollectCountersHandler (QUEUE_COUNTER_ID_LIST);
617
+ }
581
618
found = true ;
582
619
}
583
620
584
621
auto attrIter = fc.m_queueAttrIdsMap .find (queueVid);
585
622
if (attrIter != fc.m_queueAttrIdsMap .end ())
586
623
{
587
624
fc.m_queueAttrIdsMap .erase (attrIter);
625
+ if (fc.m_queueAttrIdsMap .empty ())
626
+ {
627
+ fc.removeCollectCountersHandler (QUEUE_ATTR_ID_LIST);
628
+ }
588
629
found = true ;
589
630
}
590
631
@@ -617,13 +658,21 @@ void FlexCounter::removePriorityGroup(
617
658
if (counterIter != fc.m_priorityGroupCounterIdsMap .end ())
618
659
{
619
660
fc.m_priorityGroupCounterIdsMap .erase (counterIter);
661
+ if (fc.m_priorityGroupCounterIdsMap .empty ())
662
+ {
663
+ fc.removeCollectCountersHandler (PG_COUNTER_ID_LIST);
664
+ }
620
665
found = true ;
621
666
}
622
667
623
668
auto attrIter = fc.m_priorityGroupAttrIdsMap .find (priorityGroupVid);
624
669
if (attrIter != fc.m_priorityGroupAttrIdsMap .end ())
625
670
{
626
671
fc.m_priorityGroupAttrIdsMap .erase (attrIter);
672
+ if (fc.m_priorityGroupAttrIdsMap .empty ())
673
+ {
674
+ fc.removeCollectCountersHandler (PG_ATTR_ID_LIST);
675
+ }
627
676
found = true ;
628
677
}
629
678
@@ -655,6 +704,7 @@ void FlexCounter::removeRif(
655
704
if (it == fc.m_rifCounterIdsMap .end ())
656
705
{
657
706
SWSS_LOG_NOTICE (" Trying to remove nonexisting router interface counter from Id 0x%lx" , rifVid);
707
+
658
708
// Remove flex counter if all counter IDs and plugins are unregistered
659
709
if (fc.isEmpty ())
660
710
{
@@ -665,6 +715,10 @@ void FlexCounter::removeRif(
665
715
}
666
716
667
717
fc.m_rifCounterIdsMap .erase (it);
718
+ if (fc.m_rifCounterIdsMap .empty ())
719
+ {
720
+ fc.removeCollectCountersHandler (RIF_COUNTER_ID_LIST);
721
+ }
668
722
669
723
// Remove flex counter if all counter IDs and plugins are unregistered
670
724
if (fc.isEmpty ())
@@ -689,6 +743,10 @@ void FlexCounter::removeBufferPool(
689
743
if (it != fc.m_bufferPoolCounterIdsMap .end ())
690
744
{
691
745
fc.m_bufferPoolCounterIdsMap .erase (it);
746
+ if (fc.m_bufferPoolCounterIdsMap .empty ())
747
+ {
748
+ fc.removeCollectCountersHandler (BUFFER_POOL_COUNTER_ID_LIST);
749
+ }
692
750
found = true ;
693
751
}
694
752
@@ -943,6 +1001,18 @@ void FlexCounter::collectCounters(
943
1001
{
944
1002
SWSS_LOG_ENTER ();
945
1003
1004
+ for (const auto &it : m_collectCountersHandlers)
1005
+ {
1006
+ (this ->*(it.second ))(countersTable);
1007
+ }
1008
+
1009
+ countersTable.flush ();
1010
+ }
1011
+
1012
+ void FlexCounter::collectPortCounters (_In_ swss::Table &countersTable)
1013
+ {
1014
+ SWSS_LOG_ENTER ();
1015
+
946
1016
// Collect stats for every registered port
947
1017
for (const auto &kv: m_portCounterIdsMap)
948
1018
{
@@ -978,6 +1048,11 @@ void FlexCounter::collectCounters(
978
1048
979
1049
countersTable.set (portVidStr, values, " " );
980
1050
}
1051
+ }
1052
+
1053
+ void FlexCounter::collectQueueCounters (_In_ swss::Table &countersTable)
1054
+ {
1055
+ SWSS_LOG_ENTER ();
981
1056
982
1057
// Collect stats for every registered queue
983
1058
for (const auto &kv: m_queueCounterIdsMap)
@@ -1034,6 +1109,11 @@ void FlexCounter::collectCounters(
1034
1109
1035
1110
countersTable.set (queueVidStr, values, " " );
1036
1111
}
1112
+ }
1113
+
1114
+ void FlexCounter::collectQueueAttrs (_In_ swss::Table &countersTable)
1115
+ {
1116
+ SWSS_LOG_ENTER ();
1037
1117
1038
1118
// Collect attrs for every registered queue
1039
1119
for (const auto &kv: m_queueAttrIdsMap)
@@ -1076,6 +1156,11 @@ void FlexCounter::collectCounters(
1076
1156
1077
1157
countersTable.set (queueVidStr, values, " " );
1078
1158
}
1159
+ }
1160
+
1161
+ void FlexCounter::collectPriorityGroupCounters (_In_ swss::Table &countersTable)
1162
+ {
1163
+ SWSS_LOG_ENTER ();
1079
1164
1080
1165
// Collect stats for every registered ingress priority group
1081
1166
for (const auto &kv: m_priorityGroupCounterIdsMap)
@@ -1125,6 +1210,11 @@ void FlexCounter::collectCounters(
1125
1210
1126
1211
countersTable.set (priorityGroupVidStr, values, " " );
1127
1212
}
1213
+ }
1214
+
1215
+ void FlexCounter::collectPriorityGroupAttrs (_In_ swss::Table &countersTable)
1216
+ {
1217
+ SWSS_LOG_ENTER ();
1128
1218
1129
1219
// Collect attrs for every registered priority group
1130
1220
for (const auto &kv: m_priorityGroupAttrIdsMap)
@@ -1167,6 +1257,11 @@ void FlexCounter::collectCounters(
1167
1257
1168
1258
countersTable.set (priorityGroupVidStr, values, " " );
1169
1259
}
1260
+ }
1261
+
1262
+ void FlexCounter::collectRifCounters (_In_ swss::Table &countersTable)
1263
+ {
1264
+ SWSS_LOG_ENTER ();
1170
1265
1171
1266
// Collect stats for every registered router interface
1172
1267
for (const auto &kv: m_rifCounterIdsMap)
@@ -1203,6 +1298,11 @@ void FlexCounter::collectCounters(
1203
1298
1204
1299
countersTable.set (rifVidStr, values, " " );
1205
1300
}
1301
+ }
1302
+
1303
+ void FlexCounter::collectBufferPoolCounters (_In_ swss::Table &countersTable)
1304
+ {
1305
+ SWSS_LOG_ENTER ();
1206
1306
1207
1307
// Collect stats for every registered buffer pool
1208
1308
for (const auto &it : m_bufferPoolCounterIdsMap)
@@ -1259,8 +1359,6 @@ void FlexCounter::collectCounters(
1259
1359
1260
1360
countersTable.set (sai_serialize_object_id (bufferPoolVid), fvTuples);
1261
1361
}
1262
-
1263
- countersTable.flush ();
1264
1362
}
1265
1363
1266
1364
void FlexCounter::runPlugins (
0 commit comments