Skip to content

Commit

Permalink
[scrape.yml] New OCaml Planet blog posts and videos (#2950)
Browse files Browse the repository at this point in the history
Co-authored-by: cuihtlauac <[email protected]>
  • Loading branch information
github-actions[bot] and cuihtlauac authored Feb 7, 2025
1 parent 297c90d commit ecd9974
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions data/planet/tarides/mirageos-on-ocaml-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: MirageOS on OCaml 5!
description: Discover the ongoing work to make MirageOS compatible with OCaml 5 including
the Solo5, Unikraft, and improved cross-compilation efforts.
url: https://tarides.com/blog/2025-02-06-mirageos-on-ocaml-5
date: 2025-02-06T00:00:00-00:00
preview_image: https://tarides.com/blog/images/junction-1360w.webp
authors:
- Tarides
source:
---

<p>OCaml 5 brought significant changes to fundamental parts of the language – notably concurrency using effects and multithreaded parallelism. This has caused some features and tools compatible with OCaml 4.14 to be incompatible with the new update, and several projects at Tarides aim to restore compatibility where that is the case. In today’s post, we will focus on the efforts toward creating a <a href="https://mirage.io/">MirageOS</a> port for OCaml 5.</p>
<p>The main benefit of making MirageOS compatible with OCaml 5 is to make it possible to explore how to best take advantage of features unique to the latest version of the language. An <a href="https://github.com/TheLortex/eio-solo5">early proof-of-concept</a> thus experimented with replacing Lwt with the new concurrency library <a href="https://github.com/ocaml-multicore/eio">Eio</a>. This is just one example, but it illustrates why bringing OCaml 5 support to MirageOS is a high priority for our team.</p>
<p>To port MirageOS to OCaml 5 we have been contributing to the port using <a href="https://github.com/Solo5/solo5">Solo5</a> backends but we are also exploring the possibility to use <a href="https://unikraft.org/">Unikraft</a> backends. The Solo5 port has been released recently, with the versions 1.x of the <code>ocaml-solo5</code> package on opam. As a complementary effort, work has also been ongoing to make cross-compilation easier in general. This will benefit not just the MirageOS project but also any cross-compilation projects for OCaml in the future. Let’s dive into the updates!</p>
<h2>The Solo5 Backend</h2>
<p>The first goal was to restore support for a backend that was available to OCaml 4.14 MirageOS users: <a href="https://github.com/Solo5/solo5">Solo5</a>. Solo5 is popular with developers building MirageOS unikernels, and since it is currently the only fully supported base for building a freestanding Mirage application, getting Solo5 support for OCaml 5 is a big step in the right direction for OCaml users.</p>
<p>The PRs that introduce OCaml 5.2.1 support via the Solo5 API is the fruit of the collective effort of many developers, including, in no particular order: <a href="https://github.com/palainp">Pierre Alain</a>, <a href="https://github.com/fabbing">Fabrice Buoro</a>, <a href="https://github.com/dinosaure">Romain Calascibetta</a>, <a href="https://github.com/haesbaert">Christiano Haesbert</a>, <a href="https://github.com/shym">Samuel Hym</a>, <a href="https://github.com/kit-ty-kate">@kit-ty-kate</a>, <a href="https://github.com/hannesm">Hannes Mehnert</a>, and many more helpful eyes.</p>
<p>So, what is Solo5? Mirage applications can run in a hypervisor, such as KVM or Xen, without a full OS, and Solo5 provides minimal services (such as telling the time, reading or writing a block on the disk or network, etc.) for running an application there. OCaml-Solo5 adds the extra libraries required to build the OCaml runtime on top of Solo5.</p>
<h3>What has Changed?</h3>
<p>As you can imagine, the significant features introduced in OCaml 5 depend on correspondingly large changes to its underlying design. These include assumptions of how the OS works, how the C compiler works, and how the build system is set up. All of these modifications have a direct impact on what OCaml-Solo5 must provide to build the OCaml runtime for MirageOS unikernels.</p>
<p>If you prefer to dive right into the code changes, I recommend that you <a href="https://github.com/mirage/ocaml-solo5/pull/134">visit the PR</a> directly. Otherwise, let’s take a look at what’s new!</p>
<p><strong><code>Nolibc</code> Extensions</strong>
The usage of (OS) threads has changed, as well as how memory is managed (relying in particular on <code>mmap</code>/<code>munmap</code>), some C features such as thread-local storage and C11 atomics are now required. Support for all of these must be added in a freestanding setting such as MirageOS, even if Solo5 remains monocore. Therefore, to make <code>OCaml-Solo5</code> compatible with the latest release, developers have amended the <code>nolibc</code> library and the way the C compiler is invoked. The modifications come in the form of extensions to <code>nolibc</code>, and most of the <code>nolibc</code> extensions included in the PR are inherited from previous PRs by <a href="https://github.com/kit-ty-kate">@kit-ty-kate</a>, <a href="https://github.com/dinosaure">Romain Calascibetta</a>, and <a href="https://github.com/palainp">Pierre Alain</a>, including changes to <code>pthread</code>, <code>mmap</code> and <code>TLS</code>.</p>
<p><strong>Build System</strong>
To address the build system changes, the PR applies version-specific patches to sources when fetched. This replaces the previous method of modifying the OCaml build system with <code>sed</code>s and <code>echo</code>s. The reasoning behind this change is twofold: Firstly, all bar one of the patches have been designed to improve how the compiler build system supports cross-compilation, simplifying the maintenance of the OCaml and Solo5 compatibility for MirageOS. Secondly, making the modification system into separate patches with full explanation messages makes reviewing them easier and clarifies to the user which modified build system they rely on. A neat bonus of this restructuring is that the <code>.opt</code> versions of the compiler are now also built, which should result in better performance, particularly when building large unikernels.</p>
<p><strong>Toolchain</strong>
The update also means that the <code>ocaml-solo5</code> package now installs a new <code>{aarch64,x86_64}-solo5-ocaml-*</code> toolchain. Creating a toolchain avoids baking the build-time directories containing <code>nolibc</code> and <code>openlibm</code> into the generated OCaml compilers. The package generates two versions of the toolchain: one with built-time directories, that is added to <code>PATH</code> only when the compiler builds, and the other with the final destination directories, installed in the <code>bin</code> directory by opam.</p>
<p>Beside adding compatibility with OCaml&nbsp;5 for Solo5, we are also exploring alternative options. In particular, we are working on adding Unikraft support to test whether it can provide better performance.</p>
<h2>The Unikraft Backend</h2>
<p><a href="https://unikraft.org/">Unikraft</a> is a Unikernel Development Kit that lets users create custom unikernels with a large support for standard APIs to help port applications. It is an open-source project maintained and supported by over fifty active contributors. The main benefit of adding support for a Unikraft backend to MirageOS is to improve I/O performance in comparison to the Solo5 API.</p>
<p>Because of these potential benefits, adding the Unikraft backend is a high priority. Currently, there are several repositories being worked on, the most mature of which is <a href="https://github.com/shym/ocaml-unikraft"><code>ocaml-unikraft</code></a>. The project is still in an exploratory phase, and more updates will follow when we have more to share.</p>
<h2>Making the Build of OCaml Cross Compilers Easier</h2>
<p>In addition to new backends, part of improving the user experience with OCaml 5 has focused on improving the compiler’s build system, in particular regarding cross compilers, which is helpful for MirageOS users since OCaml-Solo5 is really a cross compiler to the Solo5 target.</p>
<p>The first step to streamlining the compiler’s build system involved reducing the number of <code>Makefile</code>s down to one, the root <code>Makefile</code>. By bringing all the build logic into one place and avoiding duplication and stacking dependencies, the compiler’s build system is more consistent and easier to use. The effort is split into many PRs, both big and small, including <a href="https://github.com/ocaml/ocaml/pull/11243">#11243</a>, <a href="https://github.com/ocaml/ocaml/pull/11248">#11248</a>, <a href="https://github.com/ocaml/ocaml/pull/11268">#11268</a>, <a href="https://github.com/ocaml/ocaml/pull/11420">#11420</a>, <a href="https://github.com/ocaml/ocaml/pull/11675">#11675</a>.</p>
<p>In addition to reducing the number of <code>Makefile</code>s, the build system improvements also involved improving <code>ocamldep</code>. It needed to be able to distinguish between source vs build trees and have support for <code>lex</code> and <code>yacc</code> input files. The effort also included breaking the <code>dynlink</code> library’s dependency on <code>compilerlibs</code> to make the build system simpler and faster (<a href="https://github.com/ocaml/ocaml/pull/11996">#11996</a>).</p>
<p>Continuing on this work, several PRs have brought more fixes and improvements to cross-compilation, with more on the way. Currently, the largest upstreamed PRs are <a href="https://github.com/ocaml/ocaml/pull/13281">#13281</a>, <a href="https://github.com/ocaml/ocaml/pull/13282">#13282</a>, <a href="https://github.com/ocaml/ocaml/pull/13526">#13526</a>, and <a href="https://github.com/ocaml/ocaml/pull/13312">#13312</a>. If you use the new OCaml 5 capabilities in Mirage with either the Solo5 or Unikraft, you can take advantage of the simplified build systems and cross-compilation.</p>
<h2>Try it Yourself and Stay in Touch</h2>
<p><code>OCaml-Solo5</code> is released and you can get started simply by building MirageOS in a 5.2.1 switch, with no pinning involved. Samuel wrote a quick guide on how to get started with <code>ocaml-solo5</code> in a <a href="https://discuss.ocaml.org/t/mirageos-on-ocaml-5/15822">Discuss post</a>, and we recommend you give it a try. For Unikraft, keep an eye out for updates as work continues behind-the-scenes.</p>
<p>You can connect with us on <a href="https://bsky.app/profile/tarides.com">Bluesky</a>, <a href="https://twitter.com/tarides_">X</a>, <a href="https://mastodon.social/@tarides">Mastodon</a>, <a href="https://www.threads.net/@taridesltd">Threads</a>, and <a href="https://www.linkedin.com/company/tarides">LinkedIn</a> or sign up for our mailing list to stay updated on our latest projects. We look forward to hearing from you!</p>

0 comments on commit ecd9974

Please sign in to comment.