所讨论的系统是一个运行CentOS的双CPU Xeon服务器,内存为256 GB:
2个英特尔(R)至强(R) CPU E5-2687W 0@3.10 CPU
每个CPU有8个核心,因此使用超线程时,系统有32个处理器显示在/proc/cpuinfo中。
在使用该系统的过程中,我注意到一些数据处理中出现了一些特殊的性能问题。数据处理系统是在Python3.3.5(使用Anaconda设置环境)中构建的,它产生了一系列进程,这些进程从文件中读取数据,创建一些numpy数组,然后进行一些处理。
我使用产生的不同数量的进程来测试处理。在一定数量的进程中,性能保持相对恒定。但是,当我达到16个进程时,我注意到numpy.abs()调用开始花费大约10倍的时间,从大约2秒增加到20秒或更多。
现在,在此测试中,总内存使用量不是问题。在256 GB的系统内存中,htop显示100+ GB空闲,而meminfo没有显示任何交换。
我运行了另一个测试,使用16个进程,但加载较少的数据,总内存使用约75 GB。在本例中,numpy.abs()调用花费了1秒(这是预期的,因为它是数据的一半)。在24个进程中,仍然使用了不到一半的系统ram,numpy.abs()调用同样花费了大约1秒。因此,我不再看到10倍的性能影响。
有趣的是,如果超过一半的系统内存被使用,性能就会严重下降。这似乎不应该是这样的,但我没有其他的解释。
我编写了一个Python脚本,它在某种程度上模拟了处理框架的功能。我尝试过各种派生进程的方法,multiprocessing.Pool apply_async()、concurrent.futures和多进程,它们都给出了相同的结果。
import pdb
import os
import sys
import time
import argparse
import numpy
import multiprocessing as mp
def worker(n):
print("Running worker", n)
NX = 20000
NY = 10000
time_start = time.time()
x1r = numpy.random.rand(NX,NY)
x1i = numpy.random.rand(NX,NY)
x1 = x1r + 1j * x1i
x1a = numpy.abs(x1)
print(time.time() - time_start)
def proc_file(nproc):
procs = {}
for i in range(0,nproc):
procs[i] = mp.Process(target = worker, args = (i, ))
procs[i].start()
for i in range(0,nproc):
procs[i].join()
if __name__ == "__main__":
time_start = time.time()
DEFAULT_NUM_PROCS = 8
ap = argparse.ArgumentParser()
ap.add_argument('-nproc', default = DEFAULT_NUM_PROCS, type = int,
help = "Number of cores to run in parallel, default = %d" \
% DEFAULT_NUM_PROCS)
opts = ap.parse_args()
nproc = opts.nproc
# spawn processes
proc_file(nproc)
time_end = time.time()
print('Done in', time_end - time_start, 's')不同进程数量的一些结果:
$ python test_multiproc_2.py -nproc 4
Running worker 0
Running worker 1
Running worker 2
Running worker 3
12.1790452003479
12.180120944976807
12.191224336624146
12.205029010772705
Done in 12.22369933128357 s
$ python test_multiproc_2.py -nproc 8
Running worker 0
Running worker 1
Running worker 2
Running worker 3
Running worker 4
Running worker 5
Running worker 6
Running worker 7
12.685678720474243
12.692482948303223
12.704699039459229
13.247581243515015
13.253047227859497
13.261905670166016
13.29712200164795
13.458561897277832
Done in 13.478906154632568 s
$ python test_multiproc_2.py -nproc 16
Running worker 0
Running worker 1
Running worker 2
Running worker 3
Running worker 4
Running worker 5
Running worker 6
Running worker 7
Running worker 8
Running worker 9
Running worker 10
Running worker 11
Running worker 12
Running worker 13
Running worker 14
Running worker 15
135.4193136692047
145.7047221660614
145.99714827537537
146.088121175766
146.3116044998169
146.94093680381775
147.05147790908813
147.4889578819275
147.8443088531494
147.92090320587158
148.32112169265747
148.35854578018188
149.11916518211365
149.22325253486633
149.45888781547546
149.74489760398865
Done in 149.97473335266113 s因此,4个进程和8个进程大致相同,但16个进程的速度要慢10倍!值得注意的是,在16进程的情况下,内存使用量达到146 GB。
如果我将numpy数组的大小减半,并再次运行它:
$ python test_multiproc_2.py -nproc 4
Running worker 1
Running worker 0
Running worker 2
Running worker 3
5.926755666732788
5.93787956237793
5.949704885482788
5.955750226974487
Done in 5.970340967178345 s
$ python test_multiproc_2.py -nproc 16
Running worker 1
Running worker 3
Running worker 0
Running worker 2
Running worker 5
Running worker 4
Running worker 7
Running worker 8
Running worker 6
Running worker 11
Running worker 9
Running worker 10
Running worker 13
Running worker 12
Running worker 14
Running worker 15
7.728739023208618
7.751606225967407
7.754587173461914
7.760802984237671
7.780809164047241
7.802706241607666
7.852390766143799
7.8615334033966064
7.876686096191406
7.891174793243408
7.916942834854126
7.9261558055877686
7.947092771530151
7.967057704925537
8.012752294540405
8.119316577911377
Done in 8.135530233383179 s因此,在16到4个进程之间会有一点性能影响,但与较大阵列的性能相差甚远。
另外,如果我将数组大小加倍并再次运行:
$ python test_multiproc_2.py -nproc 4
Running worker 1
Running worker 0
Running worker 2
Running worker 3
23.567795515060425
23.747386693954468
23.76904606819153
23.781703233718872
Done in 23.83848261833191 s
$ python test_multiproc_2.py -nproc 8
Running worker 1
Running worker 0
Running worker 3
Running worker 2
Running worker 5
Running worker 4
Running worker 6
Running worker 7
103.20905923843384
103.52968168258667
103.62282609939575
103.62272334098816
103.77079129219055
103.77456998825073
103.86126565933228
103.87058663368225
Done in 104.26257705688477 s现在有8个进程,RAM使用量达到145 GB,性能下降了5倍。
我不知道这是怎么回事。如果超过一半的系统内存正在使用,则系统基本上变得不可用。但是,我不知道这是否只是巧合,还有其他原因。
这是Python的东西吗?或者是系统架构的问题?每个物理CPU只使用一半的系统内存就能正常工作吗?或者是内存带宽问题?我还能做些什么来解决这个问题呢?
发布于 2017-08-01 01:27:56
解决这个问题的唯一方法是清除缓存内存。我运行了一个测试,当操作系统使用大约200 GB的缓存时,它只需要大约256 GB的内存。它花了很长时间,一旦操作系统开始释放缓存,它就开始崩溃。在运行这个测试之后,'free -m‘显示只有3 GB的缓存内存。我运行了相同的基准测试,它在预期的时间内运行,没有出现以前见过的CPU疯狂现象。性能在重复运行时保持不变。
因此,与我在网上读到的OS内存缓存不会影响应用程序性能的说法相反,我的经验告诉我,至少在这个特定的使用案例中,它会影响应用程序的性能。
发布于 2017-06-29 02:39:38
这是使用垃圾收集的语言的一个问题:如果您太接近最大RAM,它们就会一直尝试运行GC,从而导致CPU使用率的增加。
https://stackoverflow.com/questions/44810111
复制相似问题