bcc icon indicating copy to clipboard operation
bcc copied to clipboard

bpf_ktime_get_ns() odd returns in 4.8-rc4

Open brendangregg opened this issue 10 years ago • 8 comments

Something is very amiss -- has this been found and fixed already post 4.8-rc4?

I was fixing filelife.py, and got the following:

# ./filelife.py
TIME     PID    COMM             AGE(s)  FILE
23:25:22 20376  rm               0.99    foo
23:25:24 20380  rm               1.00    foo
23:25:26 20384  rm               18446744073.70 foo
23:25:29 20388  rm               18446744073.70 foo
23:25:32 20392  rm               1.00    foo
23:25:34 20396  rm               1.00    foo

Test workload:

# time (touch foo; sleep 0.5; rm foo)

real    0m0.508s
user    0m0.000s
sys 0m0.000s

So none of filelife's output is correct. I added debug statements: https://gist.github.com/brendangregg/0e125937a48e9f27979e8a6942ced635

which print:

(root) /mnt/src/bcc/tools # cat /sys/kernel/debug/tracing/trace_pipe 
           touch-20374 [007] d... 6771453.064892: : at 2938902477515565, create of 8b929240
              rm-20376 [007] d... 6771453.569953: : unlink of 8b929240
              rm-20376 [007] d... 6771453.569959: : at 2938903469266385, delta 991
           touch-20378 [006] d... 6771454.872515: : at 2938904476374014, create of 8b929240
              rm-20380 [006] d... 6771455.377425: : unlink of 8b929240
              rm-20380 [006] d... 6771455.377429: : at 2938905478901276, delta 1002
           touch-20382 [006] d... 6771457.617427: : at 2938907478801260, create of 8b929240
              rm-20384 [006] d... 6771458.122352: : unlink of 8b929240
              rm-20384 [006] d... 6771458.122357: : at 2938907470212787, delta -140462620
           touch-20386 [006] d... 6771460.553749: : at 2938910479575068, create of 8b929240
              rm-20388 [006] d... 6771461.058745: : unlink of 8b929240
              rm-20388 [006] d... 6771461.058750: : at 2938910471184565, delta -140462619
           touch-20390 [006] d... 6771462.930027: : at 2938912469068110, create of 8b929240
              rm-20392 [006] d... 6771463.434816: : unlink of 8b929240
              rm-20392 [006] d... 6771463.434820: : at 2938913471254426, delta 1002
           touch-20394 [006] d... 6771465.162422: : at 2938914470088433, create of 8b929240
              rm-20396 [006] d... 6771465.667355: : unlink of 8b929240
              rm-20396 [006] d... 6771465.667360: : at 2938915472680074, delta 1002

It's interesting to compare the bpf_get_ktime_ns() numbers (after "at") with the builtin timestamp column from trace-pipe (before ":"). The builtin timestamps are working correctly (0.5 second jumps), but the bpf_get_ktime_ns() values are not.

I'd guess this issue is the same as #704

brendangregg avatar Oct 04 '16 23:10 brendangregg

FWIW, __ktime_get_fast_ns() was changed in: [PATCH 1/2] timekeeping: Avoid taking lock in NMI path with CONFIG_DEBUG_TIMEKEEPING

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 3b65746..e07fb09 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -401,7 +401,10 @@ static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf)
        do {
                seq = raw_read_seqcount_latch(&tkf->seq);
                tkr = tkf->base + (seq & 0x01);
-               now = ktime_to_ns(tkr->base) + timekeeping_get_ns(tkr);
+               now = ktime_to_ns(tkr->base);
+
+               now += clocksource_delta(tkr->read(tkr->clock),
+                                        tkr->cycle_last, tkr->mask);
        } while (read_seqcount_retry(&tkf->seq, seq));

        return now;

https://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/commit/?id=27727df240c7cc84f2ba6047c6f18d5addfd25ef

brendangregg avatar Oct 04 '16 23:10 brendangregg

awesome sleuthing. I think you're absolutely right. writing an email to the guys...

4ast avatar Oct 05 '16 00:10 4ast

Rolled back the patch:

TIME     PID    COMM             AGE(s)  FILE
00:16:22 4074   rm               0.50    foo

I might raise this on lkml.. Fortunately I don't think many things use it (BPF and perf).

brendangregg avatar Oct 05 '16 00:10 brendangregg

