Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

CI: Generate Docker image with Alpine Linux #321

Merged
merged 1 commit into from
Jan 6, 2024

Conversation

henrybear327
Copy link
Collaborator

Close #286

@henrybear327 henrybear327 marked this pull request as draft January 5, 2024 02:03
@henrybear327 henrybear327 self-assigned this Jan 5, 2024
@henrybear327 henrybear327 requested a review from jserv January 5, 2024 02:03
@henrybear327
Copy link
Collaborator Author

With this current change, I observed that the image size reduced from 96 MB to 12.4 MB.

However, the rv32emu will return a segmentation fault by executing any of the elf files provided in the build directory. The root cause to be investigated.

@henrybear327 henrybear327 force-pushed the ci/reduce_docker_image_size branch 2 times, most recently from 99d57be to 7f801aa Compare January 5, 2024 02:31
@henrybear327 henrybear327 marked this pull request as ready for review January 5, 2024 02:33
@henrybear327
Copy link
Collaborator Author

henrybear327 commented Jan 5, 2024

With this current change, I observed that the image size reduced from 96 MB to 12.4 MB.

However, the rv32emu will return a segmentation fault by executing any of the elf files provided in the build directory. The root cause to be investigated.

The current PR works on node11, both building the image and executing rv32emu.

However, it doesn't work on my Intel MacBook. I currently don't have a M1 laptop at hand, can @jserv help me quickly test it out?

The testing can be performed as shown:

  • build the docker image: docker build --progress=plain -t rv32emu-slim .
  • spin up a container: docker run -it rv32emu-slim
  • execute a elf file: rv32emu ./build/hello.elf

@jserv
Copy link
Contributor

jserv commented Jan 5, 2024

The testing can be performed as shown:

  • build the docker image: docker build --progress=plain -t rv32emu-slim .
  • spin up a container: docker run -it rv32emu-slim
  • execute a elf file: rv32emu ./build/hello.elf

Due to Docker being cumbersome and resource-intensive on macOS with Apple Silicon, I prefer using nerdctl, which offers a command line interface that is compatible with Docker.

Along with lima, I run the command nerdctl.lima build --progress=plain -t rv32emu-slim . and the following message appears:

#11 [base_gcc 6/6] RUN make tool
#11 0.144   CC	build/rv_histogram.o
#11 0.232   LD	build/rv_histogram
#11 0.240 build/riscv.o: file not recognized: file format not recognized
#11 0.240 collect2: error: ld returned 1 exit status
#11 0.241 make: *** [mk/tools.mk:24: build/rv_histogram] Error 1
#11 ERROR: process "/bin/sh -c make tool" did not complete successfully: exit code: 2

@henrybear327
Copy link
Collaborator Author

With this current change, I observed that the image size reduced from 96 MB to 12.4 MB.
However, the rv32emu will return a segmentation fault by executing any of the elf files provided in the build directory. The root cause to be investigated.

The current PR works on node11, both building the image and executing rv32emu.

However, it doesn't work on my Intel MacBook. I currently don't have a M1 laptop at hand, can @jserv help me quickly test it out?

The testing can be performed as shown:

  • build the docker image: docker build --progress=plain -t rv32emu-slim .
  • spin up a container: docker run -it rv32emu-slim
  • execute a elf file: rv32emu ./build/hello.elf

After running strace, we got

/home/root/rv32emu # strace rv32emu ./build/hello.elf 
execve("/home/root/rv32emu/build/rv32emu", ["rv32emu", "./build/hello.elf"], 0x7ffd9792b7b8 /* 5 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x7f8c307a1b08) = 0
set_tid_address(0x7f8c307a1f70)         = 20
brk(NULL)                               = 0x5604471ca000
brk(0x5604471cc000)                     = 0x5604471cc000
mmap(0x5604471ca000, 4096, PROT_NONE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x5604471ca000
mprotect(0x7f8c3079e000, 4096, PROT_READ) = 0
mprotect(0x560445d43000, 4096, PROT_READ) = 0
open("build/hello.elf", O_RDONLY|O_LARGEFILE) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=4812, ...}) = 0
mmap(NULL, 4812, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f8c306fe000
close(3)                                = 0
mmap(NULL, 4294967295, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Out of memory)
mmap(NULL, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8c306f2000
mmap(NULL, 73728, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8c306e0000
mmap(NULL, 28672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f8c306d9000
--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_MAPERR, si_addr=NULL} ---
+++ killed by SIGSEGV (core dumped) +++
Segmentation fault (core dumped)

As my docker/lima was configured to allocate only 4GiB of memory in the container, mmap failed with OOM. After increasing the memory allocation to 8GiB, the code runs smoothly.

After checking the source code, maybe we need to mention this in README as a hardware requirement?

/*
 * set memory size to 2^32 - 1 bytes
 *
 * The memory size is set to 2^32 - 1 bytes in order to make this emulator
 * portable for both 32-bit and 64-bit platforms. As a result, it can access
 * any segment of the memory on either platform. Furthermore, it is safe
 * because most of the test cases' data memory usage will not exceed this
 * memory size.
 */
#define MEM_SIZE 0xFFFFFFFFULL

@henrybear327
Copy link
Collaborator Author

The testing can be performed as shown:

  • build the docker image: docker build --progress=plain -t rv32emu-slim .
  • spin up a container: docker run -it rv32emu-slim
  • execute a elf file: rv32emu ./build/hello.elf

Due to Docker being cumbersome and resource-intensive on macOS with Apple Silicon, I prefer using nerdctl, which offers a command line interface that is compatible with Docker.

Along with lima, I run the command nerdctl.lima build --progress=plain -t rv32emu-slim . and the following message appears:

#11 [base_gcc 6/6] RUN make tool
#11 0.144   CC	build/rv_histogram.o
#11 0.232   LD	build/rv_histogram
#11 0.240 build/riscv.o: file not recognized: file format not recognized
#11 0.240 collect2: error: ld returned 1 exit status
#11 0.241 make: *** [mk/tools.mk:24: build/rv_histogram] Error 1
#11 ERROR: process "/bin/sh -c make tool" did not complete successfully: exit code: 2

Maybe some object files are copied from the folder directly during the build. Can you try to run make clean before building the container?

Thank you!

@henrybear327 henrybear327 force-pushed the ci/reduce_docker_image_size branch from 7f801aa to 49778e4 Compare January 5, 2024 21:42
@jserv
Copy link
Contributor

jserv commented Jan 6, 2024

Maybe some object files are copied from the folder directly during the build. Can you try to run make clean before building the container?

Additional make distclean is required. It proceeds now.

@jserv jserv merged commit e2eb61a into sysprog21:master Jan 6, 2024
6 checks passed
vestata pushed a commit to vestata/rv32emu that referenced this pull request Jan 24, 2025
…mage_size

CI: Generate Docker image with Alpine Linux
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CI: Generate Docker image with Alpine Linux
2 participants