How to profile static/global initialization?

Viewed 151

I am trying to profile my code runtime and startup time. I have used perf and uftrace so far to find runtime issues and I have detected a lot of issues at runtime but I would also like to know more about the initialization and know where I am losing the time. I have tried to use LD_DEBUG=statistics to get more info about dynamically loaded libraries but I have not noticed anything out of the ordinary, and it makes sense as I have all of the libraries statically linked with the executable.

Is it possible to measure how long it takes to initialize statics/globals and anything and everything that happens before the flow reaches main()?

1 Answers

perf works fine for init stuff.

I tried with this program:

struct BeforeInit {
    volatile int sink;

    BeforeInit() {
        for (int i = 0; i < 1000000000; i++) {
            sink = i;
        }
    }
};

BeforeInit global;

int main() {
    return 0;
}

perf record and perf report show the overhead mostly all in this function:

enter image description here

Related