yse / easy_profiler

Lightweight profiler library for c++
MIT License
2.14k stars 184 forks source link

Doesn't tracking in infinity loops #195

Open aantropov opened 2 years ago

aantropov commented 2 years ago

Hello, is there a way to track code blocks inside the infinity loops?

auto job = JobSystem::Scheduler::CreateJob("Test", []() { while (true) { EASY_BLOCK("Test"); Sleep(100); } }); JobSystem::Scheduler::GetInstance()->Run(job);

The code above is runned in a separated thread and EASY_BLOCK is not tracking Sleep(100). If you change loop to 'one shot' everything is fine.

daohu527 commented 1 year ago

need a EASY_END_BLOCK in the loop.

because EASY_BLOCK use destructor to trigger saving, or explicit end the block


I was wrong before, the for loop triggers the destructor every time it loops. So maybe the save block is not triggered?

fk-bbraun commented 1 year ago

Hi, had a similar problem. The cause was, that the function containing the infinite loop was only finished after the dumpBlocksToFile() call occured.

To make sure the latest possible dump, try a wrapper object in your main:

class ProfilerWrapper {
public:
    ProfilerWrapper() {
        EASY_PROFILER_ENABLE;
        profiler::startListen();
    }

    ~ProfilerWrapper() {
        profiler::dumpBlocksToFile("prof.prof");
    }
};

int main(int argc, char* argv[]) {
    ProfilerWrapper wp;

    ... do threaded work ...

}