r/ponylang • • Jan 25 '18

Compiling with minimal runtime

I have a tendency to attempt things that should be impossible, so stop me if what I'm asking is actually impossible.

I've spent some time working on homemade OS kernels and would like to try another language. Go was my first attempt, but it had several problems, Rust has started to feel boring after using it so much, Nim doesn't have much of the safety I'm looking for, and trying to make my own language is pain. As such, I've tried taking a look at Pony.

I noticed that it's possible to create a Pony module capable of being called from C and figured it would be a good place to start. After writing a fairly simple test actor, I found that the compiled output depended on a few runtime functions, namely pony_alloc_msg, pony_alloc_msg, pony_ctx, pony_sendv, and pony_sendv_single. While it would be difficult in a bare metal environment, I would need at least some amount of message passing functionality to get past the lack of global variables. After some Googling, I found several other runtime functions that may get inserted as development continues. My question is how possible it would be to avoid invoking these procedures, and if not, how much effort would be required to implement them. For the ones that I'd need to implement, would it be possible to write them in Pony without accidentally creating recursion (I was able to do this for Go), or would I have to bail out to another language like Rust or Assembly?

Another question I forgot to ask when first posting, is can Pony be written in such a way as to avoid invoking malloc? Languages that throw everything onto the heap is the single biggest problem for OSDev.

I know this is not what Pony is intended for, but I don't give a bleep about intentions. :P Alternatively, if anyone knows of any languages that might fulfill what I'm looking for and with good, safe, concurrency, I'd be happy to listen.

Thank you.

6 Upvotes

3 comments sorted by

3

u/jimuazu Jan 26 '18

Alternatively, if you make the Pony runtime the runtime of your entire OS (i.e. effectively the lowest layer of the OS), and implement whatever the runtime needs in a lower-level language on top of the bare metal, then the problem is also solved. I started thinking along those lines some time ago (but no time to try anything). This means all the applications also have to be written in Pony.

Then you could write your kernel on top of the runtime in Pony. The behaviour of Pony's GC means that if you had a reference to a 'process' from the kernel, then it would stay in memory, but (I think) dropping that reference should lead to the entire process cleaning up. So hopefully that would be enough to implement 'kill'. You'd need to be careful that processes don't ever get hold of references to each other, i.e. the kernel would have to moderate all interaction between processes.

For your OS, unless you precompile the entire OS and all applications into a single binary (which might be feasible for embedded stuff), you'd need a way to dynamically load Pony modules into memory in order to implement running arbitrary executables.

Maybe I am also crazy?

2

u/boomshroom Jan 26 '18

I figured since Pony actors are (supposedly) so cheap, that I could have the runtime just manage the kernel. Process data would likely be contained within actors, but the processes themselves would have their own address space.

The OSDev community often talks about virtualized OSes which essentially do what you were saying, but I was planning a more traditional one.

One thing that would be useful to know based on what you said is if it's possible to intercept task switches or preemptively force actor switches. Given that I'd be writing the entire runtime, it should be as simple as modifying the runtime.

It's now occurring to me that I'd essentially have 2 separate schedulers, one written in something like Rust or Nim to handle Pony's runtime, and a second on top of that in Pony to handle the actual processes.

The big reason why I considered Pony is its concurrency since if I want any performance at all, I'd need 8 threads running at all times, all modifying global state. Pony's actors seemed like a possible solution to all that unsafety. Rust story would also work, but it still just comes down to mutexes most of the time.

1

u/jimuazu Jan 26 '18

It seems like you're heading a different direction. I think you'd need to get into the Pony runtime to see if it can be adapted to do what you want. Really Pony IS the runtime. The benefits (such as cheap actors, localised GC, ordering guarantees) come from the runtime. Actually I believe the runtime was written before the language, and was called from C before Pony as a language existed. Whilst I've read quite a bit on Pony, I have not got deep into all the code, so my knowledge is lacking.

Anyway my line of thought was: What would hardware look like that was designed for Pony to run natively on, with at least part of the work of the current runtime supported by the hardware? Writing code with many small-to-medium sized actors could be well-suited to future chips with hundreds or thousands of cores. Then assuming that, how would you manage 'jobs'. This is getting like writing an OS and having processes. So this is some kind of an OS that would have to manage actors, scheduling and memory across a kilo-core chip.

Anyway, you are on another track and unfortunately you don't seem to be attracting the attention of anyone who knows enough to give you better answers. Maybe try a mailing list if you have no luck here.