Skip to content

Commit

Permalink
An example of unikraft application which uses vAccel
Browse files Browse the repository at this point in the history
This application can be built with Unikraft and classify an image using
vAccel over QEMU/KVM.

Signed-off-by: Charalampos Mainas <[email protected]>
  • Loading branch information
cmainas committed Feb 4, 2021
0 parents commit b60d6cf
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Config.uk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Invisible option for dependencies
config APPTEST_DEPENDENCIES
bool
default y
select LIBNOLIBC if !HAVE_LIBC
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
UK_ROOT ?= $(PWD)/../../unikraft
UK_LIBS ?= $(PWD)/../../libs
LIBS :=

all:
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS)

$(MAKECMDGOALS):
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) $(MAKECMDGOALS)

3 changes: 3 additions & 0 deletions Makefile.uk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(eval $(call addlib,apptest))

APPTEST_SRCS-y += $(APPTEST_BASE)/classification.c
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
###Image classification example

A small example to test vAccel in unikraft.

The vaccel\_config file contains a config for KVM platform which enables the following libraries:
- vAccelrt (the virtio accel driver gets enabled too)
- 9pfs, so we can pass the image to the unikernel
- vfscore with 9pfs as root filesystem
- devfs which gets mounted during boot , for /dev/accel

Afer building unikraft you can run it using the below command.

```
LD_LIBRARY_PATH=$PATH_TO_VACCELRT:/usr/local/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64 qemu-system-x86_64 \
-cpu host -m 512 -enable-kvm -nographic -vga none \
-fsdev local,id=myid,path=$PATH_TO_DIRECTORY_CONTAINING_IMAGE,security_model=none -device virtio-9p-pci,fsdev=myid,mount_tag=data,disable-modern=on,disable-legacy=off \
-object acceldev-backend-vaccelrt,id=gen0 -device virtio-accel-pci,id=accl0,runtime=gen0,disable-legacy=off,disable-modern=on \
-kernel $PATH_TO_UNIKRAFT_IMAGE -append "vfs.rootdev=data -- $IMAGE $ITERATIONS"
```

where:
- PATH\_TO\_VACCELRT is the install directory of vAccelrt
- PATH\_TO\_DIRECTORY\_CONTAINING\_IMAGE is the directory which contains $IMAGE
- PATH\_TO\_UNIKRAFT\_IMAGE, under build of the directory we built unikraft
- $IMAGE the image we want to classify
- $ITERATIONS, how many iterations we want to do

107 changes: 107 additions & 0 deletions classification.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <vaccel.h>
#include <vaccel_ops.h>

int read_file(const char *filename, char **img, size_t *img_size)
{
int fd;

fd = open(filename, O_RDONLY);
if (fd < 0) {
//perror("open: ");
fprintf(stderr, "open %s returned %d", filename, fd);
return 1;
}

struct stat info;
fstat(fd, &info);
fprintf(stdout, "Image size: %luB\n", info.st_size);

char *buf = malloc(info.st_size);
if (!buf) {
fprintf(stderr, "Could not allocate memory for image\n");
goto close_file;
}

long bytes = 0;
do {
int ret = read(fd, buf, info.st_size);
if (ret < 0) {
//perror("Error while reading image: ");
fprintf(stderr, "read returned %d", ret);
goto free_buff;
}
bytes += ret;
} while (bytes < info.st_size);

if (bytes < info.st_size) {
fprintf(stderr, "Could not read image\n");
goto free_buff;
}

*img = buf;
*img_size = info.st_size;
close(fd);

return 0;

free_buff:
free(buf);
close_file:
close(fd);
return 1;
}

int main(int argc, char *argv[])
{
int ret;
char *image;
size_t image_size;
char out_text[512], out_imagename[512];
struct vaccel_session sess;

if (argc != 3) {
fprintf(stderr, "Usage: %s filename #iterations\n", argv[0]);
return 0;
}

ret = vaccel_sess_init(&sess, 0);
if (ret != VACCEL_OK) {
fprintf(stderr, "Could not initialize session\n");
return 1;
}

printf("Initialized session with id: %u\n", sess.session_id);

ret = read_file(argv[1], &image, &image_size);
if (ret)
goto close_session;

for (int i = 0; i < atoi(argv[2]); ++i) {
ret = vaccel_image_classification(&sess, image, (unsigned char*)out_text, (unsigned char*)out_imagename,
image_size, sizeof(out_text), sizeof(out_imagename));
if (ret) {
fprintf(stderr, "Could not run op: %d\n", ret);
goto close_session;
}

if (i == 0)
printf("classification tags: %s\n", out_text);
}


close_session:
free(image);
if (vaccel_sess_free(&sess) != VACCEL_OK) {
fprintf(stderr, "Could not clear session\n");
return 1;
}

return ret;
}
207 changes: 207 additions & 0 deletions vaccel_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#
# Automatically generated file; DO NOT EDIT.
# Unikraft/0.4.0~b97e3bc-custom Configuration
#
CONFIG_UK_FULLVERSION="0.4.0~b97e3bc-custom"
CONFIG_UK_CODENAME="Rhea"
CONFIG_UK_ARCH="x86_64"
CONFIG_UK_BASE="/store/cmainas_ws/vaccel/unikraft/final/unikraft"
CONFIG_UK_APP="/store/cmainas_ws/vaccel/unikraft/final/apps/unikraft_app_classify"
CONFIG_UK_DEFNAME="unikraft_app_classify"

