pyembed: integer python into QEMU
RFC PATCH 1/1 pyembed: integer python into QEMU
This is something I started during 1.0-rc on a lark and spent some time last night actually making work. I'm sending it only to show that (1) it's possible and (2) to get some input about what other people think as a longer term direction.
I see a couple possibilities here:
1. We could code portions of QEMU as call-outs to Python. Command line parsing, GUI interface, etc. are all good candidates to be moved to Python.
2. We could support loading of user-supplied Python modules. This would be convenient as an extension mechanism since a HIL binding provide a clean interface that we could make more stable than just loading a random .so or including a .c file into the build.
3. We could use this as an super tracing mechanism. I can imagine using something like this in a production environment to trouble shoot a complex problem.
I'm not totally decided here myself on whether any of this is a good idea but since this has come up so many times in the past, I figured it's at least worth exploring.
--- Makefile.objs | 2 + configure | 32 ++++++++ hw/hw.h | 11 +++ pyembed.c | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pyembed.h | 8 ++ savevm.c | 17 ++++ vl.c | 7 ++ 7 files changed, 321 insertions(+), 0 deletions(-) create mode 100644 pyembed.c create mode 100644 pyembed.h
Makefile.objs +2 -0
@@ -293,6 +293,8 @@ hw-obj-$(CONFIG_DP8393X) += dp8393x.o hw-obj-$(CONFIG_DS1225Y) += ds1225y.o hw-obj-$(CONFIG_MIPSNET) += mipsnet.o +hw-obj-$(CONFIG_PYTHON) += pyembed.o + # Sound sound-obj-y = sound-obj-$(CONFIG_SB16) += sb16.o
configure +32 -0
@@ -185,6 +185,7 @@ opengl="" zlib="yes" guest_agent="yes" libiscsi="" +libpython="" # parse CC options first for opt do
@@ -668,6 +669,10 @@ for opt do ;; --enable-libiscsi) libiscsi="yes" ;; + --disable-python) libpython="no" + ;; + --enable-python) libpython="yes" + ;; --enable-profiler) profiler="yes" ;; --enable-cocoa)
@@ -1061,6 +1066,8 @@ echo " --enable-spice enable spice" echo " --enable-rbd enable building the rados block device (rbd)" echo " --disable-libiscsi disable iscsi support" echo " --enable-libiscsi enable iscsi support" +echo " --disable-python disable Python support" +echo " --enable-python enable Python support" echo " --disable-smartcard disable smartcard support" echo " --enable-smartcard enable smartcard support" echo " --disable-smartcard-nss disable smartcard nss support"
@@ -2418,6 +2425,26 @@ fi ########################################## +# Python support probe +if test "$libpython" != "no"; then + if $pkg_config --modversion python > /dev/null 2>&1 ; then + libpython="yes" + python_cflags=`$pkg_config --cflags python 2>/dev/null` + python_libs=`$pkg_config --libs python 2>/dev/null` + pygtk_cflags=`$pkg_config --cflags pygobject-2.0 2>/dev/null` + pygtk_libs=`$pkg_config --libs pygobject-2.0 2>/dev/null` + pygtk_libs="$pygtk_libs -lpyglib-2.0-python2.7" + QEMU_CFLAGS="$python_cflags $pygtk_cflags $QEMU_CFLAGS" + LIBS="$python_libs $pygtk_libs $LIBS" + else + if test "$libpython" = "yes"; then + feature_not_found "python" + fi + libpython="no" + fi +fi + +########################################## # Do we need librt cat > $TMPC <<EOF #include <signal.h>
@@ -2829,6 +2856,7 @@ echo "nss used $smartcard_nss" echo "usb net redir $usb_redir" echo "OpenGL support $opengl" echo "libiscsi support $libiscsi" +echo "Python support $libpython" echo "build guest agent $guest_agent" if test "$sdl_too_old" = "yes"; then
@@ -3146,6 +3174,10 @@ if test "$libiscsi" = "yes" ; then echo "CONFIG_LIBISCSI=y" >> $config_host_mak fi +if test "$libpython" = "yes" ; then + echo "CONFIG_PYTHON=y" >> $config_host_mak +fi + # XXX: suppress that if [ "$bsd" = "yes" ] ; then echo "CONFIG_BSD=y" >> $config_host_mak
hw/hw.h +11 -0
@@ -943,4 +943,15 @@ int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, int required_for_version);void vmstate_unregister (DeviceState *dev,const VMStateDescription *vmsd,void *opaque); + +typedef struct VMState +{ +const char *name; +int instance_id; +VMStateDescription *vmsd; +void *object; +}VMState ; + +GSList *vmstate_get_all (void ); +#endif
pyembed.c new file +244 -0
244 lines not on this page · raw message
pyembed.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef QEMU_PYEMBED_H +#define QEMU_PYEMBED_H + +void python_init (void ); +void python_load (const char *filename); +void python_cleanup (void ); + +#endif
savevm.c +17 -0
@@ -1468,6 +1468,23 @@ static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)return vmstate_load_state (f, se->vmsd, se->opaque, version_id); } +GSList *vmstate_get_all (void ) +{ +SaveStateEntry *se; +GSList *lst =NULL ; + + QTAILQ_FOREACH(se, &savevm_handlers, entry) { +VMState *vms =g_malloc (sizeof (*vms)); + vms->name = se->idstr; + vms->instance_id = se->instance_id; + vms->vmsd = se->vmsd; + vms->object = se->opaque; + lst =g_slist_append (lst, vms); + } + +return lst; +} +static void vmstate_save (QEMUFile *f,SaveStateEntry *se) {if (!se->vmsd) {/* Old style */
vl.c +7 -0
@@ -166,6 +166,7 @@ int main(int argc, char **argv)#include "arch_init.h" #include "ui/qemu-spice.h" +#include "pyembed.h" //#define DEBUG_NET //#define DEBUG_SLIRP
@@ -2187,6 +2188,8 @@ int main(int argc, char **argv, char **envp)atexit (qemu_run_exit_notifiers);error_set_progname (argv[0 ]); +python_init (); +g_mem_set_vtable (&mem_trace);if (!g_thread_supported ()) {g_thread_init (NULL );
@@ -3347,6 +3350,8 @@ int main(int argc, char **argv, char **envp) }qemu_add_globals (); +python_load ("myext" ); + machine->init (ram_size, boot_devices, kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
@@ -3487,5 +3492,7 @@ int main(int argc, char **argv, char **envp)net_cleanup ();res_free (); +python_cleanup (); +return 0 ; }
Signature · 1 line
1.7.4.1
Quoted 4 lines · Anthony Liguori
This is something I started during 1.0-rc on a lark and spent some time last night actually making work. I'm sending it only to show that (1) it's possible and (2) to get some input about what other people think as a longer term direction.
At the risk of starting a language flame-war, have you considered embedding something like Lua?The source of the core Lua interpreter is small enough that it could be imported into the Qemu repository. There may also be advantages to having access to LuaJIT (on supported platforms) for its speed and FFI library [1] which could make it feasible to prototype new device models in a high level language with good performance.
Alex
Quoted 9 lines · Alex Bradbury
Quoted 4 lines · Anthony Liguori
This is something I started during 1.0-rc on a lark and spent some time last night actually making work. I'm sending it only to show that (1) it's possible and (2) to get some input about what other people think as a longer term direction.
At the risk of starting a language flame-war, have you considered embedding something like Lua?The source of the core Lua interpreter is small enough that it could be imported into the Qemu repository.
Generally, I don't think importing external source code is a Good Idea. Part of the appeal to me about Python is the rich library that it brings in.
I'm less interested in Python for it's list comprehension syntax and more interested in it for it's config parsing library, RPC infrastructure, etc.
Plus, I'm reasonably confident that most QEMU developers have some experience with Python. I'd wager that very few people have any practical experience with LUA.
So in terms of embedding, I think Python is the only reasonable path forward (if we think we should even head in this direction).
Quoted 5 lines
There
may also be advantages to having access to LuaJIT (on supported platforms) for its speed and FFI library [1] which could make it feasible to prototype new device models in a high level language with good performance.
Python has a simple FFI module (ctypes).
Regards,
Anthony Liguori
Quoted 4 lines
Alex
Am 16.01.2012 20:23, schrieb Anthony Liguori:
Quoted 11 lines · Alex Bradbury
Quoted 9 lines · Anthony Liguori
Quoted 4 lines
This is something I started during 1.0-rc on a lark and spent some time last night actually making work. I'm sending it only to show that (1) it's possible and (2) to get some input about what other people think as a longer term direction.
At the risk of starting a language flame-war, have you considered embedding something like Lua?The source of the core Lua interpreter is small enough that it could be imported into the Qemu repository.
Quoted 8 lines
Generally, I don't think importing external source code is a Good Idea. Part of the appeal to me about Python is the rich library that it brings in.
I'm less interested in Python for it's list comprehension syntax and more interested in it for it's config parsing library, RPC infrastructure, etc.
Plus, I'm reasonably confident that most QEMU developers have some experience with Python. I'd wager that very few people have any practical experience with LUA.
So in terms of embedding, I think Python is the only reasonable path forward (if we think we should even head in this direction).
5 years ago, I experimented with an embedded Ruby interpreter. Although it worked (at least to some degree), that's not a reasonable path. Standalone Ruby is a good interpreter language, but embedding a Ruby interpreter is horrible.
Embedding TCL works well, but TCL is a little old fashioned, so that's also not a reasonable path.
If we need an embedded interpreter, Python is a good choice, but Lua is also an excellent embedded interpreter which would be useful for QEMU.
I'd prefer a competition with both of them instead of a single "only reasonable path". The overhead of supporting two interpreters won't be very high, and it will allow more people to contribute scripts in their favorite language.
Cheers,
Stefan Weil