Skip to content

Commit

Permalink
Add the libvaccel-python.so helper lib
Browse files Browse the repository at this point in the history
libpython.so loads all dependent libraries lazily. This does
not work for us, so by using this dummy wrapper, we force the
loader to resolve all lib dependencies at load time.

eg, see: python/cpython#65735 (comment)

Signed-off-by: Anastassios Nanos <[email protected]>
  • Loading branch information
ananos committed Nov 1, 2022
1 parent f9dacaa commit 90dbd50
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include_directories(

add_subdirectory(src)
add_subdirectory(plugins)
add_subdirectory(python)

if (BUILD_EXAMPLES)
add_subdirectory(examples)
Expand Down
23 changes: 23 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
file(GLOB_RECURSE sources *.c)

# core runtime headers
file(GLOB_RECURSE headers *.h)

add_library(vaccel-python SHARED ${headers} ${sources})
target_compile_options(vaccel-python PUBLIC -Wall -Wextra -Werror -pthread)
target_include_directories(vaccel-python PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(vaccel-python PRIVATE ${CMAKE_SOURCE_DIR}/src/include)
set_property(TARGET vaccel-python PROPERTY LINK_FLAGS "-pthread")
set_property(TARGET vaccel-python PROPERTY C_STANDARD 11)
target_link_libraries(vaccel-python)

# Setup make install
install(TARGETS vaccel-python DESTINATION ${CMAKE_INSTALL_LIBDIR})
#install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

# Create the pkg-config file
set(DEST_DIR "${CMAKE_INSTALL_PREFIX}")
CONFIGURE_FILE("vaccel-python.pc.in" "vaccel-python.pc" @ONLY)

# Install the vaccel.pc file
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/vaccel-python.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR})
52 changes: 52 additions & 0 deletions python/helper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <vaccel.h>
#include <plugin.h>
#include <assert.h>

__attribute__((constructor))
void load_vaccel(void)
{
printf("Loading libvaccel\n");
void *dl = dlopen("libvaccel.so", RTLD_LAZY | RTLD_GLOBAL);
if (!dl) {
fprintf(stderr, "Could not open libvaccel\n");
exit(1);
}

char *pname = getenv("PYTHON_VACCEL_PLUGIN");
printf("Loading plugin %s\n", pname);
void *plugin = dlopen(pname, RTLD_NOW);
if (!plugin) {
fprintf(stderr, "Failed to load plugin: %s\n", dlerror());
exit(1);
}

struct vaccel_plugin **p;

int (*register_plugin)(struct vaccel_plugin *) =
dlsym(dl, "register_plugin");
assert(register_plugin);

p = dlsym(plugin, "vaccel_plugin");
if (p == NULL) {
fprintf(stderr, "Cannot find vaccel_plugin symbol\n");
return;
}
assert(p);

int ret = register_plugin(*p);
assert(!ret);
if (ret) {
fprintf(stderr, "Failed to register plugin\n");
return;
}

ret = (*p)->info->init();
assert(!ret);
if (ret) {
fprintf(stderr, "Failed to initialize plugin\n");
return;
}
}
11 changes: 11 additions & 0 deletions python/vaccel-python.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@DEST_DIR@
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include

Name: libvaccel-python
Description: Transparent acceleration for edge and cloud computing hack
Version: 0.4.0

Libs: -L${libdir} -lvaccel-python
Cflags: -I${includedir}

0 comments on commit 90dbd50

Please sign in to comment.