Skip to content

Commit 868189c

Browse files
authored
Pr json support queue and priority-group watermark and persistent-watermark (sonic-net#3875)
What I did Added json output supoort for admin@sonic:~$ show queue watermark unicast --help Usage: show queue watermark unicast [OPTIONS] Show user WM for unicast queues Options: -j, --json Display1 output in JSON format [default: False] -?, -h, --help Show this message and exit. admin@sonic:~$ show queue watermark multicast --help Usage: show queue watermark multicast [OPTIONS] Show user WM for multicast queues Options: -j, --json Display1 output in JSON format [default: False] -?, -h, --help Show this message and exit.
1 parent 5347757 commit 868189c

4 files changed

Lines changed: 844 additions & 244 deletions

File tree

scripts/watermarkstat

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ class WatermarkstatWrapper(object):
8181
self.db = None
8282

8383
@multi_asic_util.run_on_multi_asic
84-
def run(self, clear, persistent, wm_type):
84+
def run(self, clear, persistent, wm_type, json_output):
8585
watermarkstat = Watermarkstat(self.db, self.multi_asic.current_namespace)
8686
if clear:
8787
watermarkstat.send_clear_notification(("PERSISTENT" if persistent else "USER", wm_type.upper()))
8888
else:
8989
table_prefix = PERSISTENT_TABLE_PREFIX if persistent else USER_TABLE_PREFIX
90-
watermarkstat.print_all_stat(table_prefix, wm_type)
90+
watermarkstat.print_all_stat(table_prefix, wm_type, json_output)
9191

9292

9393
class Watermarkstat(object):
@@ -283,8 +283,9 @@ class Watermarkstat(object):
283283
fields[pos] = str(int(counter_data))
284284
return fields
285285

286-
def print_all_stat(self, table_prefix, key):
286+
def print_all_stat(self, table_prefix, key, json_output):
287287
table = []
288+
json_result = []
288289
type = self.watermark_types[key]
289290
if key in ['buffer_pool', 'headroom_pool']:
290291
self.header_list = type['header']
@@ -298,6 +299,7 @@ class Watermarkstat(object):
298299
if data is None:
299300
data = STATUS_NA
300301
table.append((buf_pool, data))
302+
json_result.append({buf_pool:data})
301303
else:
302304
self.build_header(type, key)
303305
# Get stat for each port
@@ -309,10 +311,14 @@ class Watermarkstat(object):
309311
row_data.append(port)
310312
row_data.extend(data)
311313
table.append(tuple(row_data))
312-
313-
namespace_str = f" (Namespace {self.namespace})" if multi_asic.is_multi_asic() else ''
314-
print(type["message"] + namespace_str)
315-
print(tabulate(table, self.header_list, tablefmt='simple', stralign='right'))
314+
json_result.append(dict(zip(self.header_list, [port]+data)))
315+
316+
if json_output:
317+
print(json.dumps(json_result, indent=4))
318+
else:
319+
namespace_str = f" (Namespace {self.namespace})" if multi_asic.is_multi_asic() else ''
320+
print(type["message"] + namespace_str)
321+
print(tabulate(table, self.header_list, tablefmt='simple', stralign='right'))
316322

317323
def send_clear_notification(self, data):
318324
msg = json.dumps(data, separators=(',', ':'))
@@ -324,8 +330,9 @@ class Watermarkstat(object):
324330
@click.option('-p', '--persistent', is_flag=True, help='Do the operations on the persistent watermark')
325331
@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)
326332
@click.option('-n', '--namespace', type=click.Choice(multi_asic.get_namespace_list()), help='Namespace name or skip for all', default=None)
333+
@click.option('--json','-j','json_output', is_flag=True, default=False, show_default=True, help="Display output in JSON format")
327334
@click.version_option(version='1.0')
328-
def main(clear, persistent, wm_type, namespace):
335+
def main(clear, persistent, wm_type, namespace, json_output=False):
329336
"""
330337
Display the watermark counters
331338
@@ -347,7 +354,7 @@ def main(clear, persistent, wm_type, namespace):
347354
"""
348355

349356
namespace_context = WatermarkstatWrapper(namespace)
350-
namespace_context.run(clear, persistent, wm_type)
357+
namespace_context.run(clear, persistent, wm_type, json_output)
351358
sys.exit(0)
352359

353360
if __name__ == "__main__":

show/main.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,14 @@ def watermark():
842842
show_default=True,
843843
help='Namespace name or all',
844844
callback=multi_asic_util.multi_asic_namespace_validation_callback)
845-
def wm_q_uni(namespace):
845+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
846+
def wm_q_uni(namespace, json_output):
846847
"""Show user WM for unicast queues"""
847848
command = ['watermarkstat', '-t', 'q_shared_uni']
848849
if namespace is not None:
849850
command += ['-n', str(namespace)]
851+
if json_output:
852+
command += ["-j"]
850853
run_command(command)
851854

