iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.59k stars 3.88k forks source link

[Question] biolatency.py. Why can we use struct start_key to measure latency? #5102

Closed szp2016 closed 2 months ago

szp2016 commented 2 months ago

Hello everyone. The struct start_key key is defined as a local variable inside the function. When the event is triggered multiple times, the address of the structure variable will not change. Why can it be used as a key to measure the delay of each IO? Thanks.

static int __trace_req_start(struct start_key key)
{
    DISK_FILTER

    u64 ts = bpf_ktime_get_ns();
    start.update(&key, &ts);
    return 0;
}

int trace_req_start(struct pt_regs *ctx, struct request *req)
{
    struct start_key key = {
        .dev = ddevt(req->__RQ_DISK__),
        .sector = req->__sector
    };

    SET_FLAGS

    return __trace_req_start(key);
}

// output
static int __trace_req_done(struct start_key key)
{
    u64 *tsp, delta;

    // fetch timestamp and calculate delta
    tsp = start.lookup(&key);
    if (tsp == 0) {
        return 0;   // missed issue
    }
    delta = bpf_ktime_get_ns() - *tsp;

    FACTOR

    // store as histogram
    STORE

    start.delete(&key);
    return 0;
}

int trace_req_done(struct pt_regs *ctx, struct request *req)
{
    struct start_key key = {
        .dev = ddevt(req->__RQ_DISK__),
        .sector = req->__sector
    };

    SET_FLAGS

    return __trace_req_done(key);
}