[Mono-dev] Long execution time on first execution (in AOT case)

Martin Däumler mdae at cs.tu-chemnitz.de
Tue May 4 08:16:32 EDT 2010

Hello Mono developers,

I need some help from Mono internals experts. Briefly speaking,
I want to know exactly why the first execution of a code snippet,
e.g., a P/Invoke, within Mono takes more time than the second run,
especially in AOT case. First of all, I already read the AOT
documentation [1]. I also tried to dive into the code and wrote
some Mono tracing outputs ... but it is quite complex to select
the necessary information from the whole chain of methods that
keep Mono running.

Source files for the example and information about the test system
(for short: Linux on x86) can be downloaded under [2].

   Let's examine a simple P/Invoke which is executed several times.
   The invoked method just returns the method parameter's value. The
   very first P/Invoke takes much more time (JIT: approx. 312 µs,
   AOT: > 100 ms) than the following ones (JIT: 6 or 7 µs, AOT: < 1 µs)
   on the test system. Test2 and Test3 are variants of Test1 which
   demonstrate cache effects. However, the very first execution takes
   longer in every case, so that's not all due to cache effects.

I want to know how to make the execution of a code snippet/method
deterministic, i.e., like C code can be deterministic, not necessarily
"fast". That is, I tolerate a kind of "initialization" at startup time
of Mono, but the first execution must not take (much) more time than
following executions of the code snippet.

In detail:

   - (How) Is it possible to load a AOT image completely into memory
   (not just map into memory) before executing one of its methods?
   Which function of Mono has to be called which way?

   - (How) Is it possible to fill the GOT- and PLT-entries _before_
   they are used the first time? Maybe a GOT-entry can not be
   filled beforehand if a runtime object's address is not known at
   startup time? Which function is called to do this
   ( decode_patch_info()? )?

   - Which additional functions have to be called when a AOT image's
   method is executed the first time? I.e., how to get metadata
   (mono_aot_get_class_info()?) or other necessary information/runtime
   structures for the Mono runtime in advance?

   - Are there any other functions that could cause longer execution
   time on first execution?

   - Any idea to measure execution time of managed code in granularity
   of µs without using P/Invoke? The Environment.TickCount only provides
   granularity of ms.

   - Why is the execution of code snippets in a loop faster than
   when rolling out the loop in the JIT case?

   - Is it possible to JIT-compile all necessary wrapper methods
   during the "initialization" (instead of porting Full AOT to
   x86)? Note that AOTed wrapper methods (in the Full AOT case)
   could also impose some overhead when starting first execution
   since GOT- and/or PLT-entries have to be filled.

With kind regards,
Martin Däumler

[1] http://mono-project.com/Mono:Runtime:Documentation:AOT
[2] http://www.tu-chemnitz.de/~mdae/sample-code.zip

More information about the Mono-devel-list mailing list