coredevice.CommKernel: modernize. NFC.

This commit is contained in:
whitequark 2018-05-16 14:40:14 +00:00
parent ca93b94aea
commit 4d06c1d84b
2 changed files with 43 additions and 60 deletions

View File

@ -14,52 +14,33 @@ from artiq import __version__ as software_version
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class _H2DMsgType(Enum): class Request(Enum):
LOG_REQUEST = 1 SystemInfo = 3
LOG_CLEAR = 2 SwitchClock = 4
LOG_FILTER = 13
SYSTEM_INFO_REQUEST = 3 LoadKernel = 5
SWITCH_CLOCK = 4 RunKernel = 6
LOAD_KERNEL = 5 RPCReply = 7
RUN_KERNEL = 6 RPCException = 8
RPC_REPLY = 7
RPC_EXCEPTION = 8
HOTSWAP = 14
class _D2HMsgType(Enum): class Reply(Enum):
LOG_REPLY = 1 SystemInfo = 2
ClockSwitchCompleted = 3
ClockSwitchFailed = 4
SYSTEM_INFO_REPLY = 2 LoadCompleted = 5
CLOCK_SWITCH_COMPLETED = 3 LoadFailed = 6
CLOCK_SWITCH_FAILED = 4
LOAD_COMPLETED = 5 KernelFinished = 7
LOAD_FAILED = 6 KernelStartupFailed = 8
KernelException = 9
KERNEL_FINISHED = 7 RPCRequest = 10
KERNEL_STARTUP_FAILED = 8
KERNEL_EXCEPTION = 9
RPC_REQUEST = 10 WatchdogExpired = 14
ClockFailure = 15
WATCHDOG_EXPIRED = 14
CLOCK_FAILURE = 15
HOTSWAP_IMMINENT = 16
class _LogLevel(Enum):
OFF = 0
ERROR = 1
WARN = 2
INFO = 3
DEBUG = 4
TRACE = 5
class UnsupportedDevice(Exception): class UnsupportedDevice(Exception):
@ -94,12 +75,6 @@ class CommKernelDummy:
def check_system_info(self): def check_system_info(self):
pass pass
def get_log(self):
return ""
def clear_log(self):
pass
class CommKernel: class CommKernel:
warned_of_mismatch = False warned_of_mismatch = False
@ -152,7 +127,7 @@ class CommKernel:
# Read message header. # Read message header.
(raw_type, ) = struct.unpack("B", self.read(1)) (raw_type, ) = struct.unpack("B", self.read(1))
self._read_type = _D2HMsgType(raw_type) self._read_type = Reply(raw_type)
logger.debug("receiving message: type=%r", logger.debug("receiving message: type=%r",
self._read_type) self._read_type)
@ -242,10 +217,10 @@ class CommKernel:
self.write(struct.pack(">ll", 0x5a5a5a5a, 0)) self.write(struct.pack(">ll", 0x5a5a5a5a, 0))
def check_system_info(self): def check_system_info(self):
self._write_empty(_H2DMsgType.SYSTEM_INFO_REQUEST) self._write_empty(Request.SystemInfo)
self._read_header() self._read_header()
self._read_expect(_D2HMsgType.SYSTEM_INFO_REPLY) self._read_expect(Reply.SystemInfo)
runtime_id = self._read_chunk(4) runtime_id = self._read_chunk(4)
if runtime_id != b"AROR": if runtime_id != b"AROR":
raise UnsupportedDevice("Unsupported runtime ID: {}" raise UnsupportedDevice("Unsupported runtime ID: {}"
@ -263,23 +238,23 @@ class CommKernel:
logger.warning("Previous kernel did not cleanly finish") logger.warning("Previous kernel did not cleanly finish")
def switch_clock(self, external): def switch_clock(self, external):
self._write_header(_H2DMsgType.SWITCH_CLOCK) self._write_header(Request.SwitchClock)
self._write_int8(external) self._write_int8(external)
self._read_empty(_D2HMsgType.CLOCK_SWITCH_COMPLETED) self._read_empty(Reply.ClockSwitchCompleted)
def load(self, kernel_library): def load(self, kernel_library):
self._write_header(_H2DMsgType.LOAD_KERNEL) self._write_header(Request.LoadKernel)
self._write_bytes(kernel_library) self._write_bytes(kernel_library)
self._read_header() self._read_header()
if self._read_type == _D2HMsgType.LOAD_FAILED: if self._read_type == Reply.LoadFailed:
raise LoadError(self._read_string()) raise LoadError(self._read_string())
else: else:
self._read_expect(_D2HMsgType.LOAD_COMPLETED) self._read_expect(Reply.LoadCompleted)
def run(self): def run(self):
self._write_empty(_H2DMsgType.RUN_KERNEL) self._write_empty(Request.RunKernel)
logger.debug("running kernel") logger.debug("running kernel")
_rpc_sentinel = object() _rpc_sentinel = object()
@ -460,7 +435,7 @@ class CommKernel:
result = service(*args, **kwargs) result = service(*args, **kwargs)
logger.debug("rpc service: %d %r %r = %r", service_id, args, kwargs, result) logger.debug("rpc service: %d %r %r = %r", service_id, args, kwargs, result)
self._write_header(_H2DMsgType.RPC_REPLY) self._write_header(Request.RPCReply)
self._write_bytes(return_tags) self._write_bytes(return_tags)
self._send_rpc_value(bytearray(return_tags), result, result, service) self._send_rpc_value(bytearray(return_tags), result, result, service)
except RPCReturnValueError as exn: except RPCReturnValueError as exn:
@ -468,7 +443,7 @@ class CommKernel:
except Exception as exn: except Exception as exn:
logger.debug("rpc service: %d %r %r ! %r", service_id, args, kwargs, exn) logger.debug("rpc service: %d %r %r ! %r", service_id, args, kwargs, exn)
self._write_header(_H2DMsgType.RPC_EXCEPTION) self._write_header(Request.RPCException)
if hasattr(exn, "artiq_core_exception"): if hasattr(exn, "artiq_core_exception"):
exn = exn.artiq_core_exception exn = exn.artiq_core_exception
@ -536,14 +511,14 @@ class CommKernel:
def serve(self, embedding_map, symbolizer, demangler): def serve(self, embedding_map, symbolizer, demangler):
while True: while True:
self._read_header() self._read_header()
if self._read_type == _D2HMsgType.RPC_REQUEST: if self._read_type == Reply.RPCRequest:
self._serve_rpc(embedding_map) self._serve_rpc(embedding_map)
elif self._read_type == _D2HMsgType.KERNEL_EXCEPTION: elif self._read_type == Reply.KernelException:
self._serve_exception(embedding_map, symbolizer, demangler) self._serve_exception(embedding_map, symbolizer, demangler)
elif self._read_type == _D2HMsgType.WATCHDOG_EXPIRED: elif self._read_type == Reply.WatchdogExpired:
raise exceptions.WatchdogExpired raise exceptions.WatchdogExpired
elif self._read_type == _D2HMsgType.CLOCK_FAILURE: elif self._read_type == Reply.ClockFailure:
raise exceptions.ClockFailure raise exceptions.ClockFailure
else: else:
self._read_expect(_D2HMsgType.KERNEL_FINISHED) self._read_expect(Reply.KernelFinished)
return return

View File

@ -118,6 +118,8 @@ impl Request {
{ {
read_sync(reader)?; read_sync(reader)?;
Ok(match reader.read_u8()? { Ok(match reader.read_u8()? {
// 1-2, 13 were log requests
3 => Request::SystemInfo, 3 => Request::SystemInfo,
4 => Request::SwitchClock(reader.read_u8()?), 4 => Request::SwitchClock(reader.read_u8()?),
@ -141,6 +143,8 @@ impl Request {
// 9-12 were flash requests // 9-12 were flash requests
// 14 was hotswap request
ty => return Err(Error::UnknownPacket(ty)) ty => return Err(Error::UnknownPacket(ty))
}) })
} }
@ -152,6 +156,8 @@ impl<'a> Reply<'a> {
{ {
write_sync(writer)?; write_sync(writer)?;
match *self { match *self {
// 1 was log reply
Reply::SystemInfo { ident, finished_cleanly } => { Reply::SystemInfo { ident, finished_cleanly } => {
writer.write_u8(2)?; writer.write_u8(2)?;
writer.write(b"AROR")?; writer.write(b"AROR")?;
@ -211,6 +217,8 @@ impl<'a> Reply<'a> {
Reply::ClockFailure => { Reply::ClockFailure => {
writer.write_u8(15)?; writer.write_u8(15)?;
}, },
// 16 was hotswap imminent reply
} }
Ok(()) Ok(())
} }