1*387f9dfdSAndroid Build Coastguard WorkerDemonstrations of criticalstat: Find long atomic critical sections in the kernel. 2*387f9dfdSAndroid Build Coastguard Worker 3*387f9dfdSAndroid Build Coastguard Workercriticalstat traces and reports occurrences of atomic critical sections in the 4*387f9dfdSAndroid Build Coastguard Workerkernel with useful stacktraces showing the origin of them. Such critical 5*387f9dfdSAndroid Build Coastguard Workersections frequently occur due to use of spinlocks, or if interrupts or 6*387f9dfdSAndroid Build Coastguard Workerpreemption were explicitly disabled by a driver. IRQ routines in Linux are also 7*387f9dfdSAndroid Build Coastguard Workerexecuted with interrupts disabled. There are many reasons. Such critical 8*387f9dfdSAndroid Build Coastguard Workersections are a source of long latency/responsive issues for real-time systems. 9*387f9dfdSAndroid Build Coastguard Worker 10*387f9dfdSAndroid Build Coastguard WorkerThis works by probing the preempt/irq and cpuidle tracepoints in the kernel. 11*387f9dfdSAndroid Build Coastguard WorkerSince this uses BPF, only the root user can use this tool. Further, the kernel 12*387f9dfdSAndroid Build Coastguard Workerhas to be built with certain CONFIG options enabled inorder for it to work: 13*387f9dfdSAndroid Build Coastguard WorkerCONFIG_PREEMPTIRQ_EVENTS before kernel 4.19 14*387f9dfdSAndroid Build Coastguard WorkerCONFIG_PREEMPTIRQ_TRACEPOINTS in kernel 4.19 and later 15*387f9dfdSAndroid Build Coastguard WorkerCONFIG_DEBUG_PREEMPT 16*387f9dfdSAndroid Build Coastguard WorkerCONFIG_PREEMPT_TRACER 17*387f9dfdSAndroid Build Coastguard WorkerAdditionally, the following options should be turned off on older kernels: 18*387f9dfdSAndroid Build Coastguard WorkerCONFIG_PROVE_LOCKING 19*387f9dfdSAndroid Build Coastguard WorkerCONFIG_LOCKDEP 20*387f9dfdSAndroid Build Coastguard Worker 21*387f9dfdSAndroid Build Coastguard WorkerUSAGE: 22*387f9dfdSAndroid Build Coastguard Worker# ./criticalstat -h 23*387f9dfdSAndroid Build Coastguard Workerusage: criticalstat [-h] [-p] [-i] [-d DURATION] 24*387f9dfdSAndroid Build Coastguard Worker 25*387f9dfdSAndroid Build Coastguard WorkerTrace long critical sections 26*387f9dfdSAndroid Build Coastguard Worker 27*387f9dfdSAndroid Build Coastguard Workeroptional arguments: 28*387f9dfdSAndroid Build Coastguard Worker -h, --help Show this help message and exit 29*387f9dfdSAndroid Build Coastguard Worker -p, --preemptoff Find long sections where preemption was off 30*387f9dfdSAndroid Build Coastguard Worker -i, --irqoff Find long sections where IRQ was off 31*387f9dfdSAndroid Build Coastguard Worker -d DURATION, --duration DURATION 32*387f9dfdSAndroid Build Coastguard Worker Duration in uS (microseconds) below which we filter 33*387f9dfdSAndroid Build Coastguard Worker 34*387f9dfdSAndroid Build Coastguard Workerexamples: 35*387f9dfdSAndroid Build Coastguard Worker ./criticalstat # run with default options: irq off for more than 100 uS 36*387f9dfdSAndroid Build Coastguard Worker ./criticalstat -p # find sections with preemption disabled for more than 100 uS 37*387f9dfdSAndroid Build Coastguard Worker ./criticalstat -d 500 # find sections with IRQs disabled for more than 500 uS 38*387f9dfdSAndroid Build Coastguard Worker ./criticalstat -p -d 500 # find sections with preemption disabled for more than 500 uS 39*387f9dfdSAndroid Build Coastguard Worker 40*387f9dfdSAndroid Build Coastguard WorkerThe tool runs continuously until interrupted by Ctrl-C 41*387f9dfdSAndroid Build Coastguard Worker 42*387f9dfdSAndroid Build Coastguard Worker 43*387f9dfdSAndroid Build Coastguard WorkerBy default, criticalstat finds IRQ disable sections for > 100us. 44*387f9dfdSAndroid Build Coastguard Worker 45*387f9dfdSAndroid Build Coastguard Worker# ./criticalstat 46*387f9dfdSAndroid Build Coastguard WorkerFinding critical section with IRQ disabled for > 100us 47*387f9dfdSAndroid Build Coastguard Worker=================================== 48*387f9dfdSAndroid Build Coastguard WorkerTASK: kworker/u16:5 (pid 5903 tid 5903) Total Time: 194.427 us 49*387f9dfdSAndroid Build Coastguard Worker 50*387f9dfdSAndroid Build Coastguard WorkerSection start: __schedule -> schedule 51*387f9dfdSAndroid Build Coastguard WorkerSection end: _raw_spin_unlock_irq -> finish_task_switch 52*387f9dfdSAndroid Build Coastguard Worker trace_hardirqs_on+0xdc 53*387f9dfdSAndroid Build Coastguard Worker trace_hardirqs_on+0xdc 54*387f9dfdSAndroid Build Coastguard Worker _raw_spin_unlock_irq+0x18 55*387f9dfdSAndroid Build Coastguard Worker finish_task_switch+0xf0 56*387f9dfdSAndroid Build Coastguard Worker __schedule+0x8c8 57*387f9dfdSAndroid Build Coastguard Worker preempt_schedule_irq+0x38 58*387f9dfdSAndroid Build Coastguard Worker el1_preempt+0x8 59*387f9dfdSAndroid Build Coastguard Worker=================================== 60*387f9dfdSAndroid Build Coastguard Worker 61*387f9dfdSAndroid Build Coastguard Worker 62*387f9dfdSAndroid Build Coastguard WorkerIf too many sections are showing up, the user can raise the threshold to only 63*387f9dfdSAndroid Build Coastguard Workershow critical sections that are > 500us by passing "-d" option: 64*387f9dfdSAndroid Build Coastguard Worker 65*387f9dfdSAndroid Build Coastguard Worker# ./criticalstat -d 500 66*387f9dfdSAndroid Build Coastguard WorkerFinding critical section with IRQ disabled for > 500us 67*387f9dfdSAndroid Build Coastguard Worker=================================== 68*387f9dfdSAndroid Build Coastguard WorkerTASK: crtc_commit:111 (pid 246 tid 246) Total Time: 580.730 us 69*387f9dfdSAndroid Build Coastguard Worker 70*387f9dfdSAndroid Build Coastguard WorkerSection start: clk_enable_lock -> clk_enable 71*387f9dfdSAndroid Build Coastguard WorkerSection end: _raw_spin_unlock_irqrestore -> clk_enable 72*387f9dfdSAndroid Build Coastguard Worker trace_hardirqs_on+0xdc 73*387f9dfdSAndroid Build Coastguard Worker trace_hardirqs_on+0xdc 74*387f9dfdSAndroid Build Coastguard Worker _raw_spin_unlock_irqrestore+0x24 75*387f9dfdSAndroid Build Coastguard Worker clk_enable+0x80 76*387f9dfdSAndroid Build Coastguard Worker msm_dss_enable_clk+0x7c 77*387f9dfdSAndroid Build Coastguard Worker sde_power_resource_enable+0x578 78*387f9dfdSAndroid Build Coastguard Worker _sde_crtc_vblank_enable_no_lock+0x68 79*387f9dfdSAndroid Build Coastguard Worker sde_crtc_vblank+0x8c 80*387f9dfdSAndroid Build Coastguard Worker sde_kms_enable_vblank+0x18 81*387f9dfdSAndroid Build Coastguard Worker vblank_ctrl_worker+0xd0 82*387f9dfdSAndroid Build Coastguard Worker kthread_worker_fn+0xf8 83*387f9dfdSAndroid Build Coastguard Worker kthread+0x114 84*387f9dfdSAndroid Build Coastguard Worker ret_from_fork+0x10 85*387f9dfdSAndroid Build Coastguard Worker=================================== 86*387f9dfdSAndroid Build Coastguard Worker 87*387f9dfdSAndroid Build Coastguard Worker 88*387f9dfdSAndroid Build Coastguard WorkerIf instead of irq disabled sections, we want to see preempt disabled sections, 89*387f9dfdSAndroid Build Coastguard Workerthen pass the "-p" option. Below we try to find preempt-disabled critical 90*387f9dfdSAndroid Build Coastguard Workersections that are > 500us. 91*387f9dfdSAndroid Build Coastguard Worker 92*387f9dfdSAndroid Build Coastguard Worker# ./criticalstat -p -d 500 93*387f9dfdSAndroid Build Coastguard WorkerFinding critical section with preempt disabled for > 500us 94*387f9dfdSAndroid Build Coastguard Worker=================================== 95*387f9dfdSAndroid Build Coastguard WorkerTASK: swapper/1 (pid 0 tid 0) Total Time: 618.437 us 96*387f9dfdSAndroid Build Coastguard Worker 97*387f9dfdSAndroid Build Coastguard WorkerSection start: preempt_count_add -> preempt_count_add 98*387f9dfdSAndroid Build Coastguard WorkerSection end: preempt_count_sub -> preempt_count_sub 99*387f9dfdSAndroid Build Coastguard Worker trace_preempt_on+0x98 100*387f9dfdSAndroid Build Coastguard Worker trace_preempt_on+0x98 101*387f9dfdSAndroid Build Coastguard Worker preempt_latency_stop+0x164 102*387f9dfdSAndroid Build Coastguard Worker preempt_count_sub+0x50 103*387f9dfdSAndroid Build Coastguard Worker schedule+0x74 104*387f9dfdSAndroid Build Coastguard Worker schedule_preempt_disabled+0x14 105*387f9dfdSAndroid Build Coastguard Worker cpu_startup_entry+0x84 106*387f9dfdSAndroid Build Coastguard Worker secondary_start_kernel+0x1c8 107*387f9dfdSAndroid Build Coastguard Worker [unknown] 108*387f9dfdSAndroid Build Coastguard Worker=================================== 109*387f9dfdSAndroid Build Coastguard Worker 110*387f9dfdSAndroid Build Coastguard Worker 111*387f9dfdSAndroid Build Coastguard Workercriticalstat -p can also reflect kernel scheduler issues sometimes. These may 112*387f9dfdSAndroid Build Coastguard Workershow up as long preempt-off sections if the functions in the scheduler take a 113*387f9dfdSAndroid Build Coastguard Workerlong time to run (such as pick_next_task_fair which selects the CPU for a task 114*387f9dfdSAndroid Build Coastguard Worker 115*387f9dfdSAndroid Build Coastguard WorkerFollow is a report showing a preempt-off latency of 700us during the schedule 116*387f9dfdSAndroid Build Coastguard Workerloop's execution: 117*387f9dfdSAndroid Build Coastguard Worker 118*387f9dfdSAndroid Build Coastguard Worker=================================== 119*387f9dfdSAndroid Build Coastguard WorkerTASK: irq/296-cs35l36 (pid 666 tid 666) Total Time: 732.657 us 120*387f9dfdSAndroid Build Coastguard Worker 121*387f9dfdSAndroid Build Coastguard WorkerSection start: schedule -> schedule 122*387f9dfdSAndroid Build Coastguard WorkerSection end: schedule -> schedule 123*387f9dfdSAndroid Build Coastguard Worker trace_preempt_on+0x98 124*387f9dfdSAndroid Build Coastguard Worker trace_preempt_on+0x98 125*387f9dfdSAndroid Build Coastguard Worker preempt_count_sub+0xa4 126*387f9dfdSAndroid Build Coastguard Worker schedule+0x78 127*387f9dfdSAndroid Build Coastguard Worker schedule_timeout+0x80 128*387f9dfdSAndroid Build Coastguard Worker wait_for_common+0xb4 129*387f9dfdSAndroid Build Coastguard Worker wait_for_completion_timeout+0x28 130*387f9dfdSAndroid Build Coastguard Worker geni_i2c_xfer+0x298 131*387f9dfdSAndroid Build Coastguard Worker __i2c_transfer+0x4e0 132*387f9dfdSAndroid Build Coastguard Worker i2c_transfer+0x8 133*387f9dfdSAndroid Build Coastguard Worker irq_thread_fn+0x2c 134*387f9dfdSAndroid Build Coastguard Worker irq_thread+0x160 135*387f9dfdSAndroid Build Coastguard Worker kthread+0x118 136*387f9dfdSAndroid Build Coastguard Worker ret_from_fork+0x10 137*387f9dfdSAndroid Build Coastguard Worker=================================== 138*387f9dfdSAndroid Build Coastguard Worker 139*387f9dfdSAndroid Build Coastguard Worker 140*387f9dfdSAndroid Build Coastguard WorkerSee Also: Linux kernel's preemptoff and irqoff tracers which provide similar 141*387f9dfdSAndroid Build Coastguard Workertracing but with some limitations. 142