tracy icon indicating copy to clipboard operation
tracy copied to clipboard

Fibers and mutexes

Open dibyendumajumdar opened this issue 1 year ago • 9 comments

Hi, first of all many thanks for creating such a marvelous product.

I am trying to use Tracy in a system that has fibers and fiber mutexes. I notice that fibers show up in the capture - but not mutexes. If I disable fibers and use threads, then mutexes show up.

Is there an assumption that mutexes are thread based?

dibyendumajumdar avatar Dec 02 '24 19:12 dibyendumajumdar

Support for mutexes with fibers is probably not implemented now.

wolfpld avatar Dec 02 '24 19:12 wolfpld

Is this a problem in the front-end (i.e. server)?

dibyendumajumdar avatar Dec 02 '24 20:12 dibyendumajumdar

Yes, it's running for several loops, bun then stops with the "zone ended twice" message:

image

Sven-v-Beuningen avatar Dec 03 '24 07:12 Sven-v-Beuningen

I got similar issue initially but that seems to be caused by incorrect or missing Fiber Enter instrumentation.

p.s. although in your case it may be mismatched Zone start/end?

dibyendumajumdar avatar Dec 03 '24 08:12 dibyendumajumdar

In our code we have fiber mutexes, that work just like thread mutexes. So I can switch the code between fiber version and thread version. The instrumentation for mutex is unchanged and works fine in the thread version - so the issue might be that the Tracy frontend (server) is not associating a mutex lock with the active fiber.

dibyendumajumdar avatar Dec 03 '24 08:12 dibyendumajumdar

Thank you very much, for the reply. I'm pretty sure, I do something with entering and leaving the coroutines wrong, but I don't know how to do it right. The best results I do get with the following code in my thread function:

auto JobSystemWorker::workerFunction() -> void
{
    m_running = true;
    while (m_running)
    {
        auto job = std::move(getJob());

        if (job)
        {
            TracyFiberEnter(m_workerName);     
            (*job)();
            TracyFiberLeave;
        }
    }
}

With "best results" I just mean, that the zones look correct in the server. But it still stops after some loops. In the thread function I just try to grab a suspended coroutine and resume it. It can happen, that the same coroutine is suspended and resumed multiple times, because the coroutine can have multiple lines with "co_await". It can also happen, that a coroutine is resumed by a different thread, than the one which has suspended it. If I run my application just with one thread, everything works fine.

Sven-v-Beuningen avatar Dec 03 '24 09:12 Sven-v-Beuningen

FWIW, on Slides 61-62 below I have an alternative way to instrument mutex locks that works without any special Tracy knowledge or decoration: https://github.com/CppCon/CppCon2023/blob/main/Presentations/Tracy_Profiler_2024.pdf

It won't give you the neat visualization of lock/unlock flow, but it will show you contention, which in my experience is what is most relevant when instrumenting locks for performance profiling.

slomp avatar Dec 20 '24 17:12 slomp

FWIW, on Slides 61-62 below I have an alternative way to instrument mutex locks that works without any special Tracy knowledge or decoration: https://github.com/CppCon/CppCon2023/blob/main/Presentations/Tracy_Profiler_2024.pdf

It won't give you the neat visualization of lock/unlock flow, but it will show you contention, which in my experience is what is most relevant when instrumenting locks for performance profiling.

Thank you

dibyendumajumdar avatar Dec 21 '24 11:12 dibyendumajumdar

FWIW, on Slides 61-62 below I have an alternative way to instrument mutex locks that works without any special Tracy knowledge or decoration: https://github.com/CppCon/CppCon2023/blob/main/Presentations/Tracy_Profiler_2024.pdf

It won't give you the neat visualization of lock/unlock flow, but it will show you contention, which in my experience is what is most relevant when instrumenting locks for performance profiling.

Hi @slomp
I tried out your idea of faking locks as mempools. as you say it shows contention but sadly it won't show who holds the lock and who is waiting - which is something one would like to see.

dibyendumajumdar avatar Jan 05 '25 12:01 dibyendumajumdar