forked from Azure/sonic-sairedis.msft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlexCounter.cpp
More file actions
2878 lines (2533 loc) · 96.9 KB
/
Copy pathFlexCounter.cpp
File metadata and controls
2878 lines (2533 loc) · 96.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "FlexCounter.h"
#include "VidManager.h"
#include "meta/sai_serialize.h"
#include "swss/redisapi.h"
#include "swss/tokenize.h"
#include <inttypes.h>
#include <vector>
using namespace syncd;
using namespace std;
#define MUTEX std::unique_lock<std::mutex> _lock(m_mtx);
#define MUTEX_UNLOCK _lock.unlock();
static const std::string COUNTER_TYPE_PORT = "Port Counter";
static const std::string COUNTER_TYPE_PORT_DEBUG = "Port Debug Counter";
static const std::string COUNTER_TYPE_QUEUE = "Queue Counter";
static const std::string COUNTER_TYPE_PG = "Priority Group Counter";
static const std::string COUNTER_TYPE_RIF = "Rif Counter";
static const std::string COUNTER_TYPE_SWITCH_DEBUG = "Switch Debug Counter";
static const std::string COUNTER_TYPE_MACSEC_FLOW = "MACSEC Flow Counter";
static const std::string COUNTER_TYPE_MACSEC_SA = "MACSEC SA Counter";
static const std::string COUNTER_TYPE_FLOW = "Flow Counter";
static const std::string COUNTER_TYPE_TUNNEL = "Tunnel Counter";
static const std::string COUNTER_TYPE_BUFFER_POOL = "Buffer Pool Counter";
static const std::string COUNTER_TYPE_ENI = "DASH ENI Counter";
static const std::string COUNTER_TYPE_METER_BUCKET = "DASH Meter Bucket Counter";
static const std::string COUNTER_TYPE_POLICER = "Policer Counter";
static const std::string COUNTER_TYPE_SRV6 = "SRv6 Counter";
static const std::string ATTR_TYPE_QUEUE = "Queue Attribute";
static const std::string ATTR_TYPE_PG = "Priority Group Attribute";
static const std::string ATTR_TYPE_MACSEC_SA = "MACSEC SA Attribute";
static const std::string ATTR_TYPE_ACL_COUNTER = "ACL Counter Attribute";
static const std::string COUNTER_TYPE_WRED_ECN_QUEUE = "WRED Queue Counter";
static const std::string COUNTER_TYPE_WRED_ECN_PORT = "WRED Port Counter";
const std::map<std::string, std::string> FlexCounter::m_plugIn2CounterType = {
{QUEUE_PLUGIN_FIELD, COUNTER_TYPE_QUEUE},
{PG_PLUGIN_FIELD, COUNTER_TYPE_PG},
{PORT_PLUGIN_FIELD, COUNTER_TYPE_PORT},
{RIF_PLUGIN_FIELD, COUNTER_TYPE_RIF},
{BUFFER_POOL_PLUGIN_FIELD, COUNTER_TYPE_BUFFER_POOL},
{TUNNEL_PLUGIN_FIELD, COUNTER_TYPE_TUNNEL},
{FLOW_COUNTER_PLUGIN_FIELD, COUNTER_TYPE_FLOW},
{WRED_QUEUE_PLUGIN_FIELD, COUNTER_TYPE_WRED_ECN_QUEUE},
{WRED_PORT_PLUGIN_FIELD, COUNTER_TYPE_WRED_ECN_PORT}};
const std::map<std::tuple<sai_object_type_t, std::string>, std::string> FlexCounter::m_objectTypeField2CounterType = {
{{SAI_OBJECT_TYPE_PORT, PORT_COUNTER_ID_LIST}, COUNTER_TYPE_PORT},
{{SAI_OBJECT_TYPE_PORT, PORT_DEBUG_COUNTER_ID_LIST}, COUNTER_TYPE_PORT_DEBUG},
{{SAI_OBJECT_TYPE_QUEUE, QUEUE_COUNTER_ID_LIST}, COUNTER_TYPE_QUEUE},
{{SAI_OBJECT_TYPE_QUEUE, QUEUE_ATTR_ID_LIST}, ATTR_TYPE_QUEUE},
{{SAI_OBJECT_TYPE_INGRESS_PRIORITY_GROUP, PG_COUNTER_ID_LIST}, COUNTER_TYPE_PG},
{{SAI_OBJECT_TYPE_INGRESS_PRIORITY_GROUP, PG_ATTR_ID_LIST}, ATTR_TYPE_PG},
{{SAI_OBJECT_TYPE_ROUTER_INTERFACE, RIF_COUNTER_ID_LIST}, COUNTER_TYPE_RIF},
{{SAI_OBJECT_TYPE_SWITCH, SWITCH_DEBUG_COUNTER_ID_LIST}, COUNTER_TYPE_SWITCH_DEBUG},
{{SAI_OBJECT_TYPE_MACSEC_FLOW, MACSEC_FLOW_COUNTER_ID_LIST}, COUNTER_TYPE_MACSEC_FLOW},
{{SAI_OBJECT_TYPE_MACSEC_SA, MACSEC_SA_COUNTER_ID_LIST}, COUNTER_TYPE_MACSEC_SA},
{{SAI_OBJECT_TYPE_MACSEC_SA, MACSEC_SA_ATTR_ID_LIST}, ATTR_TYPE_MACSEC_SA},
{{SAI_OBJECT_TYPE_ACL_COUNTER, ACL_COUNTER_ATTR_ID_LIST}, ATTR_TYPE_ACL_COUNTER},
{{SAI_OBJECT_TYPE_COUNTER, FLOW_COUNTER_ID_LIST}, COUNTER_TYPE_FLOW},
{{SAI_OBJECT_TYPE_POLICER, POLICER_COUNTER_ID_LIST}, COUNTER_TYPE_POLICER},
{{SAI_OBJECT_TYPE_TUNNEL, TUNNEL_COUNTER_ID_LIST}, COUNTER_TYPE_TUNNEL},
{{(sai_object_type_t)SAI_OBJECT_TYPE_ENI, ENI_COUNTER_ID_LIST}, COUNTER_TYPE_ENI},
{{(sai_object_type_t)SAI_OBJECT_TYPE_ENI, DASH_METER_COUNTER_ID_LIST}, COUNTER_TYPE_METER_BUCKET},
{{SAI_OBJECT_TYPE_COUNTER, SRV6_COUNTER_ID_LIST}, COUNTER_TYPE_SRV6},
};
BaseCounterContext::BaseCounterContext(const std::string &name, const std::string &instance):
m_name(name),
m_instanceId(instance)
{
SWSS_LOG_ENTER();
}
void BaseCounterContext::addPlugins(
_In_ const std::vector<std::string>& shaStrings)
{
SWSS_LOG_ENTER();
for (const auto &sha : shaStrings)
{
auto ret = m_plugins.insert(sha);
if (ret.second)
{
SWSS_LOG_NOTICE("%s counters plugin %s registered", m_name.c_str(), sha.c_str());
}
else
{
SWSS_LOG_ERROR("Plugin %s already registered", sha.c_str());
}
}
}
void BaseCounterContext::setNoDoubleCheckBulkCapability(
_In_ bool noDoubleCheckBulkCapability)
{
SWSS_LOG_ENTER();
no_double_check_bulk_capability = noDoubleCheckBulkCapability;
}
void BaseCounterContext::setBulkChunkSize(
_In_ uint32_t bulkChunkSize)
{
SWSS_LOG_ENTER();
default_bulk_chunk_size = bulkChunkSize;
}
void BaseCounterContext::setBulkChunkSizePerPrefix(
_In_ const std::string& bulkChunkSizePerPrefix)
{
SWSS_LOG_ENTER();
m_bulkChunkSizePerPrefix = bulkChunkSizePerPrefix;
}
template <typename StatType,
typename Enable = void>
struct CounterIds
{
CounterIds(
_In_ sai_object_id_t id,
_In_ const std::vector<StatType> &ids
): rid(id), counter_ids(ids) {}
void setStatsMode(sai_stats_mode_t statsMode) {}
sai_stats_mode_t getStatsMode() const
{
SWSS_LOG_ENTER();
SWSS_LOG_THROW("This counter type has no stats mode field");
// GCC 8.3 requires a return value here
return SAI_STATS_MODE_READ_AND_CLEAR;
}
sai_object_id_t rid;
std::vector<StatType> counter_ids;
};
// CounterIds structure contains stats mode, now buffer pool is the only one
// has member stats_mode.
template <typename StatType>
struct CounterIds<StatType, typename std::enable_if_t<std::is_same<StatType, sai_buffer_pool_stat_t>::value> >
{
CounterIds(
_In_ sai_object_id_t id,
_In_ const std::vector<StatType> &ids
): rid(id), counter_ids(ids)
{
SWSS_LOG_ENTER();
}
void setStatsMode(sai_stats_mode_t statsMode)
{
SWSS_LOG_ENTER();
stats_mode = statsMode;
}
sai_stats_mode_t getStatsMode() const
{
SWSS_LOG_ENTER();
return stats_mode;
}
sai_object_id_t rid;
std::vector<StatType> counter_ids;
sai_stats_mode_t stats_mode;
};
template <typename T>
struct HasStatsMode
{
template <typename U>
static void check(decltype(&U::stats_mode));
template <typename U>
static int check(...);
enum { value = std::is_void<decltype(check<T>(0))>::value };
};
// BulkStatsContext is used to store bulk counter related
// data and avoid construct them each time calling SAI bulk API
template <typename StatType>
struct BulkStatsContext
{
std::vector<sai_object_id_t> object_vids;
std::vector<sai_object_key_t> object_keys;
std::vector<StatType> counter_ids;
std::vector<sai_status_t> object_statuses;
std::vector<uint64_t> counters;
std::string name;
uint32_t default_bulk_chunk_size;
};
// TODO: use if const expression when cpp17 is supported
template <typename StatType>
std::string serializeStat(
_In_ const StatType stat)
{
SWSS_LOG_ENTER();
SWSS_LOG_THROW("serializeStat for default type parameter is not implemented");
// GCC 8.3 requires a return value here
return "";
}
template <>
std::string serializeStat(
_In_ const sai_port_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_port_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_policer_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_policer_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_queue_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_queue_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_ingress_priority_group_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_ingress_priority_group_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_router_interface_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_router_interface_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_switch_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_switch_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_macsec_flow_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_macsec_flow_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_macsec_sa_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_macsec_sa_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_counter_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_counter_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_tunnel_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_tunnel_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_buffer_pool_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_buffer_pool_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_eni_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_eni_stat(stat);
}
template <>
std::string serializeStat(
_In_ const sai_meter_bucket_entry_stat_t stat)
{
SWSS_LOG_ENTER();
return sai_serialize_meter_bucket_entry_stat(stat);
}
template <typename StatType>
void deserializeStat(
_In_ const char* name,
_Out_ StatType *stat)
{
SWSS_LOG_ENTER();
SWSS_LOG_THROW("deserializeStat for default type parameter is not implemented");
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_port_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_port_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_policer_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_policer_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_queue_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_queue_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_ingress_priority_group_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_ingress_priority_group_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_router_interface_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_router_interface_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_switch_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_switch_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_macsec_flow_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_macsec_flow_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_macsec_sa_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_macsec_sa_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_counter_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_counter_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_tunnel_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_tunnel_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_buffer_pool_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_buffer_pool_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_eni_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_eni_stat(name, stat);
}
template <>
void deserializeStat(
_In_ const char* name,
_Out_ sai_meter_bucket_entry_stat_t *stat)
{
SWSS_LOG_ENTER();
sai_deserialize_meter_bucket_entry_stat(name, stat);
}
template <typename AttrType>
void deserializeAttr(
_In_ const std::string& name,
_Out_ AttrType &attr)
{
SWSS_LOG_ENTER();
SWSS_LOG_THROW("deserializeAttr for default type parameter is not implemented");
}
template <>
void deserializeAttr(
_In_ const std::string& name,
_Out_ sai_queue_attr_t &attr)
{
SWSS_LOG_ENTER();
sai_deserialize_queue_attr(name, attr);
}
template <>
void deserializeAttr(
_In_ const std::string& name,
_Out_ sai_ingress_priority_group_attr_t &attr)
{
SWSS_LOG_ENTER();
sai_deserialize_ingress_priority_group_attr(name, attr);
}
template <>
void deserializeAttr(
_In_ const std::string& name,
_Out_ sai_macsec_sa_attr_t &attr)
{
SWSS_LOG_ENTER();
sai_deserialize_macsec_sa_attr(name, attr);
}
template <>
void deserializeAttr(
_In_ const std::string& name,
_Out_ sai_acl_counter_attr_t &attr)
{
SWSS_LOG_ENTER();
sai_deserialize_acl_counter_attr(name, attr);
}
template <typename StatType>
class CounterContext : public BaseCounterContext
{
std::map<std::string, uint32_t> m_counterChunkSizeMapFromPrefix;
protected:
sai_object_id_t m_switchId = SAI_NULL_OBJECT_ID;
public:
typedef CounterIds<StatType> CounterIdsType;
typedef BulkStatsContext<StatType> BulkContextType;
CounterContext(
_In_ const std::string &name,
_In_ const std::string &instance,
_In_ sai_object_type_t object_type,
_In_ sairedis::SaiInterface *vendor_sai,
_In_ sai_stats_mode_t &stats_mode):
BaseCounterContext(name, instance), m_objectType(object_type), m_vendorSai(vendor_sai), m_groupStatsMode(stats_mode)
{
SWSS_LOG_ENTER();
}
// For those object type who support per object stats mode, e.g. buffer pool.
virtual void addObject(
_In_ sai_object_id_t vid,
_In_ sai_object_id_t rid,
_In_ const std::vector<std::string> &idStrings,
_In_ const std::string &per_object_stats_mode) override
{
SWSS_LOG_ENTER();
sai_stats_mode_t instance_stats_mode = SAI_STATS_MODE_READ_AND_CLEAR;
sai_stats_mode_t effective_stats_mode;
// TODO: use if const expression when c++17 is supported
if (HasStatsMode<CounterIdsType>::value)
{
if (per_object_stats_mode == STATS_MODE_READ_AND_CLEAR)
{
instance_stats_mode = SAI_STATS_MODE_READ_AND_CLEAR;
}
else if (per_object_stats_mode == STATS_MODE_READ)
{
instance_stats_mode = SAI_STATS_MODE_READ;
}
else
{
SWSS_LOG_WARN("Stats mode %s not supported for flex counter. Using STATS_MODE_READ_AND_CLEAR", per_object_stats_mode.c_str());
}
effective_stats_mode = (m_groupStatsMode == SAI_STATS_MODE_READ_AND_CLEAR ||
instance_stats_mode == SAI_STATS_MODE_READ_AND_CLEAR) ? SAI_STATS_MODE_READ_AND_CLEAR : SAI_STATS_MODE_READ;
}
else
{
effective_stats_mode = m_groupStatsMode;
}
std::vector<StatType> counter_ids;
for (const auto &str : idStrings)
{
StatType stat;
deserializeStat(str.c_str(), &stat);
counter_ids.push_back(stat);
}
updateSupportedCounters(rid, counter_ids, effective_stats_mode);
std::vector<StatType> supportedIds;
for (auto &counter : counter_ids)
{
if (isCounterSupported(counter))
{
supportedIds.push_back(counter);
}
}
if (supportedIds.empty())
{
SWSS_LOG_NOTICE("%s %s does not has supported counters", m_name.c_str(), sai_serialize_object_id(rid).c_str());
return;
}
if (double_confirm_supported_counters)
{
std::vector<uint64_t> stats(supportedIds.size());
if (!collectData(rid, supportedIds, effective_stats_mode, false, stats))
{
SWSS_LOG_ERROR("%s RID %s can't provide the statistic", m_name.c_str(), sai_serialize_object_id(rid).c_str());
return;
}
}
// Perform a remove and re-add to simplify the logic here
removeObject(vid, false);
bool supportBulk;
// TODO: use if const expression when cpp17 is supported
if (HasStatsMode<CounterIdsType>::value)
{
supportBulk = false;
}
else
{
supportBulk = no_double_check_bulk_capability || checkBulkCapability(vid, rid, supportedIds);
}
if (!supportBulk)
{
auto counter_data = std::make_shared<CounterIds<StatType>>(rid, supportedIds);
// TODO: use if const expression when cpp17 is supported
if (HasStatsMode<CounterIdsType>::value)
{
counter_data->setStatsMode(instance_stats_mode);
}
m_objectIdsMap.emplace(vid, counter_data);
}
else if (m_counterChunkSizeMapFromPrefix.empty())
{
std::sort(supportedIds.begin(), supportedIds.end());
auto bulkContext = getBulkStatsContext(supportedIds, "default", default_bulk_chunk_size);
addBulkStatsContext(vid, rid, supportedIds, *bulkContext.get());
}
else
{
std::map<std::string, vector<StatType>> counter_prefix_map;
std::vector<StatType> default_partition;
createCounterBulkChunkSizePerPrefixPartition(supportedIds, counter_prefix_map, default_partition);
for (auto &counterPrefix : counter_prefix_map)
{
std::sort(counterPrefix.second.begin(), counterPrefix.second.end());
auto bulkContext = getBulkStatsContext(counterPrefix.second, counterPrefix.first, m_counterChunkSizeMapFromPrefix[counterPrefix.first]);
addBulkStatsContext(vid, rid, counterPrefix.second, *bulkContext.get());
}
std::sort(default_partition.begin(), default_partition.end());
auto bulkContext = getBulkStatsContext(default_partition, "default", default_bulk_chunk_size);
addBulkStatsContext(vid, rid, default_partition, *bulkContext.get());
}
}
bool parseBulkChunkSizePerPrefixConfigString(
_In_ const std::string& prefixConfigString)
{
SWSS_LOG_ENTER();
m_counterChunkSizeMapFromPrefix.clear();
if (!prefixConfigString.empty() && prefixConfigString != "NULL")
{
try
{
auto tokens = swss::tokenize(prefixConfigString, ';');
for (auto &token: tokens)
{
auto counter_name_bulk_size = swss::tokenize(token, ':');
SWSS_LOG_INFO("New partition %s bulk chunk size %s", counter_name_bulk_size[0].c_str(), counter_name_bulk_size[1].c_str());
m_counterChunkSizeMapFromPrefix[counter_name_bulk_size[0]] = stoi(counter_name_bulk_size[1]);
}
}
catch (...)
{
SWSS_LOG_ERROR("Invalid bulk chunk size per counter ID field %s", prefixConfigString.c_str());
m_counterChunkSizeMapFromPrefix.clear();
return false;
}
}
return true;
}
void createCounterBulkChunkSizePerPrefixPartition(
_In_ const std::vector<StatType>& supportedIds,
_Out_ std::map<std::string, std::vector<StatType>> &counter_prefix_map,
_Out_ std::vector<StatType> &default_partition,
_In_ bool log=false)
{
SWSS_LOG_ENTER();
default_partition.clear();
for (auto &counter : supportedIds)
{
std::string counterStr = serializeStat(counter);
bool found = false;
for (auto searchRef: m_counterChunkSizeMapFromPrefix)
{
if (!searchRef.first.empty() && counterStr.find(searchRef.first) != std::string::npos)
{
found = true;
counter_prefix_map[searchRef.first].push_back(counter);
if (log)
{
SWSS_LOG_INFO("Put counter %s to partition %s", counterStr.c_str(), searchRef.first.c_str());
}
break;
}
}
if (!found)
{
default_partition.push_back(counter);
if (log)
{
SWSS_LOG_INFO("Put counter %s to the default partition", counterStr.c_str());
}
}
}
}
void setBulkChunkSize(
_In_ uint32_t bulkChunkSize) override
{
SWSS_LOG_ENTER();
default_bulk_chunk_size = bulkChunkSize;
SWSS_LOG_INFO("Bulk chunk size updated to %u", bulkChunkSize);
for (auto &bulkStatsContext : m_bulkContexts)
{
auto const &name = (*bulkStatsContext.second.get()).name;
if (name == "default")
{
SWSS_LOG_INFO("Bulk chunk size of default updated to %u", bulkChunkSize);
(*bulkStatsContext.second.get()).default_bulk_chunk_size = default_bulk_chunk_size;
break;
}
}
}
void setBulkChunkSizePerPrefix(
_In_ const std::string& bulkChunkSizePerPrefix) override
{
SWSS_LOG_ENTER();
m_bulkChunkSizePerPrefix = bulkChunkSizePerPrefix;
// No operation if the input string is invalid or no bulk context has been created
if (!parseBulkChunkSizePerPrefixConfigString(bulkChunkSizePerPrefix) || m_bulkContexts.empty())
{
return;
}
if (m_bulkContexts.size() == 1)
{
// Only one bulk context exists which means
// it is the first time per counter chunk size is configured and a unified counter ID set is polled for all objects
auto it = m_bulkContexts.begin();
std::shared_ptr<BulkContextType> singleBulkContext = it->second;
const std::vector<StatType> &allCounterIds = singleBulkContext.get()->counter_ids;
std::map<std::string, vector<StatType>> counterChunkSizePerPrefix;
std::vector<StatType> defaultPartition;
if (m_counterChunkSizeMapFromPrefix.empty())
{
// There is still no per counter prefix chunk size configured as the chunk size map is still empty.
singleBulkContext.get()->default_bulk_chunk_size = default_bulk_chunk_size;
}
else
{
// Split the counter IDs according to the counter ID prefix mapping and store them into m_bulkContexts
SWSS_LOG_NOTICE("Split counter IDs set by prefix for the first time %s", bulkChunkSizePerPrefix.c_str());
createCounterBulkChunkSizePerPrefixPartition(allCounterIds, counterChunkSizePerPrefix, defaultPartition, true);
for (auto &counterPrefix : counterChunkSizePerPrefix)
{
std::sort(counterPrefix.second.begin(), counterPrefix.second.end());
auto bulkContext = getBulkStatsContext(counterPrefix.second, counterPrefix.first, m_counterChunkSizeMapFromPrefix[counterPrefix.first]);
bulkContext.get()->counter_ids = move(counterPrefix.second);
bulkContext.get()->object_statuses.resize(singleBulkContext.get()->object_statuses.size());
bulkContext.get()->object_vids = singleBulkContext.get()->object_vids;
bulkContext.get()->object_keys = singleBulkContext.get()->object_keys;
bulkContext.get()->counters.resize(bulkContext.get()->counter_ids.size() * bulkContext.get()->object_vids.size());
SWSS_LOG_INFO("Re-initializing counter partition %s", counterPrefix.first.c_str());
}
std::sort(defaultPartition.begin(), defaultPartition.end());
setBulkStatsContext(defaultPartition, singleBulkContext);
singleBulkContext.get()->counters.resize(singleBulkContext.get()->counter_ids.size() * singleBulkContext.get()->object_vids.size());
m_bulkContexts.erase(it);
SWSS_LOG_INFO("Removed the previous default counter partition");
}
}
else if (m_counterChunkSizeMapFromPrefix.empty())
{
// There have been multiple bulk contexts which can result from
// 1. per counter prefix chunk size configuration
// 2. different objects support different counter ID set
// And there is no per counter prefix chunk size configured any more
// Multiple bulk contexts will be merged into one if they share the same object IDs set, which means case (1).
std::set<sai_object_id_t> oid_set;
std::vector<StatType> counter_ids;
std::shared_ptr<BulkContextType> defaultBulkContext;
for (auto &context : m_bulkContexts)
{
if (oid_set.empty())
{
oid_set.insert(context.second.get()->object_vids.begin(), context.second.get()->object_vids.end());
}
else
{
std::set<sai_object_id_t> tmp_oid_set(context.second.get()->object_vids.begin(), context.second.get()->object_vids.end());
if (tmp_oid_set != oid_set)
{
SWSS_LOG_ERROR("Can not merge partition because they contains different objects");
return;
}
}
if (context.second.get()->name == "default")
{
defaultBulkContext = context.second;
}
counter_ids.insert(counter_ids.end(), context.second.get()->counter_ids.begin(), context.second.get()->counter_ids.end());
}
m_bulkContexts.clear();
std::sort(counter_ids.begin(), counter_ids.end());
setBulkStatsContext(counter_ids, defaultBulkContext);
defaultBulkContext.get()->counters.resize(defaultBulkContext.get()->counter_ids.size() * defaultBulkContext.get()->object_vids.size());
}
else
{
// Multiple bulk contexts and per counter prefix chunk size
// Update the chunk size only in this case.
SWSS_LOG_NOTICE("Update bulk chunk size only %s", bulkChunkSizePerPrefix.c_str());
auto counterChunkSizeMapFromPrefix = m_counterChunkSizeMapFromPrefix;
for (auto &bulkStatsContext : m_bulkContexts)
{
auto const &name = (*bulkStatsContext.second.get()).name;
if (name == "default")
{
continue;
}
auto const &searchRef = counterChunkSizeMapFromPrefix.find(name);
if (searchRef != counterChunkSizeMapFromPrefix.end())
{
auto const &chunkSize = searchRef->second;
SWSS_LOG_INFO("Reset counter prefix %s chunk size %d", name.c_str(), chunkSize);
(*bulkStatsContext.second.get()).default_bulk_chunk_size = chunkSize;
counterChunkSizeMapFromPrefix.erase(searchRef);
}
else
{
SWSS_LOG_WARN("Update bulk chunk size: bulk chunk size for prefix %s is not provided", name.c_str());
}
}
for (auto &it : counterChunkSizeMapFromPrefix)
{
SWSS_LOG_WARN("Update bulk chunk size: prefix %s does not exist", it.first.c_str());
}
}
for (auto &it : m_bulkContexts)
{
auto &context = *it.second.get();
SWSS_LOG_INFO("%s %s partition %s number of OIDs %d number of counter IDs %d number of counters %d",
m_name.c_str(),
m_instanceId.c_str(),
context.name.c_str(),
context.object_keys.size(),
context.counter_ids.size(),
context.counters.size());
}
}
virtual void bulkAddObject(
_In_ const std::vector<sai_object_id_t>& vids,
_In_ const std::vector<sai_object_id_t>& rids,
_In_ const std::vector<std::string>& idStrings,
_In_ const std::string &per_object_stats_mode)
{
SWSS_LOG_ENTER();
sai_stats_mode_t effective_stats_mode;
// TODO: use if const expression when c++17 is supported
if (HasStatsMode<CounterIdsType>::value)
{
// Bulk operation is not supported by the counter group.
SWSS_LOG_INFO("Counter group %s %s does not support bulk. Fallback to single call", m_name.c_str(), m_instanceId.c_str());
// Fall back to old way
for (size_t i = 0; i < vids.size(); i++)
{
auto rid = rids[i];
auto vid = vids[i];
addObject(vid, rid, idStrings, per_object_stats_mode);
}
return;
}
else
{
effective_stats_mode = m_groupStatsMode;
}
std::vector<StatType> allCounterIds, supportedIds;
for (const auto &str : idStrings)
{
StatType stat;
deserializeStat(str.c_str(), &stat);
{
allCounterIds.push_back(stat);
}
}
updateSupportedCounters(rids[0]/*it is not really used*/, allCounterIds, effective_stats_mode);
for (auto stat : allCounterIds)
{
if (isCounterSupported(stat))
{
supportedIds.push_back(stat);
}
}
if (supportedIds.empty())
{
SWSS_LOG_NOTICE("%s %s does not have supported counters", m_name.c_str(), m_instanceId.c_str());
return;
}
std::map<std::vector<StatType>, std::tuple<const std::string, uint32_t>> bulkUnsupportedCounters;
auto statsMode = m_groupStatsMode == SAI_STATS_MODE_READ ? SAI_STATS_MODE_BULK_READ : SAI_STATS_MODE_BULK_READ_AND_CLEAR;
auto checkAndUpdateBulkCapability = [&](const std::vector<StatType> &counter_ids, const std::string &prefix, uint32_t bulk_chunk_size)
{
BulkContextType ctx;
// Check bulk capabilities again
std::vector<StatType> supportedBulkIds;
sai_status_t status = SAI_STATUS_SUCCESS;
if (m_supportedBulkCounters.empty())
{
status = querySupportedCounters(rids[0], statsMode, m_supportedBulkCounters);
}
if (status == SAI_STATUS_SUCCESS && !m_supportedBulkCounters.empty())
{
for (auto stat : counter_ids)
{
if (m_supportedBulkCounters.count(stat) != 0)
{
supportedBulkIds.push_back(stat);
}
}
}
if (supportedBulkIds.size() < counter_ids.size())
{
// Bulk polling is unsupported for the whole group but single polling is supported
// Add all objects to m_objectIdsMap so that they will be polled using single API
for (size_t i = 0; i < vids.size(); i++)
{
auto rid = rids[i];
auto vid = vids[i];
std::vector<uint64_t> stats(counter_ids.size());
if (collectData(rid, counter_ids, effective_stats_mode, false, stats)) {
auto it_vid = m_objectIdsMap.find(vid);
if (it_vid != m_objectIdsMap.end())
{
// Remove and re-add if vid already exists
m_objectIdsMap.erase(it_vid);
}
auto counter_data = std::make_shared<CounterIds<StatType>>(rid, counter_ids);
m_objectIdsMap.emplace(vid, counter_data);
SWSS_LOG_INFO("Fallback to single call for object 0x%" PRIx64, vid);
} else {
SWSS_LOG_WARN("%s RID %s can't provide the statistic", m_name.c_str(), sai_serialize_object_id(rid).c_str());
}
}
return;
}
ctx.counter_ids = counter_ids;
addBulkStatsContext(vids, rids, counter_ids, ctx);
status = m_vendorSai->bulkGetStats(
SAI_NULL_OBJECT_ID,
m_objectType,
static_cast<uint32_t>(ctx.object_keys.size()),
ctx.object_keys.data(),
static_cast<uint32_t>(ctx.counter_ids.size()),
reinterpret_cast<const sai_stat_id_t *>(ctx.counter_ids.data()),
statsMode,
ctx.object_statuses.data(),
ctx.counters.data());
if (status == SAI_STATUS_SUCCESS)
{
auto bulkContext = getBulkStatsContext(counter_ids, prefix, bulk_chunk_size);
addBulkStatsContext(vids, rids, counter_ids, *bulkContext.get());
}
else
{
// Bulk is not supported for this counter prefix
// Append it to bulkUnsupportedCounters
std::tuple<const std::string, uint32_t> value(prefix, bulk_chunk_size);
bulkUnsupportedCounters.emplace(counter_ids, value);
SWSS_LOG_INFO("Counters starting with %s do not support bulk. Fallback to single call for these counters", prefix.c_str());
}
};
if (m_counterChunkSizeMapFromPrefix.empty())
{
std::sort(supportedIds.begin(), supportedIds.end());
checkAndUpdateBulkCapability(supportedIds, "default", default_bulk_chunk_size);
}
else
{
std::map<std::string, vector<StatType>> counter_prefix_map;
std::vector<StatType> default_partition;
createCounterBulkChunkSizePerPrefixPartition(supportedIds, counter_prefix_map, default_partition);
for (auto &counterPrefix : counter_prefix_map)
{
std::sort(counterPrefix.second.begin(), counterPrefix.second.end());
}