skynet空服务中, 会占用4%左右的CPU, 何解?
在空服务中, 定时器线程函数thread_timer
会有CPU消耗2.6左右(i7 3.40GHz)??? top命令后使用H键可以进入线程模式, 可以看到占用CPU的线程号, 使用pstack可以查看进程或者线程CPU调用栈.
> pstack 7019
Thread 1 (process 7019):
#0 0x00007f9595505e2d in nanosleep () from /lib64/libc.so.6
#1 0x00007f9595536704 in usleep () from /lib64/libc.so.6
#2 0x000000000040bfa9 in thread_timer (p=0x7f959504d200) at skynet-src/skynet_start.c:137
#3 0x00007f9595f23dd5 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f959553eead in clone () from /lib64/libc.so.6
使用gprofile流程, 主要是加编译选项-pg
, 重新编译后执行. 在进程退出时会在工作目录留下gmon.out文件. 使用gprofile skynet gmon.out
就会在标准输出出现统计信息.
使用自带的debug_console没有发现cpu使用异常. 他的stat命令中, message是累计处理的消息, mqlen是当前消息队列长度.
stat
:01000004 cpu:0.017451 message:27 mqlen:0 task:2
:01000005 cpu:0.018209 message:26 mqlen:0 task:1
:01000007 cpu:0.012749 message:16 mqlen:0 task:0
:01000008 cpu:0.014743 message:19 mqlen:0 task:0
:0100000a cpu:0.068492 message:47 mqlen:0 task:1
:0100000b cpu:0.015418 message:23 mqlen:0 task:0
:0100000c cpu:0.018568 message:25 mqlen:0 task:0
:0100000d cpu:0.010717 message:16 mqlen:0 task:0
:0100000e cpu:0.008361 message:16 mqlen:0 task:0
:0100000f cpu:0.007513 message:16 mqlen:0 task:0
:01000010 cpu:0.006229 message:16 mqlen:0 task:0
:01000011 cpu:0.005541 message:16 mqlen:0 task:0
:01000012 cpu:0.005137 message:16 mqlen:0 task:0
:01000013 cpu:0.004924 message:16 mqlen:0 task:0
:01000014 cpu:0.004647 message:16 mqlen:0 task:0
:01000015 cpu:0.00348 message:18 mqlen:0 task:0
:01000016 cpu:0.089404 message:256 mqlen:0 task:4
:01000017 cpu:0.031313 message:242 mqlen:0 task:2
<CMD OK>
使用版本回退功能检查什么时候空闲CPU占用. 回退出现官方原版时发现空服务也是要占用3~4%CPU
timer进程的心跳usleep(2500)
过于频繁, 如果改为10ms, 则空服务时, CPU占用快速下降为1%
为什么在KVM虚拟机上单核CPU在心跳为2500us时占用率2% , 而真实的I7 CPU时占用率却4~5%?, 使用
lscpu | grep MHz
可以看到当前CPU频率
Model name: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
CPU MHz: 799.987
CPU max MHz: 4000.0000
CPU min MHz: 800.0000
Model name: QEMU Virtual CPU version (cpu64-rhel6)
CPU MHz: 1999.999
BogoMIPS: 3999.99
可以看到虽然I7的最大主频很高, 但是当前是在低功耗模块运行, 即自动降频了. 而KVM系统反而运行计算任务速度比I7高.
另外也可以用dmidecode命令(结果可能不正确, 参考Stack Exchange中的解释, dmidecode结果来自主板).
sudo dmidecode -t processor
另外也可以使用lshw工具查看
> sudo yum install lshw
> sudo lshw -C processor
*-cpu
description: CPU
product: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
vendor: Intel Corp.
vendor_id: GenuineIntel
physical id: 41
bus info: cpu@0
version: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
serial: To Be Filled By O.E.M.
slot: U3E1
size: 3912MHz
capacity: 4GHz
width: 64 bits
clock: 100MHz
capabilities: lm fpu fpu_exception wp vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp x86-64 constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch intel_pt ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp spec_ctrl intel_stibp flush_l1d cpufreq
configuration: cores=4 enabledcores=4 threads=8
评论 (0)