cpu socket是什么,cpu dla 是什么意思

  

  1.背景初学者在学习围棋的时候,可能会在编写了经典的“hello,world”程序后,急于体验围棋强大的标准库。例如,用几行代码编写一个具有完整功能的web服务器,如下例所示:   

  

  来自:微信官方账号:TonyBai   

  

  参考:go语言的中文文档;www.topgoer.com   

  

  //from https://tip.golang.org/pkg/net/http/# example _ ListenerdServepackage main import(' io ' ' log ' ' net/http ')funcmain(){ hello handler :=func(w http。ResponseWriter,req *http。请求){ io。WriteString(w,'你好,世界!\ n')} http.handlefunc ('/hello ',hello handler)log . fatal(HTTP . listenandserve(' 33608080 ',nil))} Gonet/HTTP包是一个相对均衡的通用实现,可以满足90%以上的Gopher场景的需求,具有以下优点:   

  

  标准库包,不引入任何第三方依赖;对http规范满意;无需任何优化即可获得相对较高的性能;支持HTTP代理;HTTPS;支持;无缝支持HTTP/2。但正是由于http包的“均衡”通用实现,在一些对性能要求严格的领域,net/http的性能可能无法胜任,没有太大的调优空间。这时候我们就把目光转向其他第三方的http服务器框架的实现。   

  

  在第三方http server框架中,fasthttp1这种“顾名思义”的框架被提及并被大量采用。fasthttp官网宣称其性能是net/http的十倍(基于go测试基准的测试结果)。   

  

  Fast在性能优化方面采用了许多最佳实践2,尤其是在内存对象的重用方面。同步。Pool3被广泛用于减轻Go GC的压力。   

  

  那么在现实世界中fasthttp能比net/http快多少呢?碰巧有两台性能良好的服务器可用。在本文中,我们将看看他们在这个真实环境中的实际表现。   

  

  2.性能测试我们使用net/http和fasthttp实现了两个几乎“零业务”测试的程序:   

  

  net http://github . com/big white/experiments/blob/master/http-benchmark/net http/main . go package main import(_ ' exp var ' ' log ' ' net/http ' _ ' net/http/Pprof ' ' runtime ' ' time ')funcmain(){ go func(){ for { log . println('当前例程数为3360 ',运行时。NumGoroutine())时间。睡眠(时间。秒)} }() http。句柄('/'),http。HandlerFunc(func(w http。ResponseWriter。Request) { w.Write(byte('Hello,Go!'))}))日志。致命(http。ListenAndServe(':8080 ',nil))} fast http://github . com/big white/experiments/blob/master/http-benchmark/fast http/main . go package main import(' fmt ' ' log ' ' net/http ' ' runtime ' ' time ' _ ' exp var ' _ ' net/http/pprof ' ' github . com/valya la/fast http ')type hellog handler struct { } func fasttphandler(CTX * fasttp。请求Ctx) { fmt。Fprintln(ctx,'你好,去吧!')} func main(){ go func(){ http . listenandserve(' 33606060 ',nil)}()go func(){ for { log . println('当前例程数为3360 ',runtime . numgoroutine())time . sleep(time . second)} }()s :=fast http . server { handler 3360 fast httphandler,} S. ListenerServer ('33608081')}是   

被测目标实施压力测试的客户端,我们基于hey<4>这个http压测工具进行,为了方便调整压力水平,我们将hey“包裹”在下面这个shell脚本中(仅适于在linux上运行):

  

// github.com/bigwhite/experiments/blob/master/http-benchmark/client/http_client_load.sh# ./http_client_load.sh 3 10000 10 GET http://10.10.195.181:8080echo "$0 task_num count_per_hey conn_per_hey method url"task_num=$1count_per_hey=$2conn_per_hey=$3method=$4url=$5start=$(date +%s%N)for((i=1; i<=$task_num; i++)); do { tm=$(date +%T.%N) echo "$tm: task $i start" hey -n $count_per_hey -c $conn_per_hey -m $method $url > hey_$i.log tm=$(date +%T.%N) echo "$tm: task $i done"} & donewaitend=$(date +%s%N)count=$(( $task_num * $count_per_hey ))runtime_ns=$(( $end - $start ))runtime=`echo "scale=2; $runtime_ns / 1000000000" | bc`echo "runtime: "$runtimespeed=`echo "scale=2; $count / $runtime" | bc`echo "speed: "$speed 该脚本的执行示例如下:

  

bash http_client_load.sh 8 1000000 200 GET http://10.10.195.134:8080http_client_load.sh task_num count_per_hey conn_per_hey method url16:58:09.146948690: task 1 start16:58:09.147235080: task 2 start16:58:09.147290430: task 3 start16:58:09.147740230: task 4 start16:58:09.147896010: task 5 start16:58:09.148314900: task 6 start16:58:09.148446030: task 7 start16:58:09.148930840: task 8 start16:58:45.001080740: task 3 done16:58:45.241903500: task 8 done16:58:45.261501940: task 1 done16:58:50.032383770: task 4 done16:58:50.985076450: task 7 done16:58:51.269099430: task 5 done16:58:52.008164010: task 6 done16:58:52.166402430: task 2 doneruntime: 43.02speed: 185960.01从传入的参数来看,该脚本并行启动了8个task(一个task启动一个hey),每个task向http://10.10.195.134:8080建立200个并发连接,并发送100w http GET请求。
我们使用两台服务器分别放置被测目标程序和压力工具脚本:

  

