Skip to content

Commit 4048568

Browse files
committedMay 2, 2015
support kernel handover with coherent time
1 parent cb65b1e commit 4048568

File tree

10 files changed

+129
-29
lines changed

10 files changed

+129
-29
lines changed
 

‎artiq/coredevice/comm_generic.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from enum import Enum
44
from fractions import Fraction
55

6-
from artiq.coredevice.runtime import Environment
76
from artiq.coredevice import runtime_exceptions
87
from artiq.language import core as core_language
98
from artiq.coredevice.rpc_wrapper import RPCWrapper
@@ -89,7 +88,7 @@ def _write_header(self, length, ty):
8988
logger.debug("sending message: type=%r length=%d", ty, length)
9089
self.write(struct.pack(">llB", 0x5a5a5a5a, length, ty.value))
9190

92-
def get_runtime_env(self):
91+
def check_ident(self):
9392
self._write_header(9, _H2DMsgType.IDENT_REQUEST)
9493
_, ty = self._read_header()
9594
if ty != _D2HMsgType.IDENT_REPLY:
@@ -102,7 +101,6 @@ def get_runtime_env(self):
102101
if runtime_id != "AROR":
103102
raise UnsupportedDevice("Unsupported runtime ID: {}"
104103
.format(runtime_id))
105-
return Environment()
106104

107105
def switch_clock(self, external):
108106
self._write_header(10, _H2DMsgType.SWITCH_CLOCK)
@@ -118,8 +116,9 @@ def load(self, kcode):
118116
if ty != _D2HMsgType.LOAD_COMPLETED:
119117
raise IOError("Incorrect reply from device: "+str(ty))
120118

121-
def run(self, kname):
122-
self._write_header(len(kname) + 9, _H2DMsgType.RUN_KERNEL)
119+
def run(self, kname, reset_now):
120+
self._write_header(len(kname) + 10, _H2DMsgType.RUN_KERNEL)
121+
self.write(struct.pack("B", reset_now))
123122
self.write(bytes(kname, "ascii"))
124123
logger.debug("running kernel: %s", kname)
125124

‎artiq/coredevice/core.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from artiq.transforms.lower_time import lower_time
1616
from artiq.transforms.unparse import unparse
1717

18+
from artiq.coredevice.runtime import Environment
19+
1820
from artiq.py2llvm import get_runtime_binary
1921

2022

@@ -52,12 +54,10 @@ class DBKeys:
5254
external_clock = Argument(False)
5355

5456
def build(self):
55-
self.runtime_env = self.comm.get_runtime_env()
57+
self.first_run = True
5658
self.core = self
5759
self.comm.core = self
58-
59-
self.comm.switch_clock(self.external_clock)
60-
self.initial_time = int64(self.runtime_env.warmup_time/self.ref_period)
60+
self.runtime_env = Environment()
6161

