Skip to content

Commit 148d455

Browse files
mykolafjleveque
authored andcommitted
show BPS, PPS, UTIL rates w/o previous clear (sonic-net#508)
Signed-off-by: Mykola Faryma <[email protected]>
1 parent 74cca9e commit 148d455

File tree

3 files changed

+61
-137
lines changed

3 files changed

+61
-137
lines changed

scripts/intfstat

+19-43
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ from collections import namedtuple, OrderedDict
3434
from natsort import natsorted
3535
from tabulate import tabulate
3636
from utilities_common.netstat import ns_diff, ns_brate, ns_prate, table_as_json, STATUS_NA
37+
from utilities_common import get_uptime
3738

3839
NStats = namedtuple("NStats", "rx_b_ok, rx_p_ok, tx_b_ok, tx_p_ok,\
3940
rx_b_err, rx_p_err, tx_b_err, tx_p_err,")
@@ -130,25 +131,6 @@ class Intfstat(object):
130131
else:
131132
return STATUS_NA
132133

133-
def cnstat_print(self, cnstat_dict, use_json):
134-
"""
135-
Print the cnstat.
136-
"""
137-
table = []
138-
139-
for key, data in cnstat_dict.iteritems():
140-
if key == 'time':
141-
continue
142-
143-
table.append((key, data.rx_p_ok, STATUS_NA, STATUS_NA, data.rx_p_err,
144-
data.tx_p_ok, STATUS_NA, STATUS_NA, data.tx_p_err))
145-
146-
if use_json:
147-
print table_as_json(table, header)
148-
149-
else:
150-
print tabulate(table, header, tablefmt='simple', stralign='right')
151-
152134
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json):
153135
"""
154136
Print the difference between two cnstat results.
@@ -158,33 +140,27 @@ class Intfstat(object):
158140

159141
for key, cntr in cnstat_new_dict.iteritems():
160142
if key == 'time':
161-
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
162-
time_gap = time_gap.total_seconds()
143+
if 'time' in cnstat_old_dict:
144+
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
145+
time_gap = time_gap.total_seconds()
146+
else:
147+
time_gap = get_uptime()
163148
continue
164-
old_cntr = None
149+
165150
if key in cnstat_old_dict:
166151
old_cntr = cnstat_old_dict.get(key)
167-
168-
if old_cntr is not None:
169-
table.append((key,
170-
ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok),
171-
ns_brate(cntr.rx_b_ok, old_cntr.rx_b_ok, time_gap),
172-
ns_prate(cntr.rx_p_ok, old_cntr.rx_p_ok, time_gap),
173-
ns_diff(cntr.rx_p_err, old_cntr.rx_p_err),
174-
ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok),
175-
ns_brate(cntr.tx_b_ok, old_cntr.tx_b_ok, time_gap),
176-
ns_prate(cntr.tx_p_ok, old_cntr.tx_p_ok, time_gap),
177-
ns_diff(cntr.tx_p_err, old_cntr.tx_p_err)))
178152
else:
179-
table.append((key,
180-
cntr.rx_p_ok,
181-
STATUS_NA,
182-
STATUS_NA,
183-
cntr.rx_p_err,
184-
cntr.tx_p_ok,
185-
STATUS_NA,
186-
STATUS_NA,
187-
cntr.tx_p_err))
153+
old_cntr = NStats._make([0] * (len(header) - 1))
154+
155+
table.append((key,
156+
ns_diff(cntr.rx_p_ok, old_cntr.rx_p_ok),
157+
ns_brate(cntr.rx_b_ok, old_cntr.rx_b_ok, time_gap),
158+
ns_prate(cntr.rx_p_ok, old_cntr.rx_p_ok, time_gap),
159+
ns_diff(cntr.rx_p_err, old_cntr.rx_p_err),
160+
ns_diff(cntr.tx_p_ok, old_cntr.tx_p_ok),
161+
ns_brate(cntr.tx_b_ok, old_cntr.tx_b_ok, time_gap),
162+
ns_prate(cntr.tx_p_ok, old_cntr.tx_p_ok, time_gap),
163+
ns_diff(cntr.tx_p_err, old_cntr.tx_p_err)))
188164
if use_json:
189165
print self.table_as_json(table, header)
190166
else:
@@ -331,7 +307,7 @@ def main():
331307
if interface_name:
332308
intfstat.cnstat_single_interface(interface_name, cnstat_dict, None)
333309
else:
334-
intfstat.cnstat_print(cnstat_dict, use_json)
310+
intfstat.cnstat_diff_print(cnstat_dict, {}, use_json)
335311
else:
336312
#wait for the specified time and then gather the new stats and output the difference.
337313
time.sleep(wait_time_in_seconds)

scripts/portstat

+39-94
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from collections import namedtuple, OrderedDict
2121
from natsort import natsorted
2222
from tabulate import tabulate
2323
from utilities_common.netstat import ns_diff, ns_brate, ns_prate, ns_util, table_as_json
24+
from utilities_common import get_uptime
2425

2526
PORT_RATE = 40
2627

@@ -32,6 +33,7 @@ header_all = ['IFACE', 'STATE', 'RX_OK', 'RX_BPS', 'RX_PPS', 'RX_UTIL', 'RX_ERR'
3233
header = ['IFACE', 'STATE', 'RX_OK', 'RX_BPS', 'RX_UTIL', 'RX_ERR', 'RX_DRP', 'RX_OVR',
3334
'TX_OK', 'TX_BPS', 'TX_UTIL', 'TX_ERR', 'TX_DRP', 'TX_OVR']
3435

36+
BUCKET_NUM = 10
3537
counter_bucket_dict = {
3638
'SAI_PORT_STAT_IF_IN_UCAST_PKTS': 0,
3739
'SAI_PORT_STAT_IF_IN_NON_UCAST_PKTS': 0,
@@ -116,36 +118,7 @@ class Portstat(object):
116118
else:
117119
return STATUS_NA
118120

119-
def cnstat_print(self, cnstat_dict, use_json, print_all):
120-
"""
121-
Print the cnstat.
122-
"""
123-
table = []
124-
125-
for key, data in cnstat_dict.iteritems():
126-
if key == 'time':
127-
continue
128-
129-
if print_all:
130-
table.append((key, self.get_port_state(key),
131-
data.rx_ok, STATUS_NA, STATUS_NA, STATUS_NA, data.rx_err,
132-
data.rx_drop, data.rx_ovr,
133-
data.tx_ok, STATUS_NA, STATUS_NA, STATUS_NA, data.tx_err,
134-
data.tx_drop, data.tx_ovr))
135-
else:
136-
table.append((key, self.get_port_state(key),
137-
data.rx_ok, STATUS_NA, STATUS_NA, data.rx_err,
138-
data.rx_drop, data.rx_ovr,
139-
data.tx_ok, STATUS_NA, STATUS_NA, data.tx_err,
140-
data.tx_drop, data.tx_ovr))
141-
142-
143-
if use_json:
144-
table_as_json(table, header_all if print_all else header)
145-
else:
146-
print tabulate(table, header_all, tablefmt='simple', stralign='right') # if print_all else header
147-
148-
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json, print_all):
121+
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json=False, print_all=False):
149122
"""
150123
Print the difference between two cnstat results.
151124
"""
@@ -154,75 +127,47 @@ class Portstat(object):
154127

155128
for key, cntr in cnstat_new_dict.iteritems():
156129
if key == 'time':
157-
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
158-
time_gap = time_gap.total_seconds()
130+
if 'time' in cnstat_old_dict:
131+
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
132+
time_gap = time_gap.total_seconds()
133+
else:
134+
time_gap = get_uptime()
159135
continue
160-
old_cntr = None
161136
if key in cnstat_old_dict:
162137
old_cntr = cnstat_old_dict.get(key)
138+
else:
139+
old_cntr = NStats._make([0] * BUCKET_NUM)
163140

164141
if print_all:
165-
if old_cntr is not None:
166-
table.append((key, self.get_port_state(key),
167-
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
168-
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
169-
ns_prate(cntr.rx_ok, old_cntr.rx_ok, time_gap),
170-
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap),
171-
ns_diff(cntr.rx_err, old_cntr.rx_err),
172-
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
173-
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
174-
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
175-
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
176-
ns_prate(cntr.tx_ok, old_cntr.tx_ok, time_gap),
177-
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap),
178-
ns_diff(cntr.tx_err, old_cntr.tx_err),
179-
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
180-
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
181-
else:
182-
table.append((key, self.get_port_state(key),
183-
cntr.rx_ok,
184-
STATUS_NA,
185-
STATUS_NA,
186-
STATUS_NA,
187-
cntr.rx_err,
188-
cntr.rx_drop,
189-
cntr.rx_ovr,
190-
cntr.tx_ok,
191-
STATUS_NA,
192-
STATUS_NA,
193-
STATUS_NA,
194-
cntr.tx_err,
195-
cntr.tx_drop,
196-
cntr.tx_err))
142+
table.append((key, self.get_port_state(key),
143+
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
144+
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
145+
ns_prate(cntr.rx_ok, old_cntr.rx_ok, time_gap),
146+
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap),
147+
ns_diff(cntr.rx_err, old_cntr.rx_err),
148+
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
149+
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
150+
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
151+
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
152+
ns_prate(cntr.tx_ok, old_cntr.tx_ok, time_gap),
153+
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap),
154+
ns_diff(cntr.tx_err, old_cntr.tx_err),
155+
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
156+
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
197157
else:
198-
if old_cntr is not None:
199-
table.append((key, self.get_port_state(key),
200-
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
201-
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
202-
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap),
203-
ns_diff(cntr.rx_err, old_cntr.rx_err),
204-
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
205-
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
206-
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
207-
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
208-
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap),
209-
ns_diff(cntr.tx_err, old_cntr.tx_err),
210-
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
211-
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
212-
else:
213-
table.append((key, self.get_port_state(key),
214-
cntr.rx_ok,
215-
STATUS_NA,
216-
STATUS_NA,
217-
cntr.rx_err,
218-
cntr.rx_drop,
219-
cntr.rx_ovr,
220-
cntr.tx_ok,
221-
STATUS_NA,
222-
STATUS_NA,
223-
cntr.tx_err,
224-
cntr.tx_drop,
225-
cntr.tx_err))
158+
table.append((key, self.get_port_state(key),
159+
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
160+
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
161+
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap),
162+
ns_diff(cntr.rx_err, old_cntr.rx_err),
163+
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
164+
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
165+
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
166+
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
167+
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap),
168+
ns_diff(cntr.tx_err, old_cntr.tx_err),
169+
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
170+
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
226171

227172
if use_json:
228173
print self.table_as_json(table, print_all)
@@ -340,7 +285,7 @@ Examples:
340285
print "\nFile '%s' does not exist" % cnstat_fqn_file
341286
print "Did you run 'portstat -c -t %s' to record the counters via tag %s?\n" % (tag_name, tag_name)
342287
else:
343-
portstat.cnstat_print(cnstat_dict, use_json, print_all)
288+
portstat.cnstat_diff_print(cnstat_dict, {}, use_json, print_all)
344289
else:
345290
#wait for the specified time and then gather the new stats and output the difference.
346291
time.sleep(wait_time_in_seconds)

utilities_common/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def get_uptime():
2+
with open('/proc/uptime') as fp:
3+
return float(fp.read().split(' ')[0])

0 commit comments

Comments
 (0)