Skip to content

Commit b60d6cf

Browse files
committed
An example of unikraft application which uses vAccel
This application can be built with Unikraft and classify an image using vAccel over QEMU/KVM. Signed-off-by: Charalampos Mainas <charalampos.mainas@gmail.com>
0 parents  commit b60d6cf

File tree

6 files changed

+359
-0
lines changed

6 files changed

+359
-0
lines changed

Config.uk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Invisible option for dependencies
2+
config APPTEST_DEPENDENCIES
3+
bool
4+
default y
5+
select LIBNOLIBC if !HAVE_LIBC

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
UK_ROOT ?= $(PWD)/../../unikraft
2+
UK_LIBS ?= $(PWD)/../../libs
3+
LIBS :=
4+
5+
all:
6+
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS)
7+
8+
$(MAKECMDGOALS):
9+
@$(MAKE) -C $(UK_ROOT) A=$(PWD) L=$(LIBS) $(MAKECMDGOALS)
10+

Makefile.uk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$(eval $(call addlib,apptest))
2+
3+
APPTEST_SRCS-y += $(APPTEST_BASE)/classification.c

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
###Image classification example
2+
3+
A small example to test vAccel in unikraft.
4+
5+
The vaccel\_config file contains a config for KVM platform which enables the following libraries:
6+
- vAccelrt (the virtio accel driver gets enabled too)
7+
- 9pfs, so we can pass the image to the unikernel
8+
- vfscore with 9pfs as root filesystem
9+
- devfs which gets mounted during boot , for /dev/accel
10+
11+
Afer building unikraft you can run it using the below command.
12+
13+
```
14+
LD_LIBRARY_PATH=$PATH_TO_VACCELRT:/usr/local/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64 qemu-system-x86_64 \
15+
-cpu host -m 512 -enable-kvm -nographic -vga none \
16+
-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 \
17+
-object acceldev-backend-vaccelrt,id=gen0 -device virtio-accel-pci,id=accl0,runtime=gen0,disable-legacy=off,disable-modern=on \
18+
-kernel $PATH_TO_UNIKRAFT_IMAGE -append "vfs.rootdev=data -- $IMAGE $ITERATIONS"
19+
```
20+
21+
where:
22+
- PATH\_TO\_VACCELRT is the install directory of vAccelrt
23+
- PATH\_TO\_DIRECTORY\_CONTAINING\_IMAGE is the directory which contains $IMAGE
24+
- PATH\_TO\_UNIKRAFT\_IMAGE, under build of the directory we built unikraft
25+
- $IMAGE the image we want to classify
26+
- $ITERATIONS, how many iterations we want to do
27+

classification.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <sys/types.h>
4+
#include <sys/stat.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
8+
#include <vaccel.h>
9+
#include <vaccel_ops.h>
10+
11+
int read_file(const char *filename, char **img, size_t *img_size)
12+
{
13+
int fd;
14+
15+
fd = open(filename, O_RDONLY);
16+
if (fd < 0) {
17+
//perror("open: ");
18+
fprintf(stderr, "open %s returned %d", filename, fd);
19+
return 1;
20+
}
21+
22+
struct stat info;
23+
fstat(fd, &info);
24+
fprintf(stdout, "Image size: %luB\n", info.st_size);
25+
26+
char *buf = malloc(info.st_size);
27+
if (!buf) {
28+
fprintf(stderr, "Could not allocate memory for image\n");
29+
goto close_file;
30+
}
31+
32+
long bytes = 0;
33+
do {
34+
int ret = read(fd, buf, info.st_size);
35+
if (ret < 0) {
36+
//perror("Error while reading image: ");
37+
fprintf(stderr, "read returned %d", ret);
38+
goto free_buff;
39+
}
40+
bytes += ret;
41+
} while (bytes < info.st_size);
42+
43+
if (bytes < info.st_size) {
44+
fprintf(stderr, "Could not read image\n");
45+
goto free_buff;
46+
}
47+
48+
*img = buf;
49+
*img_size = info.st_size;
50+
close(fd);
51+
52+
return 0;
53+
54+
free_buff:
55+
free(buf);
56+
close_file:
57+
close(fd);
58+
return 1;
59+
}
60+
61+
int main(int argc, char *argv[])
62+
{
63+
int ret;
64+
char *image;
65+
size_t image_size;
66+
char out_text[512], out_imagename[512];
67+
struct vaccel_session sess;
68+
69+
if (argc != 3) {
70+
fprintf(stderr, "Usage: %s filename #iterations\n", argv[0]);
71+
return 0;
72+
}
73+
74+
ret = vaccel_sess_init(&sess, 0);
75+
if (ret != VACCEL_OK) {
76+
fprintf(stderr, "Could not initialize session\n");
77+
return 1;
78+
}
79+
80+
printf("Initialized session with id: %u\n", sess.session_id);
81+
82+
ret = read_file(argv[1], &image, &image_size);
83+
if (ret)
84+
goto close_session;
85+
86+
for (int i = 0; i < atoi(argv[2]); ++i) {
87+
ret = vaccel_image_classification(&sess, image, (unsigned char*)out_text, (unsigned char*)out_imagename,
88+
image_size, sizeof(out_text), sizeof(out_imagename));
89+
if (ret) {
90+
fprintf(stderr, "Could not run op: %d\n", ret);
91+
goto close_session;
92+
}
93+
94+
if (i == 0)
95+
printf("classification tags: %s\n", out_text);
96+
}
97+
98+
99+
close_session:
100+
free(image);
101+
if (vaccel_sess_free(&sess) != VACCEL_OK) {
102+
fprintf(stderr, "Could not clear session\n");
103+
return 1;
104+
}
105+
106+
return ret;
107+
}

