2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2014-2019. The SimGrid Team. All rights reserved.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
15 def __init__(self, name, thetype):
20 return self.simcall_types[self.type]
26 class Simcall(object):
30 def __init__(self, name, handler, res, args, call_kind):
34 self.need_handler = handler
35 self.call_kind = call_kind
38 # libsmx.c simcall_BODY_
39 if self.simcalls_body is None:
40 f = open('libsmx.cpp')
41 self.simcalls_body = set(re.findall(r'simcall_BODY_(.*?)\(', f.read()))
43 if self.name not in self.simcalls_body:
44 print ('# ERROR: No function calling simcall_BODY_%s' % self.name)
45 print ('# Add something like this to libsmx.c:')
46 print ('%s simcall_%s(%s)' % (self.res.rettype(), self.name, ', '.
47 join('%s %s' % (arg.rettype(), arg.name) for arg in self.args)))
49 print (' return simcall_BODY_%s(%s);' % (self.name, "..."))
53 # smx_*.c void simcall_HANDLER_host_on(smx_simcall_t simcall,
55 if self.simcalls_pre is None:
56 self.simcalls_pre = set()
57 for fn in glob.glob('smx_*') + glob.glob('../kernel/actor/ActorImpl*') + \
58 glob.glob('../mc/*cpp') + glob.glob('../kernel/activity/*cpp'):
60 self.simcalls_pre |= set(re.findall(r'simcall_HANDLER_(.*?)\(', f.read()))
63 if self.name not in self.simcalls_pre:
64 print ('# ERROR: No function called simcall_HANDLER_%s' % self.name)
65 print ('# Add something like this to the relevant C file (like smx_io.c if it\'s an IO call):')
66 print ('%s simcall_HANDLER_%s(smx_simcall_t simcall%s)' % (self.res.rettype(), self.name, ''.
67 join(', %s %s' % (arg.rettype(), arg.name)for arg in self.args)))
69 print (' // Your code handling the simcall')
73 if self.name in self.simcalls_pre:
75 '# ERROR: You have a function called simcall_HANDLER_%s, but that simcall is not using any handler' %
77 print ('# Either change your simcall definition, or kill that function')
82 return ' SIMCALL_%s,' % (self.name.upper())
85 return ' "SIMCALL_%s",' % self.name.upper()
90 regex = re.compile(r"^boost::intrusive_ptr<(.*?)>(.*)$") # to compute the raw type
91 # Arguments getter/setters
92 for i in range(len(self.args)):
94 rawtype = regex.sub(r'\1*\2', arg.rettype())
95 res.append('static inline %s simcall_%s__get__%s(smx_simcall_t simcall)' % (
96 arg.rettype(), self.name, arg.name))
98 res.append(' return simgrid::simix::unmarshal<%s>(simcall->args[%i]);' % (arg.rettype(), i))
100 res.append('static inline %s simcall_%s__getraw__%s(smx_simcall_t simcall)' % (
101 rawtype, self.name, arg.name))
103 res.append(' return simgrid::simix::unmarshal_raw<%s>(simcall->args[%i]);' % (rawtype, i))
105 res.append('static inline void simcall_%s__set__%s(smx_simcall_t simcall, %s arg)' % (
106 self.name, arg.name, arg.rettype()))
108 res.append(' simgrid::simix::marshal<%s>(simcall->args[%i], arg);' % (arg.rettype(), i))
111 # Return value getter/setters
112 if self.res.type != 'void':
113 rawtype = regex.sub(r'\1*\2', self.res.rettype())
115 'static inline %s simcall_%s__get__result(smx_simcall_t simcall)' % (self.res.rettype(), self.name))
117 res.append(' return simgrid::simix::unmarshal<%s>(simcall->result);' % self.res.rettype())
119 res.append('static inline %s simcall_%s__getraw__result(smx_simcall_t simcall)' % (rawtype, self.name))
121 res.append(' return simgrid::simix::unmarshal_raw<%s>(simcall->result);' % rawtype)
124 'static inline void simcall_%s__set__result(smx_simcall_t simcall, %s result)' % (self.name, self.res.rettype()))
126 res.append(' simgrid::simix::marshal<%s>(simcall->result, result);' % (self.res.rettype()))
128 return '\n'.join(res)
133 args = ["simgrid::simix::unmarshal<%s>(simcall.args[%d])" % (arg.rettype(), i)
134 for i, arg in enumerate(self.args)]
135 res.append(indent + 'case SIMCALL_%s:' % (self.name.upper()))
136 if self.need_handler:
137 call = "simcall_HANDLER_%s(&simcall%s%s)" % (self.name,
138 ", " if len(args) > 0 else "",
141 call = "SIMIX_%s(%s)" % (self.name, ', '.join(args))
142 if self.call_kind == 'Func':
143 res.append(indent + " simgrid::simix::marshal<%s>(simcall.result, %s);" % (self.res.rettype(), call))
145 res.append(indent + " " + call + ";")
146 if self.call_kind != 'Blck':
147 res.append(indent + ' simcall_answer();')
148 res.append(indent + ' break;')
150 return '\n'.join(res)
155 'inline static %s simcall_BODY_%s(%s)' % (self.res.rettype(),
157 ', '.join('%s %s' % (arg.rettype(), arg.name) for arg in self.args)))
159 res.append(' if (0) /* Go to that function to follow the code flow through the simcall barrier */')
160 if self.need_handler:
161 res.append(' simcall_HANDLER_%s(%s);' % (self.name,
162 ', '.join(["&SIMIX_process_self()->simcall"] + [arg.name for arg in self.args])))
164 res.append(' SIMIX_%s(%s);' % (self.name,
165 ', '.join(arg.name for arg in self.args)))
166 res.append(' return simcall<%s%s>(SIMCALL_%s%s);' % (
168 "".join([", " + arg.rettype() for i, arg in enumerate(self.args)]),
170 "".join([", " + arg.name for i, arg in enumerate(self.args)])
173 return '\n'.join(res)
175 def handler_prototype(self):
176 if self.need_handler:
177 return "XBT_PRIVATE %s simcall_HANDLER_%s(smx_simcall_t simcall%s);" % (self.res.rettype() if self.call_kind == 'Func' else 'void',
179 ''.join(', %s %s' % (arg.rettype(), arg.name)
180 for i, arg in enumerate(self.args)))
188 simcalls_guarded = {}
189 for line in open(fn).read().split('\n'):
190 if line.startswith('##'):
192 simcalls_guarded[re.search(r'## *(.*)', line).group(1)] = resdi
193 if line.startswith('#') or not line:
196 r'^(\S+)\s+([^\)\(\s]+)\s*\(*(.*)\)\s*(\[\[.*\]\])?\s*;\s*?$', line)
198 raise AssertionError(line)
199 ret, name, args, attrs = match.groups()
201 if not re.match(r"^\s*$", args):
202 for arg in re.split(",", args):
204 match = re.match(r"^(.*?)\s*?(\S+)$", arg)
205 t, n = match.groups()
208 sargs.append(Arg(n, t))
216 for attr in re.split(",", attrs):
219 elif attr == "nohandler":
222 raise AssertionError("Unknown attribute %s in: %s" % (attr, line))
223 sim = Simcall(name, handler, Arg('result', ret), sargs, ans)
228 return simcalls, simcalls_guarded
234 '/**********************************************************************/\n')
236 '/* File generated by src/simix/simcalls.py from src/simix/simcalls.in */\n')
240 '/* DO NOT EVER CHANGE THIS FILE */\n')
244 '/* change simcalls specification in src/simix/simcalls.in */\n')
246 '/* Copyright (c) 2014-2019. The SimGrid Team. All rights reserved. */\n')
248 '/**********************************************************************/\n\n')
251 ' * Note that the name comes from http://en.wikipedia.org/wiki/Popping\n')
253 ' * Indeed, the control flow is doing a strange dance in there.\n')
256 ' * That\'s not about http://en.wikipedia.org/wiki/Poop, despite the odor :)\n')
261 def handle(fd, func, simcalls, guarded_simcalls):
264 fd.write('\n'.join(filter(nonempty, (func(simcall) for simcall in simcalls))))
266 for guard, ll in guarded_simcalls.items():
267 fd.write('\n#if %s\n' % (guard))
268 fd.write('\n'.join(func(simcall) for simcall in ll))
273 if __name__ == '__main__':
274 simcalls, simcalls_dict = parse('simcalls.in')
277 ok &= all(map(Simcall.check, simcalls))
278 for k, v in simcalls_dict.items():
279 ok &= all(map(Simcall.check, v))
281 print ("Some checks fail!")
285 # popping_accessors.hpp
287 fd = header('popping_accessors.hpp')
288 fd.write('#include "src/simix/popping_private.hpp"')
289 handle(fd, Simcall.accessors, simcalls, simcalls_dict)
291 "\n/* The prototype of all simcall handlers, automatically generated for you */\n\n")
292 handle(fd, Simcall.handler_prototype, simcalls, simcalls_dict)
298 fd = header("popping_enum.h")
300 fd.write(' * @brief All possible simcalls.\n')
302 fd.write('typedef enum {\n')
303 fd.write(' SIMCALL_NONE,\n')
305 handle(fd, Simcall.enum, simcalls, simcalls_dict)
307 fd.write(' NUM_SIMCALLS\n')
308 fd.write('} e_smx_simcall_t;\n')
312 # popping_generated.cpp
315 fd = header("popping_generated.cpp")
317 fd.write('#include "smx_private.hpp"\n')
318 fd.write('#include <xbt/base.h>\n')
319 fd.write('#if SIMGRID_HAVE_MC\n')
320 fd.write('#include "src/mc/mc_forward.hpp"\n')
322 fd.write('#include "src/kernel/activity/ConditionVariableImpl.hpp"\n')
325 fd.write('XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_popping);\n\n')
328 '/** @brief Simcalls\' names (generated from src/simix/simcalls.in) */\n')
329 fd.write('const char* simcall_names[] = {\n')
331 fd.write(' "SIMCALL_NONE",\n')
332 handle(fd, Simcall.string, simcalls, simcalls_dict)
336 fd.write('/** @private\n')
338 ' * @brief (in kernel mode) unpack the simcall and activate the handler\n')
340 fd.write(' * This function is generated from src/simix/simcalls.in\n')
343 'void simgrid::kernel::actor::ActorImpl::simcall_handle(int value) {\n')
345 ' XBT_DEBUG("Handling simcall %p: %s", &simcall, SIMIX_simcall_name(simcall.call));\n')
346 fd.write(' SIMCALL_SET_MC_VALUE(simcall, value);\n')
348 ' if (context_->iwannadie)\n')
349 fd.write(' return;\n')
350 fd.write(' switch (simcall.call) {\n')
352 handle(fd, Simcall.case, simcalls, simcalls_dict)
354 fd.write(' case NUM_SIMCALLS:\n')
355 fd.write(' break;\n')
356 fd.write(' case SIMCALL_NONE:\n')
357 fd.write(' throw std::invalid_argument(simgrid::xbt::string_printf("Asked to do the noop syscall on %s@%s",\n')
358 fd.write(' get_cname(),\n')
359 fd.write(' sg_host_get_name(get_host())));\n')
360 fd.write(' default:\n')
361 fd.write(' THROW_IMPOSSIBLE;\n')
370 fd = header('popping_bodies.cpp')
371 fd.write('#include "smx_private.hpp"\n')
372 fd.write('#include "src/mc/mc_forward.hpp"\n')
373 fd.write('#include "xbt/ex.h"\n')
374 fd.write('#include <functional>\n')
375 fd.write('#include <simgrid/simix.hpp>\n')
376 fd.write('#include <xbt/log.h>\n')
378 fd.write("/** @cond */ // Please Doxygen, don't look at this\n")
380 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix);
382 template<class R, class... T>
383 inline static R simcall(e_smx_simcall_t call, T const&... t)
385 smx_actor_t self = SIMIX_process_self();
386 simgrid::simix::marshal(&self->simcall, call, t...);
387 if (self != simix_global->maestro_process) {
388 XBT_DEBUG("Yield process '%s' on simcall %s (%d)", self->get_cname(), SIMIX_simcall_name(self->simcall.call),
389 (int)self->simcall.call);
392 self->simcall_handle(0);
394 return simgrid::simix::unmarshal<R>(self->simcall.result);
397 handle(fd, Simcall.body, simcalls, simcalls_dict)
398 fd.write("/** @endcond */\n")