[root@localhost kni]# ps -elf | grep kni 4 R root 32791 27964 99 80 0 - 8410299 - 05:12 pts/0 00:00:14 ./examples/kni/build/app/kni -c 0xFFFFF -n 4 -- -P -p 0x1 --config=(0,1,2,3,4) 1 S root 32813 2 0 80 0 - 0 kni_th 05:12 ? 00:00:00 [kni_vEth0_0] 1 S root 32817 2 0 80 0 - 0 kni_th 05:12 ? 00:00:00 [kni_vEth0_1]
[root@localhost kni]# ps -mo pid,tid,%cpu,psr -p 32813 PID TID %CPU PSR 32813 - 0.1 - - 32813 0.1 3 [root@localhost kni]# ps -mo pid,tid,%cpu,psr -p 32817 PID TID %CPU PSR 32817 - 0.4 - - 32817 0.4 4 [root@localhost kni]# ps -mo pid,tid,%cpu,psr -p 32791 PID TID %CPU PSR 32791 - 298 - - 32791 99.6 0 - 32792 0.0 60 - 32793 0.0 20 - 32794 99.3 1 - 32795 99.4 2 - 32796 0.0 3 - 32797 0.0 4 - 32798 0.0 5 - 32799 0.0 6 - 32800 0.0 7 - 32801 0.0 8 - 32802 0.0 9 - 32803 0.0 10 - 32804 0.0 11 - 32805 0.0 12 - 32806 0.0 13 - 32807 0.0 14 - 32808 0.0 15 - 32809 0.0 16 - 32810 0.0 17 - 32811 0.0 18 - 32812 0.0 19 [root@localhost kni]#
[root@localhost kni]# ps -T -p 32791 PID SPID TTY TIME CMD 32791 32791 pts/0 00:05:29 kni 32791 32792 pts/0 00:00:00 eal-intr-thread 32791 32793 pts/0 00:00:00 rte_mp_handle 32791 32794 pts/0 00:05:28 lcore-slave-1 32791 32795 pts/0 00:05:29 lcore-slave-2 32791 32796 pts/0 00:00:00 lcore-slave-3 32791 32797 pts/0 00:00:00 lcore-slave-4 32791 32798 pts/0 00:00:00 lcore-slave-5 32791 32799 pts/0 00:00:00 lcore-slave-6 32791 32800 pts/0 00:00:00 lcore-slave-7 32791 32801 pts/0 00:00:00 lcore-slave-8 32791 32802 pts/0 00:00:00 lcore-slave-9 32791 32803 pts/0 00:00:00 lcore-slave-10 32791 32804 pts/0 00:00:00 lcore-slave-11 32791 32805 pts/0 00:00:00 lcore-slave-12 32791 32806 pts/0 00:00:00 lcore-slave-13 32791 32807 pts/0 00:00:00 lcore-slave-14 32791 32808 pts/0 00:00:00 lcore-slave-15 32791 32809 pts/0 00:00:00 lcore-slave-16 32791 32810 pts/0 00:00:00 lcore-slave-17 32791 32811 pts/0 00:00:00 lcore-slave-18 32791 32812 pts/0 00:00:00 lcore-slave-19
[root@localhost ixgbe]# ps -Leo pid,tid,args:30,psr,comm | grep kni 31347 31347 ./examples/kni/build/app/kni - 0 kni 31347 31348 ./examples/kni/build/app/kni - 47 eal-intr-thread 31347 31349 ./examples/kni/build/app/kni - 21 rte_mp_handle 31347 31350 ./examples/kni/build/app/kni - 1 lcore-slave-1 31347 31351 ./examples/kni/build/app/kni - 2 lcore-slave-2 31347 31352 ./examples/kni/build/app/kni - 3 lcore-slave-3 31347 31353 ./examples/kni/build/app/kni - 4 lcore-slave-4 31347 31354 ./examples/kni/build/app/kni - 5 lcore-slave-5 31347 31355 ./examples/kni/build/app/kni - 6 lcore-slave-6 31347 31356 ./examples/kni/build/app/kni - 7 lcore-slave-7 31347 31357 ./examples/kni/build/app/kni - 8 lcore-slave-8 31347 31358 ./examples/kni/build/app/kni - 9 lcore-slave-9 31347 31359 ./examples/kni/build/app/kni - 10 lcore-slave-10 31347 31360 ./examples/kni/build/app/kni - 11 lcore-slave-11 31347 31361 ./examples/kni/build/app/kni - 12 lcore-slave-12 31347 31362 ./examples/kni/build/app/kni - 13 lcore-slave-13 31347 31363 ./examples/kni/build/app/kni - 14 lcore-slave-14 31347 31364 ./examples/kni/build/app/kni - 15 lcore-slave-15 31347 31365 ./examples/kni/build/app/kni - 16 lcore-slave-16 31347 31366 ./examples/kni/build/app/kni - 17 lcore-slave-17 31347 31367 ./examples/kni/build/app/kni - 18 lcore-slave-18 31347 31368 ./examples/kni/build/app/kni - 19 lcore-slave-19 31370 31370 [kni_vEth0] 125 kni_vEth0 31676 31676 grep --color=auto kni 48 grep [root@localhost ixgbe]#
root@ubuntu:~# ps H -o 'tid comm' 42464 TID COMMAND 42464 firecracker 42465 fc_api 42505 fc_vcpu 0 root@ubuntu:~# ps -T -p 42464 PID SPID TTY TIME CMD 42464 42464 pts/2 00:00:00 firecracker 42464 42465 pts/2 00:00:00 fc_api 42464 42505 pts/2 00:00:01 fc_vcpu 0 root@ubuntu:~#
2. ps 命令 -L参数显示进程,并尽量显示其LWP(线程ID)和NLWP(线程的个数)。 [root@jay-linux ~]# ps -eLf | grep qemu root 657 196 657 0 3 13:48 pts/1 00:00:00 qemu-sys -m 1024 -smp 2 root 657 196 660 3 3 13:48 pts/1 00:00:26 qemu-sys -m 1024 -smp 2 root 657 196 661 2 3 13:48 pts/1 00:00:19 qemu-sys -m 1024 -smp 2 root 789 9799 10789 0 1 14:02 pts/0 00:00:00 grep --color=auto qemu 上面命令查询结果的第二列为PID,第三列为PPID,第四列为LWP,第六列为NLWP。 另外,ps命令还可以查看线程在哪个CPU上运行,命令如下: [root@jay-linux ~]# ps -eo ruser,pid,ppid,lwp,psr,args -L | grep qemu root 657 196 657 1 qemu-sys -hda smep-temp.qcow -m 1024 -smp 2 root 657 196 660 1 qemu-sys -hda smep-temp.qcow -m 1024 -smp 2 root 657 196 661 2 qemu-sys -hda smep-temp.qcow -m 1024 -smp 2 root 834 9799 10834 1 grep --color=auto qemu 其中,每一列依次为:用户ID,进程ID,父进程ID,线程ID,运行该线程的CPU的序号,命令行参数(包括命令本身)。
root@25a725e7599e:/# ps -u "$(id -u)" -o pid,user,mntns,command --sort mntns PID USER MNTNS COMMAND 1 root 4026531840 /sbin/init 2 root 4026531840 [kthreadd] 3 root 4026531840 [rcu_gp] 4 root 4026531840 [rcu_par_gp] 5 root 4026531840 [kworker/0:0-eve] 6 root 4026531840 [kworker/0:0H] 8 root 4026531840 [mm_percpu_wq] 9 root 4026531840 [ksoftirqd/0] 10 root 4026531840 [rcu_sched] 11 root 4026531840 [migration/0] 12 root 4026531840 [cpuhp/0] 14 root 4026531840 [netns] 15 root 4026531840 [oom_reaper] 16 root 4026531840 [writeback] 17 root 4026531840 [kcompactd0] 18 root 4026531840 [kblockd] 19 root 4026531840 [blkcg_punt_bio] 20 root 4026531840 [kworker/0:1-eve] 21 root 4026531840 [kswapd0]
方法一
先用ps -mp pid或者top -H查出线程pid。
然后strace -p pid追踪其中一个线程。
方法二
直接用strace -fp pid追踪进程下所有线
[root@localhost l2fwd2-rss]# ps -T -p 29765 PID SPID TTY TIME CMD 29765 29765 pts/1 00:13:14 l2fwd2 29765 29766 pts/1 00:00:00 eal-intr-thread 29765 29767 pts/1 00:00:00 rte_mp_handle 29765 29768 pts/1 00:13:12 lcore-slave-8 29765 29769 pts/1 00:13:12 lcore-slave-9 29765 29770 pts/1 00:13:12 lcore-slave-10 [root@localhost l2fwd2-rss]# strace -p 29768 strace: Process 29768 attached ^Cstrace: Process 29768 detached [root@localhost l2fwd2-rss]# pstack 29768 Thread 1 (process 29768): #0 0x0000000000787168 in hinic_recv_pkts (rx_queue=0x13e4ba900, rx_pkts=0xffffb5ddcea0, nb_pkts=32) at /data1/dpdk-19.11/drivers/net/hinic/hinic_pmd_rx.c:956 #1 0x0000000000466c84 in rte_eth_rx_burst (nb_pkts=32, rx_pkts=0xffffb5ddcea0, queue_id=1, port_id=0) at /data1/dpdk-19.11/arm64-armv8a-linuxapp-gcc/include/rte_ethdev.h:4387 #2 main_loop (arg=<optimized out>) at /data1/dpdk-19.11/demo/l2fwd2-rss/main.c:1085 #3 0x0000000000593bc8 in eal_thread_loop (arg=0x0) at /data1/dpdk-19.11/lib/librte_eal/linux/eal/eal_thread.c:153 #4 0x0000ffffb6fe7d38 in start_thread (arg=0xffffb5ddd910) at pthread_create.c:309 #5 0x0000ffffb6f2f5f0 in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:91 [root@localhost l2fwd2-rss]# pstack 29769 Thread 1 (process 29769): #0 0x000000000078717c in hinic_recv_pkts (rx_queue=0x13e47a900, rx_pkts=0xffffb55ccea0, nb_pkts=32) at /data1/dpdk-19.11/drivers/net/hinic/hinic_pmd_rx.c:966 #1 0x0000000000466c84 in rte_eth_rx_burst (nb_pkts=32, rx_pkts=0xffffb55ccea0, queue_id=0, port_id=1) at /data1/dpdk-19.11/arm64-armv8a-linuxapp-gcc/include/rte_ethdev.h:4387 #2 main_loop (arg=<optimized out>) at /data1/dpdk-19.11/demo/l2fwd2-rss/main.c:1085 #3 0x0000000000593bc8 in eal_thread_loop (arg=0x0) at /data1/dpdk-19.11/lib/librte_eal/linux/eal/eal_thread.c:153 #4 0x0000ffffb6fe7d38 in start_thread (arg=0xffffb55cd910) at pthread_create.c:309 #5 0x0000ffffb6f2f5f0 in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:91 [root@localhost l2fwd2-rss]#