目标程序所在服务器:10.10.195.181(物理机,Intel x86-64 CPU,40核,128G内存, CentOs 7.6)$ cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core) $ lscpuArchitecture: x86_64CPU op-mode(s): 32-bit, 64-bitByte Order: Little EndianCPU(s): 40On-line CPU(s) list: 0-39Thread(s) per core: 2Core(s) per socket: 10座: 2NUMA 节点: 2厂商 ID: GenuineIntelCPU 系列: 6型号: 85型号名称: Intel(R) Xeon(R) Silver 4114 CPU @ 2.20GHz步进: 4CPU MHz: 800.000CPU max MHz: 2201.0000CPU min MHz: 800.0000BogoMIPS: 4400.00虚拟化: VT-xL1d 缓存: 32KL1i 缓存: 32KL2 缓存: 1024KL3 缓存: 14080KNUMA 节点0 CPU: 0-9,20-29NUMA 节点1 CPU: 10-19,30-39Flags: fpu 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 lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_pt ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke spec_ctrl intel_stibp flush_l1d压力工具所在服务器:10.10.195.133(物理机,鲲鹏arm64 cpu,96核,80G内存, CentOs 7.9)# cat /etc/redhat-release CentOS Linux release 7.9.2009 (AltArch)# lscpuArchitecture: aarch64Byte Order: Little EndianCPU(s): 96On-line CPU(s) list: 0-95Thread(s) per core: 1Core(s) per socket: 48座: 2NUMA 节点: 4型号: 0CPU max MHz: 2600.0000CPU min MHz: 200.0000BogoMIPS: 200.00L1d 缓存: 64KL1i 缓存: 64KL2 缓存: 512KL3 缓存: 49152KNUMA 节点0 CPU: 0-23NUMA 节点1 CPU: 24-47NUMA 节点2 CPU: 48-71NUMA 节点3 CPU: 72-95Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jsc我用dstat监控被测目标所在主机资源占用情况(dstat -tcdngym),尤其是cpu负荷;通过expvarmon监控memstats<5>查看目标程序中对各类资源消耗情况的排名。

  

下面是多次测试后制作的一个数据表格:

  

  

图:测试数据

  

3. 对结果的简要分析受特定场景、测试工具及脚本精确性以及压力测试环境的影响,上面的测试结果有一定局限,但却真实反映了被测目标的性能趋势。我们看到在给予同样压力的情况下,fasthttp并没有10倍于net http的性能,甚至在这样一个特定的场景下,两倍于net/http的性能都没有达到:我们看到在目标主机cpu资源消耗接近70%的几个用例中,fasthttp的性能仅比net/http高出30%~70%左右。

  

那么为什么fasthttp的性能未及预期呢?要回答这个问题,那就要看看net/http和fasthttp各自的实现原理了!我们先来看看net/http的工作原理示意图:

  

  

图:nethttp工作原理示意图

  

http包作为server端的原理很简单,那就是accept到一个连接(conn)之后,将这个conn甩给一个worker goroutine去处理,后者一直存在,直到该conn的生命周期结束:即连接关闭。
下面是fasthttp的工作原理示意图:

  

  

图:fasthttp工作原理示意图

  

而fasthttp设计了一套机制,目的是尽量复用goroutine,而不是每次都创建新的goroutine。fasthttp的Server accept一个conn之后,会尝试从workerpool中的ready切片中取出一个channel,该channel与某个worker goroutine一一对应。一旦取出channel,就会将accept到的conn写到该channel里,而channel另一端的worker goroutine就会处理该conn上的数据读写。当处理完该conn后,该worker goroutine不会退出,而是会将自己对应的那个channel重新放回workerpool中的ready切片中,等待这下一次被取出。

  

fasthttp的goroutine复用策略初衷很好,但在这里的测试场景下效果不明显,从测试结果便可看得出来,在相同的客户端并发和压力下,net/http使用的goroutine数量与fasthttp相差无几。这是由测试模型导致的:在我们这个测试中,每个task中的hey都会向被测目标发起固定数量的长连接(keep-alive),然后在每条连接上发起“饱和”请求。这样fasthttp workerpool中的goroutine一旦接收到某个conn就只能在该conn上的通讯结束后才能重新放回,而该conn直到测试结束才会close,因此这样的场景相当于让fasthttp“退化”成了net/http的模型,也染上了net/http的“缺陷”:goroutine的数量一旦多起来,go runtime自身调度所带来的消耗便不可忽视甚至超过了业务处理所消耗的资源占比。下面分别是fasthttp在200长连接、8000长连接以及16000长连接下的cpu profile的结果:

  