6262
def transform_stack(self, func_def, rpc_map, exception_map,
6363
debug_unparse=_no_debug_unparse):
@@ -79,7 +79,7 @@ def transform_stack(self, func_def, rpc_map, exception_map,
7979
interleave(func_def)
8080
debug_unparse("interleave", func_def)
8181

82-
lower_time(func_def, self.initial_time)
82+
lower_time(func_def)
8383
debug_unparse("lower_time", func_def)
8484

8585
remove_inter_assigns(func_def)
@@ -113,17 +113,22 @@ def compile(self, k_function, k_args, k_kwargs, with_attr_writeback=True):
113113
return binary, rpc_map, exception_map
114114

115115
def run(self, k_function, k_args, k_kwargs):
116+
if self.first_run:
117+
self.comm.check_ident()
118+
self.comm.switch_clock(self.external_clock)
119+
116120
binary, rpc_map, exception_map = self.compile(
117121
k_function, k_args, k_kwargs)
118122
self.comm.load(binary)
119-
self.comm.run(k_function.__name__)
123+
self.comm.run(k_function.__name__, self.first_run)
120124
self.comm.serve(rpc_map, exception_map)
125+
self.first_run = False
121126

122127
@kernel
123128
def get_rtio_time(self):
124129
return cycles_to_time(syscall("rtio_get_counter"))
125130

126131
@kernel
127132
def recover_underflow(self):
128-
t = syscall("rtio_get_counter") + self.initial_time
133+
t = syscall("rtio_get_counter") + 125000
129134
at(cycles_to_time(t))

‎artiq/coredevice/runtime.py

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
llvm.initialize_all_asmprinters()
1313

1414
_syscalls = {
15+
"now_init": "n:I",
16+
"now_save": "I:n",
1517
"watchdog_set": "i:i",
1618
"watchdog_clear": "i:n",
1719
"rtio_set_o": "Iib:n",

‎artiq/frontend/artiq_run.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DBKeys:
2727
def run(self):
2828
with open(self.file, "rb") as f:
2929
self.core.comm.load(f.read())
30-
self.core.comm.run("run")
30+
self.core.comm.run("run", False)
3131
self.core.comm.serve(dict(), dict())
3232

3333

‎artiq/transforms/lower_time.py

+19-8
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414

1515
import ast
1616

17-
from artiq.transforms.tools import value_to_ast
18-
from artiq.language.core import int64
19-
2017

2118
class _TimeLowerer(ast.NodeTransformer):
2219
def visit_Call(self, node):
@@ -45,9 +42,23 @@ def visit_Expr(self, node):
4542
return r
4643

4744

48-
def lower_time(func_def, initial_time):
45+
def lower_time(func_def):
4946
_TimeLowerer().visit(func_def)
50-
func_def.body.insert(0, ast.copy_location(
51-
ast.Assign(targets=[ast.Name("now", ast.Store())],
52-
value=value_to_ast(int64(initial_time))),
53-
func_def))
47+
call_init = ast.Call(
48+
func=ast.Name("syscall", ast.Load()),
49+
args=[ast.Str("now_init")],
50+
keywords=[], starargs=None, kwargs=None)
51+
stmt_init = ast.Assign(targets=[ast.Name("now", ast.Store())],
52+
value=call_init)
53+
call_save = ast.Call(
54+
func=ast.Name("syscall", ast.Load()),
55+
args=[ast.Str("now_save"), ast.Name("now", ast.Load())],
56+
keywords=[], starargs=None, kwargs=None)
57+
stmt_save = ast.Expr(call_save)
58+
func_def.body = [
59+
stmt_init,
60+
ast.Try(body=func_def.body,
61+
handlers=[],
62+
orelse=[],
63+
finalbody=[stmt_save])
64+
]
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from artiq import *
2+
3+
4+
class Handover(Experiment, AutoDB):
5+
class DBKeys:
6+
core = Device()
7+
led = Device()
8+
9+
@kernel
10+
def blink_once(self):
11+
self.led.pulse(250*ms)
12+
delay(250*ms)
13+
14+
def run(self):
15+
while True:
16+
self.blink_once()

‎soc/runtime/gen_service_table.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
services = [
99
("syscalls", [
10+
("now_init", "now_init"),
11+
("now_save", "now_save"),
1012
("rpc", "rpc"),
1113
("watchdog_set", "watchdog_set"),
1214
("watchdog_clear", "watchdog_clear"),

‎soc/runtime/ksupport.c

+40-7
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ void exception_handler(unsigned long vect, unsigned long *sp);
1818
void exception_handler(unsigned long vect, unsigned long *sp)
1919
{
2020
struct msg_exception msg;
21-
int i;
2221

2322
msg.type = MESSAGE_TYPE_EXCEPTION;
2423
msg.eid = EID_INTERNAL_ERROR;
25-
for(i=0;i<3;i++)
26-
msg.eparams[i] = 0;
24+
msg.eparams[0] = 256;
25+
msg.eparams[1] = 256;
26+
msg.eparams[2] = 256;
2727
mailbox_send_and_wait(&msg);
2828
while(1);
2929
}
@@ -51,8 +51,6 @@ int main(void)
5151
} else {
5252
struct msg_base msg;
5353

54-
dds_init();
55-
rtio_init();
5654
k();
5755
exception_pop(1);
5856

@@ -63,6 +61,41 @@ int main(void)
6361
while(1);
6462
}
6563

64+
long long int now_init(void);
65+
long long int now_init(void)
66+
{
67+
struct msg_base request;
68+
struct msg_now_init_reply *reply;
69+
long long int now;
70+
71+
request.type = MESSAGE_TYPE_NOW_INIT_REQUEST;
72+
mailbox_send_and_wait(&request);
73+
74+
reply = mailbox_wait_and_receive();
75+
if(reply->type != MESSAGE_TYPE_NOW_INIT_REPLY)
76+
exception_raise_params(EID_INTERNAL_ERROR, 1, 0, 0);
77+
now = reply->now;
78+
mailbox_acknowledge();
79+
80+
if(now < 0) {
81+
dds_init();
82+
rtio_init();
83+
now = 125000;
84+
}
85+
86+
return now;
87+
}
88+
89+
void now_save(long long int now);
90+
void now_save(long long int now)
91+
{
92+
struct msg_now_save request;
93+
94+
request.type = MESSAGE_TYPE_NOW_SAVE;
95+
request.now = now;
96+
mailbox_send_and_wait(&request);
97+
}
98+
6699
int watchdog_set(int ms)
67100
{
68101
struct msg_watchdog_set_request request;
@@ -75,7 +108,7 @@ int watchdog_set(int ms)
75108

76109
reply = mailbox_wait_and_receive();
77110
if(reply->type != MESSAGE_TYPE_WATCHDOG_SET_REPLY)
78-
exception_raise(EID_INTERNAL_ERROR);
111+
exception_raise_params(EID_INTERNAL_ERROR, 2, 0, 0);
79112
id = reply->id;
80113
mailbox_acknowledge();
81114

@@ -105,7 +138,7 @@ int rpc(int rpc_num, ...)
105138

106139
reply = mailbox_wait_and_receive();
107140
if(reply->type != MESSAGE_TYPE_RPC_REPLY)
108-
exception_raise(EID_INTERNAL_ERROR);
141+
exception_raise_params(EID_INTERNAL_ERROR, 3, 0, 0);
109142
eid = reply->eid;
110143
retval = reply->retval;
111144
mailbox_acknowledge();

‎soc/runtime/messages.h

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <stdarg.h>
55

66
enum {
7+
MESSAGE_TYPE_NOW_INIT_REQUEST,
8+
MESSAGE_TYPE_NOW_INIT_REPLY,
9+
MESSAGE_TYPE_NOW_SAVE,
710
MESSAGE_TYPE_FINISHED,
811
MESSAGE_TYPE_EXCEPTION,
912
MESSAGE_TYPE_WATCHDOG_SET_REQUEST,
@@ -30,6 +33,16 @@ struct msg_base {
3033

3134
/* kernel messages */
3235

36+
struct msg_now_init_reply {
37+
int type;
38+
long long int now;
39+
};
40+
41+
struct msg_now_save {
42+
int type;
43+
long long int now;
44+
};
45+
3346
struct msg_exception {
3447
int type;
3548
int eid;

‎soc/runtime/session.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static void submit_output(int len)
5454
}
5555

5656
static int user_kernel_state;
57+
static long long int now;
5758

5859
enum {
5960
USER_KERNEL_NONE = 0,
@@ -167,7 +168,10 @@ static int process_input(void)
167168
}
168169
buffer_in[buffer_in_index] = 0;
169170

170-
k = kloader_find((char *)&buffer_in[9]);
171+
if(buffer_in[9])
172+
now = -1;
173+
174+
k = kloader_find((char *)&buffer_in[10]);
171175
if(k == NULL) {
172176
log("Failed to find kernel entry point '%s' in object", &buffer_in[9]);
173177
buffer_out[8] = REMOTEMSG_TYPE_KERNEL_STARTUP_FAILED;
@@ -394,6 +398,21 @@ static int process_kmsg(struct msg_base *umsg)
394398
return 0;
395399

396400
switch(umsg->type) {
401+
case MESSAGE_TYPE_NOW_INIT_REQUEST: {
402+
struct msg_now_init_reply reply;
403+
404+
reply.type = MESSAGE_TYPE_NOW_INIT_REPLY;
405+
reply.now = now;
406+
mailbox_send_and_wait(&reply);
407+
break;
408+
}
409+
case MESSAGE_TYPE_NOW_SAVE: {
410+
struct msg_now_save *msg = (struct msg_now_save *)umsg;
411+
412+
now = msg->now;
413+
mailbox_acknowledge();
414+
break;
415+
}
397416
case MESSAGE_TYPE_FINISHED:
398417
buffer_out[8] = REMOTEMSG_TYPE_KERNEL_FINISHED;
399418
submit_output(9);

0 commit comments

Comments
 (0)
Please sign in to comment.