diff --git a/clear/main.py b/clear/main.py index c9400097a5d9..26e9e22f32ef 100755 --- a/clear/main.py +++ b/clear/main.py @@ -216,6 +216,20 @@ def clear_wm_pg_shared(): command = 'watermarkstat -c -t pg_shared' run_command(command) +@priority_group.group() +def drop(): + """Clear priority-group dropped packets stats""" + pass + +@drop.command('counters') +def clear_pg_counters(): + """Clear priority-group dropped packets counter """ + + if os.geteuid() != 0 and os.environ.get("UTILITIES_UNIT_TESTING", "0") != "2": + exit("Root privileges are required for this operation") + command = 'pg-drop -c clear' + run_command(command) + @priority_group.group(name='persistent-watermark') def persistent_watermark(): """Clear queue persistent WM. One does not simply clear WM, root is required""" diff --git a/doc/Command-Reference.md b/doc/Command-Reference.md index 172e103273f6..ffbc0c26f434 100644 --- a/doc/Command-Reference.md +++ b/doc/Command-Reference.md @@ -6153,11 +6153,14 @@ This command displays the user watermark for the queues (Egress shared pool occu **show priority-group** -This command displays the user watermark or persistent-watermark for the Ingress "headroom" or "shared pool occupancy" per priority-group for all ports +This command displays: +1) The user watermark or persistent-watermark for the Ingress "headroom" or "shared pool occupancy" per priority-group for all ports. +2) Dropped packets per priority-group for all ports - Usage: ``` show priority-group (watermark | persistent-watermark) (headroom | shared) + show priority-group drop counters ``` - Example: @@ -6187,6 +6190,18 @@ This command displays the user watermark or persistent-watermark for the Ingress admin@sonic:~$ show priority-group persistent-watermark headroom ``` +- Example (Ingress dropped packets per PG): + ``` + admin@sonic:~$ show priority-group drop counters + Ingress PG dropped packets: + Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 + ----------- ----- ----- ----- ----- ----- ----- ----- ----- + Ethernet0 0 0 0 0 0 0 0 0 + Ethernet4 0 0 0 0 0 0 0 0 + Ethernet8 0 0 0 0 0 0 0 0 + Ethernet12 0 0 0 0 0 0 0 0 + ``` + In addition to user watermark("show queue|priority-group watermark ..."), a persistent watermark is available. It hold values independently of user watermark. This way user can use "user watermark" for debugging, clear it, etc, but the "persistent watermark" will not be affected. @@ -6216,7 +6231,7 @@ This command displays the user persistet-watermark for the queues (Egress shared admin@sonic:~$ show queue persistent-watermark multicast ``` -- NOTE: Both "user watermark" and "persistent watermark" can be cleared by user: +- NOTE: "user watermark", "persistent watermark" and "ingress dropped packets" can be cleared by user: ``` admin@sonic:~$ sonic-clear queue persistent-watermark unicast @@ -6226,6 +6241,8 @@ This command displays the user persistet-watermark for the queues (Egress shared admin@sonic:~$ sonic-clear priority-group persistent-watermark shared admin@sonic:~$ sonic-clear priority-group persistent-watermark headroom + + admin@sonic:~$ sonic-clear priority-group drop counters ``` #### Buffer Pool diff --git a/scripts/pg-drop b/scripts/pg-drop new file mode 100755 index 000000000000..ae891c50b59c --- /dev/null +++ b/scripts/pg-drop @@ -0,0 +1,250 @@ +#!/usr/bin/env python3 + +##################################################################### +# +# pg-drop is a tool for show/clear ingress pg dropped packet stats. +# +##################################################################### +import _pickle as pickle +import argparse +import os +import sys +from collections import OrderedDict + +from natsort import natsorted +from tabulate import tabulate + +# mock the redis for unit test purposes # +try: + if os.environ["UTILITIES_UNIT_TESTING"] == "2": + modules_path = os.path.join(os.path.dirname(__file__), "..") + tests_path = os.path.join(modules_path, "tests") + sys.path.insert(0, modules_path) + sys.path.insert(0, tests_path) + import mock_tables.dbconnector + +except KeyError: + pass + +from swsscommon.swsscommon import SonicV2Connector + +STATUS_NA = 'N/A' + +COUNTER_TABLE_PREFIX = "COUNTERS:" + +COUNTERS_PORT_NAME_MAP = "COUNTERS_PORT_NAME_MAP" +COUNTERS_PG_NAME_MAP = "COUNTERS_PG_NAME_MAP" +COUNTERS_PG_PORT_MAP = "COUNTERS_PG_PORT_MAP" +COUNTERS_PG_INDEX_MAP = "COUNTERS_PG_INDEX_MAP" + +def get_dropstat_dir(): + dropstat_dir_prefix = '/tmp/dropstat' + return "{}-{}/".format(dropstat_dir_prefix, os.getuid()) + +class PgDropStat(object): + + def __init__(self): + self.counters_db = SonicV2Connector(host='127.0.0.1') + self.counters_db.connect(self.counters_db.COUNTERS_DB) + + dropstat_dir = get_dropstat_dir() + self.port_drop_stats_file = os.path.join(dropstat_dir, 'pg_drop_stats') + + def get_port_id(oid): + """ + Get port ID using object ID + """ + port_id = self.counters_db.get(self.counters_db.COUNTERS_DB, COUNTERS_PG_PORT_MAP, oid) + if port_id is None: + print("Port is not available for oid '{}'".format(oid), file=sys.stderr) + sys.exit(1) + return port_id + + # Get all ports + self.counter_port_name_map = self.counters_db.get_all(self.counters_db.COUNTERS_DB, COUNTERS_PORT_NAME_MAP) + if self.counter_port_name_map is None: + print("COUNTERS_PORT_NAME_MAP is empty!", file=sys.stderr) + sys.exit(1) + + self.port_pg_map = {} + self.port_name_map = {} + + for port in self.counter_port_name_map: + self.port_pg_map[port] = {} + self.port_name_map[self.counter_port_name_map[port]] = port + + # Get PGs for each port + counter_pg_name_map = self.counters_db.get_all(self.counters_db.COUNTERS_DB, COUNTERS_PG_NAME_MAP) + if counter_pg_name_map is None: + print("COUNTERS_PG_NAME_MAP is empty!", file=sys.stderr) + sys.exit(1) + + for pg in counter_pg_name_map: + port = self.port_name_map[get_port_id(counter_pg_name_map[pg])] + self.port_pg_map[port][pg] = counter_pg_name_map[pg] + + self.pg_drop_types = { + "pg_drop" : {"message" : "Ingress PG dropped packets:", + "obj_map" : self.port_pg_map, + "idx_func": self.get_pg_index, + "counter_name" : "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS", + "header_prefix": "PG"}, + } + + def get_pg_index(self, oid): + """ + return PG index (0-7) + + oid - object ID for entry in redis + """ + pg_index = self.counters_db.get(self.counters_db.COUNTERS_DB, COUNTERS_PG_INDEX_MAP, oid) + if pg_index is None: + print("Priority group index is not available for oid '{}'".format(table_id), file=sys.stderr) + sys.exit(1) + return pg_index + + def build_header(self, pg_drop_type): + """ + Construct header for table with PG counters + """ + if pg_drop_type is None: + print("Header info is not available!", file=sys.stderr) + sys.exit(1) + + self.header_list = ['Port'] + header_map = pg_drop_type["obj_map"] + single_key = list(header_map.keys())[0] + header_len = len(header_map[single_key]) + min_idx = sys.maxsize + + for name, counter_oid in header_map[single_key].items(): + curr_idx = int(pg_drop_type["idx_func"](counter_oid)) + min_idx = min(min_idx, curr_idx) + + self.min_idx = min_idx + self.header_list += ["{}{}".format(pg_drop_type["header_prefix"], idx) for idx in range(self.min_idx, self.min_idx + header_len)] + + def get_counters(self, table_prefix, port_obj, idx_func, counter_name): + """ + Get the counters of a specific table. + """ + port_drop_ckpt = {} + # Grab the latest clear checkpoint, if it exists + if os.path.isfile(self.port_drop_stats_file): + port_drop_ckpt = pickle.load(open(self.port_drop_stats_file, 'rb')) + + # Header list contains the port name followed by the PGs. Fields is used to populate the pg values + fields = ["0"]* (len(self.header_list) - 1) + + for name, obj_id in port_obj.items(): + full_table_id = table_prefix + obj_id + old_collected_data = port_drop_ckpt.get(name,{})[full_table_id] if len(port_drop_ckpt) > 0 else 0 + idx = int(idx_func(obj_id)) + pos = idx - self.min_idx + counter_data = self.counters_db.get(self.counters_db.COUNTERS_DB, full_table_id, counter_name) + if counter_data is None: + fields[pos] = STATUS_NA + elif fields[pos] != STATUS_NA: + fields[pos] = str(int(counter_data) - old_collected_data) + return fields + + def print_all_stat(self, table_prefix, key): + """ + Print table that show stats per PG + """ + table = [] + type = self.pg_drop_types[key] + self.build_header(type) + # Get stat for each port + for port in natsorted(self.counter_port_name_map): + row_data = list() + data = self.get_counters(table_prefix, type["obj_map"][port], type["idx_func"], type["counter_name"]) + row_data.append(port) + row_data.extend(data) + table.append(tuple(row_data)) + + print(type["message"]) + print(tabulate(table, self.header_list, tablefmt='simple', stralign='right')) + + def get_counts(self, counters, oid): + """ + Get the PG drop counts for an individual counter. + """ + counts = {} + table_id = COUNTER_TABLE_PREFIX + oid + for counter in counters: + counter_data = self.counters_db.get(self.counters_db.COUNTERS_DB, table_id, counter) + if counter_data is None: + counts[table_id] = 0 + else: + counts[table_id] = int(counter_data) + return counts + + def get_counts_table(self, counters, object_table): + """ + Returns a dictionary containing a mapping from an object (like a port) + to its PG drop counts. Counts are contained in a dictionary that maps + counter oid to its counts. + """ + counter_object_name_map = self.counters_db.get_all(self.counters_db.COUNTERS_DB, object_table) + current_stat_dict = OrderedDict() + + if counter_object_name_map is None: + return current_stat_dict + + for obj in natsorted(counter_object_name_map): + current_stat_dict[obj] = self.get_counts(counters, counter_object_name_map[obj]) + return current_stat_dict + + def clear_drop_counts(self): + """ + Clears the current PG drop counter. + """ + + counter_pg_drop_array = [ "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS"] + try: + pickle.dump(self.get_counts_table( + counter_pg_drop_array, + COUNTERS_PG_NAME_MAP), + open(self.port_drop_stats_file, 'wb+')) + except IOError as e: + print(e) + sys.exit(e.errno) + print("Cleared PG drop counter") + +def main(): + parser = argparse.ArgumentParser(description='Display PG drop counter', + formatter_class=argparse.RawTextHelpFormatter, + epilog=""" +Examples: +pg-drop -c show +pg-drop -c clear +""") + + parser.add_argument('-c', '--command', type=str, help='Desired action to perform') + + args = parser.parse_args() + command = args.command + + dropstat_dir = get_dropstat_dir() + # Create the directory to hold clear results + if not os.path.exists(dropstat_dir): + try: + os.makedirs(dropstat_dir) + except IOError as e: + print(e) + sys.exit(e.errno) + + pgdropstat = PgDropStat() + + if command == 'clear': + pgdropstat.clear_drop_counts() + elif command == 'show': + pgdropstat.print_all_stat(COUNTER_TABLE_PREFIX, "pg_drop" ) + else: + print("Command not recognized") + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/show/main.py b/show/main.py index 4a1cc0d855b3..5fba9d828a22 100644 --- a/show/main.py +++ b/show/main.py @@ -605,6 +605,17 @@ def wm_pg_shared(): command = 'watermarkstat -t pg_shared' run_command(command) +@priority_group.group() +def drop(): + """Show priority-group""" + pass + +@drop.command('counters') +def pg_drop_counters(): + """Show dropped packets for priority-group""" + command = 'pg-drop -c show' + run_command(command) + @priority_group.group(name='persistent-watermark') def persistent_watermark(): """Show priority-group persistent WM""" diff --git a/tests/mock_tables/counters_db.json b/tests/mock_tables/counters_db.json index 0a06fc22d965..41396c4fa412 100644 --- a/tests/mock_tables/counters_db.json +++ b/tests/mock_tables/counters_db.json @@ -294,6 +294,80 @@ "SAI_SWITCH_STAT_OUT_DROP_REASON_RANGE_BASE": "1000", "SAI_SWITCH_STAT_OUT_CONFIGURED_DROP_REASONS_1_DROPPED_PKTS": "0" }, + + "COUNTERS:oid:0x1a00000000034f": { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "800" + }, + "COUNTERS:oid:0x1a000000000350" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "801" + }, + "COUNTERS:oid:0x1a000000000351" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "802" + }, + "COUNTERS:oid:0x1a000000000352" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "803" + }, + "COUNTERS:oid:0x1a000000000353" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "804" + }, + "COUNTERS:oid:0x1a000000000354" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "805" + }, + "COUNTERS:oid:0x1a000000000355" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "806" + }, + "COUNTERS:oid:0x1a000000000356" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "807" + }, + "COUNTERS:oid:0x1a000000000377" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "400" + }, + "COUNTERS:oid:0x1a000000000378" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "401" + }, + "COUNTERS:oid:0x1a000000000379" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "402" + }, + "COUNTERS:oid:0x1a00000000037a" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "403" + }, + "COUNTERS:oid:0x1a00000000037b" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "404" + }, + "COUNTERS:oid:0x1a00000000037c" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "405" + }, + "COUNTERS:oid:0x1a00000000037d" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "406" + }, + "COUNTERS:oid:0x1a00000000037e" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "407" + }, + "COUNTERS:oid:0x1a00000000039f" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "100" + }, + "COUNTERS:oid:0x1a0000000003a0" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "101" + }, + "COUNTERS:oid:0x1a0000000003a1" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "102" + }, + "COUNTERS:oid:0x1a0000000003a2" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "103" + }, + "COUNTERS:oid:0x1a0000000003a3" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "104" + }, + "COUNTERS:oid:0x1a0000000003a4" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "105" + }, + "COUNTERS:oid:0x1a0000000003a5" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "106" + }, + "COUNTERS:oid:0x1a0000000003a6" : { + "SAI_INGRESS_PRIORITY_GROUP_STAT_DROPPED_PACKETS" : "107" + }, + "COUNTERS_PORT_NAME_MAP": { "Ethernet0": "oid:0x1000000000012", "Ethernet4": "oid:0x1000000000013", diff --git a/tests/pgdropstat_test.py b/tests/pgdropstat_test.py new file mode 100644 index 000000000000..e896ded81174 --- /dev/null +++ b/tests/pgdropstat_test.py @@ -0,0 +1,72 @@ +import os +import sys + +import show.main as show +import clear.main as clear + +from click.testing import CliRunner + +test_path = os.path.dirname(os.path.abspath(__file__)) +modules_path = os.path.dirname(test_path) +scripts_path = os.path.join(modules_path, "scripts") +sys.path.insert(0, test_path) +sys.path.insert(0, modules_path) + + +show_pg_dropped_packet_stat="""\ +Ingress PG dropped packets: + Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 +--------- ----- ----- ----- ----- ----- ----- ----- ----- +Ethernet0 800 801 802 803 804 805 806 807 +Ethernet4 400 401 402 403 404 405 406 407 +Ethernet8 100 101 102 103 104 105 106 107 +""" + +show_cleared_pg_dropped_packet_stat="""\ +Ingress PG dropped packets: + Port PG0 PG1 PG2 PG3 PG4 PG5 PG6 PG7 +--------- ----- ----- ----- ----- ----- ----- ----- ----- +Ethernet0 0 0 0 0 0 0 0 0 +Ethernet4 0 0 0 0 0 0 0 0 +Ethernet8 0 0 0 0 0 0 0 0 +""" + +class TestPgDropstat(object): + @classmethod + def setup_class(cls): + os.environ["PATH"] += os.pathsep + scripts_path + os.environ['UTILITIES_UNIT_TESTING'] = "2" + print("SETUP") + + def test_show_pg_drop_show(self): + self.executor(clear_before_show = False) + + def test_show_pg_drop_clear(self): + self.executor(clear_before_show = True) + + def executor(self, clear_before_show): + runner = CliRunner() + show_output = show_pg_dropped_packet_stat + + # Clear stats + if clear_before_show: + result = runner.invoke(clear.cli.commands["priority-group"].commands["drop"].commands["counters"], []) + assert result.exit_code == 0 + show_output = show_cleared_pg_dropped_packet_stat + + result = runner.invoke(show.cli.commands["priority-group"].commands["drop"].commands["counters"], []) + + print(result.exit_code) + print(result.output) + + assert result.exit_code == 0 + assert result.output == show_output + + @classmethod + def teardown_class(cls): + os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1]) + os.environ['UTILITIES_UNIT_TESTING'] = "0" + dropstat_dir_prefix = '/tmp/dropstat' + dir_path = "{}-{}/".format(dropstat_dir_prefix, os.getuid()) + os.system("rm -rf {}".format(dir_path)) + print("TEARDOWN")