Skip to content

perf data idle time #393

@prim

Description

@prim

speedscope provides a great time order view.

But for perf data, when the program is idle, speedscope will think that the time is consumed by the next sampled call stack. This is wrong, the program should be idle at this time.


$ cat test.c

#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>

void fselect() {
    fd_set rfds;
    struct timeval tv;
    int retval;

    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    retval = select(1, &rfds, NULL, NULL, &tv);

    if (retval == -1)
        perror("select()");
    else if (retval)
        printf("Data is available now.\n");
        /* FD_ISSET(0, &rfds) will be true. */
    else
        printf("No data within two seconds.\n");
}

void timed(int microseconds) {
    int sec, usec, diff;
    struct timeval tv;
    gettimeofday(&tv, NULL);
    // printf("begin seconds : %ld micro seconds : %ld\n", tv.tv_sec, tv.tv_usec);
    sec = tv.tv_sec;
    usec = tv.tv_usec;

    while (1) {
        gettimeofday(&tv, NULL);
        diff = (tv.tv_sec - sec) * 1000000 + tv.tv_usec - usec;
        if (diff > microseconds) {
            // printf("end seconds : %ld micro seconds : %ld\n", tv.tv_sec, tv.tv_usec);
            break;
        }
    }
}

void a() { timed(500000); }
void b() { timed(500000); }
void c() { timed(500000); }
void d() { timed(500000); }
void e() { timed(500000); }

void *fthread(void *args) {
    func();
    return NULL;
}

void func() {
    while (1) {
        a();
        b();
        fselect();
        c();
        d();
        e();
        printf("sleep begin\n");
        sleep(1.5);
        printf("sleep end\n");
    }
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, fthread, NULL);

    func();
    return 0;
}

$ gcc test.c -O0 -g -lpthread && ./a.out

$ perf record -F 99 -p 320355 -g -e cpu-clock 

The result now looks like this:

image

A more correct result should look like this:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions