[TOOL] Mod Profiling

Started by Luka Kama, July 15, 2017, 05:50:09 AM

Previous topic - Next topic

Luka Kama

I used this method to profile my assembly mod performance, which is very CPU intensive, and I think that it could be useful to someone else.

To enable profiling, you need to invoke the mono_profiler_load mono method passing the profiling configuration. The configuration is the one that you would pass to mono executable for the --profile argument.

This is an example of a class that enables the mono profiling (in my mod, I load the class by defining a Def in the defs XML of the mod, but I'm not sure that it is needed):

[StaticConstructorOnStartup]
public class ModStarter : Def {

[DllImport("__Internal")]
private static extern void mono_profiler_load(string args);

static ModStarter() {
// Enable profiling and save the report to c:\rimworld-prof.mprf
mono_profiler_load(@"default:time,file=c:/rimworld-prof.mprf");
}
}


It seems that the only available profiler in the mono runtime used by Unity is the "default" one, which is the oldest and simplest one, but it is still useful to identify performance bottlenecks.

Also, it seems that the only profiled code is the one executed after the mono_profiler_load invocation, so you must ensure to invoke it before anything else in your mod.

RawCode

you can use stopwatch natives for this task, proper usage of profiler and or debugger require custom build mono.dll

Luka Kama

Stopwatch can be useful to profile specific and limited portions of the code, but they are unusable to spot bottlenecks on a large or complex code base.

Quote from: RawCode on July 24, 2017, 10:11:30 AM
proper usage of profiler and or debugger require custom build mono.dll

I know, I tried to build a custom mono.dll using the one made by olizit as reference, but I gave up after wasting a day trying to setup the build environment using Visual Studio Community without success...

Anyway, I used the profiler with the release mono.dll shipped with Rimworld/Unity. I know that it is half working, as it is activated post-startup, but it seems enough to profile just the mod assembly.