Skip to content

Pr json support queue watermark and persistent-watermark #3875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
21 changes: 14 additions & 7 deletions scripts/watermarkstat
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ class WatermarkstatWrapper(object):
self.db = None

@multi_asic_util.run_on_multi_asic
def run(self, clear, persistent, wm_type):
def run(self, clear, persistent, wm_type, json_output):
watermarkstat = Watermarkstat(self.db, self.multi_asic.current_namespace)
if clear:
watermarkstat.send_clear_notification(("PERSISTENT" if persistent else "USER", wm_type.upper()))
else:
table_prefix = PERSISTENT_TABLE_PREFIX if persistent else USER_TABLE_PREFIX
watermarkstat.print_all_stat(table_prefix, wm_type)
watermarkstat.print_all_stat(table_prefix, wm_type, json_output)


class Watermarkstat(object):
Expand Down Expand Up @@ -283,8 +283,9 @@ class Watermarkstat(object):
fields[pos] = str(int(counter_data))
return fields

def print_all_stat(self, table_prefix, key):
def print_all_stat(self, table_prefix, key, json_output):
table = []
json_result = []
type = self.watermark_types[key]
if key in ['buffer_pool', 'headroom_pool']:
self.header_list = type['header']
Expand All @@ -298,6 +299,7 @@ class Watermarkstat(object):
if data is None:
data = STATUS_NA
table.append((buf_pool, data))
json_result.append({buf_pool:data})
else:
self.build_header(type, key)
# Get stat for each port
Expand All @@ -309,10 +311,14 @@ class Watermarkstat(object):
row_data.append(port)
row_data.extend(data)
table.append(tuple(row_data))

json_result.append(dict(zip(self.header_list, [port]+data)))

namespace_str = f" (Namespace {self.namespace})" if multi_asic.is_multi_asic() else ''
print(type["message"] + namespace_str)
print(tabulate(table, self.header_list, tablefmt='simple', stralign='right'))
if json_output:
print(json.dumps(json_result, indent=4))
else:
print(tabulate(table, self.header_list, tablefmt='simple', stralign='right'))

def send_clear_notification(self, data):
msg = json.dumps(data, separators=(',', ':'))
Expand All @@ -324,8 +330,9 @@ class Watermarkstat(object):
@click.option('-p', '--persistent', is_flag=True, help='Do the operations on the persistent watermark')
@click.option('-t', '--type', 'wm_type', type=click.Choice(['pg_headroom', 'pg_shared', 'q_shared_uni', 'q_shared_multi', 'buffer_pool', 'headroom_pool', 'q_shared_all']), help='The type of watermark', required=True)
@click.option('-n', '--namespace', type=click.Choice(multi_asic.get_namespace_list()), help='Namespace name or skip for all', default=None)
@click.option('--json','-j','json_output', is_flag=True, default=False, show_default=True, help="Display output in JSON format")
Copy link
Preview

Copilot AI May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using a consistent help message for the JSON flag across all commands. For example, align this with the 'Display JSON output' help text used elsewhere.

Suggested change
@click.option('--json','-j','json_output', is_flag=True, default=False, show_default=True, help="Display output in JSON format")
@click.option('--json','-j','json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")

Copilot uses AI. Check for mistakes.

@click.version_option(version='1.0')
def main(clear, persistent, wm_type, namespace):
def main(clear, persistent, wm_type, namespace, json_output=False):
"""
Display the watermark counters

Expand All @@ -347,7 +354,7 @@ def main(clear, persistent, wm_type, namespace):
"""

namespace_context = WatermarkstatWrapper(namespace)
namespace_context.run(clear, persistent, wm_type)
namespace_context.run(clear, persistent, wm_type, json_output)
sys.exit(0)

if __name__ == "__main__":
Expand Down
30 changes: 24 additions & 6 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,14 @@ def watermark():
show_default=True,
help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def wm_q_uni(namespace):
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
def wm_q_uni(namespace, json_output):
"""Show user WM for unicast queues"""
command = ['watermarkstat', '-t', 'q_shared_uni']
if namespace is not None:
command += ['-n', str(namespace)]
if json_output:
command += ["-j"]
run_command(command)

# 'multicast' subcommand ("show queue watermarks multicast")
Expand All @@ -852,11 +855,14 @@ def wm_q_uni(namespace):
show_default=True,
help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def wm_q_multi(namespace):
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
def wm_q_multi(namespace, json_output):
"""Show user WM for multicast queues"""
command = ['watermarkstat', '-t', 'q_shared_multi']
if namespace is not None:
command += ['-n', str(namespace)]
if json_output:
command += ["-j"]
run_command(command)

# 'all' subcommand ("show queue watermarks all")
Expand All @@ -869,11 +875,14 @@ def wm_q_multi(namespace):
show_default=True,
help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def wm_q_all(namespace):
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
def wm_q_all(namespace, json_output):
"""Show user WM for all queues"""
command = ['watermarkstat', '-t', 'q_shared_all']
if namespace is not None:
command += ['-n', str(namespace)]
if json_output:
command += ["-j"]
run_command(command)

#
Expand All @@ -895,11 +904,14 @@ def persistent_watermark():
show_default=True,
help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def pwm_q_uni(namespace):
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
def pwm_q_uni(namespace, json_output):
"""Show persistent WM for unicast queues"""
command = ['watermarkstat', '-p', '-t', 'q_shared_uni']
if namespace is not None:
command += ['-n', str(namespace)]
if json_output:
command += ["-j"]
run_command(command)

# 'multicast' subcommand ("show queue persistent-watermarks multicast")
Expand All @@ -912,11 +924,14 @@ def pwm_q_uni(namespace):
show_default=True,
help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def pwm_q_multi(namespace):
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
def pwm_q_multi(namespace, json_output):
"""Show persistent WM for multicast queues"""
command = ['watermarkstat', '-p', '-t', 'q_shared_multi']
if namespace is not None:
command += ['-n', str(namespace)]
if json_output:
command += ["-j"]
run_command(command)

# 'all' subcommand ("show queue persistent-watermarks all")
Expand All @@ -929,11 +944,14 @@ def pwm_q_multi(namespace):
show_default=True,
help='Namespace name or all',
callback=multi_asic_util.multi_asic_namespace_validation_callback)
def pwm_q_all(namespace):
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
def pwm_q_all(namespace, json_output):
"""Show persistent WM for all queues"""
command = ['watermarkstat', '-p', '-t', 'q_shared_all']
if namespace is not None:
command += ['-n', str(namespace)]
if json_output:
command += ["-j"]
run_command(command)

#
Expand Down
Loading