2015-04-10 00:59:35 +08:00
|
|
|
import struct
|
|
|
|
import logging
|
2015-08-08 21:01:08 +08:00
|
|
|
import traceback
|
2016-07-06 12:44:21 +08:00
|
|
|
import numpy
|
2015-04-10 00:59:35 +08:00
|
|
|
from enum import Enum
|
|
|
|
from fractions import Fraction
|
2016-04-26 06:05:32 +08:00
|
|
|
from collections import namedtuple
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2015-12-31 21:54:54 +08:00
|
|
|
from artiq.coredevice import exceptions
|
2018-05-16 22:07:44 +08:00
|
|
|
from artiq.coredevice.comm import initialize_connection
|
2015-12-11 22:55:40 +08:00
|
|
|
from artiq import __version__ as software_version
|
2015-04-10 00:59:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
class Request(Enum):
|
|
|
|
SystemInfo = 3
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
LoadKernel = 5
|
|
|
|
RunKernel = 6
|
2015-04-22 01:31:31 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
RPCReply = 7
|
|
|
|
RPCException = 8
|
2015-05-07 23:47:48 +08:00
|
|
|
|
2015-08-28 13:37:46 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
class Reply(Enum):
|
|
|
|
SystemInfo = 2
|
2017-03-07 00:01:28 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
LoadCompleted = 5
|
|
|
|
LoadFailed = 6
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
KernelFinished = 7
|
|
|
|
KernelStartupFailed = 8
|
|
|
|
KernelException = 9
|
2015-08-28 13:37:46 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
RPCRequest = 10
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
WatchdogExpired = 14
|
|
|
|
ClockFailure = 15
|
2017-03-03 14:06:26 +08:00
|
|
|
|
|
|
|
|
2015-04-10 00:59:35 +08:00
|
|
|
class UnsupportedDevice(Exception):
|
|
|
|
pass
|
|
|
|
|
2017-01-27 20:49:48 +08:00
|
|
|
class LoadError(Exception):
|
|
|
|
pass
|
|
|
|
|
2015-08-10 01:17:00 +08:00
|
|
|
class RPCReturnValueError(ValueError):
|
|
|
|
pass
|
|
|
|
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2016-04-26 06:05:32 +08:00
|
|
|
RPCKeyword = namedtuple('RPCKeyword', ['name', 'value'])
|
|
|
|
|
|
|
|
|
2017-05-22 15:45:45 +08:00
|
|
|
class CommKernelDummy:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def load(self, kernel_library):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def serve(self, embedding_map, symbolizer, demangler):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def check_system_info(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-02-27 18:37:30 +08:00
|
|
|
class CommKernel:
|
2017-12-27 05:13:46 +08:00
|
|
|
warned_of_mismatch = False
|
|
|
|
|
2017-05-22 15:45:45 +08:00
|
|
|
def __init__(self, host, port=1381):
|
2016-10-07 01:25:43 +08:00
|
|
|
self._read_type = None
|
2017-02-27 18:37:30 +08:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2018-05-16 22:07:44 +08:00
|
|
|
def open(self, **kwargs):
|
2017-02-27 18:37:30 +08:00
|
|
|
if hasattr(self, "socket"):
|
|
|
|
return
|
2018-05-16 22:07:44 +08:00
|
|
|
self.socket = initialize_connection(self.host, self.port, **kwargs)
|
2017-02-27 18:37:30 +08:00
|
|
|
self.socket.sendall(b"ARTIQ coredev\n")
|
2015-04-10 00:59:35 +08:00
|
|
|
|
|
|
|
def close(self):
|
2017-02-27 18:37:30 +08:00
|
|
|
if not hasattr(self, "socket"):
|
|
|
|
return
|
|
|
|
self.socket.close()
|
|
|
|
del self.socket
|
|
|
|
logger.debug("disconnected")
|
2015-04-10 00:59:35 +08:00
|
|
|
|
|
|
|
def read(self, length):
|
2017-02-27 18:37:30 +08:00
|
|
|
r = bytes()
|
|
|
|
while len(r) < length:
|
|
|
|
rn = self.socket.recv(min(8192, length - len(r)))
|
|
|
|
if not rn:
|
|
|
|
raise ConnectionResetError("Connection closed")
|
|
|
|
r += rn
|
|
|
|
return r
|
2015-04-10 00:59:35 +08:00
|
|
|
|
|
|
|
def write(self, data):
|
2017-02-27 18:37:30 +08:00
|
|
|
self.socket.sendall(data)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Reader interface
|
2015-04-10 00:59:35 +08:00
|
|
|
#
|
|
|
|
|
2015-04-22 01:31:31 +08:00
|
|
|
def _read_header(self):
|
2015-04-10 00:59:35 +08:00
|
|
|
self.open()
|
|
|
|
|
2015-08-07 21:15:44 +08:00
|
|
|
# Wait for a synchronization sequence, 5a 5a 5a 5a.
|
2015-04-22 01:31:31 +08:00
|
|
|
sync_count = 0
|
|
|
|
while sync_count < 4:
|
2015-08-07 21:15:44 +08:00
|
|
|
(sync_byte, ) = struct.unpack("B", self.read(1))
|
|
|
|
if sync_byte == 0x5a:
|
2015-04-22 01:31:31 +08:00
|
|
|
sync_count += 1
|
2015-04-10 00:59:35 +08:00
|
|
|
else:
|
2015-04-22 01:31:31 +08:00
|
|
|
sync_count = 0
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
# Read message header.
|
|
|
|
(raw_type, ) = struct.unpack("B", self.read(1))
|
2018-05-16 22:40:14 +08:00
|
|
|
self._read_type = Reply(raw_type)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2016-10-07 01:25:43 +08:00
|
|
|
logger.debug("receiving message: type=%r",
|
|
|
|
self._read_type)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
def _read_expect(self, ty):
|
|
|
|
if self._read_type != ty:
|
|
|
|
raise IOError("Incorrect reply from device: {} (expected {})".
|
|
|
|
format(self._read_type, ty))
|
|
|
|
|
|
|
|
def _read_empty(self, ty):
|
|
|
|
self._read_header()
|
|
|
|
self._read_expect(ty)
|
|
|
|
|
|
|
|
def _read_chunk(self, length):
|
|
|
|
return self.read(length)
|
|
|
|
|
|
|
|
def _read_int8(self):
|
|
|
|
(value, ) = struct.unpack("B", self._read_chunk(1))
|
|
|
|
return value
|
|
|
|
|
|
|
|
def _read_int32(self):
|
|
|
|
(value, ) = struct.unpack(">l", self._read_chunk(4))
|
|
|
|
return value
|
|
|
|
|
|
|
|
def _read_int64(self):
|
|
|
|
(value, ) = struct.unpack(">q", self._read_chunk(8))
|
|
|
|
return value
|
|
|
|
|
|
|
|
def _read_float64(self):
|
|
|
|
(value, ) = struct.unpack(">d", self._read_chunk(8))
|
|
|
|
return value
|
|
|
|
|
2016-11-01 14:51:44 +08:00
|
|
|
def _read_bool(self):
|
|
|
|
return True if self._read_int8() else False
|
|
|
|
|
2015-08-08 18:21:43 +08:00
|
|
|
def _read_bytes(self):
|
|
|
|
return self._read_chunk(self._read_int32())
|
|
|
|
|
|
|
|
def _read_string(self):
|
2016-10-06 23:38:33 +08:00
|
|
|
return self._read_bytes().decode("utf-8")
|
2015-08-08 18:21:43 +08:00
|
|
|
|
2015-08-07 21:15:44 +08:00
|
|
|
#
|
|
|
|
# Writer interface
|
|
|
|
#
|
|
|
|
|
|
|
|
def _write_header(self, ty):
|
2015-04-22 01:31:31 +08:00
|
|
|
self.open()
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2016-10-07 01:25:43 +08:00
|
|
|
logger.debug("sending message: type=%r", ty)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2016-10-07 01:25:43 +08:00
|
|
|
# Write synchronization sequence and header.
|
|
|
|
self.write(struct.pack(">lB", 0x5a5a5a5a, ty.value))
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
def _write_empty(self, ty):
|
|
|
|
self._write_header(ty)
|
|
|
|
|
2015-08-08 18:21:43 +08:00
|
|
|
def _write_chunk(self, chunk):
|
2016-10-07 01:25:43 +08:00
|
|
|
self.write(chunk)
|
2015-08-08 18:21:43 +08:00
|
|
|
|
2015-08-07 21:15:44 +08:00
|
|
|
def _write_int8(self, value):
|
2016-10-07 01:25:43 +08:00
|
|
|
self.write(struct.pack("B", value))
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
def _write_int32(self, value):
|
2016-10-07 01:25:43 +08:00
|
|
|
self.write(struct.pack(">l", value))
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
def _write_int64(self, value):
|
2016-10-07 01:25:43 +08:00
|
|
|
self.write(struct.pack(">q", value))
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
def _write_float64(self, value):
|
2016-10-07 01:25:43 +08:00
|
|
|
self.write(struct.pack(">d", value))
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2016-11-01 14:51:44 +08:00
|
|
|
def _write_bool(self, value):
|
|
|
|
self.write(struct.pack("B", value))
|
|
|
|
|
2015-08-08 18:21:43 +08:00
|
|
|
def _write_bytes(self, value):
|
|
|
|
self._write_int32(len(value))
|
2016-10-07 01:25:43 +08:00
|
|
|
self.write(value)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2015-08-08 18:21:43 +08:00
|
|
|
def _write_string(self, value):
|
2016-10-06 23:38:33 +08:00
|
|
|
self._write_bytes(value.encode("utf-8"))
|
2015-08-08 18:21:43 +08:00
|
|
|
|
2015-08-07 21:15:44 +08:00
|
|
|
#
|
|
|
|
# Exported APIs
|
|
|
|
#
|
2015-07-07 21:29:38 +08:00
|
|
|
|
|
|
|
def reset_session(self):
|
2015-08-07 21:15:44 +08:00
|
|
|
self.write(struct.pack(">ll", 0x5a5a5a5a, 0))
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2017-02-01 06:53:38 +08:00
|
|
|
def check_system_info(self):
|
2018-05-16 22:40:14 +08:00
|
|
|
self._write_empty(Request.SystemInfo)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
|
|
|
self._read_header()
|
2018-05-16 22:40:14 +08:00
|
|
|
self._read_expect(Reply.SystemInfo)
|
2015-08-07 21:15:44 +08:00
|
|
|
runtime_id = self._read_chunk(4)
|
|
|
|
if runtime_id != b"AROR":
|
2015-04-10 00:59:35 +08:00
|
|
|
raise UnsupportedDevice("Unsupported runtime ID: {}"
|
|
|
|
.format(runtime_id))
|
2017-02-25 20:05:05 +08:00
|
|
|
|
2018-07-15 17:21:17 +08:00
|
|
|
gateware_version = self._read_string().split(";")[0]
|
2017-12-27 05:13:46 +08:00
|
|
|
if gateware_version != software_version and not self.warned_of_mismatch:
|
2015-12-11 22:55:40 +08:00
|
|
|
logger.warning("Mismatch between gateware (%s) "
|
|
|
|
"and software (%s) versions",
|
|
|
|
gateware_version, software_version)
|
2017-12-27 05:13:46 +08:00
|
|
|
CommKernel.warned_of_mismatch = True
|
2017-02-25 20:05:05 +08:00
|
|
|
|
2017-02-01 06:53:38 +08:00
|
|
|
finished_cleanly = self._read_bool()
|
|
|
|
if not finished_cleanly:
|
2017-03-28 22:22:04 +08:00
|
|
|
logger.warning("Previous kernel did not cleanly finish")
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2015-08-08 21:01:08 +08:00
|
|
|
def load(self, kernel_library):
|
2018-05-16 22:40:14 +08:00
|
|
|
self._write_header(Request.LoadKernel)
|
2016-10-07 01:25:43 +08:00
|
|
|
self._write_bytes(kernel_library)
|
2015-08-08 21:01:08 +08:00
|
|
|
|
2017-01-27 20:49:48 +08:00
|
|
|
self._read_header()
|
2018-05-16 22:40:14 +08:00
|
|
|
if self._read_type == Reply.LoadFailed:
|
2017-01-27 20:49:48 +08:00
|
|
|
raise LoadError(self._read_string())
|
|
|
|
else:
|
2018-05-16 22:40:14 +08:00
|
|
|
self._read_expect(Reply.LoadCompleted)
|
2015-08-08 21:01:08 +08:00
|
|
|
|
|
|
|
def run(self):
|
2018-05-16 22:40:14 +08:00
|
|
|
self._write_empty(Request.RunKernel)
|
2015-08-08 21:01:08 +08:00
|
|
|
logger.debug("running kernel")
|
|
|
|
|
2015-08-09 07:17:19 +08:00
|
|
|
_rpc_sentinel = object()
|
|
|
|
|
2015-08-10 01:17:00 +08:00
|
|
|
# See session.c:{send,receive}_rpc_value and llvm_ir_generator.py:_rpc_tag.
|
2016-05-16 22:30:21 +08:00
|
|
|
def _receive_rpc_value(self, embedding_map):
|
2015-08-09 07:17:19 +08:00
|
|
|
tag = chr(self._read_int8())
|
|
|
|
if tag == "\x00":
|
|
|
|
return self._rpc_sentinel
|
|
|
|
elif tag == "t":
|
|
|
|
length = self._read_int8()
|
2016-05-16 22:30:21 +08:00
|
|
|
return tuple(self._receive_rpc_value(embedding_map) for _ in range(length))
|
2015-08-09 07:17:19 +08:00
|
|
|
elif tag == "n":
|
2015-04-28 01:31:55 +08:00
|
|
|
return None
|
2015-08-07 21:15:44 +08:00
|
|
|
elif tag == "b":
|
|
|
|
return bool(self._read_int8())
|
|
|
|
elif tag == "i":
|
2016-07-06 12:44:21 +08:00
|
|
|
return numpy.int32(self._read_int32())
|
2015-08-07 21:15:44 +08:00
|
|
|
elif tag == "I":
|
2016-07-06 12:44:21 +08:00
|
|
|
return numpy.int64(self._read_int64())
|
2015-08-07 21:15:44 +08:00
|
|
|
elif tag == "f":
|
|
|
|
return self._read_float64()
|
|
|
|
elif tag == "F":
|
|
|
|
numerator = self._read_int64()
|
|
|
|
denominator = self._read_int64()
|
|
|
|
return Fraction(numerator, denominator)
|
2015-08-09 07:17:19 +08:00
|
|
|
elif tag == "s":
|
|
|
|
return self._read_string()
|
2017-06-09 14:22:34 +08:00
|
|
|
elif tag == "B":
|
|
|
|
return self._read_bytes()
|
2017-06-09 15:10:30 +08:00
|
|
|
elif tag == "A":
|
|
|
|
return self._read_bytes()
|
2015-08-07 21:15:44 +08:00
|
|
|
elif tag == "l":
|
|
|
|
length = self._read_int32()
|
2016-05-16 22:30:21 +08:00
|
|
|
return [self._receive_rpc_value(embedding_map) for _ in range(length)]
|
2016-07-06 17:51:57 +08:00
|
|
|
elif tag == "a":
|
|
|
|
length = self._read_int32()
|
|
|
|
return numpy.array([self._receive_rpc_value(embedding_map) for _ in range(length)])
|
2015-08-09 07:17:19 +08:00
|
|
|
elif tag == "r":
|
2016-05-16 22:30:21 +08:00
|
|
|
start = self._receive_rpc_value(embedding_map)
|
|
|
|
stop = self._receive_rpc_value(embedding_map)
|
|
|
|
step = self._receive_rpc_value(embedding_map)
|
2015-08-10 01:17:00 +08:00
|
|
|
return range(start, stop, step)
|
2016-04-26 06:05:32 +08:00
|
|
|
elif tag == "k":
|
|
|
|
name = self._read_string()
|
2016-05-16 22:30:21 +08:00
|
|
|
value = self._receive_rpc_value(embedding_map)
|
2016-04-26 06:05:32 +08:00
|
|
|
return RPCKeyword(name, value)
|
2015-08-10 01:17:00 +08:00
|
|
|
elif tag == "O":
|
2016-05-16 22:30:21 +08:00
|
|
|
return embedding_map.retrieve_object(self._read_int32())
|
2015-08-07 21:15:44 +08:00
|
|
|
else:
|
2015-08-09 07:17:19 +08:00
|
|
|
raise IOError("Unknown RPC value tag: {}".format(repr(tag)))
|
2015-04-28 01:31:55 +08:00
|
|
|
|
2016-05-16 22:30:21 +08:00
|
|
|
def _receive_rpc_args(self, embedding_map):
|
2016-04-26 06:05:32 +08:00
|
|
|
args, kwargs = [], {}
|
2015-04-10 00:59:35 +08:00
|
|
|
while True:
|
2016-05-16 22:30:21 +08:00
|
|
|
value = self._receive_rpc_value(embedding_map)
|
2015-08-09 07:17:19 +08:00
|
|
|
if value is self._rpc_sentinel:
|
2016-04-26 06:05:32 +08:00
|
|
|
return args, kwargs
|
|
|
|
elif isinstance(value, RPCKeyword):
|
|
|
|
kwargs[value.name] = value.value
|
2015-12-18 18:03:07 +08:00
|
|
|
else:
|
|
|
|
args.append(value)
|
2015-04-10 00:59:35 +08:00
|
|
|
|
2015-08-10 01:17:00 +08:00
|
|
|
def _skip_rpc_value(self, tags):
|
|
|
|
tag = tags.pop(0)
|
|
|
|
if tag == "t":
|
|
|
|
length = tags.pop(0)
|
|
|
|
for _ in range(length):
|
|
|
|
self._skip_rpc_value(tags)
|
|
|
|
elif tag == "l":
|
|
|
|
self._skip_rpc_value(tags)
|
|
|
|
elif tag == "r":
|
|
|
|
self._skip_rpc_value(tags)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _send_rpc_value(self, tags, value, root, function):
|
|
|
|
def check(cond, expected):
|
|
|
|
if not cond:
|
|
|
|
raise RPCReturnValueError(
|
|
|
|
"type mismatch: cannot serialize {value} as {type}"
|
|
|
|
" ({function} has returned {root})".format(
|
|
|
|
value=repr(value), type=expected(),
|
|
|
|
function=function, root=root))
|
|
|
|
|
|
|
|
tag = chr(tags.pop(0))
|
|
|
|
if tag == "t":
|
|
|
|
length = tags.pop(0)
|
|
|
|
check(isinstance(value, tuple) and length == len(value),
|
|
|
|
lambda: "tuple of {}".format(length))
|
|
|
|
for elt in value:
|
|
|
|
self._send_rpc_value(tags, elt, root, function)
|
|
|
|
elif tag == "n":
|
|
|
|
check(value is None,
|
|
|
|
lambda: "None")
|
|
|
|
elif tag == "b":
|
|
|
|
check(isinstance(value, bool),
|
|
|
|
lambda: "bool")
|
|
|
|
self._write_int8(value)
|
|
|
|
elif tag == "i":
|
2016-09-09 16:52:13 +08:00
|
|
|
check(isinstance(value, (int, numpy.int32)) and
|
|
|
|
(-2**31 < value < 2**31-1),
|
2015-08-10 01:17:00 +08:00
|
|
|
lambda: "32-bit int")
|
|
|
|
self._write_int32(value)
|
|
|
|
elif tag == "I":
|
2016-09-09 16:52:13 +08:00
|
|
|
check(isinstance(value, (int, numpy.int32, numpy.int64)) and
|
|
|
|
(-2**63 < value < 2**63-1),
|
2015-08-10 01:17:00 +08:00
|
|
|
lambda: "64-bit int")
|
|
|
|
self._write_int64(value)
|
|
|
|
elif tag == "f":
|
|
|
|
check(isinstance(value, float),
|
|
|
|
lambda: "float")
|
|
|
|
self._write_float64(value)
|
|
|
|
elif tag == "F":
|
|
|
|
check(isinstance(value, Fraction) and
|
|
|
|
(-2**63 < value.numerator < 2**63-1) and
|
|
|
|
(-2**63 < value.denominator < 2**63-1),
|
|
|
|
lambda: "64-bit Fraction")
|
|
|
|
self._write_int64(value.numerator)
|
|
|
|
self._write_int64(value.denominator)
|
|
|
|
elif tag == "s":
|
|
|
|
check(isinstance(value, str) and "\x00" not in value,
|
|
|
|
lambda: "str")
|
|
|
|
self._write_string(value)
|
2017-06-09 14:22:34 +08:00
|
|
|
elif tag == "B":
|
|
|
|
check(isinstance(value, bytes),
|
|
|
|
lambda: "bytes")
|
|
|
|
self._write_bytes(value)
|
2017-06-09 15:10:30 +08:00
|
|
|
elif tag == "A":
|
|
|
|
check(isinstance(value, bytearray),
|
|
|
|
lambda: "bytearray")
|
|
|
|
self._write_bytes(value)
|
2015-08-10 01:17:00 +08:00
|
|
|
elif tag == "l":
|
|
|
|
check(isinstance(value, list),
|
|
|
|
lambda: "list")
|
|
|
|
self._write_int32(len(value))
|
|
|
|
for elt in value:
|
|
|
|
tags_copy = bytearray(tags)
|
|
|
|
self._send_rpc_value(tags_copy, elt, root, function)
|
|
|
|
self._skip_rpc_value(tags)
|
|
|
|
elif tag == "r":
|
|
|
|
check(isinstance(value, range),
|
|
|
|
lambda: "range")
|
|
|
|
tags_copy = bytearray(tags)
|
|
|
|
self._send_rpc_value(tags_copy, value.start, root, function)
|
|
|
|
tags_copy = bytearray(tags)
|
|
|
|
self._send_rpc_value(tags_copy, value.stop, root, function)
|
|
|
|
tags_copy = bytearray(tags)
|
|
|
|
self._send_rpc_value(tags_copy, value.step, root, function)
|
|
|
|
tags = tags_copy
|
|
|
|
else:
|
|
|
|
raise IOError("Unknown RPC value tag: {}".format(repr(tag)))
|
|
|
|
|
2017-06-25 12:39:08 +08:00
|
|
|
def _truncate_message(self, msg, limit=4096):
|
|
|
|
if len(msg) > limit:
|
|
|
|
return msg[0:limit] + "... (truncated)"
|
|
|
|
else:
|
|
|
|
return msg
|
|
|
|
|
2016-05-16 22:30:21 +08:00
|
|
|
def _serve_rpc(self, embedding_map):
|
2016-11-01 14:51:44 +08:00
|
|
|
async = self._read_bool()
|
|
|
|
service_id = self._read_int32()
|
2016-05-16 22:30:21 +08:00
|
|
|
args, kwargs = self._receive_rpc_args(embedding_map)
|
2016-04-26 06:05:32 +08:00
|
|
|
return_tags = self._read_bytes()
|
2016-11-01 18:30:42 +08:00
|
|
|
|
|
|
|
if service_id is 0:
|
|
|
|
service = lambda obj, attr, value: setattr(obj, attr, value)
|
|
|
|
else:
|
|
|
|
service = embedding_map.retrieve_object(service_id)
|
2016-11-01 14:51:44 +08:00
|
|
|
logger.debug("rpc service: [%d]%r%s %r %r -> %s", service_id, service,
|
|
|
|
(" (async)" if async else ""), args, kwargs, return_tags)
|
2015-08-07 21:15:44 +08:00
|
|
|
|
2016-11-01 18:30:42 +08:00
|
|
|
if async:
|
|
|
|
service(*args, **kwargs)
|
|
|
|
return
|
|
|
|
|
2015-08-08 21:01:08 +08:00
|
|
|
try:
|
2016-04-26 06:05:32 +08:00
|
|
|
result = service(*args, **kwargs)
|
2016-11-01 14:51:44 +08:00
|
|
|
logger.debug("rpc service: %d %r %r = %r", service_id, args, kwargs, result)
|
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
self._write_header(Request.RPCReply)
|
2016-11-01 18:30:42 +08:00
|
|
|
self._write_bytes(return_tags)
|
|
|
|
self._send_rpc_value(bytearray(return_tags), result, result, service)
|
2017-06-25 12:47:32 +08:00
|
|
|
except RPCReturnValueError as exn:
|
|
|
|
raise
|
2015-08-08 21:01:08 +08:00
|
|
|
except Exception as exn:
|
2016-04-26 06:05:32 +08:00
|
|
|
logger.debug("rpc service: %d %r %r ! %r", service_id, args, kwargs, exn)
|
2015-08-08 21:01:08 +08:00
|
|
|
|
2018-05-16 22:40:14 +08:00
|
|
|
self._write_header(Request.RPCException)
|
2015-08-08 21:01:08 +08:00
|
|
|
|
2016-06-26 18:50:02 +08:00
|
|
|
if hasattr(exn, "artiq_core_exception"):
|
2016-01-26 07:48:13 +08:00
|
|
|
exn = exn.artiq_core_exception
|
2015-12-31 21:54:54 +08:00
|
|
|
self._write_string(exn.name)
|
2017-06-25 12:39:08 +08:00
|
|
|
self._write_string(self._truncate_message(exn.message))
|
2015-12-31 21:54:54 +08:00
|
|
|
for index in range(3):
|
|
|
|
self._write_int64(exn.param[index])
|
|
|
|
|
|
|
|
filename, line, column, function = exn.traceback[-1]
|
|
|
|
self._write_string(filename)
|
|
|
|
self._write_int32(line)
|
|
|
|
self._write_int32(column)
|
|
|
|
self._write_string(function)
|
|
|
|
else:
|
|
|
|
exn_type = type(exn)
|
2018-08-07 13:53:13 +08:00
|
|
|
if exn_type in (ZeroDivisionError, ValueError, IndexError, RuntimeError) or \
|
2016-06-26 18:50:02 +08:00
|
|
|
hasattr(exn, "artiq_builtin"):
|
2015-12-31 21:54:54 +08:00
|
|
|
self._write_string("0:{}".format(exn_type.__name__))
|
|
|
|
else:
|
2016-05-16 22:30:21 +08:00
|
|
|
exn_id = embedding_map.store_object(exn_type)
|
2015-12-31 21:54:54 +08:00
|
|
|
self._write_string("{}:{}.{}".format(exn_id,
|
2017-06-25 12:39:08 +08:00
|
|
|
exn_type.__module__,
|
|
|
|
exn_type.__qualname__))
|
|
|
|
self._write_string(self._truncate_message(str(exn)))
|
2015-12-31 21:54:54 +08:00
|
|
|
for index in range(3):
|
|
|
|
self._write_int64(0)
|
|
|
|
|
2016-04-26 06:05:32 +08:00
|
|
|
tb = traceback.extract_tb(exn.__traceback__, 2)
|
|
|
|
if len(tb) == 2:
|
|
|
|
(_, (filename, line, function, _), ) = tb
|
|
|
|
elif len(tb) == 1:
|
|
|
|
((filename, line, function, _), ) = tb
|
|
|
|
else:
|
|
|
|
assert False
|
2015-12-31 21:54:54 +08:00
|
|
|
self._write_string(filename)
|
|
|
|
self._write_int32(line)
|
|
|
|
self._write_int32(-1) # column not known
|
|
|
|
self._write_string(function)
|
2015-08-08 21:01:08 +08:00
|
|
|
|
2016-05-16 22:30:21 +08:00
|
|
|
def _serve_exception(self, embedding_map, symbolizer, demangler):
|
2015-08-08 21:01:08 +08:00
|
|
|
name = self._read_string()
|
|
|
|
message = self._read_string()
|
|
|
|
params = [self._read_int64() for _ in range(3)]
|
|
|
|
|
|
|
|
filename = self._read_string()
|
|
|
|
line = self._read_int32()
|
|
|
|
column = self._read_int32()
|
|
|
|
function = self._read_string()
|
|
|
|
|
|
|
|
backtrace = [self._read_int32() for _ in range(self._read_int32())]
|
|
|
|
|
2015-08-10 20:12:22 +08:00
|
|
|
traceback = list(reversed(symbolizer(backtrace))) + \
|
2016-05-14 20:52:28 +08:00
|
|
|
[(filename, line, column, *demangler([function]), None)]
|
2016-01-26 07:48:13 +08:00
|
|
|
core_exn = exceptions.CoreException(name, message, params, traceback)
|
2015-12-31 21:54:54 +08:00
|
|
|
|
2016-01-26 07:48:13 +08:00
|
|
|
if core_exn.id == 0:
|
|
|
|
python_exn_type = getattr(exceptions, core_exn.name.split('.')[-1])
|
2015-12-31 21:54:54 +08:00
|
|
|
else:
|
2016-05-16 22:30:21 +08:00
|
|
|
python_exn_type = embedding_map.retrieve_object(core_exn.id)
|
2015-12-31 21:54:54 +08:00
|
|
|
|
2016-01-11 02:06:28 +08:00
|
|
|
python_exn = python_exn_type(message.format(*params))
|
2016-01-26 07:48:13 +08:00
|
|
|
python_exn.artiq_core_exception = core_exn
|
2015-12-31 21:54:54 +08:00
|
|
|
raise python_exn
|
2015-08-07 16:44:49 +08:00
|
|
|
|
2016-05-16 22:30:21 +08:00
|
|
|
def serve(self, embedding_map, symbolizer, demangler):
|
2015-04-10 00:59:35 +08:00
|
|
|
while True:
|
2015-08-07 21:15:44 +08:00
|
|
|
self._read_header()
|
2018-05-16 22:40:14 +08:00
|
|
|
if self._read_type == Reply.RPCRequest:
|
2016-05-16 22:30:21 +08:00
|
|
|
self._serve_rpc(embedding_map)
|
2018-05-16 22:40:14 +08:00
|
|
|
elif self._read_type == Reply.KernelException:
|
2016-05-16 22:30:21 +08:00
|
|
|
self._serve_exception(embedding_map, symbolizer, demangler)
|
2018-05-16 22:40:14 +08:00
|
|
|
elif self._read_type == Reply.WatchdogExpired:
|
2016-03-19 06:29:42 +08:00
|
|
|
raise exceptions.WatchdogExpired
|
2018-05-16 22:40:14 +08:00
|
|
|
elif self._read_type == Reply.ClockFailure:
|
2016-03-19 06:29:42 +08:00
|
|
|
raise exceptions.ClockFailure
|
2015-04-10 00:59:35 +08:00
|
|
|
else:
|
2018-05-16 22:40:14 +08:00
|
|
|
self._read_expect(Reply.KernelFinished)
|
2015-08-07 21:15:44 +08:00
|
|
|
return
|