vaccel_config

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#
2+
# Automatically generated file; DO NOT EDIT.
3+
# Unikraft/0.4.0~b97e3bc-custom Configuration
4+
#
5+
CONFIG_UK_FULLVERSION="0.4.0~b97e3bc-custom"
6+
CONFIG_UK_CODENAME="Rhea"
7+
CONFIG_UK_ARCH="x86_64"
8+
CONFIG_UK_BASE="/store/cmainas_ws/vaccel/unikraft/final/unikraft"
9+
CONFIG_UK_APP="/store/cmainas_ws/vaccel/unikraft/final/apps/unikraft_app_classify"
10+
CONFIG_UK_DEFNAME="unikraft_app_classify"
11+
12+
#
13+
# Architecture Selection
14+
#
15+
CONFIG_ARCH_X86_64=y
16+
# CONFIG_ARCH_ARM_64 is not set
17+
# CONFIG_ARCH_ARM_32 is not set
18+
# CONFIG_MARCH_X86_64_NATIVE is not set
19+
CONFIG_MARCH_X86_64_GENERIC=y
20+
# CONFIG_MARCH_X86_64_NOCONA is not set
21+
# CONFIG_MARCH_X86_64_CORE2 is not set
22+
# CONFIG_MARCH_X86_64_COREI7 is not set
23+
# CONFIG_MARCH_X86_64_COREI7AVX is not set
24+
# CONFIG_MARCH_X86_64_COREI7AVXI is not set
25+
# CONFIG_MARCH_X86_64_ATOM is not set
26+
# CONFIG_MARCH_X86_64_K8 is not set
27+
# CONFIG_MARCH_X86_64_K8SSE3 is not set
28+
# CONFIG_MARCH_X86_64_AMDFAM10 is not set
29+
# CONFIG_MARCH_X86_64_BTVER1 is not set
30+
# CONFIG_MARCH_X86_64_BDVER1 is not set
31+
# CONFIG_MARCH_X86_64_BDVER2 is not set
32+
# CONFIG_MARCH_X86_64_BDVER3 is not set
33+
# CONFIG_MARCH_X86_64_BTVER2 is not set
34+
CONFIG_STACK_SIZE_PAGE_ORDER=4
35+
# end of Architecture Selection
36+
37+
#
38+
# Platform Configuration
39+
#
40+
CONFIG_PLAT_KVM=y
41+
42+
#
43+
# Console Options
44+
#
45+
CONFIG_KVM_KERNEL_SERIAL_CONSOLE=y
46+
CONFIG_KVM_KERNEL_VGA_CONSOLE=y
47+
CONFIG_KVM_DEBUG_SERIAL_CONSOLE=y
48+
CONFIG_KVM_DEBUG_VGA_CONSOLE=y
49+
50+
#
51+
# Serial console configuration
52+
#
53+
CONFIG_KVM_SERIAL_BAUD_115200=y
54+
# CONFIG_KVM_SERIAL_BAUD_57600 is not set
55+
# CONFIG_KVM_SERIAL_BAUD_38400 is not set
56+
# CONFIG_KVM_SERIAL_BAUD_19200 is not set
57+
# end of Serial console configuration
58+
# end of Console Options
59+
60+
CONFIG_KVM_PCI=y
61+
CONFIG_VIRTIO_BUS=y
62+
63+
#
64+
# Virtio
65+
#
66+
CONFIG_VIRTIO_PCI=y
67+
CONFIG_VIRTIO_ACCEL=y
68+
CONFIG_VIRTIO_9P=y
69+
# end of Virtio
70+
71+
# CONFIG_PLAT_LINUXU is not set
72+
# CONFIG_PLAT_XEN is not set
73+
74+
#
75+
# Platform Interface Options
76+
#
77+
# CONFIG_UKPLAT_MEMRNAME is not set
78+
# end of Platform Interface Options
79+
80+
CONFIG_HZ=100
81+
# end of Platform Configuration
82+
83+
#
84+
# Library Configuration
85+
#
86+
CONFIG_LIB9PFS=y
87+
CONFIG_LIBDEVFS=y
88+
CONFIG_LIBDEVFS_AUTOMOUNT=y
89+
# CONFIG_LIBDEVFS_DEV_NULL is not set
90+
# CONFIG_LIBDEVFS_DEV_ZERO is not set
91+
# CONFIG_LIBFDT is not set
92+
CONFIG_LIBVACCELRT=y
93+
CONFIG_LIBNOLIBC=y
94+
CONFIG_LIBNOLIBC_UKDEBUG_ASSERT=y
95+
# CONFIG_LIBPOSIX_LIBDL is not set
96+
# CONFIG_LIBPOSIX_PROCESS is not set
97+
# CONFIG_LIBPOSIX_SYSINFO is not set
98+
# CONFIG_LIBPOSIX_USER is not set
99+
# CONFIG_LIBRAMFS is not set
100+
# CONFIG_LIBSYSCALL_SHIM is not set
101+
CONFIG_LIBUK9P=y
102+
CONFIG_LIBUKALLOC=y
103+
# CONFIG_LIBUKALLOC_IFMALLOC is not set
104+
# CONFIG_LIBUKALLOC_IFSTATS is not set
105+
CONFIG_LIBUKALLOCBBUDDY=y
106+
# CONFIG_LIBUKALLOCREGION is not set
107+
CONFIG_LIBUKARGPARSE=y
108+
# CONFIG_LIBUKBLKDEV is not set
109+
CONFIG_LIBUKBOOT=y
110+
# CONFIG_LIBUKBOOT_BANNER_NONE is not set
111+
# CONFIG_LIBUKBOOT_BANNER_MINIMAL is not set
112+
# CONFIG_LIBUKBOOT_BANNER_CLASSIC is not set
113+
CONFIG_LIBUKBOOT_BANNER_POWEREDBY=y
114+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_ANSI is not set
115+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_ANSI2 is not set
116+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_EA is not set
117+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_EAANSI is not set
118+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_EAANSI2 is not set
119+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_U8 is not set
120+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_U8ANSI is not set
121+
# CONFIG_LIBUKBOOT_BANNER_POWEREDBY_U8ANSI2 is not set
122+
CONFIG_LIBUKBOOT_MAXNBARGS=60
123+
CONFIG_LIBUKBOOT_INITBBUDDY=y
124+
# CONFIG_LIBUKBOOT_INITREGION is not set
125+
# CONFIG_LIBUKBOOT_NOALLOC is not set
126+
CONFIG_LIBUKBUS=y
127+
CONFIG_LIBUKDEBUG=y
128+
CONFIG_LIBUKDEBUG_PRINTK=y
129+
# CONFIG_LIBUKDEBUG_PRINTK_INFO is not set
130+
# CONFIG_LIBUKDEBUG_PRINTK_WARN is not set
131+
CONFIG_LIBUKDEBUG_PRINTK_ERR=y
132+
# CONFIG_LIBUKDEBUG_PRINTK_CRIT is not set
133+
# CONFIG_LIBUKDEBUG_PRINTD is not set
134+
# CONFIG_LIBUKDEBUG_NOREDIR is not set
135+
CONFIG_LIBUKDEBUG_REDIR_PRINTD=y
136+
# CONFIG_LIBUKDEBUG_REDIR_PRINTK is not set
137+
CONFIG_LIBUKDEBUG_PRINT_TIME=y
138+
# CONFIG_LIBUKDEBUG_PRINT_STACK is not set
139+
CONFIG_LIBUKDEBUG_PRINT_SRCNAME=y
140+
# CONFIG_LIBUKDEBUG_ANSI_COLOR is not set
141+
CONFIG_LIBUKDEBUG_ENABLE_ASSERT=y
142+
# CONFIG_LIBUKDEBUG_TRACEPOINTS is not set
143+
CONFIG_LIBUKLIBPARAM=y
144+
CONFIG_LIBUKLOCK=y
145+
CONFIG_LIBUKLOCK_SEMAPHORE=y
146+
CONFIG_LIBUKLOCK_MUTEX=y
147+
# CONFIG_LIBUKMMAP is not set
148+
# CONFIG_LIBUKMPI is not set
149+
# CONFIG_LIBUKNETDEV is not set
150+
# CONFIG_LIBUKRING is not set
151+
CONFIG_LIBUKSCHED=y
152+
CONFIG_LIBUKSCHEDCOOP=y
153+
CONFIG_LIBUKSGLIST=y
154+
# CONFIG_LIBUKSP is not set
155+
# CONFIG_LIBUKSWRAND is not set
156+
CONFIG_LIBUKTIME=y
157+
CONFIG_LIBUKTIMECONV=y
158+
CONFIG_LIBVFSCORE=y
159+
160+
#
161+
# vfscore: Configuration
162+
#
163+
CONFIG_LIBVFSCORE_PIPE_SIZE_ORDER=16
164+
CONFIG_LIBVFSCORE_AUTOMOUNT_ROOTFS=y
165+
# CONFIG_LIBVFSCORE_ROOTFS_RAMFS is not set
166+
CONFIG_LIBVFSCORE_ROOTFS_9PFS=y
167+
# CONFIG_LIBVFSCORE_ROOTFS_CUSTOM is not set
168+
CONFIG_LIBVFSCORE_ROOTFS="9pfs"
169+
CONFIG_LIBVFSCORE_ROOTDEV="rootfs"
170+
CONFIG_LIBVFSCORE_ROOTFLAGS=0x0
171+
CONFIG_LIBVFSCORE_ROOTOPTS=""
172+
# end of vfscore: Configuration
173+
174+
CONFIG_HAVE_BOOTENTRY=y
175+
CONFIG_HAVE_TIME=y
176+
CONFIG_HAVE_SCHED=y
177+
# end of Library Configuration
178+
179+
#
180+
# Build Options
181+
#
182+
# CONFIG_OPTIMIZE_NONE is not set
183+
CONFIG_OPTIMIZE_PERF=y
184+
# CONFIG_OPTIMIZE_SIZE is not set
185+
186+
#
187+
# Hint: Specify a CPU type to get most benefits from performance optimization
188+
#
189+
# CONFIG_OPTIMIZE_DEADELIM is not set
190+
# CONFIG_OPTIMIZE_LTO is not set
191+
# CONFIG_DEBUG_SYMBOLS_LVL0 is not set
192+
# CONFIG_DEBUG_SYMBOLS_LVL1 is not set
193+
# CONFIG_DEBUG_SYMBOLS_LVL2 is not set
194+
CONFIG_DEBUG_SYMBOLS_LVL3=y
195+
# CONFIG_OPTIMIZE_SYMFILE is not set
196+
CONFIG_OPTIMIZE_COMPRESS=y
197+
# CONFIG_RECORD_BUILDTIME is not set
198+
CONFIG_CROSS_COMPILE=""
199+
# end of Build Options
200+
201+
#
202+
# Application Options
203+
#
204+
CONFIG_APPTEST_DEPENDENCIES=y
205+
# end of Application Options
206+
207+
CONFIG_UK_NAME="unikraft_app_classify"

0 commit comments

Comments
 (0)