[Request] Performance counter/monitor

Started by biship, August 06, 2016, 05:11:05 PM

Previous topic - Next topic

biship

For most moddable games, someone eventually comes out with a performance meter of some kind.

Is there one for Rimworld?

I'm looking for, or willing to learn how to make (need pointers), a dll to monitor the frequency (to start with) that mod methods (or injectors) fire.
To paint a picture - a mod is supposed to fire when a pawn's mood changes, yet fires needlessly on every pawn item interaction. (this doesn't happen in any mod AFAIK).
An extension to the performance counter would be to determine how long is spent in each method.

I am just wondering if there is a way to determine the impact of other people's code ingame.

I've got to the typical situation where I'm getting game slowdown due to many mods, and would like to be able to identify which mods are impacting processing the most and then whack them off my mod list.
Thanks for any replies.

1000101

What you are refering to is called a "Profiler".  Unfortunately using a profiles requires certain debug information to be present in the compiled code sections that are going to be profiled.  Not a problem for mods, simply compile for debug.

Bigger problem - You can't attach a debugger or any debugging processes to RimWorld.  Many people have tried, many people have failed.  The only debugger I have ever successfully attached to RimWorld is OlyDbg which is an assembler debugger.  Not very useful to see RimWorld as an assembly dump though.

However, RimWorld has it's own built in profiler class which can be used.  I haven't tried to use it and for the release builds of the game they may only exist as stubs (empty methods).
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

biship

Thanks 69, I was hoping you'd reply.
This can even be done just in LUA mods for other games. I guess the modding approach Rimworld took limits what can be done. How about for mods that use CCL functions? Any way (yet) to another mod or CCL to profile those calls? I suspect it's not a current CCL ability.

1000101

CCL doesn't have any profiling methods but if the core RimWorld methods aren't available I suppose I could make one.  They would have to be specifically called by the modder in their code though.
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

biship

Ok, thanks for the explanations 69. Hopefully nearer or after release we have more ability to monitor something like this. Keep up the much appreciated modding. :)

mzbear

Quote from: 1000101 on August 07, 2016, 01:04:52 AM
Unfortunately using a profiles requires certain debug information to be present in the compiled code sections that are going to be profiled.  Not a problem for mods, simply compile for debug.

Not a problem, actually. The CLR DLLs come with symbol information and mono runtime can be queried to lookup names for JIT'd methods.

I've written such a profiler in the past, one that that runs from inside Unity mod, although it only supported OSX and still had some issues. It shouldn't be impossible to port it to RimWorld and to support Windows as well, but it's not a small amount of work either...

Quote from: 1000101 on August 07, 2016, 01:04:52 AM
However, RimWorld has it's own built in profiler class which can be used.  I haven't tried to use it and for the release builds of the game they may only exist as stubs (empty methods).

It looked like a cooperative profiler, not an interrupting profiler. Not exactly useful for analyzing mods, especially not ones that detour functions.

1000101

@mzbear

Doesn't really matter when you can't actually attach the debug process to RimWorld.
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

mzbear

Quote from: 1000101 on August 07, 2016, 06:57:50 PM
@mzbear

Doesn't really matter when you can't actually attach the debug process to RimWorld.

Haven't tried but I can't see why it wouldn't work with a custom tool. I've done it with another Unity based game and I don't think this one does anything different.

1000101

I think the main executable is compiled as an unmanaged.  I've attached an assembly debugger but that doesn't help much.
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

mzbear

Quote from: 1000101 on August 08, 2016, 04:14:33 AM
I think the main executable is compiled as an unmanaged.  I've attached an assembly debugger but that doesn't help much.

You can get stacktraces of the JIT'd code just fine even with a normal debugger. You just need something that can query the mono runtime for the symbols. I'm a Mac user with my own self-written tools so I can't really give you any better advice than that.

In the profiler I wrote for another Unity game, I used a c++ dll with the following code to do the symbol lookups to JIT'd code:

MonoJitInfo *ji = mono_jit_info_table_find (domain, (uint8_t*)ip);
if (ji) {
std::string methodName;
if (ji->method->klass->name_space && *ji->method->klass->name_space != '\0')
methodName += std::string() + ji->method->klass->name_space +".";
methodName += std::string() + ji->method->klass->name + "::" + ji->method->name;

return methodName;
}


This was done from inside the Unity process, my mod simply used dlopen() to load the native dll. I suppose you could perfectly well do this without the extra dll too, although I'm a C# newbie so I chose to do the heavy work in a language where I knew how things will align in memory.

mzbear

And would you look at that, I just noticed RawCode posted in another thread how to do the jit table lookups from C# properly, although for other purpose.

https://ludeon.com/forums/index.php?topic=22812.msg246250#msg246250

Maybe I'll rewrite my profiler in pure C# if I can figure out how to do the rest of the difficult things inside managed code ... namely, suspending all other threads and getting their cpu contexts to do a stack walk. There probably isn't a clean portable way to do this though. I'll have to talk a look into it later.

Illusion Distort

I suggest you guys make an open branch on git and post what you come up with.
Other have been looking for this through the Alphas (including me) so instead of 3 people writing their own code for personal use, we could maby one day have something decent that we can use for debugging.

biship

Yeah, I'd love to see the code you come up with and contribute to anything simple :P