---Ftrace---

Ftrace功能
function_profile_enabled 跟踪函数的profile
kprobe_events

创建event并跟踪,见kprobe

Kprobe

实现文件 trace_kprobe.c

主要参考函数为 __trace_kprobe_create

arm64的实现时通过将原指令替换为BRK指令实现。

echo 'p task_tick_fair' > kprobe_events

添加 task_tick_fair event,会生成

events/kprobes/p_task_tick_fair_0

echo '- task_tick_fair' > kprobe_events 删除上面添加的event
echo 'p task_tick_fair $arg1 $arg2 $arg3' > kprobe_events

task_tick_fair的定义为

task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)

这里arg1对于与rq,arg2对应于curr,arg3对应与queued

echo 'p task_tick_fair rq=$arg1 cur=$arg2 queued=$arg3' > kprobe_events 同上,只是在显示时将arg1显示为rq,方便调试
echo 'r __pick_next_task_fair $retval' > kprobe_events  添加 return probe,添加成功,但获取不到想要的结果
perf
perf probe -x /home/xxx/test/a.out /home/xxx/test/main.c:4 argc argv k 在main.c的第4行probe,打印出argc argv和k这三个参数
perf probe /home/xxx/test/a.out main argc argv 在函数main的入口处probe,打印出argc和argv
perf probe /home/xxx/test/a.out main:4 在main的第4行插入probe
perf probe scheduler* 在所有scheduler开头的函数入口处probe
root@kickseed:~/test# perf probe -V do_sys_open
Available variables at do_sys_open
        @<do_sys_open+0>
                char*   filename
                int     dfd
                int     flags
                struct open_flags       op
                umode_t mode
查看do_sys_open有哪些变量可以在probe中使用

root@kickseed:~/test# perf probe -V do_sys_open --range

Available variables at do_sys_open
        @<do_sys_open+0>
                [INV]   int     fd      @<do_sys_open+[302-307,307-346,427-657,669-690]>
                [INV]   struct filename*        tmp     @<do_sys_open+[284-293,293-348,427-657,669-690]>
                [VAL]   char*   filename        @<do_sys_open+[0-283,320-333,354-427,657-669]>
                [VAL]   int     dfd     @<do_sys_open+[0-320,320-352,352-354,354-451,657-669,685-690]>
                [VAL]   int     flags   @<do_sys_open+[0-63,63-344,354-690]>
                [VAL]   struct open_flags       op      @<do_sys_open+[0-690]>
                [VAL]   umode_t mode    @<do_sys_open+[0-350,354-620,652-690]>

查看变量的有效范围


root@kickseed:~/test# perf probe do_sys_open filename:string flags 'op.mode'

a.out-14389 [000] .... 2459018.446122: do_sys_open: (do_sys_open+0x0/0x2c0) filename_string="/lib/x86_64-linux-gnu/libdl.so.2" flags=557056 mode=0xff20
perf probe __pick_next_task_fair%return $retval 添加ret probe,但没有返回值,待查
perf probe -k /userdata/vmlinux -m /mps/ko/cexxxxv100_isp.ko cnispCreatePipe pipe=viPipe attr='pstPipeAttr->u32Maxw' perf向ko中添加kprobe event
perf buildid-cache -a /mps/ko/ceisp.ko       将ko加载到buildid中,路径是 /root/.debug/.build-id
perf top --symbols kfree // 仅显示指定的符号
echo "wakeup_latency u64 lat; pid_t pid; int prio" >> /sys/kernel/debug/tracing/synthetic_events

echo 'hist:keys=$saved_pid:saved_pid=pid:ts0=common_timestamp.usecs if comm=="time"' >> /sys/kernel/debug/tracing/events/sched/sched_waking/trigger

echo 'hist:keys=next_pid:wakeup_lat=common_timestamp.usecs-$ts0:onmatch(sched.sched_waking).wakeup_latency($wakeup_lat, $saved_pid, next_prio) if next_comm=="time"' >> /sys/kernel/debug/tracing/events/sched/sched_switch/trigger

echo 'hist:keys=pid,prio,lat:sort=pid,lat' >> /sys/kernel/debug/tracing/events/synthetic/wakeup_latency/trigger

添加一个synthetic event,统计latency。
mount -t debugfs nodev /proc/sys/debug
echo schedule > /proc/sys/debug/tracingset_graph_function
echo function_graph > /proc/sys/debug/tracingcurrent_tracer
echo 3 > /proc/sys/debug/tracingmax_graph_depth
echo 1 > /proc/sys/debug/tracingtracing_on 

echo 'r:sched_ret schedule' >> /proc/sys/debug/tracing/kprobe_events 
echo 'p:sched_ent schedule' >> /proc/sys/debug/tracing/kprobe_events 

echo 'hist:keys=common_pid:ts0=common_timestamp.usecs' >> /proc/sys/debug/tracing/events/kprobes/sched_ent/trigger

echo 'hist:keys=common_pid:wakeup_lat=common_timestamp.usecs-$ts0:onmax($wakeup_lat).snapshot()' >> /proc/sys/debug/tracing/events/kprobes/sched_ret/trigger

统计schedule函数的执行时间,并在最大时做snapshot。
上一篇:yaml模块


下一篇:linux kernel源码之kobj_map