#
# Architecture Selection
#
CONFIG_ARCH_X86_64=y
# CONFIG_ARCH_ARM_64 is not set
# CONFIG_ARCH_ARM_32 is not set
# CONFIG_MARCH_X86_64_NATIVE is not set
CONFIG_MARCH_X86_64_GENERIC=y
# CONFIG_MARCH_X86_64_NOCONA is not set
# CONFIG_MARCH_X86_64_CORE2 is not set
# CONFIG_MARCH_X86_64_COREI7 is not set
# CONFIG_MARCH_X86_64_COREI7AVX is not set
# CONFIG_MARCH_X86_64_COREI7AVXI is not set
# CONFIG_MARCH_X86_64_ATOM is not set
# CONFIG_MARCH_X86_64_K8 is not set
# CONFIG_MARCH_X86_64_K8SSE3 is not set
# CONFIG_MARCH_X86_64_AMDFAM10 is not set
# CONFIG_MARCH_X86_64_BTVER1 is not set
# CONFIG_MARCH_X86_64_BDVER1 is not set
# CONFIG_MARCH_X86_64_BDVER2 is not set
# CONFIG_MARCH_X86_64_BDVER3 is not set
# CONFIG_MARCH_X86_64_BTVER2 is not set
CONFIG_STACK_SIZE_PAGE_ORDER=4
# end of Architecture Selection

#
# Platform Configuration
#
CONFIG_PLAT_KVM=y

#
# Console Options
#
CONFIG_KVM_KERNEL_SERIAL_CONSOLE=y
CONFIG_KVM_KERNEL_VGA_CONSOLE=y
CONFIG_KVM_DEBUG_SERIAL_CONSOLE=y
CONFIG_KVM_DEBUG_VGA_CONSOLE=y

#
# Serial console configuration
#
CONFIG_KVM_SERIAL_BAUD_115200=y
# CONFIG_KVM_SERIAL_BAUD_57600 is not set
# CONFIG_KVM_SERIAL_BAUD_38400 is not set
# CONFIG_KVM_SERIAL_BAUD_19200 is not set
# end of Serial console configuration
# end of Console Options

CONFIG_KVM_PCI=y
CONFIG_VIRTIO_BUS=y

#
# Virtio
#
CONFIG_VIRTIO_PCI=y
CONFIG_VIRTIO_ACCEL=y
CONFIG_VIRTIO_9P=y
# end of Virtio

# CONFIG_PLAT_LINUXU is not set
# CONFIG_PLAT_XEN is not set

#
# Platform Interface Options
#
# CONFIG_UKPLAT_MEMRNAME is not set
# end of Platform Interface Options

CONFIG_HZ=100
# end of Platform Configuration

