Linux实时性能测试工具之cyclictest使用详解

 更新时间:2026年06月05日 09:28:24   作者:李71~李先森  
这段文章主要介绍了cyclictest工具在Linux系统中的安装、运行及参数设置,重点阐述了其测试内核实时性能的方法,包括创建高精度定时线程和测量唤醒延迟,并强调了不同参数对测试结果的影响

cyclictest 是 Linux 系统中 rt-tests 下的用于测试内核实时性能的常用工具,通过创建高精度定时线程并测量其唤醒延迟,评估系统的中断响应、调度延迟等实时特性。

cyclictest 安装

下面是 cyclictest 的安装方法,供参考:

# 下载cyclictest原码
git clone git://git.kernel.org/pub/scm/utils/rt-tests/rt-tests.git

# 切换branch stable/v1.0
git checkout stable/v1.0

# 编译
make

cyclictest 计算延时原理

cyclictest计算延时的原理是通过记录进程睡眠的开始时间、实际睡眠时间以及真正唤醒并运行的时间,用实际唤醒时间减去实际睡眠时间和睡眠开始时间来得出延时。

  • 程序会记录进程睡眠的开始时间 t1
  • 进程实际睡眠时间 t2
  • 进程 真正唤醒并运行的时间为 t3
  • 延时结果即为 t3 - t2 - t1

cyclictest 运行

编译完成后可以在运行cyclictest, 例如:

sudo ./cyclictest -m -n -p95 -d0 -a 1-17 -t 17

查看参数可以使用:

./cyclictest -h

./cyclictest: option requires an argument -- 'h'
cyclictest V 1.00
Usage:
cyclictest <options>

-a [CPUSET] --affinity     Run thread #N on processor #N, if possible, or if CPUSET
                           given, pin threads to that set of processors in round-
                           robin order.  E.g. -a 2 pins all threads to CPU 2,
                           but -a 3-5,0 -t 5 will run the first and fifth
                           threads on CPU (0),thread #2 on CPU 3, thread #3
                           on CPU 4, and thread #5 on CPU 5.
-A USEC  --aligned=USEC    align thread wakeups to a specific offset
-b USEC  --breaktrace=USEC send break trace command when latency > USEC
-B       --preemptirqs     both preempt and irqsoff tracing (used with -b)
-c CLOCK --clock=CLOCK     select clock
                           0 = CLOCK_MONOTONIC (default)
                           1 = CLOCK_REALTIME
-C       --context         context switch tracing (used with -b)
-d DIST  --distance=DIST   distance of thread intervals in us, default=500
-D       --duration=TIME   specify a length for the test run.
                           Append 'm', 'h', or 'd' to specify minutes, hours or days.
         --latency=PM_QOS  write PM_QOS to /dev/cpu_dma_latency
-E       --event           event tracing (used with -b)
-f       --ftrace          function trace (when -b is active)
-F       --fifo=<path>     create a named pipe at path and write stats to it
-h       --histogram=US    dump a latency histogram to stdout after the run
                           US is the max latency time to be be tracked in microseconds
                           This option runs all threads at the same priority.
-H       --histofall=US    same as -h except with an additional summary column
         --histfile=<path> dump the latency histogram to <path> instead of stdout
-i INTV  --interval=INTV   base interval of thread in us default=1000
-I       --irqsoff         Irqsoff tracing (used with -b)
-l LOOPS --loops=LOOPS     number of loops: default=0(endless)
         --laptop          Save battery when running cyclictest
                           This will give you poorer realtime results
                           but will not drain your battery so quickly
-m       --mlockall        lock current and future memory allocations
-M       --refresh_on_max  delay updating the screen until a new max
                           latency is hit. Userful for low bandwidth.
-n       --nanosleep       use clock_nanosleep
         --notrace         suppress tracing
-N       --nsecs           print results in ns instead of us (default us)
-o RED   --oscope=RED      oscilloscope mode, reduce verbose output by RED
-O TOPT  --traceopt=TOPT   trace option
-p PRIO  --priority=PRIO   priority of highest prio thread
-P       --preemptoff      Preempt off tracing (used with -b)
         --policy=NAME     policy of measurement thread, where NAME may be one
                           of: other, normal, batch, idle, fifo or rr.
         --priospread      spread priority levels starting at specified value
-q       --quiet           print a summary only on exit
-r       --relative        use relative timer instead of absolute
-R       --resolution      check clock resolution, calling clock_gettime() many
                           times.  List of clock_gettime() values will be
                           reported with -X
         --secaligned [USEC] align thread wakeups to the next full second
                           and apply the optional offset
