Skip to content

Commit

Permalink
Merge pull request #28 from abdosi/202205
Browse files Browse the repository at this point in the history
[202205-msft] Added support to display nonzero Queue Counters.
  • Loading branch information
abdosi authored Sep 14, 2023
2 parents 387b980 + 3f0d455 commit 66ea58a
Show file tree
Hide file tree
Showing 4 changed files with 1,163 additions and 61 deletions.
66 changes: 38 additions & 28 deletions scripts/queuestat
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class Queuestat(object):
cnstat_dict[queue] = get_counters(queue_map[queue])
return cnstat_dict

def cnstat_print(self, port, cnstat_dict, json_opt):
def cnstat_print(self, port, cnstat_dict, json_opt, non_zero):
"""
Print the cnstat. If JSON option is True, return data in
JSON format.
Expand All @@ -211,19 +211,22 @@ class Queuestat(object):
if json_opt:
json_output[port][key] = data
continue
table.append((port, data.queuetype + str(data.queueindex),
data.totalpacket, data.totalbytes,
data.droppacket, data.dropbytes))

if not non_zero or data.totalpacket != '0' or data.totalbytes != '0' or data.droppacket !='0' or data.dropbytes != '0':
table.append((port, data.queuetype + str(data.queueindex),
data.totalpacket, data.totalbytes,
data.droppacket, data.dropbytes))

if json_opt:
json_output[port].update(build_json(port, table))
return json_output
else:
hdr = voq_header if self.voq else header
print(tabulate(table, hdr, tablefmt='simple', stralign='right'))
print()
if table:
print(tabulate(table, hdr, tablefmt='simple', stralign='right'))
print()

def cnstat_diff_print(self, port, cnstat_new_dict, cnstat_old_dict, json_opt):
def cnstat_diff_print(self, port, cnstat_new_dict, cnstat_old_dict, json_opt, non_zero):
"""
Print the difference between two cnstat results. If JSON
option is True, return data in JSON format.
Expand All @@ -241,12 +244,16 @@ class Queuestat(object):
old_cntr = cnstat_old_dict.get(key)

if old_cntr is not None:
table.append((port, cntr.queuetype + str(cntr.queueindex),
ns_diff(cntr.totalpacket, old_cntr.totalpacket),
ns_diff(cntr.totalbytes, old_cntr.totalbytes),
ns_diff(cntr.droppacket, old_cntr.droppacket),
ns_diff(cntr.dropbytes, old_cntr.dropbytes)))
else:
if not non_zero or ns_diff(cntr.totalpacket, old_cntr.totalpacket) != '0' or \
ns_diff(cntr.totalbytes, old_cntr.totalbytes) != '0' or \
ns_diff(cntr.droppacket, old_cntr.droppacket) != '0' or \
ns_diff(cntr.dropbytes, old_cntr.dropbytes) != '0':
table.append((port, cntr.queuetype + str(cntr.queueindex),
ns_diff(cntr.totalpacket, old_cntr.totalpacket),
ns_diff(cntr.totalbytes, old_cntr.totalbytes),
ns_diff(cntr.droppacket, old_cntr.droppacket),
ns_diff(cntr.dropbytes, old_cntr.dropbytes)))
elif not non_zero or cntr.totalpacket != '0' or cntr.totalbytes != '0' or cntr.droppacket !='0' or cntr.dropbytes != '0':
table.append((port, cntr.queuetype + str(cntr.queueindex),
cntr.totalpacket, cntr.totalbytes,
cntr.droppacket, cntr.dropbytes))
Expand All @@ -256,10 +263,12 @@ class Queuestat(object):
return json_output
else:
hdr = voq_header if self.voq else header
print(tabulate(table, hdr, tablefmt='simple', stralign='right'))
print()
if table:
print(port + " Last cached time was " + str(cnstat_old_dict.get('time')))
print(tabulate(table, hdr, tablefmt='simple', stralign='right'))
print()