200长连接:(pprof) top -cumShowing nodes accounting for 88.17s, 55.35% of 159.30s totalDropped 150 nodes (cum <= 0.80s)Showing top 10 nodes out of 60 flat flat% sum% cum cum% 0.46s 0.29% 0.29% 101.46s 63.69% github.com/valyala/fasthttp.(*Server).serveConn 0 0% 0.29% 101.46s 63.69% github.com/valyala/fasthttp.(*workerPool).getCh.func1 0 0% 0.29% 101.46s 63.69% github.com/valyala/fasthttp.(*workerPool).workerFunc 0.04s 0.025% 0.31% 89.46s 56.16% internal/poll.ignoringEINTRIO (inline) 87.38s 54.85% 55.17% 89.27s 56.04% syscall.Syscall 0.12s 0.075% 55.24% 60.39s 37.91% bufio.(*Writer).Flush 0 0% 55.24% 60.22s 37.80% net.(*conn).Write 0.08s 0.05% 55.29% 60.21s 37.80% net.(*netFD).Write 0.09s 0.056% 55.35% 60.12s 37.74% internal/poll.(*FD).Write 0 0% 55.35% 59.86s 37.58% syscall.Write (inline)(pprof) 8000长连接:(pprof) top -cumShowing nodes accounting for 108.51s, 54.46% of 199.23s totalDropped 204 nodes (cum <= 1s)Showing top 10 nodes out of 66 flat flat% sum% cum cum% 0 0% 0% 119.11s 59.79% github.com/valyala/fasthttp.(*workerPool).getCh.func1 0 0% 0% 119.11s 59.79% github.com/valyala/fasthttp.(*workerPool).workerFunc 0.69s 0.35% 0.35% 119.05s 59.76% github.com/valyala/fasthttp.(*Server).serveConn 0.04s 0.02% 0.37% 104.22s 52.31% internal/poll.ignoringEINTRIO (inline) 101.58s 50.99% 51.35% 103.95s 52.18% syscall.Syscall 0.10s 0.05% 51.40% 79.95s 40.13% runtime.mcall 0.06s 0.03% 51.43% 79.85s 40.08% runtime.park_m 0.23s 0.12% 51.55% 79.30s 39.80% runtime.schedule 5.67s 2.85% 54.39% 77.47s 38.88% runtime.findrunnable 0.14s 0.07% 54.46% 68.96s 34.61% bufio.(*Writer).Flush16000长连接:(pprof) top -cumShowing nodes accounting for 239.60s, 87.07% of 275.17s totalDropped 190 nodes (cum <= 1.38s)Showing top 10 nodes out of 46 flat flat% sum% cum cum% 0.04s 0.015% 0.015% 153.38s 55.74% runtime.mcall 0.01s 0.0036% 0.018% 153.34s 55.73% runtime.park_m 0.12s 0.044% 0.062% 153s 55.60% runtime.schedule 0.66s 0.24% 0.3% 152.66s 55.48% runtime.findrunnable 0.15s 0.055% 0.36% 127.53s 46.35% runtime.netpoll 127.04s 46.17% 46.52% 127.04s 46.17% runtime.epollwait 0 0% 46.52% 121s 43.97% github.com/valyala/fasthttp.(*workerPool).getCh.func1 0 0% 46.52% 121s 43.97% github.com/valyala/fasthttp.(*workerPool).workerFunc 0.41s 0.15% 46.67% 120.18s 43.67% github.com/valyala/fasthttp.(*Server).serveConn 111.17s 40.40% 87.07% 111.99s 40.70% syscall.Syscall(pprof) 通过上述profile的比对,我们发现当长连接数量增多时(即workerpool中goroutine数量增多时),go runtime调度的占比会逐渐提升,在16000连接时,runtime调度的各个函数已经排名前4了。

  

4. 优化途径从上面的测试结果,我们看到fasthttp的模型不太适合这种连接连上后进行持续“饱和”请求的场景,更适合短连接或长连接但没有持续饱和请求,在后面这样的场景下,它的goroutine复用模型才能更好的得以发挥。

  

但即便“退化”为了net/http模型,fasthttp的性能依然要比net/http略好,这是为什么呢?这些性能提升主要是fasthttp在内存分配层面的优化trick的结果,比如大量使用sync.Pool,比如避免在<>byte和string互转等。

  

那么,在持续“饱和”请求的场景下,如何让fasthttp workerpool中goroutine的数量不会因conn的增多而线性增长呢?fasthttp官方没有给出答案,但一条可以考虑的路径是使用os的多路复用(linux上的实现为epoll),即go runtime netpoll使用的那套机制。在多路复用的机制下,这样可以让每个workerpool中的goroutine处理同时处理多个连接,这样我们可以根据业务规模选择workerpool池的大小,而不是像目前这样几乎是任意增长goroutine的数量。当然,在用户层面引入epoll也可能会带来系统调用占比的增多以及响应延迟增大等问题。至于该路径是否可行,还是要看具体实现和测试结果。

  

注:fasthttp.Server中的Concurrency可以用来限制workerpool中并发处理的goroutine的个数,但由于每个goroutine只处理一个连接,当Concurrency设置过小时,后续的连接可能就会被fasthttp拒绝服务。因此fasthttp的默认Concurrency为:

  

const DefaultConcurrency = 256 * 1024

相关文章