def main(): args = get_argparser().parse_args() init_logger(args) core_addr = DeviceDB(args.device_db).get("core")["arguments"]["host"] mgmt = CommMgmt(core_addr) try: if args.action == "reboot": mgmt.reboot() elif args.action == "hotswap": mgmt.hotswap(args.image.read()) else: print("An action needs to be specified.", file=sys.stderr) sys.exit(1) finally: mgmt.close()
def main(): args = get_argparser().parse_args() init_logger(args) if args.verbose == args.quiet == 0: logging.getLogger().setLevel(logging.INFO) def build_dir(*path, target=args.target): return os.path.join("/tmp/", "artiq_" + target, *path) if args.target == "kc705": board_type, firmware = "kc705", "runtime" variant = "nist_clock" if args.variant is None else args.variant elif args.target == "sayma": board_type, firmware = "sayma", "runtime" variant = "standalone" if args.variant is None else args.variant elif args.target == "kasli": board_type, firmware = "kasli", "runtime" variant = "tester" if args.variant is None else args.variant else: raise NotImplementedError("unknown target {}".format(args.target)) board = args.board.format(board_type=board_type) board_file = args.board_file.format(board=board) device = args.device.format(board=board, host=args.host) serial = args.serial.format(board=board) client = SSHClient(args.host, args.jump) flock_acquired = False flock_file = None # GC root def lock(): nonlocal flock_acquired nonlocal flock_file if not flock_acquired: fuser_args = ["fuser", "-u", board_file] fuser = client.spawn_command(fuser_args) fuser_file = fuser.makefile('r') fuser_match = re.search(r"\((.+?)\)", fuser_file.readline()) if fuser_match and fuser_match.group(1) == os.getenv("USER"): logger.info("Lock already acquired by {}".format(os.getenv("USER"))) flock_acquired = True return logger.info("Acquiring device lock") flock_args = ["flock"] if not args.wait: flock_args.append("--nonblock") flock_args += ["--verbose", board_file] flock_args += ["sleep", "86400"] flock = client.spawn_command(flock_args, get_pty=True) flock_file = flock.makefile('r') while not flock_acquired: line = flock_file.readline() if not line: break logger.debug(line.rstrip()) if line.startswith("flock: executing"): flock_acquired = True elif line.startswith("flock: failed"): logger.error("Failed to get lock") sys.exit(1) def command(*args, on_failure="Command failed"): logger.debug("Running {}".format(" ".join([shlex.quote(arg) for arg in args]))) try: subprocess.check_call(args) except subprocess.CalledProcessError: logger.error(on_failure) sys.exit(1) def build(target, *extra_args, output_dir=build_dir(), variant=variant): build_args = ["python3", "-m", "artiq.gateware.targets." + target, *extra_args] if not args.gateware: build_args.append("--no-compile-gateware") if variant: build_args += ["--variant", variant] build_args += ["--output-dir", output_dir] command(*build_args, on_failure="Build failed") def flash(*steps): lock() flash_args = ["artiq_flash"] for _ in range(args.verbose): flash_args.append("-v") flash_args += ["-H", args.host] if args.jump: flash_args += ["-J", args.jump] flash_args += ["-t", board_type] flash_args += ["-V", variant] flash_args += ["-I", "source {}".format(board_file)] flash_args += ["--srcbuild", build_dir()] flash_args += steps command(*flash_args, on_failure="Flashing failed") for action in args.actions: if action == "build": logger.info("Building target") if args.target == "sayma": build("sayma_rtm", output_dir=build_dir("rtm_gateware"), variant=None) build("sayma_amc", "--rtm-csr-csv", build_dir("rtm_gateware", "rtm_csr.csv")) else: build(args.target) elif action == "clean": logger.info("Cleaning build directory") shutil.rmtree(build_dir(), ignore_errors=True) elif action == "reset": logger.info("Resetting device") flash("start") elif action == "flash": gateware = ["gateware"] if args.gateware else [] logger.info("Flashing and booting") flash(*gateware, "bootloader", "firmware", "start") elif action == "flash+log": gateware = ["gateware"] if args.gateware else [] logger.info("Flashing") flash(*gateware, "bootloader", "firmware") flterm = client.spawn_command(["flterm", serial, "--output-only"]) logger.info("Booting") flash("start") client.drain(flterm) elif action == "load": logger.info("Loading gateware") flash("load") elif action == "connect": lock() transport = client.get_transport() transport.set_keepalive(30) def forwarder(local_stream, remote_stream): try: while True: r, _, _ = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(65535) if data == b"": break remote_stream.sendall(data) if remote_stream in r: data = remote_stream.recv(65535) if data == b"": break local_stream.sendall(data) except Exception as err: logger.error("Cannot forward on port %s: %s", port, repr(err)) local_stream.close() remote_stream.close() def listener(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(8) while True: local_stream, peer_addr = listener.accept() logger.info("Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, device, port) try: remote_stream = \ transport.open_channel('direct-tcpip', (device, port), peer_addr) except Exception: logger.exception("Cannot open channel on port %s", port) continue thread = threading.Thread(target=forwarder, args=(local_stream, remote_stream), name="forward-{}".format(port), daemon=True) thread.start() ports = [1380, 1381, 1382, 1383] for port in ports: thread = threading.Thread(target=listener, args=(port,), name="listen-{}".format(port), daemon=True) thread.start() logger.info("Forwarding ports {} to core device and logs from core device" .format(", ".join(map(str, ports)))) client.run_command(["flterm", serial, "--output-only"]) elif action == "hotswap": lock() logger.info("Hotswapping firmware") firmware = build_dir(variant, "software", firmware, firmware + ".bin") mgmt = CommMgmt(device) mgmt.open(ssh_transport=client.get_transport()) with open(firmware, "rb") as f: mgmt.hotswap(f.read()) else: logger.error("Unknown action {}".format(action)) sys.exit(1)
def main(): args = get_argparser().parse_args() init_logger(args) device_mgr = DeviceManager(DeviceDB(args.device_db)) try: core_addr = DeviceDB(args.device_db).get("core")["arguments"]["host"] mgmt = CommMgmt(core_addr) if args.tool == "log": if args.action == "set_level": mgmt.set_log_level(args.level) if args.action == "set_uart_level": mgmt.set_uart_log_level(args.level) if args.action == "clear": mgmt.clear_log() if args.action == None: print(mgmt.get_log(), end="") if args.tool == "config": if args.action == "read": value = mgmt.config_read(args.key) if not value: print("Key {} does not exist".format(args.key)) else: print(value) if args.action == "write": for key, value in args.string: mgmt.config_write(key, value.encode("utf-8")) for key, filename in args.file: with open(filename, "rb") as fi: mgmt.config_write(key, fi.read()) if args.action == "remove": for key in args.key: mgmt.config_remove(key) if args.action == "erase": mgmt.config_erase() if args.tool == "reboot": mgmt.reboot() if args.tool == "hotswap": mgmt.hotswap(args.image.read()) if args.tool == "profile": if args.action == "start": mgmt.start_profiler(args.interval, args.hits_size, args.edges_size) elif args.action == "stop": mgmt.stop_profiler() elif args.action == "save": hits, edges = mgmt.get_profile() writer = CallgrindWriter(args.output, args.firmware, "or1k-linux", args.compression, args.demangle) writer.header() for addr, count in hits.items(): writer.hit(addr, count) for (caller, callee), count in edges.items(): writer.edge(caller, callee, count) if args.tool == "debug": if args.action == "allocator": mgmt.debug_allocator() finally: device_mgr.close_devices()
def main(): args = get_argparser().parse_args() init_logger(args) if args.verbose == args.quiet == 0: logging.getLogger().setLevel(logging.INFO) def build_dir(*path, target=args.target): return os.path.join("/tmp/", "artiq_" + target, *path) if args.target == "kc705": board_type, firmware = "kc705", "runtime" variant = "nist_clock" if args.variant is None else args.variant elif args.target == "sayma": board_type, firmware = "sayma", "runtime" variant = "standalone" if args.variant is None else args.variant elif args.target == "kasli": board_type, firmware = "kasli", "runtime" variant = "tester" if args.variant is None else args.variant else: raise NotImplementedError("unknown target {}".format(args.target)) board = args.board.format(board_type=board_type) board_file = args.board_file.format(board=board) device = args.device.format(board=board, host=args.host) serial = args.serial.format(board=board) client = SSHClient(args.host, args.jump) flock_acquired = False flock_file = None # GC root def lock(): nonlocal flock_acquired nonlocal flock_file if not flock_acquired: fuser_args = ["fuser", "-u", board_file] fuser = client.spawn_command(fuser_args) fuser_file = fuser.makefile('r') fuser_match = re.search(r"\((.+?)\)", fuser_file.readline()) if fuser_match and fuser_match.group(1) == os.getenv("USER"): logger.info("Lock already acquired by {}".format( os.getenv("USER"))) flock_acquired = True return logger.info("Acquiring device lock") flock_args = ["flock"] if not args.wait: flock_args.append("--nonblock") flock_args += ["--verbose", board_file] flock_args += ["sleep", "86400"] flock = client.spawn_command(flock_args, get_pty=True) flock_file = flock.makefile('r') while not flock_acquired: line = flock_file.readline() if not line: break logger.debug(line.rstrip()) if line.startswith("flock: executing"): flock_acquired = True elif line.startswith("flock: failed"): logger.error("Failed to get lock") sys.exit(1) def command(*args, on_failure="Command failed"): logger.debug("Running {}".format(" ".join( [shlex.quote(arg) for arg in args]))) try: subprocess.check_call(args) except subprocess.CalledProcessError: logger.error(on_failure) sys.exit(1) def build(target, *extra_args, output_dir=build_dir(), variant=variant): build_args = [ "python3", "-m", "artiq.gateware.targets." + target, *extra_args ] if not args.gateware: build_args.append("--no-compile-gateware") if variant: build_args += ["--variant", variant] build_args += ["--output-dir", output_dir] command(*build_args, on_failure="Build failed") def flash(*steps): lock() flash_args = ["artiq_flash"] for _ in range(args.verbose): flash_args.append("-v") flash_args += ["-H", args.host] if args.jump: flash_args += ["-J", args.jump] flash_args += ["-t", board_type] flash_args += ["-V", variant] flash_args += ["-I", "source {}".format(board_file)] flash_args += ["--srcbuild", build_dir()] flash_args += steps command(*flash_args, on_failure="Flashing failed") for action in args.actions: if action == "build": logger.info("Building target") if args.target == "sayma": build("sayma_rtm", output_dir=build_dir("rtm_gateware"), variant=None) build("sayma_amc", "--rtm-csr-csv", build_dir("rtm_gateware", "rtm_csr.csv")) else: build(args.target) elif action == "clean": logger.info("Cleaning build directory") shutil.rmtree(build_dir(), ignore_errors=True) elif action == "reset": logger.info("Resetting device") flash("start") elif action == "flash": gateware = ["gateware"] if args.gateware else [] logger.info("Flashing and booting") flash(*gateware, "bootloader", "firmware", "start") elif action == "flash+log": gateware = ["gateware"] if args.gateware else [] logger.info("Flashing") flash(*gateware, "bootloader", "firmware") flterm = client.spawn_command(["flterm", serial, "--output-only"]) logger.info("Booting") flash("start") client.drain(flterm) elif action == "load": logger.info("Loading gateware") flash("load") elif action == "connect": lock() transport = client.get_transport() transport.set_keepalive(30) def forwarder(local_stream, remote_stream): try: while True: r, _, _ = select.select([local_stream, remote_stream], [], []) if local_stream in r: data = local_stream.recv(65535) if data == b"": break remote_stream.sendall(data) if remote_stream in r: data = remote_stream.recv(65535) if data == b"": break local_stream.sendall(data) except Exception as err: logger.error("Cannot forward on port %s: %s", port, repr(err)) local_stream.close() remote_stream.close() def listener(port): listener = socket.socket() listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind(('localhost', port)) listener.listen(8) while True: local_stream, peer_addr = listener.accept() logger.info( "Accepting %s:%s and opening SSH channel to %s:%s", *peer_addr, device, port) try: remote_stream = \ transport.open_channel('direct-tcpip', (device, port), peer_addr) except Exception: logger.exception("Cannot open channel on port %s", port) continue thread = threading.Thread(target=forwarder, args=(local_stream, remote_stream), name="forward-{}".format(port), daemon=True) thread.start() ports = [1380, 1381, 1382, 1383] for port in ports: thread = threading.Thread(target=listener, args=(port, ), name="listen-{}".format(port), daemon=True) thread.start() logger.info( "Forwarding ports {} to core device and logs from core device". format(", ".join(map(str, ports)))) client.run_command(["flterm", serial, "--output-only"]) elif action == "hotswap": lock() logger.info("Hotswapping firmware") firmware = build_dir(variant, "software", firmware, firmware + ".bin") mgmt = CommMgmt(device) mgmt.open(ssh_transport=client.get_transport()) with open(firmware, "rb") as f: mgmt.hotswap(f.read()) else: logger.error("Unknown action {}".format(action)) sys.exit(1)
def main(): args = get_argparser().parse_args() init_logger(args) if args.device is None: core_addr = DeviceDB(args.device_db).get("core")["arguments"]["host"] else: core_addr = args.device mgmt = CommMgmt(core_addr) if args.tool == "log": if args.action == "set_level": mgmt.set_log_level(args.level) if args.action == "set_uart_level": mgmt.set_uart_log_level(args.level) if args.action == "clear": mgmt.clear_log() if args.action == None: print(mgmt.get_log(), end="") if args.tool == "config": if args.action == "read": value = mgmt.config_read(args.key) if not value: print("Key {} does not exist".format(args.key)) else: print(value) if args.action == "write": for key, value in args.string: mgmt.config_write(key, value.encode("utf-8")) for key, filename in args.file: with open(filename, "rb") as fi: mgmt.config_write(key, fi.read()) if args.action == "remove": for key in args.key: mgmt.config_remove(key) if args.action == "erase": mgmt.config_erase() if args.tool == "reboot": mgmt.reboot() if args.tool == "hotswap": mgmt.hotswap(args.image.read()) if args.tool == "profile": if args.action == "start": mgmt.start_profiler(args.interval, args.hits_size, args.edges_size) elif args.action == "stop": mgmt.stop_profiler() elif args.action == "save": hits, edges = mgmt.get_profile() writer = CallgrindWriter(args.output, args.firmware, "or1k-linux", args.compression, args.demangle) writer.header() for addr, count in hits.items(): writer.hit(addr, count) for (caller, callee), count in edges.items(): writer.edge(caller, callee, count) if args.tool == "debug": if args.action == "allocator": mgmt.debug_allocator()