def get_print_all_stat(self, json_opt):
def get_print_all_stat(self, json_opt, non_zero):
"""
Get stat for each port
If JSON option is True, collect data for each port and
Expand All @@ -276,22 +285,21 @@ class Queuestat(object):
cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'rb'))
if json_opt:
json_output[port].update({"cached_time":cnstat_cached_dict.get('time')})
json_output.update(self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt))
json_output.update(self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt, non_zero))
else:
print(port + " Last cached time was " + str(cnstat_cached_dict.get('time')))
self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt)
self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt, non_zero)
except IOError as e:
print(e.errno, e)
else:
if json_opt:
json_output.update(self.cnstat_print(port, cnstat_dict, json_opt))
json_output.update(self.cnstat_print(port, cnstat_dict, json_opt, non_zero))
else:
self.cnstat_print(port, cnstat_dict, json_opt)
self.cnstat_print(port, cnstat_dict, json_opt, non_zero)

if json_opt:
print(json_dump(json_output))

def get_print_port_stat(self, port, json_opt):
def get_print_port_stat(self, port, json_opt, non_zero):
"""
Get stat for the port
If JSON option is True print data in JSON format
Expand All @@ -310,17 +318,17 @@ class Queuestat(object):
cnstat_cached_dict = pickle.load(open(cnstat_fqn_file_name, 'rb'))
if json_opt:
json_output[port].update({"cached_time":cnstat_cached_dict.get('time')})
json_output.update(self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt))
json_output.update(self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt, non_zero))
else:
print("Last cached time was " + str(cnstat_cached_dict.get('time')))
self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt)
self.cnstat_diff_print(port, cnstat_dict, cnstat_cached_dict, json_opt, non_zero)
except IOError as e:
print(e.errno, e)
else:
if json_opt:
json_output.update(self.cnstat_print(port, cnstat_dict, json_opt))
json_output.update(self.cnstat_print(port, cnstat_dict, json_opt, non_zero))
else:
self.cnstat_print(port, cnstat_dict, json_opt)
self.cnstat_print(port, cnstat_dict, json_opt, non_zero)

if json_opt:
print(json_dump(json_output))
Expand Down Expand Up @@ -358,13 +366,15 @@ Examples:
parser.add_argument('-j', '--json_opt', action='store_true', help='Print in JSON format')
parser.add_argument('-V', '--voq', action='store_true', help='display voq stats')
parser.add_argument('-n','--namespace', default=None, help='Display queue counters for specific namespace')
parser.add_argument('-nz','--non_zero', action='store_true', help='Display non-zero queue counters')
args = parser.parse_args()

save_fresh_stats = args.clear
delete_stats = args.delete
json_opt = args.json_opt
voq = args.voq
namespace = args.namespace
non_zero = args.non_zero

port_to_show_stats = args.port

Expand All @@ -383,9 +393,9 @@ Examples:
sys.exit(0)

if port_to_show_stats!=None:
queuestat.get_print_port_stat(port_to_show_stats, json_opt)
queuestat.get_print_port_stat(port_to_show_stats, json_opt, non_zero)
else:
queuestat.get_print_all_stat(json_opt)
queuestat.get_print_all_stat(json_opt, non_zero)

sys.exit(0)

Expand Down
6 changes: 5 additions & 1 deletion show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ def queue():
@click.option('--verbose', is_flag=True, help="Enable verbose output")
@click.option('--json', is_flag=True, help="JSON output")
@click.option('--voq', is_flag=True, help="VOQ counters")
def counters(interfacename, namespace, display, verbose, json, voq):
@click.option('--nonzero', is_flag=True, help="Non Zero Counters")
def counters(interfacename, namespace, display, verbose, json, voq, nonzero):
"""Show queue counters"""

cmd = "queuestat"
Expand All @@ -575,6 +576,9 @@ def counters(interfacename, namespace, display, verbose, json, voq):
if voq:
cmd += " -V"

if nonzero:
cmd += " -nz"

run_command(cmd, display_cmd=verbose)

#
Expand Down
16 changes: 8 additions & 8 deletions tests/mock_tables/counters_db.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"COUNTERS:oid:0x15000000000357": {
"SAI_QUEUE_STAT_BYTES": "30",
"SAI_QUEUE_STAT_DROPPED_BYTES": "74",
"SAI_QUEUE_STAT_DROPPED_PACKETS": "56",
"SAI_QUEUE_STAT_PACKETS": "68",
"SAI_QUEUE_STAT_BYTES": "0",
"SAI_QUEUE_STAT_DROPPED_BYTES": "0",
"SAI_QUEUE_STAT_DROPPED_PACKETS": "0",
"SAI_QUEUE_STAT_PACKETS": "0",
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES": "61"
},
"COUNTERS:oid:0x15000000000358": {
Expand Down Expand Up @@ -266,10 +266,10 @@
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES": "49"
},
"COUNTERS:oid:0x150000000003a7": {
"SAI_QUEUE_STAT_BYTES": "5",
"SAI_QUEUE_STAT_DROPPED_BYTES": "56",
"SAI_QUEUE_STAT_DROPPED_PACKETS": "36",
"SAI_QUEUE_STAT_PACKETS": "19",
"SAI_QUEUE_STAT_BYTES": "0",
"SAI_QUEUE_STAT_DROPPED_BYTES": "0",
"SAI_QUEUE_STAT_DROPPED_PACKETS": "0",
"SAI_QUEUE_STAT_PACKETS": "0",
"SAI_QUEUE_STAT_SHARED_WATERMARK_BYTES": "65"
},
"COUNTERS:oid:0x150000000003a8": {
Expand Down
Loading

0 comments on commit 66ea58a

Please sign in to comment.