-s       --system          use sys_nanosleep and sys_setitimer
-S       --smp             Standard SMP testing: options -a -t -n and
                           same priority of all threads
        --spike=<trigger>  record all spikes > trigger
        --spike-nodes=[num of nodes]
                           These are the maximum number of spikes we can record.
                           The default is 1024 if not specified
         --smi             Enable SMI counting
-t       --threads         one thread per available processor
-t [NUM] --threads=NUM     number of threads:
                           without NUM, threads = max_cpus
                           without -t default = 1
         --tracemark       write a trace mark when -b latency is exceeded
-T TRACE --tracer=TRACER   set tracing function
    configured tracers: none
-u       --unbuffered      force unbuffered output for live processing
-U       --numa            Standard NUMA testing (similar to SMP option)
                           thread data structures allocated from local node
-v       --verbose         output values on stdout for statistics
                           format: n:c:v n=tasknum c=count v=value in us
-w       --wakeup          task wakeup tracing (used with -b)
-W       --wakeuprt        rt task wakeup tracing (used with -b)
         --dbg_cyclictest  print info useful for debugging cyclictest

简单介绍下一些参数:

  • -m 锁定进程内存,防止页交换影响实时性,高精度测试时避免内存抖动。
  • -n 使用 clock_nanosleep 替代 nanosleep,提高时间精度。
  • -p 设置线程优先级(需 root 权限)。(Linux 中 SCHED_FIFO 优先级范围通常为 1-99)
  • d0 CPU上有多个实时线程时,每个线程唤醒时间的间隔增量。当设置为0时,意味着所有线程将在同一时间被唤醒。
  • -a 绑定线程到指定 CPU 核心,例如1-17 绑定到 CPU 1 到 17。
  • -t 指定测试线程数量。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Lamp环境下设置绑定apache域名的方法分析

    Lamp环境下设置绑定apache域名的方法分析

    这篇文章主要介绍了Lamp环境下设置绑定apache域名的方法,简单分析了Lamp环境下的Apache域名绑定设置相关属性、功能与操作技巧,需要的朋友可以参考下
    2018-03-03
  • CentOS7 防火墙firewall的操作命令大全

    CentOS7 防火墙firewall的操作命令大全

    大家好,本篇文章主要讲的是CentOS7 防火墙(firewall)的操作命令大全,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下哦
    2021-11-11
  • 详解linux tail命令显示最后n行

    详解linux tail命令显示最后n行

    这篇文章给大家分享了linux tail命令显示最后n行的具体代码方法,有需要的朋友可以参考学习下。
    2018-07-07
  • Linux通过设备号找到设备的方法

    Linux通过设备号找到设备的方法

    所有的Linux设备文件均位于/dev目录下,是根(/)文件系统的一个组成部分,因为这些设备文件在操作系统启动过程中必须可以使用。接下来通过本文给大家介绍Linux通过设备号找到设备的方法,感兴趣的朋友一起看看吧
    2018-07-07
  • Ubuntu下sublime中文无法输入的处理方案

    Ubuntu下sublime中文无法输入的处理方案

    本文主要是给出了Ubuntu下sublime中文无法输入的处理方案,非常的实用,遇到相同问题的小伙伴们可以参考下
    2014-11-11
  • Linux之Centos8创建CA证书教程

    Linux之Centos8创建CA证书教程

    大家好,本篇文章主要讲的是Linux之Centos8创建CA证书教程,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • shell判断一个变量是否为空方法总结

    shell判断一个变量是否为空方法总结

    在本篇文章里小编给大家分享了关于shell判断一个变量是否为空方法总结内容,有兴趣的朋友们学习下。
    2019-01-01
  • linux软链接的创建、删除和更新方式

    linux软链接的创建、删除和更新方式

    这篇文章主要介绍了linux软链接的创建、删除和更新方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • 使用Ansible安全批量修改Linux服务器密码的详细方案

    使用Ansible安全批量修改Linux服务器密码的详细方案

    Ansible通过user模块和加密工具可实现安全、高效的密码批量更新,核心步骤包括密码加密处理、Playbook编写和安全控制,本文介绍Ansible安全批量修改Linux服务器密码的详细方案,感兴趣的朋友跟随小编一起看看吧
    2018-03-03
  • 在Ubuntu服务器上部署xinference流程

    在Ubuntu服务器上部署xinference流程

    文章主要介绍了如何拉取镜像、启动容器(特别是涉及GPU的容器)以及最终的访问步骤,并强调这些是个人经验总结,供参考
    2026-03-03

最新评论