从一个CFS调度案例谈Linux系统卡顿的根源( 二 )

编译加载并创建字符设备:
[root@localhost test]# insmod ./wakedev.ko[root@localhost test]# dmesg |grep major.*minor[68.385310] major = 248 minor = 0[root@localhost test]# mknod /dev/test c 248 0OK , 接下来是生产者程序的代码:
// producer.c#include #include #include #include #include int main(int argc, char **argv){ int fd, i = 0xfff, j; int pid = -1; pid = atoi(argv[1]); fd = open("/dev/test", O_RDWR); perror("open"); while(1 || i --) {j = 0xfffff;ioctl(fd, 122,// 唤醒consumer进程while(j--) {} // 模拟生产过程! }}需要C/C++ Linux服务器架构师学习资料私信“资料”(资料包括C/C++ , Linux , golang技术 , Nginx , ZeroMQ , MySQL , Redis , fastdfs , MongoDB , ZK , 流媒体 , CDN , P2P , K8S , Docker , TCP/IP , 协程 , DPDK , ffmpeg等) , 免费分享
从一个CFS调度案例谈Linux系统卡顿的根源文章插图
接下来是消费者程序:
#include int main(){ while(1) {sleep(1); }}编译运行之:
# 名字很长是因为日志里显眼![root@localhost test]# gcc consumer.c -O0 -o consumerAconsumerACconsumerAconsumer[root@localhost test]# gcc producer.c -O0 -o producerAproducerAproducerAproducer# 启动消费者[root@localhost test]# ./consumerAconsumerACconsumerAconsumer--tt-darkmode-color: #999999;">差不多就是以上的方式进行实验 , 试了多次 , 没有发现卡顿现象 。
比较失望 , 但是我想这是必然的 , 因为如果问题真的如此容易复现的话 , 社区早就fix了 , 可见 , 这里并没有显而易见的问题需要解决 。 可能只是使用系统的方式不当 。
不管怎样 , 还是先看数据吧 , 从数据里分析细节 。
首先 , 在实验进行的情况下 , 先用perf导出进程切换的过程:
[root@localhost test]# perf record -e sched:sched_switch -e sched:sched_wakeup -a -- sleep 5[ perf record: Woken up 1 times to write data ][ perf record: Captured and wrote 0.303 MB perf.data (1303 samples) ][root@localhost test]# perf script -i perf.data > final.data下面是一个片段 , 我捕获了大概10次捕获到的一个片段:
loop_sleep6642 [000] 23085.198476: sched:sched_switch: loop_sleep:6642 [120] R ==> xfsaild/dm-0:400 [120]xfsaild/dm-0400 [000] 23085.198482: sched:sched_switch: xfsaild/dm-0:400 [120] S ==> loop_sleep:6642 [120]loop_sleep6642 [000] 23085.217752: sched:sched_switch: loop_sleep:6642 [120] R ==> producerAproduc:26285 [130]## 从这里开始霸屏 producerAproduc 26285 [000] 23085.220257: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.220259: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.220273: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.269921: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.269923: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.269927: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.292748: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.292749: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.292752: sched:sched_switch: consumerAconsum:26274 [120] producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.320205: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.320208: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.320212: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.340971: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.340973: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.340977: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.369630: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.369632: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.369637: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.400818: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.400821: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.400825: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.426043: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.426045: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.426048: sched:sched_switch: consumerAconsum:26274 [120] S ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.447646: sched:sched_wakeup: xfsaild/dm-0:400 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.447649: sched:sched_switch: producerAproduc:26285 [130] R ==> xfsaild/dm-0:400 [120]## 实在太久了 , R状态让位! ## 到这里才结束!!!xfsaild/dm-0400 [000] 23085.447654: sched:sched_switch: xfsaild/dm-0:400 [120] S ==> loop_sleep:6642 [120]loop_sleep6642 [000] 23085.468047: sched:sched_switch: loop_sleep:6642 [120] R ==> producerAproduc:26285 [130] producerAproduc 26285 [000] 23085.469862: sched:sched_wakeup: consumerAconsum:26274 [120] success=1 CPU:000 producerAproduc 26285 [000] 23085.469863: sched:sched_switch: producerAproduc:26285 [130] R ==> consumerAconsum:26274 [120] consumerAconsum 26274 [000] 23085.469867: sched:sched_switch: consumerAconsum:26274 [120] S ==> loop_sleep:6642 [120]loop_sleep6642 [000] 23085.488800: sched:sched_switch: loop_sleep:6642 [120] R ==> producerAproduc:26285 [130]