- @classmethod
- def instance(cls):
- """Return the instance of this testcase"""
- return cls.test_instance
-
- @classmethod
- def set_debug_flags(cls, d):
- cls.debug_core = False
- cls.debug_gdb = False
- cls.debug_gdbserver = False
- if d is None:
- return
- dl = d.lower()
- if dl == "core":
- cls.debug_core = True
- elif dl == "gdb":
- cls.debug_gdb = True
- elif dl == "gdbserver":
- cls.debug_gdbserver = True
- else:
- raise Exception("Unrecognized DEBUG option: '%s'" % d)
-
- @staticmethod
- def get_least_used_cpu():
- cpu_usage_list = [set(range(psutil.cpu_count()))]
- vpp_processes = [p for p in psutil.process_iter(attrs=['pid', 'name'])
- if 'vpp_main' == p.info['name']]
- for vpp_process in vpp_processes:
- for cpu_usage_set in cpu_usage_list:
- try:
- cpu_num = vpp_process.cpu_num()
- if cpu_num in cpu_usage_set:
- cpu_usage_set_index = cpu_usage_list.index(
- cpu_usage_set)
- if cpu_usage_set_index == len(cpu_usage_list) - 1:
- cpu_usage_list.append({cpu_num})
- else:
- cpu_usage_list[cpu_usage_set_index + 1].add(
- cpu_num)
- cpu_usage_set.remove(cpu_num)
- break
- except psutil.NoSuchProcess:
- pass
-
- for cpu_usage_set in cpu_usage_list:
- if len(cpu_usage_set) > 0:
- min_usage_set = cpu_usage_set
- break
-
- return random.choice(tuple(min_usage_set))
-
- @classmethod
- def setUpConstants(cls):
- """ Set-up the test case class based on environment variables """
- s = os.getenv("STEP", "n")
- cls.step = True if s.lower() in ("y", "yes", "1") else False
- d = os.getenv("DEBUG", None)
- c = os.getenv("CACHE_OUTPUT", "1")
- cls.cache_vpp_output = False if c.lower() in ("n", "no", "0") else True
- cls.set_debug_flags(d)
- cls.vpp_bin = os.getenv('VPP_BIN', "vpp")
- cls.plugin_path = os.getenv('VPP_PLUGIN_PATH')
- cls.test_plugin_path = os.getenv('VPP_TEST_PLUGIN_PATH')
- cls.extern_plugin_path = os.getenv('EXTERN_PLUGINS')
- plugin_path = None
- if cls.plugin_path is not None:
- if cls.extern_plugin_path is not None:
- plugin_path = "%s:%s" % (
- cls.plugin_path, cls.extern_plugin_path)
- else:
- plugin_path = cls.plugin_path
- elif cls.extern_plugin_path is not None:
- plugin_path = cls.extern_plugin_path
- debug_cli = ""
- if cls.step or cls.debug_gdb or cls.debug_gdbserver:
- debug_cli = "cli-listen localhost:5002"
- coredump_size = None
- size = os.getenv("COREDUMP_SIZE")
- if size is not None:
- coredump_size = "coredump-size %s" % size
- if coredump_size is None:
- coredump_size = "coredump-size unlimited"
-
- cpu_core_number = cls.get_least_used_cpu()
-
- cls.vpp_cmdline = [cls.vpp_bin, "unix",
- "{", "nodaemon", debug_cli, "full-coredump",
- coredump_size, "runtime-dir", cls.tempdir, "}",
- "api-trace", "{", "on", "}", "api-segment", "{",
- "prefix", cls.shm_prefix, "}", "cpu", "{",
- "main-core", str(cpu_core_number), "}",
- "statseg", "{", "socket-name", cls.stats_sock, "}",
- "socksvr", "{", "socket-name", cls.api_sock, "}",
- "plugins",
- "{", "plugin", "dpdk_plugin.so", "{", "disable",
- "}", "plugin", "rdma_plugin.so", "{", "disable",
- "}", "plugin", "unittest_plugin.so", "{", "enable",
- "}"] + cls.extra_vpp_plugin_config + ["}", ]
- if cls.extra_vpp_punt_config is not None:
- cls.vpp_cmdline.extend(cls.extra_vpp_punt_config)
- if plugin_path is not None:
- cls.vpp_cmdline.extend(["plugin_path", plugin_path])
- if cls.test_plugin_path is not None:
- cls.vpp_cmdline.extend(["test_plugin_path", cls.test_plugin_path])
-
- cls.logger.info("vpp_cmdline args: %s" % cls.vpp_cmdline)
- cls.logger.info("vpp_cmdline: %s" % " ".join(cls.vpp_cmdline))
-
- @classmethod
- def wait_for_enter(cls):
- if cls.debug_gdbserver:
- print(double_line_delim)
- print("Spawned GDB server with PID: %d" % cls.vpp.pid)
- elif cls.debug_gdb:
- print(double_line_delim)
- print("Spawned VPP with PID: %d" % cls.vpp.pid)
- else:
- cls.logger.debug("Spawned VPP with PID: %d" % cls.vpp.pid)
- return
- print(single_line_delim)
- print("You can debug the VPP using e.g.:")
- if cls.debug_gdbserver:
- print("gdb " + cls.vpp_bin + " -ex 'target remote localhost:7777'")
- print("Now is the time to attach a gdb by running the above "
- "command, set up breakpoints etc. and then resume VPP from "
- "within gdb by issuing the 'continue' command")
- elif cls.debug_gdb:
- print("gdb " + cls.vpp_bin + " -ex 'attach %s'" % cls.vpp.pid)
- print("Now is the time to attach a gdb by running the above "
- "command and set up breakpoints etc.")
- print(single_line_delim)
- input("Press ENTER to continue running the testcase...")
-
- @classmethod
- def run_vpp(cls):
- cmdline = cls.vpp_cmdline
-
- if cls.debug_gdbserver:
- gdbserver = '/usr/bin/gdbserver'
- if not os.path.isfile(gdbserver) or \
- not os.access(gdbserver, os.X_OK):
- raise Exception("gdbserver binary '%s' does not exist or is "
- "not executable" % gdbserver)
-
- cmdline = [gdbserver, 'localhost:7777'] + cls.vpp_cmdline
- cls.logger.info("Gdbserver cmdline is %s", " ".join(cmdline))
-
- try:
- cls.vpp = subprocess.Popen(cmdline,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- bufsize=1)
- except subprocess.CalledProcessError as e:
- cls.logger.critical("Subprocess returned with non-0 return code: ("
- "%s)", e.returncode)
- raise
- except OSError as e:
- cls.logger.critical("Subprocess returned with OS error: "
- "(%s) %s", e.errno, e.strerror)
- raise
- except Exception as e:
- cls.logger.exception("Subprocess returned unexpected from "
- "%s:", cmdline)
- raise
-
- cls.wait_for_enter()
-
- @classmethod
- def wait_for_stats_socket(cls):
- deadline = time.time() + 3
- ok = False
- while time.time() < deadline or \
- cls.debug_gdb or cls.debug_gdbserver:
- if os.path.exists(cls.stats_sock):
- ok = True
- break
- cls.sleep(0.8)
- if not ok:
- cls.logger.critical("Couldn't stat : {}".format(cls.stats_sock))
-
- @classmethod
- def wait_for_coredump(cls):
- corefile = cls.tempdir + "/core"
- if os.path.isfile(corefile):
- cls.logger.error("Waiting for coredump to complete: %s", corefile)
- curr_size = os.path.getsize(corefile)
- deadline = time.time() + 60
- ok = False
- while time.time() < deadline:
- cls.sleep(1)
- size = curr_size
- curr_size = os.path.getsize(corefile)
- if size == curr_size:
- ok = True
- break
- if not ok:
- cls.logger.error("Timed out waiting for coredump to complete:"
- " %s", corefile)
- else:
- cls.logger.error("Coredump complete: %s, size %d",
- corefile, curr_size)
-