├── .gitignore ├── LICENSE ├── Makefile ├── README.md └── trace_runqlat.c /.gitignore: -------------------------------------------------------------------------------- 1 | # 2 | # NOTE! Don't add files that are generated in specific 3 | # subdirectories here. Add them in the ".gitignore" file 4 | # in that subdirectory instead. 5 | # 6 | # NOTE! Please use 'git ls-files -i --exclude-standard' 7 | # command after changing this file, to see if there are 8 | # any tracked files which get ignored after the change. 9 | # 10 | # Normal rules (sorted alphabetically) 11 | # 12 | .* 13 | *.a 14 | *.asn1.[ch] 15 | *.bin 16 | *.bz2 17 | *.c.[012]*.* 18 | *.dt.yaml 19 | *.dtb 20 | *.dtb.S 21 | *.dwo 22 | *.elf 23 | *.gcno 24 | *.gz 25 | *.i 26 | *.ko 27 | *.lex.c 28 | *.ll 29 | *.lst 30 | *.lz4 31 | *.lzma 32 | *.lzo 33 | *.mod 34 | *.mod.c 35 | *.ns_deps 36 | *.o 37 | *.o.* 38 | *.patch 39 | *.s 40 | *.so 41 | *.so.dbg 42 | *.su 43 | *.symtypes 44 | *.tab.[ch] 45 | *.tar 46 | *.xz 47 | Module.symvers 48 | modules.builtin 49 | modules.order 50 | 51 | # 52 | # Top-level generic files 53 | # 54 | /tags 55 | /TAGS 56 | /linux 57 | /vmlinux 58 | /vmlinux.32 59 | /vmlinux-gdb.py 60 | /vmlinuz 61 | /System.map 62 | /Module.markers 63 | /modules.builtin.modinfo 64 | 65 | # 66 | # RPM spec file (make rpm-pkg) 67 | # 68 | /*.spec 69 | 70 | # 71 | # Debian directory (make deb-pkg) 72 | # 73 | /debian/ 74 | 75 | # 76 | # Snap directory (make snap-pkg) 77 | # 78 | /snap/ 79 | 80 | # 81 | # tar directory (make tar*-pkg) 82 | # 83 | /tar-install/ 84 | 85 | # 86 | # We don't want to ignore the following even if they are dot-files 87 | # 88 | !.clang-format 89 | !.cocciconfig 90 | !.get_maintainer.ignore 91 | !.gitattributes 92 | !.gitignore 93 | !.mailmap 94 | 95 | # 96 | # Generated include files 97 | # 98 | /include/config/ 99 | /include/generated/ 100 | /include/ksym/ 101 | /arch/*/include/generated/ 102 | 103 | # stgit generated dirs 104 | patches-* 105 | 106 | # quilt's files 107 | patches 108 | series 109 | 110 | # cscope files 111 | cscope.* 112 | ncscope.* 113 | 114 | # gnu global files 115 | GPATH 116 | GRTAGS 117 | GSYMS 118 | GTAGS 119 | 120 | # id-utils files 121 | ID 122 | 123 | *.orig 124 | *~ 125 | \#*# 126 | 127 | # 128 | # Leavings from module signing 129 | # 130 | extra_certificates 131 | signing_key.pem 132 | signing_key.priv 133 | signing_key.x509 134 | x509.genkey 135 | 136 | # Kconfig presets 137 | /all.config 138 | /alldef.config 139 | /allmod.config 140 | /allno.config 141 | /allrandom.config 142 | /allyes.config 143 | 144 | # Kdevelop4 145 | *.kdev4 146 | 147 | # Clang's compilation database file 148 | /compile_commands.json 149 | 150 | # Python cache files 151 | *__pycache__* 152 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | obj-m += trace_runqlat.o 2 | 3 | KERNELDIR := /lib/modules/$(shell uname -r)/build 4 | PWD := $(shell pwd) 5 | all: 6 | $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 7 | 8 | clean: 9 | rm -rf *.ko *.mod *.mod.c *.o modules.* Module.symvers 10 | 11 | install: 12 | insmod trace_runqlat.ko 13 | 14 | remove: 15 | rmmod trace_runqlat 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Trace-runqlat 2 | 3 | Trace_irqoffv工具可以帮助我们排查由于中断被关闭导致的延迟抖动问题。但是这只是第一阶段可能遇到的问题。当我们将一个线程加入runqueue后,如果系统的负载很高,在runqueue中排在你前面的进程可能会由于执行很长时间(或许你会好奇怎么可能执行很长时间,难道不会抢占吗?还真可能出现这种情况。因为我们的kernel是不开抢占)导致最终runqueue latency很大。针对这种问题排查,我们开发一款新的排查工具**Run queue latency tracer。简称:trace-runqlat。**作用是记录被跟踪的进程在加入runqueue后,前面的每个线程执行的时间以及线程command等信息。 4 | 5 | ## 如何安装 6 | 7 | 安装 trace_runqlat 工具很简单,git clone代码后执行如下命令即可安装。 8 | 9 | ```bash 10 | make -j8 11 | make install 12 | ``` 13 | 14 | ## 如何使用 15 | 16 | 安装 trace-runqlat工具成功后。会创建如下 **/proc/trace_runqlat** 目录。 17 | 18 | ```bash 19 | ls /proc/trace_runqlat 20 | distribution pid runqlat threshold 21 | ``` 22 | 23 | /proc/trace_runqlat 目录下存在 4 个文件,分别:distribution, pid, runqlat和threshold。工具安装后,默认是打开状态,但是并没有trace任何线程,我们需要手动设置需要trace的线程pid。 24 | 25 | ##### 1. 跟踪特定pid线程 26 | 27 | ``` 28 | echo $pid > /proc/trace_runqlat/pid 29 | ``` 30 | 31 | ##### 2. 关闭跟踪 32 | 33 | ``` 34 | echo -1 > /proc/trace_runqlat/pid 35 | ``` 36 | 37 | ##### 3. 设置阈值 38 | 39 | trace-runqlat只会针对延迟超过阈值的情况,记录runqueue前面的线程执行的时间情况。为了更高效的运作,我们有必要设定一个合理阈值。例如设置60ms的阈值: 40 | 41 | ``` 42 | echo 60000000 > /proc/trace_runqlat/threshold # 单位ns 43 | ``` 44 | 45 | 如果需要查看当前设定的阈值,可执行如下命令: 46 | 47 | ``` 48 | cat /proc/trace_runqlat/threshold 49 | ``` 50 | 51 | ##### 4. 查看线程runqueue latency分布 52 | 53 | 我们以直方图的形式展示被跟踪进程runqueue latency的分布情况。 54 | 55 | ``` 56 | cat /proc/trace_runqlat/distribution 57 | ``` 58 | 59 | 你看到的信息展示如同下面这样: 60 | 61 | ``` 62 | msecs : count distribution 63 | 1 -> 1 : 0 | | 64 | 2 -> 3 : 0 | | 65 | 4 -> 7 : 0 | | 66 | 8 -> 15 : 0 | | 67 | 16 -> 31 : 0 | | 68 | 32 -> 63 : 68 |****************************************| 69 | ``` 70 | 71 | 我们可以看到latency集中在[32, 63]毫秒,次数68次。 72 | 73 | ##### 5. 是谁在runqueue的前面 74 | 75 | 针对latency超过阈值的情况,我们会记录runqueue前面的线程执行情况。 76 | 77 | Note: 必须关闭trace的情况下才能查看该文件信息。 78 | 79 | ``` 80 | cat /proc/trace_runqlat/runqlat 81 | latency(us): 35999 runqlen: 10 82 | COMM: loop7 PID: 3789453 RUNTIME(us): 4000 83 | COMM: loop2 PID: 3789448 RUNTIME(us): 4001 84 | COMM: loop4 PID: 3789450 RUNTIME(us): 3998 85 | COMM: loop1 PID: 3789447 RUNTIME(us): 3999 86 | COMM: loop3 PID: 3789449 RUNTIME(us): 4000 87 | COMM: loop8 PID: 3789454 RUNTIME(us): 3999 88 | COMM: loop9 PID: 3789455 RUNTIME(us): 4001 89 | COMM: kworker/0:2 PID: 3621088 RUNTIME(us): 3 90 | COMM: loop5 PID: 3789451 RUNTIME(us): 3994 91 | COMM: loop6 PID: 3789452 RUNTIME(us): 3999 92 | ``` 93 | 94 | 我们可以看到runqueue latency是35999us。runqueue前面有10个进程。每个进程的执行时间加在一起差不多就是总的latency。 95 | -------------------------------------------------------------------------------- /trace_runqlat.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | /* 3 | * Trace Run Queue Latency 4 | * 5 | * Copyright (C) 2020 Bytedance, Inc., Muchun Song 6 | * 7 | * The main authors of the trace qunqueue latency code are: 8 | * 9 | * Muchun Song 10 | */ 11 | #define pr_fmt(fmt) "runqlat: " fmt 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0) 28 | #include 29 | #else 30 | #include 31 | #include 32 | #endif 33 | 34 | #define MAX_TRACE_ENTRIES 128 35 | #define PER_TRACE_ENTRY_TASKS 16 36 | #define MAX_TRACE_ENTRY_TASKS \ 37 | (MAX_TRACE_ENTRIES * PER_TRACE_ENTRY_TASKS) 38 | 39 | #define THRESHOLD_DEFAULT (20 * 1000 * 1000UL) 40 | 41 | #define INVALID_PID -1 42 | #define INVALID_CPU -1 43 | #define PROBE_TRACEPOINTS 4 44 | 45 | #define LATENCY_HISTOGRAM_ENTRY 12 46 | 47 | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 6, 0) 48 | #define DEFINE_PROC_ATTRIBUTE(name, __write) \ 49 | static int name##_open(struct inode *inode, struct file *file) \ 50 | { \ 51 | return single_open(file, name##_show, PDE_DATA(inode)); \ 52 | } \ 53 | \ 54 | static const struct file_operations name##_fops = { \ 55 | .owner = THIS_MODULE, \ 56 | .open = name##_open, \ 57 | .read = seq_read, \ 58 | .write = __write, \ 59 | .llseek = seq_lseek, \ 60 | .release = single_release, \ 61 | } 62 | #else 63 | #define DEFINE_PROC_ATTRIBUTE(name, __write) \ 64 | static int name##_open(struct inode *inode, struct file *file) \ 65 | { \ 66 | return single_open(file, name##_show, PDE_DATA(inode)); \ 67 | } \ 68 | \ 69 | static const struct proc_ops name##_fops = { \ 70 | .proc_open = name##_open, \ 71 | .proc_read = seq_read, \ 72 | .proc_write = __write, \ 73 | .proc_lseek = seq_lseek, \ 74 | .proc_release = single_release, \ 75 | } 76 | #endif /* LINUX_VERSION_CODE */ 77 | 78 | #define DEFINE_PROC_ATTRIBUTE_RW(name) \ 79 | static ssize_t name##_write(struct file *file, \ 80 | const char __user *buf, \ 81 | size_t count, loff_t *ppos) \ 82 | { \ 83 | return name##_store(PDE_DATA(file_inode(file)), buf, \ 84 | count); \ 85 | } \ 86 | DEFINE_PROC_ATTRIBUTE(name, name##_write) 87 | 88 | #define DEFINE_PROC_ATTRIBUTE_RO(name) \ 89 | DEFINE_PROC_ATTRIBUTE(name, NULL) 90 | 91 | /** 92 | * If we call register_trace_sched_{wakeup,wakeup_new,switch,migrate_task}() 93 | * directly in a kernel module, the compiler will complain about undefined 94 | * symbol of __tracepoint_sched_{wakeup, wakeup_new, switch, migrate_task} 95 | * because the kernel do not export the tracepoint symbol. Here is a workaround 96 | * via for_each_kernel_tracepoint() to lookup the tracepoint and save. 97 | */ 98 | struct tracepoints_probe { 99 | struct tracepoint *tps[PROBE_TRACEPOINTS]; 100 | const char *tp_names[PROBE_TRACEPOINTS]; 101 | void *tp_probes[PROBE_TRACEPOINTS]; 102 | void *priv; 103 | int num_initalized; 104 | }; 105 | 106 | struct task_entry { 107 | u64 runtime; 108 | pid_t pid; 109 | char comm[TASK_COMM_LEN]; 110 | }; 111 | 112 | struct trace_entry { 113 | u64 latency; 114 | unsigned int nr_tasks; 115 | struct task_entry *entries; 116 | }; 117 | 118 | struct runqlat_info { 119 | int cpu; /* The target CPU */ 120 | pid_t pid; /* Trace this pid only */ 121 | u64 rq_start; 122 | u64 run_start; 123 | u64 threshold; 124 | struct task_struct *curr; 125 | unsigned long latency_hist[LATENCY_HISTOGRAM_ENTRY]; 126 | 127 | unsigned int nr_trace; 128 | struct trace_entry *trace_entries; 129 | 130 | unsigned int nr_task; 131 | struct task_entry *task_entries; 132 | 133 | arch_spinlock_t lock; 134 | }; 135 | 136 | static struct runqlat_info runqlat_info = { 137 | .pid = INVALID_PID, 138 | .cpu = INVALID_CPU, 139 | .threshold = THRESHOLD_DEFAULT, 140 | .lock = __ARCH_SPIN_LOCK_UNLOCKED, 141 | }; 142 | 143 | static void probe_sched_wakeup(void *priv, struct task_struct *p) 144 | { 145 | struct runqlat_info *info = priv; 146 | 147 | if (p->pid != info->pid) 148 | return; 149 | 150 | /* interrupts should be off from try_to_wake_up() */ 151 | arch_spin_lock(&info->lock); 152 | if (unlikely(p->pid != info->pid)) { 153 | arch_spin_unlock(&info->lock); 154 | return; 155 | } 156 | 157 | info->rq_start = local_clock(); 158 | info->run_start = info->rq_start; 159 | info->cpu = task_cpu(p); 160 | arch_spin_unlock(&info->lock); 161 | } 162 | 163 | static inline void runqlat_info_reset(struct runqlat_info *info) 164 | { 165 | info->rq_start = 0; 166 | info->run_start = 0; 167 | info->cpu = INVALID_CPU; 168 | info->curr = NULL; 169 | } 170 | 171 | /* Must be called with @info->lock held */ 172 | static void record_task(struct runqlat_info *info, struct task_struct *p, 173 | u64 runtime) 174 | __must_hold(&info->lock) 175 | { 176 | struct task_entry *task; 177 | struct trace_entry *trace; 178 | 179 | task = info->task_entries + info->nr_task; 180 | trace = info->trace_entries + info->nr_trace; 181 | 182 | if (trace->nr_tasks == 0) 183 | trace->entries = task; 184 | WARN_ON_ONCE(trace->entries != task - trace->nr_tasks); 185 | trace->nr_tasks++; 186 | 187 | task->pid = p->pid; 188 | task->runtime = runtime; 189 | strncpy(task->comm, p->comm, TASK_COMM_LEN); 190 | 191 | info->nr_task++; 192 | if (unlikely(info->nr_task >= MAX_TRACE_ENTRY_TASKS)) { 193 | pr_info("BUG: MAX_TRACE_ENTRY_TASKS too low!"); 194 | runqlat_info_reset(info); 195 | /* Force disable trace */ 196 | info->pid = INVALID_PID; 197 | } 198 | } 199 | 200 | static inline void latency_histogram_store(struct runqlat_info *info, u64 delta) 201 | { 202 | int index = -1; 203 | 204 | delta /= 1000000UL; 205 | do { 206 | index++; 207 | delta >>= 1; 208 | } while (delta > 0); 209 | 210 | if (unlikely(index >= LATENCY_HISTOGRAM_ENTRY)) 211 | index = LATENCY_HISTOGRAM_ENTRY - 1; 212 | 213 | info->latency_hist[index]++; 214 | } 215 | 216 | /* Must be called with @info->lock held */ 217 | static bool record_task_commit(struct runqlat_info *info, u64 latency) 218 | __must_hold(&info->lock) 219 | { 220 | struct trace_entry *trace; 221 | 222 | latency_histogram_store(info, latency); 223 | trace = info->trace_entries + info->nr_trace; 224 | if (trace->nr_tasks == 0) 225 | return false; 226 | 227 | if (latency >= info->threshold) { 228 | trace->latency = latency; 229 | info->nr_trace++; 230 | if (unlikely(info->nr_trace >= MAX_TRACE_ENTRIES)) { 231 | pr_info("BUG: MAX_TRACE_ENTRIES too low!"); 232 | runqlat_info_reset(info); 233 | /* Force disable trace */ 234 | info->pid = INVALID_PID; 235 | } 236 | } else { 237 | info->nr_task -= trace->nr_tasks; 238 | trace->nr_tasks = 0; 239 | trace->entries = NULL; 240 | } 241 | 242 | return true; 243 | } 244 | 245 | /* interrupts should be off from __schedule() */ 246 | static void probe_sched_switch(void *priv, bool preempt, 247 | struct task_struct *prev, 248 | struct task_struct *next) 249 | { 250 | struct runqlat_info *info = priv; 251 | int cpu = smp_processor_id(); 252 | arch_spinlock_t *lock = &info->lock; 253 | 254 | if (info->pid == INVALID_PID) 255 | return; 256 | 257 | if (info->cpu != INVALID_CPU && info->cpu != cpu) 258 | return; 259 | 260 | if (READ_ONCE(info->cpu) == INVALID_CPU) { 261 | if (READ_ONCE(info->pid) != prev->pid || 262 | prev->state != TASK_RUNNING) 263 | return; 264 | 265 | arch_spin_lock(lock); 266 | /* We could race with grabbing lock */ 267 | if (unlikely(info->cpu != INVALID_CPU || 268 | info->pid != prev->pid)) { 269 | arch_spin_unlock(lock); 270 | return; 271 | } 272 | info->rq_start = cpu_clock(cpu); 273 | info->run_start = info->rq_start; 274 | info->cpu = task_cpu(prev); 275 | 276 | /* update curr for migrate task probe using*/ 277 | if (!is_idle_task(next)) 278 | info->curr = next; 279 | arch_spin_unlock(lock); 280 | } else { 281 | u64 now; 282 | 283 | if (unlikely(READ_ONCE(info->cpu) != cpu || 284 | READ_ONCE(info->pid) == INVALID_PID)) 285 | return; 286 | 287 | arch_spin_lock(lock); 288 | /* We could race with grabbing lock */ 289 | if (unlikely(info->cpu != cpu || info->pid == INVALID_PID)) { 290 | arch_spin_unlock(lock); 291 | return; 292 | } 293 | 294 | /* update curr for migrate task probe using*/ 295 | if (!is_idle_task(next)) 296 | info->curr = next; 297 | 298 | now = cpu_clock(cpu); 299 | if (info->pid == next->pid) { 300 | if (info->run_start) 301 | record_task(info, prev, now - info->run_start); 302 | record_task_commit(info, now - info->rq_start); 303 | } else if (info->pid == prev->pid) { 304 | if (prev->state == TASK_RUNNING) { 305 | info->rq_start = now; 306 | info->run_start = now; 307 | } else { 308 | runqlat_info_reset(info); 309 | } 310 | } else { 311 | if (info->run_start) 312 | record_task(info, prev, now - info->run_start); 313 | info->run_start = now; 314 | } 315 | arch_spin_unlock(lock); 316 | } 317 | } 318 | 319 | static void probe_sched_migrate_task(void *priv, struct task_struct *p, int cpu) 320 | { 321 | u64 now; 322 | struct runqlat_info *info = priv; 323 | struct task_struct *curr; 324 | 325 | if (p->pid != info->pid || info->cpu == INVALID_CPU) 326 | return; 327 | 328 | /* interrupts should be off from set_task_cpu() */ 329 | arch_spin_lock(&info->lock); 330 | if (unlikely(p->pid != info->pid || info->cpu == INVALID_CPU)) 331 | goto unlock; 332 | 333 | now = local_clock(); 334 | curr = info->curr; 335 | if (curr) { 336 | get_task_struct(curr); 337 | if (info->run_start) 338 | record_task(info, curr, now - info->run_start); 339 | put_task_struct(curr); 340 | } 341 | 342 | info->cpu = cpu; 343 | info->run_start = now; 344 | unlock: 345 | arch_spin_unlock(&info->lock); 346 | } 347 | 348 | static struct tracepoints_probe tps_probe = { 349 | .tp_names = { 350 | "sched_wakeup", 351 | "sched_wakeup_new", 352 | "sched_switch", 353 | "sched_migrate_task", 354 | }, 355 | .tp_probes = { 356 | probe_sched_wakeup, 357 | probe_sched_wakeup, 358 | probe_sched_switch, 359 | probe_sched_migrate_task, 360 | }, 361 | .priv = &runqlat_info, 362 | }; 363 | 364 | static inline bool is_tracepoint_lookup_success(struct tracepoints_probe *tps) 365 | { 366 | return tps->num_initalized == PROBE_TRACEPOINTS; 367 | } 368 | 369 | static void __init tracepoint_lookup(struct tracepoint *tp, void *priv) 370 | { 371 | int i; 372 | struct tracepoints_probe *tps = priv; 373 | 374 | if (is_tracepoint_lookup_success(tps)) 375 | return; 376 | 377 | for (i = 0; i < ARRAY_SIZE(tps->tp_names); i++) { 378 | if (tps->tps[i] || strcmp(tp->name, tps->tp_names[i])) 379 | continue; 380 | tps->tps[i] = tp; 381 | tps->num_initalized++; 382 | } 383 | } 384 | 385 | static int trace_pid_show(struct seq_file *m, void *ptr) 386 | { 387 | struct runqlat_info *info = m->private; 388 | 389 | seq_printf(m, "%d\n", info->pid); 390 | 391 | return 0; 392 | } 393 | 394 | static ssize_t trace_pid_store(void *priv, const char __user *buf, size_t count) 395 | { 396 | int pid; 397 | struct runqlat_info *info = priv; 398 | 399 | if (kstrtoint_from_user(buf, count, 0, &pid)) 400 | return -EINVAL; 401 | 402 | if (info->pid != INVALID_PID && pid != INVALID_PID) 403 | return -EPERM; 404 | 405 | local_irq_disable(); 406 | arch_spin_lock(&info->lock); 407 | if (info->pid == pid) 408 | goto unlock; 409 | 410 | if (pid != INVALID_PID) { 411 | int i; 412 | 413 | info->nr_trace = 0; 414 | info->nr_task = 0; 415 | memset(info->trace_entries, 0, 416 | MAX_TRACE_ENTRIES * sizeof(struct trace_entry) + 417 | MAX_TRACE_ENTRY_TASKS * sizeof(struct task_entry)); 418 | 419 | for (i = 0; i < LATENCY_HISTOGRAM_ENTRY; i++) 420 | info->latency_hist[i] = 0; 421 | } 422 | runqlat_info_reset(info); 423 | smp_wmb(); 424 | info->pid = pid; 425 | unlock: 426 | arch_spin_unlock(&info->lock); 427 | local_irq_enable(); 428 | 429 | return count; 430 | } 431 | 432 | DEFINE_PROC_ATTRIBUTE_RW(trace_pid); 433 | 434 | static int threshold_show(struct seq_file *m, void *ptr) 435 | { 436 | struct runqlat_info *info = m->private; 437 | 438 | seq_printf(m, "%llu\n", info->threshold); 439 | 440 | return 0; 441 | } 442 | 443 | static ssize_t threshold_store(void *priv, const char __user *buf, size_t count) 444 | { 445 | unsigned long threshold; 446 | struct runqlat_info *info = priv; 447 | 448 | if (kstrtoul_from_user(buf, count, 0, &threshold)) 449 | return -EINVAL; 450 | 451 | info->threshold = threshold; 452 | 453 | return count; 454 | } 455 | 456 | DEFINE_PROC_ATTRIBUTE_RW(threshold); 457 | 458 | static int runqlat_show(struct seq_file *m, void *ptr) 459 | { 460 | int i, j; 461 | struct runqlat_info *info = m->private; 462 | 463 | if (info->pid != INVALID_CPU) 464 | return -EPERM; 465 | 466 | local_irq_disable(); 467 | arch_spin_lock(&info->lock); 468 | for (i = 0; i < info->nr_trace; i++) { 469 | struct trace_entry *entry = info->trace_entries + i; 470 | 471 | seq_printf(m, "%*clatency(us): %llu runqlen: %d\n", 2, ' ', 472 | entry->latency / 1000, entry->nr_tasks); 473 | 474 | for (j = 0; j < entry->nr_tasks; j++) { 475 | struct task_entry *task = entry->entries + j; 476 | 477 | seq_printf(m, "%*cCOMM: %-16s PID: %-8d RUNTIME(us): %6llu\n", 478 | 6, ' ', task->comm, task->pid, 479 | task->runtime / 1000); 480 | } 481 | 482 | seq_putc(m, '\n'); 483 | } 484 | 485 | arch_spin_unlock(&info->lock); 486 | local_irq_enable(); 487 | 488 | return 0; 489 | } 490 | 491 | DEFINE_PROC_ATTRIBUTE_RO(runqlat); 492 | 493 | #define NUMBER_CHARACTER 40 494 | 495 | static bool histogram_show(struct seq_file *m, const char *header, 496 | const unsigned long *hist, unsigned long size, 497 | unsigned int factor) 498 | { 499 | int i, zero_index = 0; 500 | unsigned long count_max = 0; 501 | 502 | for (i = 0; i < size; i++) { 503 | unsigned long count = hist[i]; 504 | 505 | if (count > count_max) 506 | count_max = count; 507 | 508 | if (count) 509 | zero_index = i + 1; 510 | } 511 | if (count_max == 0) 512 | return false; 513 | 514 | /* print header */ 515 | if (header) 516 | seq_printf(m, "%s\n", header); 517 | seq_printf(m, "%*c%s%*c : %-9s %s\n", 9, ' ', "msecs", 10, ' ', "count", 518 | "distribution"); 519 | 520 | for (i = 0; i < zero_index; i++) { 521 | int num; 522 | int scale_min, scale_max; 523 | char str[NUMBER_CHARACTER + 1]; 524 | 525 | scale_max = 2 << i; 526 | scale_min = unlikely(i == 0) ? 1 : scale_max / 2; 527 | 528 | num = hist[i] * NUMBER_CHARACTER / count_max; 529 | memset(str, '*', num); 530 | memset(str + num, ' ', NUMBER_CHARACTER - num); 531 | str[NUMBER_CHARACTER] = '\0'; 532 | 533 | seq_printf(m, "%10d -> %-10d : %-8lu |%s|\n", 534 | scale_min * factor, scale_max * factor - 1, 535 | hist[i], str); 536 | } 537 | 538 | return true; 539 | } 540 | 541 | static int distribution_show(struct seq_file *m, void *ptr) 542 | { 543 | int i; 544 | unsigned long hist[LATENCY_HISTOGRAM_ENTRY]; 545 | struct runqlat_info *info = m->private; 546 | 547 | for (i = 0; i < LATENCY_HISTOGRAM_ENTRY; i++) 548 | hist[i] = info->latency_hist[i]; 549 | 550 | histogram_show(m, NULL, hist, LATENCY_HISTOGRAM_ENTRY, 1); 551 | 552 | return 0; 553 | } 554 | 555 | DEFINE_PROC_ATTRIBUTE_RO(distribution); 556 | 557 | static int __init trace_runqlat_init(void) 558 | { 559 | int i; 560 | void *buf; 561 | int ret = -ENOMEM; 562 | struct tracepoints_probe *tps = &tps_probe; 563 | struct proc_dir_entry *parent_dir; 564 | struct runqlat_info *info = &runqlat_info; 565 | 566 | buf = vzalloc(MAX_TRACE_ENTRIES * sizeof(struct trace_entry) + 567 | MAX_TRACE_ENTRY_TASKS * sizeof(struct task_entry)); 568 | if (!buf) 569 | return -ENOMEM; 570 | info->trace_entries = buf; 571 | info->task_entries = (void *)(info->trace_entries + MAX_TRACE_ENTRIES); 572 | 573 | parent_dir = proc_mkdir("trace_runqlat", NULL); 574 | if (!parent_dir) 575 | goto free_buf; 576 | 577 | if (!proc_create_data("pid", 0644, parent_dir, &trace_pid_fops, info)) 578 | goto remove_proc; 579 | 580 | if (!proc_create_data("threshold", 0644, parent_dir, &threshold_fops, 581 | info)) 582 | goto remove_proc; 583 | 584 | if (!proc_create_data("runqlat", 0, parent_dir, &runqlat_fops, info)) 585 | goto remove_proc; 586 | 587 | if (!proc_create_data("distribution", 0, parent_dir, &distribution_fops, 588 | info)) 589 | goto remove_proc; 590 | 591 | /* Lookup for the tracepoint that we needed */ 592 | for_each_kernel_tracepoint(tracepoint_lookup, tps); 593 | 594 | if (!is_tracepoint_lookup_success(tps)) 595 | goto remove_proc; 596 | 597 | for (i = 0; i < PROBE_TRACEPOINTS; i++) { 598 | ret = tracepoint_probe_register(tps->tps[i], tps->tp_probes[i], 599 | tps->priv); 600 | if (ret) { 601 | pr_err("sched trace: can not activate tracepoint " 602 | "probe to %s\n", tps->tp_names[i]); 603 | while (i--) 604 | tracepoint_probe_unregister(tps->tps[i], 605 | tps->tp_probes[i], 606 | tps->priv); 607 | goto remove_proc; 608 | } 609 | } 610 | 611 | return 0; 612 | remove_proc: 613 | remove_proc_subtree("trace_runqlat", NULL); 614 | free_buf: 615 | vfree(buf); 616 | 617 | return ret; 618 | } 619 | 620 | static void __exit trace_runqlat_exit(void) 621 | { 622 | int i; 623 | struct tracepoints_probe *tps = &tps_probe; 624 | struct runqlat_info *info = &runqlat_info; 625 | 626 | for (i = 0; i < PROBE_TRACEPOINTS; i++) 627 | tracepoint_probe_unregister(tps->tps[i], tps->tp_probes[i], 628 | tps->priv); 629 | 630 | tracepoint_synchronize_unregister(); 631 | remove_proc_subtree("trace_runqlat", NULL); 632 | vfree(info->trace_entries); 633 | } 634 | 635 | module_init(trace_runqlat_init); 636 | module_exit(trace_runqlat_exit); 637 | MODULE_LICENSE("GPL v2"); 638 | MODULE_AUTHOR("Muchun Song "); 639 | --------------------------------------------------------------------------------