#
# Library Configuration
#
CONFIG_LIB9PFS=y
CONFIG_LIBDEVFS=y
CONFIG_LIBDEVFS_AUTOMOUNT=y
# CONFIG_LIBDEVFS_DEV_NULL is not set
# CONFIG_LIBDEVFS_DEV_ZERO is not set
# CONFIG_LIBFDT is not set
CONFIG_LIBVACCELRT=y
CONFIG_LIBNOLIBC=y
CONFIG_LIBNOLIBC_UKDEBUG_ASSERT=y
# CONFIG_LIBPOSIX_LIBDL is not set
# CONFIG_LIBPOSIX_PROCESS is not set
# CONFIG_LIBPOSIX_SYSINFO is not set
# CONFIG_LIBPOSIX_USER is not set
# CONFIG_LIBRAMFS is not set
# CONFIG_LIBSYSCALL_SHIM is not set
CONFIG_LIBUK9P=y
CONFIG_LIBUKALLOC=y
# CONFIG_LIBUKALLOC_IFMALLOC is not set
# CONFIG_LIBUKALLOC_IFSTATS is not set
CONFIG_LIBUKALLOCBBUDDY=y
# CONFIG_LIBUKALLOCREGION is not set
CONFIG_LIBUKARGPARSE=y
# CONFIG_LIBUKBLKDEV is not set
CONFIG_LIBUKBOOT=y
# CONFIG_LIBUKBOOT_BANNER_NONE is not set
# CONFIG_LIBUKBOOT_BANNER_MINIMAL is not set
# CONFIG_LIBUKBOOT_BANNER_CLASSIC is not set
CONFIG_LIBUKBOOT_BANNER_POWEREDBY=y
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_ANSI is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_ANSI2 is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_EA is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_EAANSI is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_EAANSI2 is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_U8 is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_U8ANSI is not set
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_U8ANSI2 is not set
CONFIG_LIBUKBOOT_MAXNBARGS=60
CONFIG_LIBUKBOOT_INITBBUDDY=y
# CONFIG_LIBUKBOOT_INITREGION is not set
# CONFIG_LIBUKBOOT_NOALLOC is not set
CONFIG_LIBUKBUS=y
CONFIG_LIBUKDEBUG=y
CONFIG_LIBUKDEBUG_PRINTK=y
# CONFIG_LIBUKDEBUG_PRINTK_INFO is not set
# CONFIG_LIBUKDEBUG_PRINTK_WARN is not set
CONFIG_LIBUKDEBUG_PRINTK_ERR=y
# CONFIG_LIBUKDEBUG_PRINTK_CRIT is not set
# CONFIG_LIBUKDEBUG_PRINTD is not set
# CONFIG_LIBUKDEBUG_NOREDIR is not set
CONFIG_LIBUKDEBUG_REDIR_PRINTD=y
# CONFIG_LIBUKDEBUG_REDIR_PRINTK is not set
CONFIG_LIBUKDEBUG_PRINT_TIME=y
# CONFIG_LIBUKDEBUG_PRINT_STACK is not set
CONFIG_LIBUKDEBUG_PRINT_SRCNAME=y
# CONFIG_LIBUKDEBUG_ANSI_COLOR is not set
CONFIG_LIBUKDEBUG_ENABLE_ASSERT=y
# CONFIG_LIBUKDEBUG_TRACEPOINTS is not set
CONFIG_LIBUKLIBPARAM=y
CONFIG_LIBUKLOCK=y
CONFIG_LIBUKLOCK_SEMAPHORE=y
CONFIG_LIBUKLOCK_MUTEX=y
# CONFIG_LIBUKMMAP is not set
# CONFIG_LIBUKMPI is not set
# CONFIG_LIBUKNETDEV is not set
# CONFIG_LIBUKRING is not set
CONFIG_LIBUKSCHED=y
CONFIG_LIBUKSCHEDCOOP=y
CONFIG_LIBUKSGLIST=y
# CONFIG_LIBUKSP is not set
# CONFIG_LIBUKSWRAND is not set
CONFIG_LIBUKTIME=y
CONFIG_LIBUKTIMECONV=y
CONFIG_LIBVFSCORE=y

#
# vfscore: Configuration
#
CONFIG_LIBVFSCORE_PIPE_SIZE_ORDER=16
CONFIG_LIBVFSCORE_AUTOMOUNT_ROOTFS=y
# CONFIG_LIBVFSCORE_ROOTFS_RAMFS is not set
CONFIG_LIBVFSCORE_ROOTFS_9PFS=y
# CONFIG_LIBVFSCORE_ROOTFS_CUSTOM is not set
CONFIG_LIBVFSCORE_ROOTFS="9pfs"
CONFIG_LIBVFSCORE_ROOTDEV="rootfs"
CONFIG_LIBVFSCORE_ROOTFLAGS=0x0
CONFIG_LIBVFSCORE_ROOTOPTS=""
# end of vfscore: Configuration

CONFIG_HAVE_BOOTENTRY=y
CONFIG_HAVE_TIME=y
CONFIG_HAVE_SCHED=y
# end of Library Configuration

#
# Build Options
#
# CONFIG_OPTIMIZE_NONE is not set
CONFIG_OPTIMIZE_PERF=y
# CONFIG_OPTIMIZE_SIZE is not set

#
# Hint: Specify a CPU type to get most benefits from performance optimization
#
# CONFIG_OPTIMIZE_DEADELIM is not set
# CONFIG_OPTIMIZE_LTO is not set
# CONFIG_DEBUG_SYMBOLS_LVL0 is not set
# CONFIG_DEBUG_SYMBOLS_LVL1 is not set
# CONFIG_DEBUG_SYMBOLS_LVL2 is not set
CONFIG_DEBUG_SYMBOLS_LVL3=y
# CONFIG_OPTIMIZE_SYMFILE is not set
CONFIG_OPTIMIZE_COMPRESS=y
# CONFIG_RECORD_BUILDTIME is not set
CONFIG_CROSS_COMPILE=""
# end of Build Options

#
# Application Options
#
CONFIG_APPTEST_DEPENDENCIES=y
# end of Application Options

CONFIG_UK_NAME="unikraft_app_classify"

0 comments on commit b60d6cf

Please sign in to comment.