-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisagg.pyx
More file actions
173 lines (148 loc) · 5.54 KB
/
disagg.pyx
File metadata and controls
173 lines (148 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
cimport clibd
from libc cimport errno
from libc.stdlib cimport malloc, free
from functools import partial
# Pack APIs to pure python objects
cdef class LibdAction:
cdef clibd.libd_action * _c_action
cdef public object transports
cdef public object raw_transports
cdef public object cv
cdef public object runtime
def __cinit__(self, cv, str aid, **kwargs):
# prepare args for init call
strargs = []
for k, v in kwargs.items():
strargs.append(('+' + k).encode('ascii'))
if isinstance(v, list):
for i in v:
strargs.append(str(i).encode('ascii'))
else:
strargs.append(str(v).encode('ascii'))
cdef int argc = len(strargs)
cdef char ** argv = <char **>malloc(argc * sizeof(char *))
for i in range(argc):
argv[i] = strargs[i]
self.cv = cv
self._c_action = clibd.libd_action_init(
aid.encode('ascii'), argc, argv)
if self._c_action is NULL:
raise MemoryError()
free(argv)
# Python init part, I assume they have same objects
def __init__(self, *args, **kwargs):
self.runtime = None
self.transports = {}
self.raw_transports = {}
# make sure action structure is freed.
def __dealloc__(self):
if self._c_action is not NULL:
clibd.libd_action_free(self._c_action)
def terminate(self):
if self._c_action is not NULL:
clibd.libd_action_free(self._c_action)
self._c_action = NULL
def profile(self, int point):
clibd.libd_plugin_invoke(self._c_action, b"monitor", point, NULL)
# Member Functions
def add_transport(self, durl):
c_durl = durl.encode('ascii')
# cache the trans
name = durl.split(';')[0]
ret = clibd.libd_action_add_transport(self._c_action, c_durl)
if ret >= 0:
# cache the
self.raw_transports[name] = LibdTransport(self, name)
return ret
def config_transport(self, name, durl):
ret = clibd.libd_action_config_transport(
self._c_action, name.encode('ascii'), durl.encode('ascii'))
with self.cv:
self.cv.notify_all()
return ret
def get_transport(self, name, ttype):
if name not in self.transports:
# raise exception if type is not found
# delay construct of object by function wrap
trans = {
'rdma': partial(LibdTransportRDMA, self),
'rdma_server': partial(LibdTransportRDMAServer, self),
}[ttype](name)
self.transports[name] = trans
# raise exception if name is not found
return self.transports[name]
# Python APIs
# Reset methods for reuse of this instance
cdef class LibdTransport:
cdef clibd.libd_transport * _c_trans
cdef object action
def __cinit__(self, LibdAction action, str name, *argv):
self.action = action
self._c_trans = clibd.libd_action_get_transport(
action._c_action, name.encode('ascii'))
# TODO: spin here if we cannot get a transport
if self._c_trans is NULL:
raise MemoryError()
# TODO: check this
def get_msg(self):
cdef int msg_size
if self._c_trans is NULL:
raise MemoryError()
msg = <bytes>clibd.libd_transport_get_message(
self._c_trans, &msg_size)
return msg_size, msg.decode('ascii')
# we do not need to have __dealloc__ for all transports, clib will handle this
cdef class LibdTransportRDMAServer(LibdTransport):
def serve(self):
while True:
ret = clibd.libd_trdma_server_serve(self._c_trans);
if ret != -errno.EAGAIN:
return ret
# TODO: can be interrupted before it get the lock.
with self.action.cv:
self.action.cv.wait()
cdef class LibdTransportRDMA(LibdTransport):
# use of buf is required
cdef char * _c_buf
cdef size_t size
cdef object initd
cdef public char [:] buf
def __cinit__(self, LibdAction action, str name, *argv):
pass
def __init__(self, *args):
self.initd = False
def reg(self, size, LibdTransportRDMA trans = None):
if trans != None:
self._c_buf = <char *> clibd.libd_trdma_reg(
self._c_trans, size, <void *>trans._c_buf)
else:
self._c_buf = <char *> clibd.libd_trdma_reg(
self._c_trans, size, self._c_buf)
if self._c_buf is NULL:
raise MemoryError()
self.size = size
self.initd = True
self.buf = <char [:size]>self._c_buf
def read(self, size, addr, int offset = 0):
if (self.initd == False):
raise MemoryError()
# block here
while True:
ret = clibd.libd_trdma_read(self._c_trans, size, addr,
self._c_buf + offset)
if ret != -errno.EAGAIN:
return ret
# TODO: can be interrupted before it get the lock.
with self.action.cv:
self.action.cv.wait()
def write(self, size, addr, int offset = 0):
if (self.initd == False):
raise MemoryError()
# block here
while True:
ret = clibd.libd_trdma_write(self._c_trans, size, addr,
self._c_buf + offset)
if ret != -errno.EAGAIN:
return ret
with self.action.cv:
self.action.cv.wait()