diff --git a/scripts/dropstat b/scripts/dropstat index 6a54217a70..90ebdbc07b 100755 --- a/scripts/dropstat +++ b/scripts/dropstat @@ -105,8 +105,8 @@ class DropStat(object): dropstat_dir = get_dropstat_dir() self.port_drop_stats_file = os.path.join(dropstat_dir, 'port-stats') - self.switch_drop_stats_file = os.path.join(dropstat_dir + 'switch-stats') - self.switch_std_drop_stats_file = os.path.join(dropstat_dir, 'switch-std-drop-stats') + self.switch_drop_stats_file = os.path.join(dropstat_dir, 'switch-stats') + self.switch_std_drop_stats_file = os.path.join(dropstat_dir, 'switch-std-drop-stats') self.stat_lookup = {} self.reverse_stat_lookup = {} @@ -128,16 +128,36 @@ class DropStat(object): Clears the current drop counts. """ - try: - json.dump(self.get_counts_table(self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP), COUNTERS_PORT_NAME_MAP), - open(self.port_drop_stats_file, 'w+')) + counters_port_drop = {} + counters_switch_drop = {} + counters_switch_std_drop = {} + for ns in self.namespaces: + self.config_db = multi_asic.connect_config_db_for_ns(ns) + self.db = multi_asic.connect_to_all_dbs_for_ns(ns) + + counts = self.get_counts_table(self.gather_counters(std_port_rx_counters + std_port_tx_counters, DEBUG_COUNTER_PORT_STAT_MAP), COUNTERS_PORT_NAME_MAP) + if counts: + counters_port_drop.update(counts) + counters = self.gather_counters([], DEBUG_COUNTER_SWITCH_STAT_MAP) if counters: - json.dump(self.get_counts(counters, self.get_switch_id()), open(self.switch_drop_stats_file, 'w+')) + counts = self.get_counts(counters, self.get_switch_id()) + counters_switch_drop.update(counts) counters = self.get_configured_counters(DEBUG_COUNTER_SWITCH_STAT_MAP, True) if counters: - json.dump(self.get_counts(counters, self.get_switch_id()), open(self.switch_std_drop_stats_file, 'w+')) + counts = self.get_counts(counters, self.get_switch_id()) + counters_switch_std_drop.update(counts) + + try: + if counters_port_drop: + json.dump(counters_port_drop, open(self.port_drop_stats_file, 'w+')) + + if counters_switch_drop: + json.dump(counters_switch_drop, open(self.switch_drop_stats_file, 'w+')) + + if counters_switch_std_drop: + json.dump(counters_switch_std_drop, open(self.switch_std_drop_stats_file, 'w+')) except IOError as e: print(e) sys.exit(e.errno) diff --git a/tests/multi_asic_dropstat_test.py b/tests/multi_asic_dropstat_test.py index 13572b7f3c..4d3f89f1d1 100644 --- a/tests/multi_asic_dropstat_test.py +++ b/tests/multi_asic_dropstat_test.py @@ -11,7 +11,7 @@ dropstat_path = "/tmp/dropstat-27" -dropstat_masic_result = """\ +dropstat_masic_result_asic0 = """\ IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 ------------ ------- -------- ---------- -------- ---------- --------- --------- Ethernet0 U 10 100 0 0 80 20 @@ -24,6 +24,38 @@ sonic_drops_test 1000 """ +dropstat_masic_result_asic1 = """\ + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 +-------------- ------- -------- ---------- -------- ---------- --------- --------- +Ethernet-BP256 U 10 100 0 0 80 20 +Ethernet-BP260 U 0 1000 0 0 800 100 + + DEVICE DEBUG_1 +---------------- --------- +sonic_drops_test 1000 +""" + +dropstat_masic_result_clear_all = """\ + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 +------------ ------- -------- ---------- -------- ---------- --------- --------- + Ethernet0 U 0 0 0 0 0 0 + Ethernet4 U 0 0 0 0 0 0 +Ethernet-BP0 U 0 0 0 0 0 0 +Ethernet-BP4 U 0 0 0 0 0 0 + + DEVICE DEBUG_1 +---------------- --------- +sonic_drops_test 0 + IFACE STATE RX_ERR RX_DROPS TX_ERR TX_DROPS DEBUG_0 DEBUG_2 +-------------- ------- -------- ---------- -------- ---------- --------- --------- +Ethernet-BP256 U 0 0 0 0 0 0 +Ethernet-BP260 U 0 0 0 0 0 0 + + DEVICE DEBUG_1 +---------------- --------- +sonic_drops_test 0 +""" + class TestMultiAsicDropstat(object): @classmethod @@ -35,14 +67,36 @@ def setup_class(cls): os.environ["UTILITIES_UNIT_TESTING_TOPOLOGY"] = "multi_asic" print("SETUP") - def test_show_pg_drop_masic(self): + def test_show_pg_drop_masic_asic0(self): return_code, result = get_result_and_return_code([ 'dropstat', '-c', 'show', '-n', 'asic0' ]) print("return_code: {}".format(return_code)) print("result = {}".format(result)) + assert result == dropstat_masic_result_asic0 and return_code == 0 + + def test_show_pg_drop_masic_all_and_clear(self): + return_code, result = get_result_and_return_code([ + 'dropstat', '-c', 'show' + ]) + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert result == dropstat_masic_result_asic0 + dropstat_masic_result_asic1 assert return_code == 0 - assert result == dropstat_masic_result + + return_code, result = get_result_and_return_code([ + 'dropstat', '-c', 'clear' + ]) + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert result == 'Cleared drop counters\n' and return_code == 0 + + return_code, result = get_result_and_return_code([ + 'dropstat', '-c', 'show' + ]) + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert result == dropstat_masic_result_clear_all and return_code == 0 def test_show_pg_drop_masic_invalid_ns(self): return_code, result = get_result_and_return_code([ @@ -53,6 +107,14 @@ def test_show_pg_drop_masic_invalid_ns(self): assert return_code == 2 assert "asic5' is not one of" in result + def test_show_pg_drop_version(self): + return_code, result = get_result_and_return_code([ + 'dropstat', '--version' + ]) + print("return_code: {}".format(return_code)) + print("result = {}".format(result)) + assert return_code == 0 + @classmethod def teardown_class(cls): os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])