852855
# 'multicast' subcommand ("show queue watermarks multicast")
@@ -859,11 +862,14 @@ def wm_q_uni(namespace):
859862
show_default=True,
860863
help='Namespace name or all',
861864
callback=multi_asic_util.multi_asic_namespace_validation_callback)
862-
def wm_q_multi(namespace):
865+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
866+
def wm_q_multi(namespace, json_output):
863867
"""Show user WM for multicast queues"""
864868
command = ['watermarkstat', '-t', 'q_shared_multi']
865869
if namespace is not None:
866870
command += ['-n', str(namespace)]
871+
if json_output:
872+
command += ["-j"]
867873
run_command(command)
868874

869875
# 'all' subcommand ("show queue watermarks all")
@@ -876,11 +882,14 @@ def wm_q_multi(namespace):
876882
show_default=True,
877883
help='Namespace name or all',
878884
callback=multi_asic_util.multi_asic_namespace_validation_callback)
879-
def wm_q_all(namespace):
885+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
886+
def wm_q_all(namespace, json_output):
880887
"""Show user WM for all queues"""
881888
command = ['watermarkstat', '-t', 'q_shared_all']
882889
if namespace is not None:
883890
command += ['-n', str(namespace)]
891+
if json_output:
892+
command += ["-j"]
884893
run_command(command)
885894

886895
#
@@ -902,11 +911,14 @@ def persistent_watermark():
902911
show_default=True,
903912
help='Namespace name or all',
904913
callback=multi_asic_util.multi_asic_namespace_validation_callback)
905-
def pwm_q_uni(namespace):
914+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
915+
def pwm_q_uni(namespace, json_output):
906916
"""Show persistent WM for unicast queues"""
907917
command = ['watermarkstat', '-p', '-t', 'q_shared_uni']
908918
if namespace is not None:
909919
command += ['-n', str(namespace)]
920+
if json_output:
921+
command += ["-j"]
910922
run_command(command)
911923

912924
# 'multicast' subcommand ("show queue persistent-watermarks multicast")
@@ -919,11 +931,14 @@ def pwm_q_uni(namespace):
919931
show_default=True,
920932
help='Namespace name or all',
921933
callback=multi_asic_util.multi_asic_namespace_validation_callback)
922-
def pwm_q_multi(namespace):
934+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
935+
def pwm_q_multi(namespace, json_output):
923936
"""Show persistent WM for multicast queues"""
924937
command = ['watermarkstat', '-p', '-t', 'q_shared_multi']
925938
if namespace is not None:
926939
command += ['-n', str(namespace)]
940+
if json_output:
941+
command += ["-j"]
927942
run_command(command)
928943

929944
# 'all' subcommand ("show queue persistent-watermarks all")
@@ -936,11 +951,14 @@ def pwm_q_multi(namespace):
936951
show_default=True,
937952
help='Namespace name or all',
938953
callback=multi_asic_util.multi_asic_namespace_validation_callback)
939-
def pwm_q_all(namespace):
954+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
955+
def pwm_q_all(namespace, json_output):
940956
"""Show persistent WM for all queues"""
941957
command = ['watermarkstat', '-p', '-t', 'q_shared_all']
942958
if namespace is not None:
943959
command += ['-n', str(namespace)]
960+
if json_output:
961+
command += ["-j"]
944962
run_command(command)
945963

946964
#
@@ -965,11 +983,14 @@ def watermark():
965983
show_default=True,
966984
help='Namespace name or all',
967985
callback=multi_asic_util.multi_asic_namespace_validation_callback)
968-
def wm_pg_headroom(namespace):
986+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
987+
def wm_pg_headroom(namespace, json_output):
969988
"""Show user headroom WM for pg"""
970989
command = ['watermarkstat', '-t', 'pg_headroom']
971990
if namespace is not None:
972991
command += ['-n', str(namespace)]
992+
if json_output:
993+
command += ["-j"]
973994
run_command(command)
974995

975996
@watermark.command('shared')
@@ -981,11 +1002,14 @@ def wm_pg_headroom(namespace):
9811002
show_default=True,
9821003
help='Namespace name or all',
9831004
callback=multi_asic_util.multi_asic_namespace_validation_callback)
984-
def wm_pg_shared(namespace):
1005+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
1006+
def wm_pg_shared(namespace, json_output):
9851007
"""Show user shared WM for pg"""
9861008
command = ['watermarkstat', '-t', 'pg_shared']
9871009
if namespace is not None:
9881010
command += ['-n', str(namespace)]
1011+
if json_output:
1012+
command += ["-j"]
9891013
run_command(command)
9901014

