zoukankan      html  css  js  c++  java
  • ftrace笔记

    1. mount -t debugfs nodev /sys/kernel/debug
    在mount后,可以在debug目录下看到tracing目录,该目录包含了ftrace的控制与输出文件。
    (1) enable ftrace可以获得更为详细的信息
    1. sysctl kernel.ftrace_enabled=1
    (2) 使用tracer
    1. #cat availabe_tracers                             //查看当前支持的tracer有哪些
    2. blk function_graph wakeup ... nop
    3. #echo wakeup > current_tracer                     
    4. #echo latency-format > trace_options
    5. #echo 0 > tracing_max_latency
    6. #echo 1 > tracing_on
    7. #chrt -f 5 sleep 1
    8. #echo 0 > tracing_on
    9. #cat trace
    (3) 每一个CPU都有一个对应的internal trace buffer
    1. #cat buffer_size_kb
    2. #echo nop > current_tracer
    3. #echo 10000 > buffer_size_kb
    4. #cat buffer_size_kb
    (4) trace_pipe是动态的,读取trace_pipe会阻塞直到有内容写入trace_pipe,这种方式的好处是打印出来的信息全部都是你关心的内容,如果再加上一些其他的选项进行过滤,就能很快得到你想要的内容。
    1. #echo function > current_tracer
    2. #cat trace_pipe > /tmp/trace.out &
    3. #echo 1 > tracing_on
    4. #usleep 1
    5. #echo 0 > tracing_on
    6. #cat /tmp/trace.out
    (5) function graph tracer
    第一列代表函数执行在第几个cpu上,第二列代表的函数执行时间,如果其对应的第三列是一个"}",则代表的是一个函数体的执行时间。在第二列前,如果达到了duration thresholds,会有一个overhead field,"+"代表函数执行超过10usec,"!"代表函数执行超过100usec。关闭这些属性的操作(启用的话去掉"no"即可);
    1. echo nofuncgraph-cpu > trace_options
    2. echo nofuncgraph-duration > trace_options
    3. echo nofuncgraph-overhead > trace_options
    如果你想只观察某个cpu的func graph,可以使用tracing_cpu_mask文件,写入内容格式参见taskset命令
    func graph还有一些其他的选项可以启用:
    1. echo funcgraph-proc > trace_options
    2. echo funcgrpah-abstime > trace_options
    这里TIME为绝对时间
    (6) 动态ftrace
    文件set_ftrace_filter与set_ftrace_notrace用来enable或者disable某些特定函数的trace,这些函数可以通过查看文件available_filter_functions得到。
    1) 支持使用通配符:
    1. <match>*
    2. *<match>
    3. *<match>*
    4. #echo 'hrtimer_*' > set_ftrace_filter
    5. #echo > set_ftrace_filter
    2) filter commands
    1. syntax: <function>:<command>:<parameter>
    2. 1. mod, this command enables function filtering per module
    3. #echo 'write*:mod:ext3' > set_ftrace_filter
    4. #echo '!writeback*:mod:ext3' >>set_ftrace_filter
    5. 2. traceon/traceoff
    6. these commands trun tracing on and off when the specified functions are hit. The parameter determines how many times the tracing system is turned on and off. If unspecified, there is no limit.
    7. For example, to disable tracing when a schedule bug is hit in the first 5 times, run:
    8. #echo '__schedule_bug:traceoff:5' > set_ftrace_filter
    9. These command are cumulative whether or not they are appended to set_ftrace_filter. To remove a command, prepend it by '!' and drop the parameter:
    10. #echo '!__schedule_bug:traceoff'
    3) set_graph_function, help you to trace only one function and all of its children.
    1. #echo __do_fault > set_graph_function
    (7) event机制,内核中某个信息的trace需要使用event,如在2.6.38内核上,目前sched机制是使用event进行trace。
    1) 使用event有两种方式
    i) 通过set_event文件
    1. #echo sched_wakeup >> set_event
    2. #echo '!sched_wakeup' >> set_event
    3. #echo > set_event
    events被组织成一个个子系统,如ext4,irq,sched等待,一个完整的event name是这样的:<subsystem>:<event>,其中subsystem是可选的,一个子系统中所有的events可以通过<subsystem>:*指定。
    1. #echo *:* > set_event
    2. #echo 'irq:*' > set_event
    ii) 通过'enable' toggle
    1. #echo 1 > /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
    2. #echo 1 > /sys/kernel/debug/tracing/events/sched/enable
    3. #echo 1 > /sys/kernel/debug/tracing/events/enable
    2) 定义一个event-enabled tracepoint
    tracepoint不在本文讨论范围之内
    3) event format
    这里,不以common_开头的filed是每个event特有的。每一个event对应的有一个TRACE_EVENT定义一个record,这里offset为field在record中的偏移,size为其大小。下载一个内核,在samples/trace_events目录下有一个例子,可以查看TRACE_EVENT的定义方法。
    4) event filter
    语法:
    1. field-name relation-operatior value                           //一个predicate
    可以通过'&&', '||',以及圆括号将几个predicate组合起来。对于数字域,可以使用操作符==, !=, < , > , <=, >=,对于字符串域,可以使用==, !=。目前字符串只支持完全匹配,且最多可以组合16个predicate。
    例子:
    1. #cd /sys/kernel/debug/tracing/events/signal/signal_generate
    2. #echo "((sig >= 10 && sig < 15) || sig == 17) && comm != bash" > filter
    每个子系统都有独立的filter文件支持,如:
    1. #cd /sys/kernel/debug/tracing/events/sched
    2. #echo 0 > filter
    3. #cat sched_switch/filter
    4. none
    5. #cat sched_wakeup/filter
    6. #echo common_pid == 0 > filter
    7. #cat sched_switch/filter
    8. common_pid == 0
    9. #cat sched_wakeup/filter
    10. common_pid == 0
    11. #echo prev_pid == 0 > filter
    12. #cat sched_switch/filter
    13. prev_pid == 0
    14. #cat sched_wakeup/filter
    15. common_pid == 0
    可以看到,虽然将sched子系统的filter设为prev_pid == 0,但由于只有sched_switch事件存在prev_pid域,所以对sched的filter文件的设置只影响了sched_switch。
    (8) 其他一些feature
    set_ftrace_pid: 可以选择trace指定进程
    tracing_cpumask: 可以选择trace制定cpu
    tracing_max_latency: 一些tracer会trace最大延迟,并将时间写入该文件。如果当前trace的最大延迟比文件中大,就会执行写入(时间单位:microsecond)。
    trace_options: 这个文件中包含一些选项,可以控制输出内容的格式,如上面提过的funcgraph-proc
    1) irqsoff,跟踪中断disable的时间,将最大延迟写入tracing_max_latency
    1. #cd /sys/kernel/debug/tracing
    2. #echo nop > current_tracer
    3. #echo 'irq:*' > set_event
    4. #echo latency-format > trace_options
    5. #echo 0 > tracing_max_latency
    6. #echo 1 > tracing_on
    7. #sleep 1
    8. #echo 0 > tracing_on
    9. #cat trace
    中断与抢占相关的输出结果很有意思,如下图:
    cmp: trace的进程名称
    pid: 进程的PID
    CPU#: 进程运行在哪个cpu上
    irqs-off: 如果中断被disable,则为'd',否则为'.'。如果体系结构不支持读取irq flag变量,则为'X'
    need-resched: 如果需要调度,则为'N',否则为'.'
    hardirq/softirq: 'H'代表在softirq中发生了hardirq,'h'正在运行hard irq,'s'正在运行soft irq,'.'代表正常上下文。
    preempt-depth: 代表抢占disable的级别。
    time: 当enable latency-format的时候,代表自trace开始到此的相对时间,否则代表绝对时间。
    delay: '+'大于1毫秒,'!'大于preepmt_mark_thresh(默认为100), ' '小于等于1毫秒
  • 相关阅读:
    448-查找数组中消失的所有数字
    977 -排序数组的正方形
    爬虫小总结
    增量式爬虫
    分布式爬虫
    CrawlSpider:类,Spider的一个子类
    中间件
    中间件
    scrapy图片数据爬取之ImagesPipeline
    scrapy五大核心组件
  • 原文地址:https://www.cnblogs.com/jefree/p/4439015.html
Copyright © 2011-2022 走看看