2
2
3
3
#####################################################################
4
4
#
5
- # pfcstat is a tool for summarizing Priority-based Flow Control (PFC) statistics.
5
+ # pfcstat is a tool for summarizing Priority-based Flow Control (PFC) statistics.
6
6
#
7
7
#####################################################################
8
8
9
+ import swsssdk
9
10
import sys
10
11
import argparse
11
12
import cPickle as pickle
@@ -19,24 +20,6 @@ from collections import namedtuple, OrderedDict
19
20
from natsort import natsorted
20
21
from tabulate import tabulate
21
22
22
- from sonic_py_common .multi_asic import get_external_ports
23
- from utilities_common import multi_asic as multi_asic_util
24
- from utilities_common import constants
25
-
26
- # mock the redis for unit test purposes #
27
- try :
28
- if os .environ ["UTILITIES_UNIT_TESTING" ] == "2" :
29
- modules_path = os .path .join (os .path .dirname (__file__ ), ".." )
30
- tests_path = os .path .join (modules_path , "sonic-utilities-tests" )
31
- sys .path .insert (0 , modules_path )
32
- sys .path .insert (0 , tests_path )
33
- import mock_tables .dbconnector
34
- if os .environ ["UTILITIES_UNIT_TESTING_TOPOLOGY" ] == "multi_asic" :
35
- import mock_tables .mock_multi_asic
36
- mock_tables .dbconnector .load_namespace_config ()
37
-
38
- except KeyError :
39
- pass
40
23
41
24
PStats = namedtuple ("PStats" , "pfc0, pfc1, pfc2, pfc3, pfc4, pfc5, pfc6, pfc7" )
42
25
header_Rx = ['Port Rx' , 'PFC0' , 'PFC1' , 'PFC2' , 'PFC3' , 'PFC4' , 'PFC5' , 'PFC6' , 'PFC7' ]
@@ -71,14 +54,11 @@ COUNTER_TABLE_PREFIX = "COUNTERS:"
71
54
COUNTERS_PORT_NAME_MAP = "COUNTERS_PORT_NAME_MAP"
72
55
73
56
class Pfcstat (object ):
74
- def __init__ (self , namespace , display ):
75
- self .multi_asic = multi_asic_util .MultiAsic (display , namespace )
76
- self .db = None
77
- self .config_db = None
78
- self .cnstat_dict = OrderedDict ()
79
-
80
- @multi_asic_util .run_on_multi_asic
81
- def collect_cnstat (self , rx ):
57
+ def __init__ (self ):
58
+ self .db = swsssdk .SonicV2Connector (host = '127.0.0.1' )
59
+ self .db .connect (self .db .COUNTERS_DB )
60
+
61
+ def get_cnstat (self , rx ):
82
62
"""
83
63
Get the counters info from database.
84
64
"""
@@ -93,9 +73,7 @@ class Pfcstat(object):
93
73
bucket_dict = counter_bucket_tx_dict
94
74
for counter_name , pos in bucket_dict .iteritems ():
95
75
full_table_id = COUNTER_TABLE_PREFIX + table_id
96
- counter_data = self .db .get (
97
- self .db .COUNTERS_DB , full_table_id , counter_name
98
- )
76
+ counter_data = self .db .get (self .db .COUNTERS_DB , full_table_id , counter_name )
99
77
if counter_data is None :
100
78
fields [pos ] = STATUS_NA
101
79
else :
@@ -104,34 +82,15 @@ class Pfcstat(object):
104
82
return cntr
105
83
106
84
# Get the info from database
107
- counter_port_name_map = self .db .get_all (
108
- self .db .COUNTERS_DB , COUNTERS_PORT_NAME_MAP
109
- )
110
- if counter_port_name_map is None :
111
- return
112
- display_ports_set = set (counter_port_name_map .keys ())
113
- if self .multi_asic .display_option == constants .DISPLAY_EXTERNAL :
114
- display_ports_set = get_external_ports (
115
- display_ports_set , self .multi_asic .current_namespace
116
- )
85
+ counter_port_name_map = self .db .get_all (self .db .COUNTERS_DB , COUNTERS_PORT_NAME_MAP )
117
86
# Build a dictionary of the stats
118
87
cnstat_dict = OrderedDict ()
119
88
cnstat_dict ['time' ] = datetime .datetime .now ()
120
- if counter_port_name_map is not None :
121
- for port in natsorted (counter_port_name_map ):
122
- if port in display_ports_set :
123
- cnstat_dict [port ] = get_counters (
124
- counter_port_name_map [port ]
125
- )
126
- self .cnstat_dict .update (cnstat_dict )
127
-
128
- def get_cnstat (self , rx ):
129
- """
130
- Get the counters info from database.
131
- """
132
- self .cnstat_dict .clear ()
133
- self .collect_cnstat (rx )
134
- return self .cnstat_dict
89
+ if counter_port_name_map is None :
90
+ return cnstat_dict
91
+ for port in natsorted (counter_port_name_map ):
92
+ cnstat_dict [port ] = get_counters (counter_port_name_map [port ])
93
+ return cnstat_dict
135
94
136
95
def cnstat_print (self , cnstat_dict , rx ):
137
96
"""
@@ -207,22 +166,10 @@ Examples:
207
166
pfcstat
208
167
pfcstat -c
209
168
pfcstat -d
210
- pfcstat -n asic1
211
- pfcstat -s all -n asic0
212
169
""" )
213
170
214
- parser .add_argument ( '-c' , '--clear' , action = 'store_true' ,
215
- help = 'Clear previous stats and save new ones'
216
- )
217
- parser .add_argument (
218
- '-d' , '--delete' , action = 'store_true' , help = 'Delete saved stats'
219
- )
220
- parser .add_argument ('-s' , '--show' , default = constants .DISPLAY_EXTERNAL ,
221
- help = 'Display all interfaces or only external interfaces'
222
- )
223
- parser .add_argument ('-n' , '--namespace' , default = None ,
224
- help = 'Display interfaces for specific namespace'
225
- )
171
+ parser .add_argument ('-c' , '--clear' , action = 'store_true' , help = 'Clear previous stats and save new ones' )
172
+ parser .add_argument ('-d' , '--delete' , action = 'store_true' , help = 'Delete saved stats' )
226
173
args = parser .parse_args ()
227
174
228
175
save_fresh_stats = args .clear
@@ -231,20 +178,15 @@ Examples:
231
178
uid = str (os .getuid ())
232
179
cnstat_file = uid
233
180
234
- cnstat_dir = os .path .join (os .sep , "tmp" , "pfcstat-{}" .format (uid ))
235
- cnstat_fqn_file_rx = os .path .join (cnstat_dir , "{}rx" .format (cnstat_file ))
236
- cnstat_fqn_file_tx = os .path .join (cnstat_dir , "{}tx" .format (cnstat_file ))
237
-
238
- # if '-c' option is provided get stats from all (frontend and backend) ports
239
- if save_fresh_stats :
240
- args .namespace = None
241
- args .show = constants .DISPLAY_ALL
181
+ cnstat_dir = "/tmp/pfcstat-" + uid
182
+ cnstat_fqn_file_rx = cnstat_dir + "/" + cnstat_file + "rx"
183
+ cnstat_fqn_file_tx = cnstat_dir + "/" + cnstat_file + "tx"
242
184
243
- pfcstat = Pfcstat (args . namespace , args . show )
185
+ pfcstat = Pfcstat ()
244
186
245
187
if delete_all_stats :
246
188
for file in os .listdir (cnstat_dir ):
247
- os .remove (os . path . join ( cnstat_dir , file ) )
189
+ os .remove (cnstat_dir + "/" + file )
248
190
249
191
try :
250
192
os .rmdir (cnstat_dir )
@@ -286,6 +228,7 @@ Examples:
286
228
"""
287
229
Print the counters of pfc rx counter
288
230
"""
231
+ cnstat_cached_dict = OrderedDict ()
289
232
if os .path .isfile (cnstat_fqn_file_rx ):
290
233
try :
291
234
cnstat_cached_dict = pickle .load (open (cnstat_fqn_file_rx , 'r' ))
@@ -296,10 +239,11 @@ Examples:
296
239
else :
297
240
pfcstat .cnstat_print (cnstat_dict_rx , True )
298
241
299
- print ( "" )
242
+ print
300
243
"""
301
244
Print the counters of pfc tx counter
302
245
"""
246
+ cnstat_cached_dict = OrderedDict ()
303
247
if os .path .isfile (cnstat_fqn_file_tx ):
304
248
try :
305
249
cnstat_cached_dict = pickle .load (open (cnstat_fqn_file_tx , 'r' ))
0 commit comments