9911015
@priority_group.group()
@@ -1016,11 +1040,14 @@ def persistent_watermark():
10161040
show_default=True,
10171041
help='Namespace name or all',
10181042
callback=multi_asic_util.multi_asic_namespace_validation_callback)
1019-
def pwm_pg_headroom(namespace):
1043+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
1044+
def pwm_pg_headroom(namespace, json_output):
10201045
"""Show persistent headroom WM for pg"""
10211046
command = ['watermarkstat', '-p', '-t', 'pg_headroom']
10221047
if namespace is not None:
10231048
command += ['-n', str(namespace)]
1049+
if json_output:
1050+
command += ["-j"]
10241051
run_command(command)
10251052

10261053

@@ -1033,11 +1060,14 @@ def pwm_pg_headroom(namespace):
10331060
show_default=True,
10341061
help='Namespace name or all',
10351062
callback=multi_asic_util.multi_asic_namespace_validation_callback)
1036-
def pwm_pg_shared(namespace):
1063+
@click.option('--json', '-j', 'json_output', is_flag=True, default=False, show_default=True, help="Display JSON output")
1064+
def pwm_pg_shared(namespace, json_output):
10371065
"""Show persistent shared WM for pg"""
10381066
command = ['watermarkstat', '-p', '-t', 'pg_shared']
10391067
if namespace is not None:
10401068
command += ['-n', str(namespace)]
1069+
if json_output:
1070+
command += ["-j"]
10411071
run_command(command)
10421072

10431073

tests/watermarkstat_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,36 @@ def setup_class(cls):
3131
def test_show_pg_shared_wm(self):
3232
self.executor(testData['show_pg_wm_shared'])
3333

34+
def test_show_pg_shared_wm_json(self):
35+
self.executor(testData['show_pg_wm_shared_json'])
36+
3437
def test_show_pg_headroom_wm(self):
3538
self.executor(testData['show_pg_wm_hdrm'])
3639

40+
def test_show_pg_headroom_wm_json(self):
41+
self.executor(testData['show_pg_wm_hdrm_json'])
42+
3743
def test_show_queue_unicast_wm(self):
3844
self.executor(testData['show_q_wm_unicast'])
3945

46+
def test_show_queue_unicast_wm_json(self):
47+
self.executor(testData['show_q_wm_unicast_json'])
48+
4049
def test_show_queue_multicast_wm(self):
4150
self.executor(testData['show_q_wm_multicast'])
4251

52+
def test_show_queue_multicast_wm_json(self):
53+
self.executor(testData['show_q_wm_multicast_json'])
54+
4355
def test_show_queue_multicast_wm_neg(self, q_multicast_wm_neg):
4456
self.executor(testData['show_q_wm_multicast_neg'])
4557

4658
def test_show_queue_all_wm(self):
4759
self.executor(testData['show_q_wm_all'])
4860

61+
def test_show_queue_all_wm_json(self):
62+
self.executor(testData['show_q_wm_all_json'])
63+
4964
def test_show_buffer_pool_wm(self):
5065
self.executor(testData['show_buffer_pool_wm'])
5166

@@ -55,18 +70,33 @@ def test_show_headroom_pool_wm(self):
5570
def test_show_pg_shared_peristent_wm(self):
5671
self.executor(testData['show_pg_pwm_shared'])
5772

73+
def test_show_pg_shared_peristent_wm_json(self):
74+
self.executor(testData['show_pg_pwm_shared_json'])
75+
5876
def test_show_pg_headroom_persistent_wm(self):
5977
self.executor(testData['show_pg_pwm_hdrm'])
6078

79+
def test_show_pg_headroom_persistent_wm_json(self):
80+
self.executor(testData['show_pg_pwm_hdrm_json'])
81+
6182
def test_show_queue_unicast_persistent_wm(self):
6283
self.executor(testData['show_q_pwm_unicast'])
6384

85+
def test_show_queue_unicast_persistent_wm_json(self):
86+
self.executor(testData['show_q_pwm_unicast_json'])
87+
6488
def test_show_queue_multicast_persistent_wm(self):
6589
self.executor(testData['show_q_pwm_multicast'])
6690

91+
def test_show_queue_multicast_persistent_wm_json(self):
92+
self.executor(testData['show_q_pwm_multicast_json'])
93+
6794
def test_show_queue_all_persistent_wm(self):
6895
self.executor(testData['show_q_pwm_all'])
6996

97+
def test_show_queue_all_persistent_wm_json(self):
98+
self.executor(testData['show_q_pwm_all_json'])
99+
70100
def test_show_buffer_pool_persistent_wm(self):
71101
self.executor(testData['show_buffer_pool_pwm'])
72102

0 commit comments

Comments
 (0)