Generating a sysimage from running julia system

My system is running, some of the functions has run many times (are actually optimized), there are functions that has been added/overwritten by humman interaction during runtime.
I want to generate a sysimage with all functions (& functions the system know already run) to be started fast&optimized the next time I startup the system.
If only types&functions are dump to sysimage, it is ok, I do not need variables to be dumped.
How to generate a sysimage from a running system? (compiling a new instance of julia from sources is not an option because some sources are written during runtime and not saved in files nor packaged)

1 Like

Up to my knowledge, it is not possible to generate a sysimage directly on-the-fly from a running Julia session.

I would expect that if you create a ‘static’ sysimage which just contains all the dependencies and slow parts (such as plotting etc), then that would already lead to a considerable speedup.

Also, the user could run into very strange problems if functions they defined in a previous session are suddenly frozen into the sysimage and they cannot change them anymore.

1 Like

Do you have any argument to sustain your words (it is not possible)?
If you don’t know how to do it, it do not count for not been possible :slight_smile:
Building snapshots are as easy as get it right for the first time… once it works the first time, it is more of the same kind of work.

I would argue that as PackageCompiler is written in Julia, and works running julia runtime (it is implemented “in Julia”); it is a demostration that it is possible for a running Julia system (session?) to build a sysimage. IMHO (and experience writing snapshots of object environments) if the system has more contents to be dumped it is the same task, simply work on more contents of the system (that can be collected by instrospection if the design of the runtime is complete and modern).

If the internals of dumping is hidden in the runtime machinery, it is not good news, but if building of sysimage “is implemented in julia”, it should not be the case.

Here is the magic line which creates the sysimage in PackageCompiler PackageCompiler.jl/PackageCompiler.jl at master · JuliaLang/PackageCompiler.jl · GitHub

You can read for example here Creating a sysimage · PackageCompiler how julia --output-o=... works and some of the difficulties around it. To answer the question: The core of creating a sysimage is part of the Julia compiler and goes beyond just a simple Julia function to call; it is rather a different mode in which the JIT compiler runs to output the sysimage.

As you can see in the simple example from the PackageCompiler docs, it takes a massive amount of extra time to get the object file. I followed the steps from the link and createed a sysimage containing the function myfunction(x::Vector{Float64}, y::Vector{Float64}) = x .+ y and it took several minutes.

As you said, unfortunatelly, it looks like “magic” :frowning:
PackageCompiler is a hack to automate a compilation from sources (old fashioned technique where the systems are stupid and reflection of the runtime is incomplete/unusable).
In simple words, the building of sysimage is NOT implemented in Julia.

The time spent in building the image is reasonable, because all the sources are compiled… The energy spent running the system is wasted forcing to compile the functions again in another runtime context.
It is a good way to make something simple to be slow, unsecure and depending on files (as compiling in the '70s, before virtual objects environments).

One of the major features of modern&dynamic systems is to do not repeat stupid executions/compilations (exploiting instrospection).

I expected Julia to have a modern runtime capable to do not waste energy spent in previous executions.
Precompile can help but again it is based in sourcecode stored in files and cached binary become full garbage when one method is added/removed and source file is updated.

This is a long standing goal and many have thought about this before.

While in principle the concept sounds simple, the implementation is far more complicated. Work on a number of prerequisite features is ongoing.

One of those prerequisites is making compilation correct, efficient, and fast. A lot of the compilation time in Julia used to be the result of recompilation due to invalidation of previously compiled code. We’ve developed tools to evaluate issues such as this and figured out techniques to avoid invalidations in the first place.

Another prerequisite is being able to split the system image into modular components with external linkage between them. If all that you are doing in a session is purely adding new code, there is no need to rebuild the already compiled system image.

https://github.com/JuliaLang/julia/pull/44527

1 Like

You’re a bit off in your understanding of some of what PackageCompiler is and isn’t doing, but the fundamental reality is that yes, Julia wastes a lot of resources compiling code over and over again. The closest widely-known analogy is template metaprogramming in C++, which also wastes a lot of resources by compiling many times, but they have done more to cache results than we have.

As @mkitti linked, you want Support external linkage in "sysimages" by timholy · Pull Request #44527 · JuliaLang/julia · GitHub and the pull requests that will follow it. It’s not done yet, but it’s on the agenda :slightly_smiling_face: .

3 Likes