Skip to content

Commit 4740617

Browse files
mykolaflguohan
authored andcommitted
Revert "show BPS, PPS, UTIL rates w/o previous clear (sonic-net#508)" (sonic-net#718)
* Revert "show BPS, PPS, UTIL rates w/o previous clear (sonic-net#508)" This reverts commit 148d455. * [portstat] use actual port speed for util calculation after revert Signed-off-by: Mykola Faryma <[email protected]>
1 parent 0e23e1c commit 4740617

File tree

3 files changed

+138
-61
lines changed

3 files changed

+138
-61
lines changed

scripts/intfstat

+43-19
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ 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
3837

3938
NStats = namedtuple("NStats", "rx_b_ok, rx_p_ok, tx_b_ok, tx_p_ok,\
4039
rx_b_err, rx_p_err, tx_b_err, tx_p_err,")
@@ -131,6 +130,25 @@ class Intfstat(object):
131130
else:
132131
return STATUS_NA
133132

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+
134152
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json):
135153
"""
136154
Print the difference between two cnstat results.
@@ -140,27 +158,33 @@ class Intfstat(object):
140158

141159
for key, cntr in cnstat_new_dict.iteritems():
142160
if key == 'time':
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()
161+
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
162+
time_gap = time_gap.total_seconds()
148163
continue
149-
164+
old_cntr = None
150165
if key in cnstat_old_dict:
151166
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)))
152178
else:
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)))
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))
164188
if use_json:
165189
print table_as_json(table, header)
166190
else:
@@ -307,7 +331,7 @@ def main():
307331
if interface_name:
308332
intfstat.cnstat_single_interface(interface_name, cnstat_dict, None)
309333
else:
310-
intfstat.cnstat_diff_print(cnstat_dict, {}, use_json)
334+
intfstat.cnstat_print(cnstat_dict, use_json)
311335
else:
312336
#wait for the specified time and then gather the new stats and output the difference.
313337
time.sleep(wait_time_in_seconds)

scripts/portstat

+95-39
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ 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
2524

2625
PORT_RATE = 40
2726

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

36-
BUCKET_NUM = 10
3735
counter_bucket_dict = {
3836
'SAI_PORT_STAT_IF_IN_UCAST_PKTS': 0,
3937
'SAI_PORT_STAT_IF_IN_NON_UCAST_PKTS': 0,
@@ -132,7 +130,36 @@ class Portstat(object):
132130
else:
133131
return STATUS_NA
134132

135-
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json=False, print_all=False):
133+
def cnstat_print(self, cnstat_dict, use_json, print_all):
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+
if print_all:
144+
table.append((key, self.get_port_state(key),
145+
data.rx_ok, STATUS_NA, STATUS_NA, STATUS_NA, data.rx_err,
146+
data.rx_drop, data.rx_ovr,
147+
data.tx_ok, STATUS_NA, STATUS_NA, STATUS_NA, data.tx_err,
148+
data.tx_drop, data.tx_ovr))
149+
else:
150+
table.append((key, self.get_port_state(key),
151+
data.rx_ok, STATUS_NA, STATUS_NA, data.rx_err,
152+
data.rx_drop, data.rx_ovr,
153+
data.tx_ok, STATUS_NA, STATUS_NA, data.tx_err,
154+
data.tx_drop, data.tx_ovr))
155+
156+
157+
if use_json:
158+
table_as_json(table, header_all if print_all else header)
159+
else:
160+
print tabulate(table, header_all, tablefmt='simple', stralign='right') # if print_all else header
161+
162+
def cnstat_diff_print(self, cnstat_new_dict, cnstat_old_dict, use_json, print_all):
136163
"""
137164
Print the difference between two cnstat results.
138165
"""
@@ -141,47 +168,76 @@ class Portstat(object):
141168

142169
for key, cntr in cnstat_new_dict.iteritems():
143170
if key == 'time':
144-
if 'time' in cnstat_old_dict:
145-
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
146-
time_gap = time_gap.total_seconds()
147-
else:
148-
time_gap = get_uptime()
171+
time_gap = cnstat_new_dict.get('time') - cnstat_old_dict.get('time')
172+
time_gap = time_gap.total_seconds()
149173
continue
174+
old_cntr = None
150175
if key in cnstat_old_dict:
151176
old_cntr = cnstat_old_dict.get(key)
152-
else:
153-
old_cntr = NStats._make([0] * BUCKET_NUM)
177+
154178
port_speed = self.get_port_speed(key)
155179
if print_all:
156-
table.append((key, self.get_port_state(key),
157-
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
158-
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
159-
ns_prate(cntr.rx_ok, old_cntr.rx_ok, time_gap),
160-
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap, port_speed),
161-
ns_diff(cntr.rx_err, old_cntr.rx_err),
162-
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
163-
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
164-
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
165-
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
166-
ns_prate(cntr.tx_ok, old_cntr.tx_ok, time_gap),
167-
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap, port_speed),
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)))
180+
if old_cntr is not None:
181+
table.append((key, self.get_port_state(key),
182+
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
183+
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
184+
ns_prate(cntr.rx_ok, old_cntr.rx_ok, time_gap),
185+
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap, port_speed),
186+
ns_diff(cntr.rx_err, old_cntr.rx_err),
187+
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
188+
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
189+
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
190+
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
191+
ns_prate(cntr.tx_ok, old_cntr.tx_ok, time_gap),
192+
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap, port_speed),
193+
ns_diff(cntr.tx_err, old_cntr.tx_err),
194+
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
195+
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
196+
else:
197+
table.append((key, self.get_port_state(key),
198+
cntr.rx_ok,
199+
STATUS_NA,
200+
STATUS_NA,
201+
STATUS_NA,
202+
cntr.rx_err,
203+
cntr.rx_drop,
204+
cntr.rx_ovr,
205+
cntr.tx_ok,
206+
STATUS_NA,
207+
STATUS_NA,
208+
STATUS_NA,
209+
cntr.tx_err,
210+
cntr.tx_drop,
211+
cntr.tx_err))
171212
else:
172-
table.append((key, self.get_port_state(key),
173-
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
174-
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
175-
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap, port_speed),
176-
ns_diff(cntr.rx_err, old_cntr.rx_err),
177-
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
178-
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
179-
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
180-
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
181-
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap, port_speed),
182-
ns_diff(cntr.tx_err, old_cntr.tx_err),
183-
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
184-
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
213+
if old_cntr is not None:
214+
table.append((key, self.get_port_state(key),
215+
ns_diff(cntr.rx_ok, old_cntr.rx_ok),
216+
ns_brate(cntr.rx_byt, old_cntr.rx_byt, time_gap),
217+
ns_util(cntr.rx_byt, old_cntr.rx_byt, time_gap),
218+
ns_diff(cntr.rx_err, old_cntr.rx_err),
219+
ns_diff(cntr.rx_drop, old_cntr.rx_drop),
220+
ns_diff(cntr.rx_ovr, old_cntr.rx_ovr),
221+
ns_diff(cntr.tx_ok, old_cntr.tx_ok),
222+
ns_brate(cntr.tx_byt, old_cntr.tx_byt, time_gap),
223+
ns_util(cntr.tx_byt, old_cntr.tx_byt, time_gap),
224+
ns_diff(cntr.tx_err, old_cntr.tx_err),
225+
ns_diff(cntr.tx_drop, old_cntr.tx_drop),
226+
ns_diff(cntr.tx_ovr, old_cntr.tx_ovr)))
227+
else:
228+
table.append((key, self.get_port_state(key),
229+
cntr.rx_ok,
230+
STATUS_NA,
231+
STATUS_NA,
232+
cntr.rx_err,
233+
cntr.rx_drop,
234+
cntr.rx_ovr,
235+
cntr.tx_ok,
236+
STATUS_NA,
237+
STATUS_NA,
238+
cntr.tx_err,
239+
cntr.tx_drop,
240+
cntr.tx_err))
185241

186242
if use_json:
187243
print table_as_json(table, header)
@@ -299,7 +355,7 @@ Examples:
299355
print "\nFile '%s' does not exist" % cnstat_fqn_file
300356
print "Did you run 'portstat -c -t %s' to record the counters via tag %s?\n" % (tag_name, tag_name)
301357
else:
302-
portstat.cnstat_diff_print(cnstat_dict, {}, use_json, print_all)
358+
portstat.cnstat_print(cnstat_dict, use_json, print_all)
303359
else:
304360
#wait for the specified time and then gather the new stats and output the difference.
305361
time.sleep(wait_time_in_seconds)

utilities_common/__init__.py

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

0 commit comments

Comments
 (0)