@4ast oh, if you're already doing an email, here's a simpler test you can copy-n-paste:

broken system:

# perf record -F 9 --cpu 0 --clockid CLOCK_MONOTONIC -- sleep 1
# perf script
         swapper     0 [000] 2942480.468981:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942480.471468:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942480.473959:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942480.475202:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942480.477691:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942481.470226:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942481.472569:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942481.476460:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000] 2942481.478943:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)

Note the timestamp jump.

fixed (rolled back patch):

# perf record -F 9 --cpu 0 --clockid CLOCK_MONOTONIC -- sleep 1
# perf script
         swapper     0 [000]   589.729663:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   589.840772:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   589.951883:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   590.062994:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   590.174075:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   590.285217:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   590.396328:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   590.507439:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)
         swapper     0 [000]   590.618550:  111111111 cpu-clock:  ffffffff810013aa xen_hypercall_sched_op+0xa (vmlinux)

brendangregg avatar Oct 05 '16 00:10 brendangregg

thanks... will include the above and cc you.

4ast avatar Oct 05 '16 00:10 4ast

closing ticket; fix landed in Linux 4.9-rc1: https://github.com/torvalds/linux/commit/58bfea9532552d422bde7afa207e1a0f08dffa7d#diff-03ab89da17a203808142f6a1f0272153

brendangregg avatar Oct 18 '16 23:10 brendangregg

I'm guessing RHEL 7.6 beta has this as of Aug 2018, hopefully fixed soon.

brendangregg avatar Aug 29 '18 07:08 brendangregg

Just checked the RHEL 7.6 Beta source for the functions referenced.So reading this it calls time_keeping_get_ns(tkr) which calls the following timekeeping_get_delta then timekeeping_delta_to_ns which calls clocksource_delta, looking at the flow of the RHEL code it looks like it's doing the same just in more function calls It would be better for someone with more c experience to verify. I will look to see if I can replicate the issues seen however not sure if this is the same issue.

RHEL source.

static inline u64 timekeeping_get_delta(struct tk_read_base *tkr) { u64 cycle_now, delta; struct clocksource *clock;

    /* read clocksource: */
    clock = tkr->clock;
    cycle_now = tkr->clock->read(clock);

    /* calculate the delta since the last update_wall_time */
    delta = clocksource_delta(cycle_now, clock->cycle_last,

clock->mask);

    return delta;

}

static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) { u64 delta;

    delta = timekeeping_get_delta(tkr);
    return timekeeping_delta_to_ns(tkr, delta);

}

static inline s64 timekeeping_delta_to_ns(struct tk_read_base *tkr, u64 delta) { s64 nsec;

    nsec = delta * tkr->mult + tkr->xtime_nsec;
    nsec >>= tkr->shift;

    /* If arch requires, add in get_arch_timeoffset() */
    return nsec + arch_gettimeoffset();

}

static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf) { struct tk_read_base *tkr; unsigned int seq; u64 now;

    do {
            seq = raw_read_seqcount(&tkf->seq);
            tkr = tkf->base + (seq & 0x01);
            now = ktime_to_ns(tkr->base) + timekeeping_get_ns(tkr);
    } while (read_seqcount_retry(&tkf->seq, seq));

    return now;

}

mainline kernel reference in issue with changes applied.

static __always_inline u64 __ktime_get_fast_ns(struct tk_fast *tkf) { struct tk_read_base *tkr; unsigned int seq; u64 now;

do { seq = raw_read_seqcount_latch(&tkf->seq); tkr = tkf->base + (seq & 0x01); now = ktime_to_ns(tkr->base);

now += timekeeping_delta_to_ns(tkr, clocksource_delta( tkr->read(tkr->clock), tkr->cycle_last, tkr->mask)); } while (read_seqcount_retry(&tkf->seq, seq));

return now; }

On Wed, Aug 29, 2018 at 8:10 AM Brendan Gregg [email protected] wrote:

I'm guessing RHEL 7.6 beta has this as of Aug 2018, hopefully fixed soon.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/iovisor/bcc/issues/728#issuecomment-416849068, or mute the thread https://github.com/notifications/unsubscribe-auth/ADPbVD2uf9natmdkSyGCvPb17msLth-Jks5uVj57gaJpZM4KORTy .

mcaleavya avatar Aug 29 '18 10